hypercore-storage 2.7.1 → 2.9.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/index.js CHANGED
@@ -21,6 +21,7 @@ const {
21
21
  createBitfieldStream,
22
22
  createUserDataStream,
23
23
  createTreeNodeStream,
24
+ createMarkStream,
24
25
  createLocalStream
25
26
  } = require('./lib/streams.js')
26
27
 
@@ -238,6 +239,10 @@ class HypercoreStorage {
238
239
  return createUserDataStream(this.core, this.db, this.view, opts)
239
240
  }
240
241
 
242
+ createMarkStream(opts) {
243
+ return createMarkStream(this.core, this.db, this.view, opts)
244
+ }
245
+
241
246
  createLocalStream(opts) {
242
247
  return createLocalStream(this.core, this.db, this.view, opts)
243
248
  }
package/lib/keys.js CHANGED
@@ -21,6 +21,7 @@ const DATA_TREE = 4
21
21
  const DATA_BITFIELD = 5
22
22
  const DATA_USER_DATA = 6
23
23
  const DATA_LOCAL = 7
24
+ const DATA_MARK = 8
24
25
 
25
26
  const slab = { buffer: b4a.allocUnsafe(65536), start: 0, end: 0 }
26
27
 
@@ -224,6 +225,16 @@ core.userDataEnd = function (ptr) {
224
225
  return state.buffer.subarray(start, state.start)
225
226
  }
226
227
 
228
+ core.mark = function (ptr, index) {
229
+ const state = alloc()
230
+ const start = state.start
231
+ UINT.encode(state, TL_DATA)
232
+ UINT.encode(state, ptr)
233
+ UINT.encode(state, DATA_MARK)
234
+ UINT.encode(state, index)
235
+ return state.buffer.subarray(start, state.start)
236
+ }
237
+
227
238
  core.local = function (ptr, key) {
228
239
  if (key.byteLength > 2048) {
229
240
  throw new Error('local keys has an upper limit of 2048 bytes atm')
@@ -273,6 +284,14 @@ core.userDataKey = function (buffer) {
273
284
  return STRING.decode(state)
274
285
  }
275
286
 
287
+ core.markKey = function (buffer) {
288
+ const state = { buffer, start: 0, end: buffer.byteLength }
289
+ UINT.decode(state) // ns
290
+ UINT.decode(state) // ptr
291
+ UINT.decode(state) // type
292
+ return UINT.decode(state)
293
+ }
294
+
276
295
  core.localKey = function (buffer) {
277
296
  const state = { buffer, start: 0, end: buffer.byteLength }
278
297
  UINT.decode(state) // ns
package/lib/streams.js CHANGED
@@ -15,6 +15,7 @@ module.exports = {
15
15
  createAliasStream,
16
16
  createDiscoveryKeyStream,
17
17
  createTreeNodeStream,
18
+ createMarkStream,
18
19
  createLocalStream
19
20
  }
20
21
 
@@ -117,6 +118,20 @@ function createUserDataStream(
117
118
  return ite
118
119
  }
119
120
 
121
+ function createMarkStream(
122
+ ptr,
123
+ db,
124
+ view,
125
+ { gt = -1, gte = gt + 1, lte = -1, lt = lte === -1 ? -1 : lte + 1, reverse = false } = {}
126
+ ) {
127
+ const s = core.mark(ptr.dataPointer, gte)
128
+ const e = core.mark(ptr.dataPointer, lt === -1 ? Infinity : lt)
129
+ const ite = view.iterator(db, s, e, reverse)
130
+
131
+ ite._readableState.map = mapMark
132
+ return ite
133
+ }
134
+
120
135
  function createLocalStream(
121
136
  ptr,
122
137
  db,
@@ -139,6 +154,11 @@ function mapBitfield(data) {
139
154
  return { index, page: data.value }
140
155
  }
141
156
 
157
+ function mapMark(data) {
158
+ const index = core.markKey(data.key)
159
+ return { index, page: data.value }
160
+ }
161
+
142
162
  function mapLocal(data) {
143
163
  const key = core.localKey(data.key)
144
164
  return { key, value: data.value }
package/lib/tx.js CHANGED
@@ -108,6 +108,22 @@ class CoreTX {
108
108
  this.changes.push([core.userData(this.core.dataPointer, key), null, null])
109
109
  }
110
110
 
111
+ putMark(index, data) {
112
+ this.changes.push([core.mark(this.core.dataPointer, index), data, null])
113
+ }
114
+
115
+ deleteMark(index) {
116
+ this.changes.push([core.mark(this.core.dataPointer, index), null, null])
117
+ }
118
+
119
+ deleteMarkRange(start, end) {
120
+ this.changes.push([
121
+ core.mark(this.core.dataPointer, start),
122
+ null,
123
+ core.mark(this.core.dataPointer, end === -1 ? Infinity : end)
124
+ ])
125
+ }
126
+
111
127
  putLocal(key, value) {
112
128
  this.changes.push([core.local(this.core.dataPointer, key), value, null])
113
129
  }
@@ -213,6 +229,10 @@ class CoreRX {
213
229
  return this.view.get(this.read, core.userData(this.core.dataPointer, key))
214
230
  }
215
231
 
232
+ getMark(index) {
233
+ return this.view.get(this.read, core.mark(this.core.dataPointer, index))
234
+ }
235
+
216
236
  getLocal(key) {
217
237
  return this.view.get(this.read, core.local(this.core.dataPointer, key))
218
238
  }
@@ -364,14 +364,14 @@ const dataUpgrade = {
364
364
  c.uint.preencode(state, u.length)
365
365
  nodeArray.preencode(state, u.nodes)
366
366
  nodeArray.preencode(state, u.additionalNodes)
367
- c.buffer.preencode(state, u.signature)
367
+ c.optionalBuffer.preencode(state, u.signature)
368
368
  },
369
369
  encode (state, u) {
370
370
  c.uint.encode(state, u.start)
371
371
  c.uint.encode(state, u.length)
372
372
  nodeArray.encode(state, u.nodes)
373
373
  nodeArray.encode(state, u.additionalNodes)
374
- c.buffer.encode(state, u.signature)
374
+ c.optionalBuffer.encode(state, u.signature)
375
375
  },
376
376
  decode (state) {
377
377
  return {
@@ -379,7 +379,7 @@ const dataUpgrade = {
379
379
  length: c.uint.decode(state),
380
380
  nodes: nodeArray.decode(state),
381
381
  additionalNodes: nodeArray.decode(state),
382
- signature: c.buffer.decode(state)
382
+ signature: c.optionalBuffer.decode(state)
383
383
  }
384
384
  }
385
385
  }
@@ -404,18 +404,18 @@ const dataSeek = {
404
404
  const dataBlock = {
405
405
  preencode (state, b) {
406
406
  c.uint.preencode(state, b.index)
407
- c.buffer.preencode(state, b.value)
407
+ c.optionalBuffer.preencode(state, b.value)
408
408
  nodeArray.preencode(state, b.nodes)
409
409
  },
410
410
  encode (state, b) {
411
411
  c.uint.encode(state, b.index)
412
- c.buffer.encode(state, b.value)
412
+ c.optionalBuffer.encode(state, b.value)
413
413
  nodeArray.encode(state, b.nodes)
414
414
  },
415
415
  decode (state) {
416
416
  return {
417
417
  index: c.uint.decode(state),
418
- value: c.buffer.decode(state) || EMPTY,
418
+ value: c.optionalBuffer.decode(state) || EMPTY,
419
419
  nodes: nodeArray.decode(state)
420
420
  }
421
421
  }
@@ -633,16 +633,16 @@ wire.extension = {
633
633
  const keyValue = {
634
634
  preencode (state, p) {
635
635
  c.string.preencode(state, p.key)
636
- c.buffer.preencode(state, p.value)
636
+ c.optionalBuffer.preencode(state, p.value)
637
637
  },
638
638
  encode (state, p) {
639
639
  c.string.encode(state, p.key)
640
- c.buffer.encode(state, p.value)
640
+ c.optionalBuffer.encode(state, p.value)
641
641
  },
642
642
  decode (state) {
643
643
  return {
644
644
  key: c.string.decode(state),
645
- value: c.buffer.decode(state)
645
+ value: c.optionalBuffer.decode(state)
646
646
  }
647
647
  }
648
648
  }
@@ -652,20 +652,20 @@ const treeUpgrade = {
652
652
  c.uint.preencode(state, u.fork)
653
653
  c.uint.preencode(state, u.ancestors)
654
654
  c.uint.preencode(state, u.length)
655
- c.buffer.preencode(state, u.signature)
655
+ c.optionalBuffer.preencode(state, u.signature)
656
656
  },
657
657
  encode (state, u) {
658
658
  c.uint.encode(state, u.fork)
659
659
  c.uint.encode(state, u.ancestors)
660
660
  c.uint.encode(state, u.length)
661
- c.buffer.encode(state, u.signature)
661
+ c.optionalBuffer.encode(state, u.signature)
662
662
  },
663
663
  decode (state) {
664
664
  return {
665
665
  fork: c.uint.decode(state),
666
666
  ancestors: c.uint.decode(state),
667
667
  length: c.uint.decode(state),
668
- signature: c.buffer.decode(state)
668
+ signature: c.optionalBuffer.decode(state)
669
669
  }
670
670
  }
671
671
  }
@@ -737,17 +737,17 @@ oplog.entry = {
737
737
 
738
738
  const keyPair = {
739
739
  preencode (state, kp) {
740
- c.buffer.preencode(state, kp.publicKey)
741
- c.buffer.preencode(state, kp.secretKey)
740
+ c.optionalBuffer.preencode(state, kp.publicKey)
741
+ c.optionalBuffer.preencode(state, kp.secretKey)
742
742
  },
743
743
  encode (state, kp) {
744
- c.buffer.encode(state, kp.publicKey)
745
- c.buffer.encode(state, kp.secretKey)
744
+ c.optionalBuffer.encode(state, kp.publicKey)
745
+ c.optionalBuffer.encode(state, kp.secretKey)
746
746
  },
747
747
  decode (state) {
748
748
  return {
749
- publicKey: c.buffer.decode(state),
750
- secretKey: c.buffer.decode(state)
749
+ publicKey: c.optionalBuffer.decode(state),
750
+ secretKey: c.optionalBuffer.decode(state)
751
751
  }
752
752
  }
753
753
  }
@@ -795,21 +795,21 @@ const treeHeader = {
795
795
  preencode (state, t) {
796
796
  c.uint.preencode(state, t.fork)
797
797
  c.uint.preencode(state, t.length)
798
- c.buffer.preencode(state, t.rootHash)
799
- c.buffer.preencode(state, t.signature)
798
+ c.optionalBuffer.preencode(state, t.rootHash)
799
+ c.optionalBuffer.preencode(state, t.signature)
800
800
  },
801
801
  encode (state, t) {
802
802
  c.uint.encode(state, t.fork)
803
803
  c.uint.encode(state, t.length)
804
- c.buffer.encode(state, t.rootHash)
805
- c.buffer.encode(state, t.signature)
804
+ c.optionalBuffer.encode(state, t.rootHash)
805
+ c.optionalBuffer.encode(state, t.signature)
806
806
  },
807
807
  decode (state) {
808
808
  return {
809
809
  fork: c.uint.decode(state),
810
810
  length: c.uint.decode(state),
811
- rootHash: c.buffer.decode(state),
812
- signature: c.buffer.decode(state)
811
+ rootHash: c.optionalBuffer.decode(state),
812
+ signature: c.optionalBuffer.decode(state)
813
813
  }
814
814
  }
815
815
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "2.7.1",
3
+ "version": "2.9.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
@@ -41,7 +41,7 @@
41
41
  "b4a": "^1.6.7",
42
42
  "bare-fs": "^4.0.1",
43
43
  "bare-path": "^3.0.0",
44
- "compact-encoding": "^2.16.0",
44
+ "compact-encoding": "^3.0.0",
45
45
  "device-file": "^2.1.2",
46
46
  "flat-tree": "^1.12.1",
47
47
  "hypercore-crypto": "^3.4.2",
@@ -299,15 +299,15 @@ const encoding9 = {
299
299
  const encoding10 = {
300
300
  preencode(state, m) {
301
301
  c.buffer.preencode(state, m.publicKey)
302
- c.buffer.preencode(state, m.secretKey)
302
+ c.optionalBuffer.preencode(state, m.secretKey)
303
303
  },
304
304
  encode(state, m) {
305
305
  c.buffer.encode(state, m.publicKey)
306
- c.buffer.encode(state, m.secretKey)
306
+ c.optionalBuffer.encode(state, m.secretKey)
307
307
  },
308
308
  decode(state) {
309
309
  const r0 = c.buffer.decode(state)
310
- const r1 = c.buffer.decode(state)
310
+ const r1 = c.optionalBuffer.decode(state)
311
311
 
312
312
  return {
313
313
  publicKey: r0,
@@ -362,19 +362,19 @@ const encoding12 = {
362
362
  c.uint.preencode(state, m.fork)
363
363
  c.uint.preencode(state, m.length)
364
364
  c.fixed32.preencode(state, m.rootHash)
365
- c.buffer.preencode(state, m.signature)
365
+ c.optionalBuffer.preencode(state, m.signature)
366
366
  },
367
367
  encode(state, m) {
368
368
  c.uint.encode(state, m.fork)
369
369
  c.uint.encode(state, m.length)
370
370
  c.fixed32.encode(state, m.rootHash)
371
- c.buffer.encode(state, m.signature)
371
+ c.optionalBuffer.encode(state, m.signature)
372
372
  },
373
373
  decode(state) {
374
374
  const r0 = c.uint.decode(state)
375
375
  const r1 = c.uint.decode(state)
376
376
  const r2 = c.fixed32.decode(state)
377
- const r3 = c.buffer.decode(state)
377
+ const r3 = c.optionalBuffer.decode(state)
378
378
 
379
379
  return {
380
380
  fork: r0,