@nuintun/buffer 0.3.0 → 0.3.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/cjs/Binary.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
package/cjs/enum.cjs ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @package @nuintun/buffer
3
+ * @license MIT
4
+ * @version 0.3.2
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A buffer tool for javascript.
7
+ * @see https://github.com/nuintun/Buffer#readme
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ /**
13
+ * @module enum
14
+ */
15
+ // 字节序类型
16
+ exports.Endian = void 0;
17
+ (function (Endian) {
18
+ Endian[(Endian['Big'] = 0)] = 'Big';
19
+ Endian[(Endian['Little'] = 1)] = 'Little';
20
+ })(exports.Endian || (exports.Endian = {}));
package/cjs/enum.d.cts CHANGED
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * @module enum
3
3
  */
4
+ export declare enum Endian {
5
+ Big = 0,
6
+ Little = 1
7
+ }
4
8
  export declare const enum SizeOf {
5
9
  INT8 = 1,
6
10
  UINT8 = 1,
package/cjs/errors.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
@@ -12,24 +12,28 @@
12
12
  /**
13
13
  * @module errors
14
14
  */
15
- // 未支持的编码格式
15
+ /**
16
+ * @function encodingInvalid
17
+ * @description 未支持的编码格式
18
+ * @param encoding 编码格式
19
+ */
16
20
  function encodingInvalid(encoding) {
17
21
  return 'unsupported encoding ' + encoding;
18
22
  }
19
- // 未知字节序
20
- const unknownEndianness = 'unknown endianness';
21
- // 非法长度
22
- const lengthInvalid = 'invalid buffer length';
23
23
  // 非法读写指针
24
24
  const offsetInvalid = 'invalid buffer offset';
25
+ // 非法长度
26
+ const lengthInvalid = 'invalid buffer length';
27
+ // 未知字节序
28
+ const unknownEndianness = 'unknown endianness';
29
+ // 数据读取长度非法
30
+ const readLengthInvalid = 'invalid read length';
25
31
  // 数据读取溢出
26
32
  const readOverflow = 'read is outside the bounds of the Buffer';
27
- // 读写指针溢出
28
- const offsetOverflow = 'offset is outside the bounds of the Buffer';
29
33
 
30
34
  exports.encodingInvalid = encodingInvalid;
31
35
  exports.lengthInvalid = lengthInvalid;
32
36
  exports.offsetInvalid = offsetInvalid;
33
- exports.offsetOverflow = offsetOverflow;
37
+ exports.readLengthInvalid = readLengthInvalid;
34
38
  exports.readOverflow = readOverflow;
35
39
  exports.unknownEndianness = unknownEndianness;
package/cjs/errors.d.cts CHANGED
@@ -1,9 +1,15 @@
1
1
  /**
2
2
  * @module errors
3
3
  */
4
+ /**
5
+ * @function encodingInvalid
6
+ * @description 未支持的编码格式
7
+ * @param encoding 编码格式
8
+ */
4
9
  export declare function encodingInvalid(encoding: string): string;
5
- export declare const unknownEndianness = 'unknown endianness';
6
- export declare const lengthInvalid = 'invalid buffer length';
7
10
  export declare const offsetInvalid = 'invalid buffer offset';
11
+ export declare const lengthInvalid = 'invalid buffer length';
12
+ export declare const unknownEndianness = 'unknown endianness';
13
+ export declare const readLengthInvalid = 'invalid read length';
8
14
  export declare const readOverflow = 'read is outside the bounds of the Buffer';
9
15
  export declare const offsetOverflow = 'offset is outside the bounds of the Buffer';
package/cjs/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
@@ -9,31 +9,26 @@
9
9
 
10
10
  'use strict';
11
11
 
12
- const utils = require('./utils.cjs');
13
12
  const Binary = require('./Binary.cjs');
14
13
  const errors = require('./errors.cjs');
15
14
  const index = require('./Encoding/index.cjs');
15
+ const _enum = require('./enum.cjs');
16
+ const utils = require('./utils.cjs');
16
17
 
17
18
  /**
18
19
  * @module Buffer
19
20
  */
20
- // 字节序类型
21
- exports.Endian = void 0;
22
- (function (Endian) {
23
- Endian[(Endian['Big'] = 0)] = 'Big';
24
- Endian[(Endian['Little'] = 1)] = 'Little';
25
- })(exports.Endian || (exports.Endian = {}));
26
21
  /**
27
22
  * @function endianness
28
23
  * @description 获取系统默认字节序
29
24
  * @returns {Endian}
30
25
  */
31
26
  function endianness() {
32
- switch (new Uint8Array(new Uint32Array([0x12345678]))[0]) {
33
- case 0x12:
34
- return exports.Endian.Big;
35
- case 0x78:
36
- return exports.Endian.Little;
27
+ switch (new Uint8Array(new Uint16Array([0x00ff]).buffer)[0]) {
28
+ case 0x00:
29
+ return _enum.Endian.Big;
30
+ case 0xff:
31
+ return _enum.Endian.Little;
37
32
  default:
38
33
  throw new TypeError(errors.unknownEndianness);
39
34
  }
@@ -54,45 +49,64 @@ class Buffer {
54
49
  #offset = 0;
55
50
  // 已使用字节长度
56
51
  #length = 0;
52
+ /**
53
+ * @constructor
54
+ * @param {number | Uint8Array | ArrayBuffer} [input] 缓冲区初始配置
55
+ * @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
56
+ */
57
57
  constructor(input = 0, pageSize = 4096) {
58
- this.#pageSize = pageSize;
59
- if (input instanceof Uint8Array) {
60
- this.#bytes = input;
61
- this.#length = input.length;
62
- this.#dataView = new DataView(input.buffer);
58
+ let length;
59
+ let bytes;
60
+ if (utils.isTypedArray(input)) {
61
+ length = input.byteLength;
62
+ bytes = utils.makeUint8Array(length, pageSize);
63
+ if (length > 0) {
64
+ bytes.set(new Uint8Array(input.buffer));
65
+ }
66
+ } else if (input instanceof ArrayBuffer) {
67
+ length = input.byteLength;
68
+ bytes = utils.makeUint8Array(length, pageSize);
69
+ if (length > 0) {
70
+ bytes.set(new Uint8Array(input));
71
+ }
63
72
  } else {
64
- const bytes = utils.makeUint8Array(input, pageSize);
65
- this.#bytes = bytes;
66
- this.#length = input;
67
- this.#dataView = new DataView(bytes.buffer);
73
+ length = input;
74
+ bytes = utils.makeUint8Array(length, pageSize);
68
75
  }
76
+ this.#bytes = bytes;
77
+ this.#length = length;
78
+ this.#pageSize = pageSize;
79
+ this.#dataView = new DataView(bytes.buffer);
69
80
  }
70
81
  /**
71
82
  * @private
72
- * @method grow
73
- * @description 增加长度
74
- * @param {number} length 长度增加量
83
+ * @method seek
84
+ * @description 移动读写指针
85
+ * @param {number} offset 指针位置
75
86
  */
76
- #grow(length) {
77
- this.#length += length;
87
+ #seek(offset) {
88
+ if (offset > this.#length) {
89
+ this.#length = offset;
90
+ }
91
+ this.#offset = offset;
78
92
  }
79
93
  /**
80
94
  * @private
81
- * @method seek
82
- * @description 移动读写指针
83
- * @param {number} offset 指针偏移量
95
+ * @method getOffset
96
+ * @description 根据数据类型获取最新指针位置
97
+ * @param size 数据类型长度
84
98
  */
85
- #seek(offset) {
86
- this.#offset += offset;
99
+ #getOffset(size) {
100
+ return this.#offset + size;
87
101
  }
88
102
  /**
89
103
  * @private
90
104
  * @method assertRead
91
105
  * @description 读取断言,防止越界读取
92
- * @param {number} length 断言字节长度
106
+ * @param {number} size 断言字节长度
93
107
  */
94
108
  #assertRead(length) {
95
- if (length < 0 || this.#offset + length > this.#length) {
109
+ if (length > this.#length) {
96
110
  throw new RangeError(errors.readOverflow);
97
111
  }
98
112
  }
@@ -103,7 +117,6 @@ class Buffer {
103
117
  * @param {number} length 分配字节长度
104
118
  */
105
119
  #alloc(length) {
106
- length += this.#offset;
107
120
  const bytes = this.#bytes;
108
121
  if (length > bytes.length) {
109
122
  const newBytes = utils.makeUint8Array(length, this.#pageSize);
@@ -119,12 +132,9 @@ class Buffer {
119
132
  * @description 下一次调用读写方法时将在此位置开始读写
120
133
  */
121
134
  set offset(offset) {
122
- if (offset < 0) {
135
+ if (!utils.isNaturalNumber(offset)) {
123
136
  throw new RangeError(errors.offsetInvalid);
124
137
  }
125
- if (offset > this.#length) {
126
- throw new RangeError(errors.offsetOverflow);
127
- }
128
138
  this.#offset = offset;
129
139
  }
130
140
  /**
@@ -144,7 +154,7 @@ class Buffer {
144
154
  * @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
145
155
  */
146
156
  set length(length) {
147
- if (length < 0) {
157
+ if (!utils.isNaturalNumber(length)) {
148
158
  throw new RangeError(errors.lengthInvalid);
149
159
  }
150
160
  const currentLength = this.#length;
@@ -171,16 +181,16 @@ class Buffer {
171
181
  /**
172
182
  * @public
173
183
  * @property {ArrayBuffer} buffer
174
- * @description 获取 ArrayBuffer 缓冲区
184
+ * @description 获取全部 ArrayBuffer 原始缓冲区
175
185
  * @returns {ArrayBuffer}
176
186
  */
177
187
  get buffer() {
178
- return this.bytes.buffer;
188
+ return this.#bytes.buffer;
179
189
  }
180
190
  /**
181
191
  * @public
182
192
  * @property {Uint8Array} bytes
183
- * @description 获取 Uint8Array 缓冲区
193
+ * @description 获取已写入 Uint8Array 原始缓冲区
184
194
  * @returns {Uint8Array}
185
195
  */
186
196
  get bytes() {
@@ -193,10 +203,10 @@ class Buffer {
193
203
  * @param {number} value 介于 -128 和 127 之间的整数
194
204
  */
195
205
  writeInt8(value) {
196
- this.#alloc(1 /* SizeOf.INT8 */);
206
+ const offset = this.#getOffset(1 /* SizeOf.INT8 */);
207
+ this.#alloc(offset);
197
208
  this.#dataView.setInt8(this.#offset, value);
198
- this.#grow(1 /* SizeOf.INT8 */);
199
- this.#seek(1 /* SizeOf.INT8 */);
209
+ this.#seek(offset);
200
210
  }
201
211
  /**
202
212
  * @public
@@ -205,10 +215,10 @@ class Buffer {
205
215
  * @param {number} value 介于 0 和 255 之间的整数
206
216
  */
207
217
  writeUint8(value) {
208
- this.#alloc(1 /* SizeOf.UINT8 */);
218
+ const offset = this.#getOffset(1 /* SizeOf.UINT8 */);
219
+ this.#alloc(offset);
209
220
  this.#dataView.setUint8(this.#offset, value);
210
- this.#grow(1 /* SizeOf.UINT8 */);
211
- this.#seek(1 /* SizeOf.UINT8 */);
221
+ this.#seek(offset);
212
222
  }
213
223
  /**
214
224
  * @method writeBoolean
@@ -225,10 +235,10 @@ class Buffer {
225
235
  * @param {boolean} [littleEndian] 是否为小端字节序
226
236
  */
227
237
  writeInt16(value, littleEndian) {
228
- this.#alloc(2 /* SizeOf.INT16 */);
238
+ const offset = this.#getOffset(2 /* SizeOf.INT16 */);
239
+ this.#alloc(offset);
229
240
  this.#dataView.setInt16(this.#offset, value, littleEndian);
230
- this.#grow(2 /* SizeOf.INT16 */);
231
- this.#seek(2 /* SizeOf.INT16 */);
241
+ this.#seek(offset);
232
242
  }
233
243
  /**
234
244
  * @method writeUint16
@@ -237,10 +247,10 @@ class Buffer {
237
247
  * @param {boolean} [littleEndian] 是否为小端字节序
238
248
  */
239
249
  writeUint16(value, littleEndian) {
240
- this.#alloc(2 /* SizeOf.UINT16 */);
250
+ const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
251
+ this.#alloc(offset);
241
252
  this.#dataView.setUint16(this.#offset, value, littleEndian);
242
- this.#grow(2 /* SizeOf.UINT16 */);
243
- this.#seek(2 /* SizeOf.UINT16 */);
253
+ this.#seek(offset);
244
254
  }
245
255
  /**
246
256
  * @method writeInt32
@@ -249,10 +259,10 @@ class Buffer {
249
259
  * @param {boolean} [littleEndian] 是否为小端字节序
250
260
  */
251
261
  writeInt32(value, littleEndian) {
252
- this.#alloc(4 /* SizeOf.INT32 */);
262
+ const offset = this.#getOffset(4 /* SizeOf.INT32 */);
263
+ this.#alloc(offset);
253
264
  this.#dataView.setInt32(this.#offset, value, littleEndian);
254
- this.#grow(4 /* SizeOf.INT32 */);
255
- this.#seek(4 /* SizeOf.INT32 */);
265
+ this.#seek(offset);
256
266
  }
257
267
  /**
258
268
  * @method writeUint32
@@ -261,10 +271,10 @@ class Buffer {
261
271
  * @param {boolean} [littleEndian] 是否为小端字节序
262
272
  */
263
273
  writeUint32(value, littleEndian) {
264
- this.#alloc(4 /* SizeOf.UINT32 */);
274
+ const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
275
+ this.#alloc(offset);
265
276
  this.#dataView.setUint32(this.#offset, value, littleEndian);
266
- this.#grow(4 /* SizeOf.UINT32 */);
267
- this.#seek(4 /* SizeOf.UINT32 */);
277
+ this.#seek(offset);
268
278
  }
269
279
  /**
270
280
  * @method writeInt64
@@ -273,10 +283,10 @@ class Buffer {
273
283
  * @param {boolean} [littleEndian] 是否为小端字节序
274
284
  */
275
285
  writeInt64(value, littleEndian) {
276
- this.#alloc(8 /* SizeOf.INT64 */);
286
+ const offset = this.#getOffset(8 /* SizeOf.INT64 */);
287
+ this.#alloc(offset);
277
288
  this.#dataView.setBigInt64(this.#offset, value, littleEndian);
278
- this.#grow(8 /* SizeOf.INT64 */);
279
- this.#seek(8 /* SizeOf.INT64 */);
289
+ this.#seek(offset);
280
290
  }
281
291
  /**
282
292
  * @method writeUint64
@@ -285,10 +295,10 @@ class Buffer {
285
295
  * @param {boolean} [littleEndian] 是否为小端字节序
286
296
  */
287
297
  writeUint64(value, littleEndian) {
288
- this.#alloc(8 /* SizeOf.UINT64 */);
298
+ const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
299
+ this.#alloc(offset);
289
300
  this.#dataView.setBigUint64(this.#offset, value, littleEndian);
290
- this.#grow(8 /* SizeOf.UINT64 */);
291
- this.#seek(8 /* SizeOf.UINT64 */);
301
+ this.#seek(offset);
292
302
  }
293
303
  /**
294
304
  * @method writeFloat32
@@ -297,10 +307,10 @@ class Buffer {
297
307
  * @param {boolean} [littleEndian] 是否为小端字节序
298
308
  */
299
309
  writeFloat32(value, littleEndian) {
300
- this.#alloc(4 /* SizeOf.FLOAT32 */);
310
+ const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
311
+ this.#alloc(offset);
301
312
  this.#dataView.setFloat32(this.#offset, value, littleEndian);
302
- this.#grow(4 /* SizeOf.FLOAT32 */);
303
- this.#seek(4 /* SizeOf.FLOAT32 */);
313
+ this.#seek(offset);
304
314
  }
305
315
  /**
306
316
  * @method writeFloat64
@@ -309,10 +319,10 @@ class Buffer {
309
319
  * @param {boolean} [littleEndian] 是否为小端字节序
310
320
  */
311
321
  writeFloat64(value, littleEndian) {
312
- this.#alloc(8 /* SizeOf.FLOAT64 */);
322
+ const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
323
+ this.#alloc(offset);
313
324
  this.#dataView.setFloat64(this.#offset, value, littleEndian);
314
- this.#grow(8 /* SizeOf.FLOAT64 */);
315
- this.#seek(8 /* SizeOf.FLOAT64 */);
325
+ this.#seek(offset);
316
326
  }
317
327
  write(input, start, end) {
318
328
  let bytes;
@@ -323,10 +333,10 @@ class Buffer {
323
333
  }
324
334
  const { length } = bytes;
325
335
  if (length > 0) {
326
- this.#alloc(length);
336
+ const offset = this.#getOffset(length);
337
+ this.#alloc(offset);
327
338
  this.#bytes.set(bytes, this.#offset);
328
- this.#grow(length);
329
- this.#seek(length);
339
+ this.#seek(offset);
330
340
  }
331
341
  }
332
342
  /**
@@ -335,9 +345,10 @@ class Buffer {
335
345
  * @returns {number} 介于 -128 和 127 之间的整数
336
346
  */
337
347
  readInt8() {
338
- this.#assertRead(1 /* SizeOf.INT8 */);
348
+ const offset = this.#getOffset(1 /* SizeOf.INT8 */);
349
+ this.#assertRead(offset);
339
350
  const value = this.#dataView.getInt8(this.#offset);
340
- this.#seek(1 /* SizeOf.INT8 */);
351
+ this.#seek(offset);
341
352
  return value;
342
353
  }
343
354
  /**
@@ -346,9 +357,10 @@ class Buffer {
346
357
  * @returns {number} 介于 0 和 255 之间的无符号整数
347
358
  */
348
359
  readUint8() {
349
- this.#assertRead(1 /* SizeOf.UINT8 */);
360
+ const offset = this.#getOffset(1 /* SizeOf.UINT8 */);
361
+ this.#assertRead(offset);
350
362
  const value = this.#dataView.getUint8(this.#offset);
351
- this.#seek(1 /* SizeOf.UINT8 */);
363
+ this.#seek(offset);
352
364
  return value;
353
365
  }
354
366
  /**
@@ -366,9 +378,10 @@ class Buffer {
366
378
  * @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
367
379
  */
368
380
  readInt16(littleEndian) {
369
- this.#assertRead(2 /* SizeOf.INT16 */);
381
+ const offset = this.#getOffset(2 /* SizeOf.INT16 */);
382
+ this.#assertRead(offset);
370
383
  const value = this.#dataView.getInt16(this.#offset, littleEndian);
371
- this.#seek(2 /* SizeOf.INT16 */);
384
+ this.#seek(offset);
372
385
  return value;
373
386
  }
374
387
  /**
@@ -378,9 +391,10 @@ class Buffer {
378
391
  * @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
379
392
  */
380
393
  readUint16(littleEndian) {
381
- this.#assertRead(2 /* SizeOf.UINT16 */);
394
+ const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
395
+ this.#assertRead(offset);
382
396
  const value = this.#dataView.getUint16(this.#offset, littleEndian);
383
- this.#seek(2 /* SizeOf.UINT16 */);
397
+ this.#seek(offset);
384
398
  return value;
385
399
  }
386
400
  /**
@@ -390,9 +404,10 @@ class Buffer {
390
404
  * @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
391
405
  */
392
406
  readInt32(littleEndian) {
393
- this.#assertRead(4 /* SizeOf.INT32 */);
407
+ const offset = this.#getOffset(4 /* SizeOf.INT32 */);
408
+ this.#assertRead(offset);
394
409
  const value = this.#dataView.getInt32(this.#offset, littleEndian);
395
- this.#seek(4 /* SizeOf.INT32 */);
410
+ this.#seek(offset);
396
411
  return value;
397
412
  }
398
413
  /**
@@ -402,9 +417,10 @@ class Buffer {
402
417
  * @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
403
418
  */
404
419
  readUint32(littleEndian) {
405
- this.#assertRead(4 /* SizeOf.UINT32 */);
420
+ const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
421
+ this.#assertRead(offset);
406
422
  const value = this.#dataView.getUint32(this.#offset, littleEndian);
407
- this.#seek(4 /* SizeOf.UINT32 */);
423
+ this.#seek(offset);
408
424
  return value;
409
425
  }
410
426
  /**
@@ -414,9 +430,10 @@ class Buffer {
414
430
  * @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
415
431
  */
416
432
  readInt64(littleEndian) {
417
- this.#assertRead(8 /* SizeOf.INT64 */);
433
+ const offset = this.#getOffset(8 /* SizeOf.INT64 */);
434
+ this.#assertRead(offset);
418
435
  const value = this.#dataView.getBigInt64(this.#offset, littleEndian);
419
- this.#seek(8 /* SizeOf.INT64 */);
436
+ this.#seek(offset);
420
437
  return value;
421
438
  }
422
439
  /**
@@ -426,9 +443,10 @@ class Buffer {
426
443
  * @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
427
444
  */
428
445
  readUint64(littleEndian) {
429
- this.#assertRead(8 /* SizeOf.UINT64 */);
446
+ const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
447
+ this.#assertRead(offset);
430
448
  const value = this.#dataView.getBigUint64(this.#offset, littleEndian);
431
- this.#seek(8 /* SizeOf.UINT64 */);
449
+ this.#seek(offset);
432
450
  return value;
433
451
  }
434
452
  /**
@@ -438,9 +456,10 @@ class Buffer {
438
456
  * @returns {number} 单精度 32 位浮点数
439
457
  */
440
458
  readFloat32(littleEndian) {
441
- this.#assertRead(4 /* SizeOf.FLOAT32 */);
459
+ const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
460
+ this.#assertRead(offset);
442
461
  const value = this.#dataView.getFloat32(this.#offset, littleEndian);
443
- this.#seek(4 /* SizeOf.FLOAT32 */);
462
+ this.#seek(offset);
444
463
  return value;
445
464
  }
446
465
  /**
@@ -450,16 +469,20 @@ class Buffer {
450
469
  * @returns {number} 双精度 64 位浮点数
451
470
  */
452
471
  readFloat64(littleEndian) {
453
- this.#assertRead(8 /* SizeOf.FLOAT64 */);
472
+ const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
473
+ this.#assertRead(offset);
454
474
  const value = this.#dataView.getFloat64(this.#offset, littleEndian);
455
- this.#seek(8 /* SizeOf.FLOAT64 */);
475
+ this.#seek(offset);
456
476
  return value;
457
477
  }
458
478
  read(length, encoding) {
459
- this.#assertRead(length);
460
- const offset = this.#offset;
461
- const bytes = this.#bytes.slice(offset, offset + length);
462
- this.#seek(length);
479
+ if (!utils.isNaturalNumber(length)) {
480
+ throw new RangeError(errors.readLengthInvalid);
481
+ }
482
+ const offset = this.#getOffset(length);
483
+ this.#assertRead(offset);
484
+ const bytes = this.#bytes.slice(this.#offset, offset);
485
+ this.#seek(offset);
463
486
  if (arguments.length >= 2) {
464
487
  return index.decode(bytes, encoding);
465
488
  }
@@ -490,6 +513,30 @@ class Buffer {
490
513
  this.#bytes.copyWithin(target, start, end);
491
514
  return this;
492
515
  }
516
+ /**
517
+ * @method entries
518
+ * @description 获取迭代器
519
+ * @returns {IterableIterator<[number, number]>}
520
+ */
521
+ *entries() {
522
+ const bytes = this.bytes;
523
+ const length = this.#length;
524
+ for (let i = 0; i < length; i++) {
525
+ yield [i, bytes[i]];
526
+ }
527
+ }
528
+ /**
529
+ * @method values
530
+ * @description 获取迭代器
531
+ * @returns {IterableIterator<number>}
532
+ */
533
+ *values() {
534
+ const bytes = this.bytes;
535
+ const length = this.#length;
536
+ for (let i = 0; i < length; i++) {
537
+ yield bytes[i];
538
+ }
539
+ }
493
540
  /**
494
541
  * @override
495
542
  * @method toString
@@ -508,7 +555,21 @@ class Buffer {
508
555
  // 返回二进制编码
509
556
  return binary;
510
557
  }
558
+ /**
559
+ * @method iterator
560
+ * @description 迭代器
561
+ * @returns {IterableIterator<number>}
562
+ */
563
+ [Symbol.iterator]() {
564
+ return this.values();
565
+ }
511
566
  }
512
567
 
568
+ Object.defineProperty(exports, 'Endian', {
569
+ enumerable: true,
570
+ get: function () {
571
+ return _enum.Endian;
572
+ }
573
+ });
513
574
  exports.Buffer = Buffer;
514
575
  exports.endianness = endianness;