@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/core/byte-cursor.js
CHANGED
|
@@ -1,42 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
import { DEV, assert } from "../util/assert.js";
|
|
2
3
|
import { TOO_LARGE_BUFFER } from "../util/constants.js";
|
|
4
|
+
import { isU32 } from "../util/validator.js";
|
|
3
5
|
import { BareError } from "./bare-error.js";
|
|
4
|
-
|
|
6
|
+
export class ByteCursor {
|
|
7
|
+
/**
|
|
8
|
+
* @throws {BareError} Buffer exceeds `config.maxBufferLength`
|
|
9
|
+
*/
|
|
5
10
|
constructor(bytes, config) {
|
|
11
|
+
/**
|
|
12
|
+
* Read and write Offset in {@link view} and {@link bytes}
|
|
13
|
+
*/
|
|
14
|
+
this.offset = 0;
|
|
6
15
|
if (bytes.length > config.maxBufferLength) {
|
|
7
16
|
throw new BareError(0, TOO_LARGE_BUFFER);
|
|
8
17
|
}
|
|
9
18
|
this.bytes = bytes;
|
|
10
19
|
this.config = config;
|
|
11
|
-
this.offset = 0;
|
|
12
20
|
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
13
21
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
}
|
|
23
|
+
export function check(bc, min) {
|
|
24
|
+
if (DEV) {
|
|
25
|
+
assert(isU32(min));
|
|
18
26
|
}
|
|
19
|
-
|
|
20
|
-
|
|
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);
|
|
26
|
-
const newBytes = new Uint8Array(newLen);
|
|
27
|
-
newBytes.set(this.bytes);
|
|
28
|
-
this.bytes = newBytes;
|
|
29
|
-
this.view = new DataView(newBytes.buffer);
|
|
30
|
-
}
|
|
27
|
+
if (bc.offset + min > bc.bytes.length) {
|
|
28
|
+
throw new BareError(bc.offset, "missing bytes");
|
|
31
29
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
}
|
|
31
|
+
export function reserve(bc, min) {
|
|
32
|
+
if (DEV) {
|
|
33
|
+
assert(isU32(min));
|
|
34
|
+
}
|
|
35
|
+
const minLen = bc.offset + min | 0;
|
|
36
|
+
if (minLen > bc.bytes.length) {
|
|
37
|
+
if (minLen > bc.config.maxBufferLength) {
|
|
38
|
+
throw new BareError(0, TOO_LARGE_BUFFER);
|
|
39
|
+
}
|
|
40
|
+
const newLen = Math.min(minLen << 1, bc.config.maxBufferLength);
|
|
41
|
+
const newBytes = new Uint8Array(newLen);
|
|
42
|
+
newBytes.set(bc.bytes);
|
|
43
|
+
bc.bytes = newBytes;
|
|
44
|
+
bc.view = new DataView(newBytes.buffer);
|
|
37
45
|
}
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
ByteCursor
|
|
41
|
-
};
|
|
42
|
-
//# sourceMappingURL=byte-cursor.js.map
|
|
46
|
+
}
|
package/dist/core/config.d.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Config = {
|
|
2
2
|
readonly initialBufferLength: number;
|
|
3
3
|
readonly maxBufferLength: number;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
export declare function Config({ initialBufferLength, maxBufferLength, textDecoderThreshold, textEncoderThreshold, }: {
|
|
4
|
+
};
|
|
5
|
+
export declare function Config({ initialBufferLength, maxBufferLength, }: {
|
|
8
6
|
initialBufferLength?: number | undefined;
|
|
9
7
|
maxBufferLength?: number | undefined;
|
|
10
|
-
textDecoderThreshold?: number | undefined;
|
|
11
|
-
textEncoderThreshold?: number | undefined;
|
|
12
8
|
}): Config;
|
package/dist/core/config.js
CHANGED
|
@@ -1,27 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
import { assert } from "../util/assert.js";
|
|
1
|
+
"use strict";
|
|
2
|
+
import { DEV, assert } from "../util/assert.js";
|
|
3
3
|
import { TOO_LARGE_NUMBER } from "../util/constants.js";
|
|
4
4
|
import { isU32 } from "../util/validator.js";
|
|
5
|
-
function Config({
|
|
5
|
+
export function Config({
|
|
6
6
|
initialBufferLength = 1024,
|
|
7
|
-
maxBufferLength = 1024 * 1024 * 32
|
|
8
|
-
textDecoderThreshold = 256,
|
|
9
|
-
textEncoderThreshold = 256
|
|
7
|
+
maxBufferLength = 1024 * 1024 * 32
|
|
10
8
|
}) {
|
|
11
|
-
|
|
9
|
+
if (DEV) {
|
|
10
|
+
assert(isU32(initialBufferLength), TOO_LARGE_NUMBER);
|
|
11
|
+
assert(isU32(maxBufferLength), TOO_LARGE_NUMBER);
|
|
12
|
+
assert(
|
|
13
|
+
initialBufferLength <= maxBufferLength,
|
|
14
|
+
"initialBufferLength must be lower than or equal to maxBufferLength"
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
12
18
|
initialBufferLength,
|
|
13
|
-
maxBufferLength
|
|
14
|
-
textDecoderThreshold,
|
|
15
|
-
textEncoderThreshold
|
|
19
|
+
maxBufferLength
|
|
16
20
|
};
|
|
17
|
-
assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
|
|
18
|
-
assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
|
|
19
|
-
assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
|
|
20
|
-
assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
|
|
21
|
-
assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
|
|
22
|
-
return config;
|
|
23
21
|
}
|
|
24
|
-
export {
|
|
25
|
-
Config
|
|
26
|
-
};
|
|
27
|
-
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEV = false;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEV = true;
|
package/dist/env/dev.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEV: boolean;
|