mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix: ws prefix url in electron (#1896)
This commit is contained in:
25
packages/env/src/__tests__/is-valid-ip-address.spec.ts
vendored
Normal file
25
packages/env/src/__tests__/is-valid-ip-address.spec.ts
vendored
Normal file
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
28
packages/env/src/api.ts
vendored
Normal file
28
packages/env/src/api.ts
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
import { config, getEnvironment } from './config';
|
||||
import { isValidIPAddress } from './is-valid-ip-address';
|
||||
|
||||
let prefixUrl = '/';
|
||||
if (typeof window === 'undefined' || getEnvironment().isDesktop) {
|
||||
// SSR or Desktop
|
||||
const serverAPI = config.serverAPI;
|
||||
if (isValidIPAddress(serverAPI.split(':')[0])) {
|
||||
// This is for Server side rendering support
|
||||
prefixUrl = new URL('http://' + config.serverAPI + '/').origin;
|
||||
} else {
|
||||
prefixUrl = serverAPI;
|
||||
}
|
||||
prefixUrl = prefixUrl.endsWith('/') ? prefixUrl : prefixUrl + '/';
|
||||
} else {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
if (params.get('prefixUrl')) {
|
||||
prefixUrl = params.get('prefixUrl') as string;
|
||||
} else {
|
||||
prefixUrl = window.location.origin + '/';
|
||||
}
|
||||
}
|
||||
|
||||
const apiUrl = new URL(prefixUrl);
|
||||
const wsProtocol = apiUrl.protocol === 'https:' ? 'wss' : 'ws';
|
||||
const websocketPrefixUrl = `${wsProtocol}://${apiUrl.host}`;
|
||||
|
||||
export { prefixUrl, websocketPrefixUrl };
|
||||
174
packages/env/src/config.ts
vendored
Normal file
174
packages/env/src/config.ts
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
import { assertEquals } from '@blocksuite/global/utils';
|
||||
import getConfig from 'next/config';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { getUaHelper } from './ua-helper';
|
||||
|
||||
export const publicRuntimeConfigSchema = z.object({
|
||||
PROJECT_NAME: z.string(),
|
||||
BUILD_DATE: z.string(),
|
||||
gitVersion: z.string(),
|
||||
hash: z.string(),
|
||||
serverAPI: z.string(),
|
||||
editorVersion: z.string(),
|
||||
enableIndexedDBProvider: z.boolean(),
|
||||
enableBroadCastChannelProvider: z.boolean(),
|
||||
prefetchWorkspace: z.boolean(),
|
||||
enableDebugPage: z.boolean(),
|
||||
// expose internal api to globalThis, **development only**
|
||||
exposeInternal: z.boolean(),
|
||||
enableSubpage: z.boolean(),
|
||||
enableChangeLog: z.boolean(),
|
||||
});
|
||||
|
||||
export type PublicRuntimeConfig = z.infer<typeof publicRuntimeConfigSchema>;
|
||||
|
||||
const { publicRuntimeConfig: config } =
|
||||
getConfig() ??
|
||||
({
|
||||
publicRuntimeConfig: {},
|
||||
} as {
|
||||
publicRuntimeConfig: PublicRuntimeConfig;
|
||||
});
|
||||
|
||||
publicRuntimeConfigSchema.parse(config);
|
||||
|
||||
type BrowserBase = {
|
||||
/**
|
||||
* @example https://app.affine.pro
|
||||
* @example http://localhost:3000
|
||||
*/
|
||||
origin: string;
|
||||
isDesktop: boolean;
|
||||
isBrowser: true;
|
||||
isServer: false;
|
||||
isDebug: boolean;
|
||||
|
||||
// browser special properties
|
||||
isLinux: boolean;
|
||||
isMacOs: boolean;
|
||||
isIOS: boolean;
|
||||
isSafari: boolean;
|
||||
isWindows: boolean;
|
||||
isFireFox: boolean;
|
||||
isMobile: boolean;
|
||||
isChrome: boolean;
|
||||
};
|
||||
|
||||
type NonChromeBrowser = BrowserBase & {
|
||||
isChrome: false;
|
||||
};
|
||||
|
||||
type ChromeBrowser = BrowserBase & {
|
||||
isSafari: false;
|
||||
isFireFox: false;
|
||||
isChrome: true;
|
||||
chromeVersion: number;
|
||||
};
|
||||
|
||||
type Browser = NonChromeBrowser | ChromeBrowser;
|
||||
|
||||
type Server = {
|
||||
isDesktop: false;
|
||||
isBrowser: false;
|
||||
isServer: true;
|
||||
isDebug: boolean;
|
||||
};
|
||||
|
||||
interface Desktop extends ChromeBrowser {
|
||||
isDesktop: true;
|
||||
isBrowser: true;
|
||||
isServer: false;
|
||||
isDebug: boolean;
|
||||
}
|
||||
|
||||
export type Environment = Browser | Server | Desktop;
|
||||
|
||||
let environment: Environment | null = null;
|
||||
|
||||
export function getEnvironment() {
|
||||
if (environment) {
|
||||
return environment;
|
||||
}
|
||||
const isDebug = process.env.NODE_ENV === 'development';
|
||||
if (typeof window === 'undefined') {
|
||||
environment = {
|
||||
isDesktop: false,
|
||||
isBrowser: false,
|
||||
isServer: true,
|
||||
isDebug,
|
||||
} satisfies Server;
|
||||
} else {
|
||||
const uaHelper = getUaHelper();
|
||||
|
||||
environment = {
|
||||
origin: window.location.origin,
|
||||
isDesktop: window.appInfo?.electron,
|
||||
isBrowser: true,
|
||||
isServer: false,
|
||||
isDebug,
|
||||
isLinux: uaHelper.isLinux,
|
||||
isMacOs: uaHelper.isMacOs,
|
||||
isSafari: uaHelper.isSafari,
|
||||
isWindows: uaHelper.isWindows,
|
||||
isFireFox: uaHelper.isFireFox,
|
||||
isMobile: uaHelper.isMobile,
|
||||
isChrome: uaHelper.isChrome,
|
||||
isIOS: uaHelper.isIOS,
|
||||
} as Browser;
|
||||
// Chrome on iOS is still Safari
|
||||
if (environment.isChrome && !environment.isIOS) {
|
||||
assertEquals(environment.isSafari, false);
|
||||
assertEquals(environment.isFireFox, false);
|
||||
environment = {
|
||||
...environment,
|
||||
isSafari: false,
|
||||
isFireFox: false,
|
||||
isChrome: true,
|
||||
chromeVersion: uaHelper.getChromeVersion(),
|
||||
} satisfies ChromeBrowser;
|
||||
}
|
||||
}
|
||||
globalThis.environment = environment;
|
||||
return environment;
|
||||
}
|
||||
|
||||
function printBuildInfo() {
|
||||
console.group('Build info');
|
||||
console.log('Project:', config.PROJECT_NAME);
|
||||
console.log(
|
||||
'Build date:',
|
||||
config.BUILD_DATE ? new Date(config.BUILD_DATE).toLocaleString() : 'Unknown'
|
||||
);
|
||||
console.log('Editor Version:', config.editorVersion);
|
||||
|
||||
console.log('Version:', config.gitVersion);
|
||||
console.log(
|
||||
'AFFiNE is an open source project, you can view its source code on GitHub!'
|
||||
);
|
||||
console.log(`https://github.com/toeverything/AFFiNE/tree/${config.hash}`);
|
||||
console.groupEnd();
|
||||
}
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var environment: Environment;
|
||||
// eslint-disable-next-line no-var
|
||||
var $AFFINE_SETUP: boolean | undefined;
|
||||
// eslint-disable-next-line no-var
|
||||
var editorVersion: string | undefined;
|
||||
}
|
||||
|
||||
export function setupGlobal() {
|
||||
if (globalThis.$AFFINE_SETUP) {
|
||||
return;
|
||||
}
|
||||
globalThis.environment = getEnvironment();
|
||||
if (getEnvironment().isBrowser) {
|
||||
printBuildInfo();
|
||||
globalThis.editorVersion = config.editorVersion;
|
||||
}
|
||||
globalThis.$AFFINE_SETUP = true;
|
||||
}
|
||||
|
||||
export { config };
|
||||
176
packages/env/src/index.ts
vendored
176
packages/env/src/index.ts
vendored
@@ -1,175 +1,3 @@
|
||||
import { assertEquals } from '@blocksuite/global/utils';
|
||||
import getConfig from 'next/config';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { getUaHelper } from './ua-helper';
|
||||
|
||||
export const publicRuntimeConfigSchema = z.object({
|
||||
PROJECT_NAME: z.string(),
|
||||
BUILD_DATE: z.string(),
|
||||
gitVersion: z.string(),
|
||||
hash: z.string(),
|
||||
serverAPI: z.string(),
|
||||
editorVersion: z.string(),
|
||||
enableIndexedDBProvider: z.boolean(),
|
||||
enableBroadCastChannelProvider: z.boolean(),
|
||||
prefetchWorkspace: z.boolean(),
|
||||
enableDebugPage: z.boolean(),
|
||||
// expose internal api to globalThis, **development only**
|
||||
exposeInternal: z.boolean(),
|
||||
enableSubpage: z.boolean(),
|
||||
enableChangeLog: z.boolean(),
|
||||
});
|
||||
|
||||
export type PublicRuntimeConfig = z.infer<typeof publicRuntimeConfigSchema>;
|
||||
|
||||
const { publicRuntimeConfig: config } =
|
||||
getConfig() ??
|
||||
({
|
||||
publicRuntimeConfig: {},
|
||||
} as {
|
||||
publicRuntimeConfig: PublicRuntimeConfig;
|
||||
});
|
||||
|
||||
publicRuntimeConfigSchema.parse(config);
|
||||
|
||||
type BrowserBase = {
|
||||
/**
|
||||
* @example https://app.affine.pro
|
||||
* @example http://localhost:3000
|
||||
*/
|
||||
origin: string;
|
||||
isDesktop: boolean;
|
||||
isBrowser: true;
|
||||
isServer: false;
|
||||
isDebug: boolean;
|
||||
|
||||
// browser special properties
|
||||
isLinux: boolean;
|
||||
isMacOs: boolean;
|
||||
isIOS: boolean;
|
||||
isSafari: boolean;
|
||||
isWindows: boolean;
|
||||
isFireFox: boolean;
|
||||
isMobile: boolean;
|
||||
isChrome: boolean;
|
||||
};
|
||||
|
||||
type NonChromeBrowser = BrowserBase & {
|
||||
isChrome: false;
|
||||
};
|
||||
|
||||
type ChromeBrowser = BrowserBase & {
|
||||
isSafari: false;
|
||||
isFireFox: false;
|
||||
isChrome: true;
|
||||
chromeVersion: number;
|
||||
};
|
||||
|
||||
type Browser = NonChromeBrowser | ChromeBrowser;
|
||||
|
||||
type Server = {
|
||||
isDesktop: false;
|
||||
isBrowser: false;
|
||||
isServer: true;
|
||||
isDebug: boolean;
|
||||
};
|
||||
|
||||
interface Desktop extends ChromeBrowser {
|
||||
isDesktop: true;
|
||||
isBrowser: true;
|
||||
isServer: false;
|
||||
isDebug: boolean;
|
||||
}
|
||||
|
||||
export type Environment = Browser | Server | Desktop;
|
||||
|
||||
let environment: Environment | null = null;
|
||||
|
||||
export function getEnvironment() {
|
||||
if (environment) {
|
||||
return environment;
|
||||
}
|
||||
const isDebug = process.env.NODE_ENV === 'development';
|
||||
if (typeof window === 'undefined') {
|
||||
environment = {
|
||||
isDesktop: false,
|
||||
isBrowser: false,
|
||||
isServer: true,
|
||||
isDebug,
|
||||
} satisfies Server;
|
||||
} else {
|
||||
const uaHelper = getUaHelper();
|
||||
|
||||
environment = {
|
||||
origin: window.location.origin,
|
||||
isDesktop: window.appInfo?.electron,
|
||||
isBrowser: true,
|
||||
isServer: false,
|
||||
isDebug,
|
||||
isLinux: uaHelper.isLinux,
|
||||
isMacOs: uaHelper.isMacOs,
|
||||
isSafari: uaHelper.isSafari,
|
||||
isWindows: uaHelper.isWindows,
|
||||
isFireFox: uaHelper.isFireFox,
|
||||
isMobile: uaHelper.isMobile,
|
||||
isChrome: uaHelper.isChrome,
|
||||
isIOS: uaHelper.isIOS,
|
||||
} as Browser;
|
||||
// Chrome on iOS is still Safari
|
||||
if (environment.isChrome && !environment.isIOS) {
|
||||
assertEquals(environment.isSafari, false);
|
||||
assertEquals(environment.isFireFox, false);
|
||||
environment = {
|
||||
...environment,
|
||||
isSafari: false,
|
||||
isFireFox: false,
|
||||
isChrome: true,
|
||||
chromeVersion: uaHelper.getChromeVersion(),
|
||||
} satisfies ChromeBrowser;
|
||||
}
|
||||
}
|
||||
globalThis.environment = environment;
|
||||
return environment;
|
||||
}
|
||||
|
||||
function printBuildInfo() {
|
||||
console.group('Build info');
|
||||
console.log('Project:', config.PROJECT_NAME);
|
||||
console.log(
|
||||
'Build date:',
|
||||
config.BUILD_DATE ? new Date(config.BUILD_DATE).toLocaleString() : 'Unknown'
|
||||
);
|
||||
console.log('Editor Version:', config.editorVersion);
|
||||
|
||||
console.log('Version:', config.gitVersion);
|
||||
console.log(
|
||||
'AFFiNE is an open source project, you can view its source code on GitHub!'
|
||||
);
|
||||
console.log(`https://github.com/toeverything/AFFiNE/tree/${config.hash}`);
|
||||
console.groupEnd();
|
||||
}
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var environment: Environment;
|
||||
// eslint-disable-next-line no-var
|
||||
var $AFFINE_SETUP: boolean | undefined;
|
||||
// eslint-disable-next-line no-var
|
||||
var editorVersion: string | undefined;
|
||||
}
|
||||
|
||||
export function setupGlobal() {
|
||||
if (globalThis.$AFFINE_SETUP) {
|
||||
return;
|
||||
}
|
||||
globalThis.environment = getEnvironment();
|
||||
if (getEnvironment().isBrowser) {
|
||||
printBuildInfo();
|
||||
globalThis.editorVersion = config.editorVersion;
|
||||
}
|
||||
globalThis.$AFFINE_SETUP = true;
|
||||
}
|
||||
|
||||
export { config };
|
||||
export * from './api';
|
||||
export * from './config';
|
||||
export * from './constant';
|
||||
|
||||
5
packages/env/src/is-valid-ip-address.ts
vendored
Normal file
5
packages/env/src/is-valid-ip-address.ts
vendored
Normal file
@@ -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
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user