hypercore 10.0.0-alpha.42 → 10.0.0-alpha.43

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/README.md CHANGED
@@ -62,6 +62,7 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
62
62
  {
63
63
  createIfMissing: true, // create a new Hypercore key pair if none was present in storage
64
64
  overwrite: false, // overwrite any old Hypercore that might already exist
65
+ sparse: true, // enable sparse mode, counting unavailable blocks towards core.length and core.byteLength
65
66
  valueEncoding: 'json' | 'utf-8' | 'binary', // defaults to binary
66
67
  encodeBatch: batch => { ... }, // optionally apply an encoding to complete batches
67
68
  keyPair: kp, // optionally pass the public key and secret key as a key pair
@@ -235,13 +236,13 @@ Buffer containing the optional block encryption key of this core. Will be `null`
235
236
 
236
237
  #### `core.length`
237
238
 
238
- How many blocks of data are available on this core?
239
+ How many blocks of data are available on this core? If `sparse: false`, this will equal `core.contiguousLength`.
239
240
 
240
241
  Populated after `ready` has been emitted. Will be `0` before the event.
241
242
 
242
243
  #### `core.byteLength`
243
244
 
244
- How much data is available on this core in bytes?
245
+ How much data is available on this core in bytes? If `sparse: false`, this will equal `core.contiguousByteLength`.
245
246
 
246
247
  Populated after `ready` has been emitted. Will be `0` before the event.
247
248
 
@@ -251,6 +252,12 @@ How many blocks are contiguously available starting from the first block of this
251
252
 
252
253
  Populated after `ready` has been emitted. Will be `0` before the event.
253
254
 
255
+ #### `core.contiguousByteLength`
256
+
257
+ How much data is contiguously available starting from the first block of this core?
258
+
259
+ Populated after `ready` has been emitted. Will be `0` before the event.
260
+
254
261
  #### `core.fork`
255
262
 
256
263
  What is the current fork id of this core?
package/index.js CHANGED
@@ -66,6 +66,7 @@ module.exports = class Hypercore extends EventEmitter {
66
66
  this.opened = false
67
67
  this.closed = false
68
68
  this.snapshotted = !!opts.snapshot
69
+ this.sparse = opts.sparse !== false
69
70
  this.sessions = opts._sessions || [this]
70
71
  this.auth = opts.auth || null
71
72
  this.autoClose = !!opts.autoClose
@@ -151,7 +152,6 @@ module.exports = class Hypercore extends EventEmitter {
151
152
  }
152
153
  if (opts.keepAlive !== false) {
153
154
  noiseStream.setKeepAlive(5000)
154
- noiseStream.setTimeout(10000)
155
155
  }
156
156
  noiseStream.userData = protocol
157
157
  }
@@ -271,6 +271,9 @@ module.exports = class Hypercore extends EventEmitter {
271
271
  this.encodeBatch = opts.encodeBatch
272
272
  }
273
273
 
274
+ // Start continous replication if not in sparse mode.
275
+ if (!this.sparse) this.download({ start: 0, end: -1 })
276
+
274
277
  // This is a hidden option that's only used by Corestore.
275
278
  // It's required so that corestore can load a name from userData before 'ready' is emitted.
276
279
  if (opts._preready) await opts._preready(this)
@@ -411,21 +414,27 @@ module.exports = class Hypercore extends EventEmitter {
411
414
  }
412
415
 
413
416
  get length () {
414
- return this._snapshot
415
- ? this._snapshot.length
416
- : (this.core === null ? 0 : this.core.tree.length)
417
+ if (this._snapshot) return this._snapshot.length
418
+ if (this.core === null) return 0
419
+ if (!this.sparse) return this.contiguousLength
420
+ return this.core.tree.length
417
421
  }
418
422
 
419
423
  get byteLength () {
420
- return this._snapshot
421
- ? this._snapshot.byteLength
422
- : (this.core === null ? 0 : this.core.tree.byteLength - (this.core.tree.length * this.padding))
424
+ if (this._snapshot) return this._snapshot.byteLength
425
+ if (this.core === null) return 0
426
+ if (!this.sparse) return this.contiguousByteLength
427
+ return this.core.tree.byteLength - (this.core.tree.length * this.padding)
423
428
  }
424
429
 
425
430
  get contiguousLength () {
426
431
  return this.core === null ? 0 : this.core.header.contiguousLength
427
432
  }
428
433
 
434
+ get contiguousByteLength () {
435
+ return 0
436
+ }
437
+
429
438
  get fork () {
430
439
  return this.core === null ? 0 : this.core.tree.fork
431
440
  }
package/lib/replicator.js CHANGED
@@ -726,6 +726,10 @@ class Peer {
726
726
  b.inflight.push(req)
727
727
  this._send(req)
728
728
 
729
+ // Don't think this will ever happen, as the pending queue is drained before the range queue
730
+ // but doesn't hurt to check this explicitly here also.
731
+ if (b.queued) b.queued = false
732
+
729
733
  return true
730
734
  }
731
735
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.42",
3
+ "version": "10.0.0-alpha.43",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {