hypercore 10.0.0-alpha.7 → 10.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/README.md +83 -22
- package/index.js +587 -217
- package/lib/bitfield.js +109 -41
- package/lib/block-encryption.js +3 -2
- package/lib/block-store.js +6 -4
- package/lib/caps.js +32 -0
- package/lib/core.js +166 -35
- package/lib/errors.js +50 -0
- package/lib/info.js +23 -0
- package/lib/merkle-tree.js +181 -105
- package/lib/messages.js +249 -168
- package/lib/oplog.js +4 -3
- package/lib/remote-bitfield.js +28 -7
- package/lib/replicator.js +1415 -624
- package/lib/streams.js +56 -0
- package/package.json +20 -15
- 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 -90
- package/test/bitfield.js +0 -71
- package/test/core.js +0 -290
- package/test/encodings.js +0 -18
- package/test/encryption.js +0 -85
- 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 -372
- package/test/sessions.js +0 -173
- package/test/user-data.js +0 -47
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
144
|
-
nodes: c.uint.decode(state)
|
|
221
|
+
nodes: nodeArray.decode(state)
|
|
145
222
|
}
|
|
146
223
|
}
|
|
147
224
|
}
|
|
148
225
|
|
|
149
|
-
|
|
150
|
-
preencode (state,
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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,
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
175
|
-
|
|
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
|
-
|
|
181
|
-
preencode (state,
|
|
182
|
-
c.uint.preencode(state,
|
|
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,
|
|
189
|
-
c.uint.encode(state,
|
|
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
|
-
|
|
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
|
-
|
|
222
|
-
preencode (state,
|
|
223
|
-
c.uint.preencode(state,
|
|
224
|
-
|
|
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,
|
|
227
|
-
c.uint.encode(state,
|
|
228
|
-
|
|
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:
|
|
289
|
+
length: c.uint.decode(state)
|
|
234
290
|
}
|
|
235
291
|
}
|
|
236
292
|
}
|
|
237
293
|
|
|
238
|
-
|
|
239
|
-
preencode (state,
|
|
240
|
-
c.uint.preencode(state,
|
|
241
|
-
c.
|
|
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,
|
|
244
|
-
c.uint.encode(state,
|
|
245
|
-
c.
|
|
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
|
-
|
|
306
|
+
length: c.uint.decode(state)
|
|
251
307
|
}
|
|
252
308
|
}
|
|
253
309
|
}
|
|
254
310
|
|
|
255
|
-
|
|
256
|
-
preencode (state,
|
|
257
|
-
|
|
258
|
-
c.uint.preencode(state,
|
|
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,
|
|
261
|
-
c.uint.encode(state,
|
|
262
|
-
c.uint.encode(state,
|
|
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
|
-
|
|
267
|
-
|
|
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
|
-
|
|
273
|
-
preencode (state,
|
|
274
|
-
c.uint.preencode(state,
|
|
275
|
-
c.
|
|
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,
|
|
278
|
-
c.uint.encode(state,
|
|
279
|
-
c.
|
|
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
|
-
|
|
284
|
-
|
|
344
|
+
start: c.uint.decode(state),
|
|
345
|
+
bitfield: c.uint32array.decode(state)
|
|
285
346
|
}
|
|
286
347
|
}
|
|
287
348
|
}
|
|
288
349
|
|
|
289
|
-
|
|
290
|
-
preencode (state,
|
|
291
|
-
|
|
292
|
-
c.
|
|
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,
|
|
295
|
-
c.uint.encode(state,
|
|
296
|
-
c.
|
|
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
|
-
|
|
301
|
-
|
|
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
|
-
|
|
377
|
+
wire.reorgHint = {
|
|
307
378
|
preencode (state, m) {
|
|
308
|
-
c.uint.preencode(state, m.
|
|
309
|
-
c.
|
|
310
|
-
c.
|
|
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.
|
|
314
|
-
c.
|
|
315
|
-
c.
|
|
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
|
-
|
|
320
|
-
|
|
321
|
-
|
|
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
|
-
|
|
397
|
+
wire.extension = {
|
|
327
398
|
preencode (state, m) {
|
|
328
|
-
c.
|
|
399
|
+
c.string.preencode(state, m.name)
|
|
400
|
+
c.raw.preencode(state, m.message)
|
|
329
401
|
},
|
|
330
402
|
encode (state, m) {
|
|
331
|
-
c.
|
|
403
|
+
c.string.encode(state, m.name)
|
|
404
|
+
c.raw.encode(state, m.message)
|
|
332
405
|
},
|
|
333
406
|
decode (state) {
|
|
334
|
-
return {
|
|
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.
|
|
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
|
-
|
|
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(
|
|
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 =
|
|
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 =
|
|
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
|
package/lib/remote-bitfield.js
CHANGED
|
@@ -1,24 +1,45 @@
|
|
|
1
1
|
const BigSparseArray = require('big-sparse-array')
|
|
2
|
+
const bits = require('bits-to-bytes')
|
|
2
3
|
|
|
3
4
|
module.exports = class RemoteBitfield {
|
|
4
5
|
constructor () {
|
|
6
|
+
this.pageSize = 32768
|
|
5
7
|
this.pages = new BigSparseArray()
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
get (index) {
|
|
9
|
-
const
|
|
10
|
-
const i = (index -
|
|
11
|
+
const j = index & (this.pageSize - 1)
|
|
12
|
+
const i = (index - j) / this.pageSize
|
|
13
|
+
|
|
11
14
|
const p = this.pages.get(i)
|
|
12
15
|
|
|
13
|
-
return p ? (p
|
|
16
|
+
return p ? bits.get(p, j) : false
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
set (index, val) {
|
|
17
|
-
const
|
|
18
|
-
const i = (index -
|
|
20
|
+
const j = index & (this.pageSize - 1)
|
|
21
|
+
const i = (index - j) / this.pageSize
|
|
22
|
+
|
|
19
23
|
const p = this.pages.get(i) || this.pages.set(i, new Uint32Array(1024))
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
bits.set(p, j, val)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setRange (start, length, val) {
|
|
29
|
+
let j = start & (this.pageSize - 1)
|
|
30
|
+
let i = (start - j) / this.pageSize
|
|
31
|
+
|
|
32
|
+
while (length > 0) {
|
|
33
|
+
const p = this.pages.get(i) || this.pages.set(i, new Uint32Array(1024))
|
|
34
|
+
|
|
35
|
+
const end = Math.min(j + length, this.pageSize)
|
|
36
|
+
const range = end - j
|
|
37
|
+
|
|
38
|
+
bits.fill(p, val, j, end)
|
|
39
|
+
|
|
40
|
+
j = 0
|
|
41
|
+
i++
|
|
42
|
+
length -= range
|
|
43
|
+
}
|
|
23
44
|
}
|
|
24
45
|
}
|