hypercore 10.0.0-alpha.17 → 10.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +26 -23
- package/lib/merkle-tree.js +2 -0
- package/lib/protocol.js +22 -1
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -92,9 +92,28 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
92
92
|
indent + ')'
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
static createProtocolStream (isInitiator, opts) {
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
static createProtocolStream (isInitiator, opts = {}) {
|
|
96
|
+
let outerStream = isStream(isInitiator)
|
|
97
|
+
? isInitiator
|
|
98
|
+
: opts.stream
|
|
99
|
+
let noiseStream = null
|
|
100
|
+
|
|
101
|
+
if (outerStream) {
|
|
102
|
+
noiseStream = outerStream.noiseStream
|
|
103
|
+
} else {
|
|
104
|
+
noiseStream = new NoiseSecretStream(isInitiator, null, opts)
|
|
105
|
+
outerStream = noiseStream.rawStream
|
|
106
|
+
}
|
|
107
|
+
if (!noiseStream) throw new Error('Invalid stream')
|
|
108
|
+
|
|
109
|
+
if (!noiseStream.userData) {
|
|
110
|
+
const protocol = Replicator.createProtocol(noiseStream)
|
|
111
|
+
if (opts.keepAlive !== false) protocol.setKeepAlive(true)
|
|
112
|
+
noiseStream.userData = protocol
|
|
113
|
+
noiseStream.on('error', noop) // All noise errors already propagate through outerStream
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return outerStream
|
|
98
117
|
}
|
|
99
118
|
|
|
100
119
|
static defaultStorage (storage, opts = {}) {
|
|
@@ -268,33 +287,17 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
268
287
|
}
|
|
269
288
|
|
|
270
289
|
replicate (isInitiator, opts = {}) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
: opts.stream
|
|
274
|
-
let noiseStream = null
|
|
275
|
-
|
|
276
|
-
if (outerStream) {
|
|
277
|
-
noiseStream = outerStream.noiseStream
|
|
278
|
-
} else {
|
|
279
|
-
outerStream = Hypercore.createProtocolStream(isInitiator, opts)
|
|
280
|
-
noiseStream = outerStream.noiseStream
|
|
281
|
-
}
|
|
282
|
-
if (!noiseStream) throw new Error('Invalid stream passed to replicate')
|
|
283
|
-
|
|
284
|
-
if (!noiseStream.userData) {
|
|
285
|
-
const protocol = Replicator.createProtocol(noiseStream)
|
|
286
|
-
noiseStream.userData = protocol
|
|
287
|
-
noiseStream.on('error', noop) // All noise errors already propagate through outerStream
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
+
const protocolStream = Hypercore.createProtocolStream(isInitiator, opts)
|
|
291
|
+
const noiseStream = protocolStream.noiseStream
|
|
290
292
|
const protocol = noiseStream.userData
|
|
293
|
+
|
|
291
294
|
if (this.opened) {
|
|
292
295
|
this.replicator.joinProtocol(protocol, this.key, this.discoveryKey)
|
|
293
296
|
} else {
|
|
294
297
|
this.opening.then(() => this.replicator.joinProtocol(protocol, this.key, this.discoveryKey), protocol.destroy.bind(protocol))
|
|
295
298
|
}
|
|
296
299
|
|
|
297
|
-
return
|
|
300
|
+
return protocolStream
|
|
298
301
|
}
|
|
299
302
|
|
|
300
303
|
get length () {
|
package/lib/merkle-tree.js
CHANGED
|
@@ -247,6 +247,8 @@ class ReorgBatch extends MerkleTreeBatch {
|
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
_updateDiffRoot (diff) {
|
|
250
|
+
if (this.want === null) return true
|
|
251
|
+
|
|
250
252
|
const spans = flat.spans(diff.index)
|
|
251
253
|
const start = spans[0] / 2
|
|
252
254
|
const end = Math.min(this.treeLength, spans[1] / 2 + 1)
|
package/lib/protocol.js
CHANGED
|
@@ -39,7 +39,7 @@ class Extension {
|
|
|
39
39
|
_sendAlias (message, alias) {
|
|
40
40
|
if (this.destroyed) return
|
|
41
41
|
|
|
42
|
-
if (this.
|
|
42
|
+
if (this.remoteSupports) {
|
|
43
43
|
return this.protocol.send(this.type, this.encoding, alias, message)
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -256,11 +256,13 @@ module.exports = class Protocol {
|
|
|
256
256
|
this._localExtensions = 128
|
|
257
257
|
this._remoteExtensions = []
|
|
258
258
|
this._extensions = new Map()
|
|
259
|
+
this._keepAliveInterval = null
|
|
259
260
|
|
|
260
261
|
this._destroyer = this._safeDestroy.bind(this)
|
|
261
262
|
this.noiseStream.on('data', this.onmessage.bind(this))
|
|
262
263
|
this.noiseStream.on('end', this.noiseStream.end) // no half open
|
|
263
264
|
this.noiseStream.on('close', () => {
|
|
265
|
+
this.setKeepAlive(false)
|
|
264
266
|
// TODO: If the stream was destroyed with an error, we probably want to forward it here
|
|
265
267
|
for (const peer of this._peers.values()) {
|
|
266
268
|
peer.destroy(null)
|
|
@@ -270,6 +272,18 @@ module.exports = class Protocol {
|
|
|
270
272
|
this._sendHandshake()
|
|
271
273
|
}
|
|
272
274
|
|
|
275
|
+
setKeepAlive (enable) {
|
|
276
|
+
if (enable) {
|
|
277
|
+
if (this._keepAliveInterval) return
|
|
278
|
+
this._keepAliveInterval = setInterval(this.ping.bind(this), 5000)
|
|
279
|
+
if (this._keepAliveInterval.unref) this._keepAliveInterval.unref()
|
|
280
|
+
} else {
|
|
281
|
+
if (!this._keepAliveInterval) return
|
|
282
|
+
clearInterval(this._keepAliveInterval)
|
|
283
|
+
this._keepAliveInterval = null
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
273
287
|
_sendHandshake () {
|
|
274
288
|
const m = { protocolVersion: this.protocolVersion, userAgent: this.userAgent }
|
|
275
289
|
const state = { start: 0, end: 0, buffer: null }
|
|
@@ -380,6 +394,8 @@ module.exports = class Protocol {
|
|
|
380
394
|
}
|
|
381
395
|
|
|
382
396
|
_decode (buffer) {
|
|
397
|
+
if (buffer.byteLength === 0) return
|
|
398
|
+
|
|
383
399
|
const state = { start: 0, end: buffer.length, buffer }
|
|
384
400
|
|
|
385
401
|
if (this._firstMessage === true) {
|
|
@@ -508,6 +524,11 @@ module.exports = class Protocol {
|
|
|
508
524
|
return this.noiseStream.write(state.buffer)
|
|
509
525
|
}
|
|
510
526
|
|
|
527
|
+
ping () {
|
|
528
|
+
const empty = this.noiseStream.alloc(0)
|
|
529
|
+
this.noiseStream.write(empty)
|
|
530
|
+
}
|
|
531
|
+
|
|
511
532
|
destroy (err) {
|
|
512
533
|
return this.noiseStream.destroy(err)
|
|
513
534
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hypercore",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.20",
|
|
4
4
|
"description": "Hypercore 10",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"xache": "^1.0.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"brittle": "^
|
|
52
|
+
"brittle": "^2.0.0",
|
|
53
53
|
"hyperswarm": "next",
|
|
54
54
|
"random-access-memory": "^3.1.2",
|
|
55
55
|
"standard": "^16.0.3",
|