hypercore 10.0.0-alpha.42 → 10.0.0-alpha.45

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
  }
@@ -191,8 +191,10 @@ module.exports = class Hypercore extends EventEmitter {
191
191
 
192
192
  s._passCapabilities(this)
193
193
 
194
- // Pass on the cache unless explicitly disabled.
195
- if (opts.cache !== false) s.cache = this.cache
194
+ // Configure the cache unless explicitly disabled.
195
+ if (opts.cache !== false) {
196
+ s.cache = opts.cache === true || !opts.cache ? this.cache : opts.cache
197
+ }
196
198
 
197
199
  ensureEncryption(s, opts)
198
200
 
@@ -271,6 +273,9 @@ module.exports = class Hypercore extends EventEmitter {
271
273
  this.encodeBatch = opts.encodeBatch
272
274
  }
273
275
 
276
+ // Start continous replication if not in sparse mode.
277
+ if (!this.sparse) this.download({ start: 0, end: -1 })
278
+
274
279
  // This is a hidden option that's only used by Corestore.
275
280
  // It's required so that corestore can load a name from userData before 'ready' is emitted.
276
281
  if (opts._preready) await opts._preready(this)
@@ -292,7 +297,8 @@ module.exports = class Hypercore extends EventEmitter {
292
297
  crypto: this.crypto,
293
298
  legacy: opts.legacy,
294
299
  auth: opts.auth,
295
- onupdate: this._oncoreupdate.bind(this)
300
+ onupdate: this._oncoreupdate.bind(this),
301
+ oncontigupdate: this._oncorecontigupdate.bind(this)
296
302
  })
297
303
 
298
304
  if (opts.userData) {
@@ -411,21 +417,27 @@ module.exports = class Hypercore extends EventEmitter {
411
417
  }
412
418
 
413
419
  get length () {
414
- return this._snapshot
415
- ? this._snapshot.length
416
- : (this.core === null ? 0 : this.core.tree.length)
420
+ if (this._snapshot) return this._snapshot.length
421
+ if (this.core === null) return 0
422
+ if (!this.sparse) return this.contiguousLength
423
+ return this.core.tree.length
417
424
  }
418
425
 
419
426
  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))
427
+ if (this._snapshot) return this._snapshot.byteLength
428
+ if (this.core === null) return 0
429
+ if (!this.sparse) return this.contiguousByteLength
430
+ return this.core.tree.byteLength - (this.core.tree.length * this.padding)
423
431
  }
424
432
 
425
433
  get contiguousLength () {
426
434
  return this.core === null ? 0 : this.core.header.contiguousLength
427
435
  }
428
436
 
437
+ get contiguousByteLength () {
438
+ return 0
439
+ }
440
+
429
441
  get fork () {
430
442
  return this.core === null ? 0 : this.core.tree.fork
431
443
  }
@@ -473,9 +485,10 @@ module.exports = class Hypercore extends EventEmitter {
473
485
  if (s._snapshot && bitfield.start < s._snapshot.compatLength) s._snapshot.compatLength = bitfield.start
474
486
  }
475
487
 
476
- if (appended) {
477
- s.emit('append')
478
- }
488
+ // For sparse sessions, immediately emit appends. Non-sparse sessions
489
+ // are handled separately and only emit appends when their contiguous
490
+ // length is updated.
491
+ if (appended && s.sparse) s.emit('append')
479
492
  }
480
493
 
481
494
  this.replicator.onupgrade()
@@ -494,6 +507,16 @@ module.exports = class Hypercore extends EventEmitter {
494
507
  }
495
508
  }
496
509
 
510
+ _oncorecontigupdate () {
511
+ // For non-sparse sessions, emit appends only when the contiguous length is
512
+ // updated.
513
+ for (let i = 0; i < this.sessions.length; i++) {
514
+ const s = this.sessions[i]
515
+
516
+ if (!s.sparse) s.emit('append')
517
+ }
518
+ }
519
+
497
520
  _onpeerupdate (added, peer) {
498
521
  const name = added ? 'peer-add' : 'peer-remove'
499
522
 
package/lib/core.js CHANGED
@@ -9,8 +9,9 @@ const { BAD_ARGUMENT, STORAGE_EMPTY, STORAGE_CONFLICT, INVALID_SIGNATURE } = req
9
9
  const m = require('./messages')
10
10
 
11
11
  module.exports = class Core {
12
- constructor (header, crypto, oplog, tree, blocks, bitfield, auth, legacy, onupdate) {
12
+ constructor (header, crypto, oplog, tree, blocks, bitfield, auth, legacy, onupdate, oncontigupdate) {
13
13
  this.onupdate = onupdate
14
+ this.oncontigupdate = oncontigupdate
14
15
  this.header = header
15
16
  this.crypto = crypto
16
17
  this.oplog = oplog
@@ -164,7 +165,7 @@ module.exports = class Core {
164
165
  }
165
166
  }
166
167
 
167
- return new this(header, crypto, oplog, tree, blocks, bitfield, auth, !!opts.legacy, opts.onupdate || noop)
168
+ return new this(header, crypto, oplog, tree, blocks, bitfield, auth, !!opts.legacy, opts.onupdate || noop, opts.oncontigupdate || noop)
168
169
  }
169
170
 
170
171
  _shouldFlush () {
@@ -202,6 +203,7 @@ module.exports = class Core {
202
203
  while (this.bitfield.get(i)) i++
203
204
 
204
205
  this.header.contiguousLength = i
206
+ this.oncontigupdate()
205
207
  }
206
208
  }
207
209
 
@@ -285,6 +287,7 @@ module.exports = class Core {
285
287
  batch.commit()
286
288
 
287
289
  this.header.contiguousLength = batch.length
290
+ this.oncontigupdate()
288
291
  this.header.tree.length = batch.length
289
292
  this.header.tree.rootHash = hash
290
293
  this.header.tree.signature = batch.signature
@@ -472,6 +475,7 @@ module.exports = class Core {
472
475
  const appended = batch.length > batch.ancestors
473
476
 
474
477
  this.header.contiguousLength = Math.min(batch.ancestors, this.header.contiguousLength)
478
+ this.oncontigupdate()
475
479
  this.header.tree.fork = batch.fork
476
480
  this.header.tree.length = batch.length
477
481
  this.header.tree.rootHash = batch.hash()
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
 
@@ -906,9 +910,9 @@ module.exports = class Replicator {
906
910
  }
907
911
 
908
912
  addRange (session, { start = 0, end = -1, length = toLength(start, end), blocks = null, linear = false } = {}) {
909
- if (blocks !== null) {
910
- if (start >= blocks.length) start = blocks.length
911
- if (length === -1 || start + length > blocks.length) length = blocks.length - start
913
+ if (blocks !== null) { // if using blocks, start, end just acts as frames around the blocks array
914
+ start = 0
915
+ end = length = blocks.length
912
916
  }
913
917
 
914
918
  const r = new RangeRequest(this._ranges, start, length === -1 ? -1 : start + length, linear, blocks)
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.45",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {