bin-serde 1.7.0 → 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 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;
@@ -207,15 +209,17 @@ export class Reader {
207
209
  }
208
210
 
209
211
  readUVarint() {
212
+ // Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
213
+ // Use multiplication instead of << to handle values > 32 bits
210
214
  let result = 0;
211
- let shift = 0;
215
+ let multiplier = 1;
212
216
  while (true) {
213
217
  const byte = this.readUInt8();
214
- result |= (byte & 0x7f) << shift;
218
+ result += (byte & 0x7f) * multiplier;
215
219
  if (byte < 0x80) {
216
220
  return result;
217
221
  }
218
- shift += 7;
222
+ multiplier *= 128;
219
223
  }
220
224
  }
221
225
 
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;
@@ -180,15 +182,17 @@ export class Reader {
180
182
  return val;
181
183
  }
182
184
  readUVarint() {
185
+ // Protobuf-style LEB128: little-endian, 7 bits per byte, MSB is continuation
186
+ // Use multiplication instead of << to handle values > 32 bits
183
187
  let result = 0;
184
- let shift = 0;
188
+ let multiplier = 1;
185
189
  while (true) {
186
190
  const byte = this.readUInt8();
187
- result |= (byte & 0x7f) << shift;
191
+ result += (byte & 0x7f) * multiplier;
188
192
  if (byte < 0x80) {
189
193
  return result;
190
194
  }
191
- shift += 7;
195
+ multiplier *= 128;
192
196
  }
193
197
  }
194
198
  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.1",
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",