hypercore 10.0.0-alpha.5 → 10.0.0-alpha.52

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/lib/messages.js CHANGED
@@ -1,6 +1,9 @@
1
1
  const c = require('compact-encoding')
2
+ const b4a = require('b4a')
2
3
 
3
- const node = exports.node = {
4
+ const EMPTY = b4a.alloc(0)
5
+
6
+ const node = {
4
7
  preencode (state, n) {
5
8
  c.uint.preencode(state, n.index)
6
9
  c.uint.preencode(state, n.size)
@@ -22,6 +25,124 @@ const node = exports.node = {
22
25
 
23
26
  const nodeArray = c.array(node)
24
27
 
28
+ const wire = exports.wire = {}
29
+
30
+ wire.handshake = {
31
+ preencode (state, m) {
32
+ c.uint.preencode(state, 0) // flags for the future
33
+ c.fixed32.preencode(state, m.capability)
34
+ },
35
+ encode (state, m) {
36
+ c.uint.encode(state, 0) // flags for the future
37
+ c.fixed32.encode(state, m.capability)
38
+ },
39
+ decode (state) {
40
+ c.uint.decode(state) // flags for the future
41
+ return {
42
+ capability: c.fixed32.decode(state)
43
+ }
44
+ }
45
+ }
46
+
47
+ const requestBlock = {
48
+ preencode (state, b) {
49
+ c.uint.preencode(state, b.index)
50
+ c.uint.preencode(state, b.nodes)
51
+ },
52
+ encode (state, b) {
53
+ c.uint.encode(state, b.index)
54
+ c.uint.encode(state, b.nodes)
55
+ },
56
+ decode (state) {
57
+ return {
58
+ index: c.uint.decode(state),
59
+ nodes: c.uint.decode(state)
60
+ }
61
+ }
62
+ }
63
+
64
+ const requestSeek = {
65
+ preencode (state, s) {
66
+ c.uint.preencode(state, s.bytes)
67
+ },
68
+ encode (state, s) {
69
+ c.uint.encode(state, s.bytes)
70
+ },
71
+ decode (state) {
72
+ return {
73
+ bytes: c.uint.decode(state)
74
+ }
75
+ }
76
+ }
77
+
78
+ const requestUpgrade = {
79
+ preencode (state, u) {
80
+ c.uint.preencode(state, u.start)
81
+ c.uint.preencode(state, u.length)
82
+ },
83
+ encode (state, u) {
84
+ c.uint.encode(state, u.start)
85
+ c.uint.encode(state, u.length)
86
+ },
87
+ decode (state) {
88
+ return {
89
+ start: c.uint.decode(state),
90
+ length: c.uint.decode(state)
91
+ }
92
+ }
93
+ }
94
+
95
+ wire.request = {
96
+ preencode (state, m) {
97
+ state.end++ // flags
98
+ c.uint.preencode(state, m.id)
99
+ c.uint.preencode(state, m.fork)
100
+
101
+ if (m.block) requestBlock.preencode(state, m.block)
102
+ if (m.hash) requestBlock.preencode(state, m.hash)
103
+ if (m.seek) requestSeek.preencode(state, m.seek)
104
+ if (m.upgrade) requestUpgrade.preencode(state, m.upgrade)
105
+ },
106
+ encode (state, m) {
107
+ const flags = (m.block ? 1 : 0) | (m.hash ? 2 : 0) | (m.seek ? 4 : 0) | (m.upgrade ? 8 : 0)
108
+
109
+ c.uint.encode(state, flags)
110
+ c.uint.encode(state, m.id)
111
+ c.uint.encode(state, m.fork)
112
+
113
+ if (m.block) requestBlock.encode(state, m.block)
114
+ if (m.hash) requestBlock.encode(state, m.hash)
115
+ if (m.seek) requestSeek.encode(state, m.seek)
116
+ if (m.upgrade) requestUpgrade.encode(state, m.upgrade)
117
+ },
118
+ decode (state) {
119
+ const flags = c.uint.decode(state)
120
+
121
+ return {
122
+ id: c.uint.decode(state),
123
+ fork: c.uint.decode(state),
124
+ block: flags & 1 ? requestBlock.decode(state) : null,
125
+ hash: flags & 2 ? requestBlock.decode(state) : null,
126
+ seek: flags & 4 ? requestSeek.decode(state) : null,
127
+ upgrade: flags & 8 ? requestUpgrade.decode(state) : null
128
+ }
129
+ }
130
+ }
131
+
132
+ wire.cancel = {
133
+ preencode (state, m) {
134
+ c.uint.preencode(state, m.request)
135
+ },
136
+ encode (state, m) {
137
+ c.uint.encode(state, m.request)
138
+ },
139
+ decode (state, m) {
140
+ return {
141
+ request: c.uint.decode(state)
142
+ }
143
+ }
144
+ }
145
+
25
146
  const dataUpgrade = {
26
147
  preencode (state, u) {
27
148
  c.uint.preencode(state, u.start)
@@ -79,259 +200,214 @@ const dataBlock = {
79
200
  decode (state) {
80
201
  return {
81
202
  index: c.uint.decode(state),
82
- value: c.buffer.decode(state),
203
+ value: c.buffer.decode(state) || EMPTY,
83
204
  nodes: nodeArray.decode(state)
84
205
  }
85
206
  }
86
207
  }
87
208
 
88
- exports.data = {
89
- preencode (state, d) {
90
- c.uint.preencode(state, d.fork)
91
- state.end++ // flags
92
- if (d.block) dataBlock.preencode(state, d.block)
93
- if (d.seek) dataSeek.preencode(state, d.seek)
94
- if (d.upgrade) dataUpgrade.preencode(state, d.upgrade)
95
- },
96
- encode (state, d) {
97
- c.uint.encode(state, d.fork)
98
-
99
- const s = state.start++
100
- let flags = 0
101
-
102
- if (d.block) {
103
- flags |= 1
104
- dataBlock.encode(state, d.block)
105
- }
106
- if (d.seek) {
107
- flags |= 2
108
- dataSeek.encode(state, d.seek)
109
- }
110
- if (d.upgrade) {
111
- flags |= 4
112
- dataUpgrade.encode(state, d.upgrade)
113
- }
114
-
115
- state.buffer[s] = flags
116
- },
117
- decode (state) {
118
- const fork = c.uint.decode(state)
119
- const flags = c.uint.decode(state)
120
- return {
121
- fork,
122
- block: (flags & 1) === 0 ? null : dataBlock.decode(state),
123
- seek: (flags & 2) === 0 ? null : dataSeek.decode(state),
124
- upgrade: (flags & 4) === 0 ? null : dataUpgrade.decode(state)
125
- }
126
- }
127
- }
128
-
129
- const requestBlock = {
209
+ const dataHash = {
130
210
  preencode (state, b) {
131
211
  c.uint.preencode(state, b.index)
132
- c.bool.preencode(state, b.value)
133
- c.uint.preencode(state, b.nodes)
212
+ nodeArray.preencode(state, b.nodes)
134
213
  },
135
214
  encode (state, b) {
136
215
  c.uint.encode(state, b.index)
137
- c.bool.encode(state, b.value)
138
- c.uint.encode(state, b.nodes)
216
+ nodeArray.encode(state, b.nodes)
139
217
  },
140
218
  decode (state) {
141
219
  return {
142
220
  index: c.uint.decode(state),
143
- value: c.bool.decode(state),
144
- nodes: c.uint.decode(state)
221
+ nodes: nodeArray.decode(state)
145
222
  }
146
223
  }
147
224
  }
148
225
 
149
- const requestSeek = {
150
- preencode (state, s) {
151
- c.uint.preencode(state, s.bytes)
152
- },
153
- encode (state, s) {
154
- c.uint.encode(state, s.bytes)
155
- },
156
- decode (state) {
157
- return {
158
- bytes: c.uint.decode(state)
159
- }
160
- }
161
- }
226
+ wire.data = {
227
+ preencode (state, m) {
228
+ state.end++ // flags
229
+ c.uint.preencode(state, m.request)
230
+ c.uint.preencode(state, m.fork)
162
231
 
163
- const requestUpgrade = {
164
- preencode (state, u) {
165
- c.uint.preencode(state, u.start)
166
- c.uint.preencode(state, u.length)
232
+ if (m.block) dataBlock.preencode(state, m.block)
233
+ if (m.hash) dataHash.preencode(state, m.hash)
234
+ if (m.seek) dataSeek.preencode(state, m.seek)
235
+ if (m.upgrade) dataUpgrade.preencode(state, m.upgrade)
167
236
  },
168
- encode (state, u) {
169
- c.uint.encode(state, u.start)
170
- c.uint.encode(state, u.length)
237
+ encode (state, m) {
238
+ const flags = (m.block ? 1 : 0) | (m.hash ? 2 : 0) | (m.seek ? 4 : 0) | (m.upgrade ? 8 : 0)
239
+
240
+ c.uint.encode(state, flags)
241
+ c.uint.encode(state, m.request)
242
+ c.uint.encode(state, m.fork)
243
+
244
+ if (m.block) dataBlock.encode(state, m.block)
245
+ if (m.hash) dataHash.encode(state, m.hash)
246
+ if (m.seek) dataSeek.encode(state, m.seek)
247
+ if (m.upgrade) dataUpgrade.encode(state, m.upgrade)
171
248
  },
172
249
  decode (state) {
250
+ const flags = c.uint.decode(state)
251
+
173
252
  return {
174
- start: c.uint.decode(state),
175
- length: c.uint.decode(state)
253
+ request: c.uint.decode(state),
254
+ fork: c.uint.decode(state),
255
+ block: flags & 1 ? dataBlock.decode(state) : null,
256
+ hash: flags & 2 ? dataHash.decode(state) : null,
257
+ seek: flags & 4 ? dataSeek.decode(state) : null,
258
+ upgrade: flags & 8 ? dataUpgrade.decode(state) : null
176
259
  }
177
260
  }
178
261
  }
179
262
 
180
- exports.request = {
181
- preencode (state, r) {
182
- c.uint.preencode(state, r.fork)
183
- state.end++ // flags
184
- if (r.block) requestBlock.preencode(state, r.block)
185
- if (r.seek) requestSeek.preencode(state, r.seek)
186
- if (r.upgrade) requestUpgrade.preencode(state, r.upgrade)
263
+ wire.noData = {
264
+ preencode (state, m) {
265
+ c.uint.preencode(state, m.request)
187
266
  },
188
- encode (state, r) {
189
- c.uint.encode(state, r.fork)
190
-
191
- const s = state.start++
192
- let flags = 0
193
-
194
- if (r.block) {
195
- flags |= 1
196
- requestBlock.encode(state, r.block)
197
- }
198
- if (r.seek) {
199
- flags |= 2
200
- requestSeek.encode(state, r.seek)
201
- }
202
- if (r.upgrade) {
203
- flags |= 4
204
- requestUpgrade.encode(state, r.upgrade)
205
- }
206
-
207
- state.buffer[s] = flags
267
+ encode (state, m) {
268
+ c.uint.encode(state, m.request)
208
269
  },
209
- decode (state) {
210
- const fork = c.uint.decode(state)
211
- const flags = c.uint.decode(state)
270
+ decode (state, m) {
212
271
  return {
213
- fork,
214
- block: (flags & 1) === 0 ? null : requestBlock.decode(state),
215
- seek: (flags & 2) === 0 ? null : requestSeek.decode(state),
216
- upgrade: (flags & 4) === 0 ? null : requestUpgrade.decode(state)
272
+ request: c.uint.decode(state)
217
273
  }
218
274
  }
219
275
  }
220
276
 
221
- exports.have = {
222
- preencode (state, h) {
223
- c.uint.preencode(state, h.start)
224
- if (h.length > 1) c.uint.preencode(state, h.length)
277
+ wire.want = {
278
+ preencode (state, m) {
279
+ c.uint.preencode(state, m.start)
280
+ c.uint.preencode(state, m.length)
225
281
  },
226
- encode (state, h) {
227
- c.uint.encode(state, h.start)
228
- if (h.length > 1) c.uint.encode(state, h.length)
282
+ encode (state, m) {
283
+ c.uint.encode(state, m.start)
284
+ c.uint.encode(state, m.length)
229
285
  },
230
286
  decode (state) {
231
287
  return {
232
288
  start: c.uint.decode(state),
233
- length: state.start < state.end ? c.uint.decode(state) : 1
289
+ length: c.uint.decode(state)
234
290
  }
235
291
  }
236
292
  }
237
293
 
238
- exports.bitfield = {
239
- preencode (state, b) {
240
- c.uint.preencode(state, b.start)
241
- c.uint32array.preencode(state, b.bitfield)
294
+ wire.unwant = {
295
+ preencode (state, m) {
296
+ c.uint.preencode(state, m.start)
297
+ c.uint.preencode(state, m.length)
242
298
  },
243
- encode (state, b) {
244
- c.uint.encode(state, b.start)
245
- c.uint32array.encode(state, b.bitfield)
299
+ encode (state, m) {
300
+ c.uint.encode(state, m.start)
301
+ c.uint.encode(state, m.length)
246
302
  },
247
- decode (state) {
303
+ decode (state, m) {
248
304
  return {
249
305
  start: c.uint.decode(state),
250
- bitfield: c.uint32array.decode(state)
306
+ length: c.uint.decode(state)
251
307
  }
252
308
  }
253
309
  }
254
310
 
255
- exports.info = {
256
- preencode (state, i) {
257
- c.uint.preencode(state, i.length)
258
- c.uint.preencode(state, i.fork)
311
+ wire.range = {
312
+ preencode (state, m) {
313
+ state.end++ // flags
314
+ c.uint.preencode(state, m.start)
315
+ if (m.length !== 1) c.uint.preencode(state, m.length)
259
316
  },
260
- encode (state, i) {
261
- c.uint.encode(state, i.length)
262
- c.uint.encode(state, i.fork)
317
+ encode (state, m) {
318
+ c.uint.encode(state, (m.drop ? 1 : 0) | (m.length === 1 ? 2 : 0))
319
+ c.uint.encode(state, m.start)
320
+ if (m.length !== 1) c.uint.encode(state, m.length)
263
321
  },
264
322
  decode (state) {
323
+ const flags = c.uint.decode(state)
324
+
265
325
  return {
266
- length: c.uint.decode(state),
267
- fork: c.uint.decode(state)
326
+ drop: (flags & 1) !== 0,
327
+ start: c.uint.decode(state),
328
+ length: (flags & 2) !== 0 ? 1 : c.uint.decode(state)
268
329
  }
269
330
  }
270
331
  }
271
332
 
272
- exports.handshake = {
273
- preencode (state, h) {
274
- c.uint.preencode(state, h.protocolVersion)
275
- c.string.preencode(state, h.userAgent)
333
+ wire.bitfield = {
334
+ preencode (state, m) {
335
+ c.uint.preencode(state, m.start)
336
+ c.uint32array.preencode(state, m.bitfield)
276
337
  },
277
- encode (state, h) {
278
- c.uint.encode(state, h.protocolVersion)
279
- c.string.encode(state, h.userAgent)
338
+ encode (state, m) {
339
+ c.uint.encode(state, m.start)
340
+ c.uint32array.encode(state, m.bitfield)
280
341
  },
281
- decode (state) {
342
+ decode (state, m) {
282
343
  return {
283
- protocolVersion: c.uint.decode(state),
284
- userAgent: c.string.decode(state)
344
+ start: c.uint.decode(state),
345
+ bitfield: c.uint32array.decode(state)
285
346
  }
286
347
  }
287
348
  }
288
349
 
289
- exports.extension = {
290
- preencode (state, a) {
291
- c.uint.preencode(state, a.alias)
292
- c.string.preencode(state, a.name)
350
+ wire.sync = {
351
+ preencode (state, m) {
352
+ state.end++ // flags
353
+ c.uint.preencode(state, m.fork)
354
+ c.uint.preencode(state, m.length)
355
+ c.uint.preencode(state, m.remoteLength)
293
356
  },
294
- encode (state, a) {
295
- c.uint.encode(state, a.alias)
296
- c.string.encode(state, a.name)
357
+ encode (state, m) {
358
+ c.uint.encode(state, (m.canUpgrade ? 1 : 0) | (m.uploading ? 2 : 0) | (m.downloading ? 4 : 0))
359
+ c.uint.encode(state, m.fork)
360
+ c.uint.encode(state, m.length)
361
+ c.uint.encode(state, m.remoteLength)
297
362
  },
298
363
  decode (state) {
364
+ const flags = c.uint.decode(state)
365
+
299
366
  return {
300
- alias: c.uint.decode(state),
301
- name: c.string.decode(state)
367
+ fork: c.uint.decode(state),
368
+ length: c.uint.decode(state),
369
+ remoteLength: c.uint.decode(state),
370
+ canUpgrade: (flags & 1) !== 0,
371
+ uploading: (flags & 2) !== 0,
372
+ downloading: (flags & 4) !== 0
302
373
  }
303
374
  }
304
375
  }
305
376
 
306
- exports.core = {
377
+ wire.reorgHint = {
307
378
  preencode (state, m) {
308
- c.uint.preencode(state, m.alias)
309
- c.fixed32.preencode(state, m.discoveryKey)
310
- c.fixed32.preencode(state, m.capability)
379
+ c.uint.preencode(state, m.from)
380
+ c.uint.preencode(state, m.to)
381
+ c.uint.preencode(state, m.ancestors)
311
382
  },
312
383
  encode (state, m) {
313
- c.uint.encode(state, m.alias)
314
- c.fixed32.encode(state, m.discoveryKey)
315
- c.fixed32.encode(state, m.capability)
384
+ c.uint.encode(state, m.from)
385
+ c.uint.encode(state, m.to)
386
+ c.uint.encode(state, m.ancestors)
316
387
  },
317
388
  decode (state) {
318
389
  return {
319
- alias: c.uint.decode(state),
320
- discoveryKey: c.fixed32.decode(state),
321
- capability: c.fixed32.decode(state)
390
+ from: c.uint.encode(state),
391
+ to: c.uint.encode(state),
392
+ ancestors: c.uint.encode(state)
322
393
  }
323
394
  }
324
395
  }
325
396
 
326
- exports.unknownCore = {
397
+ wire.extension = {
327
398
  preencode (state, m) {
328
- c.fixed32.preencode(state, m.discoveryKey)
399
+ c.string.preencode(state, m.name)
400
+ c.raw.preencode(state, m.message)
329
401
  },
330
402
  encode (state, m) {
331
- c.fixed32.encode(state, m.discoveryKey)
403
+ c.string.encode(state, m.name)
404
+ c.raw.encode(state, m.message)
332
405
  },
333
406
  decode (state) {
334
- return { discoveryKey: c.fixed32.decode(state) }
407
+ return {
408
+ name: c.string.decode(state),
409
+ message: c.raw.decode(state)
410
+ }
335
411
  }
336
412
  }
337
413
 
@@ -396,7 +472,9 @@ const bitfieldUpdate = { // TODO: can maybe be folded into a HAVE later on with
396
472
  }
397
473
  }
398
474
 
399
- exports.oplogEntry = {
475
+ const oplog = exports.oplog = {}
476
+
477
+ oplog.entry = {
400
478
  preencode (state, m) {
401
479
  state.end++ // flags
402
480
  if (m.userData) keyValue.preencode(state, m.userData)
@@ -536,7 +614,7 @@ const types = {
536
614
 
537
615
  const keyValueArray = c.array(keyValue)
538
616
 
539
- exports.oplogHeader = {
617
+ oplog.header = {
540
618
  preencode (state, h) {
541
619
  state.end += 1 // version
542
620
  types.preencode(state, h.types)
@@ -544,6 +622,7 @@ exports.oplogHeader = {
544
622
  treeHeader.preencode(state, h.tree)
545
623
  keyPair.preencode(state, h.signer)
546
624
  hints.preencode(state, h.hints)
625
+ c.uint.preencode(state, h.contiguousLength)
547
626
  },
548
627
  encode (state, h) {
549
628
  state.buffer[state.start++] = 0 // version
@@ -552,6 +631,7 @@ exports.oplogHeader = {
552
631
  treeHeader.encode(state, h.tree)
553
632
  keyPair.encode(state, h.signer)
554
633
  hints.encode(state, h.hints)
634
+ c.uint.encode(state, h.contiguousLength)
555
635
  },
556
636
  decode (state) {
557
637
  const version = c.uint.decode(state)
@@ -565,7 +645,8 @@ exports.oplogHeader = {
565
645
  userData: keyValueArray.decode(state),
566
646
  tree: treeHeader.decode(state),
567
647
  signer: keyPair.decode(state),
568
- hints: hints.decode(state)
648
+ hints: hints.decode(state),
649
+ contiguousLength: state.end > state.start ? c.uint.decode(state) : 0
569
650
  }
570
651
  }
571
652
  }
package/lib/oplog.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const cenc = require('compact-encoding')
2
+ const b4a = require('b4a')
2
3
  const crc32 = require('crc32-universal')
3
4
 
4
5
  module.exports = class Oplog {
@@ -133,7 +134,7 @@ module.exports = class Oplog {
133
134
  return new Promise((resolve, reject) => {
134
135
  this.storage.open(err => {
135
136
  if (err && err.code !== 'ENOENT') return reject(err)
136
- if (err) return resolve(Buffer.alloc(0))
137
+ if (err) return resolve(b4a.alloc(0))
137
138
  this.storage.stat((err, stat) => {
138
139
  if (err && err.code !== 'ENOENT') return reject(err)
139
140
  this.storage.read(0, stat.size, (err, buf) => {
@@ -151,7 +152,7 @@ module.exports = class Oplog {
151
152
  const bit = (this._headers[i] + 1) & 1
152
153
 
153
154
  this.headerEncoding.preencode(state, header)
154
- state.buffer = Buffer.allocUnsafe(state.end)
155
+ state.buffer = b4a.allocUnsafe(state.end)
155
156
  this.headerEncoding.encode(state, header)
156
157
  this._addHeader(state, state.end - 8, bit, 0)
157
158
 
@@ -187,7 +188,7 @@ module.exports = class Oplog {
187
188
  this.entryEncoding.preencode(state, batch[i])
188
189
  }
189
190
 
190
- state.buffer = Buffer.allocUnsafe(state.end)
191
+ state.buffer = b4a.allocUnsafe(state.end)
191
192
 
192
193
  for (let i = 0; i < batch.length; i++) {
193
194
  const start = state.start += 8 // space for header