1
1
const crypto = require ( 'crypto' )
2
- const { EventEmitter } = require ( 'events' )
3
2
const { promisify } = require ( 'util' )
4
3
5
4
const { NanoresourcePromise : Nanoresource } = require ( 'nanoresource-promise/emitter' )
6
5
const HypercoreProtocol = require ( 'hypercore-protocol' )
7
6
const hyperswarm = require ( 'hyperswarm' )
8
7
const codecs = require ( 'codecs' )
9
8
const pump = require ( 'pump' )
10
- const eos = require ( 'end-of-stream' )
11
9
12
- const log = require ( 'debug' ) ( 'corestore:network' )
13
-
14
- const OUTER_STREAM = Symbol ( 'networker-outer-stream' )
15
10
const STREAM_PEER = Symbol ( 'networker-stream-peer' )
16
11
17
12
class CorestoreNetworker extends Nanoresource {
@@ -56,7 +51,7 @@ class CorestoreNetworker extends Nanoresource {
56
51
}
57
52
58
53
async _join ( discoveryKey , opts = { } ) {
59
- const keyString = ( typeof discoveryKey === 'string' ) ? discoveryKey : discoveryKey . toString ( 'hex' )
54
+ const keyString = toString ( discoveryKey )
60
55
const keyBuf = ( discoveryKey instanceof Buffer ) ? discoveryKey : Buffer . from ( discoveryKey , 'hex' )
61
56
62
57
this . _joined . add ( keyString )
@@ -76,7 +71,7 @@ class CorestoreNetworker extends Nanoresource {
76
71
this . emit ( 'flushed' , keyBuf )
77
72
} else {
78
73
// Wait until the stream processing has caught up.
79
- const processedListener = ( ) => {
74
+ const processedListener = ( ) => {
80
75
if ( ! this . _joined . has ( keyString ) ) {
81
76
this . removeListener ( 'stream-processed' , processedListener )
82
77
return
@@ -93,7 +88,7 @@ class CorestoreNetworker extends Nanoresource {
93
88
}
94
89
95
90
async _leave ( discoveryKey ) {
96
- const keyString = ( typeof discoveryKey === 'string' ) ? discoveryKey : discoveryKey . toString ( 'hex' )
91
+ const keyString = toString ( discoveryKey )
97
92
const keyBuf = ( discoveryKey instanceof Buffer ) ? discoveryKey : Buffer . from ( discoveryKey , 'hex' )
98
93
99
94
this . _joined . delete ( keyString )
@@ -105,7 +100,7 @@ class CorestoreNetworker extends Nanoresource {
105
100
} )
106
101
} )
107
102
108
- for ( let stream of this . streams ) {
103
+ for ( const stream of this . streams ) {
109
104
stream . close ( keyBuf )
110
105
}
111
106
}
@@ -159,8 +154,6 @@ class CorestoreNetworker extends Nanoresource {
159
154
this . swarm . on ( 'connection' , ( socket , info ) => {
160
155
const isInitiator = ! ! info . client
161
156
if ( socket . remoteAddress === '::ffff:127.0.0.1' || socket . remoteAddress === '127.0.0.1' ) return null
162
- const peerInfo = info . peer
163
- const discoveryKey = peerInfo && peerInfo . topic
164
157
165
158
var finishedHandshake = false
166
159
var processed = false
@@ -231,22 +224,48 @@ class CorestoreNetworker extends Nanoresource {
231
224
return this . _configurations
232
225
}
233
226
234
- async configure ( discoveryKey , opts = { } ) {
227
+ configure ( discoveryKey , opts = { } ) {
228
+ if ( ! this . swarm ) this . open ( ) // it is sync, which makes this easier below inregards to race conditions
229
+ if ( this . swarm && this . swarm . destroyed ) return Promise . resolve ( )
230
+
231
+ const id = Symbol ( 'id' )
232
+ const prom = this . _configure ( discoveryKey , opts , id )
233
+ const keyString = toString ( discoveryKey )
234
+ const prev = this . _configurations . get ( keyString ) || { lookup : false , announce : false , id : null }
235
+
236
+ prom . configureId = id
237
+ prom . discoveryKey = discoveryKey
238
+ prom . previous = prev
239
+
240
+ return prom
241
+ }
242
+
243
+ async unconfigure ( prom ) {
235
244
if ( this . swarm && this . swarm . destroyed ) return null
236
245
if ( ! this . swarm ) {
237
246
await this . listen ( )
238
- return this . configure ( discoveryKey , opts )
247
+ return this . unconfigure ( prom )
239
248
}
240
- const self = this
241
249
250
+ const discoveryKey = prom . discoveryKey
251
+ const keyString = toString ( discoveryKey )
252
+ const conf = this . _configurations . get ( keyString )
253
+
254
+ if ( ! conf || conf . id !== prom . configureId ) return
255
+ return this . _configure ( discoveryKey , prom . previous , prom . previous . id )
256
+ }
257
+
258
+ async _configure ( discoveryKey , opts = { } , id ) {
242
259
const config = {
243
260
announce : opts . announce !== false ,
244
261
lookup : opts . lookup !== false
245
262
}
246
- opts = { ...opts , ...config }
263
+ opts = { ...opts , ...config , id }
247
264
248
- const keyString = ( typeof discoveryKey === 'string' ) ? discoveryKey : discoveryKey . toString ( 'hex' )
249
- this . _configurations . set ( keyString , opts )
265
+ const keyString = toString ( discoveryKey )
266
+
267
+ if ( id ) this . _configurations . set ( keyString , opts )
268
+ else this . _configurations . delete ( keyString )
250
269
251
270
const joining = config . announce || config . lookup
252
271
if ( joining ) {
@@ -275,7 +294,6 @@ class CorestoreNetworker extends Nanoresource {
275
294
}
276
295
return ext
277
296
}
278
-
279
297
}
280
298
281
299
module . exports = CorestoreNetworker
@@ -342,4 +360,6 @@ function intoPeer (stream) {
342
360
}
343
361
}
344
362
345
- function noop ( ) { }
363
+ function toString ( dk ) {
364
+ return typeof dk === 'string' ? dk : dk . toString ( 'hex' )
365
+ }
0 commit comments