@bare-ts/lib 0.5.0 → 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.
- package/README.md +3 -3
- package/dist/codec/data.d.ts +1 -1
- package/dist/codec/data.js +10 -14
- package/dist/codec/f32-array.d.ts +5 -0
- package/dist/codec/f32-array.js +51 -0
- package/dist/codec/f64-array.d.ts +5 -0
- package/dist/codec/f64-array.js +51 -0
- package/dist/codec/{primitive.d.ts → fixed-primitive.d.ts} +1 -11
- package/dist/codec/fixed-primitive.js +197 -0
- package/dist/codec/i16-array.d.ts +1 -1
- package/dist/codec/i16-array.js +31 -32
- package/dist/codec/i32-array.d.ts +1 -1
- package/dist/codec/i32-array.js +31 -32
- package/dist/codec/i64-array.d.ts +1 -1
- package/dist/codec/i64-array.js +31 -32
- package/dist/codec/i8-array.d.ts +1 -1
- package/dist/codec/i8-array.js +11 -10
- package/dist/codec/int.d.ts +5 -0
- package/dist/codec/int.js +78 -0
- package/dist/codec/string.d.ts +1 -1
- package/dist/codec/string.js +143 -114
- package/dist/codec/u16-array.d.ts +1 -1
- package/dist/codec/u16-array.js +31 -32
- package/dist/codec/u32-array.d.ts +1 -1
- package/dist/codec/u32-array.js +31 -32
- package/dist/codec/u64-array.d.ts +1 -1
- package/dist/codec/u64-array.js +31 -32
- package/dist/codec/u8-array.d.ts +1 -1
- package/dist/codec/u8-array.js +25 -19
- package/dist/codec/u8-clamped-array.d.ts +1 -1
- package/dist/codec/u8-clamped-array.js +11 -10
- package/dist/codec/uint.d.ts +8 -0
- package/dist/codec/uint.js +138 -0
- package/dist/core/bare-error.js +12 -8
- package/dist/core/byte-cursor.d.ts +1 -1
- package/dist/core/byte-cursor.js +102 -30
- package/dist/core/config.js +12 -17
- package/dist/index.cjs +146 -89
- package/dist/index.d.cts +21 -16
- package/dist/index.d.ts +21 -16
- package/dist/index.js +9 -3
- package/dist/util/assert.d.ts +10 -1
- package/dist/util/assert.js +24 -13
- package/dist/util/constants.js +6 -2
- package/dist/util/validator.d.ts +27 -0
- package/dist/util/validator.js +38 -10
- package/imports/dev.d.ts +4 -0
- package/imports/dev.development.d.ts +4 -0
- package/imports/dev.development.js +4 -0
- package/imports/dev.js +4 -0
- package/imports/dev.node.d.ts +4 -0
- package/imports/dev.node.js +4 -0
- package/package.json +18 -16
- package/dist/codec/float-array.d.ts +0 -9
- package/dist/codec/float-array.js +0 -93
- package/dist/codec/primitive.js +0 -423
- package/dist/env/dev.d.ts +0 -1
- package/dist/env/dev.development.d.ts +0 -1
- package/dist/env/dev.development.js +0 -2
- package/dist/env/dev.js +0 -2
- package/dist/env/dev.node.d.ts +0 -1
- package/dist/env/dev.node.js +0 -2
package/dist/index.cjs
CHANGED
|
@@ -20,11 +20,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
AssertionError: () => AssertionError,
|
|
23
24
|
BareError: () => BareError,
|
|
24
25
|
ByteCursor: () => ByteCursor,
|
|
25
26
|
Config: () => Config,
|
|
26
27
|
DEFAULT_CONFIG: () => DEFAULT_CONFIG,
|
|
28
|
+
DEV: () => DEV,
|
|
29
|
+
assert: () => assert,
|
|
27
30
|
check: () => check,
|
|
31
|
+
isI16: () => isI16,
|
|
32
|
+
isI32: () => isI32,
|
|
33
|
+
isI64: () => isI64,
|
|
34
|
+
isI8: () => isI8,
|
|
35
|
+
isU16: () => isU16,
|
|
36
|
+
isU32: () => isU32,
|
|
37
|
+
isU64: () => isU64,
|
|
38
|
+
isU64Safe: () => isU64Safe,
|
|
39
|
+
isU8: () => isU8,
|
|
28
40
|
readBool: () => readBool,
|
|
29
41
|
readData: () => readData,
|
|
30
42
|
readF32: () => readF32,
|
|
@@ -114,30 +126,29 @@ __export(index_exports, {
|
|
|
114
126
|
writeU8FixedArray: () => writeU8FixedArray,
|
|
115
127
|
writeUint: () => writeUint,
|
|
116
128
|
writeUintSafe: () => writeUintSafe,
|
|
117
|
-
writeUintSafe32: () => writeUintSafe32
|
|
129
|
+
writeUintSafe32: () => writeUintSafe32,
|
|
130
|
+
writeUintTruncated: () => writeUintTruncated
|
|
118
131
|
});
|
|
119
132
|
module.exports = __toCommonJS(index_exports);
|
|
120
133
|
|
|
121
|
-
//
|
|
134
|
+
// imports/dev.node.js
|
|
122
135
|
var DEV = process.env.NODE_ENV === "development";
|
|
123
136
|
|
|
124
137
|
// src/util/assert.ts
|
|
125
|
-
var AssertionError = class extends Error {
|
|
126
|
-
constructor() {
|
|
127
|
-
super(...arguments);
|
|
128
|
-
this.name = "AssertionError";
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
138
|
var V8Error = Error;
|
|
132
139
|
function assert(test, message = "") {
|
|
133
140
|
if (!test) {
|
|
134
141
|
const e = new AssertionError(message);
|
|
135
|
-
|
|
136
|
-
V8Error.captureStackTrace(e, assert);
|
|
137
|
-
}
|
|
142
|
+
V8Error.captureStackTrace?.(e, assert);
|
|
138
143
|
throw e;
|
|
139
144
|
}
|
|
140
145
|
}
|
|
146
|
+
var AssertionError = class extends Error {
|
|
147
|
+
constructor() {
|
|
148
|
+
super(...arguments);
|
|
149
|
+
this.name = "AssertionError";
|
|
150
|
+
}
|
|
151
|
+
};
|
|
141
152
|
|
|
142
153
|
// src/util/validator.ts
|
|
143
154
|
function isI8(val) {
|
|
@@ -223,18 +234,43 @@ function reserve(bc, min) {
|
|
|
223
234
|
}
|
|
224
235
|
const minLen = bc.offset + min | 0;
|
|
225
236
|
if (minLen > bc.bytes.length) {
|
|
226
|
-
|
|
227
|
-
|
|
237
|
+
grow(bc, minLen);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function grow(bc, minLen) {
|
|
241
|
+
if (minLen > bc.config.maxBufferLength) {
|
|
242
|
+
throw new BareError(0, TOO_LARGE_BUFFER);
|
|
243
|
+
}
|
|
244
|
+
const buffer = bc.bytes.buffer;
|
|
245
|
+
let newBytes;
|
|
246
|
+
if (isEs2024ArrayBufferLike(buffer) && // Make sure that the view covers the end of the buffer.
|
|
247
|
+
// If it is not the case, this indicates that the user don't want
|
|
248
|
+
// to override the trailing bytes.
|
|
249
|
+
bc.bytes.byteOffset + bc.bytes.byteLength === buffer.byteLength && bc.bytes.byteLength + minLen <= buffer.maxByteLength) {
|
|
250
|
+
const newLen = Math.min(
|
|
251
|
+
minLen << 1,
|
|
252
|
+
bc.config.maxBufferLength,
|
|
253
|
+
buffer.maxByteLength
|
|
254
|
+
);
|
|
255
|
+
if (buffer instanceof ArrayBuffer) {
|
|
256
|
+
buffer.resize(newLen);
|
|
257
|
+
} else {
|
|
258
|
+
buffer.grow(newLen);
|
|
228
259
|
}
|
|
260
|
+
newBytes = new Uint8Array(buffer, bc.bytes.byteOffset, newLen);
|
|
261
|
+
} else {
|
|
229
262
|
const newLen = Math.min(minLen << 1, bc.config.maxBufferLength);
|
|
230
|
-
|
|
263
|
+
newBytes = new Uint8Array(newLen);
|
|
231
264
|
newBytes.set(bc.bytes);
|
|
232
|
-
bc.bytes = newBytes;
|
|
233
|
-
bc.view = new DataView(newBytes.buffer);
|
|
234
265
|
}
|
|
266
|
+
bc.bytes = newBytes;
|
|
267
|
+
bc.view = new DataView(newBytes.buffer);
|
|
268
|
+
}
|
|
269
|
+
function isEs2024ArrayBufferLike(buffer) {
|
|
270
|
+
return "maxByteLength" in buffer;
|
|
235
271
|
}
|
|
236
272
|
|
|
237
|
-
// src/codec/primitive.ts
|
|
273
|
+
// src/codec/fixed-primitive.ts
|
|
238
274
|
function readBool(bc) {
|
|
239
275
|
const val = readU8(bc);
|
|
240
276
|
if (val > 1) {
|
|
@@ -357,74 +393,6 @@ function writeI64Safe(bc, x) {
|
|
|
357
393
|
}
|
|
358
394
|
writeU32(bc, highest32);
|
|
359
395
|
}
|
|
360
|
-
function readInt(bc) {
|
|
361
|
-
const zigZag = readUint(bc);
|
|
362
|
-
return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
|
|
363
|
-
}
|
|
364
|
-
function writeInt(bc, x) {
|
|
365
|
-
const truncated = BigInt.asIntN(64, x);
|
|
366
|
-
if (DEV) {
|
|
367
|
-
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
368
|
-
}
|
|
369
|
-
const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
|
|
370
|
-
writeTruncatedUint(bc, zigZag);
|
|
371
|
-
}
|
|
372
|
-
function readIntSafe(bc) {
|
|
373
|
-
const firstByte = readU8(bc);
|
|
374
|
-
let result = (firstByte & 127) >> 1;
|
|
375
|
-
if (firstByte >= 128) {
|
|
376
|
-
let shiftMul = (
|
|
377
|
-
/* 2**6 */
|
|
378
|
-
64
|
|
379
|
-
);
|
|
380
|
-
let byteCount = 1;
|
|
381
|
-
let byte;
|
|
382
|
-
do {
|
|
383
|
-
byte = readU8(bc);
|
|
384
|
-
result += (byte & 127) * shiftMul;
|
|
385
|
-
shiftMul *= /* 2**7 */
|
|
386
|
-
128;
|
|
387
|
-
byteCount++;
|
|
388
|
-
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
389
|
-
if (byte === 0) {
|
|
390
|
-
bc.offset -= byteCount - 1;
|
|
391
|
-
throw new BareError(bc.offset, "must be canonical");
|
|
392
|
-
}
|
|
393
|
-
if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
|
|
394
|
-
bc.offset -= byteCount - 1;
|
|
395
|
-
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
const isNeg = (firstByte & 1) === 1;
|
|
399
|
-
if (isNeg) {
|
|
400
|
-
result = -result - 1;
|
|
401
|
-
}
|
|
402
|
-
return result;
|
|
403
|
-
}
|
|
404
|
-
function writeIntSafe(bc, x) {
|
|
405
|
-
const sign = x < 0 ? 1 : 0;
|
|
406
|
-
let zigZag = x < 0 ? -(x + 1) : x;
|
|
407
|
-
let first7Bits = (zigZag & 63) << 1 | sign;
|
|
408
|
-
zigZag = Math.floor(zigZag / /* 2**6 */
|
|
409
|
-
64);
|
|
410
|
-
if (zigZag > 0) {
|
|
411
|
-
if (!Number.isSafeInteger(x)) {
|
|
412
|
-
if (DEV) {
|
|
413
|
-
assert(false, TOO_LARGE_NUMBER);
|
|
414
|
-
}
|
|
415
|
-
const low = zigZag & 32767;
|
|
416
|
-
const high = (zigZag / 32768 >>> 0) * 32768;
|
|
417
|
-
if (first7Bits === 127 && low === 32767 && high === 4294967295) {
|
|
418
|
-
first7Bits &= ~2;
|
|
419
|
-
}
|
|
420
|
-
zigZag = high + low;
|
|
421
|
-
}
|
|
422
|
-
writeU8(bc, 128 | first7Bits);
|
|
423
|
-
writeUintSafe(bc, zigZag);
|
|
424
|
-
} else {
|
|
425
|
-
writeU8(bc, first7Bits);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
396
|
function readU8(bc) {
|
|
429
397
|
check(bc, 1);
|
|
430
398
|
return bc.bytes[bc.offset++];
|
|
@@ -496,6 +464,8 @@ function writeU64Safe(bc, x) {
|
|
|
496
464
|
4294967296 & /* 2**21-1 */
|
|
497
465
|
2097151);
|
|
498
466
|
}
|
|
467
|
+
|
|
468
|
+
// src/codec/uint.ts
|
|
499
469
|
function readUint(bc) {
|
|
500
470
|
let low = readU8(bc);
|
|
501
471
|
if (low >= 128) {
|
|
@@ -532,9 +502,9 @@ function writeUint(bc, x) {
|
|
|
532
502
|
if (DEV) {
|
|
533
503
|
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
534
504
|
}
|
|
535
|
-
|
|
505
|
+
writeUintTruncated(bc, truncated);
|
|
536
506
|
}
|
|
537
|
-
function
|
|
507
|
+
function writeUintTruncated(bc, x) {
|
|
538
508
|
let tmp = Number(BigInt.asUintN(7 * 7, x));
|
|
539
509
|
let rest = Number(x >> BigInt(7 * 7));
|
|
540
510
|
let byteCount = 0;
|
|
@@ -683,7 +653,7 @@ function writeFixedData(bc, x) {
|
|
|
683
653
|
writeU8FixedArray(bc, new Uint8Array(x));
|
|
684
654
|
}
|
|
685
655
|
|
|
686
|
-
// src/codec/
|
|
656
|
+
// src/codec/f32-array.ts
|
|
687
657
|
var readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLe : readF32FixedArrayBe;
|
|
688
658
|
function readF32FixedArrayLe(bc, len) {
|
|
689
659
|
if (DEV) {
|
|
@@ -723,6 +693,8 @@ function writeF32Array(bc, x) {
|
|
|
723
693
|
writeF32FixedArray(bc, x);
|
|
724
694
|
}
|
|
725
695
|
}
|
|
696
|
+
|
|
697
|
+
// src/codec/f64-array.ts
|
|
726
698
|
var readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLe : readF64FixedArrayBe;
|
|
727
699
|
function readF64FixedArrayLe(bc, len) {
|
|
728
700
|
if (DEV) {
|
|
@@ -901,6 +873,76 @@ function writeI64FixedArrayBe(bc, x) {
|
|
|
901
873
|
}
|
|
902
874
|
}
|
|
903
875
|
|
|
876
|
+
// src/codec/int.ts
|
|
877
|
+
function readInt(bc) {
|
|
878
|
+
const zigZag = readUint(bc);
|
|
879
|
+
return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
|
|
880
|
+
}
|
|
881
|
+
function writeInt(bc, x) {
|
|
882
|
+
const truncated = BigInt.asIntN(64, x);
|
|
883
|
+
if (DEV) {
|
|
884
|
+
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
885
|
+
}
|
|
886
|
+
const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
|
|
887
|
+
writeUintTruncated(bc, zigZag);
|
|
888
|
+
}
|
|
889
|
+
function readIntSafe(bc) {
|
|
890
|
+
const firstByte = readU8(bc);
|
|
891
|
+
let result = (firstByte & 127) >> 1;
|
|
892
|
+
if (firstByte >= 128) {
|
|
893
|
+
let shiftMul = (
|
|
894
|
+
/* 2**6 */
|
|
895
|
+
64
|
|
896
|
+
);
|
|
897
|
+
let byteCount = 1;
|
|
898
|
+
let byte;
|
|
899
|
+
do {
|
|
900
|
+
byte = readU8(bc);
|
|
901
|
+
result += (byte & 127) * shiftMul;
|
|
902
|
+
shiftMul *= /* 2**7 */
|
|
903
|
+
128;
|
|
904
|
+
byteCount++;
|
|
905
|
+
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
906
|
+
if (byte === 0) {
|
|
907
|
+
bc.offset -= byteCount - 1;
|
|
908
|
+
throw new BareError(bc.offset, "must be canonical");
|
|
909
|
+
}
|
|
910
|
+
if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
|
|
911
|
+
bc.offset -= byteCount - 1;
|
|
912
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
const isNeg = (firstByte & 1) === 1;
|
|
916
|
+
if (isNeg) {
|
|
917
|
+
result = -result - 1;
|
|
918
|
+
}
|
|
919
|
+
return result;
|
|
920
|
+
}
|
|
921
|
+
function writeIntSafe(bc, x) {
|
|
922
|
+
const sign = x < 0 ? 1 : 0;
|
|
923
|
+
let zigZag = x < 0 ? -(x + 1) : x;
|
|
924
|
+
let first7Bits = (zigZag & 63) << 1 | sign;
|
|
925
|
+
zigZag = Math.floor(zigZag / /* 2**6 */
|
|
926
|
+
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;
|
|
938
|
+
}
|
|
939
|
+
writeU8(bc, 128 | first7Bits);
|
|
940
|
+
writeUintSafe(bc, zigZag);
|
|
941
|
+
} else {
|
|
942
|
+
writeU8(bc, first7Bits);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
|
|
904
946
|
// src/codec/string.ts
|
|
905
947
|
function readString(bc) {
|
|
906
948
|
return readFixedString(bc, readUintSafe32(bc));
|
|
@@ -1190,11 +1232,23 @@ function Config({
|
|
|
1190
1232
|
var DEFAULT_CONFIG = /* @__PURE__ */ Config({});
|
|
1191
1233
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1192
1234
|
0 && (module.exports = {
|
|
1235
|
+
AssertionError,
|
|
1193
1236
|
BareError,
|
|
1194
1237
|
ByteCursor,
|
|
1195
1238
|
Config,
|
|
1196
1239
|
DEFAULT_CONFIG,
|
|
1240
|
+
DEV,
|
|
1241
|
+
assert,
|
|
1197
1242
|
check,
|
|
1243
|
+
isI16,
|
|
1244
|
+
isI32,
|
|
1245
|
+
isI64,
|
|
1246
|
+
isI8,
|
|
1247
|
+
isU16,
|
|
1248
|
+
isU32,
|
|
1249
|
+
isU64,
|
|
1250
|
+
isU64Safe,
|
|
1251
|
+
isU8,
|
|
1198
1252
|
readBool,
|
|
1199
1253
|
readData,
|
|
1200
1254
|
readF32,
|
|
@@ -1284,5 +1338,8 @@ var DEFAULT_CONFIG = /* @__PURE__ */ Config({});
|
|
|
1284
1338
|
writeU8FixedArray,
|
|
1285
1339
|
writeUint,
|
|
1286
1340
|
writeUintSafe,
|
|
1287
|
-
writeUintSafe32
|
|
1341
|
+
writeUintSafe32,
|
|
1342
|
+
writeUintTruncated
|
|
1288
1343
|
});
|
|
1344
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
1345
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
export * from "./codec/data.
|
|
2
|
-
export * from "./codec/
|
|
3
|
-
export * from "./codec/
|
|
4
|
-
export * from "./codec/
|
|
5
|
-
export * from "./codec/
|
|
6
|
-
export * from "./codec/
|
|
7
|
-
export * from "./codec/
|
|
8
|
-
export * from "./codec/
|
|
9
|
-
export * from "./codec/
|
|
10
|
-
export * from "./codec/
|
|
11
|
-
export * from "./codec/
|
|
12
|
-
export * from "./codec/
|
|
13
|
-
export * from "./codec/
|
|
14
|
-
export * from "./
|
|
15
|
-
export * from "./
|
|
16
|
-
export * from "./
|
|
1
|
+
export * from "./codec/data.ts";
|
|
2
|
+
export * from "./codec/f32-array.ts";
|
|
3
|
+
export * from "./codec/f64-array.ts";
|
|
4
|
+
export * from "./codec/fixed-primitive.ts";
|
|
5
|
+
export * from "./codec/i8-array.ts";
|
|
6
|
+
export * from "./codec/i16-array.ts";
|
|
7
|
+
export * from "./codec/i32-array.ts";
|
|
8
|
+
export * from "./codec/i64-array.ts";
|
|
9
|
+
export * from "./codec/int.ts";
|
|
10
|
+
export * from "./codec/string.ts";
|
|
11
|
+
export * from "./codec/u8-array.ts";
|
|
12
|
+
export * from "./codec/u8-clamped-array.ts";
|
|
13
|
+
export * from "./codec/u16-array.ts";
|
|
14
|
+
export * from "./codec/u32-array.ts";
|
|
15
|
+
export * from "./codec/u64-array.ts";
|
|
16
|
+
export * from "./codec/uint.ts";
|
|
17
|
+
export * from "./core/bare-error.ts";
|
|
18
|
+
export * from "./core/byte-cursor.ts";
|
|
19
|
+
export * from "./core/config.ts";
|
|
20
|
+
export * from "./util/assert.ts";
|
|
21
|
+
export * from "./util/validator.ts";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
export * from "./codec/data.
|
|
2
|
-
export * from "./codec/
|
|
3
|
-
export * from "./codec/
|
|
4
|
-
export * from "./codec/
|
|
5
|
-
export * from "./codec/
|
|
6
|
-
export * from "./codec/
|
|
7
|
-
export * from "./codec/
|
|
8
|
-
export * from "./codec/
|
|
9
|
-
export * from "./codec/
|
|
10
|
-
export * from "./codec/
|
|
11
|
-
export * from "./codec/
|
|
12
|
-
export * from "./codec/
|
|
13
|
-
export * from "./codec/
|
|
14
|
-
export * from "./
|
|
15
|
-
export * from "./
|
|
16
|
-
export * from "./
|
|
1
|
+
export * from "./codec/data.ts";
|
|
2
|
+
export * from "./codec/f32-array.ts";
|
|
3
|
+
export * from "./codec/f64-array.ts";
|
|
4
|
+
export * from "./codec/fixed-primitive.ts";
|
|
5
|
+
export * from "./codec/i8-array.ts";
|
|
6
|
+
export * from "./codec/i16-array.ts";
|
|
7
|
+
export * from "./codec/i32-array.ts";
|
|
8
|
+
export * from "./codec/i64-array.ts";
|
|
9
|
+
export * from "./codec/int.ts";
|
|
10
|
+
export * from "./codec/string.ts";
|
|
11
|
+
export * from "./codec/u8-array.ts";
|
|
12
|
+
export * from "./codec/u8-clamped-array.ts";
|
|
13
|
+
export * from "./codec/u16-array.ts";
|
|
14
|
+
export * from "./codec/u32-array.ts";
|
|
15
|
+
export * from "./codec/u64-array.ts";
|
|
16
|
+
export * from "./codec/uint.ts";
|
|
17
|
+
export * from "./core/bare-error.ts";
|
|
18
|
+
export * from "./core/byte-cursor.ts";
|
|
19
|
+
export * from "./core/config.ts";
|
|
20
|
+
export * from "./util/assert.ts";
|
|
21
|
+
export * from "./util/validator.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
2
3
|
export * from "./codec/data.js";
|
|
3
|
-
export * from "./codec/
|
|
4
|
+
export * from "./codec/f32-array.js";
|
|
5
|
+
export * from "./codec/f64-array.js";
|
|
6
|
+
export * from "./codec/fixed-primitive.js";
|
|
4
7
|
export * from "./codec/i8-array.js";
|
|
5
8
|
export * from "./codec/i16-array.js";
|
|
6
9
|
export * from "./codec/i32-array.js";
|
|
7
10
|
export * from "./codec/i64-array.js";
|
|
8
|
-
export * from "./codec/
|
|
11
|
+
export * from "./codec/int.js";
|
|
9
12
|
export * from "./codec/string.js";
|
|
10
13
|
export * from "./codec/u8-array.js";
|
|
11
14
|
export * from "./codec/u8-clamped-array.js";
|
|
12
15
|
export * from "./codec/u16-array.js";
|
|
13
16
|
export * from "./codec/u32-array.js";
|
|
14
17
|
export * from "./codec/u64-array.js";
|
|
18
|
+
export * from "./codec/uint.js";
|
|
15
19
|
export * from "./core/bare-error.js";
|
|
16
20
|
export * from "./core/byte-cursor.js";
|
|
17
21
|
export * from "./core/config.js";
|
|
22
|
+
export * from "./util/assert.js";
|
|
23
|
+
export * from "./util/validator.js";
|
package/dist/util/assert.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
export { DEV } from "#dev";
|
|
2
2
|
/**
|
|
3
|
+
* @throws {AssertionError} if `test` is `false`.
|
|
4
|
+
* The message of the error is set to `message`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function assert(test: boolean, message?: string): asserts test;
|
|
7
|
+
/**
|
|
8
|
+
* Indicates the failure of an assertion.
|
|
9
|
+
* This error is thrown by {@link assert }.
|
|
10
|
+
*
|
|
11
|
+
* This error should not be caught.
|
|
12
|
+
*
|
|
3
13
|
* @sealed
|
|
4
14
|
*/
|
|
5
15
|
export declare class AssertionError extends Error {
|
|
6
16
|
name: string;
|
|
7
17
|
}
|
|
8
|
-
export declare function assert(test: boolean, message?: string): asserts test;
|
package/dist/util/assert.js
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
}
|
package/dist/util/constants.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
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 =
|
|
14
|
+
export const IS_LITTLE_ENDIAN_PLATFORM =
|
|
15
|
+
/* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
|
package/dist/util/validator.d.ts
CHANGED
|
@@ -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;
|
package/dist/util/validator.js
CHANGED
|
@@ -1,28 +1,56 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
55
|
+
return Number.isSafeInteger(val) && val >= 0;
|
|
28
56
|
}
|
package/imports/dev.d.ts
ADDED
package/imports/dev.js
ADDED