blind-peer 2.9.2 → 2.9.4

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.
Files changed (3) hide show
  1. package/bin.js +3 -0
  2. package/index.js +27 -7
  3. package/package.json +1 -1
package/bin.js CHANGED
@@ -131,6 +131,9 @@ const cmd = command(
131
131
  blindPeer.on('announce-core', (core) => {
132
132
  logger.info(`Started announcing core ${coreToInfo(core, true)}`)
133
133
  })
134
+ blindPeer.on('announced-initial-cores', () => {
135
+ logger.info(`Announced all initial cores`)
136
+ })
134
137
  blindPeer.on('core-downloaded', (core) => {
135
138
  logger.info(`Announced core fully downloaded: ${coreToInfo(core, true)}`)
136
139
  })
package/index.js CHANGED
@@ -61,6 +61,8 @@ class CoreTracker {
61
61
  }
62
62
 
63
63
  gc() {
64
+ if (!this.record) throw new Error('Record must be set before calling gc')
65
+
64
66
  // TODO: support gc-ing till less than last block (required hypercore to support getting byteLength at arbitrary versions)
65
67
  const bytesCleared = this.core.byteLength
66
68
  const blocksCleared = this.core.length
@@ -185,7 +187,16 @@ class WakeupHandler {
185
187
  class BlindPeer extends ReadyResource {
186
188
  constructor(
187
189
  rocks,
188
- { swarm, store, wakeup, maxBytes = 100_000_000_000, enableGc = true, trustedPubKeys, port } = {}
190
+ {
191
+ swarm,
192
+ store,
193
+ wakeup,
194
+ maxBytes = 100_000_000_000,
195
+ enableGc = true,
196
+ trustedPubKeys,
197
+ port,
198
+ announcingInterval = 100
199
+ } = {}
189
200
  ) {
190
201
  super()
191
202
 
@@ -193,6 +204,7 @@ class BlindPeer extends ReadyResource {
193
204
  this.store = store || new Corestore(this.rocks, { active: false })
194
205
  this.swarm = swarm || null
195
206
  this._port = port || 0
207
+ this.announcingInterval = announcingInterval
196
208
  this.trustedPubKeys = new Set()
197
209
  for (const k of trustedPubKeys || []) this.addTrustedPubKey(k)
198
210
 
@@ -264,12 +276,7 @@ class BlindPeer extends ReadyResource {
264
276
  }
265
277
  this.swarm.on('connection', this._onconnection.bind(this))
266
278
 
267
- const announceProms = []
268
- for await (const record of this.db.createAnnouncingCoresStream()) {
269
- announceProms.push(this._announceCore(record.key))
270
- }
271
- await Promise.all(announceProms)
272
-
279
+ this._announceCores().catch(safetyCatch) // announcing cores asynchronously
273
280
  this.flushInterval = setInterval(this.flush.bind(this), 10_000)
274
281
  }
275
282
 
@@ -336,6 +343,7 @@ class BlindPeer extends ReadyResource {
336
343
 
337
344
  try {
338
345
  const tracker = this.activeReplication.get(id)
346
+ if (!tracker.record) await tracker.refresh()
339
347
  const coreBytesCleared = tracker.gc()
340
348
  bytesCleared += coreBytesCleared
341
349
  } finally {
@@ -439,6 +447,18 @@ class BlindPeer extends ReadyResource {
439
447
  stream.on('close', () => core.close().catch(safetyCatch))
440
448
  }
441
449
 
450
+ async _announceCores() {
451
+ for await (const record of this.db.createAnnouncingCoresStream()) {
452
+ if (this.closing) return
453
+ await this._announceCore(record.key)
454
+ if (this.closing) return
455
+ await new Promise((resolve) => setTimeout(resolve, this.announcingInterval))
456
+ if (this.closing) return
457
+ }
458
+
459
+ this.emit('announced-initial-cores')
460
+ }
461
+
442
462
  async _announceCore(key) {
443
463
  const coreId = IdEnc.normalize(key)
444
464
  if (this.announcedCores.has(coreId)) return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "2.9.2",
3
+ "version": "2.9.4",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "bin": {