@bare-ts/lib 0.3.0 → 0.5.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.
Files changed (57) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +7 -8
  3. package/dist/codec/data.d.ts +1 -1
  4. package/dist/codec/data.js +10 -12
  5. package/dist/codec/float-array.d.ts +7 -12
  6. package/dist/codec/float-array.js +53 -45
  7. package/dist/codec/i16-array.d.ts +4 -7
  8. package/dist/codec/i16-array.js +35 -27
  9. package/dist/codec/i32-array.d.ts +4 -7
  10. package/dist/codec/i32-array.js +35 -27
  11. package/dist/codec/i64-array.d.ts +4 -7
  12. package/dist/codec/i64-array.js +35 -27
  13. package/dist/codec/i8-array.d.ts +3 -3
  14. package/dist/codec/i8-array.js +12 -14
  15. package/dist/codec/primitive.d.ts +3 -1
  16. package/dist/codec/primitive.js +227 -149
  17. package/dist/codec/string.d.ts +1 -1
  18. package/dist/codec/string.js +45 -31
  19. package/dist/codec/u16-array.d.ts +4 -7
  20. package/dist/codec/u16-array.js +35 -27
  21. package/dist/codec/u32-array.d.ts +4 -7
  22. package/dist/codec/u32-array.js +35 -27
  23. package/dist/codec/u64-array.d.ts +4 -7
  24. package/dist/codec/u64-array.js +35 -27
  25. package/dist/codec/u8-array.d.ts +9 -3
  26. package/dist/codec/u8-array.js +23 -18
  27. package/dist/codec/u8-clamped-array.d.ts +3 -3
  28. package/dist/codec/u8-clamped-array.js +13 -15
  29. package/dist/core/bare-error.d.ts +4 -1
  30. package/dist/core/bare-error.js +4 -8
  31. package/dist/core/byte-cursor.d.ts +22 -28
  32. package/dist/core/byte-cursor.js +33 -29
  33. package/dist/core/config.d.ts +4 -10
  34. package/dist/core/config.js +15 -20
  35. package/dist/env/dev.d.ts +1 -0
  36. package/dist/env/dev.development.d.ts +1 -0
  37. package/dist/env/dev.development.js +2 -0
  38. package/dist/env/dev.js +2 -0
  39. package/dist/env/dev.node.d.ts +1 -0
  40. package/dist/env/dev.node.js +2 -0
  41. package/dist/index.cjs +545 -324
  42. package/dist/index.d.cts +16 -0
  43. package/dist/index.d.ts +16 -2
  44. package/dist/index.js +17 -4
  45. package/dist/util/assert.d.ts +6 -8
  46. package/dist/util/assert.js +10 -13
  47. package/dist/util/constants.d.ts +4 -7
  48. package/dist/util/constants.js +11 -30
  49. package/dist/util/validator.d.ts +1 -1
  50. package/dist/util/validator.js +12 -24
  51. package/package.json +31 -25
  52. package/dist/codec/index.d.ts +0 -13
  53. package/dist/codec/index.js +0 -15
  54. package/dist/core/index.d.ts +0 -3
  55. package/dist/core/index.js +0 -5
  56. package/dist/util/util.d.ts +0 -1
  57. package/dist/util/util.js +0 -6
@@ -1,8 +1,5 @@
1
- import type { ByteCursor } from "../core/index.js";
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;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare const readU16FixedArray: (bc: ByteCursor, len: number) => Uint16Array<ArrayBuffer>;
3
+ export declare function readU16Array(bc: ByteCursor): Uint16Array<ArrayBuffer>;
4
+ export declare const writeU16FixedArray: (bc: ByteCursor, x: Uint16Array) => void;
6
5
  export declare function writeU16Array(bc: ByteCursor, x: Uint16Array): void;
7
- declare function writeU16FixedArrayLE(bc: ByteCursor, x: Uint16Array): void;
8
- export {};
@@ -1,43 +1,51 @@
1
- // src/codec/u16-array.ts
2
- import { U16_BYTE_COUNT } from "../util/constants.js";
3
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { assert, DEV } from "../util/assert.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
+ import { isU32 } from "../util/validator.js";
4
6
  import { readFixedData } from "./data.js";
5
- import { readU16, readUintSafe, writeU16, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readU16,
9
+ readUintSafe32,
10
+ writeU16,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
8
- function readU16Array(bc) {
9
- return readU16FixedArray(bc, readUintSafe(bc));
14
+ export const readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLe : readU16FixedArrayBe;
15
+ export function readU16Array(bc) {
16
+ return readU16FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readU16FixedArrayLE(bc, len) {
12
- const byteCount = len * U16_BYTE_COUNT;
18
+ function readU16FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 2;
13
23
  return new Uint16Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readU16FixedArrayBE(bc, len) {
16
- bc.check(len * U16_BYTE_COUNT);
25
+ function readU16FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 2);
17
30
  const result = new Uint16Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readU16(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLE : writeU16FixedArrayBE;
23
- function writeU16Array(bc, x) {
24
- writeUintSafe(bc, x.length);
25
- if (x.length !== 0) {
36
+ export const writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLe : writeU16FixedArrayBe;
37
+ export function writeU16Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
39
+ if (x.length > 0) {
26
40
  writeU16FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeU16FixedArrayLE(bc, x) {
43
+ function writeU16FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeU16FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * U16_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeU16FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 2);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeU16(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readU16Array,
39
- readU16FixedArray,
40
- writeU16Array,
41
- writeU16FixedArray
42
- };
43
- //# sourceMappingURL=u16-array.js.map
@@ -1,8 +1,5 @@
1
- import type { ByteCursor } from "../core/index.js";
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;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare const readU32FixedArray: (bc: ByteCursor, len: number) => Uint32Array<ArrayBuffer>;
3
+ export declare function readU32Array(bc: ByteCursor): Uint32Array<ArrayBuffer>;
4
+ export declare const writeU32FixedArray: (bc: ByteCursor, x: Uint32Array) => void;
6
5
  export declare function writeU32Array(bc: ByteCursor, x: Uint32Array): void;
7
- declare function writeU32FixedArrayLE(bc: ByteCursor, x: Uint32Array): void;
8
- export {};
@@ -1,43 +1,51 @@
1
- // src/codec/u32-array.ts
2
- import { U32_BYTE_COUNT } from "../util/constants.js";
3
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { assert, DEV } from "../util/assert.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
+ import { isU32 } from "../util/validator.js";
4
6
  import { readFixedData } from "./data.js";
5
- import { readU32, readUintSafe, writeU32, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readU32,
9
+ readUintSafe32,
10
+ writeU32,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
8
- function readU32Array(bc) {
9
- return readU32FixedArray(bc, readUintSafe(bc));
14
+ export const readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLe : readU32FixedArrayBe;
15
+ export function readU32Array(bc) {
16
+ return readU32FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readU32FixedArrayLE(bc, len) {
12
- const byteCount = len * U32_BYTE_COUNT;
18
+ function readU32FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 4;
13
23
  return new Uint32Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readU32FixedArrayBE(bc, len) {
16
- bc.check(len * U32_BYTE_COUNT);
25
+ function readU32FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 4);
17
30
  const result = new Uint32Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readU32(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLE : writeU32FixedArrayBE;
23
- function writeU32Array(bc, x) {
24
- writeUintSafe(bc, x.length);
25
- if (x.length !== 0) {
36
+ export const writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLe : writeU32FixedArrayBe;
37
+ export function writeU32Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
39
+ if (x.length > 0) {
26
40
  writeU32FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeU32FixedArrayLE(bc, x) {
43
+ function writeU32FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeU32FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * U32_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeU32FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 4);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeU32(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readU32Array,
39
- readU32FixedArray,
40
- writeU32Array,
41
- writeU32FixedArray
42
- };
43
- //# sourceMappingURL=u32-array.js.map
@@ -1,8 +1,5 @@
1
- import type { ByteCursor } from "../core/index.js";
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;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare const readU64FixedArray: (bc: ByteCursor, len: number) => BigUint64Array<ArrayBuffer>;
3
+ export declare function readU64Array(bc: ByteCursor): BigUint64Array<ArrayBuffer>;
4
+ export declare const writeU64FixedArray: (bc: ByteCursor, x: BigUint64Array) => void;
6
5
  export declare function writeU64Array(bc: ByteCursor, x: BigUint64Array): void;
7
- declare function writeU64FixedArrayLE(bc: ByteCursor, x: BigUint64Array): void;
8
- export {};
@@ -1,43 +1,51 @@
1
- // src/codec/u64-array.ts
2
- import { U64_BYTE_COUNT } from "../util/constants.js";
3
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { assert, DEV } from "../util/assert.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
+ import { isU32 } from "../util/validator.js";
4
6
  import { readFixedData } from "./data.js";
5
- import { readU64, readUintSafe, writeU64, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readU64,
9
+ readUintSafe32,
10
+ writeU64,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
8
- function readU64Array(bc) {
9
- return readU64FixedArray(bc, readUintSafe(bc));
14
+ export const readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLe : readU64FixedArrayBe;
15
+ export function readU64Array(bc) {
16
+ return readU64FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readU64FixedArrayLE(bc, len) {
12
- const byteCount = len * U64_BYTE_COUNT;
18
+ function readU64FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 8;
13
23
  return new BigUint64Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readU64FixedArrayBE(bc, len) {
16
- bc.check(len * U64_BYTE_COUNT);
25
+ function readU64FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 8);
17
30
  const result = new BigUint64Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readU64(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLE : writeU64FixedArrayBE;
23
- function writeU64Array(bc, x) {
24
- writeUintSafe(bc, x.length);
25
- if (x.length !== 0) {
36
+ export const writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLe : writeU64FixedArrayBe;
37
+ export function writeU64Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
39
+ if (x.length > 0) {
26
40
  writeU64FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeU64FixedArrayLE(bc, x) {
43
+ function writeU64FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeU64FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * U64_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeU64FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 8);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeU64(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readU64Array,
39
- readU64FixedArray,
40
- writeU64Array,
41
- writeU64FixedArray
42
- };
43
- //# sourceMappingURL=u64-array.js.map
@@ -1,5 +1,11 @@
1
- import type { ByteCursor } from "../core/index.js";
2
- export declare function readU8Array(bc: ByteCursor): Uint8Array;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare function readU8Array(bc: ByteCursor): Uint8Array<ArrayBuffer>;
3
3
  export declare function writeU8Array(bc: ByteCursor, x: Uint8Array): void;
4
- export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
4
+ export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array<ArrayBuffer>;
5
5
  export declare function writeU8FixedArray(bc: ByteCursor, x: Uint8Array): void;
6
+ /**
7
+ * Advance `bc` by `len` bytes and return a view of the read bytes.
8
+ *
9
+ * WARNING: The returned array should not be modified.
10
+ */
11
+ export declare function readUnsafeU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
@@ -1,27 +1,32 @@
1
- // src/codec/u8-array.ts
2
- import { readUintSafe, writeUintSafe } from "./primitive.js";
3
- function readU8Array(bc) {
4
- return readU8FixedArray(bc, readUintSafe(bc));
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { assert, DEV } from "../util/assert.js";
4
+ import { isU32 } from "../util/validator.js";
5
+ import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
6
+ export function readU8Array(bc) {
7
+ return readU8FixedArray(bc, readUintSafe32(bc));
5
8
  }
6
- function writeU8Array(bc, x) {
7
- writeUintSafe(bc, x.length);
9
+ export function writeU8Array(bc, x) {
10
+ writeUintSafe32(bc, x.length);
8
11
  writeU8FixedArray(bc, x);
9
12
  }
10
- function readU8FixedArray(bc, len) {
11
- return bc.read(len).slice();
13
+ export function readU8FixedArray(bc, len) {
14
+ return readUnsafeU8FixedArray(bc, len).slice();
12
15
  }
13
- function writeU8FixedArray(bc, x) {
16
+ export function writeU8FixedArray(bc, x) {
14
17
  const len = x.length;
15
- if (len !== 0) {
16
- bc.reserve(len);
18
+ if (len > 0) {
19
+ reserve(bc, len);
17
20
  bc.bytes.set(x, bc.offset);
18
21
  bc.offset += len;
19
22
  }
20
23
  }
21
- export {
22
- readU8Array,
23
- readU8FixedArray,
24
- writeU8Array,
25
- writeU8FixedArray
26
- };
27
- //# sourceMappingURL=u8-array.js.map
24
+ export function readUnsafeU8FixedArray(bc, len) {
25
+ if (DEV) {
26
+ assert(isU32(len));
27
+ }
28
+ check(bc, len);
29
+ const offset = bc.offset;
30
+ bc.offset += len;
31
+ return bc.bytes.subarray(offset, offset + len);
32
+ }
@@ -1,5 +1,5 @@
1
- import type { ByteCursor } from "../core/index.js";
2
- export declare function readU8ClampedArray(bc: ByteCursor): Uint8ClampedArray;
1
+ import type { ByteCursor } from "../core/byte-cursor.js";
2
+ export declare function readU8ClampedArray(bc: ByteCursor): Uint8ClampedArray<ArrayBuffer>;
3
3
  export declare function writeU8ClampedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
4
- export declare function readU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray;
4
+ export declare function readU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray<ArrayBuffer>;
5
5
  export declare function writeU8ClampedFixedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
@@ -1,24 +1,22 @@
1
- // src/codec/u8-clamped-array.ts
1
+ "use strict";
2
+ import { assert, DEV } from "../util/assert.js";
3
+ import { isU32 } from "../util/validator.js";
2
4
  import { readFixedData } from "./data.js";
3
- import { readUintSafe, writeUintSafe } from "./primitive.js";
5
+ import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
4
6
  import { writeU8FixedArray } from "./u8-array.js";
5
- function readU8ClampedArray(bc) {
6
- return readU8ClampedFixedArray(bc, readUintSafe(bc));
7
+ export function readU8ClampedArray(bc) {
8
+ return readU8ClampedFixedArray(bc, readUintSafe32(bc));
7
9
  }
8
- function writeU8ClampedArray(bc, x) {
9
- writeUintSafe(bc, x.length);
10
+ export function writeU8ClampedArray(bc, x) {
11
+ writeUintSafe32(bc, x.length);
10
12
  writeU8ClampedFixedArray(bc, x);
11
13
  }
12
- function readU8ClampedFixedArray(bc, len) {
14
+ export function readU8ClampedFixedArray(bc, len) {
15
+ if (DEV) {
16
+ assert(isU32(len));
17
+ }
13
18
  return new Uint8ClampedArray(readFixedData(bc, len));
14
19
  }
15
- function writeU8ClampedFixedArray(bc, x) {
20
+ export function writeU8ClampedFixedArray(bc, x) {
16
21
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
17
22
  }
18
- export {
19
- readU8ClampedArray,
20
- readU8ClampedFixedArray,
21
- writeU8ClampedArray,
22
- writeU8ClampedFixedArray
23
- };
24
- //# sourceMappingURL=u8-clamped-array.js.map
@@ -1,5 +1,8 @@
1
+ /**
2
+ * @sealed
3
+ */
1
4
  export declare class BareError extends Error {
2
- readonly name: "BareError";
5
+ name: string;
3
6
  readonly cause: unknown;
4
7
  readonly issue: string;
5
8
  readonly offset: number;
@@ -1,14 +1,10 @@
1
- // src/core/bare-error.ts
2
- var BareError = class extends Error {
1
+ "use strict";
2
+ export class BareError extends Error {
3
3
  constructor(offset, issue, opts) {
4
4
  super(`(byte:${offset}) ${issue}`);
5
5
  this.name = "BareError";
6
6
  this.issue = issue;
7
7
  this.offset = offset;
8
- this.cause = opts == null ? void 0 : opts.cause;
8
+ this.cause = opts?.cause;
9
9
  }
10
- };
11
- export {
12
- BareError
13
- };
14
- //# sourceMappingURL=bare-error.js.map
10
+ }
@@ -1,16 +1,20 @@
1
1
  import type { Config } from "./config.js";
2
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
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
8
  *
9
+ * ```txt
9
10
  * | {bytes,view}.buffer |
10
11
  * | bytes |
11
12
  * | view |
12
13
  * |<------ offset ------>|
13
14
  * |<----------- config.maxBufferLength ------------>|
15
+ * ```
16
+ *
17
+ * @sealed
14
18
  */
15
19
  export declare class ByteCursor {
16
20
  bytes: Uint8Array;
@@ -21,29 +25,19 @@ export declare class ByteCursor {
21
25
  offset: number;
22
26
  view: DataView;
23
27
  /**
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
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`
28
29
  */
29
30
  constructor(bytes: Uint8Array, config: Config);
30
- /**
31
- * @param min number of needed bytes
32
- * @throw BareError when there is not enough bytes
33
- */
34
- check(min: number): void;
35
- /**
36
- * @param min number of bytes to reserve
37
- * @throw BareError when the buffer exceeds the maximum allowed length
38
- * `config.maxBufferLength`
39
- */
40
- reserve(min: number): void;
41
- /**
42
- * Advance cursor by {@code len} bytes and return a view of the read bytes.
43
- * The returned array should not be modified.
44
- *
45
- * @param len number of bytes to read
46
- * @returns read bytes
47
- */
48
- read(len: number): Uint8Array;
49
31
  }
32
+ /**
33
+ * Check that `min` number of bytes are available.
34
+ *
35
+ * @throws {BareError} bytes are missing.
36
+ */
37
+ export declare function check(bc: ByteCursor, min: number): void;
38
+ /**
39
+ * Reserve `min` number of bytes.
40
+ *
41
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`.
42
+ */
43
+ export declare function reserve(bc: ByteCursor, min: number): void;
@@ -1,42 +1,46 @@
1
- // src/core/byte-cursor.ts
1
+ "use strict";
2
+ import { assert, DEV } from "../util/assert.js";
2
3
  import { TOO_LARGE_BUFFER } from "../util/constants.js";
4
+ import { isU32 } from "../util/validator.js";
3
5
  import { BareError } from "./bare-error.js";
4
- var ByteCursor = class {
6
+ export class ByteCursor {
7
+ /**
8
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`
9
+ */
5
10
  constructor(bytes, config) {
11
+ /**
12
+ * Read and write Offset in {@link view} and {@link bytes}
13
+ */
14
+ this.offset = 0;
6
15
  if (bytes.length > config.maxBufferLength) {
7
16
  throw new BareError(0, TOO_LARGE_BUFFER);
8
17
  }
9
18
  this.bytes = bytes;
10
19
  this.config = config;
11
- this.offset = 0;
12
20
  this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
13
21
  }
14
- check(min) {
15
- if (this.offset + min > this.bytes.length) {
16
- throw new BareError(this.offset, "missing bytes");
17
- }
22
+ }
23
+ export function check(bc, min) {
24
+ if (DEV) {
25
+ assert(isU32(min));
18
26
  }
19
- reserve(min) {
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);
26
- const newBytes = new Uint8Array(newLen);
27
- newBytes.set(this.bytes);
28
- this.bytes = newBytes;
29
- this.view = new DataView(newBytes.buffer);
30
- }
27
+ if (bc.offset + min > bc.bytes.length) {
28
+ throw new BareError(bc.offset, "missing bytes");
31
29
  }
32
- read(len) {
33
- this.check(len);
34
- const offset = this.offset;
35
- this.offset += len;
36
- return this.bytes.subarray(offset, offset + len);
30
+ }
31
+ export function reserve(bc, min) {
32
+ if (DEV) {
33
+ assert(isU32(min));
34
+ }
35
+ const minLen = bc.offset + min | 0;
36
+ if (minLen > bc.bytes.length) {
37
+ if (minLen > bc.config.maxBufferLength) {
38
+ throw new BareError(0, TOO_LARGE_BUFFER);
39
+ }
40
+ const newLen = Math.min(minLen << 1, bc.config.maxBufferLength);
41
+ const newBytes = new Uint8Array(newLen);
42
+ newBytes.set(bc.bytes);
43
+ bc.bytes = newBytes;
44
+ bc.view = new DataView(newBytes.buffer);
37
45
  }
38
- };
39
- export {
40
- ByteCursor
41
- };
42
- //# sourceMappingURL=byte-cursor.js.map
46
+ }
@@ -1,12 +1,6 @@
1
- export interface Config {
1
+ export type Config = {
2
2
  readonly initialBufferLength: number;
3
3
  readonly maxBufferLength: number;
4
- readonly textDecoderThreshold: number;
5
- readonly textEncoderThreshold: number;
6
- }
7
- export declare function Config({ initialBufferLength, maxBufferLength, textDecoderThreshold, textEncoderThreshold, }: {
8
- initialBufferLength?: number | undefined;
9
- maxBufferLength?: number | undefined;
10
- textDecoderThreshold?: number | undefined;
11
- textEncoderThreshold?: number | undefined;
12
- }): Config;
4
+ };
5
+ export declare function Config({ initialBufferLength, maxBufferLength, }: Partial<Config>): Config;
6
+ export declare const DEFAULT_CONFIG: Config;
@@ -1,27 +1,22 @@
1
- // src/core/config.ts
2
- import { assert } from "../util/assert.js";
1
+ "use strict";
2
+ import { assert, DEV } from "../util/assert.js";
3
3
  import { TOO_LARGE_NUMBER } from "../util/constants.js";
4
4
  import { isU32 } from "../util/validator.js";
5
- function Config({
5
+ export function Config({
6
6
  initialBufferLength = 1024,
7
- maxBufferLength = 1024 * 1024 * 32,
8
- textDecoderThreshold = 256,
9
- textEncoderThreshold = 256
7
+ maxBufferLength = 1024 * 1024 * 32
10
8
  }) {
11
- const config = {
9
+ if (DEV) {
10
+ assert(isU32(initialBufferLength), TOO_LARGE_NUMBER);
11
+ assert(isU32(maxBufferLength), TOO_LARGE_NUMBER);
12
+ assert(
13
+ initialBufferLength <= maxBufferLength,
14
+ "initialBufferLength must be lower than or equal to maxBufferLength"
15
+ );
16
+ }
17
+ return {
12
18
  initialBufferLength,
13
- maxBufferLength,
14
- textDecoderThreshold,
15
- textEncoderThreshold
19
+ maxBufferLength
16
20
  };
17
- assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
18
- assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
19
- assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
20
- assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
21
- assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
22
- return config;
23
21
  }
24
- export {
25
- Config
26
- };
27
- //# sourceMappingURL=config.js.map
22
+ export const DEFAULT_CONFIG = /* @__PURE__ */ Config({});
@@ -0,0 +1 @@
1
+ export declare const DEV = false;
@@ -0,0 +1 @@
1
+ export declare const DEV = true;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ export const DEV = true;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ export const DEV = false;
@@ -0,0 +1 @@
1
+ export declare const DEV: boolean;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ export const DEV = process.env.NODE_ENV === "development";