@nuintun/buffer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.js ADDED
@@ -0,0 +1,568 @@
1
+ /**
2
+ * @package @nuintun/buffer
3
+ * @license MIT
4
+ * @version 0.1.0
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A buffer tool for javascript.
7
+ * @see https://github.com/nuintun/Buffer#readme
8
+ */
9
+
10
+ import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
11
+ import { calcBufferLength } from './utils.js';
12
+ import { mapping } from './Binary.js';
13
+ import { unknownEndianness, offsetInvalid, offsetOverflow, lengthInvalid, readOverflow } from './errors.js';
14
+ import { encode, decode } from './Encoding/index.js';
15
+
16
+ /**
17
+ * @module Buffer
18
+ */
19
+ var _Buffer_instances,
20
+ _Buffer_pageSize,
21
+ _Buffer_bytes,
22
+ _Buffer_dataView,
23
+ _Buffer_offset,
24
+ _Buffer_length,
25
+ _Buffer_grow,
26
+ _Buffer_seek,
27
+ _Buffer_assertRead,
28
+ _Buffer_alloc;
29
+ // 字节序类型
30
+ var Endian;
31
+ (function (Endian) {
32
+ Endian[(Endian['Big'] = 0)] = 'Big';
33
+ Endian[(Endian['Little'] = 1)] = 'Little';
34
+ })(Endian || (Endian = {}));
35
+ /**
36
+ * @function endianness
37
+ * @description 获取系统默认字节序
38
+ * @returns {Endian}
39
+ */
40
+ function endianness() {
41
+ switch (new Uint8Array(new Uint32Array([0x12345678]))[0]) {
42
+ case 0x12:
43
+ return Endian.Big;
44
+ case 0x78:
45
+ return Endian.Little;
46
+ default:
47
+ throw new TypeError(unknownEndianness);
48
+ }
49
+ }
50
+ /**
51
+ * @class Buffer
52
+ * @classdesc Buffer 类提供用于优化读取,写入以及处理二进制数据的方法和属性
53
+ */
54
+ class Buffer {
55
+ constructor(input = 0, pageSize = 4096) {
56
+ _Buffer_instances.add(this);
57
+ // 缓冲区页大小
58
+ // 容量不足时按页大小增长
59
+ _Buffer_pageSize.set(this, void 0);
60
+ // 缓冲区数据
61
+ _Buffer_bytes.set(this, void 0);
62
+ // 缓冲区视图
63
+ _Buffer_dataView.set(this, void 0);
64
+ // 读写指针位置
65
+ _Buffer_offset.set(this, 0);
66
+ // 已使用字节长度
67
+ _Buffer_length.set(this, 0);
68
+ __classPrivateFieldSet(this, _Buffer_pageSize, pageSize, 'f');
69
+ if (input instanceof Uint8Array) {
70
+ __classPrivateFieldSet(this, _Buffer_bytes, input, 'f');
71
+ __classPrivateFieldSet(this, _Buffer_length, input.length, 'f');
72
+ __classPrivateFieldSet(this, _Buffer_dataView, new DataView(input.buffer), 'f');
73
+ } else {
74
+ const bytes = new Uint8Array(calcBufferLength(input, pageSize));
75
+ __classPrivateFieldSet(this, _Buffer_bytes, bytes, 'f');
76
+ __classPrivateFieldSet(this, _Buffer_length, input, 'f');
77
+ __classPrivateFieldSet(this, _Buffer_dataView, new DataView(bytes.buffer), 'f');
78
+ }
79
+ }
80
+ /**
81
+ * @public
82
+ * @property {number} offset
83
+ * @description 设置读写指针位置,以字节为单位
84
+ * @description 下一次调用读写方法时将在此位置开始读写
85
+ */
86
+ set offset(offset) {
87
+ if (offset < 0) {
88
+ throw new RangeError(offsetInvalid);
89
+ }
90
+ if (offset > __classPrivateFieldGet(this, _Buffer_length, 'f')) {
91
+ throw new RangeError(offsetOverflow);
92
+ }
93
+ __classPrivateFieldSet(this, _Buffer_offset, offset, 'f');
94
+ }
95
+ /**
96
+ * @public
97
+ * @property {number} offset
98
+ * @description 获取读写指针的位置
99
+ * @returns {number}
100
+ */
101
+ get offset() {
102
+ return __classPrivateFieldGet(this, _Buffer_offset, 'f');
103
+ }
104
+ /**
105
+ * @public
106
+ * @property {number} length
107
+ * @description 设置 Buffer 长度
108
+ * @description 如果将长度设置为小于当前长度的值,将会截断该字节数组
109
+ * @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
110
+ */
111
+ set length(length) {
112
+ if (length < 0) {
113
+ throw new RangeError(lengthInvalid);
114
+ }
115
+ const currentLength = __classPrivateFieldGet(this, _Buffer_length, 'f');
116
+ if (length > currentLength) {
117
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, length - currentLength);
118
+ } else {
119
+ __classPrivateFieldSet(this, _Buffer_length, length, 'f');
120
+ // 重置多余字节
121
+ __classPrivateFieldGet(this, _Buffer_bytes, 'f').fill(0, length);
122
+ }
123
+ if (__classPrivateFieldGet(this, _Buffer_offset, 'f') > length) {
124
+ __classPrivateFieldSet(this, _Buffer_offset, length, 'f');
125
+ }
126
+ }
127
+ /**
128
+ * @public
129
+ * @property {number} length
130
+ * @description 获取 Buffer 长度
131
+ * @returns {number}
132
+ */
133
+ get length() {
134
+ return __classPrivateFieldGet(this, _Buffer_length, 'f');
135
+ }
136
+ /**
137
+ * @public
138
+ * @property {ArrayBuffer} buffer
139
+ * @description 获取 ArrayBuffer 缓冲区
140
+ * @returns {ArrayBuffer}
141
+ */
142
+ get buffer() {
143
+ return __classPrivateFieldGet(this, _Buffer_bytes, 'f').buffer.slice(0, __classPrivateFieldGet(this, _Buffer_length, 'f'));
144
+ }
145
+ /**
146
+ * @public
147
+ * @property {Uint8Array} bytes
148
+ * @description 获取 Uint8Array 缓冲区
149
+ * @returns {Uint8Array}
150
+ */
151
+ get bytes() {
152
+ return __classPrivateFieldGet(this, _Buffer_bytes, 'f').slice(0, __classPrivateFieldGet(this, _Buffer_length, 'f'));
153
+ }
154
+ /**
155
+ * @public
156
+ * @method writeInt8
157
+ * @description 在缓冲区中写入一个有符号整数
158
+ * @param {number} value 介于 -128 和 127 之间的整数
159
+ */
160
+ writeInt8(value) {
161
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 1 /* SizeOf.INT8 */);
162
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setInt8(__classPrivateFieldGet(this, _Buffer_offset, 'f'), value);
163
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 1 /* SizeOf.INT8 */);
164
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.INT8 */);
165
+ }
166
+ /**
167
+ * @public
168
+ * @method writeUint8
169
+ * @description 在缓冲区中写入一个无符号整数
170
+ * @param {number} value 介于 0 和 255 之间的整数
171
+ */
172
+ writeUint8(value) {
173
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 1 /* SizeOf.UINT8 */);
174
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setUint8(__classPrivateFieldGet(this, _Buffer_offset, 'f'), value);
175
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 1 /* SizeOf.UINT8 */);
176
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.UINT8 */);
177
+ }
178
+ /**
179
+ * @method writeBoolean
180
+ * @description 在缓冲区中写入布尔值,true 写 1,false写 0
181
+ * @param {boolean} value 布尔值
182
+ */
183
+ writeBoolean(value) {
184
+ this.writeUint8(value ? 1 : 0);
185
+ }
186
+ /**
187
+ * @method writeInt16
188
+ * @description 在缓冲区中写入一个 16 位有符号整数
189
+ * @param {number} value 要写入的 16 位有符号整数
190
+ * @param {boolean} [littleEndian] 是否为小端字节序
191
+ */
192
+ writeInt16(value, littleEndian) {
193
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 2 /* SizeOf.INT16 */);
194
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setInt16(
195
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
196
+ value,
197
+ littleEndian
198
+ );
199
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 2 /* SizeOf.INT16 */);
200
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.INT16 */);
201
+ }
202
+ /**
203
+ * @method writeUint16
204
+ * @description 在缓冲区中写入一个 16 位无符号整数
205
+ * @param {number} value 要写入的 16 位无符号整数
206
+ * @param {boolean} [littleEndian] 是否为小端字节序
207
+ */
208
+ writeUint16(value, littleEndian) {
209
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 2 /* SizeOf.UINT16 */);
210
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setUint16(
211
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
212
+ value,
213
+ littleEndian
214
+ );
215
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 2 /* SizeOf.UINT16 */);
216
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.UINT16 */);
217
+ }
218
+ /**
219
+ * @method writeInt32
220
+ * @description 在缓冲区中写入一个有符号的 32 位有符号整数
221
+ * @param {number} value 要写入的 32 位有符号整数
222
+ * @param {boolean} [littleEndian] 是否为小端字节序
223
+ */
224
+ writeInt32(value, littleEndian) {
225
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 4 /* SizeOf.INT32 */);
226
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setInt32(
227
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
228
+ value,
229
+ littleEndian
230
+ );
231
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.INT32 */);
232
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.INT32 */);
233
+ }
234
+ /**
235
+ * @method writeUint32
236
+ * @description 在缓冲区中写入一个无符号的 32 位无符号整数
237
+ * @param {number} value 要写入的 32 位无符号整数
238
+ * @param {boolean} [littleEndian] 是否为小端字节序
239
+ */
240
+ writeUint32(value, littleEndian) {
241
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 4 /* SizeOf.UINT32 */);
242
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setUint32(
243
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
244
+ value,
245
+ littleEndian
246
+ );
247
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.UINT32 */);
248
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.UINT32 */);
249
+ }
250
+ /**
251
+ * @method writeInt64
252
+ * @description 在缓冲区中写入一个无符号的 64 位有符号整数
253
+ * @param {bigint} value 要写入的 32 位有符号整数
254
+ * @param {boolean} [littleEndian] 是否为小端字节序
255
+ */
256
+ writeInt64(value, littleEndian) {
257
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 8 /* SizeOf.INI64 */);
258
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setBigInt64(
259
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
260
+ value,
261
+ littleEndian
262
+ );
263
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.INI64 */);
264
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.INI64 */);
265
+ }
266
+ /**
267
+ * @method writeUint64
268
+ * @description 在缓冲区中写入一个无符号的 64 位无符号整数
269
+ * @param {bigint} value 要写入的 64 位无符号整数
270
+ * @param {boolean} [littleEndian] 是否为小端字节序
271
+ */
272
+ writeUint64(value, littleEndian) {
273
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 8 /* SizeOf.UINT64 */);
274
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setBigUint64(
275
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
276
+ value,
277
+ littleEndian
278
+ );
279
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.UINT64 */);
280
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.UINT64 */);
281
+ }
282
+ /**
283
+ * @method writeFloat32
284
+ * @description 在缓冲区中写入一个 IEEE 754 单精度 32 位浮点数
285
+ * @param {number} value 单精度 32 位浮点数
286
+ * @param {boolean} [littleEndian] 是否为小端字节序
287
+ */
288
+ writeFloat32(value, littleEndian) {
289
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 4 /* SizeOf.FLOAT32 */);
290
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setFloat32(
291
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
292
+ value,
293
+ littleEndian
294
+ );
295
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.FLOAT32 */);
296
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.FLOAT32 */);
297
+ }
298
+ /**
299
+ * @method writeFloat64
300
+ * @description 在缓冲区中写入一个 IEEE 754 双精度 64 位浮点数
301
+ * @param {number} value 双精度 64 位浮点数
302
+ * @param {boolean} [littleEndian] 是否为小端字节序
303
+ */
304
+ writeFloat64(value, littleEndian) {
305
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 8 /* SizeOf.FLOAT64 */);
306
+ __classPrivateFieldGet(this, _Buffer_dataView, 'f').setFloat64(
307
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
308
+ value,
309
+ littleEndian
310
+ );
311
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.FLOAT64 */);
312
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.FLOAT64 */);
313
+ }
314
+ write(input, start, end) {
315
+ let bytes;
316
+ if (input instanceof Uint8Array) {
317
+ bytes = input.subarray(start, end);
318
+ } else {
319
+ bytes = encode(input, start);
320
+ }
321
+ const { length } = bytes;
322
+ if (length > 0) {
323
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, length);
324
+ __classPrivateFieldGet(this, _Buffer_bytes, 'f').set(bytes, __classPrivateFieldGet(this, _Buffer_offset, 'f'));
325
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, length);
326
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, length);
327
+ }
328
+ }
329
+ /**
330
+ * @method readInt8
331
+ * @description 从缓冲区中读取有符号的整数
332
+ * @returns {number} 介于 -128 和 127 之间的整数
333
+ */
334
+ readInt8() {
335
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 1 /* SizeOf.INT8 */);
336
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getInt8(
337
+ __classPrivateFieldGet(this, _Buffer_offset, 'f')
338
+ );
339
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.INT8 */);
340
+ return value;
341
+ }
342
+ /**
343
+ * @method readUint8
344
+ * @description 从缓冲区中读取无符号的整数
345
+ * @returns {number} 介于 0 和 255 之间的无符号整数
346
+ */
347
+ readUint8() {
348
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 1 /* SizeOf.UINT8 */);
349
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getUint8(
350
+ __classPrivateFieldGet(this, _Buffer_offset, 'f')
351
+ );
352
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.UINT8 */);
353
+ return value;
354
+ }
355
+ /**
356
+ * @method readBoolean
357
+ * @description 从缓冲区中读取布尔值
358
+ * @returns {boolean} 如果字节非零,则返回 true,否则返回 false
359
+ */
360
+ readBoolean() {
361
+ return Boolean(this.readUint8());
362
+ }
363
+ /**
364
+ * @method readInt16
365
+ * @description 从缓冲区中读取一个 16 位有符号整数
366
+ * @param {boolean} [littleEndian] 是否为小端字节序
367
+ * @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
368
+ */
369
+ readInt16(littleEndian) {
370
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 2 /* SizeOf.INT16 */);
371
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getInt16(
372
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
373
+ littleEndian
374
+ );
375
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.INT16 */);
376
+ return value;
377
+ }
378
+ /**
379
+ * @method readUint16
380
+ * @description 从缓冲区中读取一个 16 位无符号整数
381
+ * @param {boolean} [littleEndian] 是否为小端字节序
382
+ * @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
383
+ */
384
+ readUint16(littleEndian) {
385
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 2 /* SizeOf.UINT16 */);
386
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getUint16(
387
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
388
+ littleEndian
389
+ );
390
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.UINT16 */);
391
+ return value;
392
+ }
393
+ /**
394
+ * @method readInt32
395
+ * @description 从缓冲区中读取一个 32 位有符号整数
396
+ * @param {boolean} [littleEndian] 是否为小端字节序
397
+ * @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
398
+ */
399
+ readInt32(littleEndian) {
400
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 4 /* SizeOf.INT32 */);
401
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getInt32(
402
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
403
+ littleEndian
404
+ );
405
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.INT32 */);
406
+ return value;
407
+ }
408
+ /**
409
+ * @method readUint32
410
+ * @description 从缓冲区中读取一个 32 位无符号整数
411
+ * @param {boolean} [littleEndian] 是否为小端字节序
412
+ * @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
413
+ */
414
+ readUint32(littleEndian) {
415
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 4 /* SizeOf.UINT32 */);
416
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getUint32(
417
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
418
+ littleEndian
419
+ );
420
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.UINT32 */);
421
+ return value;
422
+ }
423
+ /**
424
+ * @method readInt64
425
+ * @description 从缓冲区中读取一个 64 位有符号整数
426
+ * @param {boolean} [littleEndian] 是否为小端字节序
427
+ * @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
428
+ */
429
+ readInt64(littleEndian) {
430
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 8 /* SizeOf.INI64 */);
431
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getBigInt64(
432
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
433
+ littleEndian
434
+ );
435
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.INI64 */);
436
+ return value;
437
+ }
438
+ /**
439
+ * @method readUint64
440
+ * @description 从缓冲区中读取一个 64 位无符号整数
441
+ * @param {boolean} [littleEndian] 是否为小端字节序
442
+ * @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
443
+ */
444
+ readUint64(littleEndian) {
445
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 8 /* SizeOf.UINT64 */);
446
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getBigUint64(
447
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
448
+ littleEndian
449
+ );
450
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.UINT64 */);
451
+ return value;
452
+ }
453
+ /**
454
+ * @method readFloat32
455
+ * @description 从缓冲区中读取一个 IEEE 754 单精度 32 位浮点数
456
+ * @param {boolean} [littleEndian] 是否为小端字节序
457
+ * @returns {number} 单精度 32 位浮点数
458
+ */
459
+ readFloat32(littleEndian) {
460
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 4 /* SizeOf.FLOAT32 */);
461
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getFloat32(
462
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
463
+ littleEndian
464
+ );
465
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.FLOAT32 */);
466
+ return value;
467
+ }
468
+ /**
469
+ * @method readFloat64
470
+ * @description 从缓冲区中读取一个 IEEE 754 双精度 64 位浮点数
471
+ * @param {boolean} [littleEndian] 是否为小端字节序
472
+ * @returns {number} 双精度 64 位浮点数
473
+ */
474
+ readFloat64(littleEndian) {
475
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 8 /* SizeOf.FLOAT64 */);
476
+ const value = __classPrivateFieldGet(this, _Buffer_dataView, 'f').getFloat64(
477
+ __classPrivateFieldGet(this, _Buffer_offset, 'f'),
478
+ littleEndian
479
+ );
480
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.FLOAT64 */);
481
+ return value;
482
+ }
483
+ read(length, encoding) {
484
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, length);
485
+ const offset = __classPrivateFieldGet(this, _Buffer_offset, 'f');
486
+ const bytes = __classPrivateFieldGet(this, _Buffer_bytes, 'f').slice(offset, offset + length);
487
+ __classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, length);
488
+ if (arguments.length >= 2) {
489
+ return decode(bytes, encoding);
490
+ }
491
+ return bytes;
492
+ }
493
+ /**
494
+ * @public
495
+ * @method slice
496
+ * @description 从指定开始和结束位置索引截取并返回新的 Buffer 对象
497
+ * @param {number} [start] 截取开始位置索引
498
+ * @param {number} [end] 截取结束位置索引
499
+ * @returns {Buffer}
500
+ */
501
+ slice(start, end) {
502
+ const bytes = __classPrivateFieldGet(this, _Buffer_bytes, 'f').slice(start, end);
503
+ return new Buffer(bytes, __classPrivateFieldGet(this, _Buffer_pageSize, 'f'));
504
+ }
505
+ /**
506
+ * @public
507
+ * @method copyWithin
508
+ * @description 从 Buffer 对象中将指定位置的数据复制到以 target 起始的位置
509
+ * @param {number} target 粘贴开始位置索引
510
+ * @param {number} start 复制开始位置索引
511
+ * @param {number} [end] 复制结束位置索引
512
+ * @returns {this}
513
+ */
514
+ copyWithin(target, start, end) {
515
+ __classPrivateFieldGet(this, _Buffer_bytes, 'f').copyWithin(target, start, end);
516
+ return this;
517
+ }
518
+ /**
519
+ * @override
520
+ * @method toString
521
+ * @description 获取 Buffer 对象二进制编码字符串
522
+ * @returns {string}
523
+ */
524
+ toString() {
525
+ // 二进制编码字符串
526
+ let binary = '';
527
+ // 提前获取 bytes,防止重复计算
528
+ const bytes = this.bytes;
529
+ // 获取二进制编码
530
+ for (const byte of bytes) {
531
+ binary += mapping[byte];
532
+ }
533
+ // 返回二进制编码
534
+ return binary;
535
+ }
536
+ }
537
+ (_Buffer_pageSize = new WeakMap()),
538
+ (_Buffer_bytes = new WeakMap()),
539
+ (_Buffer_dataView = new WeakMap()),
540
+ (_Buffer_offset = new WeakMap()),
541
+ (_Buffer_length = new WeakMap()),
542
+ (_Buffer_instances = new WeakSet()),
543
+ (_Buffer_grow = function _Buffer_grow(length) {
544
+ __classPrivateFieldSet(this, _Buffer_length, __classPrivateFieldGet(this, _Buffer_length, 'f') + length, 'f');
545
+ }),
546
+ (_Buffer_seek = function _Buffer_seek(offset) {
547
+ __classPrivateFieldSet(this, _Buffer_offset, __classPrivateFieldGet(this, _Buffer_offset, 'f') + offset, 'f');
548
+ }),
549
+ (_Buffer_assertRead = function _Buffer_assertRead(length) {
550
+ if (
551
+ length < 0 ||
552
+ __classPrivateFieldGet(this, _Buffer_offset, 'f') + length > __classPrivateFieldGet(this, _Buffer_length, 'f')
553
+ ) {
554
+ throw new RangeError(readOverflow);
555
+ }
556
+ }),
557
+ (_Buffer_alloc = function _Buffer_alloc(length) {
558
+ length += __classPrivateFieldGet(this, _Buffer_offset, 'f');
559
+ const bytes = __classPrivateFieldGet(this, _Buffer_bytes, 'f');
560
+ if (length > bytes.length) {
561
+ const newBytes = new Uint8Array(calcBufferLength(length, __classPrivateFieldGet(this, _Buffer_pageSize, 'f')));
562
+ newBytes.set(bytes);
563
+ __classPrivateFieldSet(this, _Buffer_bytes, newBytes, 'f');
564
+ __classPrivateFieldSet(this, _Buffer_dataView, new DataView(newBytes.buffer), 'f');
565
+ }
566
+ });
567
+
568
+ export { Buffer, Endian, endianness };
package/esm/utils.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @module utils
3
+ */
4
+ /**
5
+ * @function calcBufferLength
6
+ * @description 计算适合的 Buffer 长度
7
+ * @param {number} length 数据字节总大小
8
+ * @param {number} pageSize 缓冲区页大小
9
+ * @returns {number}
10
+ */
11
+ export declare function calcBufferLength(length: number, pageSize: number): number;
package/esm/utils.js ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @package @nuintun/buffer
3
+ * @license MIT
4
+ * @version 0.1.0
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 utils
12
+ */
13
+ /**
14
+ * @function calcBufferLength
15
+ * @description 计算适合的 Buffer 长度
16
+ * @param {number} length 数据字节总大小
17
+ * @param {number} pageSize 缓冲区页大小
18
+ * @returns {number}
19
+ */
20
+ function calcBufferLength(length, pageSize) {
21
+ if (length > pageSize) {
22
+ const pages = Math.ceil(length / pageSize);
23
+ return pages * pageSize;
24
+ } else {
25
+ return length;
26
+ }
27
+ }
28
+
29
+ export { calcBufferLength };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@nuintun/buffer",
3
+ "version": "0.1.0",
4
+ "description": "A buffer tool for javascript.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "cjs/index.cjs",
8
+ "module": "esm/index.js",
9
+ "exports": {
10
+ "import": "./esm/index.js",
11
+ "require": "./cjs/index.cjs"
12
+ },
13
+ "files": [
14
+ "cjs",
15
+ "esm"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/nuintun/Buffer.git"
20
+ },
21
+ "keywords": [
22
+ "buffer"
23
+ ],
24
+ "author": {
25
+ "name": "nuintun",
26
+ "email": "nuintun@qq.com"
27
+ },
28
+ "license": "MIT",
29
+ "bugs": {
30
+ "url": "https://github.com/nuintun/Buffer/issues"
31
+ },
32
+ "homepage": "https://github.com/nuintun/Buffer#readme",
33
+ "scripts": {
34
+ "lint": "tsc --noEmit",
35
+ "test": "node tests/index.js",
36
+ "clean": "node tools/clean.js",
37
+ "prepublishOnly": "npm run build",
38
+ "build:cjs": "rollup -c tools/rollup.cjs.js",
39
+ "build:esm": "rollup -c tools/rollup.esm.js",
40
+ "build:fix-types": "node tools/fix-types.js",
41
+ "build:tests": "rollup -c tools/rollup.tests.js",
42
+ "build:examples": "rollup -c tools/rollup.examples.js",
43
+ "format": "prettier --write . --ignore-path .prettierignore",
44
+ "build:main": "npm run build:cjs && npm run build:esm && npm run build:fix-types",
45
+ "build": "npm run clean && npm run build:main && npm run build:tests && npm run build:examples && npm run format"
46
+ },
47
+ "dependencies": {
48
+ "tslib": "^2.8.1"
49
+ },
50
+ "devDependencies": {
51
+ "@rollup/plugin-node-resolve": "^16.0.0",
52
+ "@rollup/plugin-typescript": "^12.1.2",
53
+ "@types/node": "^22.10.2",
54
+ "dts-paths": "^1.0.3",
55
+ "magic-string": "^0.30.17",
56
+ "prettier": "^3.4.2",
57
+ "rimraf": "^6.0.1",
58
+ "rollup": "^4.28.1",
59
+ "typescript": "^5.7.2"
60
+ }
61
+ }