Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat: add `is-valid-ip-address` (#1591)

Himself656ae06d560959
diff --git a/apps/web/src/shared/apis.ts b/apps/web/src/shared/apis.ts
index 313740c7c7..3c75725742 100644
--- a/apps/web/src/shared/apis.ts
+++ b/apps/web/src/shared/apis.ts
@@ -6,14 +6,21 @@ import {
} from '@affine/datacenter';
import { config } from '@affine/env';
+import { isValidIPAddress } from '../utils/is-valid-ip-address';
+
let prefixUrl = '/';
if (typeof window === 'undefined') {
// SSR
- if (config.serverAPI.startsWith('100')) {
+ const serverAPI = config.serverAPI;
+ if (isValidIPAddress(serverAPI)) {
// This is for Server side rendering support
prefixUrl = new URL('http://' + config.serverAPI + '/').origin;
} else {
- console.warn('serverAPI is not a valid URL', config.serverAPI);
+ try {
+ new URL(serverAPI);
+ } catch (e) {
+ console.warn('serverAPI is not a valid URL', config.serverAPI);
+ }
}
} else {
const params = new URLSearchParams(window.location.search);
diff --git a/apps/web/src/utils/__tests__/is-valid-ip-address.spec.ts b/apps/web/src/utils/__tests__/is-valid-ip-address.spec.ts
new file mode 100644
index 0000000000..c6076fd03e
--- /dev/null
+++ b/apps/web/src/utils/__tests__/is-valid-ip-address.spec.ts
@@ -0,0 +1,25 @@
+import { describe, expect, test } from 'vitest';
+
+import { isValidIPAddress } from '../is-valid-ip-address';
+
+describe('isValidIpAddress', () => {
+ test('should return true for valid IP address', () => {
+ ['115.42.150.37', '192.168.0.1', '110.234.52.124'].forEach(ip => {
+ expect(isValidIPAddress(ip)).toBe(true);
+ });
+ });
+
+ test('should return false for invalid IP address', () => {
+ [
+ '210.110',
+ '255',
+ 'y.y.y.y',
+ '255.0.0.y',
+ '666.10.10.20',
+ '4444.11.11.11',
+ '33.3333.33.3',
+ ].forEach(ip => {
+ expect(isValidIPAddress(ip)).toBe(false);
+ });
+ });
+});
diff --git a/apps/web/src/utils/is-valid-ip-address.ts b/apps/web/src/utils/is-valid-ip-address.ts
new file mode 100644
index 0000000000..643ef5f7fe
--- /dev/null
+++ b/apps/web/src/utils/is-valid-ip-address.ts
@@ -0,0 +1,5 @@
+export function isValidIPAddress(address: string) {
+ return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
+ address
+ );
+}