mirror of https://codeberg.org/pzp/pzp-sync.git
better secret-stack plugin shape
This commit is contained in:
parent
0ae7704994
commit
782b435a98
|
@ -4,6 +4,7 @@ pnpm-lock.yaml
|
||||||
package-lock.json
|
package-lock.json
|
||||||
coverage
|
coverage
|
||||||
*~
|
*~
|
||||||
|
lib/*.d.ts
|
||||||
|
|
||||||
# For misc scripts and experiments:
|
# For misc scripts and experiments:
|
||||||
/gitignored
|
/gitignored
|
||||||
|
|
157
lib/index.js
157
lib/index.js
|
@ -51,94 +51,93 @@ function assertSHSEExists(peer) {
|
||||||
if (!peer.shse) throw new Error('sync requires secret-handshake-ext')
|
if (!peer.shse) throw new Error('sync requires secret-handshake-ext')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
/**
|
||||||
name: 'sync',
|
* @param {Emitter & {
|
||||||
manifest: {
|
* db: PPPPPDB | null,
|
||||||
connect: 'duplex',
|
* dict: PPPPPDict | null,
|
||||||
initiate: 'sync',
|
* set: PPPPPSet | null,
|
||||||
},
|
* goals: PPPPPGoals | null,
|
||||||
permissions: {
|
* shse: SHSE | null
|
||||||
anonymous: {
|
* }} peer
|
||||||
allow: ['connect'],
|
* @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<SyncStream>} */ ([])
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Emitter & {
|
* @param {string} remoteId
|
||||||
* db: PPPPPDB | null,
|
* @param {boolean} iamClient
|
||||||
* dict: PPPPPDict | null,
|
|
||||||
* set: PPPPPSet | null,
|
|
||||||
* goals: PPPPPGoals | null,
|
|
||||||
* shse: SHSE | null
|
|
||||||
* }} peer
|
|
||||||
* @param {unknown} config
|
|
||||||
*/
|
*/
|
||||||
init(peer, config) {
|
function createStream(remoteId, iamClient) {
|
||||||
assertDBExists(peer)
|
|
||||||
assertGoalsExists(peer)
|
|
||||||
assertSHSEExists(peer)
|
assertSHSEExists(peer)
|
||||||
const debug = makeDebug(`ppppp:sync`)
|
assertGoalsExists(peer)
|
||||||
const algo = new Algorithm(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<SyncStream>} */ ([])
|
/**
|
||||||
|
* @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)
|
||||||
* @param {string} remoteId
|
const remote = rpc.sync.connect((networkError) => {
|
||||||
* @param {boolean} iamClient
|
if (networkError && getSeverity(networkError) >= 3) {
|
||||||
*/
|
if (isMuxrpcMissingError(networkError, 'sync', 'connect')) {
|
||||||
function createStream(remoteId, iamClient) {
|
debug('peer %s does not support sync', rpc.shse.pubkey)
|
||||||
assertSHSEExists(peer)
|
// } else if (isReconnectedError(networkError)) { // TODO: bring back
|
||||||
assertGoalsExists(peer)
|
// Do nothing, this is a harmless error
|
||||||
// prettier-ignore
|
} else {
|
||||||
debug('Opening a stream with remote %s %s', iamClient ? 'server' : 'client', remoteId)
|
console.error(`rpc.sync.connect exception:`, networkError)
|
||||||
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, () => {})
|
|
||||||
}
|
}
|
||||||
})
|
abort?.(true, () => {})
|
||||||
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 = pull(local, remote, local)
|
||||||
|
}
|
||||||
|
peer.on('rpc:connect', onSyncRPCConnect)
|
||||||
|
|
||||||
return {
|
/**
|
||||||
connect,
|
* @this {{shse: {pubkey: string}}}
|
||||||
start,
|
*/
|
||||||
|
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'],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,7 @@
|
||||||
},
|
},
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
"*.js",
|
"lib/**"
|
||||||
"lib/*.js"
|
|
||||||
],
|
],
|
||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
|
|
Loading…
Reference in New Issue