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