compact-encoding 3.3.0 → 3.3.2
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/endian.d.ts +2 -0
- package/index.d.ts +203 -0
- package/index.js +34 -14
- package/lexint.d.ts +3 -0
- package/package.json +7 -2
- package/raw.d.ts +4 -0
package/endian.d.ts
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
interface State<T extends Uint8Array = Uint8Array> {
|
|
2
|
+
start: number
|
|
3
|
+
end: number
|
|
4
|
+
buffer: T | null
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function state<T extends Uint8Array = Uint8Array>(
|
|
8
|
+
start?: number,
|
|
9
|
+
end?: number,
|
|
10
|
+
buffer?: T | null
|
|
11
|
+
): State<T>
|
|
12
|
+
|
|
13
|
+
interface Encoder<Input = unknown, Output = Input> {
|
|
14
|
+
preencode(state: State, val: Input): void
|
|
15
|
+
encode(state: State, val: Input): void
|
|
16
|
+
decode(state: State): Output
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Raw<T extends Uint8Array = Uint8Array> extends Encoder<T> {
|
|
20
|
+
buffer: Encoder<T>
|
|
21
|
+
binary: Encoder<string | T, T>
|
|
22
|
+
arraybuffer: Encoder<ArrayBuffer>
|
|
23
|
+
|
|
24
|
+
uint8array: Encoder<Uint8Array>
|
|
25
|
+
uint16array: Encoder<Uint16Array>
|
|
26
|
+
uint32array: Encoder<Uint32Array>
|
|
27
|
+
|
|
28
|
+
int8array: Encoder<Int8Array>
|
|
29
|
+
int16array: Encoder<Int16Array>
|
|
30
|
+
int32array: Encoder<Int32Array>
|
|
31
|
+
|
|
32
|
+
biguint64array: Encoder<BigUint64Array>
|
|
33
|
+
bigint64array: Encoder<BigInt64Array>
|
|
34
|
+
|
|
35
|
+
float32array: Encoder<Float32Array>
|
|
36
|
+
float64array: Encoder<Float64Array>
|
|
37
|
+
|
|
38
|
+
string: Encoder<string>
|
|
39
|
+
utf8: Encoder<string>
|
|
40
|
+
ascii: Encoder<string>
|
|
41
|
+
hex: Encoder<string>
|
|
42
|
+
base64: Encoder<string>
|
|
43
|
+
ucs2: Encoder<string>
|
|
44
|
+
utf16le: Encoder<string>
|
|
45
|
+
|
|
46
|
+
array: <I, O = I>(enc: Encoder<I, O>) => Encoder<I[], O[]>
|
|
47
|
+
|
|
48
|
+
json: Encoder<unknown>
|
|
49
|
+
ndjson: Encoder<unknown>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const raw: Raw
|
|
53
|
+
|
|
54
|
+
export const uint: Encoder<number>
|
|
55
|
+
export const uint8: Encoder<number>
|
|
56
|
+
export const uint16: Encoder<number>
|
|
57
|
+
export const uint24: Encoder<number>
|
|
58
|
+
export const uint32: Encoder<number>
|
|
59
|
+
export const uint32be: Encoder<number>
|
|
60
|
+
export const uint40: Encoder<number>
|
|
61
|
+
export const uint48: Encoder<number>
|
|
62
|
+
export const uint56: Encoder<number>
|
|
63
|
+
export const uint64: Encoder<number>
|
|
64
|
+
export const uint64be: Encoder<number>
|
|
65
|
+
|
|
66
|
+
export const int: Encoder<number>
|
|
67
|
+
export const int8: Encoder<number>
|
|
68
|
+
export const int16: Encoder<number>
|
|
69
|
+
export const int24: Encoder<number>
|
|
70
|
+
export const int32: Encoder<number>
|
|
71
|
+
export const int40: Encoder<number>
|
|
72
|
+
export const int48: Encoder<number>
|
|
73
|
+
export const int56: Encoder<number>
|
|
74
|
+
export const int64: Encoder<number>
|
|
75
|
+
|
|
76
|
+
export const biguint64: Encoder<bigint>
|
|
77
|
+
export const bigint64: Encoder<bigint>
|
|
78
|
+
export const biguint: Encoder<bigint>
|
|
79
|
+
export const bigint: Encoder<bigint>
|
|
80
|
+
|
|
81
|
+
export const lexint: Encoder<number>
|
|
82
|
+
|
|
83
|
+
export const float32: Encoder<number>
|
|
84
|
+
export const float64: Encoder<number>
|
|
85
|
+
|
|
86
|
+
export const buffer: Encoder<Uint8Array>
|
|
87
|
+
export const optionalBuffer: Encoder<Uint8Array | null>
|
|
88
|
+
export const binary: Encoder<string | Uint8Array, Uint8Array>
|
|
89
|
+
export const arraybuffer: Encoder<ArrayBuffer>
|
|
90
|
+
|
|
91
|
+
export const uint8array: Encoder<Uint8Array>
|
|
92
|
+
export const uint16array: Encoder<Uint16Array>
|
|
93
|
+
export const uint32array: Encoder<Uint32Array>
|
|
94
|
+
|
|
95
|
+
export const int8array: Encoder<Int8Array>
|
|
96
|
+
export const int16array: Encoder<Int16Array>
|
|
97
|
+
export const int32array: Encoder<Int32Array>
|
|
98
|
+
|
|
99
|
+
export const biguint64array: Encoder<BigUint64Array>
|
|
100
|
+
export const bigint64array: Encoder<BigInt64Array>
|
|
101
|
+
|
|
102
|
+
export const float32array: Encoder<Float32Array>
|
|
103
|
+
export const float64array: Encoder<Float64Array>
|
|
104
|
+
|
|
105
|
+
interface StringEncoder<T = unknown> extends Encoder<T> {
|
|
106
|
+
fixed(n: number): Encoder<T>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const string: StringEncoder<string>
|
|
110
|
+
export const utf8: StringEncoder<string>
|
|
111
|
+
export const ascii: StringEncoder<string>
|
|
112
|
+
export const hex: StringEncoder<string>
|
|
113
|
+
export const base64: StringEncoder<string>
|
|
114
|
+
export const ucs2: StringEncoder<string>
|
|
115
|
+
export const utf16le: StringEncoder<string>
|
|
116
|
+
|
|
117
|
+
export const bool: Encoder<boolean>
|
|
118
|
+
|
|
119
|
+
export function fixed(n: number): Encoder<Uint8Array>
|
|
120
|
+
export const fixed32: Encoder<Uint8Array>
|
|
121
|
+
export const fixed64: Encoder<Uint8Array>
|
|
122
|
+
|
|
123
|
+
export function array<I, O>(enc: Encoder<I, O>): Encoder<I[], O[]>
|
|
124
|
+
|
|
125
|
+
export function frame<I, O>(enc: Encoder<I, O>): Encoder<I, O>
|
|
126
|
+
|
|
127
|
+
export const date: Encoder<Date>
|
|
128
|
+
|
|
129
|
+
export const json: Encoder<unknown>
|
|
130
|
+
export const ndjson: Encoder<unknown>
|
|
131
|
+
export const none: Encoder<unknown, null>
|
|
132
|
+
export const any: Encoder<unknown>
|
|
133
|
+
|
|
134
|
+
interface AddressInput {
|
|
135
|
+
host: string
|
|
136
|
+
port: number
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface Address<F extends 4 | 6 = 4 | 6> extends AddressInput {
|
|
140
|
+
family: F
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export const port: Encoder<number>
|
|
144
|
+
export const ipv4: Encoder<string>
|
|
145
|
+
export const ipv4Address: Encoder<AddressInput, Address<4>>
|
|
146
|
+
export const ipv6: Encoder<string>
|
|
147
|
+
export const ipv6Address: Encoder<AddressInput, Address<6>>
|
|
148
|
+
export const ip: Encoder<string>
|
|
149
|
+
export const ipAddress: Encoder<AddressInput, Address>
|
|
150
|
+
|
|
151
|
+
export function record<I, O = I>(
|
|
152
|
+
keyEncoding: Encoder<string>,
|
|
153
|
+
valueEncoder: Encoder<I, O>
|
|
154
|
+
): Encoder<Record<string, I>, Record<string, O>>
|
|
155
|
+
|
|
156
|
+
export const stringRecord: Encoder<Record<string, string>>
|
|
157
|
+
|
|
158
|
+
interface CodecLike<Input, Output = Input> {
|
|
159
|
+
encode(value: Input): Uint8Array
|
|
160
|
+
decode(buffer: Uint8Array): Output
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
interface AbstractEncode<Input> {
|
|
164
|
+
(value: Input, buffer: Uint8Array, offset: number): unknown
|
|
165
|
+
bytes: number
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
interface AbstractDecode<Output> {
|
|
169
|
+
(buffer: Uint8Array, start: number, end: number): Output
|
|
170
|
+
bytes: number
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
interface AbstractEncodingLike<Input, Output = Input> {
|
|
174
|
+
encodingLength(value: Input): number
|
|
175
|
+
encode: AbstractEncode<Input>
|
|
176
|
+
decode: AbstractDecode<Output>
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function from(name: 'ascii'): Raw['ascii']
|
|
180
|
+
export function from(name: 'utf-8' | 'utf8'): Raw['utf8']
|
|
181
|
+
export function from(name: 'hex'): Raw['hex']
|
|
182
|
+
export function from(name: 'base64'): Raw['base64']
|
|
183
|
+
export function from(
|
|
184
|
+
name: 'utf16-le' | 'utf16le' | 'ucs-2' | 'ucs2'
|
|
185
|
+
): Raw['ucs2']
|
|
186
|
+
export function from(name: 'ndjson'): Raw['ndjson']
|
|
187
|
+
export function from(name: 'json'): Raw['json']
|
|
188
|
+
export function from(name: 'binary' | string): Raw['binary']
|
|
189
|
+
export function from<I, O>(enc: Encoder<I, O>): Encoder<I, O>
|
|
190
|
+
export function from<I, O>(enc: CodecLike<I, O>): Encoder<I, O>
|
|
191
|
+
export function from<I, O>(enc: AbstractEncodingLike<I, O>): Encoder<I, O>
|
|
192
|
+
|
|
193
|
+
export function encode<Input = unknown, Output = Input>(
|
|
194
|
+
enc: Encoder<Input, Output>,
|
|
195
|
+
m: Input
|
|
196
|
+
): Uint8Array
|
|
197
|
+
|
|
198
|
+
export function decode<Input = unknown, Output = Input>(
|
|
199
|
+
enc: Encoder<Input, Output>,
|
|
200
|
+
buffer: Uint8Array
|
|
201
|
+
): Output
|
|
202
|
+
|
|
203
|
+
export type { State, Encoder, Raw, StringEncoder, AddressInput, Address }
|
package/index.js
CHANGED
|
@@ -2,6 +2,11 @@ const b4a = require('b4a')
|
|
|
2
2
|
|
|
3
3
|
const { BE } = require('./endian')
|
|
4
4
|
|
|
5
|
+
// Zig-zag doubles the magnitude of a value before it is written as a uint, so
|
|
6
|
+
// an int only reaches half as far as a uint of the same width does.
|
|
7
|
+
const MAX_SAFE_INT = 2 ** 52 - 1
|
|
8
|
+
const MIN_SAFE_INT = -(2 ** 52)
|
|
9
|
+
|
|
5
10
|
exports.state = function (start = 0, end = 0, buffer = null) {
|
|
6
11
|
return { start, end, buffer }
|
|
7
12
|
}
|
|
@@ -1064,6 +1069,7 @@ function zigZagDecodeInt(n) {
|
|
|
1064
1069
|
}
|
|
1065
1070
|
|
|
1066
1071
|
function zigZagEncodeInt(n) {
|
|
1072
|
+
validateInt(n)
|
|
1067
1073
|
// 0, -1, 1, -2, 2, ...
|
|
1068
1074
|
return n < 0 ? 2 * -n - 1 : n === 0 ? 0 : 2 * n
|
|
1069
1075
|
}
|
|
@@ -1092,21 +1098,35 @@ function zigZagEncodeBigInt(n) {
|
|
|
1092
1098
|
}
|
|
1093
1099
|
|
|
1094
1100
|
function validateSafeUint(n) {
|
|
1095
|
-
if (n
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
)
|
|
1099
|
-
return n
|
|
1101
|
+
if (n <= Number.MAX_SAFE_INTEGER) return n // Handles NaN as well
|
|
1102
|
+
|
|
1103
|
+
throw outsideUintRange()
|
|
1100
1104
|
}
|
|
1101
1105
|
|
|
1102
1106
|
function validateUint(n) {
|
|
1103
|
-
if (n >= 0
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1107
|
+
if (n >= 0 && n <= Number.MAX_SAFE_INTEGER) return n // Handles NaN as well
|
|
1108
|
+
|
|
1109
|
+
throw outsideUintRange()
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
function validateInt(n) {
|
|
1113
|
+
if (n >= MIN_SAFE_INT && n <= MAX_SAFE_INT) return n // Handles NaN as well
|
|
1114
|
+
|
|
1115
|
+
throw outsideIntRange()
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
// The validations above sit on the hottest paths in the library and are small
|
|
1119
|
+
// enough to be inlined, which building a message inline would put a stop to.
|
|
1120
|
+
// Kept out here, the message costs nothing until it is actually thrown.
|
|
1121
|
+
|
|
1122
|
+
function outsideUintRange() {
|
|
1123
|
+
return new Error(
|
|
1124
|
+
`uint must be between 0 and ${Number.MAX_SAFE_INTEGER}, use biguint`
|
|
1125
|
+
)
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
function outsideIntRange() {
|
|
1129
|
+
return new Error(
|
|
1130
|
+
`int must be between ${MIN_SAFE_INT} and ${MAX_SAFE_INT}, use bigint`
|
|
1131
|
+
)
|
|
1112
1132
|
}
|
package/lexint.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compact-encoding",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.2",
|
|
4
4
|
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
6
7
|
"files": [
|
|
7
8
|
"endian.js",
|
|
9
|
+
"endian.d.ts",
|
|
8
10
|
"index.js",
|
|
11
|
+
"index.d.ts",
|
|
9
12
|
"lexint.js",
|
|
10
|
-
"
|
|
13
|
+
"lexint.d.ts",
|
|
14
|
+
"raw.js",
|
|
15
|
+
"raw.d.ts"
|
|
11
16
|
],
|
|
12
17
|
"dependencies": {
|
|
13
18
|
"b4a": "^1.3.0"
|
package/raw.d.ts
ADDED