compact-encoding 2.19.2 → 3.0.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.
@@ -0,0 +1,17 @@
1
+ name: Publish
2
+ on:
3
+ push:
4
+ tags:
5
+ - v*
6
+ permissions:
7
+ id-token: write
8
+ contents: read
9
+ jobs:
10
+ publish:
11
+ runs-on: ubuntu-latest
12
+ environment:
13
+ name: npm
14
+ name: Publish
15
+ steps:
16
+ - uses: holepunchto/actions/node-base@v1
17
+ - uses: holepunchto/actions/publish@v1
package/index.js CHANGED
@@ -301,6 +301,20 @@ exports.float64 = {
301
301
  }
302
302
 
303
303
  const buffer = (exports.buffer = {
304
+ preencode(state, b) {
305
+ uint8array.preencode(state, b)
306
+ },
307
+ encode(state, b) {
308
+ uint8array.encode(state, b)
309
+ },
310
+ decode(state) {
311
+ const len = uint.decode(state)
312
+ if (state.end - state.start < len) throw new Error('Out of bounds')
313
+ return state.buffer.subarray(state.start, (state.start += len))
314
+ }
315
+ })
316
+
317
+ exports.optionalBuffer = {
304
318
  preencode(state, b) {
305
319
  if (b) uint8array.preencode(state, b)
306
320
  else state.end++
@@ -315,7 +329,7 @@ const buffer = (exports.buffer = {
315
329
  if (state.end - state.start < len) throw new Error('Out of bounds')
316
330
  return state.buffer.subarray(state.start, (state.start += len))
317
331
  }
318
- })
332
+ }
319
333
 
320
334
  exports.binary = {
321
335
  ...buffer,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.19.2",
3
+ "version": "3.0.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
  "dependencies": {
package/raw.js CHANGED
@@ -19,16 +19,13 @@ exports = module.exports = {
19
19
 
20
20
  const buffer = (exports.buffer = {
21
21
  preencode(state, b) {
22
- if (b) uint8array.preencode(state, b)
23
- else state.end++
22
+ uint8array.preencode(state, b)
24
23
  },
25
24
  encode(state, b) {
26
- if (b) uint8array.encode(state, b)
27
- else state.buffer[state.start++] = 0
25
+ uint8array.encode(state, b)
28
26
  },
29
27
  decode(state) {
30
28
  const b = state.buffer.subarray(state.start)
31
- if (b.byteLength === 0) return null
32
29
  state.start = state.end
33
30
  return b
34
31
  }
package/test.js CHANGED
@@ -288,7 +288,7 @@ test('buffer', function (t) {
288
288
  t.alike(state, enc.state(0, 3))
289
289
  enc.buffer.preencode(state, b4a.from('hello'))
290
290
  t.alike(state, enc.state(0, 9))
291
- enc.buffer.preencode(state, null)
291
+ enc.buffer.preencode(state, b4a.alloc(0))
292
292
  t.alike(state, enc.state(0, 10))
293
293
 
294
294
  state.buffer = b4a.alloc(state.end)
@@ -299,18 +299,52 @@ test('buffer', function (t) {
299
299
  )
300
300
  enc.buffer.encode(state, b4a.from('hello'))
301
301
  t.alike(state, enc.state(9, 10, b4a.from('\x02hi\x05hello\x00')))
302
- enc.buffer.encode(state, null)
302
+ enc.buffer.encode(state, b4a.alloc(0))
303
303
  t.alike(state, enc.state(10, 10, b4a.from('\x02hi\x05hello\x00')))
304
304
 
305
305
  state.start = 0
306
306
  t.alike(enc.buffer.decode(state), b4a.from('hi'))
307
307
  t.alike(enc.buffer.decode(state), b4a.from('hello'))
308
- t.is(enc.buffer.decode(state), null)
308
+ t.alike(enc.buffer.decode(state), b4a.alloc(0))
309
309
  t.is(state.start, state.end)
310
310
 
311
311
  t.exception(() => enc.buffer.decode(state))
312
312
  })
313
313
 
314
+ test('optionalBuffer', function (t) {
315
+ const state = enc.state()
316
+
317
+ enc.optionalBuffer.preencode(state, b4a.from('hi'))
318
+ t.alike(state, enc.state(0, 3))
319
+ enc.optionalBuffer.preencode(state, b4a.from('hello'))
320
+ t.alike(state, enc.state(0, 9))
321
+ enc.optionalBuffer.preencode(state, null)
322
+ enc.optionalBuffer.preencode(state, b4a.alloc(0))
323
+ t.alike(state, enc.state(0, 11))
324
+
325
+ state.buffer = b4a.alloc(state.end)
326
+ enc.optionalBuffer.encode(state, b4a.from('hi'))
327
+ t.alike(
328
+ state,
329
+ enc.state(3, 11, b4a.from('\x02hi\x00\x00\x00\x00\x00\x00\x00\x00'))
330
+ )
331
+ enc.optionalBuffer.encode(state, b4a.from('hello'))
332
+ t.alike(state, enc.state(9, 11, b4a.from('\x02hi\x05hello\x00\x00')))
333
+ enc.optionalBuffer.encode(state, null)
334
+ t.alike(state, enc.state(10, 11, b4a.from('\x02hi\x05hello\x00\x00')))
335
+ enc.optionalBuffer.encode(state, b4a.alloc(0))
336
+ t.alike(state, enc.state(11, 11, b4a.from('\x02hi\x05hello\x00\x00')))
337
+
338
+ state.start = 0
339
+ t.alike(enc.optionalBuffer.decode(state), b4a.from('hi'))
340
+ t.alike(enc.optionalBuffer.decode(state), b4a.from('hello'))
341
+ t.is(enc.optionalBuffer.decode(state), null)
342
+ t.is(enc.optionalBuffer.decode(state), null, 'empty buffer decodes as null')
343
+ t.is(state.start, state.end)
344
+
345
+ t.exception(() => enc.optionalBuffer.decode(state))
346
+ })
347
+
314
348
  test('arraybuffer', function (t) {
315
349
  const state = enc.state()
316
350
 
@@ -624,6 +658,24 @@ test('raw string', function (t) {
624
658
  t.is(enc.raw.string.decode(state), '')
625
659
  })
626
660
 
661
+ test('raw buffer', function (t) {
662
+ const state = enc.state()
663
+
664
+ enc.raw.string.preencode(state, b4a.from('hello'))
665
+ t.alike(state, enc.state(0, 5))
666
+ enc.raw.string.preencode(state, b4a.from(' world'))
667
+ t.alike(state, enc.state(0, 11))
668
+
669
+ state.buffer = b4a.alloc(state.end)
670
+ enc.raw.buffer.encode(state, b4a.from('hello'))
671
+ enc.raw.buffer.encode(state, b4a.from(' world'))
672
+ t.alike(state, enc.state(11, 11, b4a.from('hello world')))
673
+
674
+ state.start = 0
675
+ t.alike(enc.raw.buffer.decode(state), b4a.from('hello world'))
676
+ t.alike(enc.raw.buffer.decode(state), b4a.alloc(0))
677
+ })
678
+
627
679
  test('fixed32', function (t) {
628
680
  const state = enc.state()
629
681