fix: ws prefix url in electron (#1896)

This commit is contained in:
Peng Xiao
2023-04-12 22:11:47 +08:00
committed by GitHub
parent 25d7f7c848
commit 95aa86cdf0
17 changed files with 237 additions and 261 deletions

View File

@@ -1,3 +1,4 @@
import { websocketPrefixUrl } from '@affine/env';
import { KeckProvider } from '@affine/workspace/affine/keck';
import { getLoginStorage } from '@affine/workspace/affine/login';
import type { AffineWebSocketProvider } from '@affine/workspace/type';
@@ -19,11 +20,8 @@ const createAffineWebSocketProvider = (
webSocketProvider = null;
},
connect: () => {
const wsUrl = `${
window.location.protocol === 'https:' ? 'wss' : 'ws'
}://${window.location.host}/api/sync/`;
webSocketProvider = new KeckProvider(
wsUrl,
websocketPrefixUrl + '/api/sync/',
blockSuiteWorkspace.id,
blockSuiteWorkspace.doc,
{

View File

@@ -1,3 +1,4 @@
import { prefixUrl } from '@affine/env';
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
import {
clearLoginStorage,
@@ -24,7 +25,7 @@ import { PageDetailEditor } from '../../components/page-detail-editor';
import { useAffineRefreshAuthToken } from '../../hooks/affine/use-affine-refresh-auth-token';
import { AffineSWRConfigProvider } from '../../providers/AffineSWRConfigProvider';
import { BlockSuiteWorkspace } from '../../shared';
import { affineApis, prefixUrl } from '../../shared/apis';
import { affineApis } from '../../shared/apis';
import { initPage, toast } from '../../utils';
import type { WorkspacePlugin } from '..';
import { QueryKey } from './fetcher';

View File

@@ -1,5 +1,5 @@
import { DebugLogger } from '@affine/debug';
import { config } from '@affine/env';
import { prefixUrl } from '@affine/env';
import {
createUserApis,
createWorkspaceApis,
@@ -9,26 +9,6 @@ import type { LoginResponse } from '@affine/workspace/affine/login';
import { parseIdToken, setLoginStorage } from '@affine/workspace/affine/login';
import { jotaiStore } from '@affine/workspace/atom';
import { isValidIPAddress } from '../utils';
let prefixUrl = '/';
if (typeof window === 'undefined' || environment.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);
params.get('prefixUrl') && (prefixUrl = params.get('prefixUrl') as string);
}
export { prefixUrl };
const affineApis = {} as ReturnType<typeof createUserApis> &
ReturnType<typeof createWorkspaceApis>;
Object.assign(affineApis, createUserApis(prefixUrl));

View File

@@ -1,25 +0,0 @@
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);
});
});
});

View File

@@ -1,5 +1,4 @@
export * from './blocksuite';
export * from './create-emotion-cache';
export * from './is-valid-ip-address';
export * from './string2color';
export * from './toast';

View File

@@ -1,5 +0,0 @@
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
);
}