hypercore-storage 0.0.40 → 1.0.0

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/package.json CHANGED
@@ -1,33 +1,48 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "0.0.40",
3
+ "version": "1.0.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
7
- "lib/*.js"
7
+ "lib/*.js",
8
+ "spec/hyperschema/*.js",
9
+ "migrations/0/*.js"
8
10
  ],
9
11
  "scripts": {
10
- "test": "standard && brittle test/*.js"
12
+ "test": "standard && node test/all.js",
13
+ "test:bare": "bare test/all.js",
14
+ "test:generate": "brittle -r test/all.js test/*.js"
11
15
  },
12
- "author": "Holepunch",
16
+ "author": "Holepunch Inc.",
13
17
  "license": "Apache-2.0",
14
- "description": "RocksDB storage driver for Hypercore",
18
+ "description": "Storage engine for Hypercore",
19
+ "imports": {
20
+ "fs": {
21
+ "bare": "bare-fs",
22
+ "default": "fs"
23
+ },
24
+ "path": {
25
+ "bare": "bare-path",
26
+ "default": "path"
27
+ }
28
+ },
15
29
  "dependencies": {
16
- "b4a": "^1.6.6",
17
- "compact-encoding": "^2.15.0",
18
- "flat-tree": "^1.10.0",
19
- "hypercore-errors": "^1.3.0",
20
- "index-encoder": "^3.0.1",
21
- "nanoassert": "^2.0.0",
22
- "queue-tick": "^1.0.1",
23
- "read-write-mutexify": "^2.1.0",
30
+ "b4a": "^1.6.7",
31
+ "bare-fs": "^4.0.1",
32
+ "bare-path": "^3.0.0",
33
+ "compact-encoding": "^2.16.0",
34
+ "flat-tree": "^1.12.1",
35
+ "hypercore-crypto": "^3.4.2",
36
+ "hyperschema": "^1.7.0",
37
+ "index-encoder": "^3.3.2",
24
38
  "resolve-reject-promise": "^1.0.0",
25
- "rocksdb-native": "^3.0.0",
26
- "streamx": "^2.20.1"
39
+ "rocksdb-native": "^3.1.1",
40
+ "scope-lock": "^1.2.4",
41
+ "streamx": "^2.21.1"
27
42
  },
28
43
  "devDependencies": {
29
- "brittle": "^3.5.2",
30
- "standard": "^17.1.0",
31
- "test-tmp": "^1.2.1"
44
+ "brittle": "^3.7.0",
45
+ "standard": "^17.1.2",
46
+ "test-tmp": "^1.3.1"
32
47
  }
33
48
  }
@@ -0,0 +1,510 @@
1
+ // This file is autogenerated by the hyperschema compiler
2
+ // Schema Version: 1
3
+ /* eslint-disable camelcase */
4
+ /* eslint-disable quotes */
5
+
6
+ const VERSION = 1
7
+ const { c } = require('hyperschema/runtime')
8
+
9
+ // eslint-disable-next-line no-unused-vars
10
+ let version = VERSION
11
+
12
+ // @corestore/allocated
13
+ const encoding0 = {
14
+ preencode (state, m) {
15
+ c.uint.preencode(state, m.cores)
16
+ c.uint.preencode(state, m.datas)
17
+ },
18
+ encode (state, m) {
19
+ c.uint.encode(state, m.cores)
20
+ c.uint.encode(state, m.datas)
21
+ },
22
+ decode (state) {
23
+ const r0 = c.uint.decode(state)
24
+ const r1 = c.uint.decode(state)
25
+
26
+ return {
27
+ cores: r0,
28
+ datas: r1
29
+ }
30
+ }
31
+ }
32
+
33
+ // @corestore/head
34
+ const encoding1 = {
35
+ preencode (state, m) {
36
+ c.uint.preencode(state, m.version)
37
+ state.end++ // max flag is 4 so always one byte
38
+
39
+ if (m.allocated) encoding0.preencode(state, m.allocated)
40
+ if (m.seed) c.fixed32.preencode(state, m.seed)
41
+ if (m.defaultDiscoveryKey) c.fixed32.preencode(state, m.defaultDiscoveryKey)
42
+ },
43
+ encode (state, m) {
44
+ const flags =
45
+ (m.allocated ? 1 : 0) |
46
+ (m.seed ? 2 : 0) |
47
+ (m.defaultDiscoveryKey ? 4 : 0)
48
+
49
+ c.uint.encode(state, m.version)
50
+ c.uint.encode(state, flags)
51
+
52
+ if (m.allocated) encoding0.encode(state, m.allocated)
53
+ if (m.seed) c.fixed32.encode(state, m.seed)
54
+ if (m.defaultDiscoveryKey) c.fixed32.encode(state, m.defaultDiscoveryKey)
55
+ },
56
+ decode (state) {
57
+ const r0 = c.uint.decode(state)
58
+ const flags = c.uint.decode(state)
59
+
60
+ return {
61
+ version: r0,
62
+ allocated: (flags & 1) !== 0 ? encoding0.decode(state) : null,
63
+ seed: (flags & 2) !== 0 ? c.fixed32.decode(state) : null,
64
+ defaultDiscoveryKey: (flags & 4) !== 0 ? c.fixed32.decode(state) : null
65
+ }
66
+ }
67
+ }
68
+
69
+ // @corestore/alias
70
+ const encoding2 = {
71
+ preencode (state, m) {
72
+ c.string.preencode(state, m.name)
73
+ c.fixed32.preencode(state, m.namespace)
74
+ },
75
+ encode (state, m) {
76
+ c.string.encode(state, m.name)
77
+ c.fixed32.encode(state, m.namespace)
78
+ },
79
+ decode (state) {
80
+ const r0 = c.string.decode(state)
81
+ const r1 = c.fixed32.decode(state)
82
+
83
+ return {
84
+ name: r0,
85
+ namespace: r1
86
+ }
87
+ }
88
+ }
89
+
90
+ // @corestore/core
91
+ const encoding3 = {
92
+ preencode (state, m) {
93
+ c.uint.preencode(state, m.version)
94
+ c.uint.preencode(state, m.corePointer)
95
+ c.uint.preencode(state, m.dataPointer)
96
+ state.end++ // max flag is 1 so always one byte
97
+
98
+ if (m.alias) encoding2.preencode(state, m.alias)
99
+ },
100
+ encode (state, m) {
101
+ const flags = m.alias ? 1 : 0
102
+
103
+ c.uint.encode(state, m.version)
104
+ c.uint.encode(state, m.corePointer)
105
+ c.uint.encode(state, m.dataPointer)
106
+ c.uint.encode(state, flags)
107
+
108
+ if (m.alias) encoding2.encode(state, m.alias)
109
+ },
110
+ decode (state) {
111
+ const r0 = c.uint.decode(state)
112
+ const r1 = c.uint.decode(state)
113
+ const r2 = c.uint.decode(state)
114
+ const flags = c.uint.decode(state)
115
+
116
+ return {
117
+ version: r0,
118
+ corePointer: r1,
119
+ dataPointer: r2,
120
+ alias: (flags & 1) !== 0 ? encoding2.decode(state) : null
121
+ }
122
+ }
123
+ }
124
+
125
+ const encoding4_enum = {
126
+ blake2b: 'blake2b'
127
+ }
128
+
129
+ // @core/hashes enum
130
+ const encoding4 = {
131
+ preencode (state, m) {
132
+ state.end++ // max enum is 0 so always one byte
133
+ },
134
+ encode (state, m) {
135
+ switch (m) {
136
+ case 'blake2b':
137
+ c.uint.encode(state, 0)
138
+ break
139
+ default: throw new Error('Unknown enum')
140
+ }
141
+ },
142
+ decode (state) {
143
+ switch (c.uint.decode(state)) {
144
+ case 0: return 'blake2b'
145
+ default: return null
146
+ }
147
+ }
148
+ }
149
+
150
+ const encoding5_enum = {
151
+ ed25519: 'ed25519'
152
+ }
153
+
154
+ // @core/signatures enum
155
+ const encoding5 = {
156
+ preencode (state, m) {
157
+ state.end++ // max enum is 0 so always one byte
158
+ },
159
+ encode (state, m) {
160
+ switch (m) {
161
+ case 'ed25519':
162
+ c.uint.encode(state, 0)
163
+ break
164
+ default: throw new Error('Unknown enum')
165
+ }
166
+ },
167
+ decode (state) {
168
+ switch (c.uint.decode(state)) {
169
+ case 0: return 'ed25519'
170
+ default: return null
171
+ }
172
+ }
173
+ }
174
+
175
+ // @core/tree-node
176
+ const encoding6 = {
177
+ preencode (state, m) {
178
+ c.uint.preencode(state, m.index)
179
+ c.uint.preencode(state, m.size)
180
+ c.fixed32.preencode(state, m.hash)
181
+ },
182
+ encode (state, m) {
183
+ c.uint.encode(state, m.index)
184
+ c.uint.encode(state, m.size)
185
+ c.fixed32.encode(state, m.hash)
186
+ },
187
+ decode (state) {
188
+ const r0 = c.uint.decode(state)
189
+ const r1 = c.uint.decode(state)
190
+ const r2 = c.fixed32.decode(state)
191
+
192
+ return {
193
+ index: r0,
194
+ size: r1,
195
+ hash: r2
196
+ }
197
+ }
198
+ }
199
+
200
+ // @core/signer
201
+ const encoding7 = {
202
+ preencode (state, m) {
203
+ encoding5.preencode(state, m.signature)
204
+ c.fixed32.preencode(state, m.namespace)
205
+ c.fixed32.preencode(state, m.publicKey)
206
+ },
207
+ encode (state, m) {
208
+ encoding5.encode(state, m.signature)
209
+ c.fixed32.encode(state, m.namespace)
210
+ c.fixed32.encode(state, m.publicKey)
211
+ },
212
+ decode (state) {
213
+ const r0 = encoding5.decode(state)
214
+ const r1 = c.fixed32.decode(state)
215
+ const r2 = c.fixed32.decode(state)
216
+
217
+ return {
218
+ signature: r0,
219
+ namespace: r1,
220
+ publicKey: r2
221
+ }
222
+ }
223
+ }
224
+
225
+ // @core/prologue
226
+ const encoding8 = {
227
+ preencode (state, m) {
228
+ c.fixed32.preencode(state, m.hash)
229
+ c.uint.preencode(state, m.length)
230
+ },
231
+ encode (state, m) {
232
+ c.fixed32.encode(state, m.hash)
233
+ c.uint.encode(state, m.length)
234
+ },
235
+ decode (state) {
236
+ const r0 = c.fixed32.decode(state)
237
+ const r1 = c.uint.decode(state)
238
+
239
+ return {
240
+ hash: r0,
241
+ length: r1
242
+ }
243
+ }
244
+ }
245
+
246
+ // @core/manifest.signers
247
+ const encoding9_4 = c.array(encoding7)
248
+
249
+ // @core/manifest
250
+ const encoding9 = {
251
+ preencode (state, m) {
252
+ c.uint.preencode(state, m.version)
253
+ state.end++ // max flag is 2 so always one byte
254
+ encoding4.preencode(state, m.hash)
255
+ c.uint.preencode(state, m.quorum)
256
+ encoding9_4.preencode(state, m.signers)
257
+
258
+ if (m.prologue) encoding8.preencode(state, m.prologue)
259
+ },
260
+ encode (state, m) {
261
+ const flags =
262
+ (m.allowPatch ? 1 : 0) |
263
+ (m.prologue ? 2 : 0)
264
+
265
+ c.uint.encode(state, m.version)
266
+ c.uint.encode(state, flags)
267
+ encoding4.encode(state, m.hash)
268
+ c.uint.encode(state, m.quorum)
269
+ encoding9_4.encode(state, m.signers)
270
+
271
+ if (m.prologue) encoding8.encode(state, m.prologue)
272
+ },
273
+ decode (state) {
274
+ const r0 = c.uint.decode(state)
275
+ const flags = c.uint.decode(state)
276
+
277
+ return {
278
+ version: r0,
279
+ hash: encoding4.decode(state),
280
+ quorum: c.uint.decode(state),
281
+ allowPatch: (flags & 1) !== 0,
282
+ signers: encoding9_4.decode(state),
283
+ prologue: (flags & 2) !== 0 ? encoding8.decode(state) : null
284
+ }
285
+ }
286
+ }
287
+
288
+ // @core/keyPair
289
+ const encoding10 = {
290
+ preencode (state, m) {
291
+ c.buffer.preencode(state, m.publicKey)
292
+ c.buffer.preencode(state, m.secretKey)
293
+ },
294
+ encode (state, m) {
295
+ c.buffer.encode(state, m.publicKey)
296
+ c.buffer.encode(state, m.secretKey)
297
+ },
298
+ decode (state) {
299
+ const r0 = c.buffer.decode(state)
300
+ const r1 = c.buffer.decode(state)
301
+
302
+ return {
303
+ publicKey: r0,
304
+ secretKey: r1
305
+ }
306
+ }
307
+ }
308
+
309
+ // @core/auth.manifest
310
+ const encoding11_2 = c.frame(encoding9)
311
+
312
+ // @core/auth
313
+ const encoding11 = {
314
+ preencode (state, m) {
315
+ c.fixed32.preencode(state, m.key)
316
+ c.fixed32.preencode(state, m.discoveryKey)
317
+ state.end++ // max flag is 4 so always one byte
318
+
319
+ if (m.manifest) encoding11_2.preencode(state, m.manifest)
320
+ if (m.keyPair) encoding10.preencode(state, m.keyPair)
321
+ if (m.encryptionKey) c.buffer.preencode(state, m.encryptionKey)
322
+ },
323
+ encode (state, m) {
324
+ const flags =
325
+ (m.manifest ? 1 : 0) |
326
+ (m.keyPair ? 2 : 0) |
327
+ (m.encryptionKey ? 4 : 0)
328
+
329
+ c.fixed32.encode(state, m.key)
330
+ c.fixed32.encode(state, m.discoveryKey)
331
+ c.uint.encode(state, flags)
332
+
333
+ if (m.manifest) encoding11_2.encode(state, m.manifest)
334
+ if (m.keyPair) encoding10.encode(state, m.keyPair)
335
+ if (m.encryptionKey) c.buffer.encode(state, m.encryptionKey)
336
+ },
337
+ decode (state) {
338
+ const r0 = c.fixed32.decode(state)
339
+ const r1 = c.fixed32.decode(state)
340
+ const flags = c.uint.decode(state)
341
+
342
+ return {
343
+ key: r0,
344
+ discoveryKey: r1,
345
+ manifest: (flags & 1) !== 0 ? encoding11_2.decode(state) : null,
346
+ keyPair: (flags & 2) !== 0 ? encoding10.decode(state) : null,
347
+ encryptionKey: (flags & 4) !== 0 ? c.buffer.decode(state) : null
348
+ }
349
+ }
350
+ }
351
+
352
+ // @core/head
353
+ const encoding12 = {
354
+ preencode (state, m) {
355
+ c.uint.preencode(state, m.fork)
356
+ c.uint.preencode(state, m.length)
357
+ c.fixed32.preencode(state, m.rootHash)
358
+ c.buffer.preencode(state, m.signature)
359
+ },
360
+ encode (state, m) {
361
+ c.uint.encode(state, m.fork)
362
+ c.uint.encode(state, m.length)
363
+ c.fixed32.encode(state, m.rootHash)
364
+ c.buffer.encode(state, m.signature)
365
+ },
366
+ decode (state) {
367
+ const r0 = c.uint.decode(state)
368
+ const r1 = c.uint.decode(state)
369
+ const r2 = c.fixed32.decode(state)
370
+ const r3 = c.buffer.decode(state)
371
+
372
+ return {
373
+ fork: r0,
374
+ length: r1,
375
+ rootHash: r2,
376
+ signature: r3
377
+ }
378
+ }
379
+ }
380
+
381
+ // @core/hints
382
+ const encoding13 = {
383
+ preencode (state, m) {
384
+ state.end++ // max flag is 1 so always one byte
385
+
386
+ if (m.contiguousLength) c.uint.preencode(state, m.contiguousLength)
387
+ },
388
+ encode (state, m) {
389
+ const flags = m.contiguousLength ? 1 : 0
390
+
391
+ c.uint.encode(state, flags)
392
+
393
+ if (m.contiguousLength) c.uint.encode(state, m.contiguousLength)
394
+ },
395
+ decode (state) {
396
+ const flags = c.uint.decode(state)
397
+
398
+ return {
399
+ contiguousLength: (flags & 1) !== 0 ? c.uint.decode(state) : 0
400
+ }
401
+ }
402
+ }
403
+
404
+ // @core/sessions
405
+ const encoding14 = c.array({
406
+ preencode (state, m) {
407
+ c.string.preencode(state, m.name)
408
+ c.uint.preencode(state, m.dataPointer)
409
+ },
410
+ encode (state, m) {
411
+ c.string.encode(state, m.name)
412
+ c.uint.encode(state, m.dataPointer)
413
+ },
414
+ decode (state) {
415
+ const r0 = c.string.decode(state)
416
+ const r1 = c.uint.decode(state)
417
+
418
+ return {
419
+ name: r0,
420
+ dataPointer: r1
421
+ }
422
+ }
423
+ })
424
+
425
+ // @core/dependency
426
+ const encoding15 = {
427
+ preencode (state, m) {
428
+ c.uint.preencode(state, m.dataPointer)
429
+ c.uint.preencode(state, m.length)
430
+ },
431
+ encode (state, m) {
432
+ c.uint.encode(state, m.dataPointer)
433
+ c.uint.encode(state, m.length)
434
+ },
435
+ decode (state) {
436
+ const r0 = c.uint.decode(state)
437
+ const r1 = c.uint.decode(state)
438
+
439
+ return {
440
+ dataPointer: r0,
441
+ length: r1
442
+ }
443
+ }
444
+ }
445
+
446
+ function setVersion (v) {
447
+ version = v
448
+ }
449
+
450
+ function encode (name, value, v = VERSION) {
451
+ version = v
452
+ return c.encode(getEncoding(name), value)
453
+ }
454
+
455
+ function decode (name, buffer, v = VERSION) {
456
+ version = v
457
+ return c.decode(getEncoding(name), buffer)
458
+ }
459
+
460
+ function getEnum (name) {
461
+ switch (name) {
462
+ case '@core/hashes': return encoding4_enum
463
+ case '@core/signatures': return encoding5_enum
464
+ default: throw new Error('Enum not found ' + name)
465
+ }
466
+ }
467
+
468
+ function getEncoding (name) {
469
+ switch (name) {
470
+ case '@corestore/allocated': return encoding0
471
+ case '@corestore/head': return encoding1
472
+ case '@corestore/alias': return encoding2
473
+ case '@corestore/core': return encoding3
474
+ case '@core/hashes': return encoding4
475
+ case '@core/signatures': return encoding5
476
+ case '@core/tree-node': return encoding6
477
+ case '@core/signer': return encoding7
478
+ case '@core/prologue': return encoding8
479
+ case '@core/manifest': return encoding9
480
+ case '@core/keyPair': return encoding10
481
+ case '@core/auth': return encoding11
482
+ case '@core/head': return encoding12
483
+ case '@core/hints': return encoding13
484
+ case '@core/sessions': return encoding14
485
+ case '@core/dependency': return encoding15
486
+ default: throw new Error('Encoder not found ' + name)
487
+ }
488
+ }
489
+
490
+ function getStruct (name, v = VERSION) {
491
+ const enc = getEncoding(name)
492
+ return {
493
+ preencode (state, m) {
494
+ version = v
495
+ enc.preencode(state, m)
496
+ },
497
+ encode (state, m) {
498
+ version = v
499
+ enc.encode(state, m)
500
+ },
501
+ decode (state) {
502
+ version = v
503
+ return enc.decode(state)
504
+ }
505
+ }
506
+ }
507
+
508
+ const resolveStruct = getStruct // compat
509
+
510
+ module.exports = { resolveStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }
@@ -1,111 +0,0 @@
1
- const { Readable, isEnded, getStreamError } = require('streamx')
2
-
3
- module.exports = class DependencyStream extends Readable {
4
- constructor (storage, createStream, opts = {}) {
5
- super()
6
-
7
- this.storage = storage
8
- this.createStream = createStream
9
-
10
- let max = 0
11
-
12
- const reverse = !!opts.reverse
13
- const limit = opts.limit === 0 ? 0 : (opts.limit || Infinity)
14
- const gte = typeof opts.gte === 'number' ? opts.gte : typeof opts.gt === 'number' ? opts.gt + 1 : 0
15
- const lt = typeof opts.lt === 'number' ? opts.lt : typeof opts.lte === 'number' ? opts.lte + 1 : Infinity
16
-
17
- const streams = []
18
-
19
- for (let i = 0; i < storage.dependencies.length; i++) {
20
- const min = max
21
- max += storage.dependencies[i].length
22
-
23
- streams.push({
24
- data: storage.dependencies[i].data,
25
- gte: Math.max(gte, min),
26
- lt: Math.min(lt, max)
27
- })
28
- }
29
-
30
- streams.push({
31
- data: storage.dataPointer,
32
- gte: Math.max(gte, max),
33
- lt
34
- })
35
-
36
- this._streams = reverse ? streams.reverse() : streams
37
- this._reverse = reverse
38
- this._limit = limit
39
- this._next = 0
40
- this._active = null
41
- this._pendingDestroy = null
42
- this._ondataBound = this._ondata.bind(this)
43
- this._oncloseBound = this._onclose.bind(this)
44
-
45
- this._nextStream()
46
- }
47
-
48
- _read (cb) {
49
- this._active.resume()
50
- cb(null)
51
- }
52
-
53
- _predestroy () {
54
- if (this._active) this._active.destroy()
55
- }
56
-
57
- _destroy (cb) {
58
- if (this._active === null) cb(null)
59
- else this._pendingDestroy = cb
60
- }
61
-
62
- _ondata (data) {
63
- if (this._limit > 0) this._limit--
64
- if (this.push(data) === false) this._active.pause()
65
- }
66
-
67
- _onclose () {
68
- if (!isEnded(this._active)) {
69
- const error = getStreamError(this._active)
70
- this._active = null
71
- this.destroy(error)
72
- this._continueDestroy(error)
73
- return
74
- }
75
-
76
- if (this._next >= this._streams.length || this._limit === 0) {
77
- this._active = null
78
- this.push(null)
79
- this._continueDestroy(null)
80
- return
81
- }
82
-
83
- this._nextStream()
84
- }
85
-
86
- _continueDestroy (err) {
87
- if (this._pendingDestroy === null) return
88
- const cb = this._pendingDestroy
89
- this._pendingDestroy = null
90
- cb(err)
91
- }
92
-
93
- _nextStream () {
94
- const { data, gte, lt } = this._streams[this._next++]
95
-
96
- const stream = this.createStream(this.storage.dbRead, data, {
97
- reverse: this._reverse,
98
- limit: this._limit,
99
- gte,
100
- lt
101
- })
102
-
103
- this._active = stream
104
-
105
- stream.on('data', this._ondataBound)
106
- stream.on('error', noop) // handled in onclose
107
- stream.on('close', this._oncloseBound)
108
- }
109
- }
110
-
111
- function noop () {}