update secret-stack to 8.1

This commit is contained in:
Andre Staltz 2024-01-08 11:33:45 +02:00
parent 26dcefc459
commit ae3d1d83d8
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
4 changed files with 10 additions and 61 deletions

View File

@ -24,31 +24,11 @@ function countIter(iter) {
return count return count
} }
/**
* @param {{ dict: PPPPPDict | null }} peer
* @returns {asserts peer is { dict: PPPPPDict }}
*/
function assertDictPlugin(peer) {
if (!peer.dict) {
throw new Error('sync plugin requires ppppp-dict plugin')
}
}
/**
* @param {{ set: PPPPPSet | null }} peer
* @returns {asserts peer is { set: PPPPPSet }}
*/
function assertSetPlugin(peer) {
if (!peer.set) {
throw new Error('sync plugin requires ppppp-set plugin')
}
}
class Algorithm { class Algorithm {
/** @type {{ db: PPPPPDB, dict: PPPPPDict | null, set: PPPPPSet | null }} */ /** @type {ConstructorParameters<typeof Algorithm>[0]} */
#peer #peer
/** @param {{ db: PPPPPDB, dict: PPPPPDict | null, set: PPPPPSet | null }} peer */ /** @param {{ db: PPPPPDB, dict: PPPPPDict, set: PPPPPSet }} peer */
constructor(peer) { constructor(peer) {
this.#peer = peer this.#peer = peer
} }
@ -149,12 +129,10 @@ class Algorithm {
return this.#wantAllRange(localHave, remoteHave) return this.#wantAllRange(localHave, remoteHave)
case 'dict': case 'dict':
assertDictPlugin(this.#peer)
const minDictGhostDepth = this.#peer.dict.minGhostDepth(goal.id) const minDictGhostDepth = this.#peer.dict.minGhostDepth(goal.id)
return this.#wantDictOrSetRange(minDictGhostDepth, remoteHave) return this.#wantDictOrSetRange(minDictGhostDepth, remoteHave)
case 'set': case 'set':
assertSetPlugin(this.#peer)
const minSetGhostDepth = this.#peer.set.minGhostDepth(goal.id) const minSetGhostDepth = this.#peer.set.minGhostDepth(goal.id)
return this.#wantDictOrSetRange(minSetGhostDepth, remoteHave) return this.#wantDictOrSetRange(minSetGhostDepth, remoteHave)

View File

@ -27,44 +27,17 @@ function isMuxrpcMissingError(err, namespace, methodName) {
return err.message === jsErrorMessage || err.message === goErrorMessage return err.message === jsErrorMessage || err.message === goErrorMessage
} }
/**
* @param {{ db: PPPPPDB | null }} peer
* @returns {asserts peer is { db: PPPPPDB }}
*/
function assertDBExists(peer) {
if (!peer.db) throw new Error('"sync" plugin requires "db" plugin')
}
/**
* @param {{ goals: PPPPPGoals | null }} peer
* @returns {asserts peer is { goals: PPPPPGoals }}
*/
function assertGoalsExists(peer) {
if (!peer.goals) throw new Error('"sync" plugin requires "goals" plugin')
}
/**
* @param {{ shse: SHSE | null }} peer
* @returns {asserts peer is { shse: SHSE }}
*/
function assertSHSEExists(peer) {
if (!peer.shse) throw new Error('"sync" plugin requires "shse" plugin')
}
/** /**
* @param {Emitter & { * @param {Emitter & {
* db: PPPPPDB | null, * db: PPPPPDB,
* dict: PPPPPDict | null, * dict: PPPPPDict,
* set: PPPPPSet | null, * set: PPPPPSet,
* goals: PPPPPGoals | null, * goals: PPPPPGoals,
* shse: SHSE | null * shse: SHSE
* }} peer * }} peer
* @param {unknown} config * @param {unknown} config
*/ */
function initSync(peer, config) { function initSync(peer, config) {
assertDBExists(peer)
assertGoalsExists(peer)
assertSHSEExists(peer)
const debug = makeDebug(`ppppp:sync`) const debug = makeDebug(`ppppp:sync`)
const algo = new Algorithm(peer) const algo = new Algorithm(peer)
let started = false let started = false
@ -76,8 +49,6 @@ function initSync(peer, config) {
* @param {boolean} iamClient * @param {boolean} iamClient
*/ */
function createStream(remoteId, iamClient) { function createStream(remoteId, iamClient) {
assertSHSEExists(peer)
assertGoalsExists(peer)
// prettier-ignore // prettier-ignore
debug('Opening a stream with remote %s %s', iamClient ? 'server' : 'client', remoteId) debug('Opening a stream with remote %s %s', iamClient ? 'server' : 'client', remoteId)
const stream = new SyncStream(peer.shse.pubkey, debug, peer.goals, algo) const stream = new SyncStream(peer.shse.pubkey, debug, peer.goals, algo)
@ -90,7 +61,6 @@ function initSync(peer, config) {
* @param {boolean} iamClient * @param {boolean} iamClient
*/ */
function onSyncRPCConnect(rpc, iamClient) { function onSyncRPCConnect(rpc, iamClient) {
assertSHSEExists(peer)
if (rpc.shse.pubkey === peer.shse.pubkey) return // connecting to myself if (rpc.shse.pubkey === peer.shse.pubkey) return // connecting to myself
if (!iamClient) return if (!iamClient) return
const stream = createStream(rpc.shse.pubkey, true) const stream = createStream(rpc.shse.pubkey, true)
@ -136,6 +106,7 @@ function initSync(peer, config) {
} }
exports.name = 'sync' exports.name = 'sync'
exports.needs = ['db', 'dict', 'set', 'goals', 'shse']
exports.manifest = { exports.manifest = {
connect: 'duplex', connect: 'duplex',
initiate: 'sync', initiate: 'sync',

View File

@ -46,7 +46,7 @@
"prettier": "^2.6.2", "prettier": "^2.6.2",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^4.4.0", "rimraf": "^4.4.0",
"secret-stack": "~8.0.0", "secret-stack": "~8.1.0",
"secret-handshake-ext": "0.0.11", "secret-handshake-ext": "0.0.11",
"ssb-box": "^1.0.1", "ssb-box": "^1.0.1",
"typescript": "^5.1.3" "typescript": "^5.1.3"

View File

@ -11,6 +11,6 @@
"module": "node16", "module": "node16",
"skipLibCheck": true, "skipLibCheck": true,
"strict": true, "strict": true,
"target": "es2021" "target": "es2022"
} }
} }