@bare-ts/lib 0.1.1 → 0.2.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/dist/codec/data.d.ts +4 -4
- package/dist/codec/data.js +16 -16
- package/dist/codec/float-array.d.ts +12 -12
- package/dist/codec/float-array.js +46 -49
- package/dist/codec/i16-array.d.ts +6 -6
- package/dist/codec/i16-array.js +22 -29
- package/dist/codec/i32-array.d.ts +6 -6
- package/dist/codec/i32-array.js +22 -29
- package/dist/codec/i64-array.d.ts +6 -6
- package/dist/codec/i64-array.js +22 -29
- package/dist/codec/i8-array.d.ts +4 -4
- package/dist/codec/i8-array.js +16 -16
- package/dist/codec/primitive.d.ts +36 -36
- package/dist/codec/primitive.js +139 -132
- package/dist/codec/string.d.ts +4 -2
- package/dist/codec/string.js +81 -77
- package/dist/codec/u16-array.d.ts +6 -6
- package/dist/codec/u16-array.js +22 -29
- package/dist/codec/u32-array.d.ts +6 -6
- package/dist/codec/u32-array.js +22 -29
- package/dist/codec/u64-array.d.ts +6 -6
- package/dist/codec/u64-array.js +22 -29
- package/dist/codec/u8-array.d.ts +4 -4
- package/dist/codec/u8-array.js +18 -16
- package/dist/codec/u8-clamped-array.d.ts +4 -4
- package/dist/codec/u8-clamped-array.js +16 -16
- package/dist/core/byte-cursor.d.ts +26 -8
- package/dist/core/byte-cursor.js +18 -28
- package/dist/index.cjs +551 -571
- package/package.json +1 -1
- package/CHANGELOG.md +0 -37
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare function
|
|
4
|
-
declare function
|
|
5
|
-
export declare const
|
|
6
|
-
export declare function
|
|
7
|
-
declare function
|
|
2
|
+
export declare const readU16FixedArray: typeof readU16FixedArrayLE;
|
|
3
|
+
export declare function readU16Array(bc: ByteCursor): Uint16Array;
|
|
4
|
+
declare function readU16FixedArrayLE(bc: ByteCursor, len: number): Uint16Array;
|
|
5
|
+
export declare const writeU16FixedArray: typeof writeU16FixedArrayLE;
|
|
6
|
+
export declare function writeU16Array(bc: ByteCursor, x: Uint16Array): void;
|
|
7
|
+
declare function writeU16FixedArrayLE(bc: ByteCursor, x: Uint16Array): void;
|
|
8
8
|
export {};
|
package/dist/codec/u16-array.js
CHANGED
|
@@ -1,50 +1,43 @@
|
|
|
1
1
|
// src/codec/u16-array.ts
|
|
2
|
-
import { ok as assert } from "assert";
|
|
3
2
|
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
decodeUintSafe,
|
|
8
|
-
encodeU16,
|
|
9
|
-
encodeUintSafe
|
|
10
|
-
} from "./primitive.js";
|
|
3
|
+
import { readFixedData } from "./data.js";
|
|
4
|
+
import { readU16, readUintSafe, writeU16, writeUintSafe } from "./primitive.js";
|
|
5
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
11
6
|
var U16_BYTE_COUNT = 2;
|
|
12
|
-
var
|
|
13
|
-
function
|
|
14
|
-
return
|
|
7
|
+
var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
|
|
8
|
+
function readU16Array(bc) {
|
|
9
|
+
return readU16FixedArray(bc, readUintSafe(bc));
|
|
15
10
|
}
|
|
16
|
-
function
|
|
11
|
+
function readU16FixedArrayLE(bc, len) {
|
|
17
12
|
const byteCount = len * U16_BYTE_COUNT;
|
|
18
|
-
return new Uint16Array(
|
|
13
|
+
return new Uint16Array(readFixedData(bc, byteCount));
|
|
19
14
|
}
|
|
20
|
-
function
|
|
15
|
+
function readU16FixedArrayBE(bc, len) {
|
|
21
16
|
bc.check(len * U16_BYTE_COUNT);
|
|
22
17
|
const result = new Uint16Array(len);
|
|
23
18
|
for (let i = 0; i < len; i++)
|
|
24
|
-
result[i] =
|
|
19
|
+
result[i] = readU16(bc);
|
|
25
20
|
return result;
|
|
26
21
|
}
|
|
27
|
-
var
|
|
28
|
-
function
|
|
29
|
-
|
|
22
|
+
var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLE : writeU16FixedArrayBE;
|
|
23
|
+
function writeU16Array(bc, x) {
|
|
24
|
+
writeUintSafe(bc, x.length);
|
|
30
25
|
if (x.length !== 0) {
|
|
31
|
-
|
|
26
|
+
writeU16FixedArray(bc, x);
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
29
|
+
function writeU16FixedArrayLE(bc, x) {
|
|
30
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
37
31
|
}
|
|
38
|
-
function
|
|
39
|
-
assert(x.length === len);
|
|
32
|
+
function writeU16FixedArrayBE(bc, x) {
|
|
40
33
|
bc.reserve(x.length * U16_BYTE_COUNT);
|
|
41
34
|
for (let i = 0; i < x.length; i++)
|
|
42
|
-
|
|
35
|
+
writeU16(bc, x[i]);
|
|
43
36
|
}
|
|
44
37
|
export {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
readU16Array,
|
|
39
|
+
readU16FixedArray,
|
|
40
|
+
writeU16Array,
|
|
41
|
+
writeU16FixedArray
|
|
49
42
|
};
|
|
50
43
|
//# sourceMappingURL=u16-array.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare function
|
|
4
|
-
declare function
|
|
5
|
-
export declare const
|
|
6
|
-
export declare function
|
|
7
|
-
declare function
|
|
2
|
+
export declare const readU32FixedArray: typeof readU32FixedArrayLE;
|
|
3
|
+
export declare function readU32Array(bc: ByteCursor): Uint32Array;
|
|
4
|
+
declare function readU32FixedArrayLE(bc: ByteCursor, len: number): Uint32Array;
|
|
5
|
+
export declare const writeU32FixedArray: typeof writeU32FixedArrayLE;
|
|
6
|
+
export declare function writeU32Array(bc: ByteCursor, x: Uint32Array): void;
|
|
7
|
+
declare function writeU32FixedArrayLE(bc: ByteCursor, x: Uint32Array): void;
|
|
8
8
|
export {};
|
package/dist/codec/u32-array.js
CHANGED
|
@@ -1,50 +1,43 @@
|
|
|
1
1
|
// src/codec/u32-array.ts
|
|
2
|
-
import { ok as assert } from "assert";
|
|
3
2
|
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
decodeUintSafe,
|
|
8
|
-
encodeU32,
|
|
9
|
-
encodeUintSafe
|
|
10
|
-
} from "./primitive.js";
|
|
3
|
+
import { readFixedData } from "./data.js";
|
|
4
|
+
import { readU32, readUintSafe, writeU32, writeUintSafe } from "./primitive.js";
|
|
5
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
11
6
|
var U32_BYTE_COUNT = 4;
|
|
12
|
-
var
|
|
13
|
-
function
|
|
14
|
-
return
|
|
7
|
+
var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
|
|
8
|
+
function readU32Array(bc) {
|
|
9
|
+
return readU32FixedArray(bc, readUintSafe(bc));
|
|
15
10
|
}
|
|
16
|
-
function
|
|
11
|
+
function readU32FixedArrayLE(bc, len) {
|
|
17
12
|
const byteCount = len * U32_BYTE_COUNT;
|
|
18
|
-
return new Uint32Array(
|
|
13
|
+
return new Uint32Array(readFixedData(bc, byteCount));
|
|
19
14
|
}
|
|
20
|
-
function
|
|
15
|
+
function readU32FixedArrayBE(bc, len) {
|
|
21
16
|
bc.check(len * U32_BYTE_COUNT);
|
|
22
17
|
const result = new Uint32Array(len);
|
|
23
18
|
for (let i = 0; i < len; i++)
|
|
24
|
-
result[i] =
|
|
19
|
+
result[i] = readU32(bc);
|
|
25
20
|
return result;
|
|
26
21
|
}
|
|
27
|
-
var
|
|
28
|
-
function
|
|
29
|
-
|
|
22
|
+
var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLE : writeU32FixedArrayBE;
|
|
23
|
+
function writeU32Array(bc, x) {
|
|
24
|
+
writeUintSafe(bc, x.length);
|
|
30
25
|
if (x.length !== 0) {
|
|
31
|
-
|
|
26
|
+
writeU32FixedArray(bc, x);
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
29
|
+
function writeU32FixedArrayLE(bc, x) {
|
|
30
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
37
31
|
}
|
|
38
|
-
function
|
|
39
|
-
assert(x.length === len);
|
|
32
|
+
function writeU32FixedArrayBE(bc, x) {
|
|
40
33
|
bc.reserve(x.length * U32_BYTE_COUNT);
|
|
41
34
|
for (let i = 0; i < x.length; i++)
|
|
42
|
-
|
|
35
|
+
writeU32(bc, x[i]);
|
|
43
36
|
}
|
|
44
37
|
export {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
readU32Array,
|
|
39
|
+
readU32FixedArray,
|
|
40
|
+
writeU32Array,
|
|
41
|
+
writeU32FixedArray
|
|
49
42
|
};
|
|
50
43
|
//# sourceMappingURL=u32-array.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare function
|
|
4
|
-
declare function
|
|
5
|
-
export declare const
|
|
6
|
-
export declare function
|
|
7
|
-
declare function
|
|
2
|
+
export declare const readU64FixedArray: typeof readU64FixedArrayLE;
|
|
3
|
+
export declare function readU64Array(bc: ByteCursor): BigUint64Array;
|
|
4
|
+
declare function readU64FixedArrayLE(bc: ByteCursor, len: number): BigUint64Array;
|
|
5
|
+
export declare const writeU64FixedArray: typeof writeU64FixedArrayLE;
|
|
6
|
+
export declare function writeU64Array(bc: ByteCursor, x: BigUint64Array): void;
|
|
7
|
+
declare function writeU64FixedArrayLE(bc: ByteCursor, x: BigUint64Array): void;
|
|
8
8
|
export {};
|
package/dist/codec/u64-array.js
CHANGED
|
@@ -1,50 +1,43 @@
|
|
|
1
1
|
// src/codec/u64-array.ts
|
|
2
|
-
import { ok as assert } from "assert";
|
|
3
2
|
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
decodeUintSafe,
|
|
8
|
-
encodeU64,
|
|
9
|
-
encodeUintSafe
|
|
10
|
-
} from "./primitive.js";
|
|
3
|
+
import { readFixedData } from "./data.js";
|
|
4
|
+
import { readU64, readUintSafe, writeU64, writeUintSafe } from "./primitive.js";
|
|
5
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
11
6
|
var U64_BYTE_COUNT = 8;
|
|
12
|
-
var
|
|
13
|
-
function
|
|
14
|
-
return
|
|
7
|
+
var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
|
|
8
|
+
function readU64Array(bc) {
|
|
9
|
+
return readU64FixedArray(bc, readUintSafe(bc));
|
|
15
10
|
}
|
|
16
|
-
function
|
|
11
|
+
function readU64FixedArrayLE(bc, len) {
|
|
17
12
|
const byteCount = len * U64_BYTE_COUNT;
|
|
18
|
-
return new BigUint64Array(
|
|
13
|
+
return new BigUint64Array(readFixedData(bc, byteCount));
|
|
19
14
|
}
|
|
20
|
-
function
|
|
15
|
+
function readU64FixedArrayBE(bc, len) {
|
|
21
16
|
bc.check(len * U64_BYTE_COUNT);
|
|
22
17
|
const result = new BigUint64Array(len);
|
|
23
18
|
for (let i = 0; i < len; i++)
|
|
24
|
-
result[i] =
|
|
19
|
+
result[i] = readU64(bc);
|
|
25
20
|
return result;
|
|
26
21
|
}
|
|
27
|
-
var
|
|
28
|
-
function
|
|
29
|
-
|
|
22
|
+
var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLE : writeU64FixedArrayBE;
|
|
23
|
+
function writeU64Array(bc, x) {
|
|
24
|
+
writeUintSafe(bc, x.length);
|
|
30
25
|
if (x.length !== 0) {
|
|
31
|
-
|
|
26
|
+
writeU64FixedArray(bc, x);
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
29
|
+
function writeU64FixedArrayLE(bc, x) {
|
|
30
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
37
31
|
}
|
|
38
|
-
function
|
|
39
|
-
assert(x.length === len);
|
|
32
|
+
function writeU64FixedArrayBE(bc, x) {
|
|
40
33
|
bc.reserve(x.length * U64_BYTE_COUNT);
|
|
41
34
|
for (let i = 0; i < x.length; i++)
|
|
42
|
-
|
|
35
|
+
writeU64(bc, x[i]);
|
|
43
36
|
}
|
|
44
37
|
export {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
readU64Array,
|
|
39
|
+
readU64FixedArray,
|
|
40
|
+
writeU64Array,
|
|
41
|
+
writeU64FixedArray
|
|
49
42
|
};
|
|
50
43
|
//# sourceMappingURL=u64-array.js.map
|
package/dist/codec/u8-array.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
2
|
+
export declare function readU8Array(bc: ByteCursor): Uint8Array;
|
|
3
|
+
export declare function writeU8Array(bc: ByteCursor, x: Uint8Array): void;
|
|
4
|
+
export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
|
|
5
|
+
export declare function writeU8FixedArray(bc: ByteCursor, x: Uint8Array): void;
|
package/dist/codec/u8-array.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
// src/codec/u8-array.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const len = decodeUintSafe(bc);
|
|
6
|
-
return bc.read(len).slice();
|
|
2
|
+
import { readUintSafe, writeUintSafe } from "./primitive.js";
|
|
3
|
+
function readU8Array(bc) {
|
|
4
|
+
return readU8FixedArray(bc, readUintSafe(bc));
|
|
7
5
|
}
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
bc
|
|
6
|
+
function writeU8Array(bc, x) {
|
|
7
|
+
writeUintSafe(bc, x.length);
|
|
8
|
+
writeU8FixedArray(bc, x);
|
|
11
9
|
}
|
|
12
|
-
function
|
|
10
|
+
function readU8FixedArray(bc, len) {
|
|
13
11
|
return bc.read(len).slice();
|
|
14
12
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
function writeU8FixedArray(bc, x) {
|
|
14
|
+
const len = x.length;
|
|
15
|
+
if (len !== 0) {
|
|
16
|
+
bc.reserve(len);
|
|
17
|
+
bc.bytes.set(x, bc.offset);
|
|
18
|
+
bc.offset += len;
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
export {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
readU8Array,
|
|
23
|
+
readU8FixedArray,
|
|
24
|
+
writeU8Array,
|
|
25
|
+
writeU8FixedArray
|
|
24
26
|
};
|
|
25
27
|
//# sourceMappingURL=u8-array.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
2
|
+
export declare function readU8ClampedArray(bc: ByteCursor): Uint8ClampedArray;
|
|
3
|
+
export declare function writeU8ClampedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
|
|
4
|
+
export declare function readU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray;
|
|
5
|
+
export declare function writeU8ClampedFixedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
// src/codec/u8-clamped-array.ts
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
function
|
|
6
|
-
return
|
|
2
|
+
import { readFixedData } from "./data.js";
|
|
3
|
+
import { readUintSafe, writeUintSafe } from "./primitive.js";
|
|
4
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
5
|
+
function readU8ClampedArray(bc) {
|
|
6
|
+
return readU8ClampedFixedArray(bc, readUintSafe(bc));
|
|
7
7
|
}
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
function writeU8ClampedArray(bc, x) {
|
|
9
|
+
writeUintSafe(bc, x.length);
|
|
10
|
+
writeU8ClampedFixedArray(bc, x);
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
return new Uint8ClampedArray(
|
|
12
|
+
function readU8ClampedFixedArray(bc, len) {
|
|
13
|
+
return new Uint8ClampedArray(readFixedData(bc, len));
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
15
|
+
function writeU8ClampedFixedArray(bc, x) {
|
|
16
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
17
17
|
}
|
|
18
18
|
export {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
readU8ClampedArray,
|
|
20
|
+
readU8ClampedFixedArray,
|
|
21
|
+
writeU8ClampedArray,
|
|
22
|
+
writeU8ClampedFixedArray
|
|
23
23
|
};
|
|
24
24
|
//# sourceMappingURL=u8-clamped-array.js.map
|
|
@@ -1,9 +1,32 @@
|
|
|
1
1
|
import type { Config } from "./config.js";
|
|
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
|
|
8
|
+
*
|
|
9
|
+
* | {bytes,view}.buffer |
|
|
10
|
+
* | bytes |
|
|
11
|
+
* | view |
|
|
12
|
+
* |<------ offset ------>|
|
|
13
|
+
* |<----------- config.maxBufferLength ------------>|
|
|
14
|
+
*/
|
|
2
15
|
export declare class ByteCursor {
|
|
16
|
+
bytes: Uint8Array;
|
|
3
17
|
readonly config: Config;
|
|
18
|
+
/**
|
|
19
|
+
* Read and write Offset in {@link view} and {@link bytes}
|
|
20
|
+
*/
|
|
4
21
|
offset: number;
|
|
5
22
|
view: DataView;
|
|
6
|
-
|
|
23
|
+
/**
|
|
24
|
+
* @param bytes read and/or write buffer
|
|
25
|
+
* @param config runtime configuration
|
|
26
|
+
* @throw BareError when the buffer exceeds the maximum allowed length
|
|
27
|
+
* `config.maxBufferLength`
|
|
28
|
+
*/
|
|
29
|
+
constructor(bytes: Uint8Array, config: Config);
|
|
7
30
|
/**
|
|
8
31
|
* @param min number of needed bytes
|
|
9
32
|
* @throw BareError when there is not enough bytes
|
|
@@ -11,6 +34,8 @@ export declare class ByteCursor {
|
|
|
11
34
|
check(min: number): void;
|
|
12
35
|
/**
|
|
13
36
|
* @param min number of bytes to reserve
|
|
37
|
+
* @throw BareError when the buffer exceeds the maximum allowed length
|
|
38
|
+
* `config.maxBufferLength`
|
|
14
39
|
*/
|
|
15
40
|
reserve(min: number): void;
|
|
16
41
|
/**
|
|
@@ -21,11 +46,4 @@ export declare class ByteCursor {
|
|
|
21
46
|
* @returns read bytes
|
|
22
47
|
*/
|
|
23
48
|
read(len: number): Uint8Array;
|
|
24
|
-
/**
|
|
25
|
-
* Write {@code bytes} in the buffer and advance cursor by
|
|
26
|
-
* {@code bytes.length}
|
|
27
|
-
*
|
|
28
|
-
* @param bytes bytes to copy
|
|
29
|
-
*/
|
|
30
|
-
write(bytes: Uint8Array): void;
|
|
31
49
|
}
|
package/dist/core/byte-cursor.js
CHANGED
|
@@ -1,49 +1,39 @@
|
|
|
1
1
|
// src/core/byte-cursor.ts
|
|
2
|
-
import {
|
|
3
|
-
import { BareError } from "../core/bare-error.js";
|
|
2
|
+
import { BareError } from "./bare-error.js";
|
|
4
3
|
var TOO_LARGE_BUFFER = "too large buffer";
|
|
5
4
|
var ByteCursor = class {
|
|
6
5
|
constructor(bytes, config) {
|
|
7
|
-
|
|
8
|
-
this.offset = 0;
|
|
9
|
-
this.view = bytes instanceof Uint8Array ? new DataView(bytes.buffer, bytes.byteOffset, bytes.length) : new DataView(bytes);
|
|
10
|
-
if (this.view.byteLength > config.maxBufferLength) {
|
|
6
|
+
if (bytes.length > config.maxBufferLength) {
|
|
11
7
|
throw new BareError(0, TOO_LARGE_BUFFER);
|
|
12
8
|
}
|
|
9
|
+
this.bytes = bytes;
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.offset = 0;
|
|
12
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
13
13
|
}
|
|
14
14
|
check(min) {
|
|
15
|
-
if (this.offset + min > this.
|
|
15
|
+
if (this.offset + min > this.bytes.length) {
|
|
16
16
|
throw new BareError(this.offset, "missing bytes");
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
reserve(min) {
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const newLen = Math.min(bytes.length + Math.max(minExtraLength, bytes.length), config.maxBufferLength);
|
|
20
|
+
const minLen = this.offset + min | 0;
|
|
21
|
+
if (minLen > this.bytes.length) {
|
|
22
|
+
if (minLen > this.config.maxBufferLength) {
|
|
23
|
+
throw new BareError(0, TOO_LARGE_BUFFER);
|
|
24
|
+
}
|
|
25
|
+
const newLen = Math.min(minLen << 1, this.config.maxBufferLength);
|
|
27
26
|
const newBytes = new Uint8Array(newLen);
|
|
28
|
-
newBytes.set(bytes);
|
|
29
|
-
this.
|
|
27
|
+
newBytes.set(this.bytes);
|
|
28
|
+
this.bytes = newBytes;
|
|
29
|
+
this.view = new DataView(newBytes.buffer);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
read(len) {
|
|
33
33
|
this.check(len);
|
|
34
|
-
const
|
|
34
|
+
const offset = this.offset;
|
|
35
35
|
this.offset += len;
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
write(bytes) {
|
|
39
|
-
const len = bytes.length;
|
|
40
|
-
if (len !== 0) {
|
|
41
|
-
this.reserve(len);
|
|
42
|
-
const bufferOffset = this.view.byteOffset + this.offset;
|
|
43
|
-
const buffer = new Uint8Array(this.view.buffer);
|
|
44
|
-
buffer.set(bytes, bufferOffset);
|
|
45
|
-
this.offset += len;
|
|
46
|
-
}
|
|
36
|
+
return this.bytes.subarray(offset, offset + len);
|
|
47
37
|
}
|
|
48
38
|
};
|
|
49
39
|
export {
|