| const c = require('compact-encoding') | ||
| const m = require('./messages') | ||
| const { COMMANDS: HYPERDHT_COMMANDS } = require('./constants') | ||
| module.exports = class Plugin { | ||
| constructor(name, version) { | ||
| this.name = name | ||
| this.version = version | ||
| this.dht = null | ||
| } | ||
| onregister(dht) { | ||
| this.dht = dht | ||
| } | ||
| onrequest(req, outerReq) { | ||
| throw new Error('onrequest() must be implemented') | ||
| } | ||
| onpersistent() { | ||
| throw new Error('onpersistent() must be implemented') | ||
| } | ||
| destroy() { | ||
| throw new Error('destroy() must be implemented') | ||
| } | ||
| request({ token = null, command, target = null, value = null }, to, opts) { | ||
| const req = c.encode(m.pluginRequest, { | ||
| plugin: this.name, | ||
| version: this.version, | ||
| command, | ||
| value | ||
| }) | ||
| return this.dht.request( | ||
| { | ||
| token, | ||
| target, | ||
| command: HYPERDHT_COMMANDS.PLUGIN, | ||
| value: req | ||
| }, | ||
| to, | ||
| opts | ||
| ) | ||
| } | ||
| query({ command, target = null, value = null }, opts) { | ||
| const req = c.encode(m.pluginRequest, { | ||
| plugin: this.name, | ||
| version: this.version, | ||
| command, | ||
| value | ||
| }) | ||
| return this.dht.query( | ||
| { | ||
| target, | ||
| command: HYPERDHT_COMMANDS.PLUGIN, | ||
| value: req | ||
| }, | ||
| opts | ||
| ) | ||
| } | ||
| } |
+12
-0
@@ -49,2 +49,3 @@ const DHT = require('dht-rpc') | ||
| this.rawStreams = new RawStreamSet(this) | ||
| this.plugins = new Map() | ||
@@ -66,2 +67,3 @@ this._router = new Router(this, router) | ||
| this._persistent = new Persistent(this, persistent) | ||
| for (const plugin of this.plugins.values()) plugin.onpersistent() | ||
| }) | ||
@@ -131,2 +133,3 @@ | ||
| if (this._persistent) this._persistent.destroy() | ||
| for (const plugin of this.plugins.values()) plugin.destroy() | ||
| await this.rawStreams.clear() | ||
@@ -441,2 +444,6 @@ await this._socketPool.destroy() | ||
| } | ||
| case COMMANDS.PLUGIN: { | ||
| this._persistent.onplugin(req) | ||
| return true | ||
| } | ||
| } | ||
@@ -516,2 +523,7 @@ | ||
| } | ||
| register(name, plugin) { | ||
| this.plugins.set(name, plugin) | ||
| plugin.onregister(this) | ||
| } | ||
| } | ||
@@ -518,0 +530,0 @@ |
+4
-2
@@ -758,3 +758,3 @@ const NoiseSecretStream = require('@hyperswarm/secret-stream') | ||
| try { | ||
| await updateHolepunch(peerAddress, relayAddress, { | ||
| await updateHolepunch(c, peerAddress, relayAddress, { | ||
| error: ERROR.ABORTED, | ||
@@ -770,3 +770,5 @@ firewall: FIREWALL.UNKNOWN, | ||
| }) | ||
| } catch {} | ||
| } catch (err) { | ||
| safetyCatch(err) | ||
| } | ||
@@ -773,0 +775,0 @@ destroyPuncher(c) |
+19
-10
@@ -13,3 +13,4 @@ const crypto = require('hypercore-crypto') | ||
| IMMUTABLE_PUT: 8, | ||
| IMMUTABLE_GET: 9 | ||
| IMMUTABLE_GET: 9, | ||
| PLUGIN: 10 | ||
| }) | ||
@@ -43,10 +44,17 @@ | ||
| const [NS_ANNOUNCE, NS_UNANNOUNCE, NS_MUTABLE_PUT, NS_PEER_HANDSHAKE, NS_PEER_HOLEPUNCH] = | ||
| crypto.namespace('hyperswarm/dht', [ | ||
| COMMANDS.ANNOUNCE, | ||
| COMMANDS.UNANNOUNCE, | ||
| COMMANDS.MUTABLE_PUT, | ||
| COMMANDS.PEER_HANDSHAKE, | ||
| COMMANDS.PEER_HOLEPUNCH | ||
| ]) | ||
| const [ | ||
| NS_ANNOUNCE, | ||
| NS_UNANNOUNCE, | ||
| NS_MUTABLE_PUT, | ||
| NS_PEER_HANDSHAKE, | ||
| NS_PEER_HOLEPUNCH, | ||
| NS_PLUGIN | ||
| ] = crypto.namespace('hyperswarm/dht', [ | ||
| COMMANDS.ANNOUNCE, | ||
| COMMANDS.UNANNOUNCE, | ||
| COMMANDS.MUTABLE_PUT, | ||
| COMMANDS.PEER_HANDSHAKE, | ||
| COMMANDS.PEER_HOLEPUNCH, | ||
| COMMANDS.PLUGIN | ||
| ]) | ||
@@ -58,3 +66,4 @@ exports.NS = { | ||
| PEER_HANDSHAKE: NS_PEER_HANDSHAKE, | ||
| PEER_HOLEPUNCH: NS_PEER_HOLEPUNCH | ||
| PEER_HOLEPUNCH: NS_PEER_HOLEPUNCH, | ||
| PLUGIN: NS_PLUGIN | ||
| } |
+23
-0
@@ -422,1 +422,24 @@ const c = require('compact-encoding') | ||
| } | ||
| exports.pluginRequest = { | ||
| preencode(state, m) { | ||
| c.string.preencode(state, m.plugin) | ||
| c.uint.preencode(state, m.version) | ||
| c.uint.preencode(state, m.command) | ||
| c.buffer.preencode(state, m.value) | ||
| }, | ||
| encode(state, m) { | ||
| c.string.encode(state, m.plugin) | ||
| c.uint.encode(state, m.version) | ||
| c.uint.encode(state, m.command) | ||
| c.buffer.encode(state, m.value) | ||
| }, | ||
| decode(state) { | ||
| return { | ||
| plugin: c.string.decode(state), | ||
| version: c.uint.decode(state), | ||
| command: c.uint.decode(state), | ||
| value: c.buffer.decode(state) | ||
| } | ||
| } | ||
| } |
+12
-0
@@ -230,2 +230,14 @@ const c = require('compact-encoding') | ||
| onplugin(req) { | ||
| if (!req.value) return | ||
| const plugreq = decode(m.pluginRequest, req.value) | ||
| if (plugreq === null) return | ||
| const p = this.dht.plugins.get(plugreq.plugin) | ||
| if (!p || p.version !== plugreq.version) return | ||
| p.onrequest(plugreq, req) | ||
| } | ||
| destroy() { | ||
@@ -232,0 +244,0 @@ this.records.destroy() |
+3
-1
@@ -120,3 +120,5 @@ const { EventEmitter } = require('events') | ||
| await this._listening | ||
| } catch {} | ||
| } catch (err) { | ||
| safetyCatch(err) | ||
| } | ||
@@ -123,0 +125,0 @@ this._gc() |
+1
-1
| { | ||
| "name": "hyperdht", | ||
| "version": "6.30.0", | ||
| "version": "6.31.0", | ||
| "description": "The DHT powering Hyperswarm", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
151824
1.75%28
3.7%4341
2.6%0
-100%