hypercore 10.0.0-alpha.38 → 10.0.0-alpha.40

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
@@ -245,6 +245,12 @@ How much data is available on this core in bytes?
245
245
 
246
246
  Populated after `ready` has been emitted. Will be `0` before the event.
247
247
 
248
+ #### `core.contiguousLength`
249
+
250
+ How many blocks are contiguously available starting from the first block of this core?
251
+
252
+ Populated after `ready` has been emitted. Will be `0` before the event.
253
+
248
254
  #### `core.fork`
249
255
 
250
256
  What is the current fork id of this core?
package/index.js CHANGED
@@ -190,6 +190,18 @@ module.exports = class Hypercore extends EventEmitter {
190
190
  })
191
191
 
192
192
  s._passCapabilities(this)
193
+
194
+ if (opts.encryptionKey) {
195
+ // Only override the block encryption if its either not already set or if
196
+ // the caller provided a different key.
197
+ if (
198
+ !this.encryption ||
199
+ !b4a.equals(this.encryption.key, opts.encryptionKey)
200
+ ) {
201
+ this.encryption = new BlockEncryption(opts.encryptionKey, this.key)
202
+ }
203
+ }
204
+
193
205
  this.sessions.push(s)
194
206
 
195
207
  return s
@@ -368,24 +380,34 @@ module.exports = class Hypercore extends EventEmitter {
368
380
  }
369
381
 
370
382
  replicate (isInitiator, opts = {}) {
383
+ // Only limitation here is that ondiscoverykey doesn't work atm when passing a muxer directly,
384
+ // because it doesn't really make a lot of sense.
385
+ if (Protomux.isProtomux(isInitiator)) return this._attachToMuxer(isInitiator, opts)
386
+
371
387
  const protocolStream = Hypercore.createProtocolStream(isInitiator, opts)
372
388
  const noiseStream = protocolStream.noiseStream
373
389
  const protocol = noiseStream.userData
374
390
 
391
+ this._attachToMuxer(protocol, opts)
392
+
393
+ return protocolStream
394
+ }
395
+
396
+ _attachToMuxer (mux, opts) {
375
397
  // If the user wants to, we can make this replication run in a session
376
398
  // that way the core wont close "under them" during replication
377
399
  if (opts.session) {
378
400
  const s = this.session()
379
- protocolStream.on('close', () => s.close().catch(noop))
401
+ mux.stream.on('close', () => s.close().catch(noop))
380
402
  }
381
403
 
382
404
  if (this.opened) {
383
- this.replicator.attachTo(protocol)
405
+ this.replicator.attachTo(mux)
384
406
  } else {
385
- this.opening.then(() => this.replicator.attachTo(protocol), protocol.destroy.bind(protocol))
407
+ this.opening.then(() => this.replicator.attachTo(mux), mux.destroy.bind(mux))
386
408
  }
387
409
 
388
- return protocolStream
410
+ return mux
389
411
  }
390
412
 
391
413
  get discoveryKey () {
@@ -404,6 +426,10 @@ module.exports = class Hypercore extends EventEmitter {
404
426
  : (this.core === null ? 0 : this.core.tree.byteLength - (this.core.tree.length * this.padding))
405
427
  }
406
428
 
429
+ get contiguousLength () {
430
+ return this.core === null ? 0 : this.core.header.contiguousLength
431
+ }
432
+
407
433
  get fork () {
408
434
  return this.core === null ? 0 : this.core.tree.fork
409
435
  }
package/lib/core.js CHANGED
@@ -26,6 +26,8 @@ module.exports = class Core {
26
26
  this._verifiesFlushed = null
27
27
  this._mutex = new Mutex()
28
28
  this._legacy = legacy
29
+
30
+ this._updateContiguousLength(header.contiguousLength)
29
31
  }
30
32
 
31
33
  static async open (storage, opts = {}) {
@@ -106,7 +108,8 @@ module.exports = class Core {
106
108
  signer: opts.keyPair || crypto.keyPair(),
107
109
  hints: {
108
110
  reorgs: []
109
- }
111
+ },
112
+ contiguousLength: 0
110
113
  }
111
114
 
112
115
  await oplog.flush(header)
@@ -192,6 +195,16 @@ module.exports = class Core {
192
195
  await this.blocks.put(index, value, byteOffset)
193
196
  }
194
197
 
198
+ _updateContiguousLength (index, length = 0) {
199
+ if (index === this.header.contiguousLength) {
200
+ let i = this.header.contiguousLength + length
201
+
202
+ while (this.bitfield.get(i)) i++
203
+
204
+ this.header.contiguousLength = i
205
+ }
206
+ }
207
+
195
208
  async userData (key, value) {
196
209
  // TODO: each oplog append can set user data, so we should have a way
197
210
  // to just hitch a ride on one of the other ongoing appends?
@@ -271,6 +284,7 @@ module.exports = class Core {
271
284
  this.bitfield.setRange(batch.ancestors, batch.length - batch.ancestors, true)
272
285
  batch.commit()
273
286
 
287
+ this.header.contiguousLength = batch.length
274
288
  this.header.tree.length = batch.length
275
289
  this.header.tree.rootHash = hash
276
290
  this.header.tree.signature = batch.signature
@@ -312,7 +326,11 @@ module.exports = class Core {
312
326
 
313
327
  await this.oplog.append([entry], false)
314
328
 
315
- if (bitfield) this.bitfield.set(bitfield.start, true)
329
+ if (bitfield) {
330
+ this.bitfield.set(bitfield.start, true)
331
+ this._updateContiguousLength(bitfield.start, bitfield.length)
332
+ }
333
+
316
334
  batch.commit()
317
335
 
318
336
  this.header.tree.fork = batch.fork
@@ -366,8 +384,13 @@ module.exports = class Core {
366
384
  continue
367
385
  }
368
386
 
369
- if (bitfield) this.bitfield.set(bitfield.start, true)
387
+ if (bitfield) {
388
+ this.bitfield.set(bitfield.start, true)
389
+ this._updateContiguousLength(bitfield.start, bitfield.length)
390
+ }
391
+
370
392
  batch.commit()
393
+
371
394
  this.onupdate(0, bitfield, value, from)
372
395
  }
373
396
 
@@ -448,6 +471,7 @@ module.exports = class Core {
448
471
 
449
472
  const appended = batch.length > batch.ancestors
450
473
 
474
+ this.header.contiguousLength = Math.min(batch.ancestors, this.header.contiguousLength)
451
475
  this.header.tree.fork = batch.fork
452
476
  this.header.tree.length = batch.length
453
477
  this.header.tree.rootHash = batch.hash()
package/lib/messages.js CHANGED
@@ -622,6 +622,7 @@ oplog.header = {
622
622
  treeHeader.preencode(state, h.tree)
623
623
  keyPair.preencode(state, h.signer)
624
624
  hints.preencode(state, h.hints)
625
+ c.uint.preencode(state, h.contiguousLength)
625
626
  },
626
627
  encode (state, h) {
627
628
  state.buffer[state.start++] = 0 // version
@@ -630,6 +631,7 @@ oplog.header = {
630
631
  treeHeader.encode(state, h.tree)
631
632
  keyPair.encode(state, h.signer)
632
633
  hints.encode(state, h.hints)
634
+ c.uint.encode(state, h.contiguousLength)
633
635
  },
634
636
  decode (state) {
635
637
  const version = c.uint.decode(state)
@@ -643,7 +645,8 @@ oplog.header = {
643
645
  userData: keyValueArray.decode(state),
644
646
  tree: treeHeader.decode(state),
645
647
  signer: keyPair.decode(state),
646
- hints: hints.decode(state)
648
+ hints: hints.decode(state),
649
+ contiguousLength: state.end > state.start ? c.uint.decode(state) : 0
647
650
  }
648
651
  }
649
652
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.38",
3
+ "version": "10.0.0-alpha.40",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -54,6 +54,7 @@
54
54
  "brittle": "^2.0.0",
55
55
  "hyperswarm": "next",
56
56
  "random-access-memory": "^4.1.0",
57
+ "random-access-memory-overlay": "^1.0.0",
57
58
  "standard": "^16.0.3",
58
59
  "tmp-promise": "^3.0.2"
59
60
  },
package/CHANGELOG.md DELETED
@@ -1,37 +0,0 @@
1
- # CHANGELOG
2
-
3
- See [UPGRADE.md](UPGRADE.md) for notes on breaking changes for downstream developers.
4
-
5
- ## Current
6
-
7
- ## v9.5.0
8
-
9
- - Feed close makes the replication detach the channel used for this particular stream.
10
-
11
- ## v9.4.0
12
-
13
- - feed.get accepts onwait hook, that is called if get is waiting for a network peer.
14
-
15
- ## v9.3.0
16
-
17
- - feed.get returns an id, that can be used to cancel a pending get with feed.cancel(getId)
18
-
19
- ## v9.2.0
20
-
21
- - Add `maxBlockSize` on write streams to auto chunk big blocks being written.
22
-
23
- ## v9.1.0
24
-
25
- - Make peer.remoteOpened public ([#268](https://github.com/hypercore-protocol/hypercore/pull/268))
26
-
27
- ## v9.0.1
28
-
29
- - Upgraded standard to v14 with subsequent formatting tweaks
30
- - createReadStream is up to 8x faster now! ([#261](https://github.com/hypercore-protocol/hypercore/pull/261) by [@tinchoz49](https://github.com/tinchoz49))
31
- - Fixed benchmarks ([#266](https://github.com/hypercore-protocol/hypercore/pull/266) by [@fwip](https://github.com/fwip))
32
-
33
- ## v9.0.0
34
-
35
- - Ease of use update to signatures, https://github.com/mafintosh/hypercore/issues/260
36
- - Updates [noise-protocol](https://github.com/emilbayes/noise-protocol) to latest, which uses chacha instead of xchacha and moves avoid from the sodium kx api for better compatability with the rest of the Noise ecosystem.
37
- - Updates [sodium-native](https://github.com/sodium-friends/sodium-native) from 2 to 3 across the board. 3 uses n-api, so no more builds needed when Node is released.