fix(pubsub): restore browser pure p2p publishing

This commit is contained in:
Tommaso Casaburi
2026-06-17 23:09:39 +07:00
parent d3772786ff
commit 1278cf42c0
19 changed files with 467 additions and 703 deletions
@@ -175,7 +175,7 @@ describe('SettingsModal', () => {
expect(container.querySelector('[data-testid="p2p-stats-settings-panel"]')).toBeNull();
});
it('opens the p2p stats section from its hash', () => {
it('opens the p2p stats section from its hash when browser pure p2p is enabled', () => {
render('/all/settings#p2p-stats-settings');
expect(container.querySelector('[data-testid="p2p-stats-settings-panel"]')).not.toBeNull();
@@ -81,12 +81,6 @@ const clickButton = async (text: string) => {
});
};
const getSaveAdvancedSettingsButton = () => {
const button = Array.from(container.querySelectorAll('button')).find((candidate) => candidate.textContent === 'save_advanced_settings');
expect(button).toBeTruthy();
return button as HTMLButtonElement;
};
const setTestHostname = (hostname: string) => {
Object.defineProperty(window, 'location', {
configurable: true,
@@ -210,38 +204,30 @@ describe('AdvancedSettings', () => {
expect(textInputs[2]?.value).toBe('/tmp/connected-node');
});
it('hides gateway mode settings while browser pure p2p is enabled', async () => {
it('shows pure p2p browser mode checked by default', async () => {
await renderSettings(false);
expect(container.textContent).not.toContain('advanced_ipfs_gateways');
expect(container.textContent).not.toContain('advanced_pubsub_providers');
expect(container.textContent).toContain('advanced_http_routers');
expect(container.textContent).toContain('advanced_full_node_websocket_rpc');
const checkbox = container.querySelector<HTMLInputElement>('input[type="checkbox"]');
expect(checkbox?.checked).toBe(true);
const nodeRpcInput = Array.from(container.querySelectorAll<HTMLInputElement>('input[type="text"]')).find(
(input) => input.placeholder === 'advanced_p2p_rpc_placeholder',
);
expect(nodeRpcInput).toBeTruthy();
const checkbox = container.querySelector<HTMLInputElement>('input[type="checkbox"]');
await act(async () => {
checkbox?.click();
});
expect(container.textContent).toContain('advanced_ipfs_gateways');
expect(container.textContent).toContain('advanced_pubsub_providers');
});
it('saves the browser pure p2p toggle through advanced settings', async () => {
it('saves browser pure p2p settings when the toggle is turned on', async () => {
localStorage.setItem('5chan:pure-p2p-browser-enabled', 'false');
await renderSettings(false);
const checkbox = container.querySelector<HTMLInputElement>('input[type="checkbox"]');
expect(checkbox?.checked).toBe(false);
expect(container.textContent).not.toContain('pure P2P:');
expect(checkbox?.closest('label')?.nextElementSibling?.textContent).toBe('enable_pure_p2p_tip');
expect(getSaveAdvancedSettingsButton().previousElementSibling).toBe(checkbox?.closest('div'));
await act(async () => {
checkbox?.click();
@@ -252,10 +238,10 @@ describe('AdvancedSettings', () => {
expect.objectContaining({
pkcOptions: expect.objectContaining({
httpRoutersOptions: ['https://router.old.example'],
ipfsGatewayUrls: [],
ipfsGatewayUrls: undefined,
libp2pJsClientsOptions: [{ key: 'libp2pjs' }],
pkcRpcClientsOptions: undefined,
pubsubKuboRpcClientsOptions: [],
pubsubKuboRpcClientsOptions: undefined,
}),
}),
);
@@ -263,7 +249,42 @@ describe('AdvancedSettings', () => {
expect(reloadMock).toHaveBeenCalledOnce();
});
it('allows browser pure p2p to be disabled on p2p subdomains', async () => {
it('saves gateway defaults when the browser pure p2p setting is turned off', async () => {
localStorage.setItem('5chan:pure-p2p-browser-enabled', 'true');
testState.account = {
mediaIpfsGatewayUrl: 'https://media.old.example',
pkcOptions: {
httpRoutersOptions: ['https://peers.pleb.bot'],
libp2pJsClientsOptions: [{ key: 'libp2pjs' }],
},
};
await renderSettings(false);
const checkbox = container.querySelector<HTMLInputElement>('input[type="checkbox"]');
expect(checkbox?.checked).toBe(true);
await act(async () => {
checkbox?.click();
});
await clickButton('save_advanced_settings');
expect(testState.setAccountMock).toHaveBeenCalledWith(
expect.objectContaining({
pkcOptions: expect.objectContaining({
httpRoutersOptions: ['https://peers.pleb.bot'],
ipfsGatewayUrls: ['https://ipfsgateway.xyz', 'https://gateway.plebpubsub.xyz', 'https://gateway.forumindex.com'],
libp2pJsClientsOptions: undefined,
pkcRpcClientsOptions: undefined,
pubsubKuboRpcClientsOptions: ['https://pubsubprovider.xyz/api/v0', 'https://plebpubsub.xyz/api/v0', 'https://rannithepleb.com/api/v0'],
}),
}),
);
expect(localStorage.getItem('5chan:pure-p2p-browser-enabled')).toBe('false');
expect(reloadMock).toHaveBeenCalledOnce();
});
it('preserves custom browser gateway providers while pure p2p is unavailable', async () => {
localStorage.setItem('5chan:pure-p2p-browser-enabled', 'false');
setTestHostname('p2p.5chan.app');
@@ -271,7 +292,6 @@ describe('AdvancedSettings', () => {
const checkbox = container.querySelector<HTMLInputElement>('input[type="checkbox"]');
expect(checkbox?.checked).toBe(false);
expect(checkbox?.disabled).toBe(false);
await clickButton('save_advanced_settings');
@@ -288,7 +308,7 @@ describe('AdvancedSettings', () => {
expect(localStorage.getItem('5chan:pure-p2p-browser-enabled')).toBe('false');
});
it('saves gateway mode defaults when browser pure p2p is disabled', async () => {
it('saves gateway mode defaults when browser pure p2p settings have no gateway endpoints', async () => {
testState.account = {
mediaIpfsGatewayUrl: 'https://media.old.example',
pkcOptions: {
@@ -248,6 +248,23 @@ const getTrimmedLines = (value: string | undefined): string[] | undefined => {
}, []);
};
const applyBrowserGatewayPkcOptions = (
pkcOptions: AccountProtocolOptions,
ipfsGatewayUrls: string[] | undefined,
pubsubKuboRpcClientsOptions: string[] | undefined,
httpRoutersOptions: string[] | undefined,
) => {
const gatewayOptions = getBrowserGatewayPkcOptions();
return {
...pkcOptions,
...gatewayOptions,
ipfsGatewayUrls: ipfsGatewayUrls?.length ? ipfsGatewayUrls : gatewayOptions.ipfsGatewayUrls,
pubsubKuboRpcClientsOptions: pubsubKuboRpcClientsOptions?.length ? pubsubKuboRpcClientsOptions : gatewayOptions.pubsubKuboRpcClientsOptions,
httpRoutersOptions: httpRoutersOptions?.length ? httpRoutersOptions : gatewayOptions.httpRoutersOptions,
};
};
const AdvancedSettings = () => {
const { t } = useTranslation();
const account = useAccount() as AccountShape | undefined;
@@ -305,14 +322,7 @@ const AdvancedSettings = () => {
pkcRpcClientsOptions: undefined,
};
} else {
const gatewayOptions = getBrowserGatewayPkcOptions();
pkcOptions = {
...pkcOptions,
...gatewayOptions,
ipfsGatewayUrls: ipfsGatewayUrls?.length ? ipfsGatewayUrls : gatewayOptions.ipfsGatewayUrls,
pubsubKuboRpcClientsOptions: pubsubKuboRpcClientsOptions?.length ? pubsubKuboRpcClientsOptions : gatewayOptions.pubsubKuboRpcClientsOptions,
httpRoutersOptions: httpRoutersOptions?.length ? httpRoutersOptions : gatewayOptions.httpRoutersOptions,
};
pkcOptions = applyBrowserGatewayPkcOptions(pkcOptions, ipfsGatewayUrls, pubsubKuboRpcClientsOptions, httpRoutersOptions);
}
}
-7
View File
@@ -1,12 +1,5 @@
{
"entries": [
{
"id": "release-0.9.3",
"kind": "release",
"timestamp": 1781611200,
"message": "v0.9.3: More reliable browser posting",
"version": "0.9.3"
},
{
"id": "release-0.9.2",
"kind": "release",
@@ -102,7 +102,7 @@ describe('use-state-string', () => {
expect(latestValue).toBe('Resolving address, downloading board from peers');
});
it('formats browser p2p fallback publishing states as peer downloads', () => {
it('formats browser p2p fallback publishing states as peer downloads when pure p2p is enabled', () => {
localStorage.setItem('5chan:pure-p2p-browser-enabled', 'true');
act(() => {
@@ -144,7 +144,7 @@ describe('use-state-string', () => {
expect(latestValue).toBe('Downloading board via IPFS');
});
it('formats browser p2p single-board feed fallback states as peer downloads', () => {
it('formats browser p2p single-board feed fallback states as peer downloads when pure p2p is enabled', () => {
localStorage.setItem('5chan:pure-p2p-browser-enabled', 'true');
testState.community = {
state: 'updating',
+34 -24
View File
@@ -2,8 +2,8 @@ import { describe, expect, it } from 'vitest';
import {
configureP2PBrowserPkcOptions,
getBrowserGatewayPkcOptions,
getPureP2PBrowserPreference,
P2P_BROWSER_PKC_OPTIONS,
PURE_P2P_BROWSER_SETTING_KEY,
setPureP2PBrowserPreference,
shouldUsePureP2PBrowser,
@@ -17,36 +17,54 @@ const createStorage = (values: Record<string, string | undefined> = {}) => ({
});
describe('p2p-browser-config', () => {
const defaultHttpRouters = ['https://peers.plebpubsub.xyz', 'https://routing.lol', 'https://peers.pleb.bot'];
it('configures browser PKC options for pure p2p by default', () => {
const chainProviders = {
eth: { urls: ['https://eth.example'], chainId: 1 },
};
const targetWindow = {
location: { hostname: '5chan.app' },
localStorage: createStorage(),
defaultPkcOptions: {
chainProviders,
ipfsGatewayUrls: ['https://gateway.example'],
},
};
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(true);
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(true);
expect(targetWindow.defaultPkcOptions).toEqual(P2P_BROWSER_PKC_OPTIONS);
expect(targetWindow.defaultPkcOptions).toMatchObject({
chainProviders,
httpRoutersOptions: defaultHttpRouters,
ipfsGatewayUrls: undefined,
libp2pJsClientsOptions: [{ key: 'libp2pjs' }],
pubsubKuboRpcClientsOptions: undefined,
});
});
it('respects disabled pure p2p preference on p2p subdomains', () => {
const defaultPkcOptions = {
ipfsGatewayUrls: ['https://gateway.example'],
it('configures browser PKC options for gateway mode when pure p2p is explicitly disabled', () => {
const chainProviders = {
eth: { urls: ['https://eth.example'], chainId: 1 },
};
const targetWindow = {
location: { hostname: 'p2p.5chan.app' },
location: { hostname: '5chan.app' },
localStorage: createStorage({ [PURE_P2P_BROWSER_SETTING_KEY]: 'false' }),
defaultPkcOptions,
defaultPkcOptions: {
chainProviders,
ipfsGatewayUrls: ['https://gateway.example'],
},
};
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(false);
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(false);
expect(targetWindow.defaultPkcOptions).toBe(defaultPkcOptions);
expect(targetWindow.defaultPkcOptions).toEqual({
chainProviders,
...getBrowserGatewayPkcOptions(),
});
});
it('configures browser PKC options when pure p2p is enabled', () => {
it('configures browser PKC options when pure p2p is explicitly enabled', () => {
const targetWindow = {
location: { hostname: '5chan.app' },
localStorage: createStorage({ [PURE_P2P_BROWSER_SETTING_KEY]: 'true' }),
@@ -55,22 +73,14 @@ describe('p2p-browser-config', () => {
},
};
expect(shouldUsePureP2PBrowser(targetWindow)).toBe(true);
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(true);
expect(targetWindow.defaultPkcOptions).toEqual(P2P_BROWSER_PKC_OPTIONS);
});
it('leaves browser PKC options untouched when pure p2p is disabled', () => {
const defaultPkcOptions = {
ipfsGatewayUrls: ['https://gateway.example'],
};
const targetWindow = {
location: { hostname: '5chan.app' },
localStorage: createStorage({ [PURE_P2P_BROWSER_SETTING_KEY]: 'false' }),
defaultPkcOptions,
};
expect(configureP2PBrowserPkcOptions(targetWindow)).toBe(false);
expect(targetWindow.defaultPkcOptions).toBe(defaultPkcOptions);
expect(targetWindow.defaultPkcOptions).toMatchObject({
httpRoutersOptions: defaultHttpRouters,
ipfsGatewayUrls: undefined,
libp2pJsClientsOptions: [{ key: 'libp2pjs' }],
pubsubKuboRpcClientsOptions: undefined,
});
});
it('leaves electron defaults untouched', () => {
+19 -3
View File
@@ -27,6 +27,16 @@ const browserWindowWithDisabledPureP2P = {
},
} as unknown as Window;
const browserWindowWithEnabledPureP2P = {
electronApi: undefined,
isElectron: false,
location: { hostname: '5chan.app' },
localStorage: {
getItem: () => 'true',
setItem: () => undefined,
},
} as unknown as Window;
const electronWindow = {
electronApi: { isElectron: true },
isElectron: true,
@@ -56,10 +66,14 @@ describe('p2p-runtime', () => {
expect(getP2PRuntimeMode(account, browserWindow)).toBe('full-node-rpc');
});
it('shows p2p settings in browsers when pure p2p is enabled by default', () => {
it('keeps browser pure p2p on by default while allowing gateway mode when configured', () => {
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);
expect(shouldShowP2PSettingsSection({ pkcOptions: { libp2pJsClientsOptions: [{ key: 'libp2pjs' }] } }, browserWindow)).toBe(true);
expect(isBrowserPureP2PEnabled({ pkcOptions: { libp2pJsClientsOptions: [{ key: 'libp2pjs' }] } }, browserWindow)).toBe(true);
expect(shouldShowP2PSettingsSection({ pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } }, browserWindowWithEnabledPureP2P)).toBe(true);
expect(isBrowserPureP2PEnabled({ pkcOptions: { ipfsGatewayUrls: ['https://gateway.example'] } }, browserWindowWithEnabledPureP2P)).toBe(true);
});
it('allows browser gateway mode when pure p2p is disabled', () => {
@@ -88,13 +102,15 @@ describe('p2p-runtime', () => {
expect(getBrowserPureP2PAccountOptions(account)).toMatchObject({
libp2pJsClientsOptions: [{ key: 'libp2pjs' }],
ipfsGatewayUrls: [],
ipfsGatewayUrls: undefined,
pkcRpcClientsOptions: undefined,
});
expect(getBrowserGatewayAccountOptions(account)).toMatchObject({
ipfsGatewayUrls: ['https://ipfsgateway.xyz', 'https://gateway.plebpubsub.xyz', 'https://gateway.forumindex.com'],
httpRoutersOptions: ['https://custom-router.example'],
ipfsGatewayUrls: ['https://gateway.example'],
libp2pJsClientsOptions: undefined,
pkcRpcClientsOptions: undefined,
pubsubKuboRpcClientsOptions: ['https://pubsubprovider.xyz/api/v0', 'https://plebpubsub.xyz/api/v0', 'https://rannithepleb.com/api/v0'],
});
});
});
+15 -7
View File
@@ -1,12 +1,13 @@
export const PURE_P2P_BROWSER_SETTING_KEY = '5chan:pure-p2p-browser-enabled';
export const BROWSER_PURE_P2P_DEFAULT_ENABLED = true;
export const P2P_BROWSER_PKC_OPTIONS = {
libp2pJsClientsOptions: [{ key: 'libp2pjs' }],
ipfsGatewayUrls: [],
ipfsGatewayUrls: undefined,
kuboRpcClientsOptions: undefined,
pubsubHttpClientsOptions: undefined,
pubsubKuboRpcClientsOptions: [],
httpRoutersOptions: ['https://peers.pleb.bot', 'https://peers.forumindex.com'],
pubsubKuboRpcClientsOptions: undefined,
httpRoutersOptions: ['https://peers.plebpubsub.xyz', 'https://routing.lol', 'https://peers.pleb.bot'],
};
const GATEWAY_BROWSER_PKC_OPTIONS = {
@@ -29,8 +30,6 @@ type P2PBrowserConfigWindow = {
export const getBrowserPureP2PPkcOptions = () => ({
...P2P_BROWSER_PKC_OPTIONS,
libp2pJsClientsOptions: P2P_BROWSER_PKC_OPTIONS.libp2pJsClientsOptions.map((options) => ({ ...options })),
ipfsGatewayUrls: [...P2P_BROWSER_PKC_OPTIONS.ipfsGatewayUrls],
pubsubKuboRpcClientsOptions: [...P2P_BROWSER_PKC_OPTIONS.pubsubKuboRpcClientsOptions],
httpRoutersOptions: [...P2P_BROWSER_PKC_OPTIONS.httpRoutersOptions],
});
@@ -63,17 +62,26 @@ export const setPureP2PBrowserPreference = (enabled: boolean, targetWindow: P2PB
export const isElectronRuntime = (targetWindow: P2PBrowserConfigWindow = window) => targetWindow.electronApi?.isElectron === true || targetWindow.isElectron === true;
export const canUsePureP2PBrowser = (targetWindow: P2PBrowserConfigWindow = window) => !isElectronRuntime(targetWindow);
export const shouldUsePureP2PBrowser = (targetWindow: P2PBrowserConfigWindow = window) => {
if (isElectronRuntime(targetWindow)) return false;
if (!canUsePureP2PBrowser(targetWindow)) return false;
const preference = getPureP2PBrowserPreference(targetWindow);
if (preference !== undefined) return preference;
return true;
return BROWSER_PURE_P2P_DEFAULT_ENABLED;
};
export const configureP2PBrowserPkcOptions = (targetWindow: P2PBrowserConfigWindow = window) => {
if (!shouldUsePureP2PBrowser(targetWindow)) {
if (canUsePureP2PBrowser(targetWindow)) {
targetWindow.defaultPkcOptions = {
...targetWindow.defaultPkcOptions,
...getBrowserGatewayPkcOptions(),
};
}
return false;
}
+22 -9
View File
@@ -1,4 +1,4 @@
import { getBrowserGatewayPkcOptions, getBrowserPureP2PPkcOptions, isElectronRuntime, shouldUsePureP2PBrowser } from './p2p-browser-config';
import { canUsePureP2PBrowser, getBrowserGatewayPkcOptions, getBrowserPureP2PPkcOptions, isElectronRuntime, shouldUsePureP2PBrowser } from './p2p-browser-config';
export const P2P_STATS_SECTION_ID = 'p2p-stats-settings';
@@ -46,10 +46,13 @@ export const getP2PRuntimeMode = (account?: unknown, targetWindow: Window = wind
return null;
};
export const canConfigureBrowserPureP2P = (targetWindow: Window = window) => !isElectronRuntime(targetWindow);
export const canConfigureBrowserPureP2P = (targetWindow: Window = window) => canUsePureP2PBrowser(targetWindow);
export const shouldShowP2PSettingsSection = (account?: unknown, targetWindow: Window = window) =>
getP2PRuntimeMode(account, targetWindow) !== null || (canConfigureBrowserPureP2P(targetWindow) && isBrowserPureP2PEnabled(account, targetWindow));
export const shouldShowP2PSettingsSection = (account?: unknown, targetWindow: Window = window) => {
const runtimeMode = getP2PRuntimeMode(account, targetWindow);
if (runtimeMode === 'electron-kubo-rpc' || runtimeMode === 'full-node-rpc') return true;
return canConfigureBrowserPureP2P(targetWindow) && (runtimeMode === 'browser-libp2p' || isBrowserPureP2PEnabled(account, targetWindow));
};
export const isBrowserPureP2PEnabled = (account?: unknown, targetWindow: Window = window) => {
if (!canConfigureBrowserPureP2P(targetWindow)) return false;
@@ -63,8 +66,18 @@ export const getBrowserPureP2PAccountOptions = (account?: unknown) => ({
pkcRpcClientsOptions: undefined,
});
export const getBrowserGatewayAccountOptions = (account?: unknown) => ({
...toAccountShape(account)?.pkcOptions,
...getBrowserGatewayPkcOptions(),
pkcRpcClientsOptions: undefined,
});
export const getBrowserGatewayAccountOptions = (account?: unknown) => {
const protocolOptions = toAccountShape(account)?.pkcOptions;
const gatewayOptions = getBrowserGatewayPkcOptions();
return {
...protocolOptions,
...gatewayOptions,
ipfsGatewayUrls: hasArrayItems(protocolOptions?.ipfsGatewayUrls) ? protocolOptions?.ipfsGatewayUrls : gatewayOptions.ipfsGatewayUrls,
pubsubKuboRpcClientsOptions: hasArrayItems(protocolOptions?.pubsubKuboRpcClientsOptions)
? protocolOptions?.pubsubKuboRpcClientsOptions
: gatewayOptions.pubsubKuboRpcClientsOptions,
httpRoutersOptions: hasArrayItems(protocolOptions?.httpRoutersOptions) ? protocolOptions?.httpRoutersOptions : gatewayOptions.httpRoutersOptions,
pkcRpcClientsOptions: undefined,
};
};
+1 -1
View File
@@ -227,7 +227,7 @@ describe('Rules', () => {
expect(scrollIntoViewMock).toHaveBeenCalled();
});
it('shows a friendly loading state string while a board over P2P is downloading', async () => {
it('shows a friendly loading state string while a board is downloading from peers', async () => {
testState.communities = {
'custom-board.eth': {
state: 'fetching-ipns',