mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
feat(p2p): enable pure browser p2p by default
This commit is contained in:
@@ -3,8 +3,6 @@ import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
configureP2PBrowserPkcOptions,
|
||||
getPureP2PBrowserPreference,
|
||||
isP2PBrowserHostname,
|
||||
isPureP2PBrowserForced,
|
||||
P2P_BROWSER_PKC_OPTIONS,
|
||||
PURE_P2P_BROWSER_SETTING_KEY,
|
||||
setPureP2PBrowserPreference,
|
||||
@@ -19,14 +17,7 @@ const createStorage = (values: Record<string, string | undefined> = {}) => ({
|
||||
});
|
||||
|
||||
describe('p2p-browser-config', () => {
|
||||
it('detects p2p subdomains', () => {
|
||||
expect(isP2PBrowserHostname('p2p.5chan.app')).toBe(true);
|
||||
expect(isP2PBrowserHostname('P2P.5chan.app')).toBe(true);
|
||||
expect(isP2PBrowserHostname('5chan.app')).toBe(false);
|
||||
expect(isP2PBrowserHostname('www.p2p.5chan.app')).toBe(false);
|
||||
});
|
||||
|
||||
it('leaves browser PKC options untouched by default', () => {
|
||||
it('configures browser PKC options for pure p2p by default', () => {
|
||||
const targetWindow = {
|
||||
location: { hostname: '5chan.app' },
|
||||
localStorage: createStorage(),
|
||||
@@ -35,27 +26,26 @@ describe('p2p-browser-config', () => {
|
||||
},
|
||||
};
|
||||
|
||||
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(false);
|
||||
expect(targetWindow.defaultPkcOptions).toEqual({
|
||||
ipfsGatewayUrls: ['https://gateway.example'],
|
||||
});
|
||||
});
|
||||
|
||||
it('forces browser PKC options on p2p subdomains', () => {
|
||||
const targetWindow = {
|
||||
location: { hostname: 'p2p.5chan.app' },
|
||||
localStorage: createStorage({ [PURE_P2P_BROWSER_SETTING_KEY]: 'false' }),
|
||||
defaultPkcOptions: {
|
||||
ipfsGatewayUrls: ['https://gateway.example'],
|
||||
},
|
||||
};
|
||||
|
||||
expect(isPureP2PBrowserForced(targetWindow)).toBe(true);
|
||||
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(true);
|
||||
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(true);
|
||||
expect(targetWindow.defaultPkcOptions).toEqual(P2P_BROWSER_PKC_OPTIONS);
|
||||
});
|
||||
|
||||
it('respects disabled pure p2p preference on p2p subdomains', () => {
|
||||
const defaultPkcOptions = {
|
||||
ipfsGatewayUrls: ['https://gateway.example'],
|
||||
};
|
||||
const targetWindow = {
|
||||
location: { hostname: 'p2p.5chan.app' },
|
||||
localStorage: createStorage({ [PURE_P2P_BROWSER_SETTING_KEY]: 'false' }),
|
||||
defaultPkcOptions,
|
||||
};
|
||||
|
||||
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(false);
|
||||
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(false);
|
||||
expect(targetWindow.defaultPkcOptions).toBe(defaultPkcOptions);
|
||||
});
|
||||
|
||||
it('configures browser PKC options when pure p2p is enabled', () => {
|
||||
const targetWindow = {
|
||||
location: { hostname: '5chan.app' },
|
||||
@@ -96,7 +86,6 @@ describe('p2p-browser-config', () => {
|
||||
|
||||
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(false);
|
||||
expect(targetWindow.defaultPkcOptions).toBe(defaultPkcOptions);
|
||||
expect(isPureP2PBrowserForced({ ...targetWindow, location: { hostname: 'p2p.5chan.app' } })).toBe(false);
|
||||
});
|
||||
|
||||
it('persists and reads the browser pure p2p preference', () => {
|
||||
@@ -106,7 +95,7 @@ describe('p2p-browser-config', () => {
|
||||
};
|
||||
|
||||
expect(getPureP2PBrowserPreference(targetWindow)).toBeUndefined();
|
||||
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(false);
|
||||
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(true);
|
||||
|
||||
setPureP2PBrowserPreference(false, targetWindow);
|
||||
expect(getPureP2PBrowserPreference(targetWindow)).toBe(false);
|
||||
|
||||
@@ -17,13 +17,23 @@ const browserWindow = {
|
||||
},
|
||||
} as unknown as Window;
|
||||
|
||||
const browserWindowWithDisabledPureP2P = {
|
||||
electronApi: undefined,
|
||||
isElectron: false,
|
||||
location: { hostname: '5chan.app' },
|
||||
localStorage: {
|
||||
getItem: () => 'false',
|
||||
setItem: () => undefined,
|
||||
},
|
||||
} as unknown as Window;
|
||||
|
||||
const electronWindow = {
|
||||
electronApi: { isElectron: true },
|
||||
isElectron: true,
|
||||
location: { hostname: 'localhost' },
|
||||
} as unknown as Window;
|
||||
|
||||
const p2pBrowserWindow = {
|
||||
const p2pBrowserWindowWithDisabledPureP2P = {
|
||||
electronApi: undefined,
|
||||
isElectron: false,
|
||||
location: { hostname: 'p2p.5chan.app' },
|
||||
@@ -46,17 +56,18 @@ describe('p2p-runtime', () => {
|
||||
expect(getP2PRuntimeMode(account, browserWindow)).toBeNull();
|
||||
});
|
||||
|
||||
it('shows p2p settings in browsers only when pure p2p is enabled or active', () => {
|
||||
expect(shouldShowP2PSettingsSection(undefined, browserWindow)).toBe(false);
|
||||
expect(shouldShowP2PSettingsSection({ pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } }, browserWindow)).toBe(false);
|
||||
expect(isBrowserPureP2PEnabled({ pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } }, browserWindow)).toBe(false);
|
||||
it('shows p2p settings in browsers when pure p2p is enabled by default', () => {
|
||||
expect(shouldShowP2PSettingsSection(undefined, browserWindow)).toBe(true);
|
||||
expect(shouldShowP2PSettingsSection({ pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } }, browserWindow)).toBe(true);
|
||||
expect(isBrowserPureP2PEnabled({ pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } }, browserWindow)).toBe(true);
|
||||
});
|
||||
|
||||
it('forces browser p2p on p2p subdomains even with gateway account options', () => {
|
||||
it('allows browser gateway mode when pure p2p is disabled', () => {
|
||||
const gatewayAccount = { pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } };
|
||||
|
||||
expect(isBrowserPureP2PEnabled(gatewayAccount, p2pBrowserWindow)).toBe(true);
|
||||
expect(shouldShowP2PSettingsSection(gatewayAccount, p2pBrowserWindow)).toBe(true);
|
||||
expect(isBrowserPureP2PEnabled(gatewayAccount, browserWindowWithDisabledPureP2P)).toBe(false);
|
||||
expect(shouldShowP2PSettingsSection(gatewayAccount, browserWindowWithDisabledPureP2P)).toBe(false);
|
||||
expect(isBrowserPureP2PEnabled(gatewayAccount, p2pBrowserWindowWithDisabledPureP2P)).toBe(false);
|
||||
});
|
||||
|
||||
it('builds browser p2p and gateway account options without a direct pkc-js import', () => {
|
||||
|
||||
@@ -26,8 +26,6 @@ type P2PBrowserConfigWindow = {
|
||||
localStorage?: Pick<Storage, 'getItem' | 'setItem'>;
|
||||
};
|
||||
|
||||
export const isP2PBrowserHostname = (hostname: string) => hostname.toLowerCase().startsWith('p2p.');
|
||||
|
||||
export const getBrowserPureP2PPkcOptions = () => ({
|
||||
...P2P_BROWSER_PKC_OPTIONS,
|
||||
libp2pJsClientsOptions: P2P_BROWSER_PKC_OPTIONS.libp2pJsClientsOptions.map((options) => ({ ...options })),
|
||||
@@ -63,17 +61,13 @@ export const setPureP2PBrowserPreference = (enabled: boolean, targetWindow: P2PB
|
||||
|
||||
export const isElectronRuntime = (targetWindow: P2PBrowserConfigWindow = window) => targetWindow.electronApi?.isElectron === true || targetWindow.isElectron === true;
|
||||
|
||||
export const isPureP2PBrowserForced = (targetWindow: P2PBrowserConfigWindow = window) =>
|
||||
!isElectronRuntime(targetWindow) && isP2PBrowserHostname(targetWindow.location?.hostname ?? '');
|
||||
|
||||
export const shouldUsePureP2PBrowser = (targetWindow: P2PBrowserConfigWindow = window) => {
|
||||
if (isElectronRuntime(targetWindow)) return false;
|
||||
if (isPureP2PBrowserForced(targetWindow)) return true;
|
||||
|
||||
const preference = getPureP2PBrowserPreference(targetWindow);
|
||||
if (preference !== undefined) return preference;
|
||||
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
export const configureP2PBrowserPkcOptions = (targetWindow: P2PBrowserConfigWindow = window) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getBrowserGatewayPkcOptions, getBrowserPureP2PPkcOptions, isElectronRuntime, isPureP2PBrowserForced, shouldUsePureP2PBrowser } from './p2p-browser-config';
|
||||
import { getBrowserGatewayPkcOptions, getBrowserPureP2PPkcOptions, isElectronRuntime, shouldUsePureP2PBrowser } from './p2p-browser-config';
|
||||
|
||||
export const P2P_STATS_SECTION_ID = 'p2p-stats-settings';
|
||||
|
||||
@@ -52,11 +52,8 @@ export const shouldShowP2PSettingsSection = (account?: unknown, targetWindow: Wi
|
||||
getP2PRuntimeMode(account, targetWindow) !== null || (canConfigureBrowserPureP2P(targetWindow) && isBrowserPureP2PEnabled(account, targetWindow));
|
||||
|
||||
export const isBrowserPureP2PEnabled = (account?: unknown, targetWindow: Window = window) => {
|
||||
const accountShape = toAccountShape(account);
|
||||
if (!canConfigureBrowserPureP2P(targetWindow)) return false;
|
||||
if (getP2PRuntimeMode(account, targetWindow) === 'browser-libp2p') return true;
|
||||
if (isPureP2PBrowserForced(targetWindow)) return true;
|
||||
if (hasArrayItems(accountShape?.pkcOptions?.ipfsGatewayUrls) || hasArrayItems(accountShape?.pkcOptions?.pubsubKuboRpcClientsOptions)) return false;
|
||||
return shouldUsePureP2PBrowser(targetWindow);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user