@nuintun/buffer 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -7
- package/cjs/binary.cjs +1 -1
- package/cjs/encoding.cjs +1 -1
- package/cjs/enum.cjs +1 -1
- package/cjs/errors.cjs +4 -1
- package/cjs/index.cjs +35 -29
- package/cjs/index.d.cts +6 -1
- package/cjs/utils.cjs +1 -1
- package/esm/binary.js +1 -1
- package/esm/encoding.js +1 -1
- package/esm/enum.js +1 -1
- package/esm/errors.js +4 -2
- package/esm/index.d.ts +6 -1
- package/esm/index.js +36 -30
- package/esm/utils.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -53,6 +53,11 @@ export interface Options {
|
|
|
53
53
|
* @description 文本解码函数
|
|
54
54
|
*/
|
|
55
55
|
decode?: TextDecode;
|
|
56
|
+
/**
|
|
57
|
+
* @property {boolean} [littleEndian]
|
|
58
|
+
* @description 指定默认字节序,默认小端字节序
|
|
59
|
+
*/
|
|
60
|
+
littleEndian?: boolean;
|
|
56
61
|
}
|
|
57
62
|
|
|
58
63
|
/**
|
|
@@ -79,12 +84,6 @@ export declare class Buffer {
|
|
|
79
84
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
80
85
|
*/
|
|
81
86
|
constructor(bytes: TypedArray, options?: Options);
|
|
82
|
-
/**
|
|
83
|
-
* @constructor
|
|
84
|
-
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
85
|
-
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
86
|
-
*/
|
|
87
|
-
constructor(buffer: ArrayBuffer, options?: Options);
|
|
88
87
|
/**
|
|
89
88
|
* @public
|
|
90
89
|
* @property {number} offset
|
|
@@ -179,7 +178,7 @@ export declare class Buffer {
|
|
|
179
178
|
/**
|
|
180
179
|
* @method writeInt64
|
|
181
180
|
* @description 在缓冲区中写入一个 64 位有符号整数
|
|
182
|
-
* @param {bigint} value 要写入的
|
|
181
|
+
* @param {bigint} value 要写入的 64 位有符号整数
|
|
183
182
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
184
183
|
*/
|
|
185
184
|
writeInt64(value: bigint, littleEndian?: boolean): void;
|
package/cjs/binary.cjs
CHANGED
package/cjs/encoding.cjs
CHANGED
package/cjs/enum.cjs
CHANGED
package/cjs/errors.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.7.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
|
|
@@ -30,10 +30,13 @@ const unknownEndianness = 'unknown endianness';
|
|
|
30
30
|
const readLengthInvalid = 'invalid read length';
|
|
31
31
|
// 数据读取溢出
|
|
32
32
|
const readOverflow = 'read is outside the bounds of the Buffer';
|
|
33
|
+
// 读写指针溢出
|
|
34
|
+
const offsetOverflow = 'offset is outside the bounds of the Buffer';
|
|
33
35
|
|
|
34
36
|
exports.encodingInvalid = encodingInvalid;
|
|
35
37
|
exports.lengthInvalid = lengthInvalid;
|
|
36
38
|
exports.offsetInvalid = offsetInvalid;
|
|
39
|
+
exports.offsetOverflow = offsetOverflow;
|
|
37
40
|
exports.readLengthInvalid = readLengthInvalid;
|
|
38
41
|
exports.readOverflow = readOverflow;
|
|
39
42
|
exports.unknownEndianness = unknownEndianness;
|
package/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.7.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
|
|
@@ -53,6 +53,8 @@ class Buffer {
|
|
|
53
53
|
#encode;
|
|
54
54
|
// 文本解码方法
|
|
55
55
|
#decode;
|
|
56
|
+
// 字节序
|
|
57
|
+
#littleEndian;
|
|
56
58
|
constructor(input = 0, options = {}) {
|
|
57
59
|
let length;
|
|
58
60
|
let bytes;
|
|
@@ -73,6 +75,7 @@ class Buffer {
|
|
|
73
75
|
this.#encode = options.encode ?? encoding.encode;
|
|
74
76
|
this.#decode = options.decode ?? encoding.decode;
|
|
75
77
|
this.#dataView = new DataView(bytes.buffer);
|
|
78
|
+
this.#littleEndian = options.littleEndian ?? true;
|
|
76
79
|
}
|
|
77
80
|
/**
|
|
78
81
|
* @private
|
|
@@ -81,9 +84,6 @@ class Buffer {
|
|
|
81
84
|
* @param {number} offset 指针位置
|
|
82
85
|
*/
|
|
83
86
|
#seek(offset) {
|
|
84
|
-
if (offset > this.#length) {
|
|
85
|
-
this.#length = offset;
|
|
86
|
-
}
|
|
87
87
|
this.#offset = offset;
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
@@ -99,10 +99,10 @@ class Buffer {
|
|
|
99
99
|
* @private
|
|
100
100
|
* @method assertRead
|
|
101
101
|
* @description 读取断言,防止越界读取
|
|
102
|
-
* @param {number}
|
|
102
|
+
* @param {number} offset 断言字节长度
|
|
103
103
|
*/
|
|
104
|
-
#assertRead(
|
|
105
|
-
if (
|
|
104
|
+
#assertRead(offset) {
|
|
105
|
+
if (offset > this.#length) {
|
|
106
106
|
throw new RangeError(errors.readOverflow);
|
|
107
107
|
}
|
|
108
108
|
}
|
|
@@ -120,6 +120,9 @@ class Buffer {
|
|
|
120
120
|
this.#bytes = newBytes;
|
|
121
121
|
this.#dataView = new DataView(newBytes.buffer);
|
|
122
122
|
}
|
|
123
|
+
if (length > this.#length) {
|
|
124
|
+
this.#length = length;
|
|
125
|
+
}
|
|
123
126
|
}
|
|
124
127
|
/**
|
|
125
128
|
* @public
|
|
@@ -131,6 +134,9 @@ class Buffer {
|
|
|
131
134
|
if (!utils.isNaturalNumber(offset)) {
|
|
132
135
|
throw new RangeError(errors.offsetInvalid);
|
|
133
136
|
}
|
|
137
|
+
if (offset > this.#length) {
|
|
138
|
+
throw new RangeError(errors.offsetOverflow);
|
|
139
|
+
}
|
|
134
140
|
this.#offset = offset;
|
|
135
141
|
}
|
|
136
142
|
/**
|
|
@@ -153,9 +159,8 @@ class Buffer {
|
|
|
153
159
|
if (!utils.isNaturalNumber(length)) {
|
|
154
160
|
throw new RangeError(errors.lengthInvalid);
|
|
155
161
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
this.#alloc(length - currentLength);
|
|
162
|
+
if (length > this.#length) {
|
|
163
|
+
this.#alloc(length);
|
|
159
164
|
} else {
|
|
160
165
|
this.#length = length;
|
|
161
166
|
// 重置多余字节
|
|
@@ -230,7 +235,7 @@ class Buffer {
|
|
|
230
235
|
* @param {number} value 要写入的 16 位有符号整数
|
|
231
236
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
232
237
|
*/
|
|
233
|
-
writeInt16(value, littleEndian) {
|
|
238
|
+
writeInt16(value, littleEndian = this.#littleEndian) {
|
|
234
239
|
const offset = this.#getOffset(2 /* SizeOf.INT16 */);
|
|
235
240
|
this.#alloc(offset);
|
|
236
241
|
this.#dataView.setInt16(this.#offset, value, littleEndian);
|
|
@@ -242,7 +247,7 @@ class Buffer {
|
|
|
242
247
|
* @param {number} value 要写入的 16 位无符号整数
|
|
243
248
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
244
249
|
*/
|
|
245
|
-
writeUint16(value, littleEndian) {
|
|
250
|
+
writeUint16(value, littleEndian = this.#littleEndian) {
|
|
246
251
|
const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
|
|
247
252
|
this.#alloc(offset);
|
|
248
253
|
this.#dataView.setUint16(this.#offset, value, littleEndian);
|
|
@@ -254,7 +259,7 @@ class Buffer {
|
|
|
254
259
|
* @param {number} value 要写入的 32 位有符号整数
|
|
255
260
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
256
261
|
*/
|
|
257
|
-
writeInt32(value, littleEndian) {
|
|
262
|
+
writeInt32(value, littleEndian = this.#littleEndian) {
|
|
258
263
|
const offset = this.#getOffset(4 /* SizeOf.INT32 */);
|
|
259
264
|
this.#alloc(offset);
|
|
260
265
|
this.#dataView.setInt32(this.#offset, value, littleEndian);
|
|
@@ -266,7 +271,7 @@ class Buffer {
|
|
|
266
271
|
* @param {number} value 要写入的 32 位无符号整数
|
|
267
272
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
268
273
|
*/
|
|
269
|
-
writeUint32(value, littleEndian) {
|
|
274
|
+
writeUint32(value, littleEndian = this.#littleEndian) {
|
|
270
275
|
const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
|
|
271
276
|
this.#alloc(offset);
|
|
272
277
|
this.#dataView.setUint32(this.#offset, value, littleEndian);
|
|
@@ -275,10 +280,10 @@ class Buffer {
|
|
|
275
280
|
/**
|
|
276
281
|
* @method writeInt64
|
|
277
282
|
* @description 在缓冲区中写入一个 64 位有符号整数
|
|
278
|
-
* @param {bigint} value 要写入的
|
|
283
|
+
* @param {bigint} value 要写入的 64 位有符号整数
|
|
279
284
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
280
285
|
*/
|
|
281
|
-
writeInt64(value, littleEndian) {
|
|
286
|
+
writeInt64(value, littleEndian = this.#littleEndian) {
|
|
282
287
|
const offset = this.#getOffset(8 /* SizeOf.INT64 */);
|
|
283
288
|
this.#alloc(offset);
|
|
284
289
|
this.#dataView.setBigInt64(this.#offset, value, littleEndian);
|
|
@@ -290,7 +295,7 @@ class Buffer {
|
|
|
290
295
|
* @param {bigint} value 要写入的 64 位无符号整数
|
|
291
296
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
292
297
|
*/
|
|
293
|
-
writeUint64(value, littleEndian) {
|
|
298
|
+
writeUint64(value, littleEndian = this.#littleEndian) {
|
|
294
299
|
const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
|
|
295
300
|
this.#alloc(offset);
|
|
296
301
|
this.#dataView.setBigUint64(this.#offset, value, littleEndian);
|
|
@@ -302,7 +307,7 @@ class Buffer {
|
|
|
302
307
|
* @param {number} value 单精度 32 位浮点数
|
|
303
308
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
304
309
|
*/
|
|
305
|
-
writeFloat32(value, littleEndian) {
|
|
310
|
+
writeFloat32(value, littleEndian = this.#littleEndian) {
|
|
306
311
|
const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
|
|
307
312
|
this.#alloc(offset);
|
|
308
313
|
this.#dataView.setFloat32(this.#offset, value, littleEndian);
|
|
@@ -314,7 +319,7 @@ class Buffer {
|
|
|
314
319
|
* @param {number} value 双精度 64 位浮点数
|
|
315
320
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
316
321
|
*/
|
|
317
|
-
writeFloat64(value, littleEndian) {
|
|
322
|
+
writeFloat64(value, littleEndian = this.#littleEndian) {
|
|
318
323
|
const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
|
|
319
324
|
this.#alloc(offset);
|
|
320
325
|
this.#dataView.setFloat64(this.#offset, value, littleEndian);
|
|
@@ -373,7 +378,7 @@ class Buffer {
|
|
|
373
378
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
374
379
|
* @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
|
|
375
380
|
*/
|
|
376
|
-
readInt16(littleEndian) {
|
|
381
|
+
readInt16(littleEndian = this.#littleEndian) {
|
|
377
382
|
const offset = this.#getOffset(2 /* SizeOf.INT16 */);
|
|
378
383
|
this.#assertRead(offset);
|
|
379
384
|
const value = this.#dataView.getInt16(this.#offset, littleEndian);
|
|
@@ -386,7 +391,7 @@ class Buffer {
|
|
|
386
391
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
387
392
|
* @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
|
|
388
393
|
*/
|
|
389
|
-
readUint16(littleEndian) {
|
|
394
|
+
readUint16(littleEndian = this.#littleEndian) {
|
|
390
395
|
const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
|
|
391
396
|
this.#assertRead(offset);
|
|
392
397
|
const value = this.#dataView.getUint16(this.#offset, littleEndian);
|
|
@@ -399,7 +404,7 @@ class Buffer {
|
|
|
399
404
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
400
405
|
* @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
|
|
401
406
|
*/
|
|
402
|
-
readInt32(littleEndian) {
|
|
407
|
+
readInt32(littleEndian = this.#littleEndian) {
|
|
403
408
|
const offset = this.#getOffset(4 /* SizeOf.INT32 */);
|
|
404
409
|
this.#assertRead(offset);
|
|
405
410
|
const value = this.#dataView.getInt32(this.#offset, littleEndian);
|
|
@@ -412,7 +417,7 @@ class Buffer {
|
|
|
412
417
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
413
418
|
* @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
|
|
414
419
|
*/
|
|
415
|
-
readUint32(littleEndian) {
|
|
420
|
+
readUint32(littleEndian = this.#littleEndian) {
|
|
416
421
|
const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
|
|
417
422
|
this.#assertRead(offset);
|
|
418
423
|
const value = this.#dataView.getUint32(this.#offset, littleEndian);
|
|
@@ -425,7 +430,7 @@ class Buffer {
|
|
|
425
430
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
426
431
|
* @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
|
|
427
432
|
*/
|
|
428
|
-
readInt64(littleEndian) {
|
|
433
|
+
readInt64(littleEndian = this.#littleEndian) {
|
|
429
434
|
const offset = this.#getOffset(8 /* SizeOf.INT64 */);
|
|
430
435
|
this.#assertRead(offset);
|
|
431
436
|
const value = this.#dataView.getBigInt64(this.#offset, littleEndian);
|
|
@@ -438,7 +443,7 @@ class Buffer {
|
|
|
438
443
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
439
444
|
* @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
|
|
440
445
|
*/
|
|
441
|
-
readUint64(littleEndian) {
|
|
446
|
+
readUint64(littleEndian = this.#littleEndian) {
|
|
442
447
|
const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
|
|
443
448
|
this.#assertRead(offset);
|
|
444
449
|
const value = this.#dataView.getBigUint64(this.#offset, littleEndian);
|
|
@@ -451,7 +456,7 @@ class Buffer {
|
|
|
451
456
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
452
457
|
* @returns {number} 单精度 32 位浮点数
|
|
453
458
|
*/
|
|
454
|
-
readFloat32(littleEndian) {
|
|
459
|
+
readFloat32(littleEndian = this.#littleEndian) {
|
|
455
460
|
const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
|
|
456
461
|
this.#assertRead(offset);
|
|
457
462
|
const value = this.#dataView.getFloat32(this.#offset, littleEndian);
|
|
@@ -464,7 +469,7 @@ class Buffer {
|
|
|
464
469
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
465
470
|
* @returns {number} 双精度 64 位浮点数
|
|
466
471
|
*/
|
|
467
|
-
readFloat64(littleEndian) {
|
|
472
|
+
readFloat64(littleEndian = this.#littleEndian) {
|
|
468
473
|
const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
|
|
469
474
|
this.#assertRead(offset);
|
|
470
475
|
const value = this.#dataView.getFloat64(this.#offset, littleEndian);
|
|
@@ -493,10 +498,11 @@ class Buffer {
|
|
|
493
498
|
* @returns {Buffer}
|
|
494
499
|
*/
|
|
495
500
|
slice(start, end) {
|
|
496
|
-
return new Buffer(this.bytes.
|
|
501
|
+
return new Buffer(this.bytes.subarray(start, end), {
|
|
497
502
|
encode: this.#encode,
|
|
498
503
|
decode: this.#decode,
|
|
499
|
-
pageSize: this.#pageSize
|
|
504
|
+
pageSize: this.#pageSize,
|
|
505
|
+
littleEndian: this.#littleEndian
|
|
500
506
|
});
|
|
501
507
|
}
|
|
502
508
|
/**
|
package/cjs/index.d.cts
CHANGED
|
@@ -22,6 +22,11 @@ export interface Options {
|
|
|
22
22
|
* @description 文本解码函数
|
|
23
23
|
*/
|
|
24
24
|
decode?: TextDecode;
|
|
25
|
+
/**
|
|
26
|
+
* @property {boolean} [littleEndian]
|
|
27
|
+
* @description 指定默认字节序,默认小端字节序
|
|
28
|
+
*/
|
|
29
|
+
littleEndian?: boolean;
|
|
25
30
|
}
|
|
26
31
|
/**
|
|
27
32
|
* @function endianness
|
|
@@ -141,7 +146,7 @@ export declare class Buffer {
|
|
|
141
146
|
/**
|
|
142
147
|
* @method writeInt64
|
|
143
148
|
* @description 在缓冲区中写入一个 64 位有符号整数
|
|
144
|
-
* @param {bigint} value 要写入的
|
|
149
|
+
* @param {bigint} value 要写入的 64 位有符号整数
|
|
145
150
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
146
151
|
*/
|
|
147
152
|
writeInt64(value: bigint, littleEndian?: boolean): void;
|
package/cjs/utils.cjs
CHANGED
package/esm/binary.js
CHANGED
package/esm/encoding.js
CHANGED
package/esm/enum.js
CHANGED
package/esm/errors.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.7.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
|
|
@@ -28,5 +28,7 @@ const unknownEndianness = 'unknown endianness';
|
|
|
28
28
|
const readLengthInvalid = 'invalid read length';
|
|
29
29
|
// 数据读取溢出
|
|
30
30
|
const readOverflow = 'read is outside the bounds of the Buffer';
|
|
31
|
+
// 读写指针溢出
|
|
32
|
+
const offsetOverflow = 'offset is outside the bounds of the Buffer';
|
|
31
33
|
|
|
32
|
-
export { encodingInvalid, lengthInvalid, offsetInvalid, readLengthInvalid, readOverflow, unknownEndianness };
|
|
34
|
+
export { encodingInvalid, lengthInvalid, offsetInvalid, offsetOverflow, readLengthInvalid, readOverflow, unknownEndianness };
|
package/esm/index.d.ts
CHANGED
|
@@ -22,6 +22,11 @@ export interface Options {
|
|
|
22
22
|
* @description 文本解码函数
|
|
23
23
|
*/
|
|
24
24
|
decode?: TextDecode;
|
|
25
|
+
/**
|
|
26
|
+
* @property {boolean} [littleEndian]
|
|
27
|
+
* @description 指定默认字节序,默认小端字节序
|
|
28
|
+
*/
|
|
29
|
+
littleEndian?: boolean;
|
|
25
30
|
}
|
|
26
31
|
/**
|
|
27
32
|
* @function endianness
|
|
@@ -141,7 +146,7 @@ export declare class Buffer {
|
|
|
141
146
|
/**
|
|
142
147
|
* @method writeInt64
|
|
143
148
|
* @description 在缓冲区中写入一个 64 位有符号整数
|
|
144
|
-
* @param {bigint} value 要写入的
|
|
149
|
+
* @param {bigint} value 要写入的 64 位有符号整数
|
|
145
150
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
146
151
|
*/
|
|
147
152
|
writeInt64(value: bigint, littleEndian?: boolean): void;
|
package/esm/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.7.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 { unknownEndianness, readOverflow, offsetInvalid, lengthInvalid, readLengthInvalid } from './errors.js';
|
|
10
|
+
import { unknownEndianness, readOverflow, offsetInvalid, offsetOverflow, lengthInvalid, readLengthInvalid } from './errors.js';
|
|
11
11
|
import { mapping } from './binary.js';
|
|
12
12
|
import { Endian } from './enum.js';
|
|
13
13
|
import { encode, decode } from './encoding.js';
|
|
@@ -51,6 +51,8 @@ class Buffer {
|
|
|
51
51
|
#encode;
|
|
52
52
|
// 文本解码方法
|
|
53
53
|
#decode;
|
|
54
|
+
// 字节序
|
|
55
|
+
#littleEndian;
|
|
54
56
|
constructor(input = 0, options = {}) {
|
|
55
57
|
let length;
|
|
56
58
|
let bytes;
|
|
@@ -71,6 +73,7 @@ class Buffer {
|
|
|
71
73
|
this.#encode = options.encode ?? encode;
|
|
72
74
|
this.#decode = options.decode ?? decode;
|
|
73
75
|
this.#dataView = new DataView(bytes.buffer);
|
|
76
|
+
this.#littleEndian = options.littleEndian ?? true;
|
|
74
77
|
}
|
|
75
78
|
/**
|
|
76
79
|
* @private
|
|
@@ -79,9 +82,6 @@ class Buffer {
|
|
|
79
82
|
* @param {number} offset 指针位置
|
|
80
83
|
*/
|
|
81
84
|
#seek(offset) {
|
|
82
|
-
if (offset > this.#length) {
|
|
83
|
-
this.#length = offset;
|
|
84
|
-
}
|
|
85
85
|
this.#offset = offset;
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
@@ -97,10 +97,10 @@ class Buffer {
|
|
|
97
97
|
* @private
|
|
98
98
|
* @method assertRead
|
|
99
99
|
* @description 读取断言,防止越界读取
|
|
100
|
-
* @param {number}
|
|
100
|
+
* @param {number} offset 断言字节长度
|
|
101
101
|
*/
|
|
102
|
-
#assertRead(
|
|
103
|
-
if (
|
|
102
|
+
#assertRead(offset) {
|
|
103
|
+
if (offset > this.#length) {
|
|
104
104
|
throw new RangeError(readOverflow);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
@@ -118,6 +118,9 @@ class Buffer {
|
|
|
118
118
|
this.#bytes = newBytes;
|
|
119
119
|
this.#dataView = new DataView(newBytes.buffer);
|
|
120
120
|
}
|
|
121
|
+
if (length > this.#length) {
|
|
122
|
+
this.#length = length;
|
|
123
|
+
}
|
|
121
124
|
}
|
|
122
125
|
/**
|
|
123
126
|
* @public
|
|
@@ -129,6 +132,9 @@ class Buffer {
|
|
|
129
132
|
if (!isNaturalNumber(offset)) {
|
|
130
133
|
throw new RangeError(offsetInvalid);
|
|
131
134
|
}
|
|
135
|
+
if (offset > this.#length) {
|
|
136
|
+
throw new RangeError(offsetOverflow);
|
|
137
|
+
}
|
|
132
138
|
this.#offset = offset;
|
|
133
139
|
}
|
|
134
140
|
/**
|
|
@@ -151,9 +157,8 @@ class Buffer {
|
|
|
151
157
|
if (!isNaturalNumber(length)) {
|
|
152
158
|
throw new RangeError(lengthInvalid);
|
|
153
159
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
this.#alloc(length - currentLength);
|
|
160
|
+
if (length > this.#length) {
|
|
161
|
+
this.#alloc(length);
|
|
157
162
|
} else {
|
|
158
163
|
this.#length = length;
|
|
159
164
|
// 重置多余字节
|
|
@@ -228,7 +233,7 @@ class Buffer {
|
|
|
228
233
|
* @param {number} value 要写入的 16 位有符号整数
|
|
229
234
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
230
235
|
*/
|
|
231
|
-
writeInt16(value, littleEndian) {
|
|
236
|
+
writeInt16(value, littleEndian = this.#littleEndian) {
|
|
232
237
|
const offset = this.#getOffset(2 /* SizeOf.INT16 */);
|
|
233
238
|
this.#alloc(offset);
|
|
234
239
|
this.#dataView.setInt16(this.#offset, value, littleEndian);
|
|
@@ -240,7 +245,7 @@ class Buffer {
|
|
|
240
245
|
* @param {number} value 要写入的 16 位无符号整数
|
|
241
246
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
242
247
|
*/
|
|
243
|
-
writeUint16(value, littleEndian) {
|
|
248
|
+
writeUint16(value, littleEndian = this.#littleEndian) {
|
|
244
249
|
const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
|
|
245
250
|
this.#alloc(offset);
|
|
246
251
|
this.#dataView.setUint16(this.#offset, value, littleEndian);
|
|
@@ -252,7 +257,7 @@ class Buffer {
|
|
|
252
257
|
* @param {number} value 要写入的 32 位有符号整数
|
|
253
258
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
254
259
|
*/
|
|
255
|
-
writeInt32(value, littleEndian) {
|
|
260
|
+
writeInt32(value, littleEndian = this.#littleEndian) {
|
|
256
261
|
const offset = this.#getOffset(4 /* SizeOf.INT32 */);
|
|
257
262
|
this.#alloc(offset);
|
|
258
263
|
this.#dataView.setInt32(this.#offset, value, littleEndian);
|
|
@@ -264,7 +269,7 @@ class Buffer {
|
|
|
264
269
|
* @param {number} value 要写入的 32 位无符号整数
|
|
265
270
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
266
271
|
*/
|
|
267
|
-
writeUint32(value, littleEndian) {
|
|
272
|
+
writeUint32(value, littleEndian = this.#littleEndian) {
|
|
268
273
|
const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
|
|
269
274
|
this.#alloc(offset);
|
|
270
275
|
this.#dataView.setUint32(this.#offset, value, littleEndian);
|
|
@@ -273,10 +278,10 @@ class Buffer {
|
|
|
273
278
|
/**
|
|
274
279
|
* @method writeInt64
|
|
275
280
|
* @description 在缓冲区中写入一个 64 位有符号整数
|
|
276
|
-
* @param {bigint} value 要写入的
|
|
281
|
+
* @param {bigint} value 要写入的 64 位有符号整数
|
|
277
282
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
278
283
|
*/
|
|
279
|
-
writeInt64(value, littleEndian) {
|
|
284
|
+
writeInt64(value, littleEndian = this.#littleEndian) {
|
|
280
285
|
const offset = this.#getOffset(8 /* SizeOf.INT64 */);
|
|
281
286
|
this.#alloc(offset);
|
|
282
287
|
this.#dataView.setBigInt64(this.#offset, value, littleEndian);
|
|
@@ -288,7 +293,7 @@ class Buffer {
|
|
|
288
293
|
* @param {bigint} value 要写入的 64 位无符号整数
|
|
289
294
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
290
295
|
*/
|
|
291
|
-
writeUint64(value, littleEndian) {
|
|
296
|
+
writeUint64(value, littleEndian = this.#littleEndian) {
|
|
292
297
|
const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
|
|
293
298
|
this.#alloc(offset);
|
|
294
299
|
this.#dataView.setBigUint64(this.#offset, value, littleEndian);
|
|
@@ -300,7 +305,7 @@ class Buffer {
|
|
|
300
305
|
* @param {number} value 单精度 32 位浮点数
|
|
301
306
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
302
307
|
*/
|
|
303
|
-
writeFloat32(value, littleEndian) {
|
|
308
|
+
writeFloat32(value, littleEndian = this.#littleEndian) {
|
|
304
309
|
const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
|
|
305
310
|
this.#alloc(offset);
|
|
306
311
|
this.#dataView.setFloat32(this.#offset, value, littleEndian);
|
|
@@ -312,7 +317,7 @@ class Buffer {
|
|
|
312
317
|
* @param {number} value 双精度 64 位浮点数
|
|
313
318
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
314
319
|
*/
|
|
315
|
-
writeFloat64(value, littleEndian) {
|
|
320
|
+
writeFloat64(value, littleEndian = this.#littleEndian) {
|
|
316
321
|
const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
|
|
317
322
|
this.#alloc(offset);
|
|
318
323
|
this.#dataView.setFloat64(this.#offset, value, littleEndian);
|
|
@@ -371,7 +376,7 @@ class Buffer {
|
|
|
371
376
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
372
377
|
* @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
|
|
373
378
|
*/
|
|
374
|
-
readInt16(littleEndian) {
|
|
379
|
+
readInt16(littleEndian = this.#littleEndian) {
|
|
375
380
|
const offset = this.#getOffset(2 /* SizeOf.INT16 */);
|
|
376
381
|
this.#assertRead(offset);
|
|
377
382
|
const value = this.#dataView.getInt16(this.#offset, littleEndian);
|
|
@@ -384,7 +389,7 @@ class Buffer {
|
|
|
384
389
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
385
390
|
* @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
|
|
386
391
|
*/
|
|
387
|
-
readUint16(littleEndian) {
|
|
392
|
+
readUint16(littleEndian = this.#littleEndian) {
|
|
388
393
|
const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
|
|
389
394
|
this.#assertRead(offset);
|
|
390
395
|
const value = this.#dataView.getUint16(this.#offset, littleEndian);
|
|
@@ -397,7 +402,7 @@ class Buffer {
|
|
|
397
402
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
398
403
|
* @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
|
|
399
404
|
*/
|
|
400
|
-
readInt32(littleEndian) {
|
|
405
|
+
readInt32(littleEndian = this.#littleEndian) {
|
|
401
406
|
const offset = this.#getOffset(4 /* SizeOf.INT32 */);
|
|
402
407
|
this.#assertRead(offset);
|
|
403
408
|
const value = this.#dataView.getInt32(this.#offset, littleEndian);
|
|
@@ -410,7 +415,7 @@ class Buffer {
|
|
|
410
415
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
411
416
|
* @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
|
|
412
417
|
*/
|
|
413
|
-
readUint32(littleEndian) {
|
|
418
|
+
readUint32(littleEndian = this.#littleEndian) {
|
|
414
419
|
const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
|
|
415
420
|
this.#assertRead(offset);
|
|
416
421
|
const value = this.#dataView.getUint32(this.#offset, littleEndian);
|
|
@@ -423,7 +428,7 @@ class Buffer {
|
|
|
423
428
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
424
429
|
* @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
|
|
425
430
|
*/
|
|
426
|
-
readInt64(littleEndian) {
|
|
431
|
+
readInt64(littleEndian = this.#littleEndian) {
|
|
427
432
|
const offset = this.#getOffset(8 /* SizeOf.INT64 */);
|
|
428
433
|
this.#assertRead(offset);
|
|
429
434
|
const value = this.#dataView.getBigInt64(this.#offset, littleEndian);
|
|
@@ -436,7 +441,7 @@ class Buffer {
|
|
|
436
441
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
437
442
|
* @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
|
|
438
443
|
*/
|
|
439
|
-
readUint64(littleEndian) {
|
|
444
|
+
readUint64(littleEndian = this.#littleEndian) {
|
|
440
445
|
const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
|
|
441
446
|
this.#assertRead(offset);
|
|
442
447
|
const value = this.#dataView.getBigUint64(this.#offset, littleEndian);
|
|
@@ -449,7 +454,7 @@ class Buffer {
|
|
|
449
454
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
450
455
|
* @returns {number} 单精度 32 位浮点数
|
|
451
456
|
*/
|
|
452
|
-
readFloat32(littleEndian) {
|
|
457
|
+
readFloat32(littleEndian = this.#littleEndian) {
|
|
453
458
|
const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
|
|
454
459
|
this.#assertRead(offset);
|
|
455
460
|
const value = this.#dataView.getFloat32(this.#offset, littleEndian);
|
|
@@ -462,7 +467,7 @@ class Buffer {
|
|
|
462
467
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
463
468
|
* @returns {number} 双精度 64 位浮点数
|
|
464
469
|
*/
|
|
465
|
-
readFloat64(littleEndian) {
|
|
470
|
+
readFloat64(littleEndian = this.#littleEndian) {
|
|
466
471
|
const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
|
|
467
472
|
this.#assertRead(offset);
|
|
468
473
|
const value = this.#dataView.getFloat64(this.#offset, littleEndian);
|
|
@@ -491,10 +496,11 @@ class Buffer {
|
|
|
491
496
|
* @returns {Buffer}
|
|
492
497
|
*/
|
|
493
498
|
slice(start, end) {
|
|
494
|
-
return new Buffer(this.bytes.
|
|
499
|
+
return new Buffer(this.bytes.subarray(start, end), {
|
|
495
500
|
encode: this.#encode,
|
|
496
501
|
decode: this.#decode,
|
|
497
|
-
pageSize: this.#pageSize
|
|
502
|
+
pageSize: this.#pageSize,
|
|
503
|
+
littleEndian: this.#littleEndian
|
|
498
504
|
});
|
|
499
505
|
}
|
|
500
506
|
/**
|
package/esm/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuintun/buffer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "A buffer tool for javascript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"magic-string": "^0.30.17",
|
|
42
42
|
"prettier": "^3.6.2",
|
|
43
43
|
"rimraf": "^6.0.1",
|
|
44
|
-
"rollup": "^4.
|
|
44
|
+
"rollup": "^4.46.2",
|
|
45
45
|
"typescript": "^5.8.3"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|