compact-encoding 2.12.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 +2 -0
- package/index.js +52 -0
- package/package.json +1 -1
- package/raw.js +22 -0
- package/test.js +36 -0
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
|
|
|
@@ -378,6 +403,33 @@ exports.array = function array (enc) {
|
|
|
378
403
|
}
|
|
379
404
|
}
|
|
380
405
|
|
|
406
|
+
exports.frame = function frame (enc) {
|
|
407
|
+
const dummy = exports.state()
|
|
408
|
+
|
|
409
|
+
return {
|
|
410
|
+
preencode (state, m) {
|
|
411
|
+
const end = state.end
|
|
412
|
+
enc.preencode(state, m)
|
|
413
|
+
uint.preencode(state, state.end - end)
|
|
414
|
+
},
|
|
415
|
+
encode (state, m) {
|
|
416
|
+
dummy.end = 0
|
|
417
|
+
enc.preencode(dummy, m)
|
|
418
|
+
uint.encode(state, dummy.end)
|
|
419
|
+
enc.encode(state, m)
|
|
420
|
+
},
|
|
421
|
+
decode (state) {
|
|
422
|
+
const end = state.end
|
|
423
|
+
const len = uint.decode(state)
|
|
424
|
+
state.end = state.start + len
|
|
425
|
+
const m = enc.decode(state)
|
|
426
|
+
state.start = state.end
|
|
427
|
+
state.end = end
|
|
428
|
+
return m
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
381
433
|
exports.json = {
|
|
382
434
|
preencode (state, v) {
|
|
383
435
|
utf8.preencode(state, JSON.stringify(v))
|
package/package.json
CHANGED
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
|
|
|
@@ -671,3 +699,11 @@ test('any', function (t) {
|
|
|
671
699
|
|
|
672
700
|
t.alike(enc.decode(enc.any, enc.encode(enc.any, arr)), [null, null, null])
|
|
673
701
|
})
|
|
702
|
+
|
|
703
|
+
test('framed', function (t) {
|
|
704
|
+
const e = enc.frame(enc.uint)
|
|
705
|
+
t.alike(enc.encode(e, 42), b4a.from([0x01, 0x2a]))
|
|
706
|
+
t.alike(enc.decode(e, b4a.from([0x01, 0x2a])), 42)
|
|
707
|
+
t.alike(enc.encode(e, 4200), b4a.from([0x03, 0xfd, 0x68, 0x10]))
|
|
708
|
+
t.alike(enc.decode(e, b4a.from([0x03, 0xfd, 0x68, 0x10])), 4200)
|
|
709
|
+
})
|