2026-05-07 16:56:29 +07:00
export const PURE_P2P_BROWSER_SETTING_KEY = '5chan:pure-p2p-browser-enabled' ;
2026-06-18 18:53:33 +07:00
export const BROWSER_PURE_P2P_DEFAULT_ENABLED = false ;
2026-05-07 16:56:29 +07:00
2026-06-18 14:18:14 +07:00
const BROWSER_PUBSUB_KUBO_RPC_CLIENTS_OPTIONS = [ 'https://pubsubprovider.xyz/api/v0' , 'https://plebpubsub.xyz/api/v0' , 'https://rannithepleb.com/api/v0' ];
2026-06-25 18:33:02 +07:00
// Keep this aligned with bitsocial-react-hooks' DEFAULT_HTTP_ROUTER_URLS without relying on a package-internal runtime import before window.defaultPkcOptions is configured.
const DEFAULT_HTTP_ROUTER_URLS = [
'https://peers.pleb.bot' ,
'https://routing.lol' ,
'https://peers.forumindex.com' ,
'https://peers.plebpubsub.xyz' ,
'https://routerofbitsocial.xyz' ,
'https://bsotracker.online' ,
];
2026-06-18 14:18:14 +07:00
2026-04-29 23:43:03 +07:00
export const P2P_BROWSER_PKC_OPTIONS = {
libp2pJsClientsOptions : [{ key : 'libp2pjs' }],
2026-06-17 23:09:39 +07:00
ipfsGatewayUrls : undefined ,
2026-05-07 16:56:29 +07:00
kuboRpcClientsOptions : undefined ,
pubsubHttpClientsOptions : undefined ,
2026-06-18 14:18:14 +07:00
pubsubKuboRpcClientsOptions : undefined as string [] | undefined ,
2026-06-25 18:33:02 +07:00
httpRoutersOptions : DEFAULT_HTTP_ROUTER_URLS ,
2026-04-29 23:43:03 +07:00
};
2026-05-07 16:56:29 +07:00
const GATEWAY_BROWSER_PKC_OPTIONS = {
ipfsGatewayUrls : [ 'https://ipfsgateway.xyz' , 'https://gateway.plebpubsub.xyz' , 'https://gateway.forumindex.com' ],
kuboRpcClientsOptions : undefined ,
libp2pJsClientsOptions : undefined ,
pubsubHttpClientsOptions : undefined ,
2026-06-18 14:18:14 +07:00
pubsubKuboRpcClientsOptions : BROWSER_PUBSUB_KUBO_RPC_CLIENTS_OPTIONS ,
2026-06-25 18:33:02 +07:00
httpRoutersOptions : DEFAULT_HTTP_ROUTER_URLS ,
2026-05-07 16:56:29 +07:00
};
2026-04-29 23:43:03 +07:00
type P2PBrowserConfigWindow = {
2026-05-11 17:18:16 +07:00
location? : Pick < Location , 'hostname' >;
2026-04-29 23:43:03 +07:00
defaultPkcOptions? : Record < string , unknown >;
2026-05-07 16:56:29 +07:00
electronApi ?: { isElectron? : boolean };
isElectron? : boolean ;
localStorage? : Pick < Storage , 'getItem' | 'setItem' >;
2026-04-29 23:43:03 +07:00
};
2026-06-18 14:18:14 +07:00
const cloneArray = < T >( value : T [] | undefined ) => ( value ? [... value ] : undefined );
2026-05-07 16:56:29 +07:00
export const getBrowserPureP2PPkcOptions = () => ({
... P2P_BROWSER_PKC_OPTIONS ,
libp2pJsClientsOptions : P2P_BROWSER_PKC_OPTIONS.libp2pJsClientsOptions.map (( options ) => ({ ... options })),
2026-06-18 14:18:14 +07:00
pubsubKuboRpcClientsOptions : cloneArray ( P2P_BROWSER_PKC_OPTIONS . pubsubKuboRpcClientsOptions ),
2026-05-07 16:56:29 +07:00
httpRoutersOptions : [... P2P_BROWSER_PKC_OPTIONS . httpRoutersOptions ],
});
export const getBrowserGatewayPkcOptions = () => ({
... GATEWAY_BROWSER_PKC_OPTIONS ,
ipfsGatewayUrls : [... GATEWAY_BROWSER_PKC_OPTIONS . ipfsGatewayUrls ],
pubsubKuboRpcClientsOptions : [... GATEWAY_BROWSER_PKC_OPTIONS . pubsubKuboRpcClientsOptions ],
httpRoutersOptions : [... GATEWAY_BROWSER_PKC_OPTIONS . httpRoutersOptions ],
});
export const getPureP2PBrowserPreference = ( targetWindow : P2PBrowserConfigWindow = window ) => {
try {
const storedValue = targetWindow . localStorage ? . getItem ( PURE_P2P_BROWSER_SETTING_KEY );
if ( storedValue === 'true' ) return true ;
if ( storedValue === 'false' ) return false ;
} catch {
return undefined ;
}
return undefined ;
};
export const setPureP2PBrowserPreference = ( enabled : boolean , targetWindow : P2PBrowserConfigWindow = window ) => {
try {
targetWindow . localStorage ? . setItem ( PURE_P2P_BROWSER_SETTING_KEY , String ( enabled ));
} catch {
return ;
}
};
export const isElectronRuntime = ( targetWindow : P2PBrowserConfigWindow = window ) => targetWindow . electronApi ? . isElectron === true || targetWindow . isElectron === true ;
2026-06-17 23:09:39 +07:00
export const canUsePureP2PBrowser = ( targetWindow : P2PBrowserConfigWindow = window ) => ! isElectronRuntime ( targetWindow );
2026-05-07 16:56:29 +07:00
export const shouldUsePureP2PBrowser = ( targetWindow : P2PBrowserConfigWindow = window ) => {
2026-06-17 23:09:39 +07:00
if ( ! canUsePureP2PBrowser ( targetWindow )) return false ;
2026-05-07 16:56:29 +07:00
const preference = getPureP2PBrowserPreference ( targetWindow );
if ( preference !== undefined ) return preference ;
2026-06-17 23:09:39 +07:00
return BROWSER_PURE_P2P_DEFAULT_ENABLED ;
2026-05-07 16:56:29 +07:00
};
2026-04-29 23:43:03 +07:00
export const configureP2PBrowserPkcOptions = ( targetWindow : P2PBrowserConfigWindow = window ) => {
2026-05-07 16:56:29 +07:00
if ( ! shouldUsePureP2PBrowser ( targetWindow )) {
2026-06-17 23:09:39 +07:00
if ( canUsePureP2PBrowser ( targetWindow )) {
targetWindow . defaultPkcOptions = {
... targetWindow . defaultPkcOptions ,
... getBrowserGatewayPkcOptions (),
};
}
2026-04-29 23:43:03 +07:00
return false ;
}
targetWindow . defaultPkcOptions = {
... targetWindow . defaultPkcOptions ,
2026-05-07 16:56:29 +07:00
... getBrowserPureP2PPkcOptions (),
2026-04-29 23:43:03 +07:00
};
return true ;
};