compact-encoding 2.15.0 → 2.15.1
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 +13 -0
- package/package.json +1 -1
- package/test.js +13 -0
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) {
|
|
@@ -737,3 +746,7 @@ function zigZagEncodeBigInt (n) {
|
|
|
737
746
|
// 0, -1, 1, -2, 2, ...
|
|
738
747
|
return n < 0n ? (2n * -n) - 1n : n === 0n ? 0n : 2n * n
|
|
739
748
|
}
|
|
749
|
+
|
|
750
|
+
function validateUint (n) {
|
|
751
|
+
if ((n >= 0) === false /* Handles NaN as well */) throw new Error('uint must be positive')
|
|
752
|
+
}
|
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)
|