bin-serde 1.6.9 → 1.7.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.ts +13 -51
- package/lib/index.js +13 -47
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -59,55 +59,13 @@ export class Writer {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
writeUVarint(val: number) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
68
|
-
} else if (val < 0x200000) {
|
|
69
|
-
// 3 bytes: 16,384 - 2,097,151
|
|
70
|
-
this.writeUInt8((val >> 14) | 0x80);
|
|
71
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
72
|
-
} else if (val < 0x10000000) {
|
|
73
|
-
// 4 bytes: 2,097,152 - 268,435,455
|
|
74
|
-
this.writeUInt32(
|
|
75
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
76
|
-
);
|
|
77
|
-
} else if (val < 0x800000000) {
|
|
78
|
-
// 5 bytes: 268,435,456 - 34,359,738,367
|
|
79
|
-
this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
|
|
80
|
-
this.writeUInt32(
|
|
81
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
82
|
-
);
|
|
83
|
-
} else if (val < 0x40000000000) {
|
|
84
|
-
// 6 bytes: 34,359,738,368 - 4,398,046,511,103
|
|
85
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
86
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
87
|
-
this.writeUInt32(
|
|
88
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
89
|
-
);
|
|
90
|
-
} else if (val < 0x2000000000000) {
|
|
91
|
-
// 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
|
|
92
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
93
|
-
this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
|
|
94
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
95
|
-
this.writeUInt32(
|
|
96
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
97
|
-
);
|
|
98
|
-
} else if (val <= Number.MAX_SAFE_INTEGER) {
|
|
99
|
-
// 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
|
|
100
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
101
|
-
this.writeUInt16(
|
|
102
|
-
(Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080
|
|
103
|
-
);
|
|
104
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
105
|
-
this.writeUInt32(
|
|
106
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
107
|
-
);
|
|
108
|
-
} else {
|
|
109
|
-
throw new Error("Value out of range");
|
|
62
|
+
// Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
|
|
63
|
+
// Use Math.floor instead of >>> to handle values > 32 bits
|
|
64
|
+
while (val >= 0x80) {
|
|
65
|
+
this.writeUInt8((val & 0x7f) | 0x80);
|
|
66
|
+
val = Math.floor(val / 128);
|
|
110
67
|
}
|
|
68
|
+
this.writeUInt8(val);
|
|
111
69
|
return this;
|
|
112
70
|
}
|
|
113
71
|
|
|
@@ -251,13 +209,17 @@ export class Reader {
|
|
|
251
209
|
}
|
|
252
210
|
|
|
253
211
|
readUVarint() {
|
|
254
|
-
|
|
212
|
+
// Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
|
|
213
|
+
// Use multiplication instead of << to handle values > 32 bits
|
|
214
|
+
let result = 0;
|
|
215
|
+
let multiplier = 1;
|
|
255
216
|
while (true) {
|
|
256
217
|
const byte = this.readUInt8();
|
|
218
|
+
result += (byte & 0x7f) * multiplier;
|
|
257
219
|
if (byte < 0x80) {
|
|
258
|
-
return
|
|
220
|
+
return result;
|
|
259
221
|
}
|
|
260
|
-
|
|
222
|
+
multiplier *= 128;
|
|
261
223
|
}
|
|
262
224
|
}
|
|
263
225
|
|
package/lib/index.js
CHANGED
|
@@ -48,51 +48,13 @@ export class Writer {
|
|
|
48
48
|
return this;
|
|
49
49
|
}
|
|
50
50
|
writeUVarint(val) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// 2 bytes: 128 - 16,383
|
|
57
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
58
|
-
}
|
|
59
|
-
else if (val < 0x200000) {
|
|
60
|
-
// 3 bytes: 16,384 - 2,097,151
|
|
61
|
-
this.writeUInt8((val >> 14) | 0x80);
|
|
62
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
63
|
-
}
|
|
64
|
-
else if (val < 0x10000000) {
|
|
65
|
-
// 4 bytes: 2,097,152 - 268,435,455
|
|
66
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
67
|
-
}
|
|
68
|
-
else if (val < 0x800000000) {
|
|
69
|
-
// 5 bytes: 268,435,456 - 34,359,738,367
|
|
70
|
-
this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
|
|
71
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
72
|
-
}
|
|
73
|
-
else if (val < 0x40000000000) {
|
|
74
|
-
// 6 bytes: 34,359,738,368 - 4,398,046,511,103
|
|
75
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
76
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
77
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
78
|
-
}
|
|
79
|
-
else if (val < 0x2000000000000) {
|
|
80
|
-
// 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
|
|
81
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
82
|
-
this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
|
|
83
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
84
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
85
|
-
}
|
|
86
|
-
else if (val <= Number.MAX_SAFE_INTEGER) {
|
|
87
|
-
// 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
|
|
88
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
89
|
-
this.writeUInt16((Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080);
|
|
90
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
91
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
throw new Error("Value out of range");
|
|
51
|
+
// Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
|
|
52
|
+
// Use Math.floor instead of >>> to handle values > 32 bits
|
|
53
|
+
while (val >= 0x80) {
|
|
54
|
+
this.writeUInt8((val & 0x7f) | 0x80);
|
|
55
|
+
val = Math.floor(val / 128);
|
|
95
56
|
}
|
|
57
|
+
this.writeUInt8(val);
|
|
96
58
|
return this;
|
|
97
59
|
}
|
|
98
60
|
writeVarint(val) {
|
|
@@ -220,13 +182,17 @@ export class Reader {
|
|
|
220
182
|
return val;
|
|
221
183
|
}
|
|
222
184
|
readUVarint() {
|
|
223
|
-
|
|
185
|
+
// Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
|
|
186
|
+
// Use multiplication instead of << to handle values > 32 bits
|
|
187
|
+
let result = 0;
|
|
188
|
+
let multiplier = 1;
|
|
224
189
|
while (true) {
|
|
225
190
|
const byte = this.readUInt8();
|
|
191
|
+
result += (byte & 0x7f) * multiplier;
|
|
226
192
|
if (byte < 0x80) {
|
|
227
|
-
return
|
|
193
|
+
return result;
|
|
228
194
|
}
|
|
229
|
-
|
|
195
|
+
multiplier *= 128;
|
|
230
196
|
}
|
|
231
197
|
}
|
|
232
198
|
readVarint() {
|