diff --git a/.gitignore b/.gitignore index 4b96477..873998e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ pnpm-lock.yaml package-lock.json coverage *~ +lib/*.d.ts # For misc scripts and experiments: /gitignored diff --git a/lib/index.js b/lib/index.js index 5e6e0f0..132ed82 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,94 +51,93 @@ function assertSHSEExists(peer) { if (!peer.shse) throw new Error('sync requires secret-handshake-ext') } -module.exports = { - name: 'sync', - manifest: { - connect: 'duplex', - initiate: 'sync', - }, - permissions: { - anonymous: { - allow: ['connect'], - }, - }, +/** + * @param {Emitter & { + * db: PPPPPDB | null, + * dict: PPPPPDict | null, + * set: PPPPPSet | null, + * goals: PPPPPGoals | null, + * shse: SHSE | null + * }} peer + * @param {unknown} config + */ +function initSync(peer, config) { + assertDBExists(peer) + assertGoalsExists(peer) + assertSHSEExists(peer) + const debug = makeDebug(`ppppp:sync`) + const algo = new Algorithm(peer) + + const streams = /** @type {Array} */ ([]) /** - * @param {Emitter & { - * db: PPPPPDB | null, - * dict: PPPPPDict | null, - * set: PPPPPSet | null, - * goals: PPPPPGoals | null, - * shse: SHSE | null - * }} peer - * @param {unknown} config + * @param {string} remoteId + * @param {boolean} iamClient */ - init(peer, config) { - assertDBExists(peer) - assertGoalsExists(peer) + function createStream(remoteId, iamClient) { assertSHSEExists(peer) - const debug = makeDebug(`ppppp:sync`) - const algo = new Algorithm(peer) + assertGoalsExists(peer) + // prettier-ignore + debug('Opening a stream with remote %s %s', iamClient ? 'server' : 'client', remoteId) + const stream = new SyncStream(peer.shse.pubkey, debug, peer.goals, algo) + streams.push(stream) + return stream + } - const streams = /** @type {Array} */ ([]) + /** + * @param {{ shse: SHSE, sync: { connect: GetDuplex } }} rpc + * @param {boolean} iamClient + */ + function onSyncRPCConnect(rpc, iamClient) { + assertSHSEExists(peer) + if (rpc.shse.pubkey === peer.shse.pubkey) return // connecting to myself + if (!iamClient) return + const local = toPull.duplex(createStream(rpc.shse.pubkey, true)) - /** - * @param {string} remoteId - * @param {boolean} iamClient - */ - function createStream(remoteId, iamClient) { - assertSHSEExists(peer) - assertGoalsExists(peer) - // prettier-ignore - debug('Opening a stream with remote %s %s', iamClient ? 'server' : 'client', remoteId) - const stream = new SyncStream(peer.shse.pubkey, debug, peer.goals, algo) - streams.push(stream) - return stream - } - - /** - * @param {{ shse: SHSE, sync: { connect: GetDuplex } }} rpc - * @param {boolean} iamClient - */ - function onSyncRPCConnect(rpc, iamClient) { - assertSHSEExists(peer) - if (rpc.shse.pubkey === peer.shse.pubkey) return // connecting to myself - if (!iamClient) return - const local = toPull.duplex(createStream(rpc.shse.pubkey, true)) - - let abort = /** @type {CallableFunction | null} */ (null) - const remote = rpc.sync.connect((networkError) => { - if (networkError && getSeverity(networkError) >= 3) { - if (isMuxrpcMissingError(networkError, 'sync', 'connect')) { - debug('peer %s does not support sync', rpc.shse.pubkey) - // } else if (isReconnectedError(networkError)) { // TODO: bring back - // Do nothing, this is a harmless error - } else { - console.error(`rpc.sync.connect exception:`, networkError) - } - abort?.(true, () => {}) + let abort = /** @type {CallableFunction | null} */ (null) + const remote = rpc.sync.connect((networkError) => { + if (networkError && getSeverity(networkError) >= 3) { + if (isMuxrpcMissingError(networkError, 'sync', 'connect')) { + debug('peer %s does not support sync', rpc.shse.pubkey) + // } else if (isReconnectedError(networkError)) { // TODO: bring back + // Do nothing, this is a harmless error + } else { + console.error(`rpc.sync.connect exception:`, networkError) } - }) - abort = pull(local, remote, local) - } - peer.on('rpc:connect', onSyncRPCConnect) - - /** - * @this {{shse: {pubkey: string}}} - */ - function connect() { - return toPull.duplex(createStream(this.shse.pubkey, false)) - } - - function start() { - for (const stream of streams) { - stream.initiate() + abort?.(true, () => {}) } - } + }) + abort = pull(local, remote, local) + } + peer.on('rpc:connect', onSyncRPCConnect) - return { - connect, - start, + /** + * @this {{shse: {pubkey: string}}} + */ + function connect() { + return toPull.duplex(createStream(this.shse.pubkey, false)) + } + + function start() { + for (const stream of streams) { + stream.initiate() } + } + + return { + connect, + start, + } +} + +exports.name = 'sync' +exports.manifest = { + connect: 'duplex', + initiate: 'sync', +} +exports.init = initSync +exports.permissions = { + anonymous: { + allow: ['connect'], }, } diff --git a/package.json b/package.json index 62638d8..8a31e02 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,7 @@ }, "main": "index.js", "files": [ - "*.js", - "lib/*.js" + "lib/**" ], "exports": { ".": {