@bare-ts/lib 0.1.1 → 0.2.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.
@@ -1,8 +1,8 @@
1
1
  import type { ByteCursor } from "../core/index.js";
2
- export declare const decodeU16FixedArray: typeof decodeU16FixedArrayLE;
3
- export declare function decodeU16Array(bc: ByteCursor): Uint16Array;
4
- declare function decodeU16FixedArrayLE(bc: ByteCursor, len: number): Uint16Array;
5
- export declare const encodeU16FixedArray: typeof encodeU16FixedArrayLE;
6
- export declare function encodeU16Array(bc: ByteCursor, x: Uint16Array): void;
7
- declare function encodeU16FixedArrayLE(bc: ByteCursor, x: Uint16Array, len: number): void;
2
+ export declare const readU16FixedArray: typeof readU16FixedArrayLE;
3
+ export declare function readU16Array(bc: ByteCursor): Uint16Array;
4
+ declare function readU16FixedArrayLE(bc: ByteCursor, len: number): Uint16Array;
5
+ export declare const writeU16FixedArray: typeof writeU16FixedArrayLE;
6
+ export declare function writeU16Array(bc: ByteCursor, x: Uint16Array): void;
7
+ declare function writeU16FixedArrayLE(bc: ByteCursor, x: Uint16Array): void;
8
8
  export {};
@@ -1,50 +1,43 @@
1
1
  // src/codec/u16-array.ts
2
- import { ok as assert } from "assert";
3
2
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
- import { decodeFixedData } from "./data.js";
5
- import {
6
- decodeU16,
7
- decodeUintSafe,
8
- encodeU16,
9
- encodeUintSafe
10
- } from "./primitive.js";
3
+ import { readFixedData } from "./data.js";
4
+ import { readU16, readUintSafe, writeU16, writeUintSafe } from "./primitive.js";
5
+ import { writeU8FixedArray } from "./u8-array.js";
11
6
  var U16_BYTE_COUNT = 2;
12
- var decodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU16FixedArrayLE : decodeU16FixedArrayBE;
13
- function decodeU16Array(bc) {
14
- return decodeU16FixedArray(bc, decodeUintSafe(bc));
7
+ var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
8
+ function readU16Array(bc) {
9
+ return readU16FixedArray(bc, readUintSafe(bc));
15
10
  }
16
- function decodeU16FixedArrayLE(bc, len) {
11
+ function readU16FixedArrayLE(bc, len) {
17
12
  const byteCount = len * U16_BYTE_COUNT;
18
- return new Uint16Array(decodeFixedData(bc, byteCount));
13
+ return new Uint16Array(readFixedData(bc, byteCount));
19
14
  }
20
- function decodeU16FixedArrayBE(bc, len) {
15
+ function readU16FixedArrayBE(bc, len) {
21
16
  bc.check(len * U16_BYTE_COUNT);
22
17
  const result = new Uint16Array(len);
23
18
  for (let i = 0; i < len; i++)
24
- result[i] = decodeU16(bc);
19
+ result[i] = readU16(bc);
25
20
  return result;
26
21
  }
27
- var encodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU16FixedArrayLE : encodeU16FixedArrayBE;
28
- function encodeU16Array(bc, x) {
29
- encodeUintSafe(bc, x.length);
22
+ var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLE : writeU16FixedArrayBE;
23
+ function writeU16Array(bc, x) {
24
+ writeUintSafe(bc, x.length);
30
25
  if (x.length !== 0) {
31
- encodeU16FixedArray(bc, x, x.length);
26
+ writeU16FixedArray(bc, x);
32
27
  }
33
28
  }
34
- function encodeU16FixedArrayLE(bc, x, len) {
35
- assert(x.length === len);
36
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
29
+ function writeU16FixedArrayLE(bc, x) {
30
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
31
  }
38
- function encodeU16FixedArrayBE(bc, x, len) {
39
- assert(x.length === len);
32
+ function writeU16FixedArrayBE(bc, x) {
40
33
  bc.reserve(x.length * U16_BYTE_COUNT);
41
34
  for (let i = 0; i < x.length; i++)
42
- encodeU16(bc, x[i]);
35
+ writeU16(bc, x[i]);
43
36
  }
44
37
  export {
45
- decodeU16Array,
46
- decodeU16FixedArray,
47
- encodeU16Array,
48
- encodeU16FixedArray
38
+ readU16Array,
39
+ readU16FixedArray,
40
+ writeU16Array,
41
+ writeU16FixedArray
49
42
  };
50
43
  //# sourceMappingURL=u16-array.js.map
@@ -1,8 +1,8 @@
1
1
  import type { ByteCursor } from "../core/index.js";
2
- export declare const decodeU32FixedArray: typeof decodeU32FixedArrayLE;
3
- export declare function decodeU32Array(bc: ByteCursor): Uint32Array;
4
- declare function decodeU32FixedArrayLE(bc: ByteCursor, len: number): Uint32Array;
5
- export declare const encodeU32FixedArray: typeof encodeU32FixedArrayLE;
6
- export declare function encodeU32Array(bc: ByteCursor, x: Uint32Array): void;
7
- declare function encodeU32FixedArrayLE(bc: ByteCursor, x: Uint32Array, len: number): void;
2
+ export declare const readU32FixedArray: typeof readU32FixedArrayLE;
3
+ export declare function readU32Array(bc: ByteCursor): Uint32Array;
4
+ declare function readU32FixedArrayLE(bc: ByteCursor, len: number): Uint32Array;
5
+ export declare const writeU32FixedArray: typeof writeU32FixedArrayLE;
6
+ export declare function writeU32Array(bc: ByteCursor, x: Uint32Array): void;
7
+ declare function writeU32FixedArrayLE(bc: ByteCursor, x: Uint32Array): void;
8
8
  export {};
@@ -1,50 +1,43 @@
1
1
  // src/codec/u32-array.ts
2
- import { ok as assert } from "assert";
3
2
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
- import { decodeFixedData } from "./data.js";
5
- import {
6
- decodeU32,
7
- decodeUintSafe,
8
- encodeU32,
9
- encodeUintSafe
10
- } from "./primitive.js";
3
+ import { readFixedData } from "./data.js";
4
+ import { readU32, readUintSafe, writeU32, writeUintSafe } from "./primitive.js";
5
+ import { writeU8FixedArray } from "./u8-array.js";
11
6
  var U32_BYTE_COUNT = 4;
12
- var decodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU32FixedArrayLE : decodeU32FixedArrayBE;
13
- function decodeU32Array(bc) {
14
- return decodeU32FixedArray(bc, decodeUintSafe(bc));
7
+ var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
8
+ function readU32Array(bc) {
9
+ return readU32FixedArray(bc, readUintSafe(bc));
15
10
  }
16
- function decodeU32FixedArrayLE(bc, len) {
11
+ function readU32FixedArrayLE(bc, len) {
17
12
  const byteCount = len * U32_BYTE_COUNT;
18
- return new Uint32Array(decodeFixedData(bc, byteCount));
13
+ return new Uint32Array(readFixedData(bc, byteCount));
19
14
  }
20
- function decodeU32FixedArrayBE(bc, len) {
15
+ function readU32FixedArrayBE(bc, len) {
21
16
  bc.check(len * U32_BYTE_COUNT);
22
17
  const result = new Uint32Array(len);
23
18
  for (let i = 0; i < len; i++)
24
- result[i] = decodeU32(bc);
19
+ result[i] = readU32(bc);
25
20
  return result;
26
21
  }
27
- var encodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU32FixedArrayLE : encodeU32FixedArrayBE;
28
- function encodeU32Array(bc, x) {
29
- encodeUintSafe(bc, x.length);
22
+ var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLE : writeU32FixedArrayBE;
23
+ function writeU32Array(bc, x) {
24
+ writeUintSafe(bc, x.length);
30
25
  if (x.length !== 0) {
31
- encodeU32FixedArray(bc, x, x.length);
26
+ writeU32FixedArray(bc, x);
32
27
  }
33
28
  }
34
- function encodeU32FixedArrayLE(bc, x, len) {
35
- assert(x.length === len);
36
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
29
+ function writeU32FixedArrayLE(bc, x) {
30
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
31
  }
38
- function encodeU32FixedArrayBE(bc, x, len) {
39
- assert(x.length === len);
32
+ function writeU32FixedArrayBE(bc, x) {
40
33
  bc.reserve(x.length * U32_BYTE_COUNT);
41
34
  for (let i = 0; i < x.length; i++)
42
- encodeU32(bc, x[i]);
35
+ writeU32(bc, x[i]);
43
36
  }
44
37
  export {
45
- decodeU32Array,
46
- decodeU32FixedArray,
47
- encodeU32Array,
48
- encodeU32FixedArray
38
+ readU32Array,
39
+ readU32FixedArray,
40
+ writeU32Array,
41
+ writeU32FixedArray
49
42
  };
50
43
  //# sourceMappingURL=u32-array.js.map
@@ -1,8 +1,8 @@
1
1
  import type { ByteCursor } from "../core/index.js";
2
- export declare const decodeU64FixedArray: typeof decodeU64FixedArrayLE;
3
- export declare function decodeU64Array(bc: ByteCursor): BigUint64Array;
4
- declare function decodeU64FixedArrayLE(bc: ByteCursor, len: number): BigUint64Array;
5
- export declare const encodeU64FixedArray: typeof encodeU64FixedArrayLE;
6
- export declare function encodeU64Array(bc: ByteCursor, x: BigUint64Array): void;
7
- declare function encodeU64FixedArrayLE(bc: ByteCursor, x: BigUint64Array, len: number): void;
2
+ export declare const readU64FixedArray: typeof readU64FixedArrayLE;
3
+ export declare function readU64Array(bc: ByteCursor): BigUint64Array;
4
+ declare function readU64FixedArrayLE(bc: ByteCursor, len: number): BigUint64Array;
5
+ export declare const writeU64FixedArray: typeof writeU64FixedArrayLE;
6
+ export declare function writeU64Array(bc: ByteCursor, x: BigUint64Array): void;
7
+ declare function writeU64FixedArrayLE(bc: ByteCursor, x: BigUint64Array): void;
8
8
  export {};
@@ -1,50 +1,43 @@
1
1
  // src/codec/u64-array.ts
2
- import { ok as assert } from "assert";
3
2
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
- import { decodeFixedData } from "./data.js";
5
- import {
6
- decodeU64,
7
- decodeUintSafe,
8
- encodeU64,
9
- encodeUintSafe
10
- } from "./primitive.js";
3
+ import { readFixedData } from "./data.js";
4
+ import { readU64, readUintSafe, writeU64, writeUintSafe } from "./primitive.js";
5
+ import { writeU8FixedArray } from "./u8-array.js";
11
6
  var U64_BYTE_COUNT = 8;
12
- var decodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU64FixedArrayLE : decodeU64FixedArrayBE;
13
- function decodeU64Array(bc) {
14
- return decodeU64FixedArray(bc, decodeUintSafe(bc));
7
+ var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
8
+ function readU64Array(bc) {
9
+ return readU64FixedArray(bc, readUintSafe(bc));
15
10
  }
16
- function decodeU64FixedArrayLE(bc, len) {
11
+ function readU64FixedArrayLE(bc, len) {
17
12
  const byteCount = len * U64_BYTE_COUNT;
18
- return new BigUint64Array(decodeFixedData(bc, byteCount));
13
+ return new BigUint64Array(readFixedData(bc, byteCount));
19
14
  }
20
- function decodeU64FixedArrayBE(bc, len) {
15
+ function readU64FixedArrayBE(bc, len) {
21
16
  bc.check(len * U64_BYTE_COUNT);
22
17
  const result = new BigUint64Array(len);
23
18
  for (let i = 0; i < len; i++)
24
- result[i] = decodeU64(bc);
19
+ result[i] = readU64(bc);
25
20
  return result;
26
21
  }
27
- var encodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU64FixedArrayLE : encodeU64FixedArrayBE;
28
- function encodeU64Array(bc, x) {
29
- encodeUintSafe(bc, x.length);
22
+ var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLE : writeU64FixedArrayBE;
23
+ function writeU64Array(bc, x) {
24
+ writeUintSafe(bc, x.length);
30
25
  if (x.length !== 0) {
31
- encodeU64FixedArray(bc, x, x.length);
26
+ writeU64FixedArray(bc, x);
32
27
  }
33
28
  }
34
- function encodeU64FixedArrayLE(bc, x, len) {
35
- assert(x.length === len);
36
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
29
+ function writeU64FixedArrayLE(bc, x) {
30
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
31
  }
38
- function encodeU64FixedArrayBE(bc, x, len) {
39
- assert(x.length === len);
32
+ function writeU64FixedArrayBE(bc, x) {
40
33
  bc.reserve(x.length * U64_BYTE_COUNT);
41
34
  for (let i = 0; i < x.length; i++)
42
- encodeU64(bc, x[i]);
35
+ writeU64(bc, x[i]);
43
36
  }
44
37
  export {
45
- decodeU64Array,
46
- decodeU64FixedArray,
47
- encodeU64Array,
48
- encodeU64FixedArray
38
+ readU64Array,
39
+ readU64FixedArray,
40
+ writeU64Array,
41
+ writeU64FixedArray
49
42
  };
50
43
  //# sourceMappingURL=u64-array.js.map
@@ -1,5 +1,5 @@
1
1
  import type { ByteCursor } from "../core/index.js";
2
- export declare function decodeU8Array(bc: ByteCursor): Uint8Array;
3
- export declare function encodeU8Array(bc: ByteCursor, x: Uint8Array): void;
4
- export declare function decodeU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
5
- export declare function encodeU8FixedArray(bc: ByteCursor, x: Uint8Array, len: number): void;
2
+ export declare function readU8Array(bc: ByteCursor): Uint8Array;
3
+ export declare function writeU8Array(bc: ByteCursor, x: Uint8Array): void;
4
+ export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
5
+ export declare function writeU8FixedArray(bc: ByteCursor, x: Uint8Array): void;
@@ -1,25 +1,27 @@
1
1
  // src/codec/u8-array.ts
2
- import { ok as assert } from "assert";
3
- import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
- function decodeU8Array(bc) {
5
- const len = decodeUintSafe(bc);
6
- return bc.read(len).slice();
2
+ import { readUintSafe, writeUintSafe } from "./primitive.js";
3
+ function readU8Array(bc) {
4
+ return readU8FixedArray(bc, readUintSafe(bc));
7
5
  }
8
- function encodeU8Array(bc, x) {
9
- encodeUintSafe(bc, x.length);
10
- bc.write(x);
6
+ function writeU8Array(bc, x) {
7
+ writeUintSafe(bc, x.length);
8
+ writeU8FixedArray(bc, x);
11
9
  }
12
- function decodeU8FixedArray(bc, len) {
10
+ function readU8FixedArray(bc, len) {
13
11
  return bc.read(len).slice();
14
12
  }
15
- function encodeU8FixedArray(bc, x, len) {
16
- assert(x.length === len);
17
- bc.write(x);
13
+ function writeU8FixedArray(bc, x) {
14
+ const len = x.length;
15
+ if (len !== 0) {
16
+ bc.reserve(len);
17
+ bc.bytes.set(x, bc.offset);
18
+ bc.offset += len;
19
+ }
18
20
  }
19
21
  export {
20
- decodeU8Array,
21
- decodeU8FixedArray,
22
- encodeU8Array,
23
- encodeU8FixedArray
22
+ readU8Array,
23
+ readU8FixedArray,
24
+ writeU8Array,
25
+ writeU8FixedArray
24
26
  };
25
27
  //# sourceMappingURL=u8-array.js.map
@@ -1,5 +1,5 @@
1
1
  import type { ByteCursor } from "../core/index.js";
2
- export declare function decodeU8ClampedArray(bc: ByteCursor): Uint8ClampedArray;
3
- export declare function encodeU8ClampedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
4
- export declare function decodeU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray;
5
- export declare function encodeU8ClampedFixedArray(bc: ByteCursor, x: Uint8ClampedArray, len: number): void;
2
+ export declare function readU8ClampedArray(bc: ByteCursor): Uint8ClampedArray;
3
+ export declare function writeU8ClampedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
4
+ export declare function readU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray;
5
+ export declare function writeU8ClampedFixedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
@@ -1,24 +1,24 @@
1
1
  // src/codec/u8-clamped-array.ts
2
- import { decodeFixedData } from "./data.js";
3
- import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
- import { encodeU8FixedArray } from "./u8-array.js";
5
- function decodeU8ClampedArray(bc) {
6
- return decodeU8ClampedFixedArray(bc, decodeUintSafe(bc));
2
+ import { readFixedData } from "./data.js";
3
+ import { readUintSafe, writeUintSafe } from "./primitive.js";
4
+ import { writeU8FixedArray } from "./u8-array.js";
5
+ function readU8ClampedArray(bc) {
6
+ return readU8ClampedFixedArray(bc, readUintSafe(bc));
7
7
  }
8
- function encodeU8ClampedArray(bc, x) {
9
- encodeUintSafe(bc, x.length);
10
- encodeU8ClampedFixedArray(bc, x, x.length);
8
+ function writeU8ClampedArray(bc, x) {
9
+ writeUintSafe(bc, x.length);
10
+ writeU8ClampedFixedArray(bc, x);
11
11
  }
12
- function decodeU8ClampedFixedArray(bc, len) {
13
- return new Uint8ClampedArray(decodeFixedData(bc, len));
12
+ function readU8ClampedFixedArray(bc, len) {
13
+ return new Uint8ClampedArray(readFixedData(bc, len));
14
14
  }
15
- function encodeU8ClampedFixedArray(bc, x, len) {
16
- encodeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength), len);
15
+ function writeU8ClampedFixedArray(bc, x) {
16
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
17
17
  }
18
18
  export {
19
- decodeU8ClampedArray,
20
- decodeU8ClampedFixedArray,
21
- encodeU8ClampedArray,
22
- encodeU8ClampedFixedArray
19
+ readU8ClampedArray,
20
+ readU8ClampedFixedArray,
21
+ writeU8ClampedArray,
22
+ writeU8ClampedFixedArray
23
23
  };
24
24
  //# sourceMappingURL=u8-clamped-array.js.map
@@ -1,9 +1,32 @@
1
1
  import type { Config } from "./config.js";
2
+ /**
3
+ * @invariant bytes.buffer === view.buffer
4
+ * @invariant bytes.byteOffset === view.byteOffset
5
+ * @invariant bytes.byteLength === view.byteLength
6
+ * @invariant 0 <= offset <= bytes.byteLength
7
+ * @invariant bytes.byteLength <= config.maxBufferLength
8
+ *
9
+ * | {bytes,view}.buffer |
10
+ * | bytes |
11
+ * | view |
12
+ * |<------ offset ------>|
13
+ * |<----------- config.maxBufferLength ------------>|
14
+ */
2
15
  export declare class ByteCursor {
16
+ bytes: Uint8Array;
3
17
  readonly config: Config;
18
+ /**
19
+ * Read and write Offset in {@link view} and {@link bytes}
20
+ */
4
21
  offset: number;
5
22
  view: DataView;
6
- constructor(bytes: ArrayBuffer | Uint8Array, config: Config);
23
+ /**
24
+ * @param bytes read and/or write buffer
25
+ * @param config runtime configuration
26
+ * @throw BareError when the buffer exceeds the maximum allowed length
27
+ * `config.maxBufferLength`
28
+ */
29
+ constructor(bytes: Uint8Array, config: Config);
7
30
  /**
8
31
  * @param min number of needed bytes
9
32
  * @throw BareError when there is not enough bytes
@@ -11,6 +34,8 @@ export declare class ByteCursor {
11
34
  check(min: number): void;
12
35
  /**
13
36
  * @param min number of bytes to reserve
37
+ * @throw BareError when the buffer exceeds the maximum allowed length
38
+ * `config.maxBufferLength`
14
39
  */
15
40
  reserve(min: number): void;
16
41
  /**
@@ -21,11 +46,4 @@ export declare class ByteCursor {
21
46
  * @returns read bytes
22
47
  */
23
48
  read(len: number): Uint8Array;
24
- /**
25
- * Write {@code bytes} in the buffer and advance cursor by
26
- * {@code bytes.length}
27
- *
28
- * @param bytes bytes to copy
29
- */
30
- write(bytes: Uint8Array): void;
31
49
  }
@@ -1,49 +1,39 @@
1
1
  // src/core/byte-cursor.ts
2
- import { ok as assert } from "assert";
3
- import { BareError } from "../core/bare-error.js";
2
+ import { BareError } from "./bare-error.js";
4
3
  var TOO_LARGE_BUFFER = "too large buffer";
5
4
  var ByteCursor = class {
6
5
  constructor(bytes, config) {
7
- this.config = config;
8
- this.offset = 0;
9
- this.view = bytes instanceof Uint8Array ? new DataView(bytes.buffer, bytes.byteOffset, bytes.length) : new DataView(bytes);
10
- if (this.view.byteLength > config.maxBufferLength) {
6
+ if (bytes.length > config.maxBufferLength) {
11
7
  throw new BareError(0, TOO_LARGE_BUFFER);
12
8
  }
9
+ this.bytes = bytes;
10
+ this.config = config;
11
+ this.offset = 0;
12
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
13
13
  }
14
14
  check(min) {
15
- if (this.offset + min > this.view.byteLength) {
15
+ if (this.offset + min > this.bytes.length) {
16
16
  throw new BareError(this.offset, "missing bytes");
17
17
  }
18
18
  }
19
19
  reserve(min) {
20
- const { config, offset, view } = this;
21
- if (offset + min > view.byteLength) {
22
- const bytes = new Uint8Array(view.buffer);
23
- const minExtraLength = min + offset - view.byteLength;
24
- assert(bytes.length === view.byteOffset + view.byteLength, "un-growable buffer");
25
- assert(bytes.length + minExtraLength <= config.maxBufferLength, TOO_LARGE_BUFFER);
26
- const newLen = Math.min(bytes.length + Math.max(minExtraLength, bytes.length), config.maxBufferLength);
20
+ const minLen = this.offset + min | 0;
21
+ if (minLen > this.bytes.length) {
22
+ if (minLen > this.config.maxBufferLength) {
23
+ throw new BareError(0, TOO_LARGE_BUFFER);
24
+ }
25
+ const newLen = Math.min(minLen << 1, this.config.maxBufferLength);
27
26
  const newBytes = new Uint8Array(newLen);
28
- newBytes.set(bytes);
29
- this.view = new DataView(newBytes.buffer, view.byteOffset);
27
+ newBytes.set(this.bytes);
28
+ this.bytes = newBytes;
29
+ this.view = new DataView(newBytes.buffer);
30
30
  }
31
31
  }
32
32
  read(len) {
33
33
  this.check(len);
34
- const bufferOffset = this.view.byteOffset + this.offset;
34
+ const offset = this.offset;
35
35
  this.offset += len;
36
- return new Uint8Array(this.view.buffer, bufferOffset, len);
37
- }
38
- write(bytes) {
39
- const len = bytes.length;
40
- if (len !== 0) {
41
- this.reserve(len);
42
- const bufferOffset = this.view.byteOffset + this.offset;
43
- const buffer = new Uint8Array(this.view.buffer);
44
- buffer.set(bytes, bufferOffset);
45
- this.offset += len;
46
- }
36
+ return this.bytes.subarray(offset, offset + len);
47
37
  }
48
38
  };
49
39
  export {