compact-encoding 2.12.0 → 2.13.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/index.js +27 -0
- package/package.json +1 -1
- package/test.js +8 -0
package/index.js
CHANGED
|
@@ -378,6 +378,33 @@ exports.array = function array (enc) {
|
|
|
378
378
|
}
|
|
379
379
|
}
|
|
380
380
|
|
|
381
|
+
exports.frame = function frame (enc) {
|
|
382
|
+
const dummy = exports.state()
|
|
383
|
+
|
|
384
|
+
return {
|
|
385
|
+
preencode (state, m) {
|
|
386
|
+
const end = state.end
|
|
387
|
+
enc.preencode(state, m)
|
|
388
|
+
uint.preencode(state, state.end - end)
|
|
389
|
+
},
|
|
390
|
+
encode (state, m) {
|
|
391
|
+
dummy.end = 0
|
|
392
|
+
enc.preencode(dummy, m)
|
|
393
|
+
uint.encode(state, dummy.end)
|
|
394
|
+
enc.encode(state, m)
|
|
395
|
+
},
|
|
396
|
+
decode (state) {
|
|
397
|
+
const end = state.end
|
|
398
|
+
const len = uint.decode(state)
|
|
399
|
+
state.end = state.start + len
|
|
400
|
+
const m = enc.decode(state)
|
|
401
|
+
state.start = state.end
|
|
402
|
+
state.end = end
|
|
403
|
+
return m
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
381
408
|
exports.json = {
|
|
382
409
|
preencode (state, v) {
|
|
383
410
|
utf8.preencode(state, JSON.stringify(v))
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -671,3 +671,11 @@ test('any', function (t) {
|
|
|
671
671
|
|
|
672
672
|
t.alike(enc.decode(enc.any, enc.encode(enc.any, arr)), [null, null, null])
|
|
673
673
|
})
|
|
674
|
+
|
|
675
|
+
test('framed', function (t) {
|
|
676
|
+
const e = enc.frame(enc.uint)
|
|
677
|
+
t.alike(enc.encode(e, 42), b4a.from([0x01, 0x2a]))
|
|
678
|
+
t.alike(enc.decode(e, b4a.from([0x01, 0x2a])), 42)
|
|
679
|
+
t.alike(enc.encode(e, 4200), b4a.from([0x03, 0xfd, 0x68, 0x10]))
|
|
680
|
+
t.alike(enc.decode(e, b4a.from([0x03, 0xfd, 0x68, 0x10])), 4200)
|
|
681
|
+
})
|