hypercore 10.0.0-alpha.5 → 10.0.0-alpha.50
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 +68 -5
- package/index.js +594 -173
- package/lib/bitfield.js +9 -5
- package/lib/block-encryption.js +68 -0
- package/lib/block-store.js +3 -1
- package/lib/caps.js +32 -0
- package/lib/core.js +88 -27
- package/lib/errors.js +50 -0
- package/lib/merkle-tree.js +186 -113
- package/lib/messages.js +249 -168
- package/lib/oplog.js +4 -3
- package/lib/replicator.js +1385 -616
- package/lib/streams.js +56 -0
- package/package.json +18 -9
- package/.github/workflows/test-node.yml +0 -23
- package/CHANGELOG.md +0 -37
- package/UPGRADE.md +0 -9
- package/examples/announce.js +0 -19
- package/examples/basic.js +0 -10
- package/examples/http.js +0 -123
- package/examples/lookup.js +0 -20
- package/lib/extensions.js +0 -76
- package/lib/protocol.js +0 -524
- package/lib/random-iterator.js +0 -46
- package/test/basic.js +0 -78
- package/test/bitfield.js +0 -71
- package/test/core.js +0 -290
- package/test/encodings.js +0 -18
- package/test/extension.js +0 -71
- package/test/helpers/index.js +0 -23
- package/test/merkle-tree.js +0 -518
- package/test/mutex.js +0 -137
- package/test/oplog.js +0 -399
- package/test/preload.js +0 -72
- package/test/replicate.js +0 -333
- package/test/sessions.js +0 -173
- package/test/user-data.js +0 -47
package/README.md
CHANGED
|
@@ -62,13 +62,19 @@ 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
|
|
68
|
+
keyPair: kp, // optionally pass the public key and secret key as a key pair
|
|
69
|
+
encryptionKey: k, // optionally pass an encryption key to enable block encryption
|
|
70
|
+
onwait: () => {} // hook that is called if gets are waiting for download
|
|
67
71
|
}
|
|
68
72
|
```
|
|
69
73
|
|
|
70
74
|
You can also set valueEncoding to any [abstract-encoding](https://github.com/mafintosh/abstract-encoding) or [compact-encoding](https://github.com/compact-encoding) instance.
|
|
71
75
|
|
|
76
|
+
valueEncodings will be applied to individually 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`.
|
|
77
|
+
|
|
72
78
|
#### `const seq = await core.append(block)`
|
|
73
79
|
|
|
74
80
|
Append a block of data (or an array of blocks) to the core.
|
|
@@ -83,7 +89,7 @@ Options include
|
|
|
83
89
|
|
|
84
90
|
``` js
|
|
85
91
|
{
|
|
86
|
-
wait: true, // wait for
|
|
92
|
+
wait: true, // wait for block to be downloaded
|
|
87
93
|
onwait: () => {}, // hook that is called if the get is waiting for download
|
|
88
94
|
timeout: 0, // wait at max some milliseconds (0 means no timeout)
|
|
89
95
|
valueEncoding: 'json' | 'utf-8' | 'binary' // defaults to the core's valueEncoding
|
|
@@ -97,6 +103,23 @@ Truncate the core to a smaller length.
|
|
|
97
103
|
Per default this will update the fork id of the core to `+ 1`, but you can set the fork id you prefer with the option.
|
|
98
104
|
Note that the fork id should be monotonely incrementing.
|
|
99
105
|
|
|
106
|
+
#### `const hash = await core.treeHash([length])`
|
|
107
|
+
|
|
108
|
+
Get the Merkle Tree hash of the core at a given length, defaulting to the current length of the core.
|
|
109
|
+
|
|
110
|
+
#### `const stream = core.createReadStream([options])`
|
|
111
|
+
|
|
112
|
+
Make a read stream. Options include:
|
|
113
|
+
|
|
114
|
+
``` js
|
|
115
|
+
{
|
|
116
|
+
start: 0,
|
|
117
|
+
end: core.length,
|
|
118
|
+
live: false,
|
|
119
|
+
snapshot: true // auto set end to core.length on open or update it on every read
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
100
123
|
#### `const range = core.download([range])`
|
|
101
124
|
|
|
102
125
|
Download a range of data.
|
|
@@ -113,6 +136,7 @@ A range can have the following properties:
|
|
|
113
136
|
{
|
|
114
137
|
start: startIndex,
|
|
115
138
|
end: nonInclusiveEndIndex,
|
|
139
|
+
blocks: [index1, index2, ...],
|
|
116
140
|
linear: false // download range linearly and not randomly
|
|
117
141
|
}
|
|
118
142
|
```
|
|
@@ -125,6 +149,12 @@ To download the full core continously (often referred to as non sparse mode) do
|
|
|
125
149
|
core.download({ start: 0, end: -1 })
|
|
126
150
|
```
|
|
127
151
|
|
|
152
|
+
To downloaded a discrete range of blocks pass a list of indices.
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
core.download({ blocks: [4, 9, 7] });
|
|
156
|
+
```
|
|
157
|
+
|
|
128
158
|
To cancel downloading a range simply destroy the range instance.
|
|
129
159
|
|
|
130
160
|
``` js
|
|
@@ -188,6 +218,12 @@ Buffer containing the public key identifying this core.
|
|
|
188
218
|
|
|
189
219
|
Populated after `ready` has been emitted. Will be `null` before the event.
|
|
190
220
|
|
|
221
|
+
#### `core.keyPair`
|
|
222
|
+
|
|
223
|
+
Object containing buffers of the core's public and secret key
|
|
224
|
+
|
|
225
|
+
Populated after `ready` has been emitted. Will be `null` before the event.
|
|
226
|
+
|
|
191
227
|
#### `core.discoveryKey`
|
|
192
228
|
|
|
193
229
|
Buffer containing a key derived from the core's public key.
|
|
@@ -195,15 +231,31 @@ In contrast to `core.key` this key does not allow you to verify the data but can
|
|
|
195
231
|
|
|
196
232
|
Populated after `ready` has been emitted. Will be `null` before the event.
|
|
197
233
|
|
|
234
|
+
#### `core.encryptionKey`
|
|
235
|
+
|
|
236
|
+
Buffer containing the optional block encryption key of this core. Will be `null` unless block encryption is enabled.
|
|
237
|
+
|
|
198
238
|
#### `core.length`
|
|
199
239
|
|
|
200
|
-
How many blocks of data are available on this core?
|
|
240
|
+
How many blocks of data are available on this core? If `sparse: false`, this will equal `core.contiguousLength`.
|
|
201
241
|
|
|
202
242
|
Populated after `ready` has been emitted. Will be `0` before the event.
|
|
203
243
|
|
|
204
244
|
#### `core.byteLength`
|
|
205
245
|
|
|
206
|
-
How much data is available on this core in bytes?
|
|
246
|
+
How much data is available on this core in bytes? If `sparse: false`, this will equal `core.contiguousByteLength`.
|
|
247
|
+
|
|
248
|
+
Populated after `ready` has been emitted. Will be `0` before the event.
|
|
249
|
+
|
|
250
|
+
#### `core.contiguousLength`
|
|
251
|
+
|
|
252
|
+
How many blocks are contiguously available starting from the first block of this core?
|
|
253
|
+
|
|
254
|
+
Populated after `ready` has been emitted. Will be `0` before the event.
|
|
255
|
+
|
|
256
|
+
#### `core.contiguousByteLength`
|
|
257
|
+
|
|
258
|
+
How much data is contiguously available starting from the first block of this core?
|
|
207
259
|
|
|
208
260
|
Populated after `ready` has been emitted. Will be `0` before the event.
|
|
209
261
|
|
|
@@ -213,6 +265,10 @@ What is the current fork id of this core?
|
|
|
213
265
|
|
|
214
266
|
Populated after `ready` has been emitted. Will be `0` before the event.
|
|
215
267
|
|
|
268
|
+
#### `core.padding`
|
|
269
|
+
|
|
270
|
+
How much padding is applied to each block of this core? Will be `0` unless block encryption is enabled.
|
|
271
|
+
|
|
216
272
|
#### `const stream = core.replicate(isInitiatorOrReplicationStream)`
|
|
217
273
|
|
|
218
274
|
Create a replication stream. You should pipe this to another Hypercore instance.
|
|
@@ -238,10 +294,17 @@ const socket = net.connect(...)
|
|
|
238
294
|
socket.pipe(localCore.replicate(true)).pipe(socket)
|
|
239
295
|
```
|
|
240
296
|
|
|
297
|
+
#### `const done = core.findingPeers()`
|
|
298
|
+
|
|
299
|
+
Create a hook that tells Hypercore you are finding peers for this core in the background. Call `done` when your current discovery iteration is done.
|
|
300
|
+
If you're using Hyperswarm, you'd normally call this after a `swarm.flush()` finishes.
|
|
301
|
+
|
|
302
|
+
This allows `core.update` to wait for either the `findingPeers` hook to finish or one peer to appear before deciding whether it should wait for a merkle tree update before returning.
|
|
303
|
+
|
|
241
304
|
#### `core.on('append')`
|
|
242
305
|
|
|
243
306
|
Emitted when the core has been appended to (i.e. has a new length / byteLength), either locally or remotely.
|
|
244
307
|
|
|
245
|
-
#### `core.on('truncate')`
|
|
308
|
+
#### `core.on('truncate', ancestors, forkId)`
|
|
246
309
|
|
|
247
310
|
Emitted when the core has been truncated, either locally or remotely.
|