@bare-ts/lib 0.6.0 → 0.7.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.
@@ -97,7 +97,7 @@ export function writeI64(bc, x) {
97
97
  bc.offset += 8;
98
98
  }
99
99
  export function readI64Safe(bc) {
100
- const result = readU32(bc) + readI32(bc) * /* 2**32 */ 4294967296;
100
+ const result = readU32(bc) + readI32(bc) * /* 2**32 */ 0x1_00_00_00_00;
101
101
  if (!Number.isSafeInteger(result)) {
102
102
  bc.offset -= 8;
103
103
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
@@ -110,12 +110,12 @@ export function writeI64Safe(bc, x) {
110
110
  }
111
111
  let lowest32 = x >>> 0;
112
112
  writeU32(bc, lowest32);
113
- let highest32 = (x / /* 2**32 */ 4294967296) | 0;
113
+ let highest32 = (x / /* 2**32 */ 0x1_00_00_00_00) | 0;
114
114
  if (x < 0) {
115
115
  // get two's complement representation of the highest 21bits
116
- highest32 = ~(Math.abs(highest32) & /* 2**21-1 */ 2097151) >>> 0;
116
+ highest32 = ~(Math.abs(highest32) & /* 2**21-1 */ 0x1f_ffff) >>> 0;
117
117
  if (lowest32 === 0) {
118
- if (highest32 === 2097151) {
118
+ if (highest32 === 0x1f_ffff) {
119
119
  // maps -2**53 to Number.MIN_SAFE_INTEGER
120
120
  // this is useful when assertions are skipped
121
121
  lowest32 = 1;
@@ -181,7 +181,7 @@ export function writeU64(bc, x) {
181
181
  bc.offset += 8;
182
182
  }
183
183
  export function readU64Safe(bc) {
184
- const result = readU32(bc) + readU32(bc) * /* 2**32 */ 4294967296;
184
+ const result = readU32(bc) + readU32(bc) * /* 2**32 */ 0x1_00_00_00_00;
185
185
  if (!isU64Safe(result)) {
186
186
  bc.offset -= 8;
187
187
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
@@ -193,5 +193,5 @@ export function writeU64Safe(bc, x) {
193
193
  assert(isU64Safe(x), TOO_LARGE_NUMBER);
194
194
  }
195
195
  writeU32(bc, x >>> 0);
196
- writeU32(bc, (x / /* 2**32 */ 4294967296) & /* 2**21-1 */ 2097151);
196
+ writeU32(bc, (x / /* 2**32 */ 0x1_00_00_00_00) & /* 2**21-1 */ 0x1f_ffff);
197
197
  }
package/dist/codec/int.js CHANGED
@@ -53,22 +53,22 @@ export function writeIntSafe(bc, x) {
53
53
  let zigZag = x < 0 ? -(x + 1) : x;
54
54
  let first7Bits = ((zigZag & 0x3f) << 1) | sign;
55
55
  zigZag = Math.floor(zigZag / /* 2**6 */ 0x40);
56
- if (zigZag > 0) {
57
- if (!Number.isSafeInteger(x)) {
58
- if (DEV) {
59
- assert(false, TOO_LARGE_NUMBER);
60
- }
61
- // keep only the remaining 53 - 6 = 47 bits
56
+ if (!Number.isSafeInteger(x)) {
57
+ if (DEV) {
58
+ assert(false, TOO_LARGE_NUMBER);
59
+ }
60
+ // keep only the remaining 53 - 6 = 47 bits
61
+ // this is useful when assertions are skipped
62
+ const low = zigZag & 0x7fff;
63
+ const high = ((zigZag / 0x8000) >>> 0) * 0x8000;
64
+ if (first7Bits === 0x7f && low === 0x7fff && high === 0xffff_ffff) {
65
+ // maps -2**53 to Number.MIN_SAFE_INTEGER
62
66
  // this is useful when assertions are skipped
63
- const low = zigZag & 0x7fff;
64
- const high = ((zigZag / 0x8000) >>> 0) * 0x8000;
65
- if (first7Bits === 0x7f && low === 0x7fff && high === 4294967295) {
66
- // maps -2**53 to Number.MIN_SAFE_INTEGER
67
- // this is useful when assertions are skipped
68
- first7Bits &= ~0b10;
69
- }
70
- zigZag = high + low;
67
+ first7Bits &= ~0b10;
71
68
  }
69
+ zigZag = high + low;
70
+ }
71
+ if (zigZag > 0) {
72
72
  writeU8(bc, 0x80 | first7Bits);
73
73
  writeUintSafe(bc, zigZag);
74
74
  }
@@ -124,7 +124,7 @@ function writeUtf8Js(bc, s) {
124
124
  bytes[offset++] = 0xc0 | (codePoint >> 6);
125
125
  }
126
126
  else {
127
- if (codePoint < 65536) {
127
+ if (codePoint < 0x10_000) {
128
128
  bytes[offset++] = 0xe0 | (codePoint >> 12);
129
129
  }
130
130
  else {
@@ -147,7 +147,7 @@ function utf8ByteLength(s) {
147
147
  result++;
148
148
  if (codePoint > 0x7ff) {
149
149
  result++;
150
- if (codePoint > 65535) {
150
+ if (codePoint > 0xff_ff) {
151
151
  i++; // surrogate pair encoded as two ucs2 chars
152
152
  }
153
153
  }
@@ -87,12 +87,12 @@ export function writeUintSafe32(bc, x) {
87
87
  }
88
88
  // truncate to mimic other int encoders
89
89
  // this is useful when assertions are skipped
90
- let zigZag = x >>> 0;
91
- while (zigZag >= 0x80) {
92
- writeU8(bc, 0x80 | (x & 0x7f));
93
- zigZag >>>= 7;
90
+ let rest = x >>> 0;
91
+ while (rest >= 0x80) {
92
+ writeU8(bc, 0x80 | (rest & 0x7f));
93
+ rest >>>= 7;
94
94
  }
95
- writeU8(bc, zigZag);
95
+ writeU8(bc, rest);
96
96
  }
97
97
  export function readUintSafe(bc) {
98
98
  let result = readU8(bc);
@@ -119,20 +119,27 @@ export function readUintSafe(bc) {
119
119
  return result;
120
120
  }
121
121
  export function writeUintSafe(bc, x) {
122
- if (DEV) {
123
- assert(isU64Safe(x), TOO_LARGE_NUMBER);
122
+ let rest = x;
123
+ if (!isU64Safe(x)) {
124
+ if (DEV) {
125
+ assert(false, TOO_LARGE_NUMBER);
126
+ }
127
+ // Truncate `rest` to 53 bits
128
+ // this is useful when assertions are skipped
129
+ const low = rest & 0x1fffff;
130
+ const high = ((rest / 0x200000) >>> 0) * 0x200000;
131
+ rest = high + low;
124
132
  }
125
133
  let byteCount = 1;
126
- let zigZag = x;
127
- while (zigZag >= 0x80 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
128
- writeU8(bc, 0x80 | (zigZag & 0x7f));
129
- zigZag = Math.floor(zigZag / /* 2**7 */ 0x80);
134
+ while (rest >= 0x80 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
135
+ writeU8(bc, 0x80 | (rest & 0x7f));
136
+ rest = Math.floor(rest / /* 2**7 */ 0x80);
130
137
  byteCount++;
131
138
  }
132
139
  if (byteCount === INT_SAFE_MAX_BYTE_COUNT) {
133
140
  // truncate to mimic other int encoders
134
141
  // this is useful when assertions are skipped
135
- zigZag &= 0x0f;
142
+ rest &= 0x0f;
136
143
  }
137
- writeU8(bc, zigZag);
144
+ writeU8(bc, rest);
138
145
  }
@@ -3,10 +3,10 @@
3
3
  */
4
4
  export declare class BareError extends Error {
5
5
  name: string;
6
- readonly cause: unknown;
7
6
  readonly issue: string;
7
+ /**
8
+ * Byte offset in the read buffer where the error occurred.
9
+ */
8
10
  readonly offset: number;
9
- constructor(offset: number, issue: string, opts?: {
10
- cause: unknown;
11
- });
11
+ constructor(offset: number, issue: string, options?: ErrorOptions);
12
12
  }
@@ -4,11 +4,15 @@
4
4
  * @sealed
5
5
  */
6
6
  export class BareError extends Error {
7
- constructor(offset, issue, opts) {
8
- super(`(byte:${offset}) ${issue}`);
9
- this.name = "BareError";
7
+ name = "BareError";
8
+ issue;
9
+ /**
10
+ * Byte offset in the read buffer where the error occurred.
11
+ */
12
+ offset;
13
+ constructor(offset, issue, options) {
14
+ super(`(byte:${offset}) ${issue}`, options);
10
15
  this.issue = issue;
11
16
  this.offset = offset;
12
- this.cause = opts?.cause;
13
17
  }
14
18
  }
@@ -22,14 +22,17 @@ import { BareError } from "./bare-error.js";
22
22
  * @sealed
23
23
  */
24
24
  export class ByteCursor {
25
+ bytes;
26
+ config;
27
+ /**
28
+ * Read and write Offset in {@link view} and {@link bytes}
29
+ */
30
+ offset = 0;
31
+ view;
25
32
  /**
26
33
  * @throws {BareError} Buffer exceeds `config.maxBufferLength`
27
34
  */
28
35
  constructor(bytes, config) {
29
- /**
30
- * Read and write Offset in {@link view} and {@link bytes}
31
- */
32
- this.offset = 0;
33
36
  if (bytes.length > config.maxBufferLength) {
34
37
  throw new BareError(0, TOO_LARGE_BUFFER);
35
38
  }
package/dist/index.cjs CHANGED
@@ -144,10 +144,7 @@ function assert(test, message = "") {
144
144
  }
145
145
  }
146
146
  var AssertionError = class extends Error {
147
- constructor() {
148
- super(...arguments);
149
- this.name = "AssertionError";
150
- }
147
+ name = "AssertionError";
151
148
  };
152
149
 
153
150
  // src/util/validator.ts
@@ -193,25 +190,32 @@ var IS_LITTLE_ENDIAN_PLATFORM = /* @__PURE__ */ new DataView(Uint16Array.of(1).b
193
190
 
194
191
  // src/core/bare-error.ts
195
192
  var BareError = class extends Error {
196
- constructor(offset, issue, opts) {
197
- super(`(byte:${offset}) ${issue}`);
198
- this.name = "BareError";
193
+ name = "BareError";
194
+ issue;
195
+ /**
196
+ * Byte offset in the read buffer where the error occurred.
197
+ */
198
+ offset;
199
+ constructor(offset, issue, options) {
200
+ super(`(byte:${offset}) ${issue}`, options);
199
201
  this.issue = issue;
200
202
  this.offset = offset;
201
- this.cause = opts?.cause;
202
203
  }
203
204
  };
204
205
 
205
206
  // src/core/byte-cursor.ts
206
207
  var ByteCursor = class {
208
+ bytes;
209
+ config;
210
+ /**
211
+ * Read and write Offset in {@link view} and {@link bytes}
212
+ */
213
+ offset = 0;
214
+ view;
207
215
  /**
208
216
  * @throws {BareError} Buffer exceeds `config.maxBufferLength`
209
217
  */
210
218
  constructor(bytes, config) {
211
- /**
212
- * Read and write Offset in {@link view} and {@link bytes}
213
- */
214
- this.offset = 0;
215
219
  if (bytes.length > config.maxBufferLength) {
216
220
  throw new BareError(0, TOO_LARGE_BUFFER);
217
221
  }
@@ -551,12 +555,12 @@ function writeUintSafe32(bc, x) {
551
555
  if (DEV) {
552
556
  assert(isU32(x), TOO_LARGE_NUMBER);
553
557
  }
554
- let zigZag = x >>> 0;
555
- while (zigZag >= 128) {
556
- writeU8(bc, 128 | x & 127);
557
- zigZag >>>= 7;
558
+ let rest = x >>> 0;
559
+ while (rest >= 128) {
560
+ writeU8(bc, 128 | rest & 127);
561
+ rest >>>= 7;
558
562
  }
559
- writeU8(bc, zigZag);
563
+ writeU8(bc, rest);
560
564
  }
561
565
  function readUintSafe(bc) {
562
566
  let result = readU8(bc);
@@ -590,21 +594,26 @@ function readUintSafe(bc) {
590
594
  return result;
591
595
  }
592
596
  function writeUintSafe(bc, x) {
593
- if (DEV) {
594
- assert(isU64Safe(x), TOO_LARGE_NUMBER);
597
+ let rest = x;
598
+ if (!isU64Safe(x)) {
599
+ if (DEV) {
600
+ assert(false, TOO_LARGE_NUMBER);
601
+ }
602
+ const low = rest & 2097151;
603
+ const high = (rest / 2097152 >>> 0) * 2097152;
604
+ rest = high + low;
595
605
  }
596
606
  let byteCount = 1;
597
- let zigZag = x;
598
- while (zigZag >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
599
- writeU8(bc, 128 | zigZag & 127);
600
- zigZag = Math.floor(zigZag / /* 2**7 */
607
+ while (rest >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
608
+ writeU8(bc, 128 | rest & 127);
609
+ rest = Math.floor(rest / /* 2**7 */
601
610
  128);
602
611
  byteCount++;
603
612
  }
604
613
  if (byteCount === INT_SAFE_MAX_BYTE_COUNT) {
605
- zigZag &= 15;
614
+ rest &= 15;
606
615
  }
607
- writeU8(bc, zigZag);
616
+ writeU8(bc, rest);
608
617
  }
609
618
 
610
619
  // src/codec/u8-array.ts
@@ -924,18 +933,18 @@ function writeIntSafe(bc, x) {
924
933
  let first7Bits = (zigZag & 63) << 1 | sign;
925
934
  zigZag = Math.floor(zigZag / /* 2**6 */
926
935
  64);
927
- if (zigZag > 0) {
928
- if (!Number.isSafeInteger(x)) {
929
- if (DEV) {
930
- assert(false, TOO_LARGE_NUMBER);
931
- }
932
- const low = zigZag & 32767;
933
- const high = (zigZag / 32768 >>> 0) * 32768;
934
- if (first7Bits === 127 && low === 32767 && high === 4294967295) {
935
- first7Bits &= ~2;
936
- }
937
- zigZag = high + low;
936
+ if (!Number.isSafeInteger(x)) {
937
+ if (DEV) {
938
+ assert(false, TOO_LARGE_NUMBER);
938
939
  }
940
+ const low = zigZag & 32767;
941
+ const high = (zigZag / 32768 >>> 0) * 32768;
942
+ if (first7Bits === 127 && low === 32767 && high === 4294967295) {
943
+ first7Bits &= ~2;
944
+ }
945
+ zigZag = high + low;
946
+ }
947
+ if (zigZag > 0) {
939
948
  writeU8(bc, 128 | first7Bits);
940
949
  writeUintSafe(bc, zigZag);
941
950
  } else {
@@ -22,8 +22,5 @@ export function assert(test, message = "") {
22
22
  * @sealed
23
23
  */
24
24
  export class AssertionError extends Error {
25
- constructor() {
26
- super(...arguments);
27
- this.name = "AssertionError";
28
- }
25
+ name = "AssertionError";
29
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bare-ts/lib",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "TypeScript library for BARE, a compact and simple binary-serialization format",
5
5
  "keywords": [
6
6
  "bare",
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/bare-ts/lib/issues"
22
22
  },
23
23
  "engines": {
24
- "node": "^14.18.0 || >=16.0.0"
24
+ "node": ">=16.9.0"
25
25
  },
26
26
  "type": "module",
27
27
  "exports": {
@@ -56,7 +56,7 @@
56
56
  "scripts": {
57
57
  "build": "npm run build:esm && npm run build:d.cts && npm run build:cjs",
58
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",
59
+ "build:cjs": "esbuild src/index.ts --bundle --target=es2022 --platform=node > dist/index.cjs",
60
60
  "build:esm": "tsc --build src/",
61
61
  "check": "tsc --build src src/tsconfig-test.json && biome ci --error-on-warnings .",
62
62
  "clean": "rm -rf dist coverage",
@@ -64,13 +64,15 @@
64
64
  "format": "biome check --linter-enabled=false --write .",
65
65
  "lint": "biome lint --error-on-warnings .",
66
66
  "prepublishOnly": "npm run clean && npm run build && npm test",
67
- "test": "NODE_ENV=development node --test && npm run check",
67
+ "test": "npm run test:dev && npm run test:prod",
68
+ "test:dev": "NODE_ENV=development node --test && npm run check",
69
+ "test:prod": "node --test && npm run check",
68
70
  "version": "sh ./scripts/version.sh"
69
71
  },
70
72
  "devDependencies": {
71
- "@biomejs/biome": "2.3.4",
72
- "@types/node": "20.19.21",
73
- "esbuild": "0.27.0",
73
+ "@biomejs/biome": "2.3.12",
74
+ "@types/node": "22.18.13",
75
+ "esbuild": "0.27.2",
74
76
  "typescript": "5.9.3"
75
77
  }
76
78
  }