@bare-ts/lib 0.4.1 → 0.6.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 (63) hide show
  1. package/README.md +9 -10
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +11 -15
  4. package/dist/codec/f32-array.d.ts +5 -0
  5. package/dist/codec/f32-array.js +51 -0
  6. package/dist/codec/f64-array.d.ts +5 -0
  7. package/dist/codec/f64-array.js +51 -0
  8. package/dist/codec/{primitive.d.ts → fixed-primitive.d.ts} +1 -11
  9. package/dist/codec/fixed-primitive.js +197 -0
  10. package/dist/codec/i16-array.d.ts +4 -7
  11. package/dist/codec/i16-array.js +32 -33
  12. package/dist/codec/i32-array.d.ts +4 -7
  13. package/dist/codec/i32-array.js +32 -33
  14. package/dist/codec/i64-array.d.ts +4 -7
  15. package/dist/codec/i64-array.js +32 -33
  16. package/dist/codec/i8-array.d.ts +3 -3
  17. package/dist/codec/i8-array.js +12 -11
  18. package/dist/codec/int.d.ts +5 -0
  19. package/dist/codec/int.js +78 -0
  20. package/dist/codec/string.d.ts +1 -1
  21. package/dist/codec/string.js +144 -115
  22. package/dist/codec/u16-array.d.ts +4 -7
  23. package/dist/codec/u16-array.js +32 -33
  24. package/dist/codec/u32-array.d.ts +4 -7
  25. package/dist/codec/u32-array.js +32 -33
  26. package/dist/codec/u64-array.d.ts +4 -7
  27. package/dist/codec/u64-array.js +32 -33
  28. package/dist/codec/u8-array.d.ts +3 -3
  29. package/dist/codec/u8-array.js +26 -20
  30. package/dist/codec/u8-clamped-array.d.ts +3 -3
  31. package/dist/codec/u8-clamped-array.js +12 -11
  32. package/dist/codec/uint.d.ts +8 -0
  33. package/dist/codec/uint.js +138 -0
  34. package/dist/core/bare-error.js +12 -8
  35. package/dist/core/byte-cursor.d.ts +1 -1
  36. package/dist/core/byte-cursor.js +103 -31
  37. package/dist/core/config.d.ts +2 -4
  38. package/dist/core/config.js +14 -18
  39. package/dist/index.cjs +194 -134
  40. package/dist/index.d.cts +21 -16
  41. package/dist/index.d.ts +21 -16
  42. package/dist/index.js +12 -6
  43. package/dist/util/assert.d.ts +10 -1
  44. package/dist/util/assert.js +24 -13
  45. package/dist/util/constants.js +6 -2
  46. package/dist/util/validator.d.ts +27 -0
  47. package/dist/util/validator.js +38 -10
  48. package/imports/dev.d.ts +4 -0
  49. package/imports/dev.development.d.ts +4 -0
  50. package/imports/dev.development.js +4 -0
  51. package/imports/dev.js +4 -0
  52. package/imports/dev.node.d.ts +4 -0
  53. package/imports/dev.node.js +4 -0
  54. package/package.json +22 -21
  55. package/dist/codec/float-array.d.ts +0 -14
  56. package/dist/codec/float-array.js +0 -93
  57. package/dist/codec/primitive.js +0 -423
  58. package/dist/env/dev.d.ts +0 -1
  59. package/dist/env/dev.development.d.ts +0 -1
  60. package/dist/env/dev.development.js +0 -2
  61. package/dist/env/dev.js +0 -2
  62. package/dist/env/dev.node.d.ts +0 -1
  63. package/dist/env/dev.node.js +0 -2
@@ -1,18 +1,29 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
2
3
  export { DEV } from "#dev";
3
- export class AssertionError extends Error {
4
- constructor() {
5
- super(...arguments);
6
- this.name = "AssertionError";
7
- }
8
- }
9
4
  const V8Error = Error;
5
+ /**
6
+ * @throws {AssertionError} if `test` is `false`.
7
+ * The message of the error is set to `message`.
8
+ */
10
9
  export function assert(test, message = "") {
11
- if (!test) {
12
- const e = new AssertionError(message);
13
- if (V8Error.captureStackTrace) {
14
- V8Error.captureStackTrace(e, assert);
10
+ if (!test) {
11
+ const e = new AssertionError(message);
12
+ V8Error.captureStackTrace?.(e, assert);
13
+ throw e;
14
+ }
15
+ }
16
+ /**
17
+ * Indicates the failure of an assertion.
18
+ * This error is thrown by {@link assert }.
19
+ *
20
+ * This error should not be caught.
21
+ *
22
+ * @sealed
23
+ */
24
+ export class AssertionError extends Error {
25
+ constructor() {
26
+ super(...arguments);
27
+ this.name = "AssertionError";
15
28
  }
16
- throw e;
17
- }
18
29
  }
@@ -1,4 +1,7 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+ // To make constants inlinable by bundlers like esbuild, we group them in this module.
4
+ // The module must be without any imports.
2
5
  export const TEXT_DECODER_THRESHOLD = 256;
3
6
  export const TEXT_ENCODER_THRESHOLD = 256;
4
7
  export const INT_SAFE_MAX_BYTE_COUNT = 8;
@@ -8,4 +11,5 @@ export const INVALID_UTF8_STRING = "invalid UTF-8 string";
8
11
  export const NON_CANONICAL_REPRESENTATION = "must be canonical";
9
12
  export const TOO_LARGE_BUFFER = "too large buffer";
10
13
  export const TOO_LARGE_NUMBER = "too large number";
11
- export const IS_LITTLE_ENDIAN_PLATFORM = /* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
14
+ export const IS_LITTLE_ENDIAN_PLATFORM =
15
+ /* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
@@ -1,9 +1,36 @@
1
+ /**
2
+ * Can `val` be stored as a signed integer in 8 bits?
3
+ */
1
4
  export declare function isI8(val: number): boolean;
5
+ /**
6
+ * Can `val` be stored as a signed integer in 16 bits?
7
+ */
2
8
  export declare function isI16(val: number): boolean;
9
+ /**
10
+ * Can `val` be stored as a signed integer in 32 bits?
11
+ */
3
12
  export declare function isI32(val: number): boolean;
13
+ /**
14
+ * Can `val` be stored as a signed integer in 64 bits?
15
+ */
4
16
  export declare function isI64(val: bigint): boolean;
17
+ /**
18
+ * Can `val` be stored as an unsigned integer in 8 bits?
19
+ */
5
20
  export declare function isU8(val: number): boolean;
21
+ /**
22
+ * Can `val` be stored as an unsigned integer in 16 bits?
23
+ */
6
24
  export declare function isU16(val: number): boolean;
25
+ /**
26
+ * Can `val` be stored as an unsigned integer in 32 bits?
27
+ */
7
28
  export declare function isU32(val: number): boolean;
29
+ /**
30
+ * Can `val` be stored as an unsigned integer in 64 bits?
31
+ */
8
32
  export declare function isU64(val: bigint): boolean;
33
+ /**
34
+ * Is `val` an unsigned integer that can be safely represented as a float?
35
+ */
9
36
  export declare function isU64Safe(val: number): boolean;
@@ -1,28 +1,56 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+ /**
4
+ * Can `val` be stored as a signed integer in 8 bits?
5
+ */
2
6
  export function isI8(val) {
3
- return val === val << 24 >> 24;
7
+ return val === (val << 24) >> 24;
4
8
  }
9
+ /**
10
+ * Can `val` be stored as a signed integer in 16 bits?
11
+ */
5
12
  export function isI16(val) {
6
- return val === val << 16 >> 16;
13
+ return val === (val << 16) >> 16;
7
14
  }
15
+ /**
16
+ * Can `val` be stored as a signed integer in 32 bits?
17
+ */
8
18
  export function isI32(val) {
9
- return val === (val | 0);
19
+ return val === (val | 0);
10
20
  }
21
+ /**
22
+ * Can `val` be stored as a signed integer in 64 bits?
23
+ */
11
24
  export function isI64(val) {
12
- return val === BigInt.asIntN(64, val);
25
+ return val === BigInt.asIntN(64, val);
13
26
  }
27
+ /**
28
+ * Can `val` be stored as an unsigned integer in 8 bits?
29
+ */
14
30
  export function isU8(val) {
15
- return val === (val & 255);
31
+ return val === (val & 0xff);
16
32
  }
33
+ /**
34
+ * Can `val` be stored as an unsigned integer in 16 bits?
35
+ */
17
36
  export function isU16(val) {
18
- return val === (val & 65535);
37
+ return val === (val & 0xffff);
19
38
  }
39
+ /**
40
+ * Can `val` be stored as an unsigned integer in 32 bits?
41
+ */
20
42
  export function isU32(val) {
21
- return val === val >>> 0;
43
+ return val === val >>> 0;
22
44
  }
45
+ /**
46
+ * Can `val` be stored as an unsigned integer in 64 bits?
47
+ */
23
48
  export function isU64(val) {
24
- return val === BigInt.asUintN(64, val);
49
+ return val === BigInt.asUintN(64, val);
25
50
  }
51
+ /**
52
+ * Is `val` an unsigned integer that can be safely represented as a float?
53
+ */
26
54
  export function isU64Safe(val) {
27
- return Number.isSafeInteger(val) && val >= 0;
55
+ return Number.isSafeInteger(val) && val >= 0;
28
56
  }
@@ -0,0 +1,4 @@
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+
4
+ export declare const DEV = false
@@ -0,0 +1,4 @@
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+
4
+ export declare const DEV = true
@@ -0,0 +1,4 @@
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+
4
+ export const DEV = true
package/imports/dev.js ADDED
@@ -0,0 +1,4 @@
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+
4
+ export const DEV = false
@@ -0,0 +1,4 @@
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+
4
+ export declare const DEV: boolean
@@ -0,0 +1,4 @@
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
3
+
4
+ export const DEV = process.env.NODE_ENV === "development"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bare-ts/lib",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "TypeScript library for BARE, a compact and simple binary-serialization format",
5
5
  "keywords": [
6
6
  "bare",
@@ -24,25 +24,24 @@
24
24
  "node": "^14.18.0 || >=16.0.0"
25
25
  },
26
26
  "type": "module",
27
- "module": "./dist/index.js",
28
- "main": "./dist/index.cjs",
29
27
  "exports": {
30
28
  "./package.json": "./package.json",
31
29
  ".": {
30
+ "module": "./dist/index.js",
31
+ "module-sync": "./dist/index.js",
32
32
  "require": {
33
33
  "types": "./dist/index.d.cts",
34
34
  "default": "./dist/index.cjs"
35
35
  },
36
36
  "types": "./dist/index.d.ts",
37
- "module": "./dist/index.js",
38
37
  "default": "./dist/index.js"
39
38
  }
40
39
  },
41
40
  "imports": {
42
41
  "#dev": {
43
- "development": "./dist/env/dev.development.js",
44
- "node": "./dist/env/dev.node.js",
45
- "default": "./dist/env/dev.js"
42
+ "development": "./imports/dev.development.js",
43
+ "node": "./imports/dev.node.js",
44
+ "default": "./imports/dev.js"
46
45
  }
47
46
  },
48
47
  "sideEffects": false,
@@ -50,26 +49,28 @@
50
49
  "dist/**/*.cjs",
51
50
  "dist/**/*.d.cts",
52
51
  "dist/**/*.d.ts",
53
- "dist/**/*.js"
52
+ "dist/**/*.js",
53
+ "imports/**/*.d.ts",
54
+ "imports/**/*.js"
54
55
  ],
55
- "directories": {
56
- "test": "tests"
57
- },
58
56
  "scripts": {
59
- "build": "sh ./scripts/build.sh",
57
+ "build": "npm run build:esm && npm run build:d.cts && npm run build:cjs",
58
+ "build:d.cts": "cp -f dist/index.d.ts dist/index.d.cts",
59
+ "build:cjs": "esbuild src/index.ts --bundle --target=es2020 --platform=node > dist/index.cjs",
60
+ "build:esm": "tsc --build src/",
61
+ "check": "tsc --build src src/tsconfig-test.json && biome ci --error-on-warnings .",
60
62
  "clean": "rm -rf dist coverage",
61
63
  "coverage": "c8 --reporter=lcovonly npm test",
62
- "format": "rome format --write .",
63
- "prepare": "validate-commit-msg",
64
- "prepublishOnly": "npm run clean && npm test",
65
- "test": "sh ./scripts/test.sh",
64
+ "format": "biome check --linter-enabled=false --write .",
65
+ "lint": "biome lint --error-on-warnings .",
66
+ "prepublishOnly": "npm run clean && npm run build && npm test",
67
+ "test": "NODE_ENV=development node --test && npm run check",
66
68
  "version": "sh ./scripts/version.sh"
67
69
  },
68
70
  "devDependencies": {
69
- "esbuild": "0.18.4",
70
- "oletus": "4.0.0",
71
- "rome": "12.1.3",
72
- "typescript": "5.1.3",
73
- "validate-commit-message": "3.2.0"
71
+ "@biomejs/biome": "2.3.4",
72
+ "@types/node": "20.19.21",
73
+ "esbuild": "0.27.0",
74
+ "typescript": "5.9.3"
74
75
  }
75
76
  }
@@ -1,14 +0,0 @@
1
- import { type ByteCursor } from "../core/byte-cursor.js";
2
- export declare const readF32FixedArray: typeof readF32FixedArrayLe;
3
- declare function readF32FixedArrayLe(bc: ByteCursor, len: number): Float32Array;
4
- export declare const writeF32FixedArray: typeof writeF32FixedArrayLe;
5
- declare function writeF32FixedArrayLe(bc: ByteCursor, x: Float32Array): void;
6
- export declare function readF32Array(bc: ByteCursor): Float32Array;
7
- export declare function writeF32Array(bc: ByteCursor, x: Float32Array): void;
8
- export declare const readF64FixedArray: typeof readF64FixedArrayLe;
9
- declare function readF64FixedArrayLe(bc: ByteCursor, len: number): Float64Array;
10
- export declare const writeF64FixedArray: typeof writeF64FixedArrayLe;
11
- declare function writeF64FixedArrayLe(bc: ByteCursor, x: Float64Array): void;
12
- export declare function readF64Array(bc: ByteCursor): Float64Array;
13
- export declare function writeF64Array(bc: ByteCursor, x: Float64Array): void;
14
- export {};
@@ -1,93 +0,0 @@
1
- "use strict";
2
- import { check, reserve } from "../core/byte-cursor.js";
3
- import { DEV, assert } from "../util/assert.js";
4
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
- import { isU32 } from "../util/validator.js";
6
- import { readFixedData } from "./data.js";
7
- import {
8
- readF32,
9
- readF64,
10
- readUintSafe32,
11
- writeF32,
12
- writeF64,
13
- writeUintSafe32
14
- } from "./primitive.js";
15
- import { writeU8FixedArray } from "./u8-array.js";
16
- export const readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLe : readF32FixedArrayBe;
17
- function readF32FixedArrayLe(bc, len) {
18
- if (DEV) {
19
- assert(isU32(len));
20
- }
21
- const byteLen = len * 4;
22
- const result = new Float32Array(readFixedData(bc, byteLen));
23
- return result;
24
- }
25
- function readF32FixedArrayBe(bc, len) {
26
- if (DEV) {
27
- assert(isU32(len));
28
- }
29
- check(bc, len * 4);
30
- const result = new Float32Array(len);
31
- for (let i = 0; i < len; i++) {
32
- result[i] = readF32(bc);
33
- }
34
- return result;
35
- }
36
- export const writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLe : writeF32FixedArrayBe;
37
- function writeF32FixedArrayLe(bc, x) {
38
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
39
- }
40
- function writeF32FixedArrayBe(bc, val) {
41
- reserve(bc, val.length * 4);
42
- for (let i = 0; i < val.length; i++) {
43
- writeF32(bc, val[i]);
44
- }
45
- }
46
- export function readF32Array(bc) {
47
- return readF32FixedArray(bc, readUintSafe32(bc));
48
- }
49
- export function writeF32Array(bc, x) {
50
- writeUintSafe32(bc, x.length);
51
- if (x.length !== 0) {
52
- writeF32FixedArray(bc, x);
53
- }
54
- }
55
- export const readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLe : readF64FixedArrayBe;
56
- function readF64FixedArrayLe(bc, len) {
57
- if (DEV) {
58
- assert(isU32(len));
59
- }
60
- const byteLen = len * 8;
61
- const result = new Float64Array(readFixedData(bc, byteLen));
62
- return result;
63
- }
64
- function readF64FixedArrayBe(bc, len) {
65
- if (DEV) {
66
- assert(isU32(len));
67
- }
68
- check(bc, len * 8);
69
- const result = new Float64Array(len);
70
- for (let i = 0; i < len; i++) {
71
- result[i] = readF64(bc);
72
- }
73
- return result;
74
- }
75
- export const writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLe : writeF64FixedArrayBe;
76
- function writeF64FixedArrayLe(bc, x) {
77
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
78
- }
79
- function writeF64FixedArrayBe(bc, x) {
80
- reserve(bc, x.length * 8);
81
- for (let i = 0; i < x.length; i++) {
82
- writeF64(bc, x[i]);
83
- }
84
- }
85
- export function readF64Array(bc) {
86
- return readF64FixedArray(bc, readUintSafe32(bc));
87
- }
88
- export function writeF64Array(bc, x) {
89
- writeUintSafe32(bc, x.length);
90
- if (x.length !== 0) {
91
- writeF64FixedArray(bc, x);
92
- }
93
- }