1
- const crypto = require ( 'crypto' )
2
- const { promisify } = require ( 'util' )
3
-
4
1
const { NanoresourcePromise : Nanoresource } = require ( 'nanoresource-promise/emitter' )
5
2
const HypercoreProtocol = require ( 'hypercore-protocol' )
6
3
const hyperswarm = require ( 'hyperswarm' )
7
4
const codecs = require ( 'codecs' )
8
5
const pump = require ( 'pump' )
6
+ const maybe = require ( 'call-me-maybe' )
9
7
10
8
const STREAM_PEER = Symbol ( 'networker-stream-peer' )
11
9
12
10
class CorestoreNetworker extends Nanoresource {
13
11
constructor ( corestore , opts = { } ) {
14
12
super ( )
15
13
this . corestore = corestore
16
- this . id = opts . id || crypto . randomBytes ( 32 )
17
14
this . opts = opts
18
15
this . keyPair = opts . keyPair || HypercoreProtocol . keyPair ( )
19
16
20
17
this . _replicationOpts = {
21
- id : this . id ,
22
18
encrypt : true ,
23
19
live : true ,
24
20
keyPair : this . keyPair
@@ -61,7 +57,12 @@ class CorestoreNetworker extends Nanoresource {
61
57
lookup : opts . lookup
62
58
} )
63
59
if ( opts . flush !== false ) {
64
- await promisify ( this . swarm . flush . bind ( this . swarm ) ) ( )
60
+ await new Promise ( ( resolve , reject ) => {
61
+ this . swarm . flush ( err => {
62
+ if ( err ) reject ( err )
63
+ else resolve ( )
64
+ } )
65
+ } )
65
66
if ( ! this . _joined . has ( keyString ) ) {
66
67
return
67
68
}
@@ -221,12 +222,18 @@ class CorestoreNetworker extends Nanoresource {
221
222
}
222
223
223
224
allStatuses ( ) {
224
- return this . _configurations
225
+ return [ ...this . _configurations ] . map ( ( [ k , v ] ) => {
226
+ return {
227
+ discoveryKey : Buffer . from ( k , 'hex' ) ,
228
+ announce : v . announce ,
229
+ lookup : v . lookup
230
+ }
231
+ } )
225
232
}
226
233
227
- configure ( discoveryKey , opts = { } ) {
234
+ configure ( discoveryKey , opts = { } , cb ) {
228
235
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 ( )
236
+ if ( this . swarm && this . swarm . destroyed ) return maybeOptional ( cb , Promise . resolve ( ) )
230
237
231
238
const id = Symbol ( 'id' )
232
239
const prom = this . _configure ( discoveryKey , opts , id , false )
@@ -237,10 +244,16 @@ class CorestoreNetworker extends Nanoresource {
237
244
prom . discoveryKey = discoveryKey
238
245
prom . previous = prev
239
246
247
+ maybeOptional ( cb , prom )
248
+
240
249
return prom
241
250
}
242
251
243
- async unconfigure ( prom ) {
252
+ unconfigure ( prom , cb ) {
253
+ return maybeOptional ( cb , this . _unconfigure ( prom ) )
254
+ }
255
+
256
+ async _unconfigure ( prom ) {
244
257
if ( this . swarm && this . swarm . destroyed ) return null
245
258
if ( ! this . swarm ) {
246
259
await this . listen ( )
@@ -365,3 +378,11 @@ function intoPeer (stream) {
365
378
function toString ( dk ) {
366
379
return typeof dk === 'string' ? dk : dk . toString ( 'hex' )
367
380
}
381
+
382
+ function noop ( ) { }
383
+
384
+ function maybeOptional ( cb , prom ) {
385
+ if ( cb ) maybe ( cb , prom )
386
+ else prom . catch ( noop )
387
+ return prom
388
+ }
0 commit comments