bin-serde 1.4.2 → 1.5.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/.prettierrc ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "semi": true,
3
+ "trailingComma": "es5",
4
+ "singleQuote": false,
5
+ "printWidth": 120,
6
+ "tabWidth": 2,
7
+ "useTabs": false,
8
+ "arrowParens": "always"
9
+ }
package/index.ts CHANGED
@@ -3,118 +3,101 @@ import utf8Size from "utf8-buffer-size";
3
3
 
4
4
  export class Writer {
5
5
  private pos = 0;
6
- private view: DataView;
7
6
  private bytes: Uint8Array;
7
+ private _view: DataView | null = null;
8
8
 
9
- public constructor() {
10
- this.view = new DataView(new ArrayBuffer(64));
11
- this.bytes = new Uint8Array(this.view.buffer);
9
+ constructor(initialSize = 256) {
10
+ this.bytes = new Uint8Array(Math.max(initialSize, 16));
12
11
  }
13
12
 
14
- public writeUInt8(val: number) {
13
+ writeUInt8(val: number) {
15
14
  this.ensureSize(1);
16
- this.view.setUint8(this.pos, val);
17
- this.pos += 1;
15
+ this.bytes[this.pos++] = val;
18
16
  return this;
19
17
  }
20
18
 
21
- public writeUInt32(val: number) {
22
- this.ensureSize(4);
23
- this.view.setUint32(this.pos, val);
24
- this.pos += 4;
19
+ writeUInt16(val: number) {
20
+ this.ensureSize(2);
21
+ this.view.setUint16(this.pos, val);
22
+ this.pos += 2;
25
23
  return this;
26
24
  }
27
25
 
28
- public writeUInt64(val: bigint) {
29
- this.ensureSize(8);
30
- this.view.setBigUint64(this.pos, val);
31
- this.pos += 8;
26
+ writeUInt32(val: number) {
27
+ this.ensureSize(4);
28
+ this.view.setUint32(this.pos, val);
29
+ this.pos += 4;
32
30
  return this;
33
31
  }
34
32
 
35
- public writeUVarint(val: number) {
33
+ writeUVarint(val: number) {
36
34
  if (val < 0x80) {
37
- this.ensureSize(1);
38
- this.view.setUint8(this.pos, val);
39
- this.pos += 1;
35
+ this.writeUInt8(val);
40
36
  } else if (val < 0x4000) {
41
- this.ensureSize(2);
42
- this.view.setUint16(this.pos, (val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
43
- this.pos += 2;
37
+ this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
44
38
  } else if (val < 0x200000) {
45
- this.ensureSize(3);
46
- this.view.setUint8(this.pos, (val >> 14) | 0x80);
47
- this.view.setUint16(this.pos + 1, (val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
48
- this.pos += 3;
39
+ this.writeUInt8((val >> 14) | 0x80);
40
+ this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
49
41
  } else if (val < 0x10000000) {
50
- this.ensureSize(4);
51
- this.view.setUint32(
52
- this.pos,
42
+ this.writeUInt32(
53
43
  (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
54
44
  );
55
- this.pos += 4;
56
45
  } else if (val < 0x800000000) {
57
- this.ensureSize(5);
58
- this.view.setUint8(this.pos, Math.floor(val / Math.pow(2, 28)) | 0x80);
59
- this.view.setUint32(
60
- this.pos + 1,
46
+ this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
47
+ this.writeUInt32(
61
48
  (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
62
49
  );
63
- this.pos += 5;
64
50
  } else if (val < 0x40000000000) {
65
- this.ensureSize(6);
66
- const shiftedVal = Math.floor(val / Math.pow(2, 28));
67
- this.view.setUint16(this.pos, (shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
68
- this.view.setUint32(
69
- this.pos + 2,
51
+ const shiftedVal = Math.floor(val / 0x10000000);
52
+ this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
53
+ this.writeUInt32(
70
54
  (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
71
55
  );
72
- this.pos += 6;
73
56
  } else {
74
57
  throw new Error("Value out of range");
75
58
  }
76
59
  return this;
77
60
  }
78
61
 
79
- public writeVarint(val: number) {
80
- const bigval = BigInt(val);
81
- this.writeUVarint(Number((bigval >> 63n) ^ (bigval << 1n)));
62
+ writeVarint(val: number) {
63
+ this.writeUVarint((val << 1) ^ (val >> 31));
82
64
  return this;
83
65
  }
84
66
 
85
- public writeFloat(val: number) {
67
+ writeFloat(val: number) {
86
68
  this.ensureSize(4);
87
69
  this.view.setFloat32(this.pos, val, true);
88
70
  this.pos += 4;
89
71
  return this;
90
72
  }
91
73
 
92
- public writeBits(bits: boolean[]) {
74
+ writeBits(bits: boolean[]) {
75
+ const numBytes = Math.ceil(bits.length / 8);
76
+ this.ensureSize(numBytes);
93
77
  for (let i = 0; i < bits.length; i += 8) {
94
78
  let byte = 0;
95
- for (let j = 0; j < 8; j++) {
96
- if (i + j == bits.length) {
97
- break;
79
+ for (let j = 0; j < 8 && i + j < bits.length; j++) {
80
+ if (bits[i + j]) {
81
+ byte |= 1 << j;
98
82
  }
99
- byte |= (bits[i + j] ? 1 : 0) << j;
100
83
  }
101
- this.writeUInt8(byte);
84
+ this.bytes[this.pos++] = byte;
102
85
  }
103
86
  return this;
104
87
  }
105
88
 
106
- public writeStringAscii(val: string) {
89
+ writeStringAscii(val: string) {
107
90
  if (val.length === 0) {
108
91
  return this;
109
92
  }
110
93
  this.ensureSize(val.length);
111
94
  for (let i = 0; i < val.length; i++) {
112
- this.view.setUint8(this.pos++, val.charCodeAt(i));
95
+ this.bytes[this.pos++] = val.charCodeAt(i);
113
96
  }
114
97
  return this;
115
98
  }
116
99
 
117
- public writeStringUtf8(val: string, len?: number) {
100
+ writeStringUtf8(val: string, len?: number) {
118
101
  if (len != null) {
119
102
  if (len === 0) {
120
103
  return this;
@@ -136,60 +119,81 @@ export class Writer {
136
119
  return this;
137
120
  }
138
121
 
139
- public writeBuffer(buf: Uint8Array) {
122
+ writeBuffer(buf: Uint8Array) {
140
123
  this.ensureSize(buf.length);
141
124
  this.bytes.set(buf, this.pos);
142
125
  this.pos += buf.length;
143
126
  return this;
144
127
  }
145
128
 
146
- public toBuffer() {
129
+ concat(other: Writer) {
130
+ const otherLen = other.pos;
131
+ this.ensureSize(otherLen);
132
+ if (otherLen < 64) {
133
+ for (let i = 0; i < otherLen; i++) {
134
+ this.bytes[this.pos++] = other.bytes[i];
135
+ }
136
+ } else {
137
+ this.bytes.set(other.bytes.subarray(0, otherLen), this.pos);
138
+ this.pos += otherLen;
139
+ }
140
+ return this;
141
+ }
142
+
143
+ toBuffer() {
147
144
  return this.bytes.subarray(0, this.pos);
148
145
  }
149
146
 
147
+ get size(): number {
148
+ return this.pos;
149
+ }
150
+
150
151
  private ensureSize(size: number) {
151
- while (this.view.byteLength < this.pos + size) {
152
- const newView = new DataView(new ArrayBuffer(this.view.byteLength * 2));
153
- const newBytes = new Uint8Array(newView.buffer);
154
- newBytes.set(this.bytes);
155
- this.view = newView;
156
- this.bytes = newBytes;
152
+ if (this.bytes.length >= this.pos + size) {
153
+ return;
154
+ }
155
+ let newSize = this.bytes.length * 2;
156
+ while (newSize < this.pos + size) {
157
+ newSize *= 2;
157
158
  }
159
+ const newBytes = new Uint8Array(newSize);
160
+ newBytes.set(this.bytes);
161
+ this.bytes = newBytes;
162
+ this._view = null;
163
+ }
164
+
165
+ private get view(): DataView {
166
+ if (!this._view) {
167
+ this._view = new DataView(this.bytes.buffer);
168
+ }
169
+ return this._view;
158
170
  }
159
171
  }
160
172
 
161
173
  export class Reader {
162
174
  private pos = 0;
163
- private view: DataView;
164
175
  private bytes: Uint8Array;
176
+ private view: DataView;
165
177
 
166
- public constructor(buf: ArrayBufferView) {
178
+ constructor(buf: Uint8Array) {
179
+ this.bytes = buf;
167
180
  this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
168
- this.bytes = new Uint8Array(this.view.buffer, buf.byteOffset, buf.byteLength);
169
181
  }
170
182
 
171
- public readUInt8() {
172
- const val = this.view.getUint8(this.pos);
173
- this.pos += 1;
174
- return val;
183
+ readUInt8() {
184
+ return this.bytes[this.pos++];
175
185
  }
176
186
 
177
- public readUInt32() {
187
+ readUInt32() {
178
188
  const val = this.view.getUint32(this.pos);
179
189
  this.pos += 4;
180
190
  return val;
181
191
  }
182
192
 
183
- public readUInt64() {
184
- const val = this.view.getBigUint64(this.pos);
185
- this.pos += 8;
186
- return val;
187
- }
188
-
189
- public readUVarint() {
193
+ readUVarint() {
190
194
  let val = 0;
191
195
  while (true) {
192
- let byte = this.view.getUint8(this.pos++);
196
+ const byte = this.readUInt8();
193
197
  if (byte < 0x80) {
194
198
  return val + byte;
195
199
  }
@@ -197,42 +201,41 @@ export class Reader {
197
201
  }
198
202
  }
199
203
 
200
- public readVarint() {
201
- const val = BigInt(this.readUVarint());
202
- return Number((val >> 1n) ^ -(val & 1n));
204
+ readVarint() {
205
+ const val = this.readUVarint();
206
+ return (val >>> 1) ^ -(val & 1);
203
207
  }
204
208
 
205
- public readFloat() {
209
+ readFloat() {
206
210
  const val = this.view.getFloat32(this.pos, true);
207
211
  this.pos += 4;
208
212
  return val;
209
213
  }
210
214
 
211
- public readBits(numBits: number) {
215
+ readBits(numBits: number) {
212
216
  const numBytes = Math.ceil(numBits / 8);
213
- const bytes = this.bytes.slice(this.pos, this.pos + numBytes);
214
217
  const bits: boolean[] = [];
215
- for (const byte of bytes) {
216
- for (let i = 0; i < 8 && bits.length < numBits; i++) {
217
- bits.push(((byte >> i) & 1) === 1);
218
+ for (let i = 0; i < numBytes; i++) {
219
+ const byte = this.readUInt8();
220
+ for (let j = 0; j < 8 && bits.length < numBits; j++) {
221
+ bits.push(((byte >> j) & 1) === 1);
218
222
  }
219
223
  }
220
- this.pos += numBytes;
221
224
  return bits;
222
225
  }
223
226
 
224
- public readStringAscii(len: number) {
227
+ readStringAscii(len: number) {
225
228
  if (len === 0) {
226
229
  return "";
227
230
  }
228
231
  let val = "";
229
232
  for (let i = 0; i < len; i++) {
230
- val += String.fromCharCode(this.view.getUint8(this.pos++));
233
+ val += String.fromCharCode(this.readUInt8());
231
234
  }
232
235
  return val;
233
236
  }
234
237
 
235
- public readStringUtf8(len?: number) {
238
+ readStringUtf8(len?: number) {
236
239
  if (len == null) {
237
240
  len = this.readUVarint();
238
241
  }
@@ -244,13 +247,13 @@ export class Reader {
244
247
  return val;
245
248
  }
246
249
 
247
- public readBuffer(numBytes: number) {
250
+ readBuffer(numBytes: number) {
248
251
  const bytes = this.bytes.slice(this.pos, this.pos + numBytes);
249
252
  this.pos += numBytes;
250
253
  return bytes;
251
254
  }
252
255
 
253
- public remaining() {
254
- return this.view.byteLength - this.pos;
256
+ remaining() {
257
+ return this.bytes.length - this.pos;
255
258
  }
256
259
  }
package/lib/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  export declare class Writer {
2
2
  private pos;
3
- private view;
4
3
  private bytes;
5
- constructor();
4
+ private _view;
5
+ constructor(initialSize?: number);
6
6
  writeUInt8(val: number): this;
7
+ writeUInt16(val: number): this;
7
8
  writeUInt32(val: number): this;
8
- writeUInt64(val: bigint): this;
9
9
  writeUVarint(val: number): this;
10
10
  writeVarint(val: number): this;
11
11
  writeFloat(val: number): this;
@@ -13,17 +13,19 @@ export declare class Writer {
13
13
  writeStringAscii(val: string): this;
14
14
  writeStringUtf8(val: string, len?: number): this;
15
15
  writeBuffer(buf: Uint8Array): this;
16
+ concat(other: Writer): this;
16
17
  toBuffer(): Uint8Array;
18
+ get size(): number;
17
19
  private ensureSize;
20
+ private get view();
18
21
  }
19
22
  export declare class Reader {
20
23
  private pos;
21
- private view;
22
24
  private bytes;
23
- constructor(buf: ArrayBufferView);
25
+ private view;
26
+ constructor(buf: Uint8Array);
24
27
  readUInt8(): number;
25
28
  readUInt32(): number;
26
- readUInt64(): bigint;
27
29
  readUVarint(): number;
28
30
  readVarint(): number;
29
31
  readFloat(): number;
package/lib/index.js CHANGED
@@ -2,16 +2,20 @@ import { pack, unpack } from "./utf8-buffer.js";
2
2
  import utf8Size from "utf8-buffer-size";
3
3
  export class Writer {
4
4
  pos = 0;
5
- view;
6
5
  bytes;
7
- constructor() {
8
- this.view = new DataView(new ArrayBuffer(64));
9
- this.bytes = new Uint8Array(this.view.buffer);
6
+ _view = null;
7
+ constructor(initialSize = 256) {
8
+ this.bytes = new Uint8Array(Math.max(initialSize, 16));
10
9
  }
11
10
  writeUInt8(val) {
12
11
  this.ensureSize(1);
13
- this.view.setUint8(this.pos, val);
14
- this.pos += 1;
12
+ this.bytes[this.pos++] = val;
13
+ return this;
14
+ }
15
+ writeUInt16(val) {
16
+ this.ensureSize(2);
17
+ this.view.setUint16(this.pos, val);
18
+ this.pos += 2;
15
19
  return this;
16
20
  }
17
21
  writeUInt32(val) {
@@ -20,46 +24,28 @@ export class Writer {
20
24
  this.pos += 4;
21
25
  return this;
22
26
  }
23
- writeUInt64(val) {
24
- this.ensureSize(8);
25
- this.view.setBigUint64(this.pos, val);
26
- this.pos += 8;
27
- return this;
28
- }
29
27
  writeUVarint(val) {
30
28
  if (val < 0x80) {
31
- this.ensureSize(1);
32
- this.view.setUint8(this.pos, val);
33
- this.pos += 1;
29
+ this.writeUInt8(val);
34
30
  }
35
31
  else if (val < 0x4000) {
36
- this.ensureSize(2);
37
- this.view.setUint16(this.pos, (val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
38
- this.pos += 2;
32
+ this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
39
33
  }
40
34
  else if (val < 0x200000) {
41
- this.ensureSize(3);
42
- this.view.setUint8(this.pos, (val >> 14) | 0x80);
43
- this.view.setUint16(this.pos + 1, (val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
44
- this.pos += 3;
35
+ this.writeUInt8((val >> 14) | 0x80);
36
+ this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
45
37
  }
46
38
  else if (val < 0x10000000) {
47
- this.ensureSize(4);
48
- this.view.setUint32(this.pos, (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
49
- this.pos += 4;
39
+ this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
50
40
  }
51
41
  else if (val < 0x800000000) {
52
- this.ensureSize(5);
53
- this.view.setUint8(this.pos, Math.floor(val / Math.pow(2, 28)) | 0x80);
54
- this.view.setUint32(this.pos + 1, (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
55
- this.pos += 5;
42
+ this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
43
+ this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
56
44
  }
57
45
  else if (val < 0x40000000000) {
58
- this.ensureSize(6);
59
- const shiftedVal = Math.floor(val / Math.pow(2, 28));
60
- this.view.setUint16(this.pos, (shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
61
- this.view.setUint32(this.pos + 2, (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
62
- this.pos += 6;
46
+ const shiftedVal = Math.floor(val / 0x10000000);
47
+ this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
48
+ this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
63
49
  }
64
50
  else {
65
51
  throw new Error("Value out of range");
@@ -67,8 +53,7 @@ export class Writer {
67
53
  return this;
68
54
  }
69
55
  writeVarint(val) {
70
- const bigval = BigInt(val);
71
- this.writeUVarint(Number((bigval >> 63n) ^ (bigval << 1n)));
56
+ this.writeUVarint((val << 1) ^ (val >> 31));
72
57
  return this;
73
58
  }
74
59
  writeFloat(val) {
@@ -78,15 +63,16 @@ export class Writer {
78
63
  return this;
79
64
  }
80
65
  writeBits(bits) {
66
+ const numBytes = Math.ceil(bits.length / 8);
67
+ this.ensureSize(numBytes);
81
68
  for (let i = 0; i < bits.length; i += 8) {
82
69
  let byte = 0;
83
- for (let j = 0; j < 8; j++) {
84
- if (i + j == bits.length) {
85
- break;
70
+ for (let j = 0; j < 8 && i + j < bits.length; j++) {
71
+ if (bits[i + j]) {
72
+ byte |= 1 << j;
86
73
  }
87
- byte |= (bits[i + j] ? 1 : 0) << j;
88
74
  }
89
- this.writeUInt8(byte);
75
+ this.bytes[this.pos++] = byte;
90
76
  }
91
77
  return this;
92
78
  }
@@ -96,7 +82,7 @@ export class Writer {
96
82
  }
97
83
  this.ensureSize(val.length);
98
84
  for (let i = 0; i < val.length; i++) {
99
- this.view.setUint8(this.pos++, val.charCodeAt(i));
85
+ this.bytes[this.pos++] = val.charCodeAt(i);
100
86
  }
101
87
  return this;
102
88
  }
@@ -127,46 +113,66 @@ export class Writer {
127
113
  this.pos += buf.length;
128
114
  return this;
129
115
  }
116
+ concat(other) {
117
+ const otherLen = other.pos;
118
+ this.ensureSize(otherLen);
119
+ if (otherLen < 64) {
120
+ for (let i = 0; i < otherLen; i++) {
121
+ this.bytes[this.pos++] = other.bytes[i];
122
+ }
123
+ }
124
+ else {
125
+ this.bytes.set(other.bytes.subarray(0, otherLen), this.pos);
126
+ this.pos += otherLen;
127
+ }
128
+ return this;
129
+ }
130
130
  toBuffer() {
131
131
  return this.bytes.subarray(0, this.pos);
132
132
  }
133
+ get size() {
134
+ return this.pos;
135
+ }
133
136
  ensureSize(size) {
134
- while (this.view.byteLength < this.pos + size) {
135
- const newView = new DataView(new ArrayBuffer(this.view.byteLength * 2));
136
- const newBytes = new Uint8Array(newView.buffer);
137
- newBytes.set(this.bytes);
138
- this.view = newView;
139
- this.bytes = newBytes;
137
+ if (this.bytes.length >= this.pos + size) {
138
+ return;
139
+ }
140
+ let newSize = this.bytes.length * 2;
141
+ while (newSize < this.pos + size) {
142
+ newSize *= 2;
143
+ }
144
+ const newBytes = new Uint8Array(newSize);
145
+ newBytes.set(this.bytes);
146
+ this.bytes = newBytes;
147
+ this._view = null;
148
+ }
149
+ get view() {
150
+ if (!this._view) {
151
+ this._view = new DataView(this.bytes.buffer);
140
152
  }
153
+ return this._view;
141
154
  }
142
155
  }
143
156
  export class Reader {
144
157
  pos = 0;
145
- view;
146
158
  bytes;
159
+ view;
147
160
  constructor(buf) {
161
+ this.bytes = buf;
148
162
  this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
149
- this.bytes = new Uint8Array(this.view.buffer, buf.byteOffset, buf.byteLength);
150
163
  }
151
164
  readUInt8() {
152
- const val = this.view.getUint8(this.pos);
153
- this.pos += 1;
154
- return val;
165
+ return this.bytes[this.pos++];
155
166
  }
156
167
  readUInt32() {
157
168
  const val = this.view.getUint32(this.pos);
158
169
  this.pos += 4;
159
170
  return val;
160
171
  }
161
- readUInt64() {
162
- const val = this.view.getBigUint64(this.pos);
163
- this.pos += 8;
164
- return val;
165
- }
166
172
  readUVarint() {
167
173
  let val = 0;
168
174
  while (true) {
169
- let byte = this.view.getUint8(this.pos++);
175
+ const byte = this.readUInt8();
170
176
  if (byte < 0x80) {
171
177
  return val + byte;
172
178
  }
@@ -174,8 +180,8 @@ export class Reader {
174
180
  }
175
181
  }
176
182
  readVarint() {
177
- const val = BigInt(this.readUVarint());
178
- return Number((val >> 1n) ^ -(val & 1n));
183
+ const val = this.readUVarint();
184
+ return (val >>> 1) ^ -(val & 1);
179
185
  }
180
186
  readFloat() {
181
187
  const val = this.view.getFloat32(this.pos, true);
@@ -184,14 +190,13 @@ export class Reader {
184
190
  }
185
191
  readBits(numBits) {
186
192
  const numBytes = Math.ceil(numBits / 8);
187
- const bytes = this.bytes.slice(this.pos, this.pos + numBytes);
188
193
  const bits = [];
189
- for (const byte of bytes) {
190
- for (let i = 0; i < 8 && bits.length < numBits; i++) {
191
- bits.push(((byte >> i) & 1) === 1);
194
+ for (let i = 0; i < numBytes; i++) {
195
+ const byte = this.readUInt8();
196
+ for (let j = 0; j < 8 && bits.length < numBits; j++) {
197
+ bits.push(((byte >> j) & 1) === 1);
192
198
  }
193
199
  }
194
- this.pos += numBytes;
195
200
  return bits;
196
201
  }
197
202
  readStringAscii(len) {
@@ -200,7 +205,7 @@ export class Reader {
200
205
  }
201
206
  let val = "";
202
207
  for (let i = 0; i < len; i++) {
203
- val += String.fromCharCode(this.view.getUint8(this.pos++));
208
+ val += String.fromCharCode(this.readUInt8());
204
209
  }
205
210
  return val;
206
211
  }
@@ -221,6 +226,6 @@ export class Reader {
221
226
  return bytes;
222
227
  }
223
228
  remaining() {
224
- return this.view.byteLength - this.pos;
229
+ return this.bytes.length - this.pos;
225
230
  }
226
231
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
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",