Skip to content

Commit 62e347a

Browse files
mafintoshandrewosh
authored andcommitted
optional cbs, trim deps
1 parent c62794d commit 62e347a

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
const crypto = require('crypto')
2-
const { promisify } = require('util')
3-
41
const { NanoresourcePromise: Nanoresource } = require('nanoresource-promise/emitter')
52
const HypercoreProtocol = require('hypercore-protocol')
63
const hyperswarm = require('hyperswarm')
74
const codecs = require('codecs')
85
const pump = require('pump')
6+
const maybe = require('call-me-maybe')
97

108
const STREAM_PEER = Symbol('networker-stream-peer')
119

1210
class CorestoreNetworker extends Nanoresource {
1311
constructor (corestore, opts = {}) {
1412
super()
1513
this.corestore = corestore
16-
this.id = opts.id || crypto.randomBytes(32)
1714
this.opts = opts
1815
this.keyPair = opts.keyPair || HypercoreProtocol.keyPair()
1916

2017
this._replicationOpts = {
21-
id: this.id,
2218
encrypt: true,
2319
live: true,
2420
keyPair: this.keyPair
@@ -61,7 +57,12 @@ class CorestoreNetworker extends Nanoresource {
6157
lookup: opts.lookup
6258
})
6359
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+
})
6566
if (!this._joined.has(keyString)) {
6667
return
6768
}
@@ -221,12 +222,18 @@ class CorestoreNetworker extends Nanoresource {
221222
}
222223

223224
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+
})
225232
}
226233

227-
configure (discoveryKey, opts = {}) {
234+
configure (discoveryKey, opts = {}, cb) {
228235
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())
230237

231238
const id = Symbol('id')
232239
const prom = this._configure(discoveryKey, opts, id, false)
@@ -237,10 +244,16 @@ class CorestoreNetworker extends Nanoresource {
237244
prom.discoveryKey = discoveryKey
238245
prom.previous = prev
239246

247+
maybeOptional(cb, prom)
248+
240249
return prom
241250
}
242251

243-
async unconfigure (prom) {
252+
unconfigure (prom, cb) {
253+
return maybeOptional(cb, this._unconfigure(prom))
254+
}
255+
256+
async _unconfigure (prom) {
244257
if (this.swarm && this.swarm.destroyed) return null
245258
if (!this.swarm) {
246259
await this.listen()
@@ -365,3 +378,11 @@ function intoPeer (stream) {
365378
function toString (dk) {
366379
return typeof dk === 'string' ? dk : dk.toString('hex')
367380
}
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+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A corestore networking module based on Hyperswarm.",
55
"main": "index.js",
66
"dependencies": {
7+
"call-me-maybe": "^1.0.1",
78
"codecs": "^2.1.0",
89
"hypercore-protocol": "^8.0.0",
910
"hyperswarm": "^2.14.1",

0 commit comments

Comments
 (0)