hypercore 10.0.0-alpha.17 → 10.0.0-alpha.18
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 +25 -23
- package/lib/protocol.js +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -92,9 +92,27 @@ 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
|
+
noiseStream.userData = protocol
|
|
112
|
+
noiseStream.on('error', noop) // All noise errors already propagate through outerStream
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return outerStream
|
|
98
116
|
}
|
|
99
117
|
|
|
100
118
|
static defaultStorage (storage, opts = {}) {
|
|
@@ -268,33 +286,17 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
268
286
|
}
|
|
269
287
|
|
|
270
288
|
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
|
-
|
|
289
|
+
const protocolStream = Hypercore.createProtocolStream(isInitiator, opts)
|
|
290
|
+
const noiseStream = protocolStream.noiseStream
|
|
290
291
|
const protocol = noiseStream.userData
|
|
292
|
+
|
|
291
293
|
if (this.opened) {
|
|
292
294
|
this.replicator.joinProtocol(protocol, this.key, this.discoveryKey)
|
|
293
295
|
} else {
|
|
294
296
|
this.opening.then(() => this.replicator.joinProtocol(protocol, this.key, this.discoveryKey), protocol.destroy.bind(protocol))
|
|
295
297
|
}
|
|
296
298
|
|
|
297
|
-
return
|
|
299
|
+
return protocolStream
|
|
298
300
|
}
|
|
299
301
|
|
|
300
302
|
get length () {
|
package/lib/protocol.js
CHANGED