compact-encoding 2.17.0 → 2.18.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/.gitattributes +1 -0
- package/.github/workflows/test-node.yml +7 -7
- package/.prettierrc +1 -0
- package/README.md +92 -93
- package/endian.js +2 -1
- package/index.js +306 -217
- package/lexint.js +19 -16
- package/package.json +5 -3
- package/raw.js +34 -34
- package/test.js +234 -60
package/index.js
CHANGED
|
@@ -3,16 +3,16 @@ const b4a = require('b4a')
|
|
|
3
3
|
const { BE } = require('./endian')
|
|
4
4
|
|
|
5
5
|
exports.state = function (start = 0, end = 0, buffer = null) {
|
|
6
|
-
return { start, end, buffer
|
|
6
|
+
return { start, end, buffer }
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
const raw = exports.raw = require('./raw')
|
|
9
|
+
const raw = (exports.raw = require('./raw'))
|
|
10
10
|
|
|
11
|
-
const uint = exports.uint = {
|
|
12
|
-
preencode
|
|
11
|
+
const uint = (exports.uint = {
|
|
12
|
+
preencode(state, n) {
|
|
13
13
|
state.end += n <= 0xfc ? 1 : n <= 0xffff ? 3 : n <= 0xffffffff ? 5 : 9
|
|
14
14
|
},
|
|
15
|
-
encode
|
|
15
|
+
encode(state, n) {
|
|
16
16
|
if (n <= 0xfc) uint8.encode(state, n)
|
|
17
17
|
else if (n <= 0xffff) {
|
|
18
18
|
state.buffer[state.start++] = 0xfd
|
|
@@ -25,58 +25,55 @@ const uint = exports.uint = {
|
|
|
25
25
|
uint64.encode(state, n)
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
|
-
decode
|
|
28
|
+
decode(state) {
|
|
29
29
|
const a = uint8.decode(state)
|
|
30
30
|
if (a <= 0xfc) return a
|
|
31
31
|
if (a === 0xfd) return uint16.decode(state)
|
|
32
32
|
if (a === 0xfe) return uint32.decode(state)
|
|
33
33
|
return uint64.decode(state)
|
|
34
34
|
}
|
|
35
|
-
}
|
|
35
|
+
})
|
|
36
36
|
|
|
37
|
-
const uint8 = exports.uint8 = {
|
|
38
|
-
preencode
|
|
37
|
+
const uint8 = (exports.uint8 = {
|
|
38
|
+
preencode(state, n) {
|
|
39
39
|
state.end += 1
|
|
40
40
|
},
|
|
41
|
-
encode
|
|
41
|
+
encode(state, n) {
|
|
42
42
|
validateUint(n)
|
|
43
43
|
state.buffer[state.start++] = n
|
|
44
44
|
},
|
|
45
|
-
decode
|
|
45
|
+
decode(state) {
|
|
46
46
|
if (state.start >= state.end) throw new Error('Out of bounds')
|
|
47
47
|
return state.buffer[state.start++]
|
|
48
48
|
}
|
|
49
|
-
}
|
|
49
|
+
})
|
|
50
50
|
|
|
51
|
-
const uint16 = exports.uint16 = {
|
|
52
|
-
preencode
|
|
51
|
+
const uint16 = (exports.uint16 = {
|
|
52
|
+
preencode(state, n) {
|
|
53
53
|
state.end += 2
|
|
54
54
|
},
|
|
55
|
-
encode
|
|
55
|
+
encode(state, n) {
|
|
56
56
|
validateUint(n)
|
|
57
57
|
state.buffer[state.start++] = n
|
|
58
58
|
state.buffer[state.start++] = n >>> 8
|
|
59
59
|
},
|
|
60
|
-
decode
|
|
60
|
+
decode(state) {
|
|
61
61
|
if (state.end - state.start < 2) throw new Error('Out of bounds')
|
|
62
|
-
return
|
|
63
|
-
state.buffer[state.start++] +
|
|
64
|
-
state.buffer[state.start++] * 0x100
|
|
65
|
-
)
|
|
62
|
+
return state.buffer[state.start++] + state.buffer[state.start++] * 0x100
|
|
66
63
|
}
|
|
67
|
-
}
|
|
64
|
+
})
|
|
68
65
|
|
|
69
|
-
const uint24 = exports.uint24 = {
|
|
70
|
-
preencode
|
|
66
|
+
const uint24 = (exports.uint24 = {
|
|
67
|
+
preencode(state, n) {
|
|
71
68
|
state.end += 3
|
|
72
69
|
},
|
|
73
|
-
encode
|
|
70
|
+
encode(state, n) {
|
|
74
71
|
validateUint(n)
|
|
75
72
|
state.buffer[state.start++] = n
|
|
76
73
|
state.buffer[state.start++] = n >>> 8
|
|
77
74
|
state.buffer[state.start++] = n >>> 16
|
|
78
75
|
},
|
|
79
|
-
decode
|
|
76
|
+
decode(state) {
|
|
80
77
|
if (state.end - state.start < 3) throw new Error('Out of bounds')
|
|
81
78
|
return (
|
|
82
79
|
state.buffer[state.start++] +
|
|
@@ -84,20 +81,20 @@ const uint24 = exports.uint24 = {
|
|
|
84
81
|
state.buffer[state.start++] * 0x10000
|
|
85
82
|
)
|
|
86
83
|
}
|
|
87
|
-
}
|
|
84
|
+
})
|
|
88
85
|
|
|
89
|
-
const uint32 = exports.uint32 = {
|
|
90
|
-
preencode
|
|
86
|
+
const uint32 = (exports.uint32 = {
|
|
87
|
+
preencode(state, n) {
|
|
91
88
|
state.end += 4
|
|
92
89
|
},
|
|
93
|
-
encode
|
|
90
|
+
encode(state, n) {
|
|
94
91
|
validateUint(n)
|
|
95
92
|
state.buffer[state.start++] = n
|
|
96
93
|
state.buffer[state.start++] = n >>> 8
|
|
97
94
|
state.buffer[state.start++] = n >>> 16
|
|
98
95
|
state.buffer[state.start++] = n >>> 24
|
|
99
96
|
},
|
|
100
|
-
decode
|
|
97
|
+
decode(state) {
|
|
101
98
|
if (state.end - state.start < 4) throw new Error('Out of bounds')
|
|
102
99
|
return (
|
|
103
100
|
state.buffer[state.start++] +
|
|
@@ -106,73 +103,73 @@ const uint32 = exports.uint32 = {
|
|
|
106
103
|
state.buffer[state.start++] * 0x1000000
|
|
107
104
|
)
|
|
108
105
|
}
|
|
109
|
-
}
|
|
106
|
+
})
|
|
110
107
|
|
|
111
|
-
const uint40 = exports.uint40 = {
|
|
112
|
-
preencode
|
|
108
|
+
const uint40 = (exports.uint40 = {
|
|
109
|
+
preencode(state, n) {
|
|
113
110
|
state.end += 5
|
|
114
111
|
},
|
|
115
|
-
encode
|
|
112
|
+
encode(state, n) {
|
|
116
113
|
validateUint(n)
|
|
117
114
|
const r = Math.floor(n / 0x100)
|
|
118
115
|
uint8.encode(state, n)
|
|
119
116
|
uint32.encode(state, r)
|
|
120
117
|
},
|
|
121
|
-
decode
|
|
118
|
+
decode(state) {
|
|
122
119
|
if (state.end - state.start < 5) throw new Error('Out of bounds')
|
|
123
120
|
return uint8.decode(state) + 0x100 * uint32.decode(state)
|
|
124
121
|
}
|
|
125
|
-
}
|
|
122
|
+
})
|
|
126
123
|
|
|
127
|
-
const uint48 = exports.uint48 = {
|
|
128
|
-
preencode
|
|
124
|
+
const uint48 = (exports.uint48 = {
|
|
125
|
+
preencode(state, n) {
|
|
129
126
|
state.end += 6
|
|
130
127
|
},
|
|
131
|
-
encode
|
|
128
|
+
encode(state, n) {
|
|
132
129
|
validateUint(n)
|
|
133
130
|
const r = Math.floor(n / 0x10000)
|
|
134
131
|
uint16.encode(state, n)
|
|
135
132
|
uint32.encode(state, r)
|
|
136
133
|
},
|
|
137
|
-
decode
|
|
134
|
+
decode(state) {
|
|
138
135
|
if (state.end - state.start < 6) throw new Error('Out of bounds')
|
|
139
136
|
return uint16.decode(state) + 0x10000 * uint32.decode(state)
|
|
140
137
|
}
|
|
141
|
-
}
|
|
138
|
+
})
|
|
142
139
|
|
|
143
|
-
const uint56 = exports.uint56 = {
|
|
144
|
-
preencode
|
|
140
|
+
const uint56 = (exports.uint56 = {
|
|
141
|
+
preencode(state, n) {
|
|
145
142
|
state.end += 7
|
|
146
143
|
},
|
|
147
|
-
encode
|
|
144
|
+
encode(state, n) {
|
|
148
145
|
validateUint(n)
|
|
149
146
|
const r = Math.floor(n / 0x1000000)
|
|
150
147
|
uint24.encode(state, n)
|
|
151
148
|
uint32.encode(state, r)
|
|
152
149
|
},
|
|
153
|
-
decode
|
|
150
|
+
decode(state) {
|
|
154
151
|
if (state.end - state.start < 7) throw new Error('Out of bounds')
|
|
155
152
|
return uint24.decode(state) + 0x1000000 * uint32.decode(state)
|
|
156
153
|
}
|
|
157
|
-
}
|
|
154
|
+
})
|
|
158
155
|
|
|
159
|
-
const uint64 = exports.uint64 = {
|
|
160
|
-
preencode
|
|
156
|
+
const uint64 = (exports.uint64 = {
|
|
157
|
+
preencode(state, n) {
|
|
161
158
|
state.end += 8
|
|
162
159
|
},
|
|
163
|
-
encode
|
|
160
|
+
encode(state, n) {
|
|
164
161
|
validateUint(n)
|
|
165
162
|
const r = Math.floor(n / 0x100000000)
|
|
166
163
|
uint32.encode(state, n)
|
|
167
164
|
uint32.encode(state, r)
|
|
168
165
|
},
|
|
169
|
-
decode
|
|
166
|
+
decode(state) {
|
|
170
167
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
171
168
|
return uint32.decode(state) + 0x100000000 * uint32.decode(state)
|
|
172
169
|
}
|
|
173
|
-
}
|
|
170
|
+
})
|
|
174
171
|
|
|
175
|
-
const int = exports.int = zigZagInt(uint)
|
|
172
|
+
const int = (exports.int = zigZagInt(uint))
|
|
176
173
|
exports.int8 = zigZagInt(uint8)
|
|
177
174
|
exports.int16 = zigZagInt(uint16)
|
|
178
175
|
exports.int24 = zigZagInt(uint24)
|
|
@@ -182,70 +179,95 @@ exports.int48 = zigZagInt(uint48)
|
|
|
182
179
|
exports.int56 = zigZagInt(uint56)
|
|
183
180
|
exports.int64 = zigZagInt(uint64)
|
|
184
181
|
|
|
185
|
-
const biguint64 = exports.biguint64 = {
|
|
186
|
-
preencode
|
|
182
|
+
const biguint64 = (exports.biguint64 = {
|
|
183
|
+
preencode(state, n) {
|
|
187
184
|
state.end += 8
|
|
188
185
|
},
|
|
189
|
-
encode
|
|
190
|
-
const view = new DataView(
|
|
186
|
+
encode(state, n) {
|
|
187
|
+
const view = new DataView(
|
|
188
|
+
state.buffer.buffer,
|
|
189
|
+
state.start + state.buffer.byteOffset,
|
|
190
|
+
8
|
|
191
|
+
)
|
|
191
192
|
view.setBigUint64(0, n, true) // little endian
|
|
192
193
|
state.start += 8
|
|
193
194
|
},
|
|
194
|
-
decode
|
|
195
|
+
decode(state) {
|
|
195
196
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
196
|
-
const view = new DataView(
|
|
197
|
+
const view = new DataView(
|
|
198
|
+
state.buffer.buffer,
|
|
199
|
+
state.start + state.buffer.byteOffset,
|
|
200
|
+
8
|
|
201
|
+
)
|
|
197
202
|
const n = view.getBigUint64(0, true) // little endian
|
|
198
203
|
state.start += 8
|
|
199
204
|
return n
|
|
200
205
|
}
|
|
201
|
-
}
|
|
206
|
+
})
|
|
202
207
|
|
|
203
208
|
exports.bigint64 = zigZagBigInt(biguint64)
|
|
204
209
|
|
|
205
|
-
const biguint = exports.biguint = {
|
|
206
|
-
preencode
|
|
210
|
+
const biguint = (exports.biguint = {
|
|
211
|
+
preencode(state, n) {
|
|
207
212
|
let len = 0
|
|
208
213
|
for (let m = n; m; m = m >> 64n) len++
|
|
209
214
|
uint.preencode(state, len)
|
|
210
215
|
state.end += 8 * len
|
|
211
216
|
},
|
|
212
|
-
encode
|
|
217
|
+
encode(state, n) {
|
|
213
218
|
let len = 0
|
|
214
219
|
for (let m = n; m; m = m >> 64n) len++
|
|
215
220
|
uint.encode(state, len)
|
|
216
|
-
const view = new DataView(
|
|
221
|
+
const view = new DataView(
|
|
222
|
+
state.buffer.buffer,
|
|
223
|
+
state.start + state.buffer.byteOffset,
|
|
224
|
+
8 * len
|
|
225
|
+
)
|
|
217
226
|
for (let m = n, i = 0; m; m = m >> 64n, i += 8) {
|
|
218
227
|
view.setBigUint64(i, BigInt.asUintN(64, m), true) // little endian
|
|
219
228
|
}
|
|
220
229
|
state.start += 8 * len
|
|
221
230
|
},
|
|
222
|
-
decode
|
|
231
|
+
decode(state) {
|
|
223
232
|
const len = uint.decode(state)
|
|
224
233
|
if (state.end - state.start < 8 * len) throw new Error('Out of bounds')
|
|
225
|
-
const view = new DataView(
|
|
234
|
+
const view = new DataView(
|
|
235
|
+
state.buffer.buffer,
|
|
236
|
+
state.start + state.buffer.byteOffset,
|
|
237
|
+
8 * len
|
|
238
|
+
)
|
|
226
239
|
let n = 0n
|
|
227
|
-
for (let i = len - 1; i >= 0; i--)
|
|
240
|
+
for (let i = len - 1; i >= 0; i--)
|
|
241
|
+
n = (n << 64n) + view.getBigUint64(i * 8, true) // little endian
|
|
228
242
|
state.start += 8 * len
|
|
229
243
|
return n
|
|
230
244
|
}
|
|
231
|
-
}
|
|
245
|
+
})
|
|
232
246
|
|
|
233
247
|
exports.bigint = zigZagBigInt(biguint)
|
|
234
248
|
|
|
235
249
|
exports.lexint = require('./lexint')
|
|
236
250
|
|
|
237
251
|
exports.float32 = {
|
|
238
|
-
preencode
|
|
252
|
+
preencode(state, n) {
|
|
239
253
|
state.end += 4
|
|
240
254
|
},
|
|
241
|
-
encode
|
|
242
|
-
const view = new DataView(
|
|
255
|
+
encode(state, n) {
|
|
256
|
+
const view = new DataView(
|
|
257
|
+
state.buffer.buffer,
|
|
258
|
+
state.start + state.buffer.byteOffset,
|
|
259
|
+
4
|
|
260
|
+
)
|
|
243
261
|
view.setFloat32(0, n, true) // little endian
|
|
244
262
|
state.start += 4
|
|
245
263
|
},
|
|
246
|
-
decode
|
|
264
|
+
decode(state) {
|
|
247
265
|
if (state.end - state.start < 4) throw new Error('Out of bounds')
|
|
248
|
-
const view = new DataView(
|
|
266
|
+
const view = new DataView(
|
|
267
|
+
state.buffer.buffer,
|
|
268
|
+
state.start + state.buffer.byteOffset,
|
|
269
|
+
4
|
|
270
|
+
)
|
|
249
271
|
const float = view.getFloat32(0, true) // little endian
|
|
250
272
|
state.start += 4
|
|
251
273
|
return float
|
|
@@ -253,58 +275,66 @@ exports.float32 = {
|
|
|
253
275
|
}
|
|
254
276
|
|
|
255
277
|
exports.float64 = {
|
|
256
|
-
preencode
|
|
278
|
+
preencode(state, n) {
|
|
257
279
|
state.end += 8
|
|
258
280
|
},
|
|
259
|
-
encode
|
|
260
|
-
const view = new DataView(
|
|
281
|
+
encode(state, n) {
|
|
282
|
+
const view = new DataView(
|
|
283
|
+
state.buffer.buffer,
|
|
284
|
+
state.start + state.buffer.byteOffset,
|
|
285
|
+
8
|
|
286
|
+
)
|
|
261
287
|
view.setFloat64(0, n, true) // little endian
|
|
262
288
|
state.start += 8
|
|
263
289
|
},
|
|
264
|
-
decode
|
|
290
|
+
decode(state) {
|
|
265
291
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
266
|
-
const view = new DataView(
|
|
292
|
+
const view = new DataView(
|
|
293
|
+
state.buffer.buffer,
|
|
294
|
+
state.start + state.buffer.byteOffset,
|
|
295
|
+
8
|
|
296
|
+
)
|
|
267
297
|
const float = view.getFloat64(0, true) // little endian
|
|
268
298
|
state.start += 8
|
|
269
299
|
return float
|
|
270
300
|
}
|
|
271
301
|
}
|
|
272
302
|
|
|
273
|
-
const buffer = exports.buffer = {
|
|
274
|
-
preencode
|
|
303
|
+
const buffer = (exports.buffer = {
|
|
304
|
+
preencode(state, b) {
|
|
275
305
|
if (b) uint8array.preencode(state, b)
|
|
276
306
|
else state.end++
|
|
277
307
|
},
|
|
278
|
-
encode
|
|
308
|
+
encode(state, b) {
|
|
279
309
|
if (b) uint8array.encode(state, b)
|
|
280
310
|
else state.buffer[state.start++] = 0
|
|
281
311
|
},
|
|
282
|
-
decode
|
|
312
|
+
decode(state) {
|
|
283
313
|
const len = uint.decode(state)
|
|
284
314
|
if (len === 0) return null
|
|
285
315
|
if (state.end - state.start < len) throw new Error('Out of bounds')
|
|
286
316
|
return state.buffer.subarray(state.start, (state.start += len))
|
|
287
317
|
}
|
|
288
|
-
}
|
|
318
|
+
})
|
|
289
319
|
|
|
290
320
|
exports.binary = {
|
|
291
321
|
...buffer,
|
|
292
|
-
preencode
|
|
322
|
+
preencode(state, b) {
|
|
293
323
|
if (typeof b === 'string') utf8.preencode(state, b)
|
|
294
324
|
else buffer.preencode(state, b)
|
|
295
325
|
},
|
|
296
|
-
encode
|
|
326
|
+
encode(state, b) {
|
|
297
327
|
if (typeof b === 'string') utf8.encode(state, b)
|
|
298
328
|
else buffer.encode(state, b)
|
|
299
329
|
}
|
|
300
330
|
}
|
|
301
331
|
|
|
302
332
|
exports.arraybuffer = {
|
|
303
|
-
preencode
|
|
333
|
+
preencode(state, b) {
|
|
304
334
|
uint.preencode(state, b.byteLength)
|
|
305
335
|
state.end += b.byteLength
|
|
306
336
|
},
|
|
307
|
-
encode
|
|
337
|
+
encode(state, b) {
|
|
308
338
|
uint.encode(state, b.byteLength)
|
|
309
339
|
|
|
310
340
|
const view = new Uint8Array(b)
|
|
@@ -312,27 +342,27 @@ exports.arraybuffer = {
|
|
|
312
342
|
state.buffer.set(view, state.start)
|
|
313
343
|
state.start += b.byteLength
|
|
314
344
|
},
|
|
315
|
-
decode
|
|
345
|
+
decode(state) {
|
|
316
346
|
const len = uint.decode(state)
|
|
317
347
|
|
|
318
348
|
const b = new ArrayBuffer(len)
|
|
319
349
|
const view = new Uint8Array(b)
|
|
320
350
|
|
|
321
|
-
view.set(state.buffer.subarray(state.start, state.start += len))
|
|
351
|
+
view.set(state.buffer.subarray(state.start, (state.start += len)))
|
|
322
352
|
|
|
323
353
|
return b
|
|
324
354
|
}
|
|
325
355
|
}
|
|
326
356
|
|
|
327
|
-
function typedarray
|
|
357
|
+
function typedarray(TypedArray, swap) {
|
|
328
358
|
const n = TypedArray.BYTES_PER_ELEMENT
|
|
329
359
|
|
|
330
360
|
return {
|
|
331
|
-
preencode
|
|
361
|
+
preencode(state, b) {
|
|
332
362
|
uint.preencode(state, b.length)
|
|
333
363
|
state.end += b.byteLength
|
|
334
364
|
},
|
|
335
|
-
encode
|
|
365
|
+
encode(state, b) {
|
|
336
366
|
uint.encode(state, b.length)
|
|
337
367
|
|
|
338
368
|
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
|
@@ -342,12 +372,12 @@ function typedarray (TypedArray, swap) {
|
|
|
342
372
|
state.buffer.set(view, state.start)
|
|
343
373
|
state.start += b.byteLength
|
|
344
374
|
},
|
|
345
|
-
decode
|
|
375
|
+
decode(state) {
|
|
346
376
|
const len = uint.decode(state)
|
|
347
377
|
|
|
348
|
-
let b = state.buffer.subarray(state.start, state.start += len * n)
|
|
378
|
+
let b = state.buffer.subarray(state.start, (state.start += len * n))
|
|
349
379
|
if (b.byteLength !== len * n) throw new Error('Out of bounds')
|
|
350
|
-
if (
|
|
380
|
+
if (b.byteOffset % n !== 0) b = new Uint8Array(b)
|
|
351
381
|
|
|
352
382
|
if (BE && swap) swap(b)
|
|
353
383
|
|
|
@@ -356,7 +386,7 @@ function typedarray (TypedArray, swap) {
|
|
|
356
386
|
}
|
|
357
387
|
}
|
|
358
388
|
|
|
359
|
-
const uint8array = exports.uint8array = typedarray(Uint8Array)
|
|
389
|
+
const uint8array = (exports.uint8array = typedarray(Uint8Array))
|
|
360
390
|
exports.uint16array = typedarray(Uint16Array, b4a.swap16)
|
|
361
391
|
exports.uint32array = typedarray(Uint32Array, b4a.swap32)
|
|
362
392
|
|
|
@@ -370,92 +400,102 @@ exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
|
|
|
370
400
|
exports.float32array = typedarray(Float32Array, b4a.swap32)
|
|
371
401
|
exports.float64array = typedarray(Float64Array, b4a.swap64)
|
|
372
402
|
|
|
373
|
-
function string
|
|
403
|
+
function string(encoding) {
|
|
374
404
|
return {
|
|
375
|
-
preencode
|
|
405
|
+
preencode(state, s) {
|
|
376
406
|
const len = b4a.byteLength(s, encoding)
|
|
377
407
|
uint.preencode(state, len)
|
|
378
408
|
state.end += len
|
|
379
409
|
},
|
|
380
|
-
encode
|
|
410
|
+
encode(state, s) {
|
|
381
411
|
const len = b4a.byteLength(s, encoding)
|
|
382
412
|
uint.encode(state, len)
|
|
383
413
|
b4a.write(state.buffer, s, state.start, encoding)
|
|
384
414
|
state.start += len
|
|
385
415
|
},
|
|
386
|
-
decode
|
|
416
|
+
decode(state) {
|
|
387
417
|
const len = uint.decode(state)
|
|
388
418
|
if (state.end - state.start < len) throw new Error('Out of bounds')
|
|
389
|
-
return b4a.toString(
|
|
419
|
+
return b4a.toString(
|
|
420
|
+
state.buffer,
|
|
421
|
+
encoding,
|
|
422
|
+
state.start,
|
|
423
|
+
(state.start += len)
|
|
424
|
+
)
|
|
390
425
|
},
|
|
391
|
-
fixed
|
|
426
|
+
fixed(n) {
|
|
392
427
|
return {
|
|
393
|
-
preencode
|
|
428
|
+
preencode(state) {
|
|
394
429
|
state.end += n
|
|
395
430
|
},
|
|
396
|
-
encode
|
|
431
|
+
encode(state, s) {
|
|
397
432
|
b4a.write(state.buffer, s, state.start, n, encoding)
|
|
398
433
|
state.start += n
|
|
399
434
|
},
|
|
400
|
-
decode
|
|
435
|
+
decode(state) {
|
|
401
436
|
if (state.end - state.start < n) throw new Error('Out of bounds')
|
|
402
|
-
return b4a.toString(
|
|
437
|
+
return b4a.toString(
|
|
438
|
+
state.buffer,
|
|
439
|
+
encoding,
|
|
440
|
+
state.start,
|
|
441
|
+
(state.start += n)
|
|
442
|
+
)
|
|
403
443
|
}
|
|
404
444
|
}
|
|
405
445
|
}
|
|
406
446
|
}
|
|
407
447
|
}
|
|
408
448
|
|
|
409
|
-
const utf8 = exports.string = exports.utf8 = string('utf-8')
|
|
449
|
+
const utf8 = (exports.string = exports.utf8 = string('utf-8'))
|
|
410
450
|
exports.ascii = string('ascii')
|
|
411
451
|
exports.hex = string('hex')
|
|
412
452
|
exports.base64 = string('base64')
|
|
413
453
|
exports.ucs2 = exports.utf16le = string('utf16le')
|
|
414
454
|
|
|
415
455
|
exports.bool = {
|
|
416
|
-
preencode
|
|
456
|
+
preencode(state, b) {
|
|
417
457
|
state.end++
|
|
418
458
|
},
|
|
419
|
-
encode
|
|
459
|
+
encode(state, b) {
|
|
420
460
|
state.buffer[state.start++] = b ? 1 : 0
|
|
421
461
|
},
|
|
422
|
-
decode
|
|
462
|
+
decode(state) {
|
|
423
463
|
if (state.start >= state.end) throw Error('Out of bounds')
|
|
424
464
|
return state.buffer[state.start++] === 1
|
|
425
465
|
}
|
|
426
466
|
}
|
|
427
467
|
|
|
428
|
-
const fixed = exports.fixed = function fixed
|
|
468
|
+
const fixed = (exports.fixed = function fixed(n) {
|
|
429
469
|
return {
|
|
430
|
-
preencode
|
|
470
|
+
preencode(state, s) {
|
|
431
471
|
if (s.byteLength !== n) throw new Error('Incorrect buffer size')
|
|
432
472
|
state.end += n
|
|
433
473
|
},
|
|
434
|
-
encode
|
|
474
|
+
encode(state, s) {
|
|
435
475
|
state.buffer.set(s, state.start)
|
|
436
476
|
state.start += n
|
|
437
477
|
},
|
|
438
|
-
decode
|
|
478
|
+
decode(state) {
|
|
439
479
|
if (state.end - state.start < n) throw new Error('Out of bounds')
|
|
440
480
|
return state.buffer.subarray(state.start, (state.start += n))
|
|
441
481
|
}
|
|
442
482
|
}
|
|
443
|
-
}
|
|
483
|
+
})
|
|
444
484
|
|
|
445
485
|
exports.fixed32 = fixed(32)
|
|
446
486
|
exports.fixed64 = fixed(64)
|
|
447
487
|
|
|
448
|
-
exports.array = function array
|
|
488
|
+
exports.array = function array(enc) {
|
|
449
489
|
return {
|
|
450
|
-
preencode
|
|
490
|
+
preencode(state, list) {
|
|
451
491
|
uint.preencode(state, list.length)
|
|
452
492
|
for (let i = 0; i < list.length; i++) enc.preencode(state, list[i])
|
|
453
493
|
},
|
|
454
|
-
encode
|
|
494
|
+
encode(state, list) {
|
|
455
495
|
uint.encode(state, list.length)
|
|
456
496
|
for (let i = 0; i < list.length; i++) enc.encode(state, list[i])
|
|
457
497
|
},
|
|
458
|
-
decode
|
|
498
|
+
decode(state) {
|
|
459
499
|
const len = uint.decode(state)
|
|
460
500
|
if (len > 0x100000) throw new Error('Array is too big')
|
|
461
501
|
const arr = new Array(len)
|
|
@@ -465,22 +505,22 @@ exports.array = function array (enc) {
|
|
|
465
505
|
}
|
|
466
506
|
}
|
|
467
507
|
|
|
468
|
-
exports.frame = function frame
|
|
508
|
+
exports.frame = function frame(enc) {
|
|
469
509
|
const dummy = exports.state()
|
|
470
510
|
|
|
471
511
|
return {
|
|
472
|
-
preencode
|
|
512
|
+
preencode(state, m) {
|
|
473
513
|
const end = state.end
|
|
474
514
|
enc.preencode(state, m)
|
|
475
515
|
uint.preencode(state, state.end - end)
|
|
476
516
|
},
|
|
477
|
-
encode
|
|
517
|
+
encode(state, m) {
|
|
478
518
|
dummy.end = 0
|
|
479
519
|
enc.preencode(dummy, m)
|
|
480
520
|
uint.encode(state, dummy.end)
|
|
481
521
|
enc.encode(state, m)
|
|
482
522
|
},
|
|
483
|
-
decode
|
|
523
|
+
decode(state) {
|
|
484
524
|
const end = state.end
|
|
485
525
|
const len = uint.decode(state)
|
|
486
526
|
state.end = state.start + len
|
|
@@ -493,50 +533,50 @@ exports.frame = function frame (enc) {
|
|
|
493
533
|
}
|
|
494
534
|
|
|
495
535
|
exports.date = {
|
|
496
|
-
preencode
|
|
536
|
+
preencode(state, d) {
|
|
497
537
|
int.preencode(state, d.getTime())
|
|
498
538
|
},
|
|
499
|
-
encode
|
|
539
|
+
encode(state, d) {
|
|
500
540
|
int.encode(state, d.getTime())
|
|
501
541
|
},
|
|
502
|
-
decode
|
|
542
|
+
decode(state, d) {
|
|
503
543
|
return new Date(int.decode(state))
|
|
504
544
|
}
|
|
505
545
|
}
|
|
506
546
|
|
|
507
547
|
exports.json = {
|
|
508
|
-
preencode
|
|
548
|
+
preencode(state, v) {
|
|
509
549
|
utf8.preencode(state, JSON.stringify(v))
|
|
510
550
|
},
|
|
511
|
-
encode
|
|
551
|
+
encode(state, v) {
|
|
512
552
|
utf8.encode(state, JSON.stringify(v))
|
|
513
553
|
},
|
|
514
|
-
decode
|
|
554
|
+
decode(state) {
|
|
515
555
|
return JSON.parse(utf8.decode(state))
|
|
516
556
|
}
|
|
517
557
|
}
|
|
518
558
|
|
|
519
559
|
exports.ndjson = {
|
|
520
|
-
preencode
|
|
560
|
+
preencode(state, v) {
|
|
521
561
|
utf8.preencode(state, JSON.stringify(v) + '\n')
|
|
522
562
|
},
|
|
523
|
-
encode
|
|
563
|
+
encode(state, v) {
|
|
524
564
|
utf8.encode(state, JSON.stringify(v) + '\n')
|
|
525
565
|
},
|
|
526
|
-
decode
|
|
566
|
+
decode(state) {
|
|
527
567
|
return JSON.parse(utf8.decode(state))
|
|
528
568
|
}
|
|
529
569
|
}
|
|
530
570
|
|
|
531
571
|
// simple helper for when you want to just express nothing
|
|
532
572
|
exports.none = {
|
|
533
|
-
preencode
|
|
573
|
+
preencode(state, n) {
|
|
534
574
|
// do nothing
|
|
535
575
|
},
|
|
536
|
-
encode
|
|
576
|
+
encode(state, n) {
|
|
537
577
|
// do nothing
|
|
538
578
|
},
|
|
539
|
-
decode
|
|
579
|
+
decode(state) {
|
|
540
580
|
return null
|
|
541
581
|
}
|
|
542
582
|
}
|
|
@@ -544,19 +584,19 @@ exports.none = {
|
|
|
544
584
|
// "any" encoders here for helping just structure any object without schematising it
|
|
545
585
|
|
|
546
586
|
const anyArray = {
|
|
547
|
-
preencode
|
|
587
|
+
preencode(state, arr) {
|
|
548
588
|
uint.preencode(state, arr.length)
|
|
549
589
|
for (let i = 0; i < arr.length; i++) {
|
|
550
590
|
any.preencode(state, arr[i])
|
|
551
591
|
}
|
|
552
592
|
},
|
|
553
|
-
encode
|
|
593
|
+
encode(state, arr) {
|
|
554
594
|
uint.encode(state, arr.length)
|
|
555
595
|
for (let i = 0; i < arr.length; i++) {
|
|
556
596
|
any.encode(state, arr[i])
|
|
557
597
|
}
|
|
558
598
|
},
|
|
559
|
-
decode
|
|
599
|
+
decode(state) {
|
|
560
600
|
const arr = []
|
|
561
601
|
let len = uint.decode(state)
|
|
562
602
|
while (len-- > 0) {
|
|
@@ -567,7 +607,7 @@ const anyArray = {
|
|
|
567
607
|
}
|
|
568
608
|
|
|
569
609
|
const anyObject = {
|
|
570
|
-
preencode
|
|
610
|
+
preencode(state, o) {
|
|
571
611
|
const keys = Object.keys(o)
|
|
572
612
|
uint.preencode(state, keys.length)
|
|
573
613
|
for (const key of keys) {
|
|
@@ -575,7 +615,7 @@ const anyObject = {
|
|
|
575
615
|
any.preencode(state, o[key])
|
|
576
616
|
}
|
|
577
617
|
},
|
|
578
|
-
encode
|
|
618
|
+
encode(state, o) {
|
|
579
619
|
const keys = Object.keys(o)
|
|
580
620
|
uint.encode(state, keys.length)
|
|
581
621
|
for (const key of keys) {
|
|
@@ -583,7 +623,7 @@ const anyObject = {
|
|
|
583
623
|
any.encode(state, o[key])
|
|
584
624
|
}
|
|
585
625
|
},
|
|
586
|
-
decode
|
|
626
|
+
decode(state) {
|
|
587
627
|
let len = uint.decode(state)
|
|
588
628
|
const o = {}
|
|
589
629
|
while (len-- > 0) {
|
|
@@ -607,37 +647,37 @@ const anyTypes = [
|
|
|
607
647
|
exports.date
|
|
608
648
|
]
|
|
609
649
|
|
|
610
|
-
const any = exports.any = {
|
|
611
|
-
preencode
|
|
650
|
+
const any = (exports.any = {
|
|
651
|
+
preencode(state, o) {
|
|
612
652
|
const t = getType(o)
|
|
613
653
|
uint.preencode(state, t)
|
|
614
654
|
anyTypes[t].preencode(state, o)
|
|
615
655
|
},
|
|
616
|
-
encode
|
|
656
|
+
encode(state, o) {
|
|
617
657
|
const t = getType(o)
|
|
618
658
|
uint.encode(state, t)
|
|
619
659
|
anyTypes[t].encode(state, o)
|
|
620
660
|
},
|
|
621
|
-
decode
|
|
661
|
+
decode(state) {
|
|
622
662
|
const t = uint.decode(state)
|
|
623
663
|
if (t >= anyTypes.length) throw new Error('Unknown type: ' + t)
|
|
624
664
|
return anyTypes[t].decode(state)
|
|
625
665
|
}
|
|
626
|
-
}
|
|
666
|
+
})
|
|
627
667
|
|
|
628
|
-
const port = exports.port = uint16
|
|
668
|
+
const port = (exports.port = uint16)
|
|
629
669
|
|
|
630
670
|
const address = (host, family) => {
|
|
631
671
|
return {
|
|
632
|
-
preencode
|
|
672
|
+
preencode(state, m) {
|
|
633
673
|
host.preencode(state, m.host)
|
|
634
674
|
port.preencode(state, m.port)
|
|
635
675
|
},
|
|
636
|
-
encode
|
|
676
|
+
encode(state, m) {
|
|
637
677
|
host.encode(state, m.host)
|
|
638
678
|
port.encode(state, m.port)
|
|
639
679
|
},
|
|
640
|
-
decode
|
|
680
|
+
decode(state) {
|
|
641
681
|
return {
|
|
642
682
|
host: host.decode(state),
|
|
643
683
|
family,
|
|
@@ -647,11 +687,11 @@ const address = (host, family) => {
|
|
|
647
687
|
}
|
|
648
688
|
}
|
|
649
689
|
|
|
650
|
-
const ipv4 = exports.ipv4 = {
|
|
651
|
-
preencode
|
|
690
|
+
const ipv4 = (exports.ipv4 = {
|
|
691
|
+
preencode(state) {
|
|
652
692
|
state.end += 4
|
|
653
693
|
},
|
|
654
|
-
encode
|
|
694
|
+
encode(state, string) {
|
|
655
695
|
const start = state.start
|
|
656
696
|
const end = start + 4
|
|
657
697
|
|
|
@@ -661,7 +701,10 @@ const ipv4 = exports.ipv4 = {
|
|
|
661
701
|
let n = 0
|
|
662
702
|
let c
|
|
663
703
|
|
|
664
|
-
while (
|
|
704
|
+
while (
|
|
705
|
+
i < string.length &&
|
|
706
|
+
(c = string.charCodeAt(i++)) !== /* . */ 0x2e
|
|
707
|
+
) {
|
|
665
708
|
n = n * 10 + (c - /* 0 */ 0x30)
|
|
666
709
|
}
|
|
667
710
|
|
|
@@ -670,24 +713,27 @@ const ipv4 = exports.ipv4 = {
|
|
|
670
713
|
|
|
671
714
|
state.start = end
|
|
672
715
|
},
|
|
673
|
-
decode
|
|
716
|
+
decode(state) {
|
|
674
717
|
if (state.end - state.start < 4) throw new Error('Out of bounds')
|
|
675
718
|
return (
|
|
676
|
-
state.buffer[state.start++] +
|
|
677
|
-
|
|
678
|
-
state.buffer[state.start++] +
|
|
719
|
+
state.buffer[state.start++] +
|
|
720
|
+
'.' +
|
|
721
|
+
state.buffer[state.start++] +
|
|
722
|
+
'.' +
|
|
723
|
+
state.buffer[state.start++] +
|
|
724
|
+
'.' +
|
|
679
725
|
state.buffer[state.start++]
|
|
680
726
|
)
|
|
681
727
|
}
|
|
682
|
-
}
|
|
728
|
+
})
|
|
683
729
|
|
|
684
730
|
exports.ipv4Address = address(ipv4, 4)
|
|
685
731
|
|
|
686
|
-
const ipv6 = exports.ipv6 = {
|
|
687
|
-
preencode
|
|
732
|
+
const ipv6 = (exports.ipv6 = {
|
|
733
|
+
preencode(state) {
|
|
688
734
|
state.end += 16
|
|
689
735
|
},
|
|
690
|
-
encode
|
|
736
|
+
encode(state, string) {
|
|
691
737
|
const start = state.start
|
|
692
738
|
const end = start + 16
|
|
693
739
|
|
|
@@ -698,7 +744,10 @@ const ipv6 = exports.ipv6 = {
|
|
|
698
744
|
let n = 0
|
|
699
745
|
let c
|
|
700
746
|
|
|
701
|
-
while (
|
|
747
|
+
while (
|
|
748
|
+
i < string.length &&
|
|
749
|
+
(c = string.charCodeAt(i++)) !== /* : */ 0x3a
|
|
750
|
+
) {
|
|
702
751
|
if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30)
|
|
703
752
|
else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10)
|
|
704
753
|
else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
|
|
@@ -722,53 +771,84 @@ const ipv6 = exports.ipv6 = {
|
|
|
722
771
|
|
|
723
772
|
state.start = end
|
|
724
773
|
},
|
|
725
|
-
decode
|
|
774
|
+
decode(state) {
|
|
726
775
|
if (state.end - state.start < 16) throw new Error('Out of bounds')
|
|
727
776
|
return (
|
|
728
|
-
(
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
(
|
|
734
|
-
|
|
735
|
-
|
|
777
|
+
(
|
|
778
|
+
state.buffer[state.start++] * 256 +
|
|
779
|
+
state.buffer[state.start++]
|
|
780
|
+
).toString(16) +
|
|
781
|
+
':' +
|
|
782
|
+
(
|
|
783
|
+
state.buffer[state.start++] * 256 +
|
|
784
|
+
state.buffer[state.start++]
|
|
785
|
+
).toString(16) +
|
|
786
|
+
':' +
|
|
787
|
+
(
|
|
788
|
+
state.buffer[state.start++] * 256 +
|
|
789
|
+
state.buffer[state.start++]
|
|
790
|
+
).toString(16) +
|
|
791
|
+
':' +
|
|
792
|
+
(
|
|
793
|
+
state.buffer[state.start++] * 256 +
|
|
794
|
+
state.buffer[state.start++]
|
|
795
|
+
).toString(16) +
|
|
796
|
+
':' +
|
|
797
|
+
(
|
|
798
|
+
state.buffer[state.start++] * 256 +
|
|
799
|
+
state.buffer[state.start++]
|
|
800
|
+
).toString(16) +
|
|
801
|
+
':' +
|
|
802
|
+
(
|
|
803
|
+
state.buffer[state.start++] * 256 +
|
|
804
|
+
state.buffer[state.start++]
|
|
805
|
+
).toString(16) +
|
|
806
|
+
':' +
|
|
807
|
+
(
|
|
808
|
+
state.buffer[state.start++] * 256 +
|
|
809
|
+
state.buffer[state.start++]
|
|
810
|
+
).toString(16) +
|
|
811
|
+
':' +
|
|
812
|
+
(
|
|
813
|
+
state.buffer[state.start++] * 256 +
|
|
814
|
+
state.buffer[state.start++]
|
|
815
|
+
).toString(16)
|
|
736
816
|
)
|
|
737
817
|
}
|
|
738
|
-
}
|
|
818
|
+
})
|
|
739
819
|
|
|
740
820
|
exports.ipv6Address = address(ipv6, 6)
|
|
741
821
|
|
|
742
|
-
const ip = exports.ip = {
|
|
743
|
-
preencode
|
|
822
|
+
const ip = (exports.ip = {
|
|
823
|
+
preencode(state, string) {
|
|
744
824
|
const family = string.includes(':') ? 6 : 4
|
|
745
825
|
uint8.preencode(state, family)
|
|
746
826
|
if (family === 4) ipv4.preencode(state)
|
|
747
827
|
else ipv6.preencode(state)
|
|
748
828
|
},
|
|
749
|
-
encode
|
|
829
|
+
encode(state, string) {
|
|
750
830
|
const family = string.includes(':') ? 6 : 4
|
|
751
831
|
uint8.encode(state, family)
|
|
752
832
|
if (family === 4) ipv4.encode(state, string)
|
|
753
833
|
else ipv6.encode(state, string)
|
|
754
834
|
},
|
|
755
|
-
decode
|
|
835
|
+
decode(state) {
|
|
756
836
|
const family = uint8.decode(state)
|
|
757
837
|
if (family === 4) return ipv4.decode(state)
|
|
758
838
|
else return ipv6.decode(state)
|
|
759
839
|
}
|
|
760
|
-
}
|
|
840
|
+
})
|
|
761
841
|
|
|
762
842
|
exports.ipAddress = {
|
|
763
|
-
preencode
|
|
843
|
+
preencode(state, m) {
|
|
764
844
|
ip.preencode(state, m.host)
|
|
765
845
|
port.preencode(state, m.port)
|
|
766
846
|
},
|
|
767
|
-
encode
|
|
847
|
+
encode(state, m) {
|
|
768
848
|
ip.encode(state, m.host)
|
|
769
849
|
port.encode(state, m.port)
|
|
770
850
|
},
|
|
771
|
-
decode
|
|
851
|
+
decode(state) {
|
|
772
852
|
const family = uint8.decode(state)
|
|
773
853
|
return {
|
|
774
854
|
host: family === 4 ? ipv4.decode(state) : ipv6.decode(state),
|
|
@@ -778,7 +858,7 @@ exports.ipAddress = {
|
|
|
778
858
|
}
|
|
779
859
|
}
|
|
780
860
|
|
|
781
|
-
function getType
|
|
861
|
+
function getType(o) {
|
|
782
862
|
if (o === null || o === undefined) return 0
|
|
783
863
|
if (typeof o === 'boolean') return 1
|
|
784
864
|
if (typeof o === 'string') return 2
|
|
@@ -794,61 +874,69 @@ function getType (o) {
|
|
|
794
874
|
throw new Error('Unsupported type for ' + o)
|
|
795
875
|
}
|
|
796
876
|
|
|
797
|
-
exports.from = function from
|
|
877
|
+
exports.from = function from(enc) {
|
|
798
878
|
if (typeof enc === 'string') return fromNamed(enc)
|
|
799
879
|
if (enc.preencode) return enc
|
|
800
880
|
if (enc.encodingLength) return fromAbstractEncoder(enc)
|
|
801
881
|
return fromCodec(enc)
|
|
802
882
|
}
|
|
803
883
|
|
|
804
|
-
function fromNamed
|
|
884
|
+
function fromNamed(enc) {
|
|
805
885
|
switch (enc) {
|
|
806
|
-
case 'ascii':
|
|
886
|
+
case 'ascii':
|
|
887
|
+
return raw.ascii
|
|
807
888
|
case 'utf-8':
|
|
808
|
-
case 'utf8':
|
|
809
|
-
|
|
810
|
-
case '
|
|
889
|
+
case 'utf8':
|
|
890
|
+
return raw.utf8
|
|
891
|
+
case 'hex':
|
|
892
|
+
return raw.hex
|
|
893
|
+
case 'base64':
|
|
894
|
+
return raw.base64
|
|
811
895
|
case 'utf16-le':
|
|
812
896
|
case 'utf16le':
|
|
813
897
|
case 'ucs-2':
|
|
814
|
-
case 'ucs2':
|
|
815
|
-
|
|
816
|
-
case '
|
|
898
|
+
case 'ucs2':
|
|
899
|
+
return raw.ucs2
|
|
900
|
+
case 'ndjson':
|
|
901
|
+
return raw.ndjson
|
|
902
|
+
case 'json':
|
|
903
|
+
return raw.json
|
|
817
904
|
case 'binary':
|
|
818
|
-
default:
|
|
905
|
+
default:
|
|
906
|
+
return raw.binary
|
|
819
907
|
}
|
|
820
908
|
}
|
|
821
909
|
|
|
822
|
-
function fromCodec
|
|
910
|
+
function fromCodec(enc) {
|
|
823
911
|
let tmpM = null
|
|
824
912
|
let tmpBuf = null
|
|
825
913
|
|
|
826
914
|
return {
|
|
827
|
-
preencode
|
|
915
|
+
preencode(state, m) {
|
|
828
916
|
tmpM = m
|
|
829
917
|
tmpBuf = enc.encode(m)
|
|
830
918
|
state.end += tmpBuf.byteLength
|
|
831
919
|
},
|
|
832
|
-
encode
|
|
920
|
+
encode(state, m) {
|
|
833
921
|
raw.encode(state, m === tmpM ? tmpBuf : enc.encode(m))
|
|
834
922
|
tmpM = tmpBuf = null
|
|
835
923
|
},
|
|
836
|
-
decode
|
|
924
|
+
decode(state) {
|
|
837
925
|
return enc.decode(raw.decode(state))
|
|
838
926
|
}
|
|
839
927
|
}
|
|
840
928
|
}
|
|
841
929
|
|
|
842
|
-
function fromAbstractEncoder
|
|
930
|
+
function fromAbstractEncoder(enc) {
|
|
843
931
|
return {
|
|
844
|
-
preencode
|
|
932
|
+
preencode(state, m) {
|
|
845
933
|
state.end += enc.encodingLength(m)
|
|
846
934
|
},
|
|
847
|
-
encode
|
|
935
|
+
encode(state, m) {
|
|
848
936
|
enc.encode(m, state.buffer, state.start)
|
|
849
937
|
state.start += enc.encode.bytes
|
|
850
938
|
},
|
|
851
|
-
decode
|
|
939
|
+
decode(state) {
|
|
852
940
|
const m = enc.decode(state.buffer, state.start, state.end)
|
|
853
941
|
state.start += enc.decode.bytes
|
|
854
942
|
return m
|
|
@@ -856,7 +944,7 @@ function fromAbstractEncoder (enc) {
|
|
|
856
944
|
}
|
|
857
945
|
}
|
|
858
946
|
|
|
859
|
-
exports.encode = function encode
|
|
947
|
+
exports.encode = function encode(enc, m) {
|
|
860
948
|
const state = exports.state()
|
|
861
949
|
enc.preencode(state, m)
|
|
862
950
|
state.buffer = b4a.allocUnsafe(state.end)
|
|
@@ -864,56 +952,57 @@ exports.encode = function encode (enc, m) {
|
|
|
864
952
|
return state.buffer
|
|
865
953
|
}
|
|
866
954
|
|
|
867
|
-
exports.decode = function decode
|
|
955
|
+
exports.decode = function decode(enc, buffer) {
|
|
868
956
|
return enc.decode(exports.state(0, buffer.byteLength, buffer))
|
|
869
957
|
}
|
|
870
958
|
|
|
871
|
-
function zigZagInt
|
|
959
|
+
function zigZagInt(enc) {
|
|
872
960
|
return {
|
|
873
|
-
preencode
|
|
961
|
+
preencode(state, n) {
|
|
874
962
|
enc.preencode(state, zigZagEncodeInt(n))
|
|
875
963
|
},
|
|
876
|
-
encode
|
|
964
|
+
encode(state, n) {
|
|
877
965
|
enc.encode(state, zigZagEncodeInt(n))
|
|
878
966
|
},
|
|
879
|
-
decode
|
|
967
|
+
decode(state) {
|
|
880
968
|
return zigZagDecodeInt(enc.decode(state))
|
|
881
969
|
}
|
|
882
970
|
}
|
|
883
971
|
}
|
|
884
972
|
|
|
885
|
-
function zigZagDecodeInt
|
|
973
|
+
function zigZagDecodeInt(n) {
|
|
886
974
|
return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2
|
|
887
975
|
}
|
|
888
976
|
|
|
889
|
-
function zigZagEncodeInt
|
|
977
|
+
function zigZagEncodeInt(n) {
|
|
890
978
|
// 0, -1, 1, -2, 2, ...
|
|
891
|
-
return n < 0 ?
|
|
979
|
+
return n < 0 ? 2 * -n - 1 : n === 0 ? 0 : 2 * n
|
|
892
980
|
}
|
|
893
981
|
|
|
894
|
-
function zigZagBigInt
|
|
982
|
+
function zigZagBigInt(enc) {
|
|
895
983
|
return {
|
|
896
|
-
preencode
|
|
984
|
+
preencode(state, n) {
|
|
897
985
|
enc.preencode(state, zigZagEncodeBigInt(n))
|
|
898
986
|
},
|
|
899
|
-
encode
|
|
987
|
+
encode(state, n) {
|
|
900
988
|
enc.encode(state, zigZagEncodeBigInt(n))
|
|
901
989
|
},
|
|
902
|
-
decode
|
|
990
|
+
decode(state) {
|
|
903
991
|
return zigZagDecodeBigInt(enc.decode(state))
|
|
904
992
|
}
|
|
905
993
|
}
|
|
906
994
|
}
|
|
907
995
|
|
|
908
|
-
function zigZagDecodeBigInt
|
|
996
|
+
function zigZagDecodeBigInt(n) {
|
|
909
997
|
return n === 0n ? n : (n & 1n) === 0n ? n / 2n : -(n + 1n) / 2n
|
|
910
998
|
}
|
|
911
999
|
|
|
912
|
-
function zigZagEncodeBigInt
|
|
1000
|
+
function zigZagEncodeBigInt(n) {
|
|
913
1001
|
// 0, -1, 1, -2, 2, ...
|
|
914
|
-
return n < 0n ?
|
|
1002
|
+
return n < 0n ? 2n * -n - 1n : n === 0n ? 0n : 2n * n
|
|
915
1003
|
}
|
|
916
1004
|
|
|
917
|
-
function validateUint
|
|
918
|
-
if (
|
|
1005
|
+
function validateUint(n) {
|
|
1006
|
+
if (n >= 0 === false /* Handles NaN as well */)
|
|
1007
|
+
throw new Error('uint must be positive')
|
|
919
1008
|
}
|