@nuintun/buffer 0.2.5 → 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.2.5
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.2.5
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.2.5
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.2.5
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.2.5
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.2.5
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 = new Uint8Array(utils.calcBufferLength(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,10 +112,9 @@ 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
- const newBytes = new Uint8Array(utils.calcBufferLength(length, this.#pageSize));
117
+ const newBytes = utils.makeUint8Array(length, this.#pageSize);
110
118
  newBytes.set(bytes);
111
119
  this.#bytes = newBytes;
112
120
  this.#dataView = new DataView(newBytes.buffer);
@@ -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;
@@ -175,7 +180,7 @@ class Buffer {
175
180
  * @returns {ArrayBuffer}
176
181
  */
177
182
  get buffer() {
178
- return this.#bytes.buffer.slice(0, this.#length);
183
+ return this.bytes.buffer;
179
184
  }
180
185
  /**
181
186
  * @public
@@ -184,7 +189,7 @@ class Buffer {
184
189
  * @returns {Uint8Array}
185
190
  */
186
191
  get bytes() {
187
- return this.#bytes.slice(0, this.#length);
192
+ return this.#bytes.subarray(0, this.#length);
188
193
  }
189
194
  /**
190
195
  * @public
@@ -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.2.5
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
@@ -13,19 +13,27 @@
13
13
  * @module utils
14
14
  */
15
15
  /**
16
- * @function calcBufferLength
17
- * @description 计算适合的 Buffer 长度
18
- * @param {number} length 数据字节总大小
16
+ * @function isNaturalNumber
17
+ * @description 判断是否为自然数
18
+ * @param value 待判断的值
19
+ * @returns {boolean}
20
+ */
21
+ function isNaturalNumber(value) {
22
+ return value >= 0 && Number.isInteger(value);
23
+ }
24
+ /**
25
+ * @function makeUint8Array
26
+ * @description 创建一个合适长度的 Uint8Array
27
+ * @param {number} byteLength 数据字节总大小
19
28
  * @param {number} pageSize 缓冲区页大小
20
- * @returns {number}
29
+ * @returns {Uint8Array}
21
30
  */
22
- function calcBufferLength(length, pageSize) {
23
- if (length > pageSize) {
24
- const pages = Math.ceil(length / pageSize);
25
- return pages * pageSize;
26
- } else {
27
- return length;
31
+ function makeUint8Array(byteLength, pageSize) {
32
+ if (byteLength > pageSize) {
33
+ return new Uint8Array(Math.ceil(byteLength / pageSize) * pageSize);
28
34
  }
35
+ return new Uint8Array(pageSize);
29
36
  }
30
37
 
31
- exports.calcBufferLength = calcBufferLength;
38
+ exports.isNaturalNumber = isNaturalNumber;
39
+ exports.makeUint8Array = makeUint8Array;