compact-encoding 3.3.2 → 3.4.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 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.
package/endian.js CHANGED
@@ -1,4 +1,3 @@
1
- const LE = (exports.LE =
2
- new Uint8Array(new Uint16Array([0xff]).buffer)[0] === 0xff)
1
+ const LE = (exports.LE = new Uint8Array(new Uint16Array([0xff]).buffer)[0] === 0xff)
3
2
 
4
3
  exports.BE = !LE
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>
@@ -180,9 +181,7 @@ export function from(name: 'ascii'): Raw['ascii']
180
181
  export function from(name: 'utf-8' | 'utf8'): Raw['utf8']
181
182
  export function from(name: 'hex'): Raw['hex']
182
183
  export function from(name: 'base64'): Raw['base64']
183
- export function from(
184
- name: 'utf16-le' | 'utf16le' | 'ucs-2' | 'ucs2'
185
- ): Raw['ucs2']
184
+ export function from(name: 'utf16-le' | 'utf16le' | 'ucs-2' | 'ucs2'): Raw['ucs2']
186
185
  export function from(name: 'ndjson'): Raw['ndjson']
187
186
  export function from(name: 'json'): Raw['json']
188
187
  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
  }
@@ -764,10 +744,7 @@ const ipv4 = (exports.ipv4 = {
764
744
  let n = 0
765
745
  let c
766
746
 
767
- while (
768
- i < string.length &&
769
- (c = string.charCodeAt(i++)) !== /* . */ 0x2e
770
- ) {
747
+ while (i < string.length && (c = string.charCodeAt(i++)) !== /* . */ 0x2e) {
771
748
  n = n * 10 + (c - /* 0 */ 0x30)
772
749
  }
773
750
 
@@ -807,10 +784,7 @@ const ipv6 = (exports.ipv6 = {
807
784
  let n = 0
808
785
  let c
809
786
 
810
- while (
811
- i < string.length &&
812
- (c = string.charCodeAt(i++)) !== /* : */ 0x3a
813
- ) {
787
+ while (i < string.length && (c = string.charCodeAt(i++)) !== /* : */ 0x3a) {
814
788
  if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30)
815
789
  else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10)
816
790
  else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
@@ -827,9 +801,7 @@ const ipv6 = (exports.ipv6 = {
827
801
 
828
802
  if (split !== null) {
829
803
  const offset = end - state.start
830
- state.buffer
831
- .copyWithin(split + offset, split)
832
- .fill(0, split, split + offset)
804
+ state.buffer.copyWithin(split + offset, split).fill(0, split, split + offset)
833
805
  }
834
806
 
835
807
  state.start = end
@@ -837,45 +809,21 @@ const ipv6 = (exports.ipv6 = {
837
809
  decode(state) {
838
810
  if (state.end - state.start < 16) throw new Error('Out of bounds')
839
811
  return (
840
- (
841
- state.buffer[state.start++] * 256 +
842
- state.buffer[state.start++]
843
- ).toString(16) +
812
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
844
813
  ':' +
845
- (
846
- state.buffer[state.start++] * 256 +
847
- state.buffer[state.start++]
848
- ).toString(16) +
814
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
849
815
  ':' +
850
- (
851
- state.buffer[state.start++] * 256 +
852
- state.buffer[state.start++]
853
- ).toString(16) +
816
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
854
817
  ':' +
855
- (
856
- state.buffer[state.start++] * 256 +
857
- state.buffer[state.start++]
858
- ).toString(16) +
818
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
859
819
  ':' +
860
- (
861
- state.buffer[state.start++] * 256 +
862
- state.buffer[state.start++]
863
- ).toString(16) +
820
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
864
821
  ':' +
865
- (
866
- state.buffer[state.start++] * 256 +
867
- state.buffer[state.start++]
868
- ).toString(16) +
822
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
869
823
  ':' +
870
- (
871
- state.buffer[state.start++] * 256 +
872
- state.buffer[state.start++]
873
- ).toString(16) +
824
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) +
874
825
  ':' +
875
- (
876
- state.buffer[state.start++] * 256 +
877
- state.buffer[state.start++]
878
- ).toString(16)
826
+ (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16)
879
827
  )
880
828
  }
881
829
  })
@@ -1120,13 +1068,9 @@ function validateInt(n) {
1120
1068
  // Kept out here, the message costs nothing until it is actually thrown.
1121
1069
 
1122
1070
  function outsideUintRange() {
1123
- return new Error(
1124
- `uint must be between 0 and ${Number.MAX_SAFE_INTEGER}, use biguint`
1125
- )
1071
+ return new Error(`uint must be between 0 and ${Number.MAX_SAFE_INTEGER}, use biguint`)
1126
1072
  }
1127
1073
 
1128
1074
  function outsideIntRange() {
1129
- return new Error(
1130
- `int must be between ${MIN_SAFE_INT} and ${MAX_SAFE_INT}, use bigint`
1131
- )
1075
+ return new Error(`int must be between ${MIN_SAFE_INT} and ${MAX_SAFE_INT}, use bigint`)
1132
1076
  }
package/lexint.js CHANGED
@@ -79,9 +79,7 @@ function decode(state) {
79
79
  }
80
80
 
81
81
  if (flag < 253) {
82
- return (
83
- (state.buffer[state.start++] << 8) + state.buffer[state.start++] + max
84
- )
82
+ return (state.buffer[state.start++] << 8) + state.buffer[state.start++] + max
85
83
  }
86
84
 
87
85
  if (flag < 254) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "3.3.2",
3
+ "version": "3.4.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": "^3.0.0",
21
+ "brittle": "^4.0.0",
22
22
  "prettier": "^3.6.2",
23
- "prettier-config-holepunch": "^1.0.0"
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",