bin-serde 1.6.8 → 1.7.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.ts +16 -54
- package/lib/index.js +15 -50
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import utf8Size from "utf8-buffer-size";
|
|
|
4
4
|
const SLAB_SIZE = 8192;
|
|
5
5
|
const MAX_POOLED = 4096;
|
|
6
6
|
|
|
7
|
-
let slab
|
|
7
|
+
let slab = allocUint8Array(SLAB_SIZE);
|
|
8
8
|
let slabOffset = 0;
|
|
9
9
|
|
|
10
10
|
const f32 = new Float32Array(1);
|
|
@@ -13,11 +13,11 @@ const f32u8 = new Uint8Array(f32.buffer);
|
|
|
13
13
|
function allocFromSlab(size: number): Uint8Array {
|
|
14
14
|
if (size > MAX_POOLED) {
|
|
15
15
|
// Too large for pool, allocate directly
|
|
16
|
-
return
|
|
16
|
+
return allocUint8Array(size);
|
|
17
17
|
}
|
|
18
18
|
if (slabOffset + size > SLAB_SIZE) {
|
|
19
19
|
// Slab full, allocate new one
|
|
20
|
-
slab =
|
|
20
|
+
slab = allocUint8Array(SLAB_SIZE);
|
|
21
21
|
slabOffset = 0;
|
|
22
22
|
}
|
|
23
23
|
const buf = slab.subarray(slabOffset, slabOffset + size);
|
|
@@ -25,6 +25,10 @@ function allocFromSlab(size: number): Uint8Array {
|
|
|
25
25
|
return buf;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
function allocUint8Array(size: number): Uint8Array {
|
|
29
|
+
return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
|
|
30
|
+
}
|
|
31
|
+
|
|
28
32
|
export class Writer {
|
|
29
33
|
private pos = 0;
|
|
30
34
|
private bytes: Uint8Array;
|
|
@@ -55,55 +59,11 @@ export class Writer {
|
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
writeUVarint(val: number) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
} else if (val < 0x4000) {
|
|
62
|
-
// 2 bytes: 128 - 16,383
|
|
63
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
64
|
-
} else if (val < 0x200000) {
|
|
65
|
-
// 3 bytes: 16,384 - 2,097,151
|
|
66
|
-
this.writeUInt8((val >> 14) | 0x80);
|
|
67
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
68
|
-
} else if (val < 0x10000000) {
|
|
69
|
-
// 4 bytes: 2,097,152 - 268,435,455
|
|
70
|
-
this.writeUInt32(
|
|
71
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
72
|
-
);
|
|
73
|
-
} else if (val < 0x800000000) {
|
|
74
|
-
// 5 bytes: 268,435,456 - 34,359,738,367
|
|
75
|
-
this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
|
|
76
|
-
this.writeUInt32(
|
|
77
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
78
|
-
);
|
|
79
|
-
} else if (val < 0x40000000000) {
|
|
80
|
-
// 6 bytes: 34,359,738,368 - 4,398,046,511,103
|
|
81
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
82
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
83
|
-
this.writeUInt32(
|
|
84
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
85
|
-
);
|
|
86
|
-
} else if (val < 0x2000000000000) {
|
|
87
|
-
// 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
|
|
88
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
89
|
-
this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
|
|
90
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
91
|
-
this.writeUInt32(
|
|
92
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
93
|
-
);
|
|
94
|
-
} else if (val <= Number.MAX_SAFE_INTEGER) {
|
|
95
|
-
// 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
|
|
96
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
97
|
-
this.writeUInt16(
|
|
98
|
-
(Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080
|
|
99
|
-
);
|
|
100
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
101
|
-
this.writeUInt32(
|
|
102
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
103
|
-
);
|
|
104
|
-
} else {
|
|
105
|
-
throw new Error("Value out of range");
|
|
62
|
+
while (val >= 0x80) {
|
|
63
|
+
this.writeUInt8((val & 0x7f) | 0x80);
|
|
64
|
+
val >>>= 7;
|
|
106
65
|
}
|
|
66
|
+
this.writeUInt8(val);
|
|
107
67
|
return this;
|
|
108
68
|
}
|
|
109
69
|
|
|
@@ -247,13 +207,15 @@ export class Reader {
|
|
|
247
207
|
}
|
|
248
208
|
|
|
249
209
|
readUVarint() {
|
|
250
|
-
let
|
|
210
|
+
let result = 0;
|
|
211
|
+
let shift = 0;
|
|
251
212
|
while (true) {
|
|
252
213
|
const byte = this.readUInt8();
|
|
214
|
+
result |= (byte & 0x7f) << shift;
|
|
253
215
|
if (byte < 0x80) {
|
|
254
|
-
return
|
|
216
|
+
return result;
|
|
255
217
|
}
|
|
256
|
-
|
|
218
|
+
shift += 7;
|
|
257
219
|
}
|
|
258
220
|
}
|
|
259
221
|
|
package/lib/index.js
CHANGED
|
@@ -2,24 +2,27 @@ import { pack, unpack } from "./utf8-buffer.js";
|
|
|
2
2
|
import utf8Size from "utf8-buffer-size";
|
|
3
3
|
const SLAB_SIZE = 8192;
|
|
4
4
|
const MAX_POOLED = 4096;
|
|
5
|
-
let slab =
|
|
5
|
+
let slab = allocUint8Array(SLAB_SIZE);
|
|
6
6
|
let slabOffset = 0;
|
|
7
7
|
const f32 = new Float32Array(1);
|
|
8
8
|
const f32u8 = new Uint8Array(f32.buffer);
|
|
9
9
|
function allocFromSlab(size) {
|
|
10
10
|
if (size > MAX_POOLED) {
|
|
11
11
|
// Too large for pool, allocate directly
|
|
12
|
-
return
|
|
12
|
+
return allocUint8Array(size);
|
|
13
13
|
}
|
|
14
14
|
if (slabOffset + size > SLAB_SIZE) {
|
|
15
15
|
// Slab full, allocate new one
|
|
16
|
-
slab =
|
|
16
|
+
slab = allocUint8Array(SLAB_SIZE);
|
|
17
17
|
slabOffset = 0;
|
|
18
18
|
}
|
|
19
19
|
const buf = slab.subarray(slabOffset, slabOffset + size);
|
|
20
20
|
slabOffset += size;
|
|
21
21
|
return buf;
|
|
22
22
|
}
|
|
23
|
+
function allocUint8Array(size) {
|
|
24
|
+
return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
|
|
25
|
+
}
|
|
23
26
|
export class Writer {
|
|
24
27
|
pos = 0;
|
|
25
28
|
bytes;
|
|
@@ -45,51 +48,11 @@ export class Writer {
|
|
|
45
48
|
return this;
|
|
46
49
|
}
|
|
47
50
|
writeUVarint(val) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
else if (val < 0x4000) {
|
|
53
|
-
// 2 bytes: 128 - 16,383
|
|
54
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
55
|
-
}
|
|
56
|
-
else if (val < 0x200000) {
|
|
57
|
-
// 3 bytes: 16,384 - 2,097,151
|
|
58
|
-
this.writeUInt8((val >> 14) | 0x80);
|
|
59
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
60
|
-
}
|
|
61
|
-
else if (val < 0x10000000) {
|
|
62
|
-
// 4 bytes: 2,097,152 - 268,435,455
|
|
63
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
64
|
-
}
|
|
65
|
-
else if (val < 0x800000000) {
|
|
66
|
-
// 5 bytes: 268,435,456 - 34,359,738,367
|
|
67
|
-
this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
|
|
68
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
69
|
-
}
|
|
70
|
-
else if (val < 0x40000000000) {
|
|
71
|
-
// 6 bytes: 34,359,738,368 - 4,398,046,511,103
|
|
72
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
73
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
74
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
75
|
-
}
|
|
76
|
-
else if (val < 0x2000000000000) {
|
|
77
|
-
// 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
|
|
78
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
79
|
-
this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
|
|
80
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
81
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
82
|
-
}
|
|
83
|
-
else if (val <= Number.MAX_SAFE_INTEGER) {
|
|
84
|
-
// 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
|
|
85
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
86
|
-
this.writeUInt16((Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080);
|
|
87
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
88
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
throw new Error("Value out of range");
|
|
51
|
+
while (val >= 0x80) {
|
|
52
|
+
this.writeUInt8((val & 0x7f) | 0x80);
|
|
53
|
+
val >>>= 7;
|
|
92
54
|
}
|
|
55
|
+
this.writeUInt8(val);
|
|
93
56
|
return this;
|
|
94
57
|
}
|
|
95
58
|
writeVarint(val) {
|
|
@@ -217,13 +180,15 @@ export class Reader {
|
|
|
217
180
|
return val;
|
|
218
181
|
}
|
|
219
182
|
readUVarint() {
|
|
220
|
-
let
|
|
183
|
+
let result = 0;
|
|
184
|
+
let shift = 0;
|
|
221
185
|
while (true) {
|
|
222
186
|
const byte = this.readUInt8();
|
|
187
|
+
result |= (byte & 0x7f) << shift;
|
|
223
188
|
if (byte < 0x80) {
|
|
224
|
-
return
|
|
189
|
+
return result;
|
|
225
190
|
}
|
|
226
|
-
|
|
191
|
+
shift += 7;
|
|
227
192
|
}
|
|
228
193
|
}
|
|
229
194
|
readVarint() {
|