hypercore 11.11.0 → 11.11.2
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 +23 -2
- package/index.js +5 -0
- package/lib/merkle-tree.js +1 -1
- package/lib/replicator.js +3 -2
- package/lib/session-state.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,15 +61,24 @@ Alternatively you can pass a [Hypercore Storage](https://github.com/holepunchto/
|
|
|
61
61
|
ongc: (session) => { ... }, // A callback called when the session is garbage collected
|
|
62
62
|
notDownloadingLinger: 20000, // How many milliseconds to wait after downloading finishes keeping the connection open. Defaults to a random number between 20-40s
|
|
63
63
|
allowFork: true, // Enables updating core when it forks
|
|
64
|
+
userData: { foo: 'bar' }, // An object to assign to the local User Uata storage
|
|
64
65
|
}
|
|
65
66
|
```
|
|
66
67
|
|
|
67
|
-
You can also set valueEncoding to any [compact-encoding](https://github.com/compact-encoding) instance.
|
|
68
|
+
You can also set `valueEncoding` to any [compact-encoding](https://github.com/compact-encoding) instance.
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
`valueEncoding`s will be applied to individual blocks, even if you append batches. If you want to control encoding at the batch-level, you can use the `encodeBatch` option, which is a function that takes a batch and returns a binary-encoded batch. If you provide a custom `valueEncoding`, it will not be applied prior to `encodeBatch`.
|
|
70
71
|
|
|
71
72
|
The user may provide a custom encryption module as `opts.encryption`, which should satisfy the [HypercoreEncryption](https://github.com/holepunchto/hypercore-encryption) interface.
|
|
72
73
|
|
|
74
|
+
##### User Data
|
|
75
|
+
|
|
76
|
+
User Data is a key-value store that is persisted locally and is not replicated with the Hypercore. This is useful as a quick store for data only used by the current peer. For example, `autobase` uses User Data to store information such as encryption keys and connections between a peer's local writer and the Autobase's bootstrap core.
|
|
77
|
+
|
|
78
|
+
Keys are always strings and values can be strings or buffers.
|
|
79
|
+
|
|
80
|
+
See [`core.setUserData(key, value)`](#await-coresetuserdatakey-value) and [`core.getUserData(key)`](#const-value--await-coregetuserdatakey) for updating User Data.
|
|
81
|
+
|
|
73
82
|
#### `const { length, byteLength } = await core.append(block)`
|
|
74
83
|
|
|
75
84
|
Append a block of data (or an array of blocks) to the core.
|
|
@@ -455,6 +464,18 @@ Populated after `ready` has been emitted. Will be `0` before the event.
|
|
|
455
464
|
|
|
456
465
|
How much padding is applied to each block of this core? Will be `0` unless block encryption is enabled.
|
|
457
466
|
|
|
467
|
+
#### `await core.setUserData(key, value)`
|
|
468
|
+
|
|
469
|
+
Set a key in the User Data key-value store.
|
|
470
|
+
|
|
471
|
+
`key` is a string and `value` can be a string or buffer.
|
|
472
|
+
|
|
473
|
+
#### `const value = await core.getUserData(key)`
|
|
474
|
+
|
|
475
|
+
Return the value for a key in the User Data key-value store.
|
|
476
|
+
|
|
477
|
+
`key` is a string.
|
|
478
|
+
|
|
458
479
|
#### `const stream = core.replicate(isInitiatorOrReplicationStream)`
|
|
459
480
|
|
|
460
481
|
Create a replication stream. You should pipe this to another Hypercore instance.
|
package/index.js
CHANGED
|
@@ -71,6 +71,7 @@ class Hypercore extends EventEmitter {
|
|
|
71
71
|
this.closed = false
|
|
72
72
|
this.weak = !!opts.weak
|
|
73
73
|
this.snapshotted = !!opts.snapshot
|
|
74
|
+
this.onseq = opts.onseq || null
|
|
74
75
|
this.onwait = opts.onwait || null
|
|
75
76
|
this.wait = opts.wait !== false
|
|
76
77
|
this.timeout = opts.timeout || 0
|
|
@@ -219,6 +220,7 @@ class Hypercore extends EventEmitter {
|
|
|
219
220
|
const wait = opts.wait === false ? false : this.wait
|
|
220
221
|
const writable = opts.writable === undefined ? !this._readonly : opts.writable === true
|
|
221
222
|
const onwait = opts.onwait === undefined ? this.onwait : opts.onwait
|
|
223
|
+
const onseq = opts.onseq === undefined ? this.onseq : opts.onseq
|
|
222
224
|
const timeout = opts.timeout === undefined ? this.timeout : opts.timeout
|
|
223
225
|
const weak = opts.weak === undefined ? this.weak : opts.weak
|
|
224
226
|
const Clz = opts.class || Hypercore
|
|
@@ -226,6 +228,7 @@ class Hypercore extends EventEmitter {
|
|
|
226
228
|
...opts,
|
|
227
229
|
wait,
|
|
228
230
|
onwait,
|
|
231
|
+
onseq,
|
|
229
232
|
timeout,
|
|
230
233
|
writable,
|
|
231
234
|
weak,
|
|
@@ -768,6 +771,8 @@ class Hypercore extends EventEmitter {
|
|
|
768
771
|
|
|
769
772
|
const encoding = (opts && opts.valueEncoding && c.from(opts.valueEncoding)) || this.valueEncoding
|
|
770
773
|
|
|
774
|
+
if (this.onseq !== null) this.onseq(index, this)
|
|
775
|
+
|
|
771
776
|
const req = this._get(index, opts)
|
|
772
777
|
|
|
773
778
|
let block = await req
|
package/lib/merkle-tree.js
CHANGED
|
@@ -1092,7 +1092,7 @@ function nodesToRoot (index, nodes, head) {
|
|
|
1092
1092
|
|
|
1093
1093
|
for (let i = 0; i < nodes; i++) {
|
|
1094
1094
|
ite.parent()
|
|
1095
|
-
if (ite.contains(head)) throw
|
|
1095
|
+
if (ite.contains(head)) throw INVALID_OPERATION('Nodes is out of bounds')
|
|
1096
1096
|
}
|
|
1097
1097
|
|
|
1098
1098
|
return ite.index
|
package/lib/replicator.js
CHANGED
|
@@ -927,6 +927,7 @@ class Peer {
|
|
|
927
927
|
this._checkIfConflict()
|
|
928
928
|
}
|
|
929
929
|
|
|
930
|
+
this.paused = true
|
|
930
931
|
this.replicator._onnodata(this, req)
|
|
931
932
|
this.replicator._oninvalid(err, req, data, this)
|
|
932
933
|
return
|
|
@@ -2281,7 +2282,7 @@ module.exports = class Replicator {
|
|
|
2281
2282
|
}
|
|
2282
2283
|
|
|
2283
2284
|
_updatePeer (peer) {
|
|
2284
|
-
if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
|
|
2285
|
+
if (!peer.isActive() || peer.inflight >= peer.getMaxInflight() || !peer.remoteUploading) {
|
|
2285
2286
|
return false
|
|
2286
2287
|
}
|
|
2287
2288
|
|
|
@@ -2313,7 +2314,7 @@ module.exports = class Replicator {
|
|
|
2313
2314
|
}
|
|
2314
2315
|
|
|
2315
2316
|
_updatePeerNonPrimary (peer) {
|
|
2316
|
-
if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
|
|
2317
|
+
if (!peer.isActive() || peer.inflight >= peer.getMaxInflight() || !peer.remoteUploading) {
|
|
2317
2318
|
return false
|
|
2318
2319
|
}
|
|
2319
2320
|
|
package/lib/session-state.js
CHANGED
|
@@ -483,7 +483,8 @@ module.exports = class SessionState {
|
|
|
483
483
|
}
|
|
484
484
|
}
|
|
485
485
|
|
|
486
|
-
tx.
|
|
486
|
+
if ((end - start) === 1) tx.deleteBlock(start)
|
|
487
|
+
else tx.deleteBlockRange(start, end)
|
|
487
488
|
|
|
488
489
|
const dependency = start < this.flushedLength() ? updateDependency(this, start, true) : null
|
|
489
490
|
|