@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/esm/index.js CHANGED
@@ -1,17 +1,17 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.1
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
- import { mapping } from './Binary.js';
11
10
  import { unknownEndianness, readOverflow, offsetInvalid, lengthInvalid, readLengthInvalid } from './errors.js';
12
- import { encode, decode } from './Encoding/index.js';
11
+ import { mapping } from './binary.js';
13
12
  import { Endian } from './enum.js';
14
- import { makeUint8Array, isNaturalNumber } from './utils.js';
13
+ import { encode, decode } from './encoding.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 Uint32Array([0x12345678]))[0]) {
26
- case 0x12:
25
+ switch (new Uint8Array(new Uint16Array([0x00ff]).buffer)[0]) {
26
+ case 0x00:
27
27
  return Endian.Big;
28
- case 0x78:
28
+ case 0xff:
29
29
  return Endian.Little;
30
30
  default:
31
31
  throw new TypeError(unknownEndianness);
@@ -47,29 +47,36 @@ class Buffer {
47
47
  #offset = 0;
48
48
  // 已使用字节长度
49
49
  #length = 0;
50
- /**
51
- * @constructor
52
- * @param {number | Uint8Array} input 缓冲区初始配置
53
- * @param {number} pageSize 缓冲区分页大小,扩容时将按分页大小增加
54
- */
55
- constructor(input = 0, pageSize = 4096) {
50
+ // 文本编码方法
51
+ #encode;
52
+ // 文本解码方法
53
+ #decode;
54
+ constructor(input = 0, options = {}) {
56
55
  let length;
57
56
  let bytes;
58
- let dataView;
59
- if (input instanceof Uint8Array) {
60
- length = input.length;
57
+ const { pageSize = 4096 } = options;
58
+ if (isTypedArray(input)) {
59
+ length = input.byteLength;
61
60
  bytes = makeUint8Array(length, pageSize);
62
- bytes.set(input);
63
- dataView = new DataView(bytes.buffer);
61
+ if (length > 0) {
62
+ bytes.set(new Uint8Array(input.buffer));
63
+ }
64
+ } else if (input instanceof ArrayBuffer) {
65
+ length = input.byteLength;
66
+ bytes = makeUint8Array(length, pageSize);
67
+ if (length > 0) {
68
+ bytes.set(new Uint8Array(input));
69
+ }
64
70
  } else {
65
71
  length = input;
66
- bytes = makeUint8Array(input, pageSize);
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.#encode = options.encode ?? encode;
78
+ this.#decode = options.decode ?? decode;
79
+ this.#dataView = new DataView(bytes.buffer);
73
80
  }
74
81
  /**
75
82
  * @private
@@ -174,16 +181,16 @@ class Buffer {
174
181
  /**
175
182
  * @public
176
183
  * @property {ArrayBuffer} buffer
177
- * @description 获取 ArrayBuffer 缓冲区
184
+ * @description 获取全部 ArrayBuffer 原始缓冲区
178
185
  * @returns {ArrayBuffer}
179
186
  */
180
187
  get buffer() {
181
- return this.bytes.buffer;
188
+ return this.#bytes.buffer;
182
189
  }
183
190
  /**
184
191
  * @public
185
192
  * @property {Uint8Array} bytes
186
- * @description 获取 Uint8Array 缓冲区
193
+ * @description 获取已写入 Uint8Array 原始缓冲区
187
194
  * @returns {Uint8Array}
188
195
  */
189
196
  get bytes() {
@@ -322,7 +329,7 @@ class Buffer {
322
329
  if (input instanceof Uint8Array) {
323
330
  bytes = input.subarray(start, end);
324
331
  } else {
325
- bytes = encode(input, start);
332
+ bytes = this.#encode(input, start ?? 'utf-8');
326
333
  }
327
334
  const { length } = bytes;
328
335
  if (length > 0) {
@@ -476,8 +483,8 @@ class Buffer {
476
483
  this.#assertRead(offset);
477
484
  const bytes = this.#bytes.slice(this.#offset, offset);
478
485
  this.#seek(offset);
479
- if (arguments.length >= 2) {
480
- return decode(bytes, encoding);
486
+ if (encoding != null) {
487
+ return this.#decode(bytes, encoding);
481
488
  }
482
489
  return bytes;
483
490
  }
@@ -490,8 +497,11 @@ class Buffer {
490
497
  * @returns {Buffer}
491
498
  */
492
499
  slice(start, end) {
493
- const bytes = this.#bytes.slice(start, end);
494
- return new Buffer(bytes, this.#pageSize);
500
+ return new Buffer(this.#bytes.slice(start, end), {
501
+ encode: this.#encode,
502
+ decode: this.#decode,
503
+ pageSize: this.#pageSize
504
+ });
495
505
  }
496
506
  /**
497
507
  * @public
@@ -506,6 +516,38 @@ class Buffer {
506
516
  this.#bytes.copyWithin(target, start, end);
507
517
  return this;
508
518
  }
519
+ /**
520
+ * @method entries
521
+ * @description 获取迭代器
522
+ * @returns {IterableIterator<[number, number]>}
523
+ */
524
+ *entries() {
525
+ const bytes = this.bytes;
526
+ const length = this.#length;
527
+ for (let i = 0; i < length; i++) {
528
+ yield [i, bytes[i]];
529
+ }
530
+ }
531
+ /**
532
+ * @method values
533
+ * @description 获取迭代器
534
+ * @returns {IterableIterator<number>}
535
+ */
536
+ *values() {
537
+ const bytes = this.bytes;
538
+ const length = this.#length;
539
+ for (let i = 0; i < length; i++) {
540
+ yield bytes[i];
541
+ }
542
+ }
543
+ /**
544
+ * @method iterator
545
+ * @description 迭代器
546
+ * @returns {IterableIterator<number>}
547
+ */
548
+ [Symbol.iterator]() {
549
+ return this.values();
550
+ }
509
551
  /**
510
552
  * @override
511
553
  * @method toString
@@ -515,10 +557,8 @@ class Buffer {
515
557
  toString() {
516
558
  // 二进制编码字符串
517
559
  let binary = '';
518
- // 提前获取 bytes,防止重复计算
519
- const bytes = this.bytes;
520
560
  // 获取二进制编码
521
- for (const byte of bytes) {
561
+ for (const byte of this) {
522
562
  binary += mapping[byte];
523
563
  }
524
564
  // 返回二进制编码
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: number): boolean;
29
+ export declare function isNaturalNumber(value: unknown): value is number;
11
30
  /**
12
31
  * @function makeUint8Array
13
32
  * @description 创建一个合适长度的 Uint8Array
14
- * @param {number} byteLength 数据字节总大小
33
+ * @param {number} length 数据长度大小
15
34
  * @param {number} pageSize 缓冲区页大小
16
35
  * @returns {Uint8Array}
17
36
  */
18
- export declare function makeUint8Array(byteLength: number, pageSize: number): Uint8Array;
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.1
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
@@ -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 value >= 0 && Number.isInteger(value);
31
+ return Number.isInteger(value) && value >= 0;
21
32
  }
22
33
  /**
23
34
  * @function makeUint8Array
24
35
  * @description 创建一个合适长度的 Uint8Array
25
- * @param {number} byteLength 数据字节总大小
36
+ * @param {number} length 数据长度大小
26
37
  * @param {number} pageSize 缓冲区页大小
27
38
  * @returns {Uint8Array}
28
39
  */
29
- function makeUint8Array(byteLength, pageSize) {
30
- if (byteLength > pageSize) {
31
- return new Uint8Array(Math.ceil(byteLength / pageSize) * pageSize);
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuintun/buffer",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "A buffer tool for javascript.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -1,33 +0,0 @@
1
- /**
2
- * @package @nuintun/buffer
3
- * @license MIT
4
- * @version 0.3.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A buffer tool for javascript.
7
- * @see https://github.com/nuintun/Buffer#readme
8
- */
9
-
10
- 'use strict';
11
-
12
- /**
13
- * @module UTF8
14
- */
15
- // 编码器实例
16
- const encoder = new TextEncoder();
17
- // 解码器实例
18
- const decoder = new TextDecoder();
19
- /**
20
- * @function encode
21
- * @param {string} input
22
- * @returns {Uint8Array}
23
- */
24
- const encode = encoder.encode.bind(encoder);
25
- /**
26
- * @function decode
27
- * @param {BufferSource} input
28
- * @returns {string}
29
- */
30
- const decode = decoder.decode.bind(decoder);
31
-
32
- exports.decode = decode;
33
- exports.encode = encode;
@@ -1,15 +0,0 @@
1
- /**
2
- * @module UTF8
3
- */
4
- /**
5
- * @function encode
6
- * @param {string} input
7
- * @returns {Uint8Array}
8
- */
9
- export declare const encode: (input?: string) => Uint8Array;
10
- /**
11
- * @function decode
12
- * @param {BufferSource} input
13
- * @returns {string}
14
- */
15
- export declare const decode: (input?: BufferSource) => string;
@@ -1,45 +0,0 @@
1
- /**
2
- * @package @nuintun/buffer
3
- * @license MIT
4
- * @version 0.3.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A buffer tool for javascript.
7
- * @see https://github.com/nuintun/Buffer#readme
8
- */
9
-
10
- 'use strict';
11
-
12
- /**
13
- * @module Unicode
14
- */
15
- /**
16
- * @function encode
17
- * @param {string} input
18
- * @param {TypeArray} Buffer
19
- * @returns {Uint8Array}
20
- */
21
- function encode(input, TypeArray) {
22
- const { length } = input;
23
- const array = new TypeArray(length);
24
- for (let i = 0; i < length; i++) {
25
- array[i] = input.codePointAt(i) || 0;
26
- }
27
- return new Uint8Array(array.buffer);
28
- }
29
- /**
30
- * @function decode
31
- * @param {BufferSource} input
32
- * @param {TypeArray} Buffer
33
- * @returns {string}
34
- */
35
- function decode(input, TypeArray) {
36
- let result = '';
37
- const array = new TypeArray(ArrayBuffer.isView(input) ? input.buffer : input);
38
- for (const code of array) {
39
- result += String.fromCodePoint(code);
40
- }
41
- return result;
42
- }
43
-
44
- exports.decode = decode;
45
- exports.encode = encode;
@@ -1,18 +0,0 @@
1
- /**
2
- * @module Unicode
3
- */
4
- export type TypeArray = typeof Uint16Array | typeof Uint32Array;
5
- /**
6
- * @function encode
7
- * @param {string} input
8
- * @param {TypeArray} Buffer
9
- * @returns {Uint8Array}
10
- */
11
- export declare function encode(input: string, TypeArray: TypeArray): Uint8Array;
12
- /**
13
- * @function decode
14
- * @param {BufferSource} input
15
- * @param {TypeArray} Buffer
16
- * @returns {string}
17
- */
18
- export declare function decode(input: BufferSource, TypeArray: TypeArray): string;
@@ -1,65 +0,0 @@
1
- /**
2
- * @package @nuintun/buffer
3
- * @license MIT
4
- * @version 0.3.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A buffer tool for javascript.
7
- * @see https://github.com/nuintun/Buffer#readme
8
- */
9
-
10
- 'use strict';
11
-
12
- const UTF8 = require('./UTF8.cjs');
13
- const errors = require('../errors.cjs');
14
- const Unicode = require('./Unicode.cjs');
15
-
16
- /**
17
- * @module Encoding
18
- */
19
- /**
20
- * @function encode
21
- * @description 用指定编码编码字符串
22
- * @param {string} input 需要编码的字符串
23
- * @param {string} [encoding] 字符串编码
24
- * @returns {Uint8Array}
25
- */
26
- function encode(input, encoding = 'UTF8') {
27
- switch (encoding.toUpperCase()) {
28
- case 'UTF8':
29
- case 'UTF-8':
30
- return UTF8.encode(input);
31
- case 'UTF16':
32
- case 'UTF-16':
33
- return Unicode.encode(input, Uint16Array);
34
- case 'UTF32':
35
- case 'UTF-32':
36
- return Unicode.encode(input, Uint32Array);
37
- default:
38
- throw new TypeError(errors.encodingInvalid(encoding));
39
- }
40
- }
41
- /**
42
- * @function decode
43
- * @description 用指定编码解码字符串数据
44
- * @param {BufferSource} input 需要解码的字符串数据
45
- * @param {string} [encoding] 字符串编码
46
- * @returns {string}
47
- */
48
- function decode(input, encoding = 'UTF8') {
49
- switch (encoding.toUpperCase()) {
50
- case 'UTF8':
51
- case 'UTF-8':
52
- return UTF8.decode(input);
53
- case 'UTF16':
54
- case 'UTF-16':
55
- return Unicode.decode(input, Uint16Array);
56
- case 'UTF32':
57
- case 'UTF-32':
58
- return Unicode.decode(input, Uint32Array);
59
- default:
60
- throw new TypeError(errors.encodingInvalid(encoding));
61
- }
62
- }
63
-
64
- exports.decode = decode;
65
- exports.encode = encode;
@@ -1,19 +0,0 @@
1
- /**
2
- * @module Encoding
3
- */
4
- /**
5
- * @function encode
6
- * @description 用指定编码编码字符串
7
- * @param {string} input 需要编码的字符串
8
- * @param {string} [encoding] 字符串编码
9
- * @returns {Uint8Array}
10
- */
11
- export declare function encode(input: string, encoding?: string): Uint8Array;
12
- /**
13
- * @function decode
14
- * @description 用指定编码解码字符串数据
15
- * @param {BufferSource} input 需要解码的字符串数据
16
- * @param {string} [encoding] 字符串编码
17
- * @returns {string}
18
- */
19
- export declare function decode(input: BufferSource, encoding?: string): string;
@@ -1,15 +0,0 @@
1
- /**
2
- * @module UTF8
3
- */
4
- /**
5
- * @function encode
6
- * @param {string} input
7
- * @returns {Uint8Array}
8
- */
9
- export declare const encode: (input?: string) => Uint8Array;
10
- /**
11
- * @function decode
12
- * @param {BufferSource} input
13
- * @returns {string}
14
- */
15
- export declare const decode: (input?: BufferSource) => string;
@@ -1,30 +0,0 @@
1
- /**
2
- * @package @nuintun/buffer
3
- * @license MIT
4
- * @version 0.3.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A buffer tool for javascript.
7
- * @see https://github.com/nuintun/Buffer#readme
8
- */
9
-
10
- /**
11
- * @module UTF8
12
- */
13
- // 编码器实例
14
- const encoder = new TextEncoder();
15
- // 解码器实例
16
- const decoder = new TextDecoder();
17
- /**
18
- * @function encode
19
- * @param {string} input
20
- * @returns {Uint8Array}
21
- */
22
- const encode = encoder.encode.bind(encoder);
23
- /**
24
- * @function decode
25
- * @param {BufferSource} input
26
- * @returns {string}
27
- */
28
- const decode = decoder.decode.bind(decoder);
29
-
30
- export { decode, encode };
@@ -1,18 +0,0 @@
1
- /**
2
- * @module Unicode
3
- */
4
- export type TypeArray = typeof Uint16Array | typeof Uint32Array;
5
- /**
6
- * @function encode
7
- * @param {string} input
8
- * @param {TypeArray} Buffer
9
- * @returns {Uint8Array}
10
- */
11
- export declare function encode(input: string, TypeArray: TypeArray): Uint8Array;
12
- /**
13
- * @function decode
14
- * @param {BufferSource} input
15
- * @param {TypeArray} Buffer
16
- * @returns {string}
17
- */
18
- export declare function decode(input: BufferSource, TypeArray: TypeArray): string;
@@ -1,42 +0,0 @@
1
- /**
2
- * @package @nuintun/buffer
3
- * @license MIT
4
- * @version 0.3.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A buffer tool for javascript.
7
- * @see https://github.com/nuintun/Buffer#readme
8
- */
9
-
10
- /**
11
- * @module Unicode
12
- */
13
- /**
14
- * @function encode
15
- * @param {string} input
16
- * @param {TypeArray} Buffer
17
- * @returns {Uint8Array}
18
- */
19
- function encode(input, TypeArray) {
20
- const { length } = input;
21
- const array = new TypeArray(length);
22
- for (let i = 0; i < length; i++) {
23
- array[i] = input.codePointAt(i) || 0;
24
- }
25
- return new Uint8Array(array.buffer);
26
- }
27
- /**
28
- * @function decode
29
- * @param {BufferSource} input
30
- * @param {TypeArray} Buffer
31
- * @returns {string}
32
- */
33
- function decode(input, TypeArray) {
34
- let result = '';
35
- const array = new TypeArray(ArrayBuffer.isView(input) ? input.buffer : input);
36
- for (const code of array) {
37
- result += String.fromCodePoint(code);
38
- }
39
- return result;
40
- }
41
-
42
- export { decode, encode };
@@ -1,19 +0,0 @@
1
- /**
2
- * @module Encoding
3
- */
4
- /**
5
- * @function encode
6
- * @description 用指定编码编码字符串
7
- * @param {string} input 需要编码的字符串
8
- * @param {string} [encoding] 字符串编码
9
- * @returns {Uint8Array}
10
- */
11
- export declare function encode(input: string, encoding?: string): Uint8Array;
12
- /**
13
- * @function decode
14
- * @description 用指定编码解码字符串数据
15
- * @param {BufferSource} input 需要解码的字符串数据
16
- * @param {string} [encoding] 字符串编码
17
- * @returns {string}
18
- */
19
- export declare function decode(input: BufferSource, encoding?: string): string;
@@ -1,62 +0,0 @@
1
- /**
2
- * @package @nuintun/buffer
3
- * @license MIT
4
- * @version 0.3.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A buffer tool for javascript.
7
- * @see https://github.com/nuintun/Buffer#readme
8
- */
9
-
10
- import { encode as encode$2, decode as decode$2 } from './UTF8.js';
11
- import { encodingInvalid } from '../errors.js';
12
- import { encode as encode$1, decode as decode$1 } from './Unicode.js';
13
-
14
- /**
15
- * @module Encoding
16
- */
17
- /**
18
- * @function encode
19
- * @description 用指定编码编码字符串
20
- * @param {string} input 需要编码的字符串
21
- * @param {string} [encoding] 字符串编码
22
- * @returns {Uint8Array}
23
- */
24
- function encode(input, encoding = 'UTF8') {
25
- switch (encoding.toUpperCase()) {
26
- case 'UTF8':
27
- case 'UTF-8':
28
- return encode$2(input);
29
- case 'UTF16':
30
- case 'UTF-16':
31
- return encode$1(input, Uint16Array);
32
- case 'UTF32':
33
- case 'UTF-32':
34
- return encode$1(input, Uint32Array);
35
- default:
36
- throw new TypeError(encodingInvalid(encoding));
37
- }
38
- }
39
- /**
40
- * @function decode
41
- * @description 用指定编码解码字符串数据
42
- * @param {BufferSource} input 需要解码的字符串数据
43
- * @param {string} [encoding] 字符串编码
44
- * @returns {string}
45
- */
46
- function decode(input, encoding = 'UTF8') {
47
- switch (encoding.toUpperCase()) {
48
- case 'UTF8':
49
- case 'UTF-8':
50
- return decode$2(input);
51
- case 'UTF16':
52
- case 'UTF-16':
53
- return decode$1(input, Uint16Array);
54
- case 'UTF32':
55
- case 'UTF-32':
56
- return decode$1(input, Uint32Array);
57
- default:
58
- throw new TypeError(encodingInvalid(encoding));
59
- }
60
- }
61
-
62
- export { decode, encode };