compact-encoding 2.15.0 → 2.16.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 +1 -0
- package/index.js +28 -1
- package/package.json +1 -1
- package/test.js +20 -0
package/README.md
CHANGED
|
@@ -143,6 +143,7 @@ to build others on top. Feel free to PR more that are missing.
|
|
|
143
143
|
* `cenc.fixed32` - Encodes a fixed 32 byte buffer.
|
|
144
144
|
* `cenc.fixed64` - Encodes a fixed 64 byte buffer.
|
|
145
145
|
* `cenc.fixed(n)` - Makes a fixed sized encoder.
|
|
146
|
+
* `cenc.date(d)` - Encodes a date object.
|
|
146
147
|
* `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
|
|
147
148
|
* `cenc.raw.array(enc)` - Makes an array encoder from another encoder, without a length prefixed.
|
|
148
149
|
* `cenc.json` - Encodes a JSON value as utf-8.
|
package/index.js
CHANGED
|
@@ -39,6 +39,7 @@ const uint8 = exports.uint8 = {
|
|
|
39
39
|
state.end += 1
|
|
40
40
|
},
|
|
41
41
|
encode (state, n) {
|
|
42
|
+
validateUint(n)
|
|
42
43
|
state.buffer[state.start++] = n
|
|
43
44
|
},
|
|
44
45
|
decode (state) {
|
|
@@ -52,6 +53,7 @@ const uint16 = exports.uint16 = {
|
|
|
52
53
|
state.end += 2
|
|
53
54
|
},
|
|
54
55
|
encode (state, n) {
|
|
56
|
+
validateUint(n)
|
|
55
57
|
state.buffer[state.start++] = n
|
|
56
58
|
state.buffer[state.start++] = n >>> 8
|
|
57
59
|
},
|
|
@@ -69,6 +71,7 @@ const uint24 = exports.uint24 = {
|
|
|
69
71
|
state.end += 3
|
|
70
72
|
},
|
|
71
73
|
encode (state, n) {
|
|
74
|
+
validateUint(n)
|
|
72
75
|
state.buffer[state.start++] = n
|
|
73
76
|
state.buffer[state.start++] = n >>> 8
|
|
74
77
|
state.buffer[state.start++] = n >>> 16
|
|
@@ -88,6 +91,7 @@ const uint32 = exports.uint32 = {
|
|
|
88
91
|
state.end += 4
|
|
89
92
|
},
|
|
90
93
|
encode (state, n) {
|
|
94
|
+
validateUint(n)
|
|
91
95
|
state.buffer[state.start++] = n
|
|
92
96
|
state.buffer[state.start++] = n >>> 8
|
|
93
97
|
state.buffer[state.start++] = n >>> 16
|
|
@@ -109,6 +113,7 @@ const uint40 = exports.uint40 = {
|
|
|
109
113
|
state.end += 5
|
|
110
114
|
},
|
|
111
115
|
encode (state, n) {
|
|
116
|
+
validateUint(n)
|
|
112
117
|
const r = Math.floor(n / 0x100)
|
|
113
118
|
uint8.encode(state, n)
|
|
114
119
|
uint32.encode(state, r)
|
|
@@ -124,6 +129,7 @@ const uint48 = exports.uint48 = {
|
|
|
124
129
|
state.end += 6
|
|
125
130
|
},
|
|
126
131
|
encode (state, n) {
|
|
132
|
+
validateUint(n)
|
|
127
133
|
const r = Math.floor(n / 0x10000)
|
|
128
134
|
uint16.encode(state, n)
|
|
129
135
|
uint32.encode(state, r)
|
|
@@ -139,6 +145,7 @@ const uint56 = exports.uint56 = {
|
|
|
139
145
|
state.end += 7
|
|
140
146
|
},
|
|
141
147
|
encode (state, n) {
|
|
148
|
+
validateUint(n)
|
|
142
149
|
const r = Math.floor(n / 0x1000000)
|
|
143
150
|
uint24.encode(state, n)
|
|
144
151
|
uint32.encode(state, r)
|
|
@@ -154,6 +161,7 @@ const uint64 = exports.uint64 = {
|
|
|
154
161
|
state.end += 8
|
|
155
162
|
},
|
|
156
163
|
encode (state, n) {
|
|
164
|
+
validateUint(n)
|
|
157
165
|
const r = Math.floor(n / 0x100000000)
|
|
158
166
|
uint32.encode(state, n)
|
|
159
167
|
uint32.encode(state, r)
|
|
@@ -420,6 +428,7 @@ exports.bool = {
|
|
|
420
428
|
const fixed = exports.fixed = function fixed (n) {
|
|
421
429
|
return {
|
|
422
430
|
preencode (state, s) {
|
|
431
|
+
if (s.byteLength !== n) throw new Error('Incorrect buffer size')
|
|
423
432
|
state.end += n
|
|
424
433
|
},
|
|
425
434
|
encode (state, s) {
|
|
@@ -483,6 +492,18 @@ exports.frame = function frame (enc) {
|
|
|
483
492
|
}
|
|
484
493
|
}
|
|
485
494
|
|
|
495
|
+
exports.date = {
|
|
496
|
+
preencode (state, d) {
|
|
497
|
+
uint.preencode(state, d.getTime())
|
|
498
|
+
},
|
|
499
|
+
encode (state, d) {
|
|
500
|
+
uint.encode(state, d.getTime())
|
|
501
|
+
},
|
|
502
|
+
decode (state, d) {
|
|
503
|
+
return new Date(uint.decode(state))
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
486
507
|
exports.json = {
|
|
487
508
|
preencode (state, v) {
|
|
488
509
|
utf8.preencode(state, JSON.stringify(v))
|
|
@@ -582,7 +603,8 @@ const anyTypes = [
|
|
|
582
603
|
exports.int,
|
|
583
604
|
exports.float64,
|
|
584
605
|
anyArray,
|
|
585
|
-
anyObject
|
|
606
|
+
anyObject,
|
|
607
|
+
exports.date
|
|
586
608
|
]
|
|
587
609
|
|
|
588
610
|
const any = exports.any = {
|
|
@@ -613,6 +635,7 @@ function getType (o) {
|
|
|
613
635
|
return 6
|
|
614
636
|
}
|
|
615
637
|
if (Array.isArray(o)) return 7
|
|
638
|
+
if (o instanceof Date) return 9
|
|
616
639
|
if (typeof o === 'object') return 8
|
|
617
640
|
|
|
618
641
|
throw new Error('Unsupported type for ' + o)
|
|
@@ -737,3 +760,7 @@ function zigZagEncodeBigInt (n) {
|
|
|
737
760
|
// 0, -1, 1, -2, 2, ...
|
|
738
761
|
return n < 0n ? (2n * -n) - 1n : n === 0n ? 0n : 2n * n
|
|
739
762
|
}
|
|
763
|
+
|
|
764
|
+
function validateUint (n) {
|
|
765
|
+
if ((n >= 0) === false /* Handles NaN as well */) throw new Error('uint must be positive')
|
|
766
|
+
}
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -565,6 +565,19 @@ test('fixed n', function (t) {
|
|
|
565
565
|
t.exception(() => fixed.decode(state))
|
|
566
566
|
})
|
|
567
567
|
|
|
568
|
+
test('error for incorrect buffer sizes when encoding fixed-length buffers', function (t) {
|
|
569
|
+
const smallbuf = b4a.from('aa', 'hex')
|
|
570
|
+
const bigBuf = b4a.from('aa'.repeat(500), 'hex')
|
|
571
|
+
|
|
572
|
+
t.exception(() => enc.encode(enc.fixed32, smallbuf), /Incorrect buffer size/)
|
|
573
|
+
t.exception(() => enc.encode(enc.fixed64, smallbuf), /Incorrect buffer size/)
|
|
574
|
+
t.exception(() => enc.encode(enc.fixed(100), smallbuf), /Incorrect buffer size/)
|
|
575
|
+
|
|
576
|
+
t.exception(() => enc.encode(enc.fixed32, bigBuf), /Incorrect buffer size/)
|
|
577
|
+
t.exception(() => enc.encode(enc.fixed64, bigBuf), /Incorrect buffer size/)
|
|
578
|
+
t.exception(() => enc.encode(enc.fixed(100), bigBuf), /Incorrect buffer size/)
|
|
579
|
+
})
|
|
580
|
+
|
|
568
581
|
test('array', function (t) {
|
|
569
582
|
const state = enc.state()
|
|
570
583
|
const arr = enc.array(enc.bool)
|
|
@@ -797,6 +810,12 @@ test('lexint: unpack', function (t) {
|
|
|
797
810
|
}
|
|
798
811
|
})
|
|
799
812
|
|
|
813
|
+
test('date', function (t) {
|
|
814
|
+
const d = new Date()
|
|
815
|
+
|
|
816
|
+
t.alike(enc.decode(enc.date, enc.encode(enc.date, d)), d)
|
|
817
|
+
})
|
|
818
|
+
|
|
800
819
|
test('any', function (t) {
|
|
801
820
|
const o = {
|
|
802
821
|
hello: 'world',
|
|
@@ -804,6 +823,7 @@ test('any', function (t) {
|
|
|
804
823
|
neg: -42,
|
|
805
824
|
arr: [{ yes: 1 }, { no: false }],
|
|
806
825
|
nest: {},
|
|
826
|
+
today: new Date(),
|
|
807
827
|
float: 0.54
|
|
808
828
|
}
|
|
809
829
|
|