@bare-ts/lib 0.5.0 → 0.6.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/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 +147 -90
- 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 +20 -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/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# bare-ts library
|
|
2
2
|
|
|
3
3
|
[![NPM version][npm-img]][npm-url]
|
|
4
|
-
[![minified and gzipped
|
|
4
|
+
[![bundle size - minified and gzipped][bundlejs-img]][bundlejs]
|
|
5
5
|
[![Coverage percentage][coveralls-img]][coveralls-url]
|
|
6
6
|
|
|
7
7
|
[BARE][bare] (Binary Application Record Encoding) is a schema-based binary format that favors compactness and simplicity.
|
|
@@ -20,5 +20,5 @@ The binary format is fairly stable.
|
|
|
20
20
|
[npm-url]: https://www.npmjs.com/package/@bare-ts/lib
|
|
21
21
|
[coveralls-img]: https://img.shields.io/coveralls/github/bare-ts/lib?style=flat-square
|
|
22
22
|
[coveralls-url]: https://coveralls.io/github/bare-ts/lib?branch=main
|
|
23
|
-
[
|
|
24
|
-
[
|
|
23
|
+
[bundlejs-img]: https://img.shields.io/badge/dynamic/json?style=flat-square&label=minzipped&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=@bare-ts/lib
|
|
24
|
+
[bundlejs]: https://bundlejs.com/?q=%40bare-ts%2Flib
|
package/dist/codec/data.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ByteCursor } from "../core/byte-cursor.
|
|
1
|
+
import type { ByteCursor } from "../core/byte-cursor.ts";
|
|
2
2
|
export declare function readData(bc: ByteCursor): ArrayBuffer;
|
|
3
3
|
export declare function writeData(bc: ByteCursor, x: ArrayBuffer): void;
|
|
4
4
|
export declare function readFixedData(bc: ByteCursor, len: number): ArrayBuffer;
|
package/dist/codec/data.js
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
2
3
|
import { assert, DEV } from "../util/assert.js";
|
|
3
4
|
import { isU32 } from "../util/validator.js";
|
|
4
|
-
import {
|
|
5
|
-
readU8Array,
|
|
6
|
-
readU8FixedArray,
|
|
7
|
-
writeU8Array,
|
|
8
|
-
writeU8FixedArray
|
|
9
|
-
} from "./u8-array.js";
|
|
5
|
+
import { readU8Array, readU8FixedArray, writeU8Array, writeU8FixedArray, } from "./u8-array.js";
|
|
10
6
|
export function readData(bc) {
|
|
11
|
-
|
|
7
|
+
return readU8Array(bc).buffer;
|
|
12
8
|
}
|
|
13
9
|
export function writeData(bc, x) {
|
|
14
|
-
|
|
10
|
+
writeU8Array(bc, new Uint8Array(x));
|
|
15
11
|
}
|
|
16
12
|
export function readFixedData(bc, len) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
if (DEV) {
|
|
14
|
+
assert(isU32(len));
|
|
15
|
+
}
|
|
16
|
+
return readU8FixedArray(bc, len).buffer;
|
|
21
17
|
}
|
|
22
18
|
export function writeFixedData(bc, x) {
|
|
23
|
-
|
|
19
|
+
writeU8FixedArray(bc, new Uint8Array(x));
|
|
24
20
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.ts";
|
|
2
|
+
export declare const readF32FixedArray: (bc: ByteCursor, len: number) => Float32Array<ArrayBuffer>;
|
|
3
|
+
export declare const writeF32FixedArray: (bc: ByteCursor, x: Float32Array) => void;
|
|
4
|
+
export declare function readF32Array(bc: ByteCursor): Float32Array<ArrayBuffer>;
|
|
5
|
+
export declare function writeF32Array(bc: ByteCursor, x: Float32Array): void;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
3
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
4
|
+
import { assert, DEV } from "../util/assert.js";
|
|
5
|
+
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
|
|
6
|
+
import { isU32 } from "../util/validator.js";
|
|
7
|
+
import { readFixedData } from "./data.js";
|
|
8
|
+
import { readF32, writeF32 } from "./fixed-primitive.js";
|
|
9
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
10
|
+
import { readUintSafe32, writeUintSafe32 } from "./uint.js";
|
|
11
|
+
export const readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM
|
|
12
|
+
? readF32FixedArrayLe
|
|
13
|
+
: readF32FixedArrayBe;
|
|
14
|
+
function readF32FixedArrayLe(bc, len) {
|
|
15
|
+
if (DEV) {
|
|
16
|
+
assert(isU32(len));
|
|
17
|
+
}
|
|
18
|
+
const byteLen = len * 4;
|
|
19
|
+
const result = new Float32Array(readFixedData(bc, byteLen));
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
function readF32FixedArrayBe(bc, len) {
|
|
23
|
+
if (DEV) {
|
|
24
|
+
assert(isU32(len));
|
|
25
|
+
}
|
|
26
|
+
check(bc, len * 4);
|
|
27
|
+
const result = new Float32Array(len);
|
|
28
|
+
for (let i = 0; i < len; i++) {
|
|
29
|
+
result[i] = readF32(bc);
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
export const writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLe : writeF32FixedArrayBe;
|
|
34
|
+
function writeF32FixedArrayLe(bc, x) {
|
|
35
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
36
|
+
}
|
|
37
|
+
function writeF32FixedArrayBe(bc, val) {
|
|
38
|
+
reserve(bc, val.length * 4);
|
|
39
|
+
for (let i = 0; i < val.length; i++) {
|
|
40
|
+
writeF32(bc, val[i]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function readF32Array(bc) {
|
|
44
|
+
return readF32FixedArray(bc, readUintSafe32(bc));
|
|
45
|
+
}
|
|
46
|
+
export function writeF32Array(bc, x) {
|
|
47
|
+
writeUintSafe32(bc, x.length);
|
|
48
|
+
if (x.length > 0) {
|
|
49
|
+
writeF32FixedArray(bc, x);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.ts";
|
|
2
|
+
export declare const readF64FixedArray: (bc: ByteCursor, len: number) => Float64Array<ArrayBuffer>;
|
|
3
|
+
export declare const writeF64FixedArray: (bc: ByteCursor, x: Float64Array) => void;
|
|
4
|
+
export declare function readF64Array(bc: ByteCursor): Float64Array<ArrayBuffer>;
|
|
5
|
+
export declare function writeF64Array(bc: ByteCursor, x: Float64Array): void;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
3
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
4
|
+
import { assert, DEV } from "../util/assert.js";
|
|
5
|
+
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
|
|
6
|
+
import { isU32 } from "../util/validator.js";
|
|
7
|
+
import { readFixedData } from "./data.js";
|
|
8
|
+
import { readF64, writeF64 } from "./fixed-primitive.js";
|
|
9
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
10
|
+
import { readUintSafe32, writeUintSafe32 } from "./uint.js";
|
|
11
|
+
export const readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM
|
|
12
|
+
? readF64FixedArrayLe
|
|
13
|
+
: readF64FixedArrayBe;
|
|
14
|
+
function readF64FixedArrayLe(bc, len) {
|
|
15
|
+
if (DEV) {
|
|
16
|
+
assert(isU32(len));
|
|
17
|
+
}
|
|
18
|
+
const byteLen = len * 8;
|
|
19
|
+
const result = new Float64Array(readFixedData(bc, byteLen));
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
function readF64FixedArrayBe(bc, len) {
|
|
23
|
+
if (DEV) {
|
|
24
|
+
assert(isU32(len));
|
|
25
|
+
}
|
|
26
|
+
check(bc, len * 8);
|
|
27
|
+
const result = new Float64Array(len);
|
|
28
|
+
for (let i = 0; i < len; i++) {
|
|
29
|
+
result[i] = readF64(bc);
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
export const writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLe : writeF64FixedArrayBe;
|
|
34
|
+
function writeF64FixedArrayLe(bc, x) {
|
|
35
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
36
|
+
}
|
|
37
|
+
function writeF64FixedArrayBe(bc, x) {
|
|
38
|
+
reserve(bc, x.length * 8);
|
|
39
|
+
for (let i = 0; i < x.length; i++) {
|
|
40
|
+
writeF64(bc, x[i]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function readF64Array(bc) {
|
|
44
|
+
return readF64FixedArray(bc, readUintSafe32(bc));
|
|
45
|
+
}
|
|
46
|
+
export function writeF64Array(bc, x) {
|
|
47
|
+
writeUintSafe32(bc, x.length);
|
|
48
|
+
if (x.length > 0) {
|
|
49
|
+
writeF64FixedArray(bc, x);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ByteCursor } from "../core/byte-cursor.
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.ts";
|
|
2
2
|
export declare function readBool(bc: ByteCursor): boolean;
|
|
3
3
|
export declare function writeBool(bc: ByteCursor, x: boolean): void;
|
|
4
4
|
export declare function readF32(bc: ByteCursor): number;
|
|
@@ -15,10 +15,6 @@ export declare function readI64(bc: ByteCursor): bigint;
|
|
|
15
15
|
export declare function writeI64(bc: ByteCursor, x: bigint): void;
|
|
16
16
|
export declare function readI64Safe(bc: ByteCursor): number;
|
|
17
17
|
export declare function writeI64Safe(bc: ByteCursor, x: number): void;
|
|
18
|
-
export declare function readInt(bc: ByteCursor): bigint;
|
|
19
|
-
export declare function writeInt(bc: ByteCursor, x: bigint): void;
|
|
20
|
-
export declare function readIntSafe(bc: ByteCursor): number;
|
|
21
|
-
export declare function writeIntSafe(bc: ByteCursor, x: number): void;
|
|
22
18
|
export declare function readU8(bc: ByteCursor): number;
|
|
23
19
|
export declare function writeU8(bc: ByteCursor, x: number): void;
|
|
24
20
|
export declare function readU16(bc: ByteCursor): number;
|
|
@@ -29,9 +25,3 @@ export declare function readU64(bc: ByteCursor): bigint;
|
|
|
29
25
|
export declare function writeU64(bc: ByteCursor, x: bigint): void;
|
|
30
26
|
export declare function readU64Safe(bc: ByteCursor): number;
|
|
31
27
|
export declare function writeU64Safe(bc: ByteCursor, x: number): void;
|
|
32
|
-
export declare function readUint(bc: ByteCursor): bigint;
|
|
33
|
-
export declare function writeUint(bc: ByteCursor, x: bigint): void;
|
|
34
|
-
export declare function readUintSafe32(bc: ByteCursor): number;
|
|
35
|
-
export declare function writeUintSafe32(bc: ByteCursor, x: number): void;
|
|
36
|
-
export declare function readUintSafe(bc: ByteCursor): number;
|
|
37
|
-
export declare function writeUintSafe(bc: ByteCursor, x: number): void;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
3
|
+
import { BareError } from "../core/bare-error.js";
|
|
4
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
5
|
+
import { assert, DEV } from "../util/assert.js";
|
|
6
|
+
import { TOO_LARGE_NUMBER } from "../util/constants.js";
|
|
7
|
+
import { isI8, isI16, isI32, isI64, isU8, isU16, isU32, isU64, isU64Safe, } from "../util/validator.js";
|
|
8
|
+
export function readBool(bc) {
|
|
9
|
+
const val = readU8(bc);
|
|
10
|
+
if (val > 1) {
|
|
11
|
+
bc.offset--;
|
|
12
|
+
throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
|
|
13
|
+
}
|
|
14
|
+
return val > 0;
|
|
15
|
+
}
|
|
16
|
+
export function writeBool(bc, x) {
|
|
17
|
+
writeU8(bc, x ? 1 : 0);
|
|
18
|
+
}
|
|
19
|
+
export function readF32(bc) {
|
|
20
|
+
check(bc, 4);
|
|
21
|
+
const result = bc.view.getFloat32(bc.offset, true);
|
|
22
|
+
bc.offset += 4;
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
export function writeF32(bc, x) {
|
|
26
|
+
reserve(bc, 4);
|
|
27
|
+
bc.view.setFloat32(bc.offset, x, true);
|
|
28
|
+
if (DEV) {
|
|
29
|
+
assert(Number.isNaN(x) ||
|
|
30
|
+
Math.abs(bc.view.getFloat32(bc.offset, true) - x) <=
|
|
31
|
+
Number.EPSILON, TOO_LARGE_NUMBER);
|
|
32
|
+
}
|
|
33
|
+
bc.offset += 4;
|
|
34
|
+
}
|
|
35
|
+
export function readF64(bc) {
|
|
36
|
+
check(bc, 8);
|
|
37
|
+
const result = bc.view.getFloat64(bc.offset, true);
|
|
38
|
+
bc.offset += 8;
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
export function writeF64(bc, x) {
|
|
42
|
+
reserve(bc, 8);
|
|
43
|
+
bc.view.setFloat64(bc.offset, x, true);
|
|
44
|
+
bc.offset += 8;
|
|
45
|
+
}
|
|
46
|
+
export function readI8(bc) {
|
|
47
|
+
check(bc, 1);
|
|
48
|
+
return bc.view.getInt8(bc.offset++);
|
|
49
|
+
}
|
|
50
|
+
export function writeI8(bc, x) {
|
|
51
|
+
if (DEV) {
|
|
52
|
+
assert(isI8(x), TOO_LARGE_NUMBER);
|
|
53
|
+
}
|
|
54
|
+
reserve(bc, 1);
|
|
55
|
+
bc.view.setInt8(bc.offset++, x);
|
|
56
|
+
}
|
|
57
|
+
export function readI16(bc) {
|
|
58
|
+
check(bc, 2);
|
|
59
|
+
const result = bc.view.getInt16(bc.offset, true);
|
|
60
|
+
bc.offset += 2;
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
export function writeI16(bc, x) {
|
|
64
|
+
if (DEV) {
|
|
65
|
+
assert(isI16(x), TOO_LARGE_NUMBER);
|
|
66
|
+
}
|
|
67
|
+
reserve(bc, 2);
|
|
68
|
+
bc.view.setInt16(bc.offset, x, true);
|
|
69
|
+
bc.offset += 2;
|
|
70
|
+
}
|
|
71
|
+
export function readI32(bc) {
|
|
72
|
+
check(bc, 4);
|
|
73
|
+
const result = bc.view.getInt32(bc.offset, true);
|
|
74
|
+
bc.offset += 4;
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
export function writeI32(bc, x) {
|
|
78
|
+
if (DEV) {
|
|
79
|
+
assert(isI32(x), TOO_LARGE_NUMBER);
|
|
80
|
+
}
|
|
81
|
+
reserve(bc, 4);
|
|
82
|
+
bc.view.setInt32(bc.offset, x, true);
|
|
83
|
+
bc.offset += 4;
|
|
84
|
+
}
|
|
85
|
+
export function readI64(bc) {
|
|
86
|
+
check(bc, 8);
|
|
87
|
+
const result = bc.view.getBigInt64(bc.offset, true);
|
|
88
|
+
bc.offset += 8;
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
export function writeI64(bc, x) {
|
|
92
|
+
if (DEV) {
|
|
93
|
+
assert(isI64(x), TOO_LARGE_NUMBER);
|
|
94
|
+
}
|
|
95
|
+
reserve(bc, 8);
|
|
96
|
+
bc.view.setBigInt64(bc.offset, x, true);
|
|
97
|
+
bc.offset += 8;
|
|
98
|
+
}
|
|
99
|
+
export function readI64Safe(bc) {
|
|
100
|
+
const result = readU32(bc) + readI32(bc) * /* 2**32 */ 4294967296;
|
|
101
|
+
if (!Number.isSafeInteger(result)) {
|
|
102
|
+
bc.offset -= 8;
|
|
103
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
export function writeI64Safe(bc, x) {
|
|
108
|
+
if (DEV) {
|
|
109
|
+
assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
|
|
110
|
+
}
|
|
111
|
+
let lowest32 = x >>> 0;
|
|
112
|
+
writeU32(bc, lowest32);
|
|
113
|
+
let highest32 = (x / /* 2**32 */ 4294967296) | 0;
|
|
114
|
+
if (x < 0) {
|
|
115
|
+
// get two's complement representation of the highest 21bits
|
|
116
|
+
highest32 = ~(Math.abs(highest32) & /* 2**21-1 */ 2097151) >>> 0;
|
|
117
|
+
if (lowest32 === 0) {
|
|
118
|
+
if (highest32 === 2097151) {
|
|
119
|
+
// maps -2**53 to Number.MIN_SAFE_INTEGER
|
|
120
|
+
// this is useful when assertions are skipped
|
|
121
|
+
lowest32 = 1;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
highest32++;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
writeU32(bc, highest32);
|
|
129
|
+
}
|
|
130
|
+
export function readU8(bc) {
|
|
131
|
+
check(bc, 1);
|
|
132
|
+
return bc.bytes[bc.offset++];
|
|
133
|
+
}
|
|
134
|
+
export function writeU8(bc, x) {
|
|
135
|
+
if (DEV) {
|
|
136
|
+
assert(isU8(x), TOO_LARGE_NUMBER);
|
|
137
|
+
}
|
|
138
|
+
reserve(bc, 1);
|
|
139
|
+
bc.bytes[bc.offset++] = x;
|
|
140
|
+
}
|
|
141
|
+
export function readU16(bc) {
|
|
142
|
+
check(bc, 2);
|
|
143
|
+
const result = bc.view.getUint16(bc.offset, true);
|
|
144
|
+
bc.offset += 2;
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
export function writeU16(bc, x) {
|
|
148
|
+
if (DEV) {
|
|
149
|
+
assert(isU16(x), TOO_LARGE_NUMBER);
|
|
150
|
+
}
|
|
151
|
+
reserve(bc, 2);
|
|
152
|
+
bc.view.setUint16(bc.offset, x, true);
|
|
153
|
+
bc.offset += 2;
|
|
154
|
+
}
|
|
155
|
+
export function readU32(bc) {
|
|
156
|
+
check(bc, 4);
|
|
157
|
+
const result = bc.view.getUint32(bc.offset, true);
|
|
158
|
+
bc.offset += 4;
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
export function writeU32(bc, x) {
|
|
162
|
+
if (DEV) {
|
|
163
|
+
assert(isU32(x), TOO_LARGE_NUMBER);
|
|
164
|
+
}
|
|
165
|
+
reserve(bc, 4);
|
|
166
|
+
bc.view.setUint32(bc.offset, x, true);
|
|
167
|
+
bc.offset += 4;
|
|
168
|
+
}
|
|
169
|
+
export function readU64(bc) {
|
|
170
|
+
check(bc, 8);
|
|
171
|
+
const result = bc.view.getBigUint64(bc.offset, true);
|
|
172
|
+
bc.offset += 8;
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
export function writeU64(bc, x) {
|
|
176
|
+
if (DEV) {
|
|
177
|
+
assert(isU64(x), TOO_LARGE_NUMBER);
|
|
178
|
+
}
|
|
179
|
+
reserve(bc, 8);
|
|
180
|
+
bc.view.setBigUint64(bc.offset, x, true);
|
|
181
|
+
bc.offset += 8;
|
|
182
|
+
}
|
|
183
|
+
export function readU64Safe(bc) {
|
|
184
|
+
const result = readU32(bc) + readU32(bc) * /* 2**32 */ 4294967296;
|
|
185
|
+
if (!isU64Safe(result)) {
|
|
186
|
+
bc.offset -= 8;
|
|
187
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
export function writeU64Safe(bc, x) {
|
|
192
|
+
if (DEV) {
|
|
193
|
+
assert(isU64Safe(x), TOO_LARGE_NUMBER);
|
|
194
|
+
}
|
|
195
|
+
writeU32(bc, x >>> 0);
|
|
196
|
+
writeU32(bc, (x / /* 2**32 */ 4294967296) & /* 2**21-1 */ 2097151);
|
|
197
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ByteCursor } from "../core/byte-cursor.
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.ts";
|
|
2
2
|
export declare const readI16FixedArray: (bc: ByteCursor, len: number) => Int16Array<ArrayBuffer>;
|
|
3
3
|
export declare function readI16Array(bc: ByteCursor): Int16Array<ArrayBuffer>;
|
|
4
4
|
export declare const writeI16FixedArray: (bc: ByteCursor, x: Int16Array) => void;
|
package/dist/codec/i16-array.js
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
2
3
|
import { check, reserve } from "../core/byte-cursor.js";
|
|
3
4
|
import { assert, DEV } from "../util/assert.js";
|
|
4
5
|
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
|
|
5
6
|
import { isU32 } from "../util/validator.js";
|
|
6
7
|
import { readFixedData } from "./data.js";
|
|
7
|
-
import {
|
|
8
|
-
readI16,
|
|
9
|
-
readUintSafe32,
|
|
10
|
-
writeI16,
|
|
11
|
-
writeUintSafe32
|
|
12
|
-
} from "./primitive.js";
|
|
8
|
+
import { readI16, writeI16 } from "./fixed-primitive.js";
|
|
13
9
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
14
|
-
|
|
10
|
+
import { readUintSafe32, writeUintSafe32 } from "./uint.js";
|
|
11
|
+
export const readI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM
|
|
12
|
+
? readI16FixedArrayLe
|
|
13
|
+
: readI16FixedArrayBe;
|
|
15
14
|
export function readI16Array(bc) {
|
|
16
|
-
|
|
15
|
+
return readI16FixedArray(bc, readUintSafe32(bc));
|
|
17
16
|
}
|
|
18
17
|
function readI16FixedArrayLe(bc, len) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
if (DEV) {
|
|
19
|
+
assert(isU32(len));
|
|
20
|
+
}
|
|
21
|
+
const byteCount = len * 2;
|
|
22
|
+
return new Int16Array(readFixedData(bc, byteCount));
|
|
24
23
|
}
|
|
25
24
|
function readI16FixedArrayBe(bc, len) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
if (DEV) {
|
|
26
|
+
assert(isU32(len));
|
|
27
|
+
}
|
|
28
|
+
check(bc, len * 2);
|
|
29
|
+
const result = new Int16Array(len);
|
|
30
|
+
for (let i = 0; i < len; i++) {
|
|
31
|
+
result[i] = readI16(bc);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
35
34
|
}
|
|
36
35
|
export const writeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI16FixedArrayLe : writeI16FixedArrayBe;
|
|
37
36
|
export function writeI16Array(bc, x) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
writeUintSafe32(bc, x.length);
|
|
38
|
+
if (x.length > 0) {
|
|
39
|
+
writeI16FixedArray(bc, x);
|
|
40
|
+
}
|
|
42
41
|
}
|
|
43
42
|
function writeI16FixedArrayLe(bc, x) {
|
|
44
|
-
|
|
43
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
45
44
|
}
|
|
46
45
|
function writeI16FixedArrayBe(bc, x) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
reserve(bc, x.length * 2);
|
|
47
|
+
for (let i = 0; i < x.length; i++) {
|
|
48
|
+
writeI16(bc, x[i]);
|
|
49
|
+
}
|
|
51
50
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ByteCursor } from "../core/byte-cursor.
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.ts";
|
|
2
2
|
export declare const readI32FixedArray: (bc: ByteCursor, len: number) => Int32Array<ArrayBuffer>;
|
|
3
3
|
export declare function readI32Array(bc: ByteCursor): Int32Array<ArrayBuffer>;
|
|
4
4
|
export declare const writeI32FixedArray: (bc: ByteCursor, x: Int32Array) => void;
|
package/dist/codec/i32-array.js
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
2
3
|
import { check, reserve } from "../core/byte-cursor.js";
|
|
3
4
|
import { assert, DEV } from "../util/assert.js";
|
|
4
5
|
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
|
|
5
6
|
import { isU32 } from "../util/validator.js";
|
|
6
7
|
import { readFixedData } from "./data.js";
|
|
7
|
-
import {
|
|
8
|
-
readI32,
|
|
9
|
-
readUintSafe32,
|
|
10
|
-
writeI32,
|
|
11
|
-
writeUintSafe32
|
|
12
|
-
} from "./primitive.js";
|
|
8
|
+
import { readI32, writeI32 } from "./fixed-primitive.js";
|
|
13
9
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
14
|
-
|
|
10
|
+
import { readUintSafe32, writeUintSafe32 } from "./uint.js";
|
|
11
|
+
export const readI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM
|
|
12
|
+
? readI32FixedArrayLe
|
|
13
|
+
: readI32FixedArrayBe;
|
|
15
14
|
export function readI32Array(bc) {
|
|
16
|
-
|
|
15
|
+
return readI32FixedArray(bc, readUintSafe32(bc));
|
|
17
16
|
}
|
|
18
17
|
function readI32FixedArrayLe(bc, len) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
if (DEV) {
|
|
19
|
+
assert(isU32(len));
|
|
20
|
+
}
|
|
21
|
+
const byteCount = len * 4;
|
|
22
|
+
return new Int32Array(readFixedData(bc, byteCount));
|
|
24
23
|
}
|
|
25
24
|
function readI32FixedArrayBe(bc, len) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
if (DEV) {
|
|
26
|
+
assert(isU32(len));
|
|
27
|
+
}
|
|
28
|
+
check(bc, len * 4);
|
|
29
|
+
const result = new Int32Array(len);
|
|
30
|
+
for (let i = 0; i < len; i++) {
|
|
31
|
+
result[i] = readI32(bc);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
35
34
|
}
|
|
36
35
|
export const writeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI32FixedArrayLe : writeI32FixedArrayBe;
|
|
37
36
|
export function writeI32Array(bc, x) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
writeUintSafe32(bc, x.length);
|
|
38
|
+
if (x.length > 0) {
|
|
39
|
+
writeI32FixedArray(bc, x);
|
|
40
|
+
}
|
|
42
41
|
}
|
|
43
42
|
function writeI32FixedArrayLe(bc, x) {
|
|
44
|
-
|
|
43
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
45
44
|
}
|
|
46
45
|
function writeI32FixedArrayBe(bc, x) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
reserve(bc, x.length * 4);
|
|
47
|
+
for (let i = 0; i < x.length; i++) {
|
|
48
|
+
writeI32(bc, x[i]);
|
|
49
|
+
}
|
|
51
50
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ByteCursor } from "../core/byte-cursor.
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.ts";
|
|
2
2
|
export declare const readI64FixedArray: (bc: ByteCursor, len: number) => BigInt64Array<ArrayBuffer>;
|
|
3
3
|
export declare function readI64Array(bc: ByteCursor): BigInt64Array<ArrayBuffer>;
|
|
4
4
|
export declare const writeI64FixedArray: (bc: ByteCursor, x: BigInt64Array) => void;
|
package/dist/codec/i64-array.js
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
2
3
|
import { check, reserve } from "../core/byte-cursor.js";
|
|
3
4
|
import { assert, DEV } from "../util/assert.js";
|
|
4
5
|
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
|
|
5
6
|
import { isU32 } from "../util/validator.js";
|
|
6
7
|
import { readFixedData } from "./data.js";
|
|
7
|
-
import {
|
|
8
|
-
readI64,
|
|
9
|
-
readUintSafe32,
|
|
10
|
-
writeI64,
|
|
11
|
-
writeUintSafe32
|
|
12
|
-
} from "./primitive.js";
|
|
8
|
+
import { readI64, writeI64 } from "./fixed-primitive.js";
|
|
13
9
|
import { writeU8FixedArray } from "./u8-array.js";
|
|
14
|
-
|
|
10
|
+
import { readUintSafe32, writeUintSafe32 } from "./uint.js";
|
|
11
|
+
export const readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM
|
|
12
|
+
? readI64FixedArrayLe
|
|
13
|
+
: readI64FixedArrayBe;
|
|
15
14
|
export function readI64Array(bc) {
|
|
16
|
-
|
|
15
|
+
return readI64FixedArray(bc, readUintSafe32(bc));
|
|
17
16
|
}
|
|
18
17
|
function readI64FixedArrayLe(bc, len) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
if (DEV) {
|
|
19
|
+
assert(isU32(len));
|
|
20
|
+
}
|
|
21
|
+
const byteCount = len * 8;
|
|
22
|
+
return new BigInt64Array(readFixedData(bc, byteCount));
|
|
24
23
|
}
|
|
25
24
|
function readI64FixedArrayBe(bc, len) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
if (DEV) {
|
|
26
|
+
assert(isU32(len));
|
|
27
|
+
}
|
|
28
|
+
check(bc, len * 8);
|
|
29
|
+
const result = new BigInt64Array(len);
|
|
30
|
+
for (let i = 0; i < len; i++) {
|
|
31
|
+
result[i] = readI64(bc);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
35
34
|
}
|
|
36
35
|
export const writeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI64FixedArrayLe : writeI64FixedArrayBe;
|
|
37
36
|
export function writeI64Array(bc, x) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
writeUintSafe32(bc, x.length);
|
|
38
|
+
if (x.length > 0) {
|
|
39
|
+
writeI64FixedArray(bc, x);
|
|
40
|
+
}
|
|
42
41
|
}
|
|
43
42
|
function writeI64FixedArrayLe(bc, x) {
|
|
44
|
-
|
|
43
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
45
44
|
}
|
|
46
45
|
function writeI64FixedArrayBe(bc, x) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
reserve(bc, x.length * 8);
|
|
47
|
+
for (let i = 0; i < x.length; i++) {
|
|
48
|
+
writeI64(bc, x[i]);
|
|
49
|
+
}
|
|
51
50
|
}
|