@bare-ts/lib 0.1.0 → 0.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ ## 0.1.1 (2022-01-09)
6
+
7
+ * Fix write offset when byteOffset > 0
8
+
9
+ A ByteCursor may be instantiated with an array of bytes such that
10
+ the array's byteOffset is greater than 0.
11
+ The previous ByteCursor.write implementation did not take care of adding this byteOffset to the ByteCursor's offset.
12
+
13
+ The following code no longer fail:
14
+
15
+ ```js
16
+ const bytes = Uint8Array.of(42, 24).subarray(1)
17
+ assert(bytes.byteOffset === 1)
18
+
19
+ const bc = ByteCursor(bytes, ...)
20
+ bc.write(Uint8Array.of(24))
21
+ assert.deepEqual(Array.from(bytes), [42, 24]) // Previously failed
22
+ ```
23
+
24
+ * Smaller CommonJS bundle
25
+
26
+ * Improve byte-length computation of small string
27
+
3
28
  ## 0.1.0 (2022-01-02)
4
29
 
5
30
  * `ByteCursor` abstraction to read and write safely a buffer of bytes
package/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # bare-ts library
2
2
 
3
- ![CI status][ci]
3
+ [![CI status][ci-img]][ci-url]
4
+ [![Coverage percentage][coveralls-img]][coveralls-url]
5
+ [![minified and gzipped size][bundlephobia-img]][bundlephobia-url]
6
+ [![NPM version][npm-img]][npm-url]
4
7
 
5
8
  [BARE][bare] (Binary Application Record Encoding) is a schema-based binary format that favors compactness and simplicity.
6
9
  [bare-ts/lib](#) provides decoders and encoders for basic BARE types.
@@ -12,4 +15,11 @@ The specification is likely to evolve before its final release. [bare-ts](#) imp
12
15
 
13
16
  [bare]: https://baremessages.org
14
17
  [bare-ts-tools]: https://github.com/bare-ts/tools
15
- [ci]: https://github.com/bare-ts/lib/actions/workflows/ci.yml/badge.svg
18
+ [ci-img]: https://img.shields.io/github/workflow/status/bare-ts/lib/CI?label=CI&style=flat-square
19
+ [ci-url]: https://github.com/bare-ts/lib/actions/workflows/ci.yml
20
+ [npm-img]: https://img.shields.io/npm/v/@bare-ts/lib.svg?style=flat-square
21
+ [npm-url]: https://www.npmjs.com/package/@bare-ts/lib
22
+ [coveralls-img]: https://img.shields.io/coveralls/github/bare-ts/lib?style=flat-square
23
+ [coveralls-url]: https://coveralls.io/github/bare-ts/lib?branch=main
24
+ [bundlephobia-img]: https://img.shields.io/bundlephobia/minzip/@bare-ts/lib?label=minzipped&style=flat-square
25
+ [bundlephobia-url]: https://bundlephobia.com/package/@bare-ts/lib
@@ -0,0 +1,26 @@
1
+ // src/codec/data.ts
2
+ import {
3
+ decodeU8Array,
4
+ decodeU8FixedArray,
5
+ encodeU8Array,
6
+ encodeU8FixedArray
7
+ } from "./u8-array.js";
8
+ function decodeData(bc) {
9
+ return decodeU8Array(bc).buffer;
10
+ }
11
+ function encodeData(bc, x) {
12
+ encodeU8Array(bc, new Uint8Array(x));
13
+ }
14
+ function decodeFixedData(bc, len) {
15
+ return decodeU8FixedArray(bc, len).buffer;
16
+ }
17
+ function encodeFixedData(bc, x, len) {
18
+ encodeU8FixedArray(bc, new Uint8Array(x), len);
19
+ }
20
+ export {
21
+ decodeData,
22
+ decodeFixedData,
23
+ encodeData,
24
+ encodeFixedData
25
+ };
26
+ //# sourceMappingURL=data.js.map
@@ -0,0 +1,99 @@
1
+ // src/codec/float-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { BareError } from "../core/index.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
5
+ import { decodeFixedData } from "./data.js";
6
+ import {
7
+ decodeF32,
8
+ decodeF64,
9
+ decodeUintSafe,
10
+ encodeF32,
11
+ encodeF64,
12
+ encodeUintSafe
13
+ } from "./primitive.js";
14
+ var NAN_NOT_ALLOWED = "NaN is not allowed";
15
+ var decodeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeF32FixedArrayLE : decodeF32FixedArrayBE;
16
+ function decodeF32FixedArrayLE(bc, len) {
17
+ const byteLen = len * 4;
18
+ const result = new Float32Array(decodeFixedData(bc, byteLen));
19
+ if (result.some(Number.isNaN)) {
20
+ throw new BareError(bc.offset, NAN_NOT_ALLOWED);
21
+ }
22
+ return result;
23
+ }
24
+ function decodeF32FixedArrayBE(bc, len) {
25
+ bc.check(len * 4);
26
+ const result = new Float32Array(len);
27
+ for (let i = 0; i < len; i++)
28
+ result[i] = decodeF32(bc);
29
+ return result;
30
+ }
31
+ var encodeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeF32FixedArrayLE : encodeF32FixedArrayBE;
32
+ function encodeF32FixedArrayLE(bc, x, len) {
33
+ assert(x.length === len);
34
+ assert(!x.every(Number.isNaN), NAN_NOT_ALLOWED);
35
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
36
+ }
37
+ function encodeF32FixedArrayBE(bc, val, len) {
38
+ assert(val.length === len);
39
+ bc.reserve(val.length * 4);
40
+ for (let i = 0; i < val.length; i++)
41
+ encodeF32(bc, val[i]);
42
+ }
43
+ function decodeF32Array(bc) {
44
+ return decodeF32FixedArray(bc, decodeUintSafe(bc));
45
+ }
46
+ function encodeF32Array(bc, x) {
47
+ encodeUintSafe(bc, x.length);
48
+ if (x.length !== 0) {
49
+ encodeF32FixedArray(bc, x, x.length);
50
+ }
51
+ }
52
+ var decodeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeF64FixedArrayLE : decodeF64FixedArrayBE;
53
+ function decodeF64FixedArrayLE(bc, len) {
54
+ const byteLen = len * 8;
55
+ const result = new Float64Array(decodeFixedData(bc, byteLen));
56
+ if (result.some(Number.isNaN)) {
57
+ throw new BareError(bc.offset, NAN_NOT_ALLOWED);
58
+ }
59
+ return result;
60
+ }
61
+ function decodeF64FixedArrayBE(bc, len) {
62
+ bc.check(len * 8);
63
+ const result = new Float64Array(len);
64
+ for (let i = 0; i < len; i++)
65
+ result[i] = decodeF64(bc);
66
+ return result;
67
+ }
68
+ var encodeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeF64FixedArrayLE : encodeF64FixedArrayBE;
69
+ function encodeF64FixedArrayLE(bc, x, len) {
70
+ assert(x.length === len);
71
+ assert(!x.every(Number.isNaN), NAN_NOT_ALLOWED);
72
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
73
+ }
74
+ function encodeF64FixedArrayBE(bc, x, len) {
75
+ assert(x.length === len);
76
+ bc.reserve(x.length * 8);
77
+ for (let i = 0; i < x.length; i++)
78
+ encodeF64(bc, x[i]);
79
+ }
80
+ function decodeF64Array(bc) {
81
+ return decodeF64FixedArray(bc, decodeUintSafe(bc));
82
+ }
83
+ function encodeF64Array(bc, x) {
84
+ encodeUintSafe(bc, x.length);
85
+ if (x.length !== 0) {
86
+ encodeF64FixedArray(bc, x, x.length);
87
+ }
88
+ }
89
+ export {
90
+ decodeF32Array,
91
+ decodeF32FixedArray,
92
+ decodeF64Array,
93
+ decodeF64FixedArray,
94
+ encodeF32Array,
95
+ encodeF32FixedArray,
96
+ encodeF64Array,
97
+ encodeF64FixedArray
98
+ };
99
+ //# sourceMappingURL=float-array.js.map
@@ -0,0 +1,50 @@
1
+ // src/codec/i16-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
+ import { decodeFixedData } from "./data.js";
5
+ import {
6
+ decodeI16,
7
+ decodeUintSafe,
8
+ encodeI16,
9
+ encodeUintSafe
10
+ } from "./primitive.js";
11
+ var I16_BYTE_COUNT = 2;
12
+ var decodeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI16FixedArrayLE : decodeI16FixedArrayBE;
13
+ function decodeI16Array(bc) {
14
+ return decodeI16FixedArray(bc, decodeUintSafe(bc));
15
+ }
16
+ function decodeI16FixedArrayLE(bc, len) {
17
+ const byteCount = len * I16_BYTE_COUNT;
18
+ return new Int16Array(decodeFixedData(bc, byteCount));
19
+ }
20
+ function decodeI16FixedArrayBE(bc, len) {
21
+ bc.check(len * I16_BYTE_COUNT);
22
+ const result = new Int16Array(len);
23
+ for (let i = 0; i < len; i++)
24
+ result[i] = decodeI16(bc);
25
+ return result;
26
+ }
27
+ var encodeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeI16FixedArrayLE : encodeI16FixedArrayBE;
28
+ function encodeI16Array(bc, x) {
29
+ encodeUintSafe(bc, x.length);
30
+ if (x.length !== 0) {
31
+ encodeI16FixedArray(bc, x, x.length);
32
+ }
33
+ }
34
+ function encodeI16FixedArrayLE(bc, x, len) {
35
+ assert(x.length === len);
36
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
+ }
38
+ function encodeI16FixedArrayBE(bc, x, len) {
39
+ assert(x.length === len);
40
+ bc.reserve(x.length * I16_BYTE_COUNT);
41
+ for (let i = 0; i < x.length; i++)
42
+ encodeI16(bc, x[i]);
43
+ }
44
+ export {
45
+ decodeI16Array,
46
+ decodeI16FixedArray,
47
+ encodeI16Array,
48
+ encodeI16FixedArray
49
+ };
50
+ //# sourceMappingURL=i16-array.js.map
@@ -0,0 +1,50 @@
1
+ // src/codec/i32-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
+ import { decodeFixedData } from "./data.js";
5
+ import {
6
+ decodeI32,
7
+ decodeUintSafe,
8
+ encodeI32,
9
+ encodeUintSafe
10
+ } from "./primitive.js";
11
+ var I32_BYTE_COUNT = 4;
12
+ var decodeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI32FixedArrayLE : decodeI32FixedArrayBE;
13
+ function decodeI32Array(bc) {
14
+ return decodeI32FixedArray(bc, decodeUintSafe(bc));
15
+ }
16
+ function decodeI32FixedArrayLE(bc, len) {
17
+ const byteCount = len * I32_BYTE_COUNT;
18
+ return new Int32Array(decodeFixedData(bc, byteCount));
19
+ }
20
+ function decodeI32FixedArrayBE(bc, len) {
21
+ bc.check(len * I32_BYTE_COUNT);
22
+ const result = new Int32Array(len);
23
+ for (let i = 0; i < len; i++)
24
+ result[i] = decodeI32(bc);
25
+ return result;
26
+ }
27
+ var encodeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeI32FixedArrayLE : encodeI32FixedArrayBE;
28
+ function encodeI32Array(bc, x) {
29
+ encodeUintSafe(bc, x.length);
30
+ if (x.length !== 0) {
31
+ encodeI32FixedArray(bc, x, x.length);
32
+ }
33
+ }
34
+ function encodeI32FixedArrayLE(bc, x, len) {
35
+ assert(x.length === len);
36
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
+ }
38
+ function encodeI32FixedArrayBE(bc, x, len) {
39
+ assert(x.length === len);
40
+ bc.reserve(x.length * I32_BYTE_COUNT);
41
+ for (let i = 0; i < x.length; i++)
42
+ encodeI32(bc, x[i]);
43
+ }
44
+ export {
45
+ decodeI32Array,
46
+ decodeI32FixedArray,
47
+ encodeI32Array,
48
+ encodeI32FixedArray
49
+ };
50
+ //# sourceMappingURL=i32-array.js.map
@@ -0,0 +1,50 @@
1
+ // src/codec/i64-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
+ import { decodeFixedData } from "./data.js";
5
+ import {
6
+ decodeI64,
7
+ decodeUintSafe,
8
+ encodeI64,
9
+ encodeUintSafe
10
+ } from "./primitive.js";
11
+ var I64_BYTE_COUNT = 8;
12
+ var decodeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI64FixedArrayLE : decodeI64FixedArrayBE;
13
+ function decodeI64Array(bc) {
14
+ return decodeI64FixedArray(bc, decodeUintSafe(bc));
15
+ }
16
+ function decodeI64FixedArrayLE(bc, len) {
17
+ const byteCount = len * I64_BYTE_COUNT;
18
+ return new BigInt64Array(decodeFixedData(bc, byteCount));
19
+ }
20
+ function decodeI64FixedArrayBE(bc, len) {
21
+ bc.check(len * I64_BYTE_COUNT);
22
+ const result = new BigInt64Array(len);
23
+ for (let i = 0; i < len; i++)
24
+ result[i] = decodeI64(bc);
25
+ return result;
26
+ }
27
+ var encodeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeI64FixedArrayLE : encodeI64FixedArrayBE;
28
+ function encodeI64Array(bc, x) {
29
+ encodeUintSafe(bc, x.length);
30
+ if (x.length !== 0) {
31
+ encodeI64FixedArray(bc, x, x.length);
32
+ }
33
+ }
34
+ function encodeI64FixedArrayLE(bc, x, len) {
35
+ assert(x.length === len);
36
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
+ }
38
+ function encodeI64FixedArrayBE(bc, x, len) {
39
+ assert(x.length === len);
40
+ bc.reserve(x.length * I64_BYTE_COUNT);
41
+ for (let i = 0; i < x.length; i++)
42
+ encodeI64(bc, x[i]);
43
+ }
44
+ export {
45
+ decodeI64Array,
46
+ decodeI64FixedArray,
47
+ encodeI64Array,
48
+ encodeI64FixedArray
49
+ };
50
+ //# sourceMappingURL=i64-array.js.map
@@ -0,0 +1,24 @@
1
+ // src/codec/i8-array.ts
2
+ import { decodeFixedData } from "./data.js";
3
+ import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
+ import { encodeU8FixedArray } from "./u8-array.js";
5
+ function decodeI8Array(bc) {
6
+ return decodeI8FixedArray(bc, decodeUintSafe(bc));
7
+ }
8
+ function encodeI8Array(bc, x) {
9
+ encodeUintSafe(bc, x.length);
10
+ encodeI8FixedArray(bc, x, x.length);
11
+ }
12
+ function decodeI8FixedArray(bc, len) {
13
+ return new Int8Array(decodeFixedData(bc, len));
14
+ }
15
+ function encodeI8FixedArray(bc, x, len) {
16
+ encodeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength), len);
17
+ }
18
+ export {
19
+ decodeI8Array,
20
+ decodeI8FixedArray,
21
+ encodeI8Array,
22
+ encodeI8FixedArray
23
+ };
24
+ //# sourceMappingURL=i8-array.js.map
@@ -0,0 +1,15 @@
1
+ // src/codec/index.ts
2
+ export * from "./data.js";
3
+ export * from "./float-array.js";
4
+ export * from "./i16-array.js";
5
+ export * from "./i32-array.js";
6
+ export * from "./i64-array.js";
7
+ export * from "./i8-array.js";
8
+ export * from "./primitive.js";
9
+ export * from "./string.js";
10
+ export * from "./u16-array.js";
11
+ export * from "./u32-array.js";
12
+ export * from "./u64-array.js";
13
+ export * from "./u8-array.js";
14
+ export * from "./u8-clamped-array.js";
15
+ //# sourceMappingURL=index.js.map