compact-encoding 3.3.1 → 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.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export const LE: boolean
2
+ export const BE: boolean
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 ADDED
@@ -0,0 +1,202 @@
1
+ interface State<T extends Uint8Array = Uint8Array> {
2
+ start: number
3
+ end: number
4
+ buffer: T | null
5
+ }
6
+
7
+ export function state<T extends Uint8Array = Uint8Array>(
8
+ start?: number,
9
+ end?: number,
10
+ buffer?: T | null
11
+ ): State<T>
12
+
13
+ interface Encoder<Input = unknown, Output = Input> {
14
+ preencode(state: State, val: Input): void
15
+ encode(state: State, val: Input): void
16
+ decode(state: State): Output
17
+ }
18
+
19
+ interface Raw<T extends Uint8Array = Uint8Array> extends Encoder<T> {
20
+ buffer: Encoder<T>
21
+ binary: Encoder<string | T, T>
22
+ arraybuffer: Encoder<ArrayBuffer>
23
+
24
+ uint8array: Encoder<Uint8Array>
25
+ uint16array: Encoder<Uint16Array>
26
+ uint32array: Encoder<Uint32Array>
27
+
28
+ int8array: Encoder<Int8Array>
29
+ int16array: Encoder<Int16Array>
30
+ int32array: Encoder<Int32Array>
31
+
32
+ biguint64array: Encoder<BigUint64Array>
33
+ bigint64array: Encoder<BigInt64Array>
34
+
35
+ float32array: Encoder<Float32Array>
36
+ float64array: Encoder<Float64Array>
37
+
38
+ string: Encoder<string>
39
+ utf8: Encoder<string>
40
+ ascii: Encoder<string>
41
+ hex: Encoder<string>
42
+ base64: Encoder<string>
43
+ ucs2: Encoder<string>
44
+ utf16le: Encoder<string>
45
+
46
+ array: <I, O = I>(enc: Encoder<I, O>) => Encoder<I[], O[]>
47
+
48
+ json: Encoder<unknown>
49
+ ndjson: Encoder<unknown>
50
+ }
51
+
52
+ export const raw: Raw
53
+
54
+ export const uint: Encoder<number>
55
+ export const uint8: Encoder<number>
56
+ export const uint16: Encoder<number>
57
+ export const uint24: Encoder<number>
58
+ export const uint32: Encoder<number>
59
+ export const uint32be: Encoder<number>
60
+ export const uint40: Encoder<number>
61
+ export const uint48: Encoder<number>
62
+ export const uint56: Encoder<number>
63
+ export const uint64: Encoder<number>
64
+ export const uint64be: Encoder<number>
65
+
66
+ export const int: Encoder<number>
67
+ export const int8: Encoder<number>
68
+ export const int16: Encoder<number>
69
+ export const int24: Encoder<number>
70
+ export const int32: Encoder<number>
71
+ export const int40: Encoder<number>
72
+ export const int48: Encoder<number>
73
+ export const int56: Encoder<number>
74
+ export const int64: Encoder<number>
75
+
76
+ export const biguint64: Encoder<bigint>
77
+ export const bigint64: Encoder<bigint>
78
+ export const biguint: Encoder<bigint>
79
+ export const bigint: Encoder<bigint>
80
+
81
+ export const lexint: Encoder<number>
82
+
83
+ export const float32: Encoder<number>
84
+ export const float64: Encoder<number>
85
+
86
+ export const buffer: Encoder<Uint8Array>
87
+ export const optionalBuffer: Encoder<Uint8Array | null>
88
+ export const binary: Encoder<string | Uint8Array, Uint8Array>
89
+ export const arraybuffer: Encoder<ArrayBuffer>
90
+ export const bitarray: Encoder<Array<0 | 1 | boolean>, boolean[]>
91
+
92
+ export const uint8array: Encoder<Uint8Array>
93
+ export const uint16array: Encoder<Uint16Array>
94
+ export const uint32array: Encoder<Uint32Array>
95
+
96
+ export const int8array: Encoder<Int8Array>
97
+ export const int16array: Encoder<Int16Array>
98
+ export const int32array: Encoder<Int32Array>
99
+
100
+ export const biguint64array: Encoder<BigUint64Array>
101
+ export const bigint64array: Encoder<BigInt64Array>
102
+
103
+ export const float32array: Encoder<Float32Array>
104
+ export const float64array: Encoder<Float64Array>
105
+
106
+ interface StringEncoder<T = unknown> extends Encoder<T> {
107
+ fixed(n: number): Encoder<T>
108
+ }
109
+
110
+ export const string: StringEncoder<string>
111
+ export const utf8: StringEncoder<string>
112
+ export const ascii: StringEncoder<string>
113
+ export const hex: StringEncoder<string>
114
+ export const base64: StringEncoder<string>
115
+ export const ucs2: StringEncoder<string>
116
+ export const utf16le: StringEncoder<string>
117
+
118
+ export const bool: Encoder<boolean>
119
+
120
+ export function fixed(n: number): Encoder<Uint8Array>
121
+ export const fixed32: Encoder<Uint8Array>
122
+ export const fixed64: Encoder<Uint8Array>
123
+
124
+ export function array<I, O>(enc: Encoder<I, O>): Encoder<I[], O[]>
125
+
126
+ export function frame<I, O>(enc: Encoder<I, O>): Encoder<I, O>
127
+
128
+ export const date: Encoder<Date>
129
+
130
+ export const json: Encoder<unknown>
131
+ export const ndjson: Encoder<unknown>
132
+ export const none: Encoder<unknown, null>
133
+ export const any: Encoder<unknown>
134
+
135
+ interface AddressInput {
136
+ host: string
137
+ port: number
138
+ }
139
+
140
+ interface Address<F extends 4 | 6 = 4 | 6> extends AddressInput {
141
+ family: F
142
+ }
143
+
144
+ export const port: Encoder<number>
145
+ export const ipv4: Encoder<string>
146
+ export const ipv4Address: Encoder<AddressInput, Address<4>>
147
+ export const ipv6: Encoder<string>
148
+ export const ipv6Address: Encoder<AddressInput, Address<6>>
149
+ export const ip: Encoder<string>
150
+ export const ipAddress: Encoder<AddressInput, Address>
151
+
152
+ export function record<I, O = I>(
153
+ keyEncoding: Encoder<string>,
154
+ valueEncoder: Encoder<I, O>
155
+ ): Encoder<Record<string, I>, Record<string, O>>
156
+
157
+ export const stringRecord: Encoder<Record<string, string>>
158
+
159
+ interface CodecLike<Input, Output = Input> {
160
+ encode(value: Input): Uint8Array
161
+ decode(buffer: Uint8Array): Output
162
+ }
163
+
164
+ interface AbstractEncode<Input> {
165
+ (value: Input, buffer: Uint8Array, offset: number): unknown
166
+ bytes: number
167
+ }
168
+
169
+ interface AbstractDecode<Output> {
170
+ (buffer: Uint8Array, start: number, end: number): Output
171
+ bytes: number
172
+ }
173
+
174
+ interface AbstractEncodingLike<Input, Output = Input> {
175
+ encodingLength(value: Input): number
176
+ encode: AbstractEncode<Input>
177
+ decode: AbstractDecode<Output>
178
+ }
179
+
180
+ export function from(name: 'ascii'): Raw['ascii']
181
+ export function from(name: 'utf-8' | 'utf8'): Raw['utf8']
182
+ export function from(name: 'hex'): Raw['hex']
183
+ export function from(name: 'base64'): Raw['base64']
184
+ export function from(name: 'utf16-le' | 'utf16le' | 'ucs-2' | 'ucs2'): Raw['ucs2']
185
+ export function from(name: 'ndjson'): Raw['ndjson']
186
+ export function from(name: 'json'): Raw['json']
187
+ export function from(name: 'binary' | string): Raw['binary']
188
+ export function from<I, O>(enc: Encoder<I, O>): Encoder<I, O>
189
+ export function from<I, O>(enc: CodecLike<I, O>): Encoder<I, O>
190
+ export function from<I, O>(enc: AbstractEncodingLike<I, O>): Encoder<I, O>
191
+
192
+ export function encode<Input = unknown, Output = Input>(
193
+ enc: Encoder<Input, Output>,
194
+ m: Input
195
+ ): Uint8Array
196
+
197
+ export function decode<Input = unknown, Output = Input>(
198
+ enc: Encoder<Input, Output>,
199
+ buffer: Uint8Array
200
+ ): Output
201
+
202
+ export type { State, Encoder, Raw, StringEncoder, AddressInput, Address }
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.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { lexint } from './index'
2
+
3
+ export = lexint
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,25 +1,30 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "3.3.1",
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
+ "types": "index.d.ts",
6
7
  "files": [
7
8
  "endian.js",
9
+ "endian.d.ts",
8
10
  "index.js",
11
+ "index.d.ts",
9
12
  "lexint.js",
10
- "raw.js"
13
+ "lexint.d.ts",
14
+ "raw.js",
15
+ "raw.d.ts"
11
16
  ],
12
17
  "dependencies": {
13
18
  "b4a": "^1.3.0"
14
19
  },
15
20
  "devDependencies": {
16
- "brittle": "^3.0.0",
21
+ "brittle": "^4.0.0",
17
22
  "prettier": "^3.6.2",
18
- "prettier-config-holepunch": "^1.0.0"
23
+ "prettier-config-holepunch": "^2.0.0"
19
24
  },
20
25
  "scripts": {
21
26
  "format": "prettier . --write",
22
- "test": "prettier . --check && brittle test.js"
27
+ "test": "prettier . --check && brittle-node test.js"
23
28
  },
24
29
  "repository": {
25
30
  "type": "git",
package/raw.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Raw } from './index'
2
+
3
+ declare const raw: Raw
4
+ export = raw