autobee 1.0.0 → 1.0.3

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.
@@ -0,0 +1,317 @@
1
+ const c = require('compact-encoding')
2
+
3
+ const Checkout = {
4
+ preencode(state, m) {
5
+ c.fixed32.preencode(state, m.key)
6
+ c.uint.preencode(state, m.length)
7
+ },
8
+ encode(state, m) {
9
+ c.fixed32.encode(state, m.key)
10
+ c.uint.encode(state, m.length)
11
+ },
12
+ decode(state) {
13
+ return {
14
+ key: c.fixed32.decode(state),
15
+ length: c.uint.decode(state)
16
+ }
17
+ }
18
+ }
19
+
20
+ const Clock = c.array(Checkout)
21
+
22
+ const IndexCheckpoint = {
23
+ preencode(state, m) {
24
+ c.fixed64.preencode(state, m.signature)
25
+ c.uint.preencode(state, m.length)
26
+ },
27
+ encode(state, m) {
28
+ c.fixed64.encode(state, m.signature)
29
+ c.uint.encode(state, m.length)
30
+ },
31
+ decode(state) {
32
+ return {
33
+ signature: c.fixed64.decode(state),
34
+ length: c.uint.decode(state)
35
+ }
36
+ }
37
+ }
38
+
39
+ const BootRecordV0 = {
40
+ preencode() {
41
+ throw new Error('version 0 records cannot be encoded')
42
+ },
43
+ encode() {
44
+ throw new Error('version 0 records cannot be encoded')
45
+ },
46
+ decode(state) {
47
+ const indexed = Checkout.decode(state)
48
+ const heads = Clock.decode(state)
49
+
50
+ // one cause initial recover is not ff recovery
51
+ return {
52
+ version: 0,
53
+ key: indexed.key,
54
+ systemLength: indexed.length,
55
+ indexersUpdated: false,
56
+ fastForwarding: false,
57
+ recoveries: 1,
58
+ migrating: false,
59
+ heads
60
+ }
61
+ }
62
+ }
63
+
64
+ const Checkpointer = {
65
+ preencode(state, idx) {
66
+ c.uint.preencode(state, idx.checkpointer)
67
+ if (idx.checkpoint !== null) IndexCheckpoint.preencode(state, idx.checkpoint)
68
+ },
69
+ encode(state, idx) {
70
+ c.uint.encode(state, idx.checkpointer)
71
+ if (idx.checkpoint !== null) IndexCheckpoint.encode(state, idx.checkpoint)
72
+ },
73
+ decode(state) {
74
+ const checkpointer = c.uint.decode(state)
75
+ const checkpoint = checkpointer ? null : IndexCheckpoint.decode(state)
76
+
77
+ return {
78
+ checkpointer,
79
+ checkpoint
80
+ }
81
+ }
82
+ }
83
+
84
+ const CheckpointerArray = c.array(Checkpointer)
85
+
86
+ const Indexer = {
87
+ preencode(state, m) {
88
+ c.uint.preencode(state, m.signature)
89
+ c.fixed32.preencode(state, m.namespace)
90
+ c.fixed32.preencode(state, m.publicKey)
91
+ },
92
+ encode(state, m) {
93
+ c.uint.encode(state, m.signature)
94
+ c.fixed32.encode(state, m.namespace)
95
+ c.fixed32.encode(state, m.publicKey)
96
+ },
97
+ decode(state) {
98
+ return {
99
+ signature: c.uint.decode(state),
100
+ namespace: c.fixed32.decode(state),
101
+ publicKey: c.fixed32.decode(state)
102
+ }
103
+ }
104
+ }
105
+
106
+ const Indexers = c.array(Indexer)
107
+
108
+ const DigestV0 = {
109
+ preencode(state, m) {
110
+ c.uint.preencode(state, m.pointer)
111
+ if (m.pointer === 0) {
112
+ Indexers.preencode(state, m.indexers)
113
+ }
114
+ },
115
+ encode(state, m) {
116
+ c.uint.encode(state, m.pointer)
117
+ if (m.pointer === 0) {
118
+ Indexers.encode(state, m.indexers)
119
+ }
120
+ },
121
+ decode(state) {
122
+ const pointer = c.uint.decode(state)
123
+ return {
124
+ pointer,
125
+ indexers: pointer === 0 ? Indexers.decode(state) : null
126
+ }
127
+ }
128
+ }
129
+
130
+ const Digest = {
131
+ preencode(state, m) {
132
+ c.uint.preencode(state, m.pointer)
133
+ if (m.pointer === 0) {
134
+ c.fixed32.preencode(state, m.key)
135
+ }
136
+ },
137
+ encode(state, m) {
138
+ c.uint.encode(state, m.pointer)
139
+ if (m.pointer === 0) {
140
+ c.fixed32.encode(state, m.key)
141
+ }
142
+ },
143
+ decode(state) {
144
+ const pointer = c.uint.decode(state)
145
+ return {
146
+ pointer,
147
+ key: pointer === 0 ? c.fixed32.decode(state) : null
148
+ }
149
+ }
150
+ }
151
+
152
+ const Node = {
153
+ preencode(state, m) {
154
+ Clock.preencode(state, m.heads)
155
+ c.uint.preencode(state, m.batch)
156
+ c.buffer.preencode(state, m.value)
157
+ },
158
+ encode(state, m) {
159
+ Clock.encode(state, m.heads)
160
+ c.uint.encode(state, m.batch)
161
+ c.buffer.encode(state, m.value)
162
+ },
163
+ decode(state, m) {
164
+ return {
165
+ heads: Clock.decode(state),
166
+ batch: c.uint.decode(state),
167
+ value: c.buffer.decode(state)
168
+ }
169
+ }
170
+ }
171
+
172
+ const Additional = {
173
+ preencode(state, m) {
174
+ c.uint.preencode(state, m.pointer)
175
+ if (m.pointer === 0) {
176
+ AdditionalData.preencode(state, m.data)
177
+ }
178
+ },
179
+ encode(state, m) {
180
+ c.uint.encode(state, m.pointer)
181
+ if (m.pointer === 0) {
182
+ AdditionalData.encode(state, m.data)
183
+ }
184
+ },
185
+ decode(state) {
186
+ const pointer = c.uint.decode(state)
187
+ return {
188
+ pointer,
189
+ data: pointer === 0 ? AdditionalData.decode(state) : null
190
+ }
191
+ }
192
+ }
193
+
194
+ const AdditionalData = {
195
+ preencode(state, m) {
196
+ c.uint.preencode(state, 0)
197
+ },
198
+ encode(state, m) {
199
+ c.uint.encode(state, 0) // empty for now, for the future
200
+ },
201
+ decode(state) {
202
+ const flags = c.uint.decode(state)
203
+ return {
204
+ encryptionId: flags & 1 ? c.fixed32.decode(state) : null, // to help validate the encryption key used
205
+ abi: flags & 2 ? c.uint.decode(state) : 0
206
+ }
207
+ }
208
+ }
209
+
210
+ const OplogMessageV1 = {
211
+ preencode(state, m) {
212
+ throw new Error('Encoding not supported')
213
+ },
214
+ encode(state, m) {
215
+ throw new Error('Encoding not supported')
216
+ },
217
+ decode(state) {
218
+ const maxSupportedVersion = c.uint.decode(state)
219
+
220
+ const flags = c.uint.decode(state)
221
+
222
+ const isCheckpointer = (flags & 1) !== 0
223
+
224
+ const chk = isCheckpointer ? CheckpointerArray.decode(state) : null
225
+ const digest = isCheckpointer ? Digest.decode(state) : null
226
+
227
+ const node = Node.decode(state)
228
+
229
+ return {
230
+ version: 1,
231
+ maxSupportedVersion,
232
+ digest,
233
+ checkpoint: chk ? { system: chk[0], encryption: null, user: chk.slice(1) } : null,
234
+ optimistic: (flags & 2) !== 0,
235
+ node
236
+ }
237
+ }
238
+ }
239
+
240
+ const OplogMessageV0 = {
241
+ preencode(state, m) {
242
+ throw new Error('Encoding not supported')
243
+ },
244
+ encode(state, m) {
245
+ throw new Error('Encoding not supported')
246
+ },
247
+ decode(state) {
248
+ const flags = c.uint.decode(state)
249
+
250
+ const isCheckpointer = (flags & 1) !== 0
251
+
252
+ if (isCheckpointer) DigestV0.decode(state)
253
+ const chk = isCheckpointer ? CheckpointerArray.decode(state) : null
254
+ const node = Node.decode(state)
255
+ Additional.decode(state)
256
+ const maxSupportedVersion = state.start < state.end ? c.uint.decode(state) : 0
257
+
258
+ return {
259
+ version: 0,
260
+ maxSupportedVersion,
261
+ digest: null,
262
+ checkpoint: chk ? { system: chk[0], encryption: null, user: chk.slice(1) } : null,
263
+ optimistic: null,
264
+ node
265
+ }
266
+ }
267
+ }
268
+
269
+ const SystemWriterV0 = {
270
+ preencode(state, m) {
271
+ throw new Error('Encoding not supported')
272
+ },
273
+ encode(state, m) {
274
+ throw new Error('Encoding not supported')
275
+ },
276
+ decode(state) {
277
+ const flags = c.uint.decode(state)
278
+
279
+ return {
280
+ version: 0,
281
+ isIndexer: flags & 1,
282
+ isRemoved: flags & 2,
283
+ length: c.uint.decode(state)
284
+ }
285
+ }
286
+ }
287
+
288
+ function infoLegacyMap(info) {
289
+ return {
290
+ version: info.version,
291
+ members: info.members,
292
+ pendingIndexers: info.pendingIndexers,
293
+ indexers: info.indexers,
294
+ heads: info.heads,
295
+ views: info.views,
296
+ encryptionLength: 0,
297
+ entropy: null
298
+ }
299
+ }
300
+
301
+ function memberLegacyMap(m) {
302
+ return {
303
+ version: 0,
304
+ isRemoved: m.isRemoved,
305
+ weight: m.isIndexer ? 2 : 1,
306
+ length: m.length
307
+ }
308
+ }
309
+
310
+ module.exports = {
311
+ BootRecordV0,
312
+ OplogMessageV0,
313
+ OplogMessageV1,
314
+ SystemWriterV0,
315
+ infoLegacyMap,
316
+ memberLegacyMap
317
+ }