compact-encoding 2.13.0 → 2.14.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
@@ -99,6 +99,8 @@ to build others on top. Feel free to PR more that are missing.
99
99
  * `cenc.float64` - Encodes a fixed size float64.
100
100
  * `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
101
101
  * `cenc.raw.buffer` - Encodes a buffer without a length prefixed.
102
+ * `cenc.arraybuffer` - Encodes an arraybuffer with its length uint prefixed.
103
+ * `cenc.raw.arraybuffer` - Encodes an arraybuffer without a length prefixed.
102
104
  * `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
103
105
  * `cenc.raw.uint8array` - Encodes a uint8array without a length prefixed.
104
106
  * `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
package/index.js CHANGED
@@ -241,6 +241,31 @@ exports.binary = {
241
241
  }
242
242
  }
243
243
 
244
+ exports.arraybuffer = {
245
+ preencode (state, b) {
246
+ uint.preencode(state, b.byteLength)
247
+ state.end += b.byteLength
248
+ },
249
+ encode (state, b) {
250
+ uint.encode(state, b.byteLength)
251
+
252
+ const view = new Uint8Array(b)
253
+
254
+ state.buffer.set(view, state.start)
255
+ state.start += b.byteLength
256
+ },
257
+ decode (state) {
258
+ const len = uint.decode(state)
259
+
260
+ const b = new ArrayBuffer(len)
261
+ const view = new Uint8Array(b)
262
+
263
+ view.set(state.buffer.subarray(state.start, state.start += len))
264
+
265
+ return b
266
+ }
267
+ }
268
+
244
269
  function typedarray (TypedArray, swap) {
245
270
  const n = TypedArray.BYTES_PER_ELEMENT
246
271
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.13.0",
3
+ "version": "2.14.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
@@ -46,6 +46,28 @@ exports.binary = {
46
46
  }
47
47
  }
48
48
 
49
+ exports.arraybuffer = {
50
+ preencode (state, b) {
51
+ state.end += b.byteLength
52
+ },
53
+ encode (state, b) {
54
+ const view = new Uint8Array(b)
55
+
56
+ state.buffer.set(view, state.start)
57
+ state.start += b.byteLength
58
+ },
59
+ decode (state) {
60
+ const b = new ArrayBuffer(state.end - state.start)
61
+ const view = new Uint8Array(b)
62
+
63
+ view.set(state.buffer.subarray(state.start))
64
+
65
+ state.start = state.end
66
+
67
+ return b
68
+ }
69
+ }
70
+
49
71
  function typedarray (TypedArray, swap) {
50
72
  const n = TypedArray.BYTES_PER_ELEMENT
51
73
 
package/test.js CHANGED
@@ -173,6 +173,34 @@ test('buffer', function (t) {
173
173
  t.exception(() => enc.buffer.decode(state))
174
174
  })
175
175
 
176
+ test('arraybuffer', function (t) {
177
+ const state = enc.state()
178
+
179
+ const b1 = new ArrayBuffer(4)
180
+ b4a.from(b1).fill('a')
181
+
182
+ const b2 = new ArrayBuffer(8)
183
+ b4a.from(b2).fill('b')
184
+
185
+ enc.arraybuffer.preencode(state, b1)
186
+ t.alike(state, enc.state(0, 5))
187
+ enc.arraybuffer.preencode(state, b2)
188
+ t.alike(state, enc.state(0, 14))
189
+
190
+ state.buffer = b4a.alloc(state.end)
191
+ enc.arraybuffer.encode(state, b1)
192
+ t.alike(state, enc.state(5, 14, b4a.from('\x04aaaa\x00\x00\x00\x00\x00\x00\x00\x00\x00')))
193
+ enc.arraybuffer.encode(state, b2)
194
+ t.alike(state, enc.state(14, 14, b4a.from('\x04aaaa\x08bbbbbbbb')))
195
+
196
+ state.start = 0
197
+ t.alike(enc.arraybuffer.decode(state), b1)
198
+ t.alike(enc.arraybuffer.decode(state), b2)
199
+ t.is(state.start, state.end)
200
+
201
+ t.exception(() => enc.arraybuffer.decode(state))
202
+ })
203
+
176
204
  test('raw', function (t) {
177
205
  const state = enc.state()
178
206