compact-encoding 3.0.1 → 3.2.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 +45 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -105,6 +105,28 @@ const uint32 = (exports.uint32 = {
|
|
|
105
105
|
}
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
+
const uint32be = (exports.uint32be = {
|
|
109
|
+
preencode(state, n) {
|
|
110
|
+
state.end += 4
|
|
111
|
+
},
|
|
112
|
+
encode(state, n) {
|
|
113
|
+
validateUint(n)
|
|
114
|
+
state.buffer[state.start++] = n >>> 24
|
|
115
|
+
state.buffer[state.start++] = n >>> 16
|
|
116
|
+
state.buffer[state.start++] = n >>> 8
|
|
117
|
+
state.buffer[state.start++] = n
|
|
118
|
+
},
|
|
119
|
+
decode(state) {
|
|
120
|
+
if (state.end - state.start < 4) throw new Error('Out of bounds')
|
|
121
|
+
return (
|
|
122
|
+
state.buffer[state.start++] * 0x1000000 +
|
|
123
|
+
state.buffer[state.start++] * 0x10000 +
|
|
124
|
+
state.buffer[state.start++] * 0x100 +
|
|
125
|
+
state.buffer[state.start++]
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
108
130
|
const uint40 = (exports.uint40 = {
|
|
109
131
|
preencode(state, n) {
|
|
110
132
|
state.end += 5
|
|
@@ -169,6 +191,22 @@ const uint64 = (exports.uint64 = {
|
|
|
169
191
|
}
|
|
170
192
|
})
|
|
171
193
|
|
|
194
|
+
exports.uint64be = {
|
|
195
|
+
preencode(state, n) {
|
|
196
|
+
state.end += 8
|
|
197
|
+
},
|
|
198
|
+
encode(state, n) {
|
|
199
|
+
validateUint(n)
|
|
200
|
+
const r = Math.floor(n / 0x100000000)
|
|
201
|
+
uint32be.encode(state, r)
|
|
202
|
+
uint32be.encode(state, n)
|
|
203
|
+
},
|
|
204
|
+
decode(state) {
|
|
205
|
+
if (state.end - state.start < 8) throw new Error('Out of bounds')
|
|
206
|
+
return 0x100000000 * uint32be.decode(state) + uint32be.decode(state)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
172
210
|
const int = (exports.int = zigZagInt(uint))
|
|
173
211
|
exports.int8 = zigZagInt(uint8)
|
|
174
212
|
exports.int16 = zigZagInt(uint16)
|
|
@@ -1050,4 +1088,11 @@ function zigZagEncodeBigInt(n) {
|
|
|
1050
1088
|
function validateUint(n) {
|
|
1051
1089
|
if (n >= 0 === false /* Handles NaN as well */)
|
|
1052
1090
|
throw new Error('uint must be positive')
|
|
1091
|
+
// Beyond this, Number arithmetic loses precision and silently corrupts the
|
|
1092
|
+
// encoding. The zig-zag int codecs route through here too, so this also
|
|
1093
|
+
// guards int values whose doubled magnitude exceeds the safe range.
|
|
1094
|
+
if (n > Number.MAX_SAFE_INTEGER)
|
|
1095
|
+
throw new Error(
|
|
1096
|
+
'integer is greater than the maximum safe integer, use biguint/bigint'
|
|
1097
|
+
)
|
|
1053
1098
|
}
|