@nuintun/buffer 0.3.1 → 0.4.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/README.md +63 -40
- package/cjs/{Binary.cjs → binary.cjs} +2 -2
- package/{esm/Binary.d.ts → cjs/binary.d.cts} +1 -1
- package/cjs/encoding.cjs +94 -0
- package/cjs/encoding.d.cts +25 -0
- package/cjs/enum.cjs +1 -1
- package/cjs/errors.cjs +2 -2
- package/cjs/errors.d.cts +1 -1
- package/cjs/index.cjs +74 -34
- package/cjs/index.d.cts +50 -6
- package/cjs/utils.cjs +18 -6
- package/cjs/utils.d.cts +22 -3
- package/{cjs/Binary.d.cts → esm/binary.d.ts} +1 -1
- package/esm/{Binary.js → binary.js} +2 -2
- package/esm/encoding.d.ts +25 -0
- package/esm/encoding.js +91 -0
- package/esm/enum.js +1 -1
- package/esm/errors.d.ts +1 -1
- package/esm/errors.js +2 -2
- package/esm/index.d.ts +50 -6
- package/esm/index.js +72 -32
- package/esm/utils.d.ts +22 -3
- package/esm/utils.js +18 -7
- package/package.json +1 -1
- package/cjs/Encoding/UTF8.cjs +0 -33
- package/cjs/Encoding/UTF8.d.cts +0 -15
- package/cjs/Encoding/Unicode.cjs +0 -45
- package/cjs/Encoding/Unicode.d.cts +0 -18
- package/cjs/Encoding/index.cjs +0 -65
- package/cjs/Encoding/index.d.cts +0 -19
- package/esm/Encoding/UTF8.d.ts +0 -15
- package/esm/Encoding/UTF8.js +0 -30
- package/esm/Encoding/Unicode.d.ts +0 -18
- package/esm/Encoding/Unicode.js +0 -42
- package/esm/Encoding/index.d.ts +0 -19
- package/esm/Encoding/index.js +0 -62
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.4.0
|
|
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,10 +9,10 @@
|
|
|
9
9
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
const Binary = require('./Binary.cjs');
|
|
13
12
|
const errors = require('./errors.cjs');
|
|
14
|
-
const
|
|
13
|
+
const binary = require('./binary.cjs');
|
|
15
14
|
const _enum = require('./enum.cjs');
|
|
15
|
+
const encoding = require('./encoding.cjs');
|
|
16
16
|
const utils = require('./utils.cjs');
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -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);
|
|
@@ -49,29 +49,36 @@ class Buffer {
|
|
|
49
49
|
#offset = 0;
|
|
50
50
|
// 已使用字节长度
|
|
51
51
|
#length = 0;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
constructor(input = 0, pageSize = 4096) {
|
|
52
|
+
// 文本编码方法
|
|
53
|
+
#encode;
|
|
54
|
+
// 文本解码方法
|
|
55
|
+
#decode;
|
|
56
|
+
constructor(input = 0, options = {}) {
|
|
58
57
|
let length;
|
|
59
58
|
let bytes;
|
|
60
|
-
|
|
61
|
-
if (input
|
|
62
|
-
length = input.
|
|
59
|
+
const { pageSize = 4096 } = options;
|
|
60
|
+
if (utils.isTypedArray(input)) {
|
|
61
|
+
length = input.byteLength;
|
|
63
62
|
bytes = utils.makeUint8Array(length, pageSize);
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
if (length > 0) {
|
|
64
|
+
bytes.set(new Uint8Array(input.buffer));
|
|
65
|
+
}
|
|
66
|
+
} else if (input instanceof ArrayBuffer) {
|
|
67
|
+
length = input.byteLength;
|
|
68
|
+
bytes = utils.makeUint8Array(length, pageSize);
|
|
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.#encode = options.encode ?? encoding.encode;
|
|
80
|
+
this.#decode = options.decode ?? encoding.decode;
|
|
81
|
+
this.#dataView = new DataView(bytes.buffer);
|
|
75
82
|
}
|
|
76
83
|
/**
|
|
77
84
|
* @private
|
|
@@ -176,16 +183,16 @@ class Buffer {
|
|
|
176
183
|
/**
|
|
177
184
|
* @public
|
|
178
185
|
* @property {ArrayBuffer} buffer
|
|
179
|
-
* @description
|
|
186
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
180
187
|
* @returns {ArrayBuffer}
|
|
181
188
|
*/
|
|
182
189
|
get buffer() {
|
|
183
|
-
return this
|
|
190
|
+
return this.#bytes.buffer;
|
|
184
191
|
}
|
|
185
192
|
/**
|
|
186
193
|
* @public
|
|
187
194
|
* @property {Uint8Array} bytes
|
|
188
|
-
* @description
|
|
195
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
189
196
|
* @returns {Uint8Array}
|
|
190
197
|
*/
|
|
191
198
|
get bytes() {
|
|
@@ -324,7 +331,7 @@ class Buffer {
|
|
|
324
331
|
if (input instanceof Uint8Array) {
|
|
325
332
|
bytes = input.subarray(start, end);
|
|
326
333
|
} else {
|
|
327
|
-
bytes =
|
|
334
|
+
bytes = this.#encode(input, start ?? 'utf-8');
|
|
328
335
|
}
|
|
329
336
|
const { length } = bytes;
|
|
330
337
|
if (length > 0) {
|
|
@@ -478,8 +485,8 @@ class Buffer {
|
|
|
478
485
|
this.#assertRead(offset);
|
|
479
486
|
const bytes = this.#bytes.slice(this.#offset, offset);
|
|
480
487
|
this.#seek(offset);
|
|
481
|
-
if (
|
|
482
|
-
return
|
|
488
|
+
if (encoding != null) {
|
|
489
|
+
return this.#decode(bytes, encoding);
|
|
483
490
|
}
|
|
484
491
|
return bytes;
|
|
485
492
|
}
|
|
@@ -492,8 +499,11 @@ class Buffer {
|
|
|
492
499
|
* @returns {Buffer}
|
|
493
500
|
*/
|
|
494
501
|
slice(start, end) {
|
|
495
|
-
|
|
496
|
-
|
|
502
|
+
return new Buffer(this.#bytes.slice(start, end), {
|
|
503
|
+
encode: this.#encode,
|
|
504
|
+
decode: this.#decode,
|
|
505
|
+
pageSize: this.#pageSize
|
|
506
|
+
});
|
|
497
507
|
}
|
|
498
508
|
/**
|
|
499
509
|
* @public
|
|
@@ -508,6 +518,38 @@ class Buffer {
|
|
|
508
518
|
this.#bytes.copyWithin(target, start, end);
|
|
509
519
|
return this;
|
|
510
520
|
}
|
|
521
|
+
/**
|
|
522
|
+
* @method entries
|
|
523
|
+
* @description 获取迭代器
|
|
524
|
+
* @returns {IterableIterator<[number, number]>}
|
|
525
|
+
*/
|
|
526
|
+
*entries() {
|
|
527
|
+
const bytes = this.bytes;
|
|
528
|
+
const length = this.#length;
|
|
529
|
+
for (let i = 0; i < length; i++) {
|
|
530
|
+
yield [i, bytes[i]];
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* @method values
|
|
535
|
+
* @description 获取迭代器
|
|
536
|
+
* @returns {IterableIterator<number>}
|
|
537
|
+
*/
|
|
538
|
+
*values() {
|
|
539
|
+
const bytes = this.bytes;
|
|
540
|
+
const length = this.#length;
|
|
541
|
+
for (let i = 0; i < length; i++) {
|
|
542
|
+
yield bytes[i];
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* @method iterator
|
|
547
|
+
* @description 迭代器
|
|
548
|
+
* @returns {IterableIterator<number>}
|
|
549
|
+
*/
|
|
550
|
+
[Symbol.iterator]() {
|
|
551
|
+
return this.values();
|
|
552
|
+
}
|
|
511
553
|
/**
|
|
512
554
|
* @override
|
|
513
555
|
* @method toString
|
|
@@ -516,15 +558,13 @@ class Buffer {
|
|
|
516
558
|
*/
|
|
517
559
|
toString() {
|
|
518
560
|
// 二进制编码字符串
|
|
519
|
-
let binary = '';
|
|
520
|
-
// 提前获取 bytes,防止重复计算
|
|
521
|
-
const bytes = this.bytes;
|
|
561
|
+
let binary$1 = '';
|
|
522
562
|
// 获取二进制编码
|
|
523
|
-
for (const byte of
|
|
524
|
-
binary +=
|
|
563
|
+
for (const byte of this) {
|
|
564
|
+
binary$1 += binary.mapping[byte];
|
|
525
565
|
}
|
|
526
566
|
// 返回二进制编码
|
|
527
|
-
return binary;
|
|
567
|
+
return binary$1;
|
|
528
568
|
}
|
|
529
569
|
}
|
|
530
570
|
|
package/cjs/index.d.cts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Buffer
|
|
3
3
|
*/
|
|
4
|
+
import type { TypedArray } from './utils.cjs';
|
|
4
5
|
import { Endian } from './enum.cjs';
|
|
6
|
+
import { TextDecode, TextEncode } from './encoding.cjs';
|
|
5
7
|
export { Endian };
|
|
8
|
+
export type { TypedArray };
|
|
9
|
+
export interface Options {
|
|
10
|
+
/**
|
|
11
|
+
* @property {number} [pageSize]
|
|
12
|
+
* @description 缓存页大小
|
|
13
|
+
*/
|
|
14
|
+
pageSize?: number;
|
|
15
|
+
/**
|
|
16
|
+
* @property {TextEncode} [encode]
|
|
17
|
+
* @description 文本编码函数
|
|
18
|
+
*/
|
|
19
|
+
encode?: TextEncode;
|
|
20
|
+
/**
|
|
21
|
+
* @property {TextDecode} [decode]
|
|
22
|
+
* @description 文本解码函数
|
|
23
|
+
*/
|
|
24
|
+
decode?: TextDecode;
|
|
25
|
+
}
|
|
6
26
|
/**
|
|
7
27
|
* @function endianness
|
|
8
28
|
* @description 获取系统默认字节序
|
|
@@ -17,16 +37,22 @@ export declare class Buffer {
|
|
|
17
37
|
#private;
|
|
18
38
|
/**
|
|
19
39
|
* @constructor
|
|
20
|
-
* @param {number} [length]
|
|
40
|
+
* @param {number} [length] 缓冲区初始字节大小
|
|
21
41
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
22
42
|
*/
|
|
23
|
-
constructor(length?: number,
|
|
43
|
+
constructor(length?: number, options?: Options);
|
|
24
44
|
/**
|
|
25
45
|
* @constructor
|
|
26
|
-
* @param {Uint8Array} bytes
|
|
46
|
+
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
27
47
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
28
48
|
*/
|
|
29
|
-
constructor(bytes
|
|
49
|
+
constructor(bytes: TypedArray, options?: Options);
|
|
50
|
+
/**
|
|
51
|
+
* @constructor
|
|
52
|
+
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
53
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
54
|
+
*/
|
|
55
|
+
constructor(buffer: ArrayBuffer, options?: Options);
|
|
30
56
|
/**
|
|
31
57
|
* @public
|
|
32
58
|
* @property {number} offset
|
|
@@ -59,14 +85,14 @@ export declare class Buffer {
|
|
|
59
85
|
/**
|
|
60
86
|
* @public
|
|
61
87
|
* @property {ArrayBuffer} buffer
|
|
62
|
-
* @description
|
|
88
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
63
89
|
* @returns {ArrayBuffer}
|
|
64
90
|
*/
|
|
65
91
|
get buffer(): ArrayBuffer;
|
|
66
92
|
/**
|
|
67
93
|
* @public
|
|
68
94
|
* @property {Uint8Array} bytes
|
|
69
|
-
* @description
|
|
95
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
70
96
|
* @returns {Uint8Array}
|
|
71
97
|
*/
|
|
72
98
|
get bytes(): Uint8Array;
|
|
@@ -269,6 +295,24 @@ export declare class Buffer {
|
|
|
269
295
|
* @returns {this}
|
|
270
296
|
*/
|
|
271
297
|
copyWithin(target: number, start: number, end?: number): this;
|
|
298
|
+
/**
|
|
299
|
+
* @method entries
|
|
300
|
+
* @description 获取迭代器
|
|
301
|
+
* @returns {IterableIterator<[number, number]>}
|
|
302
|
+
*/
|
|
303
|
+
entries(): IterableIterator<[number, number]>;
|
|
304
|
+
/**
|
|
305
|
+
* @method values
|
|
306
|
+
* @description 获取迭代器
|
|
307
|
+
* @returns {IterableIterator<number>}
|
|
308
|
+
*/
|
|
309
|
+
values(): IterableIterator<number>;
|
|
310
|
+
/**
|
|
311
|
+
* @method iterator
|
|
312
|
+
* @description 迭代器
|
|
313
|
+
* @returns {IterableIterator<number>}
|
|
314
|
+
*/
|
|
315
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
272
316
|
/**
|
|
273
317
|
* @override
|
|
274
318
|
* @method toString
|
package/cjs/utils.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.4.0
|
|
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;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.4.0
|
|
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
10
|
/**
|
|
11
|
-
* @module
|
|
11
|
+
* @module binary
|
|
12
12
|
*/
|
|
13
13
|
/**
|
|
14
14
|
* @type {string[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module encoding
|
|
3
|
+
*/
|
|
4
|
+
export interface TextEncode {
|
|
5
|
+
/**
|
|
6
|
+
* @function encode
|
|
7
|
+
* @description 用指定编码编码字符串
|
|
8
|
+
* @param {string} content 待编码文本
|
|
9
|
+
* @param {string} encoding 编码类型
|
|
10
|
+
* @returns {Uint8Array}
|
|
11
|
+
*/
|
|
12
|
+
(content: string, encoding: string): Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
export interface TextDecode {
|
|
15
|
+
/**
|
|
16
|
+
* @function decode
|
|
17
|
+
* @description 用指定编码解码字节数组
|
|
18
|
+
* @param {Uint8Array} bytes 待解码字节数组
|
|
19
|
+
* @param {string} encoding 编码类型
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
(bytes: Uint8Array, encoding: string): string;
|
|
23
|
+
}
|
|
24
|
+
export declare function encode(content: string, encoding: string): Uint8Array;
|
|
25
|
+
export declare function decode(bytes: Uint8Array, encoding: string): string;
|
package/esm/encoding.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.4.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 { encodingInvalid } from './errors.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @module encoding
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @function encodeSBSC
|
|
17
|
+
* @description 单字节字符编码
|
|
18
|
+
* @param {string} content 文本内容
|
|
19
|
+
* @param {number} maxCode 最大编码
|
|
20
|
+
* @returns {Uint8Array}
|
|
21
|
+
*/
|
|
22
|
+
function encodeSBSC(content, maxCode) {
|
|
23
|
+
const bytes = [];
|
|
24
|
+
for (const character of content) {
|
|
25
|
+
const code = character.codePointAt(0);
|
|
26
|
+
// If gt max code, push "?".
|
|
27
|
+
bytes.push(code == null || code > maxCode ? 63 : code);
|
|
28
|
+
}
|
|
29
|
+
return new Uint8Array(bytes);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @function swapEndian
|
|
33
|
+
* @description 翻转字节序
|
|
34
|
+
* @param {number} value 待翻转字节序的值
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
function swapEndian(value) {
|
|
38
|
+
return ((value & 0xff) << 8) | ((value >> 8) & 0xff);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @function encodeUTF16
|
|
42
|
+
* @param {string} input 待编码字符串
|
|
43
|
+
* @param {boolean} [littleEndian] 是否使用小端字节序
|
|
44
|
+
* @returns {Uint8Array}
|
|
45
|
+
*/
|
|
46
|
+
function encodeUTF16(input, littleEndian) {
|
|
47
|
+
let offset = 0;
|
|
48
|
+
// 分配内存
|
|
49
|
+
const codes = new Uint16Array(input.length);
|
|
50
|
+
for (const char of input) {
|
|
51
|
+
const code = char.codePointAt(0);
|
|
52
|
+
if (code > 0xffff) {
|
|
53
|
+
// 代理对处理
|
|
54
|
+
const high = 0xd800 | ((code - 0x10000) >> 10);
|
|
55
|
+
const low = 0xdc00 | (code & 0x3ff);
|
|
56
|
+
codes[offset++] = littleEndian ? high : swapEndian(high);
|
|
57
|
+
codes[offset++] = littleEndian ? low : swapEndian(low);
|
|
58
|
+
} else {
|
|
59
|
+
codes[offset++] = littleEndian ? code : swapEndian(code);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return new Uint8Array(codes.buffer, 0, offset * 2);
|
|
63
|
+
}
|
|
64
|
+
function encode(content, encoding) {
|
|
65
|
+
switch (encoding.toLowerCase()) {
|
|
66
|
+
case 'ascii':
|
|
67
|
+
return encodeSBSC(content, 0x7f);
|
|
68
|
+
case 'latin1':
|
|
69
|
+
return encodeSBSC(content, 0xff);
|
|
70
|
+
case 'utf8':
|
|
71
|
+
case 'utf-8':
|
|
72
|
+
return new TextEncoder().encode(content);
|
|
73
|
+
case 'utf16le':
|
|
74
|
+
case 'utf-16le':
|
|
75
|
+
return encodeUTF16(content, true);
|
|
76
|
+
case 'utf16be':
|
|
77
|
+
case 'utf-16be':
|
|
78
|
+
return encodeUTF16(content, false);
|
|
79
|
+
default:
|
|
80
|
+
throw new Error(encodingInvalid(encoding));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function decode(bytes, encoding) {
|
|
84
|
+
try {
|
|
85
|
+
return new TextDecoder(encoding).decode(bytes);
|
|
86
|
+
} catch {
|
|
87
|
+
throw new Error(encodingInvalid(encoding));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { decode, encode };
|
package/esm/enum.js
CHANGED
package/esm/errors.d.ts
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.4.0
|
|
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,7 +13,7 @@
|
|
|
13
13
|
/**
|
|
14
14
|
* @function encodingInvalid
|
|
15
15
|
* @description 未支持的编码格式
|
|
16
|
-
* @param encoding 编码格式
|
|
16
|
+
* @param {string} encoding 编码格式
|
|
17
17
|
*/
|
|
18
18
|
function encodingInvalid(encoding) {
|
|
19
19
|
return 'unsupported encoding ' + encoding;
|
package/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Buffer
|
|
3
3
|
*/
|
|
4
|
+
import type { TypedArray } from './utils.js';
|
|
4
5
|
import { Endian } from './enum.js';
|
|
6
|
+
import { TextDecode, TextEncode } from './encoding.js';
|
|
5
7
|
export { Endian };
|
|
8
|
+
export type { TypedArray };
|
|
9
|
+
export interface Options {
|
|
10
|
+
/**
|
|
11
|
+
* @property {number} [pageSize]
|
|
12
|
+
* @description 缓存页大小
|
|
13
|
+
*/
|
|
14
|
+
pageSize?: number;
|
|
15
|
+
/**
|
|
16
|
+
* @property {TextEncode} [encode]
|
|
17
|
+
* @description 文本编码函数
|
|
18
|
+
*/
|
|
19
|
+
encode?: TextEncode;
|
|
20
|
+
/**
|
|
21
|
+
* @property {TextDecode} [decode]
|
|
22
|
+
* @description 文本解码函数
|
|
23
|
+
*/
|
|
24
|
+
decode?: TextDecode;
|
|
25
|
+
}
|
|
6
26
|
/**
|
|
7
27
|
* @function endianness
|
|
8
28
|
* @description 获取系统默认字节序
|
|
@@ -17,16 +37,22 @@ export declare class Buffer {
|
|
|
17
37
|
#private;
|
|
18
38
|
/**
|
|
19
39
|
* @constructor
|
|
20
|
-
* @param {number} [length]
|
|
40
|
+
* @param {number} [length] 缓冲区初始字节大小
|
|
21
41
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
22
42
|
*/
|
|
23
|
-
constructor(length?: number,
|
|
43
|
+
constructor(length?: number, options?: Options);
|
|
24
44
|
/**
|
|
25
45
|
* @constructor
|
|
26
|
-
* @param {Uint8Array} bytes
|
|
46
|
+
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
27
47
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
28
48
|
*/
|
|
29
|
-
constructor(bytes
|
|
49
|
+
constructor(bytes: TypedArray, options?: Options);
|
|
50
|
+
/**
|
|
51
|
+
* @constructor
|
|
52
|
+
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
53
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
54
|
+
*/
|
|
55
|
+
constructor(buffer: ArrayBuffer, options?: Options);
|
|
30
56
|
/**
|
|
31
57
|
* @public
|
|
32
58
|
* @property {number} offset
|
|
@@ -59,14 +85,14 @@ export declare class Buffer {
|
|
|
59
85
|
/**
|
|
60
86
|
* @public
|
|
61
87
|
* @property {ArrayBuffer} buffer
|
|
62
|
-
* @description
|
|
88
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
63
89
|
* @returns {ArrayBuffer}
|
|
64
90
|
*/
|
|
65
91
|
get buffer(): ArrayBuffer;
|
|
66
92
|
/**
|
|
67
93
|
* @public
|
|
68
94
|
* @property {Uint8Array} bytes
|
|
69
|
-
* @description
|
|
95
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
70
96
|
* @returns {Uint8Array}
|
|
71
97
|
*/
|
|
72
98
|
get bytes(): Uint8Array;
|
|
@@ -269,6 +295,24 @@ export declare class Buffer {
|
|
|
269
295
|
* @returns {this}
|
|
270
296
|
*/
|
|
271
297
|
copyWithin(target: number, start: number, end?: number): this;
|
|
298
|
+
/**
|
|
299
|
+
* @method entries
|
|
300
|
+
* @description 获取迭代器
|
|
301
|
+
* @returns {IterableIterator<[number, number]>}
|
|
302
|
+
*/
|
|
303
|
+
entries(): IterableIterator<[number, number]>;
|
|
304
|
+
/**
|
|
305
|
+
* @method values
|
|
306
|
+
* @description 获取迭代器
|
|
307
|
+
* @returns {IterableIterator<number>}
|
|
308
|
+
*/
|
|
309
|
+
values(): IterableIterator<number>;
|
|
310
|
+
/**
|
|
311
|
+
* @method iterator
|
|
312
|
+
* @description 迭代器
|
|
313
|
+
* @returns {IterableIterator<number>}
|
|
314
|
+
*/
|
|
315
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
272
316
|
/**
|
|
273
317
|
* @override
|
|
274
318
|
* @method toString
|