compact-encoding 3.3.2 → 3.5.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 +4 -0
- package/endian.js +1 -2
- package/index.d.ts +5 -3
- package/index.js +59 -112
- package/lexint.js +1 -3
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -123,6 +123,7 @@ to build others on top. Feel free to PR more that are missing.
|
|
|
123
123
|
- `cenc.raw.float32array` - Encodes a float32array without a length prefixed.
|
|
124
124
|
- `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
|
|
125
125
|
- `cenc.raw.float64array` - Encodes a float64array without a length prefixed.
|
|
126
|
+
- `cenc.bitarray` - Encodes an array of bits or booleans with its length uint prefixed. Decoding returns array of booleans.
|
|
126
127
|
- `cenc.bool` - Encodes a boolean as 1 or 0.
|
|
127
128
|
- `cenc.string`, `cenc.utf8` - Encodes a utf-8 string, similar to buffer.
|
|
128
129
|
- `cenc.raw.string`, `cenc.raw.utf8` - Encodes a utf-8 string without a length prefixed.
|
|
@@ -139,6 +140,9 @@ to build others on top. Feel free to PR more that are missing.
|
|
|
139
140
|
- `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
|
|
140
141
|
- `cenc.raw.utf16le`, `cenc.raw.ucs2` - Encodes a utf16le string without a length prefixed.
|
|
141
142
|
- `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
|
|
143
|
+
- `cenc.fixed8` - Encodes a fixed 8 byte buffer.
|
|
144
|
+
- `cenc.fixed16` - Encodes a fixed 16 byte buffer.
|
|
145
|
+
- `cenc.fixed24` - Encodes a fixed 24 byte buffer.
|
|
142
146
|
- `cenc.fixed32` - Encodes a fixed 32 byte buffer.
|
|
143
147
|
- `cenc.fixed64` - Encodes a fixed 64 byte buffer.
|
|
144
148
|
- `cenc.fixed(n)` - Makes a fixed sized encoder.
|
package/endian.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -87,6 +87,7 @@ export const buffer: Encoder<Uint8Array>
|
|
|
87
87
|
export const optionalBuffer: Encoder<Uint8Array | null>
|
|
88
88
|
export const binary: Encoder<string | Uint8Array, Uint8Array>
|
|
89
89
|
export const arraybuffer: Encoder<ArrayBuffer>
|
|
90
|
+
export const bitarray: Encoder<Array<0 | 1 | boolean>, boolean[]>
|
|
90
91
|
|
|
91
92
|
export const uint8array: Encoder<Uint8Array>
|
|
92
93
|
export const uint16array: Encoder<Uint16Array>
|
|
@@ -117,6 +118,9 @@ export const utf16le: StringEncoder<string>
|
|
|
117
118
|
export const bool: Encoder<boolean>
|
|
118
119
|
|
|
119
120
|
export function fixed(n: number): Encoder<Uint8Array>
|
|
121
|
+
export const fixed8: Encoder<Uint8Array>
|
|
122
|
+
export const fixed16: Encoder<Uint8Array>
|
|
123
|
+
export const fixed24: Encoder<Uint8Array>
|
|
120
124
|
export const fixed32: Encoder<Uint8Array>
|
|
121
125
|
export const fixed64: Encoder<Uint8Array>
|
|
122
126
|
|
|
@@ -180,9 +184,7 @@ export function from(name: 'ascii'): Raw['ascii']
|
|
|
180
184
|
export function from(name: 'utf-8' | 'utf8'): Raw['utf8']
|
|
181
185
|
export function from(name: 'hex'): Raw['hex']
|
|
182
186
|
export function from(name: 'base64'): Raw['base64']
|
|
183
|
-
export function from(
|
|
184
|
-
name: 'utf16-le' | 'utf16le' | 'ucs-2' | 'ucs2'
|
|
185
|
-
): Raw['ucs2']
|
|
187
|
+
export function from(name: 'utf16-le' | 'utf16le' | 'ucs-2' | 'ucs2'): Raw['ucs2']
|
|
186
188
|
export function from(name: 'ndjson'): Raw['ndjson']
|
|
187
189
|
export function from(name: 'json'): Raw['json']
|
|
188
190
|
export function from(name: 'binary' | string): Raw['binary']
|
package/index.js
CHANGED
|
@@ -176,9 +176,7 @@ const uint56 = (exports.uint56 = {
|
|
|
176
176
|
},
|
|
177
177
|
decode(state) {
|
|
178
178
|
if (state.end - state.start < 7) throw new Error('Out of bounds')
|
|
179
|
-
return validateSafeUint(
|
|
180
|
-
uint24.decode(state) + 0x1000000 * uint32.decode(state)
|
|
181
|
-
)
|
|
179
|
+
return validateSafeUint(uint24.decode(state) + 0x1000000 * uint32.decode(state))
|
|
182
180
|
}
|
|
183
181
|
})
|
|
184
182
|
|
|
@@ -194,9 +192,7 @@ const uint64 = (exports.uint64 = {
|
|
|
194
192
|
},
|
|
195
193
|
decode(state) {
|
|
196
194
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
197
|
-
return validateSafeUint(
|
|
198
|
-
uint32.decode(state) + 0x100000000 * uint32.decode(state)
|
|
199
|
-
)
|
|
195
|
+
return validateSafeUint(uint32.decode(state) + 0x100000000 * uint32.decode(state))
|
|
200
196
|
}
|
|
201
197
|
})
|
|
202
198
|
|
|
@@ -212,9 +208,7 @@ exports.uint64be = {
|
|
|
212
208
|
},
|
|
213
209
|
decode(state) {
|
|
214
210
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
215
|
-
return validateSafeUint(
|
|
216
|
-
0x100000000 * uint32be.decode(state) + uint32be.decode(state)
|
|
217
|
-
)
|
|
211
|
+
return validateSafeUint(0x100000000 * uint32be.decode(state) + uint32be.decode(state))
|
|
218
212
|
}
|
|
219
213
|
}
|
|
220
214
|
|
|
@@ -233,21 +227,13 @@ const biguint64 = (exports.biguint64 = {
|
|
|
233
227
|
state.end += 8
|
|
234
228
|
},
|
|
235
229
|
encode(state, n) {
|
|
236
|
-
const view = new DataView(
|
|
237
|
-
state.buffer.buffer,
|
|
238
|
-
state.start + state.buffer.byteOffset,
|
|
239
|
-
8
|
|
240
|
-
)
|
|
230
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
|
|
241
231
|
view.setBigUint64(0, n, true) // little endian
|
|
242
232
|
state.start += 8
|
|
243
233
|
},
|
|
244
234
|
decode(state) {
|
|
245
235
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
246
|
-
const view = new DataView(
|
|
247
|
-
state.buffer.buffer,
|
|
248
|
-
state.start + state.buffer.byteOffset,
|
|
249
|
-
8
|
|
250
|
-
)
|
|
236
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
|
|
251
237
|
const n = view.getBigUint64(0, true) // little endian
|
|
252
238
|
state.start += 8
|
|
253
239
|
return n
|
|
@@ -267,11 +253,7 @@ const biguint = (exports.biguint = {
|
|
|
267
253
|
let len = 0
|
|
268
254
|
for (let m = n; m; m = m >> 64n) len++
|
|
269
255
|
uint.encode(state, len)
|
|
270
|
-
const view = new DataView(
|
|
271
|
-
state.buffer.buffer,
|
|
272
|
-
state.start + state.buffer.byteOffset,
|
|
273
|
-
8 * len
|
|
274
|
-
)
|
|
256
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8 * len)
|
|
275
257
|
for (let m = n, i = 0; m; m = m >> 64n, i += 8) {
|
|
276
258
|
view.setBigUint64(i, BigInt.asUintN(64, m), true) // little endian
|
|
277
259
|
}
|
|
@@ -280,14 +262,9 @@ const biguint = (exports.biguint = {
|
|
|
280
262
|
decode(state) {
|
|
281
263
|
const len = uint.decode(state)
|
|
282
264
|
if (state.end - state.start < 8 * len) throw new Error('Out of bounds')
|
|
283
|
-
const view = new DataView(
|
|
284
|
-
state.buffer.buffer,
|
|
285
|
-
state.start + state.buffer.byteOffset,
|
|
286
|
-
8 * len
|
|
287
|
-
)
|
|
265
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8 * len)
|
|
288
266
|
let n = 0n
|
|
289
|
-
for (let i = len - 1; i >= 0; i--)
|
|
290
|
-
n = (n << 64n) + view.getBigUint64(i * 8, true) // little endian
|
|
267
|
+
for (let i = len - 1; i >= 0; i--) n = (n << 64n) + view.getBigUint64(i * 8, true) // little endian
|
|
291
268
|
state.start += 8 * len
|
|
292
269
|
return n
|
|
293
270
|
}
|
|
@@ -302,21 +279,13 @@ exports.float32 = {
|
|
|
302
279
|
state.end += 4
|
|
303
280
|
},
|
|
304
281
|
encode(state, n) {
|
|
305
|
-
const view = new DataView(
|
|
306
|
-
state.buffer.buffer,
|
|
307
|
-
state.start + state.buffer.byteOffset,
|
|
308
|
-
4
|
|
309
|
-
)
|
|
282
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 4)
|
|
310
283
|
view.setFloat32(0, n, true) // little endian
|
|
311
284
|
state.start += 4
|
|
312
285
|
},
|
|
313
286
|
decode(state) {
|
|
314
287
|
if (state.end - state.start < 4) throw new Error('Out of bounds')
|
|
315
|
-
const view = new DataView(
|
|
316
|
-
state.buffer.buffer,
|
|
317
|
-
state.start + state.buffer.byteOffset,
|
|
318
|
-
4
|
|
319
|
-
)
|
|
288
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 4)
|
|
320
289
|
const float = view.getFloat32(0, true) // little endian
|
|
321
290
|
state.start += 4
|
|
322
291
|
return float
|
|
@@ -328,21 +297,13 @@ exports.float64 = {
|
|
|
328
297
|
state.end += 8
|
|
329
298
|
},
|
|
330
299
|
encode(state, n) {
|
|
331
|
-
const view = new DataView(
|
|
332
|
-
state.buffer.buffer,
|
|
333
|
-
state.start + state.buffer.byteOffset,
|
|
334
|
-
8
|
|
335
|
-
)
|
|
300
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
|
|
336
301
|
view.setFloat64(0, n, true) // little endian
|
|
337
302
|
state.start += 8
|
|
338
303
|
},
|
|
339
304
|
decode(state) {
|
|
340
305
|
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
341
|
-
const view = new DataView(
|
|
342
|
-
state.buffer.buffer,
|
|
343
|
-
state.start + state.buffer.byteOffset,
|
|
344
|
-
8
|
|
345
|
-
)
|
|
306
|
+
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
|
|
346
307
|
const float = view.getFloat64(0, true) // little endian
|
|
347
308
|
state.start += 8
|
|
348
309
|
return float
|
|
@@ -417,6 +378,35 @@ exports.arraybuffer = {
|
|
|
417
378
|
}
|
|
418
379
|
}
|
|
419
380
|
|
|
381
|
+
exports.bitarray = {
|
|
382
|
+
preencode(state, m) {
|
|
383
|
+
uint.preencode(state, m.length)
|
|
384
|
+
state.end += Math.ceil(m.length / 8)
|
|
385
|
+
},
|
|
386
|
+
encode(state, m) {
|
|
387
|
+
uint.encode(state, m.length)
|
|
388
|
+
for (let i = 0; i < m.length; i += 8) {
|
|
389
|
+
let byte = 0
|
|
390
|
+
for (let j = 0; j < 8 && i + j < m.length; j++) {
|
|
391
|
+
if (m[i + j]) byte |= 1 << j
|
|
392
|
+
}
|
|
393
|
+
state.buffer[state.start++] = byte
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
decode(state) {
|
|
397
|
+
const n = uint.decode(state)
|
|
398
|
+
if (state.end - state.start < Math.ceil(n / 8)) throw new Error('Out of bounds')
|
|
399
|
+
const m = new Array(n)
|
|
400
|
+
for (let i = 0; i < n; i += 8) {
|
|
401
|
+
const byte = state.buffer[state.start++]
|
|
402
|
+
for (let j = 0; j < 8 && i + j < n; j++) {
|
|
403
|
+
m[i + j] = (byte & (1 << j)) !== 0
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return m
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
420
410
|
function typedarray(TypedArray, swap) {
|
|
421
411
|
const n = TypedArray.BYTES_PER_ELEMENT
|
|
422
412
|
|
|
@@ -479,12 +469,7 @@ function string(encoding) {
|
|
|
479
469
|
decode(state) {
|
|
480
470
|
const len = uint.decode(state)
|
|
481
471
|
if (state.end - state.start < len) throw new Error('Out of bounds')
|
|
482
|
-
return b4a.toString(
|
|
483
|
-
state.buffer,
|
|
484
|
-
encoding,
|
|
485
|
-
state.start,
|
|
486
|
-
(state.start += len)
|
|
487
|
-
)
|
|
472
|
+
return b4a.toString(state.buffer, encoding, state.start, (state.start += len))
|
|
488
473
|
},
|
|
489
474
|
fixed(n) {
|
|
490
475
|
return {
|
|
@@ -497,12 +482,7 @@ function string(encoding) {
|
|
|
497
482
|
},
|
|
498
483
|
decode(state) {
|
|
499
484
|
if (state.end - state.start < n) throw new Error('Out of bounds')
|
|
500
|
-
return b4a.toString(
|
|
501
|
-
state.buffer,
|
|
502
|
-
encoding,
|
|
503
|
-
state.start,
|
|
504
|
-
(state.start += n)
|
|
505
|
-
)
|
|
485
|
+
return b4a.toString(state.buffer, encoding, state.start, (state.start += n))
|
|
506
486
|
}
|
|
507
487
|
}
|
|
508
488
|
}
|
|
@@ -545,6 +525,9 @@ const fixed = (exports.fixed = function fixed(n) {
|
|
|
545
525
|
}
|
|
546
526
|
})
|
|
547
527
|
|
|
528
|
+
exports.fixed8 = fixed(8)
|
|
529
|
+
exports.fixed16 = fixed(16)
|
|
530
|
+
exports.fixed24 = fixed(24)
|
|
548
531
|
exports.fixed32 = fixed(32)
|
|
549
532
|
exports.fixed64 = fixed(64)
|
|
550
533
|
|
|
@@ -764,10 +747,7 @@ const ipv4 = (exports.ipv4 = {
|
|
|
764
747
|
let n = 0
|
|
765
748
|
let c
|
|
766
749
|
|
|
767
|
-
while (
|
|
768
|
-
i < string.length &&
|
|
769
|
-
(c = string.charCodeAt(i++)) !== /* . */ 0x2e
|
|
770
|
-
) {
|
|
750
|
+
while (i < string.length && (c = string.charCodeAt(i++)) !== /* . */ 0x2e) {
|
|
771
751
|
n = n * 10 + (c - /* 0 */ 0x30)
|
|
772
752
|
}
|
|
773
753
|
|
|
@@ -807,10 +787,7 @@ const ipv6 = (exports.ipv6 = {
|
|
|
807
787
|
let n = 0
|
|
808
788
|
let c
|
|
809
789
|
|
|
810
|
-
while (
|
|
811
|
-
i < string.length &&
|
|
812
|
-
(c = string.charCodeAt(i++)) !== /* : */ 0x3a
|
|
813
|
-
) {
|
|
790
|
+
while (i < string.length && (c = string.charCodeAt(i++)) !== /* : */ 0x3a) {
|
|
814
791
|
if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30)
|
|
815
792
|
else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10)
|
|
816
793
|
else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
|
|
@@ -827,9 +804,7 @@ const ipv6 = (exports.ipv6 = {
|
|
|
827
804
|
|
|
828
805
|
if (split !== null) {
|
|
829
806
|
const offset = end - state.start
|
|
830
|
-
state.buffer
|
|
831
|
-
.copyWithin(split + offset, split)
|
|
832
|
-
.fill(0, split, split + offset)
|
|
807
|
+
state.buffer.copyWithin(split + offset, split).fill(0, split, split + offset)
|
|
833
808
|
}
|
|
834
809
|
|
|
835
810
|
state.start = end
|
|
@@ -837,45 +812,21 @@ const ipv6 = (exports.ipv6 = {
|
|
|
837
812
|
decode(state) {
|
|
838
813
|
if (state.end - state.start < 16) throw new Error('Out of bounds')
|
|
839
814
|
return (
|
|
840
|
-
(
|
|
841
|
-
state.buffer[state.start++] * 256 +
|
|
842
|
-
state.buffer[state.start++]
|
|
843
|
-
).toString(16) +
|
|
815
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
844
816
|
':' +
|
|
845
|
-
(
|
|
846
|
-
state.buffer[state.start++] * 256 +
|
|
847
|
-
state.buffer[state.start++]
|
|
848
|
-
).toString(16) +
|
|
817
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
849
818
|
':' +
|
|
850
|
-
(
|
|
851
|
-
state.buffer[state.start++] * 256 +
|
|
852
|
-
state.buffer[state.start++]
|
|
853
|
-
).toString(16) +
|
|
819
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
854
820
|
':' +
|
|
855
|
-
(
|
|
856
|
-
state.buffer[state.start++] * 256 +
|
|
857
|
-
state.buffer[state.start++]
|
|
858
|
-
).toString(16) +
|
|
821
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
859
822
|
':' +
|
|
860
|
-
(
|
|
861
|
-
state.buffer[state.start++] * 256 +
|
|
862
|
-
state.buffer[state.start++]
|
|
863
|
-
).toString(16) +
|
|
823
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
864
824
|
':' +
|
|
865
|
-
(
|
|
866
|
-
state.buffer[state.start++] * 256 +
|
|
867
|
-
state.buffer[state.start++]
|
|
868
|
-
).toString(16) +
|
|
825
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
869
826
|
':' +
|
|
870
|
-
(
|
|
871
|
-
state.buffer[state.start++] * 256 +
|
|
872
|
-
state.buffer[state.start++]
|
|
873
|
-
).toString(16) +
|
|
827
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
|
|
874
828
|
':' +
|
|
875
|
-
(
|
|
876
|
-
state.buffer[state.start++] * 256 +
|
|
877
|
-
state.buffer[state.start++]
|
|
878
|
-
).toString(16)
|
|
829
|
+
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16)
|
|
879
830
|
)
|
|
880
831
|
}
|
|
881
832
|
})
|
|
@@ -1120,13 +1071,9 @@ function validateInt(n) {
|
|
|
1120
1071
|
// Kept out here, the message costs nothing until it is actually thrown.
|
|
1121
1072
|
|
|
1122
1073
|
function outsideUintRange() {
|
|
1123
|
-
return new Error(
|
|
1124
|
-
`uint must be between 0 and ${Number.MAX_SAFE_INTEGER}, use biguint`
|
|
1125
|
-
)
|
|
1074
|
+
return new Error(`uint must be between 0 and ${Number.MAX_SAFE_INTEGER}, use biguint`)
|
|
1126
1075
|
}
|
|
1127
1076
|
|
|
1128
1077
|
function outsideIntRange() {
|
|
1129
|
-
return new Error(
|
|
1130
|
-
`int must be between ${MIN_SAFE_INT} and ${MAX_SAFE_INT}, use bigint`
|
|
1131
|
-
)
|
|
1078
|
+
return new Error(`int must be between ${MIN_SAFE_INT} and ${MAX_SAFE_INT}, use bigint`)
|
|
1132
1079
|
}
|
package/lexint.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compact-encoding",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"b4a": "^1.3.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"brittle": "^
|
|
21
|
+
"brittle": "^4.0.0",
|
|
22
22
|
"prettier": "^3.6.2",
|
|
23
|
-
"prettier-config-holepunch": "^
|
|
23
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"format": "prettier . --write",
|
|
27
|
-
"test": "prettier . --check && brittle test.js"
|
|
27
|
+
"test": "prettier . --check && brittle-node test.js"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|