compact-encoding 2.17.0 → 2.19.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/.gitattributes +1 -0
- package/.github/workflows/test-node.yml +7 -7
- package/.prettierrc +1 -0
- package/README.md +92 -93
- package/endian.js +2 -1
- package/index.js +337 -217
- package/lexint.js +19 -16
- package/package.json +5 -3
- package/raw.js +34 -34
- package/test.js +277 -60
package/lexint.js
CHANGED
|
@@ -4,7 +4,7 @@ module.exports = {
|
|
|
4
4
|
decode
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
function preencode
|
|
7
|
+
function preencode(state, num) {
|
|
8
8
|
if (num < 251) {
|
|
9
9
|
state.end++
|
|
10
10
|
} else if (num < 256) {
|
|
@@ -23,7 +23,7 @@ function preencode (state, num) {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function encode
|
|
26
|
+
function encode(state, num) {
|
|
27
27
|
const max = 251
|
|
28
28
|
const x = num - max
|
|
29
29
|
|
|
@@ -34,18 +34,18 @@ function encode (state, num) {
|
|
|
34
34
|
state.buffer[state.start++] = x
|
|
35
35
|
} else if (num < 0x10000) {
|
|
36
36
|
state.buffer[state.start++] = max + 1
|
|
37
|
-
state.buffer[state.start++] = x >> 8 & 0xff
|
|
37
|
+
state.buffer[state.start++] = (x >> 8) & 0xff
|
|
38
38
|
state.buffer[state.start++] = x & 0xff
|
|
39
39
|
} else if (num < 0x1000000) {
|
|
40
40
|
state.buffer[state.start++] = max + 2
|
|
41
41
|
state.buffer[state.start++] = x >> 16
|
|
42
|
-
state.buffer[state.start++] = x >> 8 & 0xff
|
|
42
|
+
state.buffer[state.start++] = (x >> 8) & 0xff
|
|
43
43
|
state.buffer[state.start++] = x & 0xff
|
|
44
44
|
} else if (num < 0x100000000) {
|
|
45
45
|
state.buffer[state.start++] = max + 3
|
|
46
46
|
state.buffer[state.start++] = x >> 24
|
|
47
|
-
state.buffer[state.start++] = x >> 16 & 0xff
|
|
48
|
-
state.buffer[state.start++] = x >> 8 & 0xff
|
|
47
|
+
state.buffer[state.start++] = (x >> 16) & 0xff
|
|
48
|
+
state.buffer[state.start++] = (x >> 8) & 0xff
|
|
49
49
|
state.buffer[state.start++] = x & 0xff
|
|
50
50
|
} else {
|
|
51
51
|
// need to use Math here as bitwise ops are 32 bit
|
|
@@ -56,12 +56,12 @@ function encode (state, num) {
|
|
|
56
56
|
const rem = x / Math.pow(2, exp - 11)
|
|
57
57
|
|
|
58
58
|
for (let i = 5; i >= 0; i--) {
|
|
59
|
-
state.buffer[state.start++] = rem / Math.pow(2, 8 * i) & 0xff
|
|
59
|
+
state.buffer[state.start++] = (rem / Math.pow(2, 8 * i)) & 0xff
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
function decode
|
|
64
|
+
function decode(state) {
|
|
65
65
|
const max = 251
|
|
66
66
|
|
|
67
67
|
if (state.end - state.start < 1) throw new Error('Out of bounds')
|
|
@@ -75,30 +75,33 @@ function decode (state) {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
if (flag < 252) {
|
|
78
|
-
return state.buffer[state.start++] +
|
|
79
|
-
max
|
|
78
|
+
return state.buffer[state.start++] + max
|
|
80
79
|
}
|
|
81
80
|
|
|
82
81
|
if (flag < 253) {
|
|
83
|
-
return (
|
|
84
|
-
state.buffer[state.start++] +
|
|
85
|
-
|
|
82
|
+
return (
|
|
83
|
+
(state.buffer[state.start++] << 8) + state.buffer[state.start++] + max
|
|
84
|
+
)
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
if (flag < 254) {
|
|
89
|
-
return (
|
|
88
|
+
return (
|
|
89
|
+
(state.buffer[state.start++] << 16) +
|
|
90
90
|
(state.buffer[state.start++] << 8) +
|
|
91
91
|
state.buffer[state.start++] +
|
|
92
92
|
max
|
|
93
|
+
)
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
// << 24 result may be interpreted as negative
|
|
96
97
|
if (flag < 255) {
|
|
97
|
-
return (
|
|
98
|
+
return (
|
|
99
|
+
state.buffer[state.start++] * 0x1000000 +
|
|
98
100
|
(state.buffer[state.start++] << 16) +
|
|
99
101
|
(state.buffer[state.start++] << 8) +
|
|
100
102
|
state.buffer[state.start++] +
|
|
101
103
|
max
|
|
104
|
+
)
|
|
102
105
|
}
|
|
103
106
|
|
|
104
107
|
const exp = decode(state)
|
|
@@ -110,5 +113,5 @@ function decode (state) {
|
|
|
110
113
|
rem += state.buffer[state.start++] * Math.pow(2, 8 * i)
|
|
111
114
|
}
|
|
112
115
|
|
|
113
|
-
return
|
|
116
|
+
return rem * Math.pow(2, exp - 11) + max
|
|
114
117
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compact-encoding",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.0",
|
|
4
4
|
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"brittle": "^3.0.0",
|
|
11
|
-
"
|
|
11
|
+
"prettier": "^3.6.2",
|
|
12
|
+
"prettier-config-holepunch": "^1.0.0"
|
|
12
13
|
},
|
|
13
14
|
"scripts": {
|
|
14
|
-
"
|
|
15
|
+
"format": "prettier . --write",
|
|
16
|
+
"test": "prettier . --check && brittle test.js"
|
|
15
17
|
},
|
|
16
18
|
"repository": {
|
|
17
19
|
"type": "git",
|
package/raw.js
CHANGED
|
@@ -3,60 +3,60 @@ const b4a = require('b4a')
|
|
|
3
3
|
const { BE } = require('./endian')
|
|
4
4
|
|
|
5
5
|
exports = module.exports = {
|
|
6
|
-
preencode
|
|
6
|
+
preencode(state, b) {
|
|
7
7
|
state.end += b.byteLength
|
|
8
8
|
},
|
|
9
|
-
encode
|
|
9
|
+
encode(state, b) {
|
|
10
10
|
state.buffer.set(b, state.start)
|
|
11
11
|
state.start += b.byteLength
|
|
12
12
|
},
|
|
13
|
-
decode
|
|
13
|
+
decode(state) {
|
|
14
14
|
const b = state.buffer.subarray(state.start, state.end)
|
|
15
15
|
state.start = state.end
|
|
16
16
|
return b
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const buffer = exports.buffer = {
|
|
21
|
-
preencode
|
|
20
|
+
const buffer = (exports.buffer = {
|
|
21
|
+
preencode(state, b) {
|
|
22
22
|
if (b) uint8array.preencode(state, b)
|
|
23
23
|
else state.end++
|
|
24
24
|
},
|
|
25
|
-
encode
|
|
25
|
+
encode(state, b) {
|
|
26
26
|
if (b) uint8array.encode(state, b)
|
|
27
27
|
else state.buffer[state.start++] = 0
|
|
28
28
|
},
|
|
29
|
-
decode
|
|
29
|
+
decode(state) {
|
|
30
30
|
const b = state.buffer.subarray(state.start)
|
|
31
31
|
if (b.byteLength === 0) return null
|
|
32
32
|
state.start = state.end
|
|
33
33
|
return b
|
|
34
34
|
}
|
|
35
|
-
}
|
|
35
|
+
})
|
|
36
36
|
|
|
37
37
|
exports.binary = {
|
|
38
38
|
...buffer,
|
|
39
|
-
preencode
|
|
39
|
+
preencode(state, b) {
|
|
40
40
|
if (typeof b === 'string') utf8.preencode(state, b)
|
|
41
41
|
else buffer.preencode(state, b)
|
|
42
42
|
},
|
|
43
|
-
encode
|
|
43
|
+
encode(state, b) {
|
|
44
44
|
if (typeof b === 'string') utf8.encode(state, b)
|
|
45
45
|
else buffer.encode(state, b)
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
exports.arraybuffer = {
|
|
50
|
-
preencode
|
|
50
|
+
preencode(state, b) {
|
|
51
51
|
state.end += b.byteLength
|
|
52
52
|
},
|
|
53
|
-
encode
|
|
53
|
+
encode(state, b) {
|
|
54
54
|
const view = new Uint8Array(b)
|
|
55
55
|
|
|
56
56
|
state.buffer.set(view, state.start)
|
|
57
57
|
state.start += b.byteLength
|
|
58
58
|
},
|
|
59
|
-
decode
|
|
59
|
+
decode(state) {
|
|
60
60
|
const b = new ArrayBuffer(state.end - state.start)
|
|
61
61
|
const view = new Uint8Array(b)
|
|
62
62
|
|
|
@@ -68,14 +68,14 @@ exports.arraybuffer = {
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
function typedarray
|
|
71
|
+
function typedarray(TypedArray, swap) {
|
|
72
72
|
const n = TypedArray.BYTES_PER_ELEMENT
|
|
73
73
|
|
|
74
74
|
return {
|
|
75
|
-
preencode
|
|
75
|
+
preencode(state, b) {
|
|
76
76
|
state.end += b.byteLength
|
|
77
77
|
},
|
|
78
|
-
encode
|
|
78
|
+
encode(state, b) {
|
|
79
79
|
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
|
80
80
|
|
|
81
81
|
if (BE && swap) swap(view)
|
|
@@ -83,9 +83,9 @@ function typedarray (TypedArray, swap) {
|
|
|
83
83
|
state.buffer.set(view, state.start)
|
|
84
84
|
state.start += b.byteLength
|
|
85
85
|
},
|
|
86
|
-
decode
|
|
86
|
+
decode(state) {
|
|
87
87
|
let b = state.buffer.subarray(state.start)
|
|
88
|
-
if (
|
|
88
|
+
if (b.byteOffset % n !== 0) b = new Uint8Array(b)
|
|
89
89
|
|
|
90
90
|
if (BE && swap) swap(b)
|
|
91
91
|
|
|
@@ -96,7 +96,7 @@ function typedarray (TypedArray, swap) {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
const uint8array = exports.uint8array = typedarray(Uint8Array)
|
|
99
|
+
const uint8array = (exports.uint8array = typedarray(Uint8Array))
|
|
100
100
|
exports.uint16array = typedarray(Uint16Array, b4a.swap16)
|
|
101
101
|
exports.uint32array = typedarray(Uint32Array, b4a.swap32)
|
|
102
102
|
|
|
@@ -110,15 +110,15 @@ exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
|
|
|
110
110
|
exports.float32array = typedarray(Float32Array, b4a.swap32)
|
|
111
111
|
exports.float64array = typedarray(Float64Array, b4a.swap64)
|
|
112
112
|
|
|
113
|
-
function string
|
|
113
|
+
function string(encoding) {
|
|
114
114
|
return {
|
|
115
|
-
preencode
|
|
115
|
+
preencode(state, s) {
|
|
116
116
|
state.end += b4a.byteLength(s, encoding)
|
|
117
117
|
},
|
|
118
|
-
encode
|
|
118
|
+
encode(state, s) {
|
|
119
119
|
state.start += b4a.write(state.buffer, s, state.start, encoding)
|
|
120
120
|
},
|
|
121
|
-
decode
|
|
121
|
+
decode(state) {
|
|
122
122
|
const s = b4a.toString(state.buffer, encoding, state.start)
|
|
123
123
|
state.start = state.end
|
|
124
124
|
return s
|
|
@@ -126,21 +126,21 @@ function string (encoding) {
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
const utf8 = exports.string = exports.utf8 = string('utf-8')
|
|
129
|
+
const utf8 = (exports.string = exports.utf8 = string('utf-8'))
|
|
130
130
|
exports.ascii = string('ascii')
|
|
131
131
|
exports.hex = string('hex')
|
|
132
132
|
exports.base64 = string('base64')
|
|
133
133
|
exports.ucs2 = exports.utf16le = string('utf16le')
|
|
134
134
|
|
|
135
|
-
exports.array = function array
|
|
135
|
+
exports.array = function array(enc) {
|
|
136
136
|
return {
|
|
137
|
-
preencode
|
|
137
|
+
preencode(state, list) {
|
|
138
138
|
for (const value of list) enc.preencode(state, value)
|
|
139
139
|
},
|
|
140
|
-
encode
|
|
140
|
+
encode(state, list) {
|
|
141
141
|
for (const value of list) enc.encode(state, value)
|
|
142
142
|
},
|
|
143
|
-
decode
|
|
143
|
+
decode(state) {
|
|
144
144
|
const arr = []
|
|
145
145
|
while (state.start < state.end) arr.push(enc.decode(state))
|
|
146
146
|
return arr
|
|
@@ -149,25 +149,25 @@ exports.array = function array (enc) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
exports.json = {
|
|
152
|
-
preencode
|
|
152
|
+
preencode(state, v) {
|
|
153
153
|
utf8.preencode(state, JSON.stringify(v))
|
|
154
154
|
},
|
|
155
|
-
encode
|
|
155
|
+
encode(state, v) {
|
|
156
156
|
utf8.encode(state, JSON.stringify(v))
|
|
157
157
|
},
|
|
158
|
-
decode
|
|
158
|
+
decode(state) {
|
|
159
159
|
return JSON.parse(utf8.decode(state))
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
exports.ndjson = {
|
|
164
|
-
preencode
|
|
164
|
+
preencode(state, v) {
|
|
165
165
|
utf8.preencode(state, JSON.stringify(v) + '\n')
|
|
166
166
|
},
|
|
167
|
-
encode
|
|
167
|
+
encode(state, v) {
|
|
168
168
|
utf8.encode(state, JSON.stringify(v) + '\n')
|
|
169
169
|
},
|
|
170
|
-
decode
|
|
170
|
+
decode(state) {
|
|
171
171
|
return JSON.parse(utf8.decode(state))
|
|
172
172
|
}
|
|
173
173
|
}
|