@bare-ts/lib 0.3.0 → 0.4.1
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/LICENSE +21 -201
- package/dist/codec/data.d.ts +1 -1
- package/dist/codec/data.js +10 -12
- package/dist/codec/float-array.d.ts +9 -9
- package/dist/codec/float-array.js +51 -43
- package/dist/codec/i16-array.d.ts +5 -5
- package/dist/codec/i16-array.js +34 -26
- package/dist/codec/i32-array.d.ts +5 -5
- package/dist/codec/i32-array.js +34 -26
- package/dist/codec/i64-array.d.ts +5 -5
- package/dist/codec/i64-array.js +34 -26
- package/dist/codec/i8-array.d.ts +1 -1
- package/dist/codec/i8-array.js +12 -14
- package/dist/codec/primitive.d.ts +3 -1
- package/dist/codec/primitive.js +225 -147
- package/dist/codec/string.d.ts +1 -1
- package/dist/codec/string.js +45 -31
- package/dist/codec/u16-array.d.ts +5 -5
- package/dist/codec/u16-array.js +34 -26
- package/dist/codec/u32-array.d.ts +5 -5
- package/dist/codec/u32-array.js +34 -26
- package/dist/codec/u64-array.d.ts +5 -5
- package/dist/codec/u64-array.js +34 -26
- package/dist/codec/u8-array.d.ts +7 -1
- package/dist/codec/u8-array.js +22 -17
- package/dist/codec/u8-clamped-array.d.ts +1 -1
- package/dist/codec/u8-clamped-array.js +13 -15
- package/dist/core/bare-error.d.ts +4 -1
- package/dist/core/bare-error.js +4 -8
- package/dist/core/byte-cursor.d.ts +22 -28
- package/dist/core/byte-cursor.js +33 -29
- package/dist/core/config.d.ts +3 -7
- package/dist/core/config.js +14 -20
- package/dist/env/dev.d.ts +1 -0
- package/dist/env/dev.development.d.ts +1 -0
- package/dist/env/dev.development.js +2 -0
- package/dist/env/dev.js +2 -0
- package/dist/env/dev.node.d.ts +1 -0
- package/dist/env/dev.node.js +2 -0
- package/dist/index.cjs +504 -286
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -2
- package/dist/index.js +17 -4
- package/dist/util/assert.d.ts +6 -8
- package/dist/util/assert.js +10 -13
- package/dist/util/constants.d.ts +4 -7
- package/dist/util/constants.js +11 -30
- package/dist/util/validator.d.ts +1 -1
- package/dist/util/validator.js +12 -24
- package/package.json +31 -24
- package/dist/codec/index.d.ts +0 -13
- package/dist/codec/index.js +0 -15
- package/dist/core/index.d.ts +0 -3
- package/dist/core/index.js +0 -5
- package/dist/util/util.d.ts +0 -1
- package/dist/util/util.js +0 -6
package/dist/codec/string.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.js";
|
|
2
2
|
export declare function readString(bc: ByteCursor): string;
|
|
3
3
|
export declare function writeString(bc: ByteCursor, x: string): void;
|
|
4
4
|
export declare function readFixedString(bc: ByteCursor, byteLen: number): string;
|
package/dist/codec/string.js
CHANGED
|
@@ -1,44 +1,54 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
import { BareError } from "../core/bare-error.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
4
|
+
import { DEV, assert } from "../util/assert.js";
|
|
5
|
+
import {
|
|
6
|
+
INVALID_UTF8_STRING,
|
|
7
|
+
TEXT_DECODER_THRESHOLD,
|
|
8
|
+
TEXT_ENCODER_THRESHOLD
|
|
9
|
+
} from "../util/constants.js";
|
|
10
|
+
import { isU32 } from "../util/validator.js";
|
|
11
|
+
import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
|
|
12
|
+
import { readUnsafeU8FixedArray, writeU8FixedArray } from "./u8-array.js";
|
|
13
|
+
export function readString(bc) {
|
|
14
|
+
return readFixedString(bc, readUintSafe32(bc));
|
|
8
15
|
}
|
|
9
|
-
function writeString(bc, x) {
|
|
10
|
-
if (x.length <
|
|
16
|
+
export function writeString(bc, x) {
|
|
17
|
+
if (x.length < TEXT_ENCODER_THRESHOLD) {
|
|
11
18
|
const byteLen = utf8ByteLength(x);
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
writeUintSafe32(bc, byteLen);
|
|
20
|
+
reserve(bc, byteLen);
|
|
14
21
|
writeUtf8Js(bc, x);
|
|
15
22
|
} else {
|
|
16
23
|
const strBytes = UTF8_ENCODER.encode(x);
|
|
17
|
-
|
|
24
|
+
writeUintSafe32(bc, strBytes.length);
|
|
18
25
|
writeU8FixedArray(bc, strBytes);
|
|
19
26
|
}
|
|
20
27
|
}
|
|
21
|
-
function readFixedString(bc, byteLen) {
|
|
22
|
-
if (
|
|
28
|
+
export function readFixedString(bc, byteLen) {
|
|
29
|
+
if (DEV) {
|
|
30
|
+
assert(isU32(byteLen));
|
|
31
|
+
}
|
|
32
|
+
if (byteLen < TEXT_DECODER_THRESHOLD) {
|
|
23
33
|
return readUtf8Js(bc, byteLen);
|
|
24
34
|
}
|
|
25
35
|
try {
|
|
26
|
-
return UTF8_DECODER.decode(bc
|
|
27
|
-
} catch (
|
|
36
|
+
return UTF8_DECODER.decode(readUnsafeU8FixedArray(bc, byteLen));
|
|
37
|
+
} catch (_cause) {
|
|
28
38
|
throw new BareError(bc.offset, INVALID_UTF8_STRING);
|
|
29
39
|
}
|
|
30
40
|
}
|
|
31
|
-
function writeFixedString(bc, x) {
|
|
32
|
-
if (x.length <
|
|
41
|
+
export function writeFixedString(bc, x) {
|
|
42
|
+
if (x.length < TEXT_ENCODER_THRESHOLD) {
|
|
33
43
|
const byteLen = utf8ByteLength(x);
|
|
34
|
-
|
|
44
|
+
reserve(bc, byteLen);
|
|
35
45
|
writeUtf8Js(bc, x);
|
|
36
46
|
} else {
|
|
37
47
|
writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
|
|
38
48
|
}
|
|
39
49
|
}
|
|
40
50
|
function readUtf8Js(bc, byteLen) {
|
|
41
|
-
|
|
51
|
+
check(bc, byteLen);
|
|
42
52
|
let result = "";
|
|
43
53
|
const bytes = bc.bytes;
|
|
44
54
|
let offset = bc.offset;
|
|
@@ -51,18 +61,29 @@ function readUtf8Js(bc, byteLen) {
|
|
|
51
61
|
if (offset < upperOffset && codePoint < 224) {
|
|
52
62
|
const byte2 = bytes[offset++];
|
|
53
63
|
codePoint = (byte1 & 31) << 6 | byte2 & 63;
|
|
54
|
-
malformed = codePoint >> 7 === 0 ||
|
|
64
|
+
malformed = codePoint >> 7 === 0 || // non-canonical char
|
|
65
|
+
byte1 >> 5 !== 6 || // invalid tag
|
|
66
|
+
byte2 >> 6 !== 2;
|
|
55
67
|
} else if (offset + 1 < upperOffset && codePoint < 240) {
|
|
56
68
|
const byte2 = bytes[offset++];
|
|
57
69
|
const byte3 = bytes[offset++];
|
|
58
70
|
codePoint = (byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63;
|
|
59
|
-
malformed = codePoint >> 11 === 0 ||
|
|
71
|
+
malformed = codePoint >> 11 === 0 || // non-canonical char or missing data
|
|
72
|
+
codePoint >> 11 === 27 || // surrogate char (0xD800 <= codePoint <= 0xDFFF)
|
|
73
|
+
byte1 >> 4 !== 14 || // invalid tag
|
|
74
|
+
byte2 >> 6 !== 2 || // invalid tag
|
|
75
|
+
byte3 >> 6 !== 2;
|
|
60
76
|
} else if (offset + 2 < upperOffset) {
|
|
61
77
|
const byte2 = bytes[offset++];
|
|
62
78
|
const byte3 = bytes[offset++];
|
|
63
79
|
const byte4 = bytes[offset++];
|
|
64
80
|
codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
|
|
65
|
-
malformed = codePoint >> 16 === 0 ||
|
|
81
|
+
malformed = codePoint >> 16 === 0 || // non-canonical char or missing data
|
|
82
|
+
codePoint > 1114111 || // too large code point
|
|
83
|
+
byte1 >> 3 !== 30 || // invalid tag
|
|
84
|
+
byte2 >> 6 !== 2 || // invalid tag
|
|
85
|
+
byte3 >> 6 !== 2 || // invalid tag
|
|
86
|
+
byte4 >> 6 !== 2;
|
|
66
87
|
}
|
|
67
88
|
if (malformed) {
|
|
68
89
|
throw new BareError(bc.offset, INVALID_UTF8_STRING);
|
|
@@ -115,12 +136,5 @@ function utf8ByteLength(s) {
|
|
|
115
136
|
}
|
|
116
137
|
return result;
|
|
117
138
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
export {
|
|
121
|
-
readFixedString,
|
|
122
|
-
readString,
|
|
123
|
-
writeFixedString,
|
|
124
|
-
writeString
|
|
125
|
-
};
|
|
126
|
-
//# sourceMappingURL=string.js.map
|
|
139
|
+
const UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
|
|
140
|
+
const UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
export declare const readU16FixedArray: typeof
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.js";
|
|
2
|
+
export declare const readU16FixedArray: typeof readU16FixedArrayLe;
|
|
3
3
|
export declare function readU16Array(bc: ByteCursor): Uint16Array;
|
|
4
|
-
declare function
|
|
5
|
-
export declare const writeU16FixedArray: typeof
|
|
4
|
+
declare function readU16FixedArrayLe(bc: ByteCursor, len: number): Uint16Array;
|
|
5
|
+
export declare const writeU16FixedArray: typeof writeU16FixedArrayLe;
|
|
6
6
|
export declare function writeU16Array(bc: ByteCursor, x: Uint16Array): void;
|
|
7
|
-
declare function
|
|
7
|
+
declare function writeU16FixedArrayLe(bc: ByteCursor, x: Uint16Array): void;
|
|
8
8
|
export {};
|
package/dist/codec/u16-array.js
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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";
|
|
4
6
|
import { readFixedData } from "./data.js";
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
readU16,
|
|
9
|
+
readUintSafe32,
|
|
10
|
+
writeU16,
|
|
11
|
+
writeUintSafe32
|
|
12
|
+
} from "./primitive.js";
|
|
6
13
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
7
|
-
|
|
8
|
-
function readU16Array(bc) {
|
|
9
|
-
return readU16FixedArray(bc,
|
|
14
|
+
export const readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLe : readU16FixedArrayBe;
|
|
15
|
+
export function readU16Array(bc) {
|
|
16
|
+
return readU16FixedArray(bc, readUintSafe32(bc));
|
|
10
17
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
18
|
+
function readU16FixedArrayLe(bc, len) {
|
|
19
|
+
if (DEV) {
|
|
20
|
+
assert(isU32(len));
|
|
21
|
+
}
|
|
22
|
+
const byteCount = len * 2;
|
|
13
23
|
return new Uint16Array(readFixedData(bc, byteCount));
|
|
14
24
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
25
|
+
function readU16FixedArrayBe(bc, len) {
|
|
26
|
+
if (DEV) {
|
|
27
|
+
assert(isU32(len));
|
|
28
|
+
}
|
|
29
|
+
check(bc, len * 2);
|
|
17
30
|
const result = new Uint16Array(len);
|
|
18
|
-
for (let i = 0; i < len; i++)
|
|
31
|
+
for (let i = 0; i < len; i++) {
|
|
19
32
|
result[i] = readU16(bc);
|
|
33
|
+
}
|
|
20
34
|
return result;
|
|
21
35
|
}
|
|
22
|
-
|
|
23
|
-
function writeU16Array(bc, x) {
|
|
24
|
-
|
|
36
|
+
export const writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLe : writeU16FixedArrayBe;
|
|
37
|
+
export function writeU16Array(bc, x) {
|
|
38
|
+
writeUintSafe32(bc, x.length);
|
|
25
39
|
if (x.length !== 0) {
|
|
26
40
|
writeU16FixedArray(bc, x);
|
|
27
41
|
}
|
|
28
42
|
}
|
|
29
|
-
function
|
|
43
|
+
function writeU16FixedArrayLe(bc, x) {
|
|
30
44
|
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
31
45
|
}
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
for (let i = 0; i < x.length; i++)
|
|
46
|
+
function writeU16FixedArrayBe(bc, x) {
|
|
47
|
+
reserve(bc, x.length * 2);
|
|
48
|
+
for (let i = 0; i < x.length; i++) {
|
|
35
49
|
writeU16(bc, x[i]);
|
|
50
|
+
}
|
|
36
51
|
}
|
|
37
|
-
export {
|
|
38
|
-
readU16Array,
|
|
39
|
-
readU16FixedArray,
|
|
40
|
-
writeU16Array,
|
|
41
|
-
writeU16FixedArray
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=u16-array.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
export declare const readU32FixedArray: typeof
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.js";
|
|
2
|
+
export declare const readU32FixedArray: typeof readU32FixedArrayLe;
|
|
3
3
|
export declare function readU32Array(bc: ByteCursor): Uint32Array;
|
|
4
|
-
declare function
|
|
5
|
-
export declare const writeU32FixedArray: typeof
|
|
4
|
+
declare function readU32FixedArrayLe(bc: ByteCursor, len: number): Uint32Array;
|
|
5
|
+
export declare const writeU32FixedArray: typeof writeU32FixedArrayLe;
|
|
6
6
|
export declare function writeU32Array(bc: ByteCursor, x: Uint32Array): void;
|
|
7
|
-
declare function
|
|
7
|
+
declare function writeU32FixedArrayLe(bc: ByteCursor, x: Uint32Array): void;
|
|
8
8
|
export {};
|
package/dist/codec/u32-array.js
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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";
|
|
4
6
|
import { readFixedData } from "./data.js";
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
readU32,
|
|
9
|
+
readUintSafe32,
|
|
10
|
+
writeU32,
|
|
11
|
+
writeUintSafe32
|
|
12
|
+
} from "./primitive.js";
|
|
6
13
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
7
|
-
|
|
8
|
-
function readU32Array(bc) {
|
|
9
|
-
return readU32FixedArray(bc,
|
|
14
|
+
export const readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLe : readU32FixedArrayBe;
|
|
15
|
+
export function readU32Array(bc) {
|
|
16
|
+
return readU32FixedArray(bc, readUintSafe32(bc));
|
|
10
17
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
18
|
+
function readU32FixedArrayLe(bc, len) {
|
|
19
|
+
if (DEV) {
|
|
20
|
+
assert(isU32(len));
|
|
21
|
+
}
|
|
22
|
+
const byteCount = len * 4;
|
|
13
23
|
return new Uint32Array(readFixedData(bc, byteCount));
|
|
14
24
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
25
|
+
function readU32FixedArrayBe(bc, len) {
|
|
26
|
+
if (DEV) {
|
|
27
|
+
assert(isU32(len));
|
|
28
|
+
}
|
|
29
|
+
check(bc, len * 4);
|
|
17
30
|
const result = new Uint32Array(len);
|
|
18
|
-
for (let i = 0; i < len; i++)
|
|
31
|
+
for (let i = 0; i < len; i++) {
|
|
19
32
|
result[i] = readU32(bc);
|
|
33
|
+
}
|
|
20
34
|
return result;
|
|
21
35
|
}
|
|
22
|
-
|
|
23
|
-
function writeU32Array(bc, x) {
|
|
24
|
-
|
|
36
|
+
export const writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLe : writeU32FixedArrayBe;
|
|
37
|
+
export function writeU32Array(bc, x) {
|
|
38
|
+
writeUintSafe32(bc, x.length);
|
|
25
39
|
if (x.length !== 0) {
|
|
26
40
|
writeU32FixedArray(bc, x);
|
|
27
41
|
}
|
|
28
42
|
}
|
|
29
|
-
function
|
|
43
|
+
function writeU32FixedArrayLe(bc, x) {
|
|
30
44
|
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
31
45
|
}
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
for (let i = 0; i < x.length; i++)
|
|
46
|
+
function writeU32FixedArrayBe(bc, x) {
|
|
47
|
+
reserve(bc, x.length * 4);
|
|
48
|
+
for (let i = 0; i < x.length; i++) {
|
|
35
49
|
writeU32(bc, x[i]);
|
|
50
|
+
}
|
|
36
51
|
}
|
|
37
|
-
export {
|
|
38
|
-
readU32Array,
|
|
39
|
-
readU32FixedArray,
|
|
40
|
-
writeU32Array,
|
|
41
|
-
writeU32FixedArray
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=u32-array.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
export declare const readU64FixedArray: typeof
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.js";
|
|
2
|
+
export declare const readU64FixedArray: typeof readU64FixedArrayLe;
|
|
3
3
|
export declare function readU64Array(bc: ByteCursor): BigUint64Array;
|
|
4
|
-
declare function
|
|
5
|
-
export declare const writeU64FixedArray: typeof
|
|
4
|
+
declare function readU64FixedArrayLe(bc: ByteCursor, len: number): BigUint64Array;
|
|
5
|
+
export declare const writeU64FixedArray: typeof writeU64FixedArrayLe;
|
|
6
6
|
export declare function writeU64Array(bc: ByteCursor, x: BigUint64Array): void;
|
|
7
|
-
declare function
|
|
7
|
+
declare function writeU64FixedArrayLe(bc: ByteCursor, x: BigUint64Array): void;
|
|
8
8
|
export {};
|
package/dist/codec/u64-array.js
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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";
|
|
4
6
|
import { readFixedData } from "./data.js";
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
readU64,
|
|
9
|
+
readUintSafe32,
|
|
10
|
+
writeU64,
|
|
11
|
+
writeUintSafe32
|
|
12
|
+
} from "./primitive.js";
|
|
6
13
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
7
|
-
|
|
8
|
-
function readU64Array(bc) {
|
|
9
|
-
return readU64FixedArray(bc,
|
|
14
|
+
export const readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLe : readU64FixedArrayBe;
|
|
15
|
+
export function readU64Array(bc) {
|
|
16
|
+
return readU64FixedArray(bc, readUintSafe32(bc));
|
|
10
17
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
18
|
+
function readU64FixedArrayLe(bc, len) {
|
|
19
|
+
if (DEV) {
|
|
20
|
+
assert(isU32(len));
|
|
21
|
+
}
|
|
22
|
+
const byteCount = len * 8;
|
|
13
23
|
return new BigUint64Array(readFixedData(bc, byteCount));
|
|
14
24
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
25
|
+
function readU64FixedArrayBe(bc, len) {
|
|
26
|
+
if (DEV) {
|
|
27
|
+
assert(isU32(len));
|
|
28
|
+
}
|
|
29
|
+
check(bc, len * 8);
|
|
17
30
|
const result = new BigUint64Array(len);
|
|
18
|
-
for (let i = 0; i < len; i++)
|
|
31
|
+
for (let i = 0; i < len; i++) {
|
|
19
32
|
result[i] = readU64(bc);
|
|
33
|
+
}
|
|
20
34
|
return result;
|
|
21
35
|
}
|
|
22
|
-
|
|
23
|
-
function writeU64Array(bc, x) {
|
|
24
|
-
|
|
36
|
+
export const writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLe : writeU64FixedArrayBe;
|
|
37
|
+
export function writeU64Array(bc, x) {
|
|
38
|
+
writeUintSafe32(bc, x.length);
|
|
25
39
|
if (x.length !== 0) {
|
|
26
40
|
writeU64FixedArray(bc, x);
|
|
27
41
|
}
|
|
28
42
|
}
|
|
29
|
-
function
|
|
43
|
+
function writeU64FixedArrayLe(bc, x) {
|
|
30
44
|
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
31
45
|
}
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
for (let i = 0; i < x.length; i++)
|
|
46
|
+
function writeU64FixedArrayBe(bc, x) {
|
|
47
|
+
reserve(bc, x.length * 8);
|
|
48
|
+
for (let i = 0; i < x.length; i++) {
|
|
35
49
|
writeU64(bc, x[i]);
|
|
50
|
+
}
|
|
36
51
|
}
|
|
37
|
-
export {
|
|
38
|
-
readU64Array,
|
|
39
|
-
readU64FixedArray,
|
|
40
|
-
writeU64Array,
|
|
41
|
-
writeU64FixedArray
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=u64-array.js.map
|
package/dist/codec/u8-array.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.js";
|
|
2
2
|
export declare function readU8Array(bc: ByteCursor): Uint8Array;
|
|
3
3
|
export declare function writeU8Array(bc: ByteCursor, x: Uint8Array): void;
|
|
4
4
|
export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
|
|
5
5
|
export declare function writeU8FixedArray(bc: ByteCursor, x: Uint8Array): void;
|
|
6
|
+
/**
|
|
7
|
+
* Advance `bc` by `len` bytes and return a view of the read bytes.
|
|
8
|
+
*
|
|
9
|
+
* WARNING: The returned array should not be modified.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readUnsafeU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
|
package/dist/codec/u8-array.js
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
3
|
+
import { DEV, assert } from "../util/assert.js";
|
|
4
|
+
import { isU32 } from "../util/validator.js";
|
|
5
|
+
import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
|
|
6
|
+
export function readU8Array(bc) {
|
|
7
|
+
return readU8FixedArray(bc, readUintSafe32(bc));
|
|
5
8
|
}
|
|
6
|
-
function writeU8Array(bc, x) {
|
|
7
|
-
|
|
9
|
+
export function writeU8Array(bc, x) {
|
|
10
|
+
writeUintSafe32(bc, x.length);
|
|
8
11
|
writeU8FixedArray(bc, x);
|
|
9
12
|
}
|
|
10
|
-
function readU8FixedArray(bc, len) {
|
|
11
|
-
return bc
|
|
13
|
+
export function readU8FixedArray(bc, len) {
|
|
14
|
+
return readUnsafeU8FixedArray(bc, len).slice();
|
|
12
15
|
}
|
|
13
|
-
function writeU8FixedArray(bc, x) {
|
|
16
|
+
export function writeU8FixedArray(bc, x) {
|
|
14
17
|
const len = x.length;
|
|
15
18
|
if (len !== 0) {
|
|
16
|
-
|
|
19
|
+
reserve(bc, len);
|
|
17
20
|
bc.bytes.set(x, bc.offset);
|
|
18
21
|
bc.offset += len;
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
|
-
export {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
export function readUnsafeU8FixedArray(bc, len) {
|
|
25
|
+
if (DEV) {
|
|
26
|
+
assert(isU32(len));
|
|
27
|
+
}
|
|
28
|
+
check(bc, len);
|
|
29
|
+
const offset = bc.offset;
|
|
30
|
+
bc.offset += len;
|
|
31
|
+
return bc.bytes.subarray(offset, offset + len);
|
|
32
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ByteCursor } from "../core/
|
|
1
|
+
import type { ByteCursor } from "../core/byte-cursor.js";
|
|
2
2
|
export declare function readU8ClampedArray(bc: ByteCursor): Uint8ClampedArray;
|
|
3
3
|
export declare function writeU8ClampedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
|
|
4
4
|
export declare function readU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray;
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
import { DEV, assert } from "../util/assert.js";
|
|
3
|
+
import { isU32 } from "../util/validator.js";
|
|
2
4
|
import { readFixedData } from "./data.js";
|
|
3
|
-
import {
|
|
5
|
+
import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
|
|
4
6
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
5
|
-
function readU8ClampedArray(bc) {
|
|
6
|
-
return readU8ClampedFixedArray(bc,
|
|
7
|
+
export function readU8ClampedArray(bc) {
|
|
8
|
+
return readU8ClampedFixedArray(bc, readUintSafe32(bc));
|
|
7
9
|
}
|
|
8
|
-
function writeU8ClampedArray(bc, x) {
|
|
9
|
-
|
|
10
|
+
export function writeU8ClampedArray(bc, x) {
|
|
11
|
+
writeUintSafe32(bc, x.length);
|
|
10
12
|
writeU8ClampedFixedArray(bc, x);
|
|
11
13
|
}
|
|
12
|
-
function readU8ClampedFixedArray(bc, len) {
|
|
14
|
+
export function readU8ClampedFixedArray(bc, len) {
|
|
15
|
+
if (DEV) {
|
|
16
|
+
assert(isU32(len));
|
|
17
|
+
}
|
|
13
18
|
return new Uint8ClampedArray(readFixedData(bc, len));
|
|
14
19
|
}
|
|
15
|
-
function writeU8ClampedFixedArray(bc, x) {
|
|
20
|
+
export function writeU8ClampedFixedArray(bc, x) {
|
|
16
21
|
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
17
22
|
}
|
|
18
|
-
export {
|
|
19
|
-
readU8ClampedArray,
|
|
20
|
-
readU8ClampedFixedArray,
|
|
21
|
-
writeU8ClampedArray,
|
|
22
|
-
writeU8ClampedFixedArray
|
|
23
|
-
};
|
|
24
|
-
//# sourceMappingURL=u8-clamped-array.js.map
|
package/dist/core/bare-error.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
export class BareError extends Error {
|
|
3
3
|
constructor(offset, issue, opts) {
|
|
4
4
|
super(`(byte:${offset}) ${issue}`);
|
|
5
5
|
this.name = "BareError";
|
|
6
6
|
this.issue = issue;
|
|
7
7
|
this.offset = offset;
|
|
8
|
-
this.cause = opts
|
|
8
|
+
this.cause = opts?.cause;
|
|
9
9
|
}
|
|
10
|
-
}
|
|
11
|
-
export {
|
|
12
|
-
BareError
|
|
13
|
-
};
|
|
14
|
-
//# sourceMappingURL=bare-error.js.map
|
|
10
|
+
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type { Config } from "./config.js";
|
|
2
2
|
/**
|
|
3
|
-
* @invariant bytes.buffer === view.buffer
|
|
4
|
-
* @invariant bytes.byteOffset === view.byteOffset
|
|
5
|
-
* @invariant bytes.byteLength === view.byteLength
|
|
6
|
-
* @invariant 0 <= offset <= bytes.byteLength
|
|
7
|
-
* @invariant bytes.byteLength <= config.maxBufferLength
|
|
3
|
+
* @invariant `bytes.buffer === view.buffer`
|
|
4
|
+
* @invariant `bytes.byteOffset === view.byteOffset`
|
|
5
|
+
* @invariant `bytes.byteLength === view.byteLength`
|
|
6
|
+
* @invariant `0 <= offset <= bytes.byteLength`
|
|
7
|
+
* @invariant `bytes.byteLength <= config.maxBufferLength`
|
|
8
8
|
*
|
|
9
|
+
* ```txt
|
|
9
10
|
* | {bytes,view}.buffer |
|
|
10
11
|
* | bytes |
|
|
11
12
|
* | view |
|
|
12
13
|
* |<------ offset ------>|
|
|
13
14
|
* |<----------- config.maxBufferLength ------------>|
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @sealed
|
|
14
18
|
*/
|
|
15
19
|
export declare class ByteCursor {
|
|
16
20
|
bytes: Uint8Array;
|
|
@@ -21,29 +25,19 @@ export declare class ByteCursor {
|
|
|
21
25
|
offset: number;
|
|
22
26
|
view: DataView;
|
|
23
27
|
/**
|
|
24
|
-
* @
|
|
25
|
-
* @param config runtime configuration
|
|
26
|
-
* @throw BareError when the buffer exceeds the maximum allowed length
|
|
27
|
-
* `config.maxBufferLength`
|
|
28
|
+
* @throws {BareError} Buffer exceeds `config.maxBufferLength`
|
|
28
29
|
*/
|
|
29
30
|
constructor(bytes: Uint8Array, config: Config);
|
|
30
|
-
/**
|
|
31
|
-
* @param min number of needed bytes
|
|
32
|
-
* @throw BareError when there is not enough bytes
|
|
33
|
-
*/
|
|
34
|
-
check(min: number): void;
|
|
35
|
-
/**
|
|
36
|
-
* @param min number of bytes to reserve
|
|
37
|
-
* @throw BareError when the buffer exceeds the maximum allowed length
|
|
38
|
-
* `config.maxBufferLength`
|
|
39
|
-
*/
|
|
40
|
-
reserve(min: number): void;
|
|
41
|
-
/**
|
|
42
|
-
* Advance cursor by {@code len} bytes and return a view of the read bytes.
|
|
43
|
-
* The returned array should not be modified.
|
|
44
|
-
*
|
|
45
|
-
* @param len number of bytes to read
|
|
46
|
-
* @returns read bytes
|
|
47
|
-
*/
|
|
48
|
-
read(len: number): Uint8Array;
|
|
49
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Check that `min` number of bytes are available.
|
|
34
|
+
*
|
|
35
|
+
* @throws {BareError} bytes are missing.
|
|
36
|
+
*/
|
|
37
|
+
export declare function check(bc: ByteCursor, min: number): void;
|
|
38
|
+
/**
|
|
39
|
+
* Reserve `min` number of bytes.
|
|
40
|
+
*
|
|
41
|
+
* @throws {BareError} Buffer exceeds `config.maxBufferLength`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function reserve(bc: ByteCursor, min: number): void;
|