@nnilky/structo 1.0.5 → 1.0.7
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/datatypes/containers/array.test.js +6 -10
- package/dist/datatypes/containers/exhuastiveArray.test.js +9 -12
- package/dist/datatypes/containers/fastObject.d.ts +2 -0
- package/dist/datatypes/containers/fastObject.js +2 -0
- package/dist/datatypes/containers/fastObject.test.js +18 -6
- package/dist/datatypes/containers/list.test.js +6 -12
- package/dist/datatypes/containers/object.js +7 -1
- package/dist/datatypes/containers/object.test.js +22 -10
- package/dist/datatypes/containers/taggedUnion.d.ts +12 -8
- package/dist/datatypes/containers/taggedUnion.js +4 -2
- package/dist/datatypes/containers/taggedUnion.test.js +22 -18
- package/dist/datatypes/index.d.ts +2 -9
- package/dist/datatypes/index.js +2 -9
- package/dist/datatypes/numbers/bigints.test.js +11 -11
- package/dist/datatypes/numbers/floats.test.js +6 -6
- package/dist/datatypes/numbers/sints.test.js +11 -11
- package/dist/datatypes/numbers/uints.test.js +11 -11
- package/dist/datatypes/values/bytes.test.js +6 -8
- package/dist/datatypes/values/json.d.ts +10 -0
- package/dist/datatypes/values/json.js +21 -0
- package/dist/datatypes/values/sizedbytes.d.ts +2 -0
- package/dist/datatypes/values/sizedbytes.js +17 -0
- package/dist/datatypes/values/sizedbytes.test.d.ts +1 -0
- package/dist/datatypes/values/sizedbytes.test.js +25 -0
- package/dist/datatypes/values/string.d.ts +14 -0
- package/dist/datatypes/values/string.js +36 -0
- package/dist/datatypes/values/string.test.d.ts +1 -0
- package/dist/datatypes/values/string.test.js +18 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/transforms/encode.d.ts +2 -0
- package/dist/transforms/encode.js +7 -0
- package/dist/transforms/enum.d.ts +2 -0
- package/dist/transforms/enum.js +13 -0
- package/dist/transforms/enum.test.d.ts +1 -0
- package/dist/transforms/enum.test.js +21 -0
- package/dist/transforms/fixedOffset.d.ts +2 -0
- package/dist/transforms/fixedOffset.js +18 -0
- package/dist/transforms/index.d.ts +12 -0
- package/dist/transforms/index.js +12 -0
- package/dist/transforms/literal.d.ts +1 -0
- package/dist/transforms/literal.js +12 -0
- package/dist/transforms/literal.test.d.ts +1 -0
- package/dist/transforms/literal.test.js +23 -0
- package/dist/transforms/modify.d.ts +2 -0
- package/dist/transforms/modify.js +7 -0
- package/dist/transforms/noAdvance.d.ts +16 -0
- package/dist/transforms/noAdvance.js +30 -0
- package/dist/transforms/noAdvance.test.d.ts +1 -0
- package/dist/transforms/noAdvance.test.js +17 -0
- package/dist/transforms/pipe.d.ts +11 -0
- package/dist/transforms/pipe.js +16 -0
- package/dist/transforms/toAscii.d.ts +1 -0
- package/dist/transforms/toAscii.js +11 -0
- package/dist/transforms/toAscii.test.d.ts +1 -0
- package/dist/transforms/toAscii.test.js +25 -0
- package/dist/transforms/toBase64.d.ts +1 -0
- package/dist/transforms/toBase64.js +4 -0
- package/dist/transforms/toBase64.test.d.ts +1 -0
- package/dist/transforms/toBase64.test.js +27 -0
- package/dist/transforms/toBytes.d.ts +13 -0
- package/dist/transforms/toBytes.js +21 -0
- package/dist/transforms/toBytes.test.d.ts +1 -0
- package/dist/transforms/toBytes.test.js +27 -0
- package/dist/transforms/toHex.d.ts +13 -0
- package/dist/transforms/toHex.js +16 -0
- package/dist/transforms/toHex.test.d.ts +1 -0
- package/dist/transforms/toHex.test.js +23 -0
- package/dist/transforms/toTypedArray.d.ts +16 -0
- package/dist/transforms/toTypedArray.js +14 -0
- package/dist/transforms/toTypedArray.test.d.ts +1 -0
- package/dist/transforms/toTypedArray.test.js +29 -0
- package/dist/utilities/index.d.ts +2 -0
- package/dist/utilities/index.js +2 -0
- package/dist/utilities/lazy.d.ts +2 -0
- package/dist/utilities/lazy.js +25 -0
- package/dist/utilities/lazy.test.d.ts +1 -0
- package/dist/utilities/lazy.test.js +36 -0
- package/dist/utilities/remember.d.ts +17 -0
- package/dist/utilities/remember.js +38 -0
- package/dist/utilities/remember.test.d.ts +1 -0
- package/dist/utilities/remember.test.js +76 -0
- package/dist/utils.test.d.ts +9 -1
- package/dist/utils.test.js +42 -1
- package/package.json +12 -7
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import { encodeTest, encodeFailTest } from "../../utils.test";
|
|
3
3
|
import * as st from "../../index";
|
|
4
4
|
describe("st.array", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
|
-
|
|
7
|
-
expectEncode(spec, [1, 2, 3, 4]);
|
|
6
|
+
encodeTest(st.array(4, st.u16()), [1, 2, 3, 4]);
|
|
8
7
|
});
|
|
9
8
|
it("works on empty lists", () => {
|
|
10
|
-
|
|
11
|
-
expectEncode(spec, []);
|
|
9
|
+
encodeTest(st.array(0, st.u16()), []);
|
|
12
10
|
});
|
|
13
11
|
it("throws error on invalid size", () => {
|
|
14
|
-
|
|
15
|
-
expectError(() => st.write(spec, [1]));
|
|
12
|
+
encodeFailTest(st.array(2, st.u16()), [1]);
|
|
16
13
|
});
|
|
17
14
|
it("throws error on invalid value", () => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
expectError(() => st.write(spec, [2 ** 16 + 1, 0]));
|
|
15
|
+
encodeFailTest(st.array(2, st.u16()), [1, -1]);
|
|
16
|
+
encodeFailTest(st.array(2, st.u16()), [2 ** 16 + 1, 0]);
|
|
21
17
|
});
|
|
22
18
|
});
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import { encodeTest, encodeSnapshotTest, encodeFailTest } from "../../utils.test";
|
|
3
3
|
import * as st from "../../index";
|
|
4
4
|
describe("st.exhuastiveArray", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
6
|
const spec = st.exhuastiveArray(st.u32());
|
|
7
|
-
|
|
7
|
+
encodeTest(spec, [1, 2, 3, 4]);
|
|
8
8
|
});
|
|
9
9
|
it("works on empty arrays", () => {
|
|
10
10
|
const spec = st.exhuastiveArray(st.u32());
|
|
11
|
-
|
|
11
|
+
encodeTest(spec, []);
|
|
12
12
|
});
|
|
13
13
|
it("accepts large array", () => {
|
|
14
14
|
const spec = st.exhuastiveArray(st.u32());
|
|
15
|
-
|
|
15
|
+
encodeTest(spec, Array.from({ length: 10000 }, (_, i) => i));
|
|
16
16
|
});
|
|
17
17
|
it("works composed", () => {
|
|
18
18
|
const spec = st.object({
|
|
@@ -20,22 +20,19 @@ describe("st.exhuastiveArray", () => {
|
|
|
20
20
|
b: st.u8(),
|
|
21
21
|
c: st.exhuastiveArray(st.u32()),
|
|
22
22
|
});
|
|
23
|
-
|
|
23
|
+
encodeTest(spec, { a: 1, b: 2, c: [3, 4, 5, 6, 7] });
|
|
24
24
|
});
|
|
25
25
|
it("throws error on invalid value", () => {
|
|
26
|
-
|
|
27
|
-
expectError(() => {
|
|
28
|
-
st.write(spec, [-1]);
|
|
29
|
-
});
|
|
26
|
+
encodeFailTest(st.exhuastiveArray(st.u16()), [-1]);
|
|
30
27
|
});
|
|
31
28
|
it("nested arrays encode correctly", () => {
|
|
32
|
-
|
|
29
|
+
encodeTest(st.exhuastiveArray(st.exhuastiveArray(st.u16())), //
|
|
33
30
|
[[1, 2]]);
|
|
34
31
|
});
|
|
35
32
|
it(`matches snapshots`, () => {
|
|
36
|
-
|
|
33
|
+
encodeSnapshotTest(st.exhuastiveArray(st.string(st.u16())), //
|
|
37
34
|
["foo", "bar", "baz"]);
|
|
38
|
-
|
|
35
|
+
encodeSnapshotTest(st.exhuastiveArray(st.u8()), //
|
|
39
36
|
[0, 1, 2, 3]);
|
|
40
37
|
});
|
|
41
38
|
});
|
|
@@ -14,6 +14,8 @@ type InferObject<T> = T extends Record<string, Serializer<any>> ? {
|
|
|
14
14
|
* createdAt: st.f64(),
|
|
15
15
|
* })
|
|
16
16
|
* ```
|
|
17
|
+
*
|
|
18
|
+
* Note: Using st.lazy affects the performance of fastObject as it prevents discovery of subobjects, avoid when performance is a requirement
|
|
17
19
|
*/
|
|
18
20
|
export declare function fastObject<T extends Record<string, Serializer<any>>>(definition: T): Serializer<InferObject<T>>;
|
|
19
21
|
export {};
|
|
@@ -11,6 +11,8 @@ const definitionSymbol = Symbol();
|
|
|
11
11
|
* createdAt: st.f64(),
|
|
12
12
|
* })
|
|
13
13
|
* ```
|
|
14
|
+
*
|
|
15
|
+
* Note: Using st.lazy affects the performance of fastObject as it prevents discovery of subobjects, avoid when performance is a requirement
|
|
14
16
|
*/
|
|
15
17
|
export function fastObject(definition) {
|
|
16
18
|
let serializers = [];
|
|
@@ -1,26 +1,38 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import { bytes,
|
|
3
|
+
import { bytes, encodeTest, encodeSnapshotTest } from "../../utils.test";
|
|
4
4
|
import * as st from "../../index";
|
|
5
5
|
describe("st.fastObject", () => {
|
|
6
|
-
const
|
|
6
|
+
const spec = st.fastObject({
|
|
7
7
|
a: st.u8(),
|
|
8
8
|
b: st.u8(),
|
|
9
9
|
});
|
|
10
10
|
it("encodes correctly", () => {
|
|
11
|
-
st.write(
|
|
11
|
+
st.write(spec, { b: 2, a: 1 });
|
|
12
|
+
});
|
|
13
|
+
it("encodes nest correctly", () => {
|
|
14
|
+
st.write(st.fastObject({
|
|
15
|
+
a: st.fastObject({
|
|
16
|
+
cat: st.u8(),
|
|
17
|
+
puppy: st.string(st.u8()),
|
|
18
|
+
}),
|
|
19
|
+
b: st.u8(),
|
|
20
|
+
}), {
|
|
21
|
+
a: { cat: 1, puppy: "woof" },
|
|
22
|
+
b: 3,
|
|
23
|
+
});
|
|
12
24
|
});
|
|
13
25
|
it("encode in correct order", () => {
|
|
14
|
-
const data = st.write(
|
|
26
|
+
const data = st.write(spec, { b: 2, a: 1 });
|
|
15
27
|
const arr = new Uint8Array(data);
|
|
16
28
|
expect(arr[0]).toBe(1);
|
|
17
29
|
expect(arr[1]).toBe(2);
|
|
18
30
|
});
|
|
19
31
|
it("encodes empty", () => {
|
|
20
|
-
|
|
32
|
+
encodeTest(st.fastObject({}), {});
|
|
21
33
|
});
|
|
22
34
|
it(`matches snapshots`, () => {
|
|
23
|
-
|
|
35
|
+
encodeSnapshotTest(st.fastObject({
|
|
24
36
|
number: st.u32(),
|
|
25
37
|
puppy: st.string(st.s32()),
|
|
26
38
|
buffer: st.bytes(4),
|
|
@@ -1,29 +1,23 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import { encodeTest, encodeSnapshotTest, encodeFailTest } from "../../utils.test";
|
|
3
3
|
import * as st from "../../index";
|
|
4
4
|
describe("st.list", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
6
|
const spec = st.list(st.u8(), st.u32());
|
|
7
|
-
|
|
7
|
+
encodeTest(spec, [1, 2, 3, 4]);
|
|
8
8
|
});
|
|
9
9
|
it("works on empty lists", () => {
|
|
10
10
|
const spec = st.list(st.u8(), st.u32());
|
|
11
|
-
|
|
11
|
+
encodeTest(spec, []);
|
|
12
12
|
});
|
|
13
13
|
it("throws error on too large array", () => {
|
|
14
|
-
|
|
15
|
-
expectError(() => {
|
|
16
|
-
st.write(spec, Array.from({ length: 10000 }, () => 0));
|
|
17
|
-
});
|
|
14
|
+
encodeFailTest(st.list(st.u8(), st.u32()), Array.from({ length: 10000 }, () => 0));
|
|
18
15
|
});
|
|
19
16
|
it("throws error on invalid value", () => {
|
|
20
|
-
|
|
21
|
-
expectError(() => {
|
|
22
|
-
st.write(spec, [-1]);
|
|
23
|
-
});
|
|
17
|
+
encodeFailTest(st.list(st.u8(), st.u16()), [-1]);
|
|
24
18
|
});
|
|
25
19
|
it(`matches snapshots`, () => {
|
|
26
|
-
|
|
20
|
+
encodeSnapshotTest(st.list(st.s32(), st.string(st.u16())), //
|
|
27
21
|
["foo", "bar", "baz"]);
|
|
28
22
|
});
|
|
29
23
|
});
|
|
@@ -21,7 +21,13 @@ export function object(definition) {
|
|
|
21
21
|
if (size)
|
|
22
22
|
ctx.alloc(size);
|
|
23
23
|
for (let i = 0; i < entires.length; i++) {
|
|
24
|
-
|
|
24
|
+
const key = entires[i][0];
|
|
25
|
+
try {
|
|
26
|
+
entires[i][1].write(ctx, value[key]);
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
throw new Error(`Failed to encode key '${key}'`, { cause: e });
|
|
30
|
+
}
|
|
25
31
|
}
|
|
26
32
|
},
|
|
27
33
|
read: (ctx) => {
|
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
|
-
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import { bytes,
|
|
2
|
+
import { describe, it, expect, expectTypeOf } from "bun:test";
|
|
3
|
+
import { bytes, encodeTest, encodeSnapshotTest } from "../../utils.test";
|
|
4
4
|
import * as st from "../../index";
|
|
5
5
|
describe("st.object", () => {
|
|
6
|
-
const test = st.object({
|
|
7
|
-
a: st.u8(),
|
|
8
|
-
b: st.u8(),
|
|
9
|
-
});
|
|
10
6
|
it("encodes correctly", () => {
|
|
11
|
-
st.
|
|
7
|
+
encodeTest(st.object({ a: st.u8(), b: st.u8() }), //
|
|
8
|
+
{ b: 2, a: 1 });
|
|
9
|
+
});
|
|
10
|
+
it("encodes nested correctly", () => {
|
|
11
|
+
encodeTest(st.object({
|
|
12
|
+
a: st.object({ a: st.u8() }),
|
|
13
|
+
b: st.u8(),
|
|
14
|
+
}), //
|
|
15
|
+
{ a: { a: 2 }, b: 1 });
|
|
12
16
|
});
|
|
13
17
|
it("encode in correct order", () => {
|
|
14
|
-
const data = st.write(
|
|
18
|
+
const data = st.write(st.object({
|
|
19
|
+
a: st.u8(),
|
|
20
|
+
b: st.u8(),
|
|
21
|
+
}), { b: 2, a: 1 });
|
|
15
22
|
const arr = new Uint8Array(data);
|
|
16
23
|
expect(arr[0]).toBe(1);
|
|
17
24
|
expect(arr[1]).toBe(2);
|
|
18
25
|
});
|
|
19
26
|
it("encodes empty", () => {
|
|
20
|
-
|
|
27
|
+
encodeTest(st.object({}), {});
|
|
21
28
|
});
|
|
22
29
|
it(`matches snapshots`, () => {
|
|
23
|
-
|
|
30
|
+
encodeSnapshotTest(st.object({
|
|
24
31
|
number: st.u32(),
|
|
25
32
|
puppy: st.string(st.s32()),
|
|
26
33
|
buffer: st.bytes(4),
|
|
@@ -30,4 +37,9 @@ describe("st.object", () => {
|
|
|
30
37
|
buffer: bytes([19, 87, 19, 83]),
|
|
31
38
|
});
|
|
32
39
|
});
|
|
40
|
+
// Type Tests
|
|
41
|
+
expectTypeOf(st.object({
|
|
42
|
+
number: st.u32(),
|
|
43
|
+
string: st.string(st.u32()),
|
|
44
|
+
})).toEqualTypeOf();
|
|
33
45
|
});
|
|
@@ -11,12 +11,16 @@ import type { InferInput, InferOutput, Serializer } from "../../types";
|
|
|
11
11
|
* 2: MouseMovePacket,
|
|
12
12
|
* })
|
|
13
13
|
* ```
|
|
14
|
-
*
|
|
15
14
|
*/
|
|
16
|
-
export declare function taggedUnion<
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
15
|
+
export declare function taggedUnion<Tag extends PropertyKey, //
|
|
16
|
+
Variants extends Record<Tag, Serializer<any>>>(tag: Serializer<Tag>, variants: Variants & Record<Exclude<keyof Variants, Tag>, never>): Serializer<{
|
|
17
|
+
[K in keyof Variants]: {
|
|
18
|
+
type: K;
|
|
19
|
+
value: InferInput<Variants[K]>;
|
|
20
|
+
};
|
|
21
|
+
}[keyof Variants], {
|
|
22
|
+
[K in keyof Variants]: {
|
|
23
|
+
type: K;
|
|
24
|
+
value: InferOutput<Variants[K]>;
|
|
25
|
+
};
|
|
26
|
+
}[keyof Variants]>;
|
|
@@ -10,15 +10,16 @@
|
|
|
10
10
|
* 2: MouseMovePacket,
|
|
11
11
|
* })
|
|
12
12
|
* ```
|
|
13
|
-
*
|
|
14
13
|
*/
|
|
15
14
|
export function taggedUnion(tag, variants) {
|
|
16
15
|
return {
|
|
17
16
|
write: (ctx, value) => {
|
|
18
17
|
if (!(value.type in variants))
|
|
19
|
-
throw new Error(`Unknown type ${value.type}`);
|
|
18
|
+
throw new Error(`Unknown type ${String(value.type)}`);
|
|
19
|
+
//@ts-expect-error
|
|
20
20
|
tag.write(ctx, value.type);
|
|
21
21
|
const variant = variants[value.type];
|
|
22
|
+
//@ts-expect-error
|
|
22
23
|
variant.write(ctx, value.value);
|
|
23
24
|
},
|
|
24
25
|
read: (ctx) => {
|
|
@@ -26,6 +27,7 @@ export function taggedUnion(tag, variants) {
|
|
|
26
27
|
if (!(type in variants))
|
|
27
28
|
throw new Error(`Unknown type ${tag}`);
|
|
28
29
|
const variant = variants[type];
|
|
30
|
+
//@ts-expect-error
|
|
29
31
|
const value = variant.read(ctx);
|
|
30
32
|
return { type, value };
|
|
31
33
|
},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { describe, it } from "bun:test";
|
|
2
|
-
import {
|
|
1
|
+
import { describe, expectTypeOf, it } from "bun:test";
|
|
2
|
+
import { encodeTest, expectEncodeSize, encodeSnapshotTest, encodeFailTest } from "../../utils.test";
|
|
3
3
|
import * as st from "../../index";
|
|
4
4
|
describe("st.taggedUnion", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
@@ -7,57 +7,61 @@ describe("st.taggedUnion", () => {
|
|
|
7
7
|
1: st.string(st.u8()),
|
|
8
8
|
2: st.u8(),
|
|
9
9
|
});
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
encodeTest(spec, { type: 1, value: "s" });
|
|
11
|
+
encodeTest(spec, { type: 2, value: 52 });
|
|
12
12
|
});
|
|
13
13
|
it("encode with string tag", () => {
|
|
14
14
|
const spec = st.taggedUnion(st.string(st.u8()), {
|
|
15
15
|
foo: st.string(st.u8()),
|
|
16
16
|
bar: st.u8(),
|
|
17
17
|
});
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
encodeTest(spec, { type: "foo", value: "woof" });
|
|
19
|
+
encodeTest(spec, { type: "bar", value: 52 });
|
|
20
20
|
});
|
|
21
21
|
it("throws error on invalid tag", () => {
|
|
22
22
|
const spec = st.taggedUnion(st.string(st.u8()), {
|
|
23
23
|
foo: st.string(st.u8()),
|
|
24
24
|
bar: st.u8(),
|
|
25
25
|
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
26
|
+
//@ts-expect-error, intended
|
|
27
|
+
encodeFailTest(spec, { type: "unknown", value: "woof" });
|
|
29
28
|
});
|
|
30
29
|
it("throws error on invalid type", () => {
|
|
31
30
|
const spec = st.taggedUnion(st.string(st.u8()), {
|
|
32
31
|
foo: st.string(st.u8()),
|
|
33
32
|
bar: st.u8(),
|
|
34
33
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
});
|
|
34
|
+
//@ts-expect-error, intended
|
|
35
|
+
encodeFailTest(spec, { type: "unknown", value: "woof" });
|
|
38
36
|
});
|
|
39
37
|
it("throws error on invalid value", () => {
|
|
40
38
|
const spec = st.taggedUnion(st.string(st.u8()), {
|
|
41
39
|
foo: st.string(st.u8()),
|
|
42
40
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
st.write(spec, { type: "foo", value: 0 });
|
|
46
|
-
});
|
|
41
|
+
//@ts-expect-error, intended
|
|
42
|
+
encodeFailTest(spec, { type: "foo", value: 0 });
|
|
47
43
|
});
|
|
48
44
|
it("encodes varying sizes", () => {
|
|
49
45
|
const spec = st.taggedUnion(st.u8(), {
|
|
50
46
|
1: st.string(st.u8()),
|
|
51
47
|
2: st.u8(),
|
|
52
48
|
});
|
|
53
|
-
|
|
49
|
+
encodeTest(spec, { type: 1, value: "s" });
|
|
54
50
|
expectEncodeSize(spec, 5, { type: 1, value: "foo" });
|
|
55
51
|
expectEncodeSize(spec, 2, { type: 2, value: 52 });
|
|
56
52
|
});
|
|
57
53
|
it("encodes snapshots", () => {
|
|
58
|
-
|
|
54
|
+
encodeSnapshotTest(st.taggedUnion(st.u32(), {
|
|
59
55
|
0: st.list(st.u8(), st.f64()),
|
|
60
56
|
3: st.u32(),
|
|
61
57
|
}), { type: 0, value: [1] });
|
|
62
58
|
});
|
|
59
|
+
expectTypeOf(st.taggedUnion(st.u32(), {
|
|
60
|
+
0: st.u8(),
|
|
61
|
+
1: st.string(st.u32()),
|
|
62
|
+
})).toEqualTypeOf();
|
|
63
|
+
() => {
|
|
64
|
+
//@ts-expect-error, must be same type as tags
|
|
65
|
+
st.taggedUnion(st.string(st.u8()), { 0: st.u8(), 1: st.string(st.u32()) });
|
|
66
|
+
};
|
|
63
67
|
});
|
|
@@ -6,15 +6,8 @@ export { array } from "./containers/array";
|
|
|
6
6
|
export { fastObject } from "./containers/fastObject";
|
|
7
7
|
export { object } from "./containers/object";
|
|
8
8
|
export { list } from "./containers/list";
|
|
9
|
-
export { sizedBytes } from "./containers/sizedbytes";
|
|
10
9
|
export { taggedUnion } from "./containers/taggedUnion";
|
|
11
10
|
export { exhuastiveArray } from "./containers/exhuastiveArray";
|
|
12
|
-
export { string } from "./containers/string";
|
|
13
11
|
export { bytes } from "./values/bytes";
|
|
14
|
-
export {
|
|
15
|
-
export {
|
|
16
|
-
export { fixedOffset } from "./transforms/fixedOffset";
|
|
17
|
-
export { modify } from "./transforms/modify";
|
|
18
|
-
export { encode } from "./transforms/encode";
|
|
19
|
-
export { noAdvance } from "./transforms/noAdvance";
|
|
20
|
-
export { createRememberedValue } from "./utilities/remember";
|
|
12
|
+
export { sizedBytes } from "./values/sizedbytes";
|
|
13
|
+
export { string } from "./values/string";
|
package/dist/datatypes/index.js
CHANGED
|
@@ -6,15 +6,8 @@ export { array } from "./containers/array";
|
|
|
6
6
|
export { fastObject } from "./containers/fastObject";
|
|
7
7
|
export { object } from "./containers/object";
|
|
8
8
|
export { list } from "./containers/list";
|
|
9
|
-
export { sizedBytes } from "./containers/sizedbytes";
|
|
10
9
|
export { taggedUnion } from "./containers/taggedUnion";
|
|
11
10
|
export { exhuastiveArray } from "./containers/exhuastiveArray";
|
|
12
|
-
export { string } from "./containers/string";
|
|
13
11
|
export { bytes } from "./values/bytes";
|
|
14
|
-
export {
|
|
15
|
-
export {
|
|
16
|
-
export { fixedOffset } from "./transforms/fixedOffset";
|
|
17
|
-
export { modify } from "./transforms/modify";
|
|
18
|
-
export { encode } from "./transforms/encode";
|
|
19
|
-
export { noAdvance } from "./transforms/noAdvance";
|
|
20
|
-
export { createRememberedValue } from "./utilities/remember";
|
|
12
|
+
export { sizedBytes } from "./values/sizedbytes";
|
|
13
|
+
export { string } from "./values/string";
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import { encodeTest, encodeSnapshotTest, randbigint, encodeFailTest } from "../../utils.test";
|
|
3
3
|
import * as st from "../../index";
|
|
4
4
|
function test_bigint(options) {
|
|
5
5
|
const { name, range: [start, end], serializer, size, } = options;
|
|
6
6
|
describe(name, () => {
|
|
7
7
|
it("works in bounds", () => {
|
|
8
|
-
|
|
8
|
+
encodeTest(serializer, start);
|
|
9
9
|
for (let i = 0; i < 100; i++) {
|
|
10
|
-
|
|
10
|
+
encodeTest(serializer, randbigint(start, end));
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
encodeTest(serializer, end);
|
|
13
13
|
});
|
|
14
14
|
it("outside bounds", () => {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
encodeFailTest(serializer, start - 1n);
|
|
16
|
+
encodeFailTest(serializer, end + 1n);
|
|
17
17
|
});
|
|
18
18
|
it(`is right size`, () => {
|
|
19
19
|
const expectValueSize = (value) => {
|
|
@@ -27,11 +27,11 @@ function test_bigint(options) {
|
|
|
27
27
|
expectValueSize(end);
|
|
28
28
|
});
|
|
29
29
|
it(`matches snapshots`, () => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
encodeSnapshotTest(serializer, 0n);
|
|
31
|
+
encodeSnapshotTest(serializer, start);
|
|
32
|
+
encodeSnapshotTest(serializer, start + 10n);
|
|
33
|
+
encodeSnapshotTest(serializer, end - 10n);
|
|
34
|
+
encodeSnapshotTest(serializer, end);
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import {
|
|
3
|
+
import { encodeTest, expectEncodeSize } from "../../utils.test";
|
|
4
4
|
import * as st from "../../index";
|
|
5
5
|
function test_float(options) {
|
|
6
6
|
const { name, serializer, size } = options;
|
|
@@ -22,17 +22,17 @@ function test_float(options) {
|
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
24
|
it(`works on Infinty`, () => {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
encodeTest(serializer, Infinity);
|
|
26
|
+
encodeTest(serializer, -Infinity);
|
|
27
27
|
});
|
|
28
28
|
it(`works on NaN`, () => {
|
|
29
|
-
|
|
29
|
+
encodeTest(serializer, NaN);
|
|
30
30
|
});
|
|
31
31
|
it(`works on 0`, () => {
|
|
32
|
-
|
|
32
|
+
encodeTest(serializer, 0);
|
|
33
33
|
});
|
|
34
34
|
it(`works on -0`, () => {
|
|
35
|
-
|
|
35
|
+
encodeTest(serializer, -0);
|
|
36
36
|
});
|
|
37
37
|
it(`is right size`, () => {
|
|
38
38
|
for (let i = 0; i < 100; i++) {
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import {
|
|
3
|
+
import { encodeTest, encodeSnapshotTest, expectError, randint, encodeFailTest, } from "../../utils.test";
|
|
4
4
|
import * as st from "../../index";
|
|
5
5
|
function test_sint(options) {
|
|
6
6
|
const { name, range: [start, end], serializer, size, disableRangeCheck, } = options;
|
|
7
7
|
describe(name, () => {
|
|
8
8
|
it(`works in bounds`, () => {
|
|
9
|
-
|
|
9
|
+
encodeTest(serializer, start);
|
|
10
10
|
for (let i = 0; i < 100; i++) {
|
|
11
|
-
|
|
11
|
+
encodeTest(serializer, randint(start, end));
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
encodeTest(serializer, end);
|
|
14
14
|
});
|
|
15
15
|
if (!disableRangeCheck) {
|
|
16
16
|
it(`errors outside bounds`, () => {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
encodeFailTest(serializer, start - 1);
|
|
18
|
+
encodeFailTest(serializer, end + 1);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
it(`errors on decimal`, () => {
|
|
@@ -33,11 +33,11 @@ function test_sint(options) {
|
|
|
33
33
|
expectValueSize(end);
|
|
34
34
|
});
|
|
35
35
|
it(`matches snapshots`, () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
encodeSnapshotTest(serializer, 0);
|
|
37
|
+
encodeSnapshotTest(serializer, start);
|
|
38
|
+
encodeSnapshotTest(serializer, start + 10);
|
|
39
|
+
encodeSnapshotTest(serializer, end - 10);
|
|
40
|
+
encodeSnapshotTest(serializer, end);
|
|
41
41
|
});
|
|
42
42
|
});
|
|
43
43
|
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import {
|
|
3
|
+
import { encodeTest, encodeSnapshotTest, expectError, randint, encodeFailTest, } from "../../utils.test";
|
|
4
4
|
import * as st from "../../index";
|
|
5
5
|
function test_uint(options) {
|
|
6
6
|
const { name, range: [start, end], serializer, size, disableMaxCheck, } = options;
|
|
7
7
|
describe(name, () => {
|
|
8
8
|
it(`works in bounds`, () => {
|
|
9
|
-
|
|
9
|
+
encodeTest(serializer, start);
|
|
10
10
|
for (let i = 0; i < 100; i++) {
|
|
11
|
-
|
|
11
|
+
encodeTest(serializer, randint(start, end));
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
encodeTest(serializer, end);
|
|
14
14
|
});
|
|
15
15
|
it(`errors outside bounds`, () => {
|
|
16
|
-
|
|
16
|
+
encodeFailTest(serializer, start - 1);
|
|
17
17
|
if (!disableMaxCheck) {
|
|
18
|
-
|
|
18
|
+
encodeFailTest(serializer, end + 1);
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
it(`errors on decimal`, () => {
|
|
@@ -33,11 +33,11 @@ function test_uint(options) {
|
|
|
33
33
|
expectValueSize(end);
|
|
34
34
|
});
|
|
35
35
|
it(`matches snapshots`, () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
encodeSnapshotTest(serializer, 0);
|
|
37
|
+
encodeSnapshotTest(serializer, start);
|
|
38
|
+
encodeSnapshotTest(serializer, start + 10);
|
|
39
|
+
encodeSnapshotTest(serializer, end - 10);
|
|
40
|
+
encodeSnapshotTest(serializer, end);
|
|
41
41
|
});
|
|
42
42
|
});
|
|
43
43
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import { bytes,
|
|
2
|
+
import { bytes, encodeFailTest, encodeTest } from "../../utils.test";
|
|
3
3
|
import * as st from "../../index";
|
|
4
4
|
describe("st.bytes", () => {
|
|
5
5
|
const buffer = st.bytes(2);
|
|
6
6
|
it("encode correctly", () => {
|
|
7
|
-
|
|
7
|
+
encodeTest(buffer, bytes([1, 2]));
|
|
8
8
|
});
|
|
9
9
|
it("encodes empty correctly", () => {
|
|
10
10
|
const spec = st.bytes(0);
|
|
11
|
-
|
|
11
|
+
encodeTest(spec, bytes([]));
|
|
12
12
|
});
|
|
13
13
|
it("holds large data", () => {
|
|
14
14
|
const size = 1024 * 1024 * 8; // 8MB
|
|
@@ -16,7 +16,7 @@ describe("st.bytes", () => {
|
|
|
16
16
|
const data = new Uint8Array(size);
|
|
17
17
|
data.set([3, 4], 1000);
|
|
18
18
|
data.set([3, 4], 2000);
|
|
19
|
-
|
|
19
|
+
encodeTest(spec, data.buffer);
|
|
20
20
|
});
|
|
21
21
|
it("large data writes work", () => {
|
|
22
22
|
const size = 1024 * 1024 * 8; // 8MB
|
|
@@ -28,7 +28,7 @@ describe("st.bytes", () => {
|
|
|
28
28
|
const data = new Uint8Array(size);
|
|
29
29
|
data.set([3, 4], 1000);
|
|
30
30
|
data.set([3, 4], 2000);
|
|
31
|
-
|
|
31
|
+
encodeTest(spec, {
|
|
32
32
|
before: 1,
|
|
33
33
|
data: data.buffer,
|
|
34
34
|
after: 2,
|
|
@@ -36,8 +36,6 @@ describe("st.bytes", () => {
|
|
|
36
36
|
});
|
|
37
37
|
it("errors on invalid length", () => {
|
|
38
38
|
const spec = st.bytes(5);
|
|
39
|
-
|
|
40
|
-
st.write(spec, bytes([1, 2, 3, 4]));
|
|
41
|
-
});
|
|
39
|
+
encodeFailTest(spec, bytes([1, 2, 3, 4]));
|
|
42
40
|
});
|
|
43
41
|
});
|