exchange-protocol
Advanced tools
Comparing version 4.0.0 to 4.1.0
54
index.js
@@ -66,3 +66,3 @@ // SPDX-License-Identifier: GPL-3.0-or-later | ||
const accept = keys => { | ||
return this.sendRequest (manifest.namespace, keys, manifest.id, peer) | ||
return this.sendRequest(manifest.namespace, keys, manifest.id, peer) | ||
} | ||
@@ -97,3 +97,5 @@ process.nextTick(() => this.handlers.onmanifest(manifest, accept, peer)) | ||
feeds: manifest.map(feed => { | ||
const strKey = feed.key.toString('hex') | ||
const out = { key: feed.key } | ||
if (typeof out.key === 'string') out.key = Buffer.from(out.key, 'hex') | ||
const strKey = out.key.toString('hex') | ||
this.offeredKeys[namespace][strKey] = 1 | ||
@@ -118,4 +120,4 @@ | ||
}) | ||
feed.headers = arrayHeaders | ||
return feed | ||
out.headers = arrayHeaders | ||
return out | ||
}) | ||
@@ -150,7 +152,7 @@ } | ||
sendRequest (namespace, keys, manifestId, peer) { | ||
this.requestedKeys[namespace] = this.requestedKeys[namespace] || [] | ||
// TODO: I don't know if keys are normalized to either hexstring or buffers at this stage. | ||
this.requestedKeys[namespace] = this.requestedKeys[namespace] || {} | ||
keys.forEach(k => { | ||
if (this.requestedKeys[namespace].indexOf(k) === -1) { | ||
this.requestedKeys[namespace].push(k) | ||
} | ||
if (Buffer.isBuffer(k)) k = k.toString('hex') | ||
this.requestedKeys[namespace][k] = 1 | ||
}) | ||
@@ -162,6 +164,5 @@ | ||
manifest_id: manifestId, | ||
keys: keys.map(k => Buffer.from(k, 'hex')) | ||
keys: keys.map(k => Buffer.isBuffer(k) ? k : Buffer.from(k, 'hex')) | ||
} | ||
} | ||
if (peer) this.send(message, peer) | ||
@@ -171,25 +172,15 @@ else this.broadcast(message) | ||
/* | ||
* Same as negotiatedKeysNS except returns a flat array of keys | ||
/* Reverse looks up the negotiated keys and tries to determine | ||
* what namespace the key was either offered or requested | ||
* returns undefined if provided key was neither offererd nor requested | ||
*/ | ||
get negotiatedKeys () { | ||
return Object.keys(this.negotiatedKeysNS) | ||
} | ||
namespaceFor (key) { | ||
if (Buffer.isBuffer(key)) key = key.toString('hex') | ||
for (const ns of Object.keys(this.offeredKeys)) { | ||
if (this.offeredKeys[ns][key]) return ns | ||
} | ||
/* | ||
* Each peer allows offered-keys and requested-keys | ||
* to be replicated on the stream | ||
* negotiated = offered - requested for each namespace | ||
* as key value, { feedKey: namespace, ... } | ||
*/ | ||
get negotiatedKeysNS () { | ||
const m = {} | ||
Object.keys(this.offeredKeys).forEach(ns => { | ||
this.offeredKeys[ns].forEach(k => { m[k] = ns }) | ||
}) | ||
Object.keys(this.requestedKeys).forEach(ns => { | ||
this.requestedKeys[ns].forEach(k => { m[k] = ns }) | ||
}) | ||
return m | ||
for (const ns of Object.keys(this.requestedKeys)) { | ||
if (this.requestedKeys[ns][key]) return ns | ||
} | ||
} | ||
@@ -221,2 +212,3 @@ } | ||
} | ||
return inst | ||
@@ -223,0 +215,0 @@ } |
{ | ||
"name": "exchange-protocol", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Hypercore-extension that allows peers to exchange feed-descriptors", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
37148
772