bin-serde 1.7.0 → 1.7.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/index.ts CHANGED
@@ -59,9 +59,11 @@ export class Writer {
59
59
  }
60
60
 
61
61
  writeUVarint(val: number) {
62
+ // Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
63
+ // Use Math.floor instead of >>> to handle values > 32 bits
62
64
  while (val >= 0x80) {
63
65
  this.writeUInt8((val & 0x7f) | 0x80);
64
- val >>>= 7;
66
+ val = Math.floor(val / 128);
65
67
  }
66
68
  this.writeUInt8(val);
67
69
  return this;
@@ -155,6 +157,11 @@ export class Writer {
155
157
  return this.bytes.subarray(0, this.pos);
156
158
  }
157
159
 
160
+ reset(): this {
161
+ this.pos = 0;
162
+ return this;
163
+ }
164
+
158
165
  get size(): number {
159
166
  return this.pos;
160
167
  }
@@ -207,15 +214,17 @@ export class Reader {
207
214
  }
208
215
 
209
216
  readUVarint() {
217
+ // Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
218
+ // Use multiplication instead of << to handle values > 32 bits
210
219
  let result = 0;
211
- let shift = 0;
220
+ let multiplier = 1;
212
221
  while (true) {
213
222
  const byte = this.readUInt8();
214
- result |= (byte & 0x7f) << shift;
223
+ result += (byte & 0x7f) * multiplier;
215
224
  if (byte < 0x80) {
216
225
  return result;
217
226
  }
218
- shift += 7;
227
+ multiplier *= 128;
219
228
  }
220
229
  }
221
230
 
package/lib/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare class Writer {
15
15
  writeBuffer(buf: Uint8Array): this;
16
16
  concat(other: Writer): this;
17
17
  toBuffer(): Uint8Array<ArrayBufferLike>;
18
+ reset(): this;
18
19
  get size(): number;
19
20
  private ensureSize;
20
21
  private get view();
package/lib/index.js CHANGED
@@ -48,9 +48,11 @@ export class Writer {
48
48
  return this;
49
49
  }
50
50
  writeUVarint(val) {
51
+ // Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
52
+ // Use Math.floor instead of >>> to handle values > 32 bits
51
53
  while (val >= 0x80) {
52
54
  this.writeUInt8((val & 0x7f) | 0x80);
53
- val >>>= 7;
55
+ val = Math.floor(val / 128);
54
56
  }
55
57
  this.writeUInt8(val);
56
58
  return this;
@@ -136,6 +138,10 @@ export class Writer {
136
138
  toBuffer() {
137
139
  return this.bytes.subarray(0, this.pos);
138
140
  }
141
+ reset() {
142
+ this.pos = 0;
143
+ return this;
144
+ }
139
145
  get size() {
140
146
  return this.pos;
141
147
  }
@@ -180,15 +186,17 @@ export class Reader {
180
186
  return val;
181
187
  }
182
188
  readUVarint() {
189
+ // Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
190
+ // Use multiplication instead of << to handle values > 32 bits
183
191
  let result = 0;
184
- let shift = 0;
192
+ let multiplier = 1;
185
193
  while (true) {
186
194
  const byte = this.readUInt8();
187
- result |= (byte & 0x7f) << shift;
195
+ result += (byte & 0x7f) * multiplier;
188
196
  if (byte < 0x80) {
189
197
  return result;
190
198
  }
191
- shift += 7;
199
+ multiplier *= 128;
192
200
  }
193
201
  }
194
202
  readVarint() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",