@bare-ts/lib 0.2.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.
@@ -1,6 +1,4 @@
1
1
  // src/codec/float-array.ts
2
- import { ok as assert } from "assert";
3
- import { BareError } from "../core/index.js";
4
2
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
5
3
  import { readFixedData } from "./data.js";
6
4
  import {
@@ -12,14 +10,10 @@ import {
12
10
  writeUintSafe
13
11
  } from "./primitive.js";
14
12
  import { writeU8FixedArray } from "./u8-array.js";
15
- var NAN_NOT_ALLOWED = "NaN is not allowed";
16
13
  var readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLE : readF32FixedArrayBE;
17
14
  function readF32FixedArrayLE(bc, len) {
18
15
  const byteLen = len * 4;
19
16
  const result = new Float32Array(readFixedData(bc, byteLen));
20
- if (result.some(Number.isNaN)) {
21
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
22
- }
23
17
  return result;
24
18
  }
25
19
  function readF32FixedArrayBE(bc, len) {
@@ -31,7 +25,6 @@ function readF32FixedArrayBE(bc, len) {
31
25
  }
32
26
  var writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLE : writeF32FixedArrayBE;
33
27
  function writeF32FixedArrayLE(bc, x) {
34
- assert(!x.every(Number.isNaN), NAN_NOT_ALLOWED);
35
28
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
36
29
  }
37
30
  function writeF32FixedArrayBE(bc, val) {
@@ -52,9 +45,6 @@ var readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLE : readF6
52
45
  function readF64FixedArrayLE(bc, len) {
53
46
  const byteLen = len * 8;
54
47
  const result = new Float64Array(readFixedData(bc, byteLen));
55
- if (result.some(Number.isNaN)) {
56
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
57
- }
58
48
  return result;
59
49
  }
60
50
  function readF64FixedArrayBE(bc, len) {
@@ -66,7 +56,6 @@ function readF64FixedArrayBE(bc, len) {
66
56
  }
67
57
  var writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLE : writeF64FixedArrayBE;
68
58
  function writeF64FixedArrayLE(bc, x) {
69
- assert(!x.every(Number.isNaN), NAN_NOT_ALLOWED);
70
59
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
71
60
  }
72
61
  function writeF64FixedArrayBE(bc, x) {
@@ -1,9 +1,9 @@
1
1
  // src/codec/i16-array.ts
2
+ import { I16_BYTE_COUNT } from "../util/constants.js";
2
3
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
3
4
  import { readFixedData } from "./data.js";
4
5
  import { readI16, readUintSafe, writeI16, writeUintSafe } from "./primitive.js";
5
6
  import { writeU8FixedArray } from "./u8-array.js";
6
- var I16_BYTE_COUNT = 2;
7
7
  var readI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI16FixedArrayLE : readI16FixedArrayBE;
8
8
  function readI16Array(bc) {
9
9
  return readI16FixedArray(bc, readUintSafe(bc));
@@ -1,9 +1,9 @@
1
1
  // src/codec/i32-array.ts
2
+ import { I32_BYTE_COUNT } from "../util/constants.js";
2
3
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
3
4
  import { readFixedData } from "./data.js";
4
5
  import { readI32, readUintSafe, writeI32, writeUintSafe } from "./primitive.js";
5
6
  import { writeU8FixedArray } from "./u8-array.js";
6
- var I32_BYTE_COUNT = 4;
7
7
  var readI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI32FixedArrayLE : readI32FixedArrayBE;
8
8
  function readI32Array(bc) {
9
9
  return readI32FixedArray(bc, readUintSafe(bc));
@@ -1,9 +1,9 @@
1
1
  // src/codec/i64-array.ts
2
+ import { I64_BYTE_COUNT } from "../util/constants.js";
2
3
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
3
4
  import { readFixedData } from "./data.js";
4
5
  import { readI64, readUintSafe, writeI64, writeUintSafe } from "./primitive.js";
5
6
  import { writeU8FixedArray } from "./u8-array.js";
6
- var I64_BYTE_COUNT = 8;
7
7
  var readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI64FixedArrayLE : readI64FixedArrayBE;
8
8
  function readI64Array(bc) {
9
9
  return readI64FixedArray(bc, readUintSafe(bc));
@@ -33,5 +33,3 @@ export declare function readUint(bc: ByteCursor): bigint;
33
33
  export declare function writeUint(bc: ByteCursor, x: bigint): void;
34
34
  export declare function readUintSafe(bc: ByteCursor): number;
35
35
  export declare function writeUintSafe(bc: ByteCursor, x: number): void;
36
- export declare function readVoid(_dc: ByteCursor): undefined;
37
- export declare function writeVoid(_dc: ByteCursor, _x: undefined): void;
@@ -1,6 +1,13 @@
1
1
  // src/codec/primitive.ts
2
- import { ok as assert } from "assert";
2
+ import { assert } from "../util/assert.js";
3
3
  import { BareError } from "../core/index.js";
4
+ import {
5
+ INT_SAFE_MAX_BYTE_COUNT,
6
+ NON_CANONICAL_REPRESENTATION,
7
+ TOO_LARGE_NUMBER,
8
+ UINT_MAX_BYTE_COUNT,
9
+ UINT_SAFE_MAX_BYTE_COUNT
10
+ } from "../util/constants.js";
4
11
  import {
5
12
  isI16,
6
13
  isI32,
@@ -12,9 +19,6 @@ import {
12
19
  isU64,
13
20
  isU8
14
21
  } from "../util/validator.js";
15
- var NAN_NOT_ALLOWED = "NaN is not allowed";
16
- var NON_CANONICAL_REPRESENTATION = "must be canonical";
17
- var TOO_LARGE_NUMBER = "too large number";
18
22
  function readBool(bc) {
19
23
  const val = readU8(bc);
20
24
  if (val > 1) {
@@ -29,30 +33,22 @@ function writeBool(bc, x) {
29
33
  function readF32(bc) {
30
34
  bc.check(4);
31
35
  const result = bc.view.getFloat32(bc.offset, true);
32
- if (Number.isNaN(result)) {
33
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
34
- }
35
36
  bc.offset += 4;
36
37
  return result;
37
38
  }
38
39
  function writeF32(bc, x) {
39
- assert(!Number.isNaN(x), NAN_NOT_ALLOWED);
40
40
  bc.reserve(4);
41
41
  bc.view.setFloat32(bc.offset, x, true);
42
- assert(Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
42
+ assert(Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
43
43
  bc.offset += 4;
44
44
  }
45
45
  function readF64(bc) {
46
46
  bc.check(8);
47
47
  const result = bc.view.getFloat64(bc.offset, true);
48
- if (Number.isNaN(result)) {
49
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
50
- }
51
48
  bc.offset += 8;
52
49
  return result;
53
50
  }
54
51
  function writeF64(bc, x) {
55
- assert(!Number.isNaN(x), NAN_NOT_ALLOWED);
56
52
  bc.reserve(8);
57
53
  bc.view.setFloat64(bc.offset, x, true);
58
54
  bc.offset += 8;
@@ -132,7 +128,6 @@ function writeInt(bc, x) {
132
128
  const zigZag = x >> BigInt(63) ^ x << BigInt(1);
133
129
  writeUint(bc, zigZag);
134
130
  }
135
- var INT_SAFE_MAX_BYTE_COUNT = 8;
136
131
  function readIntSafe(bc) {
137
132
  const firstByte = readU8(bc);
138
133
  let result = (firstByte & 127) >> 1;
@@ -234,7 +229,6 @@ function writeU64Safe(bc, x) {
234
229
  writeU32(bc, x >>> 0);
235
230
  writeU32(bc, x / 4294967296 >>> 0);
236
231
  }
237
- var UINT_MAX_BYTE_COUNT = 10;
238
232
  function readUint(bc) {
239
233
  let low = readU8(bc);
240
234
  if (low >= 128) {
@@ -280,7 +274,6 @@ function writeUint(bc, x) {
280
274
  }
281
275
  writeU8(bc, tmp);
282
276
  }
283
- var UINT_SAFE_MAX_BYTE_COUNT = 8;
284
277
  function readUintSafe(bc) {
285
278
  let result = readU8(bc);
286
279
  if (result >= 128) {
@@ -313,11 +306,6 @@ function writeUintSafe(bc, x) {
313
306
  }
314
307
  writeU8(bc, x);
315
308
  }
316
- function readVoid(_dc) {
317
- return void 0;
318
- }
319
- function writeVoid(_dc, _x) {
320
- }
321
309
  export {
322
310
  readBool,
323
311
  readF32,
@@ -336,7 +324,6 @@ export {
336
324
  readU8,
337
325
  readUint,
338
326
  readUintSafe,
339
- readVoid,
340
327
  writeBool,
341
328
  writeF32,
342
329
  writeF64,
@@ -353,7 +340,6 @@ export {
353
340
  writeU64Safe,
354
341
  writeU8,
355
342
  writeUint,
356
- writeUintSafe,
357
- writeVoid
343
+ writeUintSafe
358
344
  };
359
345
  //# sourceMappingURL=primitive.js.map
@@ -1,8 +1,8 @@
1
1
  // src/codec/string.ts
2
2
  import { BareError } from "../core/bare-error.js";
3
+ import { INVALID_UTF8_STRING } from "../util/constants.js";
3
4
  import { readUintSafe, writeUintSafe } from "./primitive.js";
4
5
  import { writeU8FixedArray } from "./u8-array.js";
5
- var INVALID_UTF8_STRING = "invalid UTF-8 string";
6
6
  function readString(bc) {
7
7
  return readFixedString(bc, readUintSafe(bc));
8
8
  }
@@ -115,8 +115,8 @@ function utf8ByteLength(s) {
115
115
  }
116
116
  return result;
117
117
  }
118
- var UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
119
- var UTF8_ENCODER = new TextEncoder();
118
+ var UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
119
+ var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
120
120
  export {
121
121
  readFixedString,
122
122
  readString,
@@ -1,9 +1,9 @@
1
1
  // src/codec/u16-array.ts
2
+ import { U16_BYTE_COUNT } from "../util/constants.js";
2
3
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
3
4
  import { readFixedData } from "./data.js";
4
5
  import { readU16, readUintSafe, writeU16, writeUintSafe } from "./primitive.js";
5
6
  import { writeU8FixedArray } from "./u8-array.js";
6
- var U16_BYTE_COUNT = 2;
7
7
  var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
8
8
  function readU16Array(bc) {
9
9
  return readU16FixedArray(bc, readUintSafe(bc));
@@ -1,9 +1,9 @@
1
1
  // src/codec/u32-array.ts
2
+ import { U32_BYTE_COUNT } from "../util/constants.js";
2
3
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
3
4
  import { readFixedData } from "./data.js";
4
5
  import { readU32, readUintSafe, writeU32, writeUintSafe } from "./primitive.js";
5
6
  import { writeU8FixedArray } from "./u8-array.js";
6
- var U32_BYTE_COUNT = 4;
7
7
  var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
8
8
  function readU32Array(bc) {
9
9
  return readU32FixedArray(bc, readUintSafe(bc));
@@ -1,9 +1,9 @@
1
1
  // src/codec/u64-array.ts
2
+ import { U64_BYTE_COUNT } from "../util/constants.js";
2
3
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
3
4
  import { readFixedData } from "./data.js";
4
5
  import { readU64, readUintSafe, writeU64, writeUintSafe } from "./primitive.js";
5
6
  import { writeU8FixedArray } from "./u8-array.js";
6
- var U64_BYTE_COUNT = 8;
7
7
  var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
8
8
  function readU64Array(bc) {
9
9
  return readU64FixedArray(bc, readUintSafe(bc));
@@ -1,6 +1,6 @@
1
1
  // src/core/byte-cursor.ts
2
+ import { TOO_LARGE_BUFFER } from "../util/constants.js";
2
3
  import { BareError } from "./bare-error.js";
3
- var TOO_LARGE_BUFFER = "too large buffer";
4
4
  var ByteCursor = class {
5
5
  constructor(bytes, config) {
6
6
  if (bytes.length > config.maxBufferLength) {
@@ -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;
@@ -1,18 +1,19 @@
1
1
  // src/core/config.ts
2
- import { ok as assert } from "assert";
2
+ import { assert } from "../util/assert.js";
3
+ import { TOO_LARGE_NUMBER } from "../util/constants.js";
3
4
  import { isU32 } from "../util/validator.js";
4
- var TOO_LARGE_NUMBER = "too large number";
5
- var initialBufferLength = 1024;
6
- var maxBufferLength = 1024 * 1024 * 32;
7
- var textDecoderThreshold = 256;
8
- var textEncoderThreshold = 256;
9
- function Config(part) {
10
- const config = Object.assign({
5
+ function Config({
6
+ initialBufferLength = 1024,
7
+ maxBufferLength = 1024 * 1024 * 32,
8
+ textDecoderThreshold = 256,
9
+ textEncoderThreshold = 256
10
+ }) {
11
+ const config = {
11
12
  initialBufferLength,
12
13
  maxBufferLength,
13
14
  textDecoderThreshold,
14
15
  textEncoderThreshold
15
- }, part);
16
+ };
16
17
  assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
17
18
  assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
18
19
  assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
package/dist/index.cjs CHANGED
@@ -70,7 +70,6 @@ __export(src_exports, {
70
70
  readU8FixedArray: () => readU8FixedArray,
71
71
  readUint: () => readUint,
72
72
  readUintSafe: () => readUintSafe,
73
- readVoid: () => readVoid,
74
73
  writeBool: () => writeBool,
75
74
  writeData: () => writeData,
76
75
  writeF32: () => writeF32,
@@ -113,12 +112,25 @@ __export(src_exports, {
113
112
  writeU8ClampedFixedArray: () => writeU8ClampedFixedArray,
114
113
  writeU8FixedArray: () => writeU8FixedArray,
115
114
  writeUint: () => writeUint,
116
- writeUintSafe: () => writeUintSafe,
117
- writeVoid: () => writeVoid
115
+ writeUintSafe: () => writeUintSafe
118
116
  });
119
117
 
120
- // src/codec/primitive.ts
121
- var import_assert2 = require("assert");
118
+ // src/util/assert.ts
119
+ var AssertionError = class extends Error {
120
+ constructor(message) {
121
+ super(message);
122
+ this.name = "AssertionError";
123
+ }
124
+ };
125
+ function assert(test, message) {
126
+ if (!test) {
127
+ const e = new AssertionError(message);
128
+ if (Error.captureStackTrace) {
129
+ Error.captureStackTrace(e, assert);
130
+ }
131
+ throw e;
132
+ }
133
+ }
122
134
 
123
135
  // src/core/bare-error.ts
124
136
  var BareError = class extends Error {
@@ -131,8 +143,20 @@ var BareError = class extends Error {
131
143
  }
132
144
  };
133
145
 
134
- // src/core/config.ts
135
- var import_assert = require("assert");
146
+ // src/util/constants.ts
147
+ var I16_BYTE_COUNT = 2;
148
+ var I32_BYTE_COUNT = 4;
149
+ var I64_BYTE_COUNT = 8;
150
+ var U16_BYTE_COUNT = 2;
151
+ var U32_BYTE_COUNT = 4;
152
+ var U64_BYTE_COUNT = 8;
153
+ var INT_SAFE_MAX_BYTE_COUNT = 8;
154
+ var UINT_MAX_BYTE_COUNT = 10;
155
+ var UINT_SAFE_MAX_BYTE_COUNT = 8;
156
+ var INVALID_UTF8_STRING = "invalid UTF-8 string";
157
+ var NON_CANONICAL_REPRESENTATION = "must be canonical";
158
+ var TOO_LARGE_BUFFER = "too large buffer";
159
+ var TOO_LARGE_NUMBER = "too large number";
136
160
 
137
161
  // src/util/validator.ts
138
162
  function isI8(val) {
@@ -164,28 +188,27 @@ function isU64(val) {
164
188
  }
165
189
 
166
190
  // src/core/config.ts
167
- var TOO_LARGE_NUMBER = "too large number";
168
- var initialBufferLength = 1024;
169
- var maxBufferLength = 1024 * 1024 * 32;
170
- var textDecoderThreshold = 256;
171
- var textEncoderThreshold = 256;
172
- function Config(part) {
173
- const config = Object.assign({
191
+ function Config({
192
+ initialBufferLength = 1024,
193
+ maxBufferLength = 1024 * 1024 * 32,
194
+ textDecoderThreshold = 256,
195
+ textEncoderThreshold = 256
196
+ }) {
197
+ const config = {
174
198
  initialBufferLength,
175
199
  maxBufferLength,
176
200
  textDecoderThreshold,
177
201
  textEncoderThreshold
178
- }, part);
179
- (0, import_assert.ok)(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
180
- (0, import_assert.ok)(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
181
- (0, import_assert.ok)(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
182
- (0, import_assert.ok)(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
183
- (0, import_assert.ok)(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
202
+ };
203
+ assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
204
+ assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
205
+ assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
206
+ assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
207
+ assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
184
208
  return config;
185
209
  }
186
210
 
187
211
  // src/core/byte-cursor.ts
188
- var TOO_LARGE_BUFFER = "too large buffer";
189
212
  var ByteCursor = class {
190
213
  constructor(bytes, config) {
191
214
  if (bytes.length > config.maxBufferLength) {
@@ -223,9 +246,6 @@ var ByteCursor = class {
223
246
  };
224
247
 
225
248
  // src/codec/primitive.ts
226
- var NAN_NOT_ALLOWED = "NaN is not allowed";
227
- var NON_CANONICAL_REPRESENTATION = "must be canonical";
228
- var TOO_LARGE_NUMBER2 = "too large number";
229
249
  function readBool(bc) {
230
250
  const val = readU8(bc);
231
251
  if (val > 1) {
@@ -240,30 +260,22 @@ function writeBool(bc, x) {
240
260
  function readF32(bc) {
241
261
  bc.check(4);
242
262
  const result = bc.view.getFloat32(bc.offset, true);
243
- if (Number.isNaN(result)) {
244
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
245
- }
246
263
  bc.offset += 4;
247
264
  return result;
248
265
  }
249
266
  function writeF32(bc, x) {
250
- (0, import_assert2.ok)(!Number.isNaN(x), NAN_NOT_ALLOWED);
251
267
  bc.reserve(4);
252
268
  bc.view.setFloat32(bc.offset, x, true);
253
- (0, import_assert2.ok)(Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER2);
269
+ assert(Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
254
270
  bc.offset += 4;
255
271
  }
256
272
  function readF64(bc) {
257
273
  bc.check(8);
258
274
  const result = bc.view.getFloat64(bc.offset, true);
259
- if (Number.isNaN(result)) {
260
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
261
- }
262
275
  bc.offset += 8;
263
276
  return result;
264
277
  }
265
278
  function writeF64(bc, x) {
266
- (0, import_assert2.ok)(!Number.isNaN(x), NAN_NOT_ALLOWED);
267
279
  bc.reserve(8);
268
280
  bc.view.setFloat64(bc.offset, x, true);
269
281
  bc.offset += 8;
@@ -273,7 +285,7 @@ function readI8(bc) {
273
285
  return bc.view.getInt8(bc.offset++);
274
286
  }
275
287
  function writeI8(bc, x) {
276
- (0, import_assert2.ok)(isI8(x), TOO_LARGE_NUMBER2);
288
+ assert(isI8(x), TOO_LARGE_NUMBER);
277
289
  bc.reserve(1);
278
290
  bc.view.setInt8(bc.offset++, x);
279
291
  }
@@ -284,7 +296,7 @@ function readI16(bc) {
284
296
  return result;
285
297
  }
286
298
  function writeI16(bc, x) {
287
- (0, import_assert2.ok)(isI16(x), TOO_LARGE_NUMBER2);
299
+ assert(isI16(x), TOO_LARGE_NUMBER);
288
300
  bc.reserve(2);
289
301
  bc.view.setInt16(bc.offset, x, true);
290
302
  bc.offset += 2;
@@ -296,7 +308,7 @@ function readI32(bc) {
296
308
  return result;
297
309
  }
298
310
  function writeI32(bc, x) {
299
- (0, import_assert2.ok)(isI32(x), TOO_LARGE_NUMBER2);
311
+ assert(isI32(x), TOO_LARGE_NUMBER);
300
312
  bc.reserve(4);
301
313
  bc.view.setInt32(bc.offset, x, true);
302
314
  bc.offset += 4;
@@ -308,7 +320,7 @@ function readI64(bc) {
308
320
  return result;
309
321
  }
310
322
  function writeI64(bc, x) {
311
- (0, import_assert2.ok)(isI64(x), TOO_LARGE_NUMBER2);
323
+ assert(isI64(x), TOO_LARGE_NUMBER);
312
324
  bc.reserve(8);
313
325
  bc.view.setBigInt64(bc.offset, x, true);
314
326
  bc.offset += 8;
@@ -317,12 +329,12 @@ function readI64Safe(bc) {
317
329
  const result = readU32(bc) + readI32(bc) * 4294967296;
318
330
  if (!Number.isSafeInteger(result)) {
319
331
  bc.offset -= 8;
320
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
332
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
321
333
  }
322
334
  return result;
323
335
  }
324
336
  function writeI64Safe(bc, x) {
325
- (0, import_assert2.ok)(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
337
+ assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
326
338
  const lowest32 = x >>> 0;
327
339
  writeU32(bc, lowest32);
328
340
  let highest32 = x / 4294967296 | 0;
@@ -339,11 +351,10 @@ function readInt(bc) {
339
351
  return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
340
352
  }
341
353
  function writeInt(bc, x) {
342
- (0, import_assert2.ok)(isI64(x), TOO_LARGE_NUMBER2);
354
+ assert(isI64(x), TOO_LARGE_NUMBER);
343
355
  const zigZag = x >> BigInt(63) ^ x << BigInt(1);
344
356
  writeUint(bc, zigZag);
345
357
  }
346
- var INT_SAFE_MAX_BYTE_COUNT = 8;
347
358
  function readIntSafe(bc) {
348
359
  const firstByte = readU8(bc);
349
360
  let result = (firstByte & 127) >> 1;
@@ -363,7 +374,7 @@ function readIntSafe(bc) {
363
374
  }
364
375
  if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
365
376
  bc.offset -= byteCount - 1;
366
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
377
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
367
378
  }
368
379
  }
369
380
  const isNeg = (firstByte & 1) === 1;
@@ -373,7 +384,7 @@ function readIntSafe(bc) {
373
384
  return result;
374
385
  }
375
386
  function writeIntSafe(bc, x) {
376
- (0, import_assert2.ok)(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
387
+ assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
377
388
  const sign = x < 0 ? 1 : 0;
378
389
  if (x < 0) {
379
390
  x = -(x + 1);
@@ -392,7 +403,7 @@ function readU8(bc) {
392
403
  return bc.bytes[bc.offset++];
393
404
  }
394
405
  function writeU8(bc, x) {
395
- (0, import_assert2.ok)(isU8(x), TOO_LARGE_NUMBER2);
406
+ assert(isU8(x), TOO_LARGE_NUMBER);
396
407
  bc.reserve(1);
397
408
  bc.bytes[bc.offset++] = x;
398
409
  }
@@ -403,7 +414,7 @@ function readU16(bc) {
403
414
  return result;
404
415
  }
405
416
  function writeU16(bc, x) {
406
- (0, import_assert2.ok)(isU16(x), TOO_LARGE_NUMBER2);
417
+ assert(isU16(x), TOO_LARGE_NUMBER);
407
418
  bc.reserve(2);
408
419
  bc.view.setUint16(bc.offset, x, true);
409
420
  bc.offset += 2;
@@ -415,7 +426,7 @@ function readU32(bc) {
415
426
  return result;
416
427
  }
417
428
  function writeU32(bc, x) {
418
- (0, import_assert2.ok)(isU32(x), TOO_LARGE_NUMBER2);
429
+ assert(isU32(x), TOO_LARGE_NUMBER);
419
430
  bc.reserve(4);
420
431
  bc.view.setUint32(bc.offset, x, true);
421
432
  bc.offset += 4;
@@ -427,7 +438,7 @@ function readU64(bc) {
427
438
  return result;
428
439
  }
429
440
  function writeU64(bc, x) {
430
- (0, import_assert2.ok)(isU64(x), TOO_LARGE_NUMBER2);
441
+ assert(isU64(x), TOO_LARGE_NUMBER);
431
442
  bc.reserve(8);
432
443
  bc.view.setBigUint64(bc.offset, x, true);
433
444
  bc.offset += 8;
@@ -436,16 +447,15 @@ function readU64Safe(bc) {
436
447
  const result = readU32(bc) + readU32(bc) * 4294967296;
437
448
  if (!isSafeU64(result)) {
438
449
  bc.offset -= 8;
439
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
450
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
440
451
  }
441
452
  return result;
442
453
  }
443
454
  function writeU64Safe(bc, x) {
444
- (0, import_assert2.ok)(isSafeU64(x), TOO_LARGE_NUMBER2);
455
+ assert(isSafeU64(x), TOO_LARGE_NUMBER);
445
456
  writeU32(bc, x >>> 0);
446
457
  writeU32(bc, x / 4294967296 >>> 0);
447
458
  }
448
- var UINT_MAX_BYTE_COUNT = 10;
449
459
  function readUint(bc) {
450
460
  let low = readU8(bc);
451
461
  if (low >= 128) {
@@ -476,7 +486,7 @@ function readUint(bc) {
476
486
  return BigInt(low);
477
487
  }
478
488
  function writeUint(bc, x) {
479
- (0, import_assert2.ok)(isU64(x), TOO_LARGE_NUMBER2);
489
+ assert(isU64(x), TOO_LARGE_NUMBER);
480
490
  let tmp = Number(BigInt.asUintN(7 * 7, x));
481
491
  let rest = Number(x >> BigInt(7 * 7));
482
492
  let byteCount = 0;
@@ -491,7 +501,6 @@ function writeUint(bc, x) {
491
501
  }
492
502
  writeU8(bc, tmp);
493
503
  }
494
- var UINT_SAFE_MAX_BYTE_COUNT = 8;
495
504
  function readUintSafe(bc) {
496
505
  let result = readU8(bc);
497
506
  if (result >= 128) {
@@ -511,24 +520,19 @@ function readUintSafe(bc) {
511
520
  }
512
521
  if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
513
522
  bc.offset -= byteCount - 1;
514
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
523
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
515
524
  }
516
525
  }
517
526
  return result;
518
527
  }
519
528
  function writeUintSafe(bc, x) {
520
- (0, import_assert2.ok)(isSafeU64(x), TOO_LARGE_NUMBER2);
529
+ assert(isSafeU64(x), TOO_LARGE_NUMBER);
521
530
  while (x >= 128) {
522
531
  writeU8(bc, 128 | x & 127);
523
532
  x = Math.floor(x / 128);
524
533
  }
525
534
  writeU8(bc, x);
526
535
  }
527
- function readVoid(_dc) {
528
- return void 0;
529
- }
530
- function writeVoid(_dc, _x) {
531
- }
532
536
 
533
537
  // src/codec/u8-array.ts
534
538
  function readU8Array(bc) {
@@ -564,21 +568,14 @@ function writeFixedData(bc, x) {
564
568
  writeU8FixedArray(bc, new Uint8Array(x));
565
569
  }
566
570
 
567
- // src/codec/float-array.ts
568
- var import_assert3 = require("assert");
569
-
570
571
  // src/util/util.ts
571
- var IS_LITTLE_ENDIAN_PLATFORM = new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
572
+ var IS_LITTLE_ENDIAN_PLATFORM = /* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
572
573
 
573
574
  // src/codec/float-array.ts
574
- var NAN_NOT_ALLOWED2 = "NaN is not allowed";
575
575
  var readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLE : readF32FixedArrayBE;
576
576
  function readF32FixedArrayLE(bc, len) {
577
577
  const byteLen = len * 4;
578
578
  const result = new Float32Array(readFixedData(bc, byteLen));
579
- if (result.some(Number.isNaN)) {
580
- throw new BareError(bc.offset, NAN_NOT_ALLOWED2);
581
- }
582
579
  return result;
583
580
  }
584
581
  function readF32FixedArrayBE(bc, len) {
@@ -590,7 +587,6 @@ function readF32FixedArrayBE(bc, len) {
590
587
  }
591
588
  var writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLE : writeF32FixedArrayBE;
592
589
  function writeF32FixedArrayLE(bc, x) {
593
- (0, import_assert3.ok)(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
594
590
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
595
591
  }
596
592
  function writeF32FixedArrayBE(bc, val) {
@@ -611,9 +607,6 @@ var readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLE : readF6
611
607
  function readF64FixedArrayLE(bc, len) {
612
608
  const byteLen = len * 8;
613
609
  const result = new Float64Array(readFixedData(bc, byteLen));
614
- if (result.some(Number.isNaN)) {
615
- throw new BareError(bc.offset, NAN_NOT_ALLOWED2);
616
- }
617
610
  return result;
618
611
  }
619
612
  function readF64FixedArrayBE(bc, len) {
@@ -625,7 +618,6 @@ function readF64FixedArrayBE(bc, len) {
625
618
  }
626
619
  var writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLE : writeF64FixedArrayBE;
627
620
  function writeF64FixedArrayLE(bc, x) {
628
- (0, import_assert3.ok)(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
629
621
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
630
622
  }
631
623
  function writeF64FixedArrayBE(bc, x) {
@@ -644,7 +636,6 @@ function writeF64Array(bc, x) {
644
636
  }
645
637
 
646
638
  // src/codec/i16-array.ts
647
- var I16_BYTE_COUNT = 2;
648
639
  var readI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI16FixedArrayLE : readI16FixedArrayBE;
649
640
  function readI16Array(bc) {
650
641
  return readI16FixedArray(bc, readUintSafe(bc));
@@ -677,7 +668,6 @@ function writeI16FixedArrayBE(bc, x) {
677
668
  }
678
669
 
679
670
  // src/codec/i32-array.ts
680
- var I32_BYTE_COUNT = 4;
681
671
  var readI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI32FixedArrayLE : readI32FixedArrayBE;
682
672
  function readI32Array(bc) {
683
673
  return readI32FixedArray(bc, readUintSafe(bc));
@@ -710,7 +700,6 @@ function writeI32FixedArrayBE(bc, x) {
710
700
  }
711
701
 
712
702
  // src/codec/i64-array.ts
713
- var I64_BYTE_COUNT = 8;
714
703
  var readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI64FixedArrayLE : readI64FixedArrayBE;
715
704
  function readI64Array(bc) {
716
705
  return readI64FixedArray(bc, readUintSafe(bc));
@@ -758,7 +747,6 @@ function writeI8FixedArray(bc, x) {
758
747
  }
759
748
 
760
749
  // src/codec/string.ts
761
- var INVALID_UTF8_STRING = "invalid UTF-8 string";
762
750
  function readString(bc) {
763
751
  return readFixedString(bc, readUintSafe(bc));
764
752
  }
@@ -871,11 +859,10 @@ function utf8ByteLength(s) {
871
859
  }
872
860
  return result;
873
861
  }
874
- var UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
875
- var UTF8_ENCODER = new TextEncoder();
862
+ var UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
863
+ var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
876
864
 
877
865
  // src/codec/u16-array.ts
878
- var U16_BYTE_COUNT = 2;
879
866
  var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
880
867
  function readU16Array(bc) {
881
868
  return readU16FixedArray(bc, readUintSafe(bc));
@@ -908,7 +895,6 @@ function writeU16FixedArrayBE(bc, x) {
908
895
  }
909
896
 
910
897
  // src/codec/u32-array.ts
911
- var U32_BYTE_COUNT = 4;
912
898
  var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
913
899
  function readU32Array(bc) {
914
900
  return readU32FixedArray(bc, readUintSafe(bc));
@@ -941,7 +927,6 @@ function writeU32FixedArrayBE(bc, x) {
941
927
  }
942
928
 
943
929
  // src/codec/u64-array.ts
944
- var U64_BYTE_COUNT = 8;
945
930
  var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
946
931
  function readU64Array(bc) {
947
932
  return readU64FixedArray(bc, readUintSafe(bc));
@@ -1036,7 +1021,6 @@ module.exports = __toCommonJS(src_exports);
1036
1021
  readU8FixedArray,
1037
1022
  readUint,
1038
1023
  readUintSafe,
1039
- readVoid,
1040
1024
  writeBool,
1041
1025
  writeData,
1042
1026
  writeF32,
@@ -1079,6 +1063,5 @@ module.exports = __toCommonJS(src_exports);
1079
1063
  writeU8ClampedFixedArray,
1080
1064
  writeU8FixedArray,
1081
1065
  writeUint,
1082
- writeUintSafe,
1083
- writeVoid
1066
+ writeUintSafe
1084
1067
  });
@@ -2,5 +2,9 @@ export declare class AssertionError extends Error {
2
2
  readonly name: "AssertionError";
3
3
  constructor(message: string);
4
4
  }
5
- export default function assert(test: boolean, message: Error | string): asserts test;
6
- export declare const ok: typeof assert;
5
+ export declare function assert(test: boolean, message: string): asserts test;
6
+ declare const Error: V8ErrorConstructor;
7
+ interface V8ErrorConstructor extends ErrorConstructor {
8
+ readonly captureStackTrace?: (e: Error, f: unknown) => void;
9
+ }
10
+ export {};
@@ -7,9 +7,6 @@ var AssertionError = class extends Error {
7
7
  };
8
8
  function assert(test, message) {
9
9
  if (!test) {
10
- if (message instanceof Error) {
11
- throw message;
12
- }
13
10
  const e = new AssertionError(message);
14
11
  if (Error.captureStackTrace) {
15
12
  Error.captureStackTrace(e, assert);
@@ -17,10 +14,8 @@ function assert(test, message) {
17
14
  throw e;
18
15
  }
19
16
  }
20
- var ok = assert;
21
17
  export {
22
18
  AssertionError,
23
- assert as default,
24
- ok
19
+ assert
25
20
  };
26
21
  //# sourceMappingURL=assert.js.map
@@ -0,0 +1,13 @@
1
+ export declare const I16_BYTE_COUNT = 2;
2
+ export declare const I32_BYTE_COUNT = 4;
3
+ export declare const I64_BYTE_COUNT = 8;
4
+ export declare const U16_BYTE_COUNT = 2;
5
+ export declare const U32_BYTE_COUNT = 4;
6
+ export declare const U64_BYTE_COUNT = 8;
7
+ export declare const INT_SAFE_MAX_BYTE_COUNT = 8;
8
+ export declare const UINT_MAX_BYTE_COUNT = 10;
9
+ export declare const UINT_SAFE_MAX_BYTE_COUNT = 8;
10
+ export declare const INVALID_UTF8_STRING = "invalid UTF-8 string";
11
+ export declare const NON_CANONICAL_REPRESENTATION = "must be canonical";
12
+ export declare const TOO_LARGE_BUFFER = "too large buffer";
13
+ export declare const TOO_LARGE_NUMBER = "too large number";
@@ -0,0 +1,30 @@
1
+ // src/util/constants.ts
2
+ var I16_BYTE_COUNT = 2;
3
+ var I32_BYTE_COUNT = 4;
4
+ var I64_BYTE_COUNT = 8;
5
+ var U16_BYTE_COUNT = 2;
6
+ var U32_BYTE_COUNT = 4;
7
+ var U64_BYTE_COUNT = 8;
8
+ var INT_SAFE_MAX_BYTE_COUNT = 8;
9
+ var UINT_MAX_BYTE_COUNT = 10;
10
+ var UINT_SAFE_MAX_BYTE_COUNT = 8;
11
+ var INVALID_UTF8_STRING = "invalid UTF-8 string";
12
+ var NON_CANONICAL_REPRESENTATION = "must be canonical";
13
+ var TOO_LARGE_BUFFER = "too large buffer";
14
+ var TOO_LARGE_NUMBER = "too large number";
15
+ export {
16
+ I16_BYTE_COUNT,
17
+ I32_BYTE_COUNT,
18
+ I64_BYTE_COUNT,
19
+ INT_SAFE_MAX_BYTE_COUNT,
20
+ INVALID_UTF8_STRING,
21
+ NON_CANONICAL_REPRESENTATION,
22
+ TOO_LARGE_BUFFER,
23
+ TOO_LARGE_NUMBER,
24
+ U16_BYTE_COUNT,
25
+ U32_BYTE_COUNT,
26
+ U64_BYTE_COUNT,
27
+ UINT_MAX_BYTE_COUNT,
28
+ UINT_SAFE_MAX_BYTE_COUNT
29
+ };
30
+ //# sourceMappingURL=constants.js.map
package/dist/util/util.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/util/util.ts
2
- var IS_LITTLE_ENDIAN_PLATFORM = new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
2
+ var IS_LITTLE_ENDIAN_PLATFORM = /* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
3
3
  export {
4
4
  IS_LITTLE_ENDIAN_PLATFORM
5
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bare-ts/lib",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "TypeScript library for BARE, a compact and simple binary-serialization format",
5
5
  "keywords": [
6
6
  "bare",
@@ -28,12 +28,9 @@
28
28
  "module": "./dist/index.js",
29
29
  "es2015": "./dist/index.js",
30
30
  "types": "./dist/index.d.ts",
31
- "browser": {
32
- "assert": "./dist/util/assert.js"
33
- },
34
31
  "exports": {
35
- "import": "./dist/index.js",
36
- "require": "./dist/index.cjs"
32
+ "require": "./dist/index.cjs",
33
+ "default": "./dist/index.js"
37
34
  },
38
35
  "sideEffects": false,
39
36
  "files": [
@@ -50,6 +47,7 @@
50
47
  "scripts": {
51
48
  "build": "sh ./scripts/build.sh",
52
49
  "clean": "rm -rf dist coverage",
50
+ "prepare": "validate-commit-msg",
53
51
  "prepublishOnly": "npm run clean && npm test",
54
52
  "test": "npm run build && tsc -b tests && oletus tests/**/*.test.js",
55
53
  "test:coverage": "c8 --reporter=lcovonly npm test"
@@ -61,7 +59,6 @@
61
59
  }
62
60
  ],
63
61
  "devDependencies": {
64
- "@types/node": "^17.0.6",
65
62
  "esbuild": "~0.14.10",
66
63
  "oletus": "^3.2.0",
67
64
  "prettier": "~2.5.1",
@@ -1,55 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __reExport = (target, module2, copyDefault, desc) => {
11
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
- for (let key of __getOwnPropNames(module2))
13
- if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
- }
16
- return target;
17
- };
18
- var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
- return (module2, temp) => {
20
- return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
- };
22
- })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
-
24
- // src/util/assert.ts
25
- var assert_exports = {};
26
- __export(assert_exports, {
27
- AssertionError: () => AssertionError,
28
- default: () => assert,
29
- ok: () => ok
30
- });
31
- var AssertionError = class extends Error {
32
- constructor(message) {
33
- super(message);
34
- this.name = "AssertionError";
35
- }
36
- };
37
- function assert(test, message) {
38
- if (!test) {
39
- if (message instanceof Error) {
40
- throw message;
41
- }
42
- const e = new AssertionError(message);
43
- if (Error.captureStackTrace) {
44
- Error.captureStackTrace(e, assert);
45
- }
46
- throw e;
47
- }
48
- }
49
- var ok = assert;
50
- module.exports = __toCommonJS(assert_exports);
51
- // Annotate the CommonJS export names for ESM import in node:
52
- 0 && (module.exports = {
53
- AssertionError,
54
- ok
55
- });