@nuintun/buffer 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/Binary.cjs +1 -1
- package/cjs/Encoding/UTF8.cjs +1 -1
- package/cjs/Encoding/Unicode.cjs +1 -1
- package/cjs/Encoding/index.cjs +1 -1
- package/cjs/enum.cjs +1 -1
- package/cjs/errors.cjs +1 -1
- package/cjs/index.cjs +54 -17
- package/cjs/index.d.cts +31 -5
- package/cjs/utils.cjs +18 -6
- package/cjs/utils.d.cts +22 -3
- package/esm/Binary.js +1 -1
- package/esm/Encoding/UTF8.js +1 -1
- package/esm/Encoding/Unicode.js +1 -1
- package/esm/Encoding/index.js +1 -1
- package/esm/enum.js +1 -1
- package/esm/errors.js +1 -1
- package/esm/index.d.ts +31 -5
- package/esm/index.js +55 -18
- package/esm/utils.d.ts +22 -3
- package/esm/utils.js +18 -7
- package/package.json +1 -1
package/cjs/Binary.cjs
CHANGED
package/cjs/Encoding/UTF8.cjs
CHANGED
package/cjs/Encoding/Unicode.cjs
CHANGED
package/cjs/Encoding/index.cjs
CHANGED
package/cjs/enum.cjs
CHANGED
package/cjs/errors.cjs
CHANGED
package/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.3.
|
|
4
|
+
* @version 0.3.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -24,10 +24,10 @@ const utils = require('./utils.cjs');
|
|
|
24
24
|
* @returns {Endian}
|
|
25
25
|
*/
|
|
26
26
|
function endianness() {
|
|
27
|
-
switch (new Uint8Array(new
|
|
28
|
-
case
|
|
27
|
+
switch (new Uint8Array(new Uint16Array([0x00ff]).buffer)[0]) {
|
|
28
|
+
case 0x00:
|
|
29
29
|
return _enum.Endian.Big;
|
|
30
|
-
case
|
|
30
|
+
case 0xff:
|
|
31
31
|
return _enum.Endian.Little;
|
|
32
32
|
default:
|
|
33
33
|
throw new TypeError(errors.unknownEndianness);
|
|
@@ -51,27 +51,32 @@ class Buffer {
|
|
|
51
51
|
#length = 0;
|
|
52
52
|
/**
|
|
53
53
|
* @constructor
|
|
54
|
-
* @param {number | Uint8Array} input 缓冲区初始配置
|
|
55
|
-
* @param {number} pageSize 缓冲区分页大小,扩容时将按分页大小增加
|
|
54
|
+
* @param {number | Uint8Array | ArrayBuffer} [input] 缓冲区初始配置
|
|
55
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
56
56
|
*/
|
|
57
57
|
constructor(input = 0, pageSize = 4096) {
|
|
58
58
|
let length;
|
|
59
59
|
let bytes;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
if (utils.isTypedArray(input)) {
|
|
61
|
+
length = input.byteLength;
|
|
62
|
+
bytes = utils.makeUint8Array(length, pageSize);
|
|
63
|
+
if (length > 0) {
|
|
64
|
+
bytes.set(new Uint8Array(input.buffer));
|
|
65
|
+
}
|
|
66
|
+
} else if (input instanceof ArrayBuffer) {
|
|
67
|
+
length = input.byteLength;
|
|
63
68
|
bytes = utils.makeUint8Array(length, pageSize);
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
if (length > 0) {
|
|
70
|
+
bytes.set(new Uint8Array(input));
|
|
71
|
+
}
|
|
66
72
|
} else {
|
|
67
73
|
length = input;
|
|
68
|
-
bytes = utils.makeUint8Array(
|
|
69
|
-
dataView = new DataView(bytes.buffer);
|
|
74
|
+
bytes = utils.makeUint8Array(length, pageSize);
|
|
70
75
|
}
|
|
71
76
|
this.#bytes = bytes;
|
|
72
77
|
this.#length = length;
|
|
73
|
-
this.#dataView = dataView;
|
|
74
78
|
this.#pageSize = pageSize;
|
|
79
|
+
this.#dataView = new DataView(bytes.buffer);
|
|
75
80
|
}
|
|
76
81
|
/**
|
|
77
82
|
* @private
|
|
@@ -176,16 +181,16 @@ class Buffer {
|
|
|
176
181
|
/**
|
|
177
182
|
* @public
|
|
178
183
|
* @property {ArrayBuffer} buffer
|
|
179
|
-
* @description
|
|
184
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
180
185
|
* @returns {ArrayBuffer}
|
|
181
186
|
*/
|
|
182
187
|
get buffer() {
|
|
183
|
-
return this
|
|
188
|
+
return this.#bytes.buffer;
|
|
184
189
|
}
|
|
185
190
|
/**
|
|
186
191
|
* @public
|
|
187
192
|
* @property {Uint8Array} bytes
|
|
188
|
-
* @description
|
|
193
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
189
194
|
* @returns {Uint8Array}
|
|
190
195
|
*/
|
|
191
196
|
get bytes() {
|
|
@@ -508,6 +513,30 @@ class Buffer {
|
|
|
508
513
|
this.#bytes.copyWithin(target, start, end);
|
|
509
514
|
return this;
|
|
510
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* @method entries
|
|
518
|
+
* @description 获取迭代器
|
|
519
|
+
* @returns {IterableIterator<[number, number]>}
|
|
520
|
+
*/
|
|
521
|
+
*entries() {
|
|
522
|
+
const bytes = this.bytes;
|
|
523
|
+
const length = this.#length;
|
|
524
|
+
for (let i = 0; i < length; i++) {
|
|
525
|
+
yield [i, bytes[i]];
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* @method values
|
|
530
|
+
* @description 获取迭代器
|
|
531
|
+
* @returns {IterableIterator<number>}
|
|
532
|
+
*/
|
|
533
|
+
*values() {
|
|
534
|
+
const bytes = this.bytes;
|
|
535
|
+
const length = this.#length;
|
|
536
|
+
for (let i = 0; i < length; i++) {
|
|
537
|
+
yield bytes[i];
|
|
538
|
+
}
|
|
539
|
+
}
|
|
511
540
|
/**
|
|
512
541
|
* @override
|
|
513
542
|
* @method toString
|
|
@@ -526,6 +555,14 @@ class Buffer {
|
|
|
526
555
|
// 返回二进制编码
|
|
527
556
|
return binary;
|
|
528
557
|
}
|
|
558
|
+
/**
|
|
559
|
+
* @method iterator
|
|
560
|
+
* @description 迭代器
|
|
561
|
+
* @returns {IterableIterator<number>}
|
|
562
|
+
*/
|
|
563
|
+
[Symbol.iterator]() {
|
|
564
|
+
return this.values();
|
|
565
|
+
}
|
|
529
566
|
}
|
|
530
567
|
|
|
531
568
|
Object.defineProperty(exports, 'Endian', {
|
package/cjs/index.d.cts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Buffer
|
|
3
3
|
*/
|
|
4
|
+
import type { TypedArray } from './utils.cjs';
|
|
4
5
|
import { Endian } from './enum.cjs';
|
|
5
6
|
export { Endian };
|
|
7
|
+
export type { TypedArray };
|
|
6
8
|
/**
|
|
7
9
|
* @function endianness
|
|
8
10
|
* @description 获取系统默认字节序
|
|
@@ -17,16 +19,22 @@ export declare class Buffer {
|
|
|
17
19
|
#private;
|
|
18
20
|
/**
|
|
19
21
|
* @constructor
|
|
20
|
-
* @param {number} [length]
|
|
22
|
+
* @param {number} [length] 缓冲区初始字节大小
|
|
21
23
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
22
24
|
*/
|
|
23
25
|
constructor(length?: number, pageSize?: number);
|
|
24
26
|
/**
|
|
25
27
|
* @constructor
|
|
26
|
-
* @param {Uint8Array} bytes
|
|
28
|
+
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
27
29
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
28
30
|
*/
|
|
29
|
-
constructor(bytes
|
|
31
|
+
constructor(bytes: TypedArray, pageSize?: number);
|
|
32
|
+
/**
|
|
33
|
+
* @constructor
|
|
34
|
+
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
35
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
36
|
+
*/
|
|
37
|
+
constructor(buffer: ArrayBuffer, pageSize?: number);
|
|
30
38
|
/**
|
|
31
39
|
* @public
|
|
32
40
|
* @property {number} offset
|
|
@@ -59,14 +67,14 @@ export declare class Buffer {
|
|
|
59
67
|
/**
|
|
60
68
|
* @public
|
|
61
69
|
* @property {ArrayBuffer} buffer
|
|
62
|
-
* @description
|
|
70
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
63
71
|
* @returns {ArrayBuffer}
|
|
64
72
|
*/
|
|
65
73
|
get buffer(): ArrayBuffer;
|
|
66
74
|
/**
|
|
67
75
|
* @public
|
|
68
76
|
* @property {Uint8Array} bytes
|
|
69
|
-
* @description
|
|
77
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
70
78
|
* @returns {Uint8Array}
|
|
71
79
|
*/
|
|
72
80
|
get bytes(): Uint8Array;
|
|
@@ -269,6 +277,18 @@ export declare class Buffer {
|
|
|
269
277
|
* @returns {this}
|
|
270
278
|
*/
|
|
271
279
|
copyWithin(target: number, start: number, end?: number): this;
|
|
280
|
+
/**
|
|
281
|
+
* @method entries
|
|
282
|
+
* @description 获取迭代器
|
|
283
|
+
* @returns {IterableIterator<[number, number]>}
|
|
284
|
+
*/
|
|
285
|
+
entries(): IterableIterator<[number, number]>;
|
|
286
|
+
/**
|
|
287
|
+
* @method values
|
|
288
|
+
* @description 获取迭代器
|
|
289
|
+
* @returns {IterableIterator<number>}
|
|
290
|
+
*/
|
|
291
|
+
values(): IterableIterator<number>;
|
|
272
292
|
/**
|
|
273
293
|
* @override
|
|
274
294
|
* @method toString
|
|
@@ -276,4 +296,10 @@ export declare class Buffer {
|
|
|
276
296
|
* @returns {string}
|
|
277
297
|
*/
|
|
278
298
|
toString(): string;
|
|
299
|
+
/**
|
|
300
|
+
* @method iterator
|
|
301
|
+
* @description 迭代器
|
|
302
|
+
* @returns {IterableIterator<number>}
|
|
303
|
+
*/
|
|
304
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
279
305
|
}
|
package/cjs/utils.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.3.
|
|
4
|
+
* @version 0.3.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -12,6 +12,17 @@
|
|
|
12
12
|
/**
|
|
13
13
|
* @module utils
|
|
14
14
|
*/
|
|
15
|
+
// 获取 TypedArray 原型
|
|
16
|
+
const TypedArray = Object.getPrototypeOf(Uint8Array);
|
|
17
|
+
/**
|
|
18
|
+
* @function isTypedArray
|
|
19
|
+
* @description 检测是否为 TypedArray
|
|
20
|
+
* @param value 待判断的值
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
function isTypedArray(value) {
|
|
24
|
+
return value instanceof TypedArray;
|
|
25
|
+
}
|
|
15
26
|
/**
|
|
16
27
|
* @function isNaturalNumber
|
|
17
28
|
* @description 判断是否为自然数
|
|
@@ -19,21 +30,22 @@
|
|
|
19
30
|
* @returns {boolean}
|
|
20
31
|
*/
|
|
21
32
|
function isNaturalNumber(value) {
|
|
22
|
-
return
|
|
33
|
+
return Number.isInteger(value) && value >= 0;
|
|
23
34
|
}
|
|
24
35
|
/**
|
|
25
36
|
* @function makeUint8Array
|
|
26
37
|
* @description 创建一个合适长度的 Uint8Array
|
|
27
|
-
* @param {number}
|
|
38
|
+
* @param {number} length 数据长度大小
|
|
28
39
|
* @param {number} pageSize 缓冲区页大小
|
|
29
40
|
* @returns {Uint8Array}
|
|
30
41
|
*/
|
|
31
|
-
function makeUint8Array(
|
|
32
|
-
if (
|
|
33
|
-
return new Uint8Array(Math.ceil(
|
|
42
|
+
function makeUint8Array(length, pageSize) {
|
|
43
|
+
if (length > pageSize) {
|
|
44
|
+
return new Uint8Array(Math.ceil(length / pageSize) * pageSize);
|
|
34
45
|
}
|
|
35
46
|
return new Uint8Array(pageSize);
|
|
36
47
|
}
|
|
37
48
|
|
|
38
49
|
exports.isNaturalNumber = isNaturalNumber;
|
|
50
|
+
exports.isTypedArray = isTypedArray;
|
|
39
51
|
exports.makeUint8Array = makeUint8Array;
|
package/cjs/utils.d.cts
CHANGED
|
@@ -1,18 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module utils
|
|
3
3
|
*/
|
|
4
|
+
export type TypedArray =
|
|
5
|
+
| Int8Array
|
|
6
|
+
| Int16Array
|
|
7
|
+
| Int32Array
|
|
8
|
+
| Uint8Array
|
|
9
|
+
| Uint16Array
|
|
10
|
+
| Uint32Array
|
|
11
|
+
| Float32Array
|
|
12
|
+
| Float64Array
|
|
13
|
+
| BigInt64Array
|
|
14
|
+
| BigUint64Array
|
|
15
|
+
| Uint8ClampedArray;
|
|
16
|
+
/**
|
|
17
|
+
* @function isTypedArray
|
|
18
|
+
* @description 检测是否为 TypedArray
|
|
19
|
+
* @param value 待判断的值
|
|
20
|
+
* @returns {boolean}
|
|
21
|
+
*/
|
|
22
|
+
export declare function isTypedArray(value: unknown): value is TypedArray;
|
|
4
23
|
/**
|
|
5
24
|
* @function isNaturalNumber
|
|
6
25
|
* @description 判断是否为自然数
|
|
7
26
|
* @param value 待判断的值
|
|
8
27
|
* @returns {boolean}
|
|
9
28
|
*/
|
|
10
|
-
export declare function isNaturalNumber(value:
|
|
29
|
+
export declare function isNaturalNumber(value: unknown): value is number;
|
|
11
30
|
/**
|
|
12
31
|
* @function makeUint8Array
|
|
13
32
|
* @description 创建一个合适长度的 Uint8Array
|
|
14
|
-
* @param {number}
|
|
33
|
+
* @param {number} length 数据长度大小
|
|
15
34
|
* @param {number} pageSize 缓冲区页大小
|
|
16
35
|
* @returns {Uint8Array}
|
|
17
36
|
*/
|
|
18
|
-
export declare function makeUint8Array(
|
|
37
|
+
export declare function makeUint8Array(length: number, pageSize: number): Uint8Array;
|
package/esm/Binary.js
CHANGED
package/esm/Encoding/UTF8.js
CHANGED
package/esm/Encoding/Unicode.js
CHANGED
package/esm/Encoding/index.js
CHANGED
package/esm/enum.js
CHANGED
package/esm/errors.js
CHANGED
package/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Buffer
|
|
3
3
|
*/
|
|
4
|
+
import type { TypedArray } from './utils.js';
|
|
4
5
|
import { Endian } from './enum.js';
|
|
5
6
|
export { Endian };
|
|
7
|
+
export type { TypedArray };
|
|
6
8
|
/**
|
|
7
9
|
* @function endianness
|
|
8
10
|
* @description 获取系统默认字节序
|
|
@@ -17,16 +19,22 @@ export declare class Buffer {
|
|
|
17
19
|
#private;
|
|
18
20
|
/**
|
|
19
21
|
* @constructor
|
|
20
|
-
* @param {number} [length]
|
|
22
|
+
* @param {number} [length] 缓冲区初始字节大小
|
|
21
23
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
22
24
|
*/
|
|
23
25
|
constructor(length?: number, pageSize?: number);
|
|
24
26
|
/**
|
|
25
27
|
* @constructor
|
|
26
|
-
* @param {Uint8Array} bytes
|
|
28
|
+
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
27
29
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
28
30
|
*/
|
|
29
|
-
constructor(bytes
|
|
31
|
+
constructor(bytes: TypedArray, pageSize?: number);
|
|
32
|
+
/**
|
|
33
|
+
* @constructor
|
|
34
|
+
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
35
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
36
|
+
*/
|
|
37
|
+
constructor(buffer: ArrayBuffer, pageSize?: number);
|
|
30
38
|
/**
|
|
31
39
|
* @public
|
|
32
40
|
* @property {number} offset
|
|
@@ -59,14 +67,14 @@ export declare class Buffer {
|
|
|
59
67
|
/**
|
|
60
68
|
* @public
|
|
61
69
|
* @property {ArrayBuffer} buffer
|
|
62
|
-
* @description
|
|
70
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
63
71
|
* @returns {ArrayBuffer}
|
|
64
72
|
*/
|
|
65
73
|
get buffer(): ArrayBuffer;
|
|
66
74
|
/**
|
|
67
75
|
* @public
|
|
68
76
|
* @property {Uint8Array} bytes
|
|
69
|
-
* @description
|
|
77
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
70
78
|
* @returns {Uint8Array}
|
|
71
79
|
*/
|
|
72
80
|
get bytes(): Uint8Array;
|
|
@@ -269,6 +277,18 @@ export declare class Buffer {
|
|
|
269
277
|
* @returns {this}
|
|
270
278
|
*/
|
|
271
279
|
copyWithin(target: number, start: number, end?: number): this;
|
|
280
|
+
/**
|
|
281
|
+
* @method entries
|
|
282
|
+
* @description 获取迭代器
|
|
283
|
+
* @returns {IterableIterator<[number, number]>}
|
|
284
|
+
*/
|
|
285
|
+
entries(): IterableIterator<[number, number]>;
|
|
286
|
+
/**
|
|
287
|
+
* @method values
|
|
288
|
+
* @description 获取迭代器
|
|
289
|
+
* @returns {IterableIterator<number>}
|
|
290
|
+
*/
|
|
291
|
+
values(): IterableIterator<number>;
|
|
272
292
|
/**
|
|
273
293
|
* @override
|
|
274
294
|
* @method toString
|
|
@@ -276,4 +296,10 @@ export declare class Buffer {
|
|
|
276
296
|
* @returns {string}
|
|
277
297
|
*/
|
|
278
298
|
toString(): string;
|
|
299
|
+
/**
|
|
300
|
+
* @method iterator
|
|
301
|
+
* @description 迭代器
|
|
302
|
+
* @returns {IterableIterator<number>}
|
|
303
|
+
*/
|
|
304
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
279
305
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.3.
|
|
4
|
+
* @version 0.3.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -11,7 +11,7 @@ import { mapping } from './Binary.js';
|
|
|
11
11
|
import { unknownEndianness, readOverflow, offsetInvalid, lengthInvalid, readLengthInvalid } from './errors.js';
|
|
12
12
|
import { encode, decode } from './Encoding/index.js';
|
|
13
13
|
import { Endian } from './enum.js';
|
|
14
|
-
import { makeUint8Array, isNaturalNumber } from './utils.js';
|
|
14
|
+
import { isTypedArray, makeUint8Array, isNaturalNumber } from './utils.js';
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* @module Buffer
|
|
@@ -22,10 +22,10 @@ import { makeUint8Array, isNaturalNumber } from './utils.js';
|
|
|
22
22
|
* @returns {Endian}
|
|
23
23
|
*/
|
|
24
24
|
function endianness() {
|
|
25
|
-
switch (new Uint8Array(new
|
|
26
|
-
case
|
|
25
|
+
switch (new Uint8Array(new Uint16Array([0x00ff]).buffer)[0]) {
|
|
26
|
+
case 0x00:
|
|
27
27
|
return Endian.Big;
|
|
28
|
-
case
|
|
28
|
+
case 0xff:
|
|
29
29
|
return Endian.Little;
|
|
30
30
|
default:
|
|
31
31
|
throw new TypeError(unknownEndianness);
|
|
@@ -49,27 +49,32 @@ class Buffer {
|
|
|
49
49
|
#length = 0;
|
|
50
50
|
/**
|
|
51
51
|
* @constructor
|
|
52
|
-
* @param {number | Uint8Array} input 缓冲区初始配置
|
|
53
|
-
* @param {number} pageSize 缓冲区分页大小,扩容时将按分页大小增加
|
|
52
|
+
* @param {number | Uint8Array | ArrayBuffer} [input] 缓冲区初始配置
|
|
53
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
54
54
|
*/
|
|
55
55
|
constructor(input = 0, pageSize = 4096) {
|
|
56
56
|
let length;
|
|
57
57
|
let bytes;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
if (isTypedArray(input)) {
|
|
59
|
+
length = input.byteLength;
|
|
60
|
+
bytes = makeUint8Array(length, pageSize);
|
|
61
|
+
if (length > 0) {
|
|
62
|
+
bytes.set(new Uint8Array(input.buffer));
|
|
63
|
+
}
|
|
64
|
+
} else if (input instanceof ArrayBuffer) {
|
|
65
|
+
length = input.byteLength;
|
|
61
66
|
bytes = makeUint8Array(length, pageSize);
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
if (length > 0) {
|
|
68
|
+
bytes.set(new Uint8Array(input));
|
|
69
|
+
}
|
|
64
70
|
} else {
|
|
65
71
|
length = input;
|
|
66
|
-
bytes = makeUint8Array(
|
|
67
|
-
dataView = new DataView(bytes.buffer);
|
|
72
|
+
bytes = makeUint8Array(length, pageSize);
|
|
68
73
|
}
|
|
69
74
|
this.#bytes = bytes;
|
|
70
75
|
this.#length = length;
|
|
71
|
-
this.#dataView = dataView;
|
|
72
76
|
this.#pageSize = pageSize;
|
|
77
|
+
this.#dataView = new DataView(bytes.buffer);
|
|
73
78
|
}
|
|
74
79
|
/**
|
|
75
80
|
* @private
|
|
@@ -174,16 +179,16 @@ class Buffer {
|
|
|
174
179
|
/**
|
|
175
180
|
* @public
|
|
176
181
|
* @property {ArrayBuffer} buffer
|
|
177
|
-
* @description
|
|
182
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
178
183
|
* @returns {ArrayBuffer}
|
|
179
184
|
*/
|
|
180
185
|
get buffer() {
|
|
181
|
-
return this
|
|
186
|
+
return this.#bytes.buffer;
|
|
182
187
|
}
|
|
183
188
|
/**
|
|
184
189
|
* @public
|
|
185
190
|
* @property {Uint8Array} bytes
|
|
186
|
-
* @description
|
|
191
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
187
192
|
* @returns {Uint8Array}
|
|
188
193
|
*/
|
|
189
194
|
get bytes() {
|
|
@@ -506,6 +511,30 @@ class Buffer {
|
|
|
506
511
|
this.#bytes.copyWithin(target, start, end);
|
|
507
512
|
return this;
|
|
508
513
|
}
|
|
514
|
+
/**
|
|
515
|
+
* @method entries
|
|
516
|
+
* @description 获取迭代器
|
|
517
|
+
* @returns {IterableIterator<[number, number]>}
|
|
518
|
+
*/
|
|
519
|
+
*entries() {
|
|
520
|
+
const bytes = this.bytes;
|
|
521
|
+
const length = this.#length;
|
|
522
|
+
for (let i = 0; i < length; i++) {
|
|
523
|
+
yield [i, bytes[i]];
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* @method values
|
|
528
|
+
* @description 获取迭代器
|
|
529
|
+
* @returns {IterableIterator<number>}
|
|
530
|
+
*/
|
|
531
|
+
*values() {
|
|
532
|
+
const bytes = this.bytes;
|
|
533
|
+
const length = this.#length;
|
|
534
|
+
for (let i = 0; i < length; i++) {
|
|
535
|
+
yield bytes[i];
|
|
536
|
+
}
|
|
537
|
+
}
|
|
509
538
|
/**
|
|
510
539
|
* @override
|
|
511
540
|
* @method toString
|
|
@@ -524,6 +553,14 @@ class Buffer {
|
|
|
524
553
|
// 返回二进制编码
|
|
525
554
|
return binary;
|
|
526
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* @method iterator
|
|
558
|
+
* @description 迭代器
|
|
559
|
+
* @returns {IterableIterator<number>}
|
|
560
|
+
*/
|
|
561
|
+
[Symbol.iterator]() {
|
|
562
|
+
return this.values();
|
|
563
|
+
}
|
|
527
564
|
}
|
|
528
565
|
|
|
529
566
|
export { Buffer, Endian, endianness };
|
package/esm/utils.d.ts
CHANGED
|
@@ -1,18 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module utils
|
|
3
3
|
*/
|
|
4
|
+
export type TypedArray =
|
|
5
|
+
| Int8Array
|
|
6
|
+
| Int16Array
|
|
7
|
+
| Int32Array
|
|
8
|
+
| Uint8Array
|
|
9
|
+
| Uint16Array
|
|
10
|
+
| Uint32Array
|
|
11
|
+
| Float32Array
|
|
12
|
+
| Float64Array
|
|
13
|
+
| BigInt64Array
|
|
14
|
+
| BigUint64Array
|
|
15
|
+
| Uint8ClampedArray;
|
|
16
|
+
/**
|
|
17
|
+
* @function isTypedArray
|
|
18
|
+
* @description 检测是否为 TypedArray
|
|
19
|
+
* @param value 待判断的值
|
|
20
|
+
* @returns {boolean}
|
|
21
|
+
*/
|
|
22
|
+
export declare function isTypedArray(value: unknown): value is TypedArray;
|
|
4
23
|
/**
|
|
5
24
|
* @function isNaturalNumber
|
|
6
25
|
* @description 判断是否为自然数
|
|
7
26
|
* @param value 待判断的值
|
|
8
27
|
* @returns {boolean}
|
|
9
28
|
*/
|
|
10
|
-
export declare function isNaturalNumber(value:
|
|
29
|
+
export declare function isNaturalNumber(value: unknown): value is number;
|
|
11
30
|
/**
|
|
12
31
|
* @function makeUint8Array
|
|
13
32
|
* @description 创建一个合适长度的 Uint8Array
|
|
14
|
-
* @param {number}
|
|
33
|
+
* @param {number} length 数据长度大小
|
|
15
34
|
* @param {number} pageSize 缓冲区页大小
|
|
16
35
|
* @returns {Uint8Array}
|
|
17
36
|
*/
|
|
18
|
-
export declare function makeUint8Array(
|
|
37
|
+
export declare function makeUint8Array(length: number, pageSize: number): 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.
|
|
4
|
+
* @version 0.3.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -10,6 +10,17 @@
|
|
|
10
10
|
/**
|
|
11
11
|
* @module utils
|
|
12
12
|
*/
|
|
13
|
+
// 获取 TypedArray 原型
|
|
14
|
+
const TypedArray = Object.getPrototypeOf(Uint8Array);
|
|
15
|
+
/**
|
|
16
|
+
* @function isTypedArray
|
|
17
|
+
* @description 检测是否为 TypedArray
|
|
18
|
+
* @param value 待判断的值
|
|
19
|
+
* @returns {boolean}
|
|
20
|
+
*/
|
|
21
|
+
function isTypedArray(value) {
|
|
22
|
+
return value instanceof TypedArray;
|
|
23
|
+
}
|
|
13
24
|
/**
|
|
14
25
|
* @function isNaturalNumber
|
|
15
26
|
* @description 判断是否为自然数
|
|
@@ -17,20 +28,20 @@
|
|
|
17
28
|
* @returns {boolean}
|
|
18
29
|
*/
|
|
19
30
|
function isNaturalNumber(value) {
|
|
20
|
-
return
|
|
31
|
+
return Number.isInteger(value) && value >= 0;
|
|
21
32
|
}
|
|
22
33
|
/**
|
|
23
34
|
* @function makeUint8Array
|
|
24
35
|
* @description 创建一个合适长度的 Uint8Array
|
|
25
|
-
* @param {number}
|
|
36
|
+
* @param {number} length 数据长度大小
|
|
26
37
|
* @param {number} pageSize 缓冲区页大小
|
|
27
38
|
* @returns {Uint8Array}
|
|
28
39
|
*/
|
|
29
|
-
function makeUint8Array(
|
|
30
|
-
if (
|
|
31
|
-
return new Uint8Array(Math.ceil(
|
|
40
|
+
function makeUint8Array(length, pageSize) {
|
|
41
|
+
if (length > pageSize) {
|
|
42
|
+
return new Uint8Array(Math.ceil(length / pageSize) * pageSize);
|
|
32
43
|
}
|
|
33
44
|
return new Uint8Array(pageSize);
|
|
34
45
|
}
|
|
35
46
|
|
|
36
|
-
export { isNaturalNumber, makeUint8Array };
|
|
47
|
+
export { isNaturalNumber, isTypedArray, makeUint8Array };
|