@bare-ts/lib 0.1.0 → 0.3.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 (44) hide show
  1. package/README.md +12 -2
  2. package/dist/codec/data.d.ts +4 -4
  3. package/dist/codec/data.js +26 -0
  4. package/dist/codec/float-array.d.ts +12 -12
  5. package/dist/codec/float-array.js +85 -0
  6. package/dist/codec/i16-array.d.ts +6 -6
  7. package/dist/codec/i16-array.js +43 -0
  8. package/dist/codec/i32-array.d.ts +6 -6
  9. package/dist/codec/i32-array.js +43 -0
  10. package/dist/codec/i64-array.d.ts +6 -6
  11. package/dist/codec/i64-array.js +43 -0
  12. package/dist/codec/i8-array.d.ts +4 -4
  13. package/dist/codec/i8-array.js +24 -0
  14. package/dist/codec/index.js +15 -0
  15. package/dist/codec/primitive.d.ts +34 -36
  16. package/dist/codec/primitive.js +345 -0
  17. package/dist/codec/string.d.ts +4 -2
  18. package/dist/codec/string.js +126 -0
  19. package/dist/codec/u16-array.d.ts +6 -6
  20. package/dist/codec/u16-array.js +43 -0
  21. package/dist/codec/u32-array.d.ts +6 -6
  22. package/dist/codec/u32-array.js +43 -0
  23. package/dist/codec/u64-array.d.ts +6 -6
  24. package/dist/codec/u64-array.js +43 -0
  25. package/dist/codec/u8-array.d.ts +4 -4
  26. package/dist/codec/u8-array.js +27 -0
  27. package/dist/codec/u8-clamped-array.d.ts +4 -4
  28. package/dist/codec/u8-clamped-array.js +24 -0
  29. package/dist/core/bare-error.js +14 -0
  30. package/dist/core/byte-cursor.d.ts +26 -8
  31. package/dist/core/byte-cursor.js +42 -0
  32. package/dist/core/config.d.ts +6 -1
  33. package/dist/core/config.js +27 -0
  34. package/dist/core/index.js +5 -0
  35. package/dist/index.cjs +591 -636
  36. package/dist/index.js +3 -987
  37. package/dist/util/assert.d.ts +6 -1
  38. package/dist/util/assert.js +1 -4
  39. package/dist/util/constants.d.ts +13 -0
  40. package/dist/util/constants.js +30 -0
  41. package/dist/util/util.js +6 -0
  42. package/dist/util/validator.js +40 -0
  43. package/package.json +16 -14
  44. package/CHANGELOG.md +0 -12
@@ -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;
@@ -0,0 +1,24 @@
1
+ // src/codec/u8-clamped-array.ts
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
+ }
8
+ function writeU8ClampedArray(bc, x) {
9
+ writeUintSafe(bc, x.length);
10
+ writeU8ClampedFixedArray(bc, x);
11
+ }
12
+ function readU8ClampedFixedArray(bc, len) {
13
+ return new Uint8ClampedArray(readFixedData(bc, len));
14
+ }
15
+ function writeU8ClampedFixedArray(bc, x) {
16
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
17
+ }
18
+ export {
19
+ readU8ClampedArray,
20
+ readU8ClampedFixedArray,
21
+ writeU8ClampedArray,
22
+ writeU8ClampedFixedArray
23
+ };
24
+ //# sourceMappingURL=u8-clamped-array.js.map
@@ -0,0 +1,14 @@
1
+ // src/core/bare-error.ts
2
+ var BareError = class extends Error {
3
+ constructor(offset, issue, opts) {
4
+ super(`(byte:${offset}) ${issue}`);
5
+ this.name = "BareError";
6
+ this.issue = issue;
7
+ this.offset = offset;
8
+ this.cause = opts == null ? void 0 : opts.cause;
9
+ }
10
+ };
11
+ export {
12
+ BareError
13
+ };
14
+ //# sourceMappingURL=bare-error.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
  }
@@ -0,0 +1,42 @@
1
+ // src/core/byte-cursor.ts
2
+ import { TOO_LARGE_BUFFER } from "../util/constants.js";
3
+ import { BareError } from "./bare-error.js";
4
+ var ByteCursor = class {
5
+ constructor(bytes, config) {
6
+ if (bytes.length > config.maxBufferLength) {
7
+ throw new BareError(0, TOO_LARGE_BUFFER);
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
+ }
14
+ check(min) {
15
+ if (this.offset + min > this.bytes.length) {
16
+ throw new BareError(this.offset, "missing bytes");
17
+ }
18
+ }
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
+ }
31
+ }
32
+ read(len) {
33
+ this.check(len);
34
+ const offset = this.offset;
35
+ this.offset += len;
36
+ return this.bytes.subarray(offset, offset + len);
37
+ }
38
+ };
39
+ export {
40
+ ByteCursor
41
+ };
42
+ //# sourceMappingURL=byte-cursor.js.map
@@ -4,4 +4,9 @@ export interface Config {
4
4
  readonly textDecoderThreshold: number;
5
5
  readonly textEncoderThreshold: number;
6
6
  }
7
- export declare function Config(part: Partial<Config>): Config;
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;
@@ -0,0 +1,27 @@
1
+ // src/core/config.ts
2
+ import { assert } from "../util/assert.js";
3
+ import { TOO_LARGE_NUMBER } from "../util/constants.js";
4
+ import { isU32 } from "../util/validator.js";
5
+ function Config({
6
+ initialBufferLength = 1024,
7
+ maxBufferLength = 1024 * 1024 * 32,
8
+ textDecoderThreshold = 256,
9
+ textEncoderThreshold = 256
10
+ }) {
11
+ const config = {
12
+ initialBufferLength,
13
+ maxBufferLength,
14
+ textDecoderThreshold,
15
+ textEncoderThreshold
16
+ };
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
+ }
24
+ export {
25
+ Config
26
+ };
27
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1,5 @@
1
+ // src/core/index.ts
2
+ export * from "./bare-error.js";
3
+ export * from "./config.js";
4
+ export * from "./byte-cursor.js";
5
+ //# sourceMappingURL=index.js.map