let booting = null;
async function bootGun(relayPort) {
if (globalThis.user && globalThis.gun) {
return { gun: globalThis.gun, user: globalThis.user };
}
if (booting) return booting;
booting = (async () => {
const Gun = (await import('gun')).default;
await import('gun/sea.js');
const { default: WebSocket } = await import('ws');
Gun.window = Gun.window || {};
Gun.window.WebSocket = WebSocket;
const gun = Gun({
peers: ['ws://localhost:' + relayPort + '/gun'],
localStorage: false,
radisk: false,
});
const mesh = gun._.opt.mesh;
if (mesh) {
for (const url of Object.keys(gun._.opt.peers)) {
mesh.hi(gun._.opt.peers[url]);
}
}
globalThis.gun = gun;
globalThis.user = gun.user();
return { gun, user: globalThis.user };
})();
return booting;
}
module.exports = { bootGun };