@nnilky/structo 1.0.1 → 1.0.3
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 +130 -0
- package/dist/datatypes/containers/array.d.ts +2 -0
- package/dist/datatypes/containers/array.js +22 -0
- package/dist/datatypes/containers/array.test.d.ts +1 -0
- package/dist/datatypes/containers/array.test.js +22 -0
- package/dist/datatypes/containers/fastObject.d.ts +19 -0
- package/dist/datatypes/containers/fastObject.js +59 -0
- package/dist/datatypes/containers/list.d.ts +5 -0
- package/dist/datatypes/containers/list.js +22 -0
- package/dist/datatypes/containers/list.test.d.ts +1 -0
- package/dist/datatypes/containers/list.test.js +31 -0
- package/dist/datatypes/containers/object.d.ts +20 -0
- package/dist/datatypes/containers/object.js +35 -0
- package/dist/datatypes/containers/object.test.d.ts +1 -0
- package/dist/datatypes/containers/object.test.js +33 -0
- package/dist/datatypes/containers/sizedbuffer.d.ts +2 -0
- package/dist/datatypes/containers/sizedbuffer.js +17 -0
- package/dist/datatypes/containers/sizedbuffer.test.d.ts +1 -0
- package/dist/datatypes/containers/sizedbuffer.test.js +27 -0
- package/dist/datatypes/index.d.ts +16 -0
- package/dist/datatypes/index.js +16 -0
- package/dist/datatypes/lazy.d.ts +1 -0
- package/dist/datatypes/lazy.js +1 -0
- package/dist/datatypes/numbers/bigints.d.ts +3 -0
- package/dist/datatypes/numbers/bigints.js +37 -0
- package/dist/datatypes/numbers/bigints.test.d.ts +1 -0
- package/dist/datatypes/numbers/bigints.test.js +61 -0
- package/dist/datatypes/numbers/floats.d.ts +3 -0
- package/dist/datatypes/numbers/floats.js +30 -0
- package/dist/datatypes/numbers/floats.test.d.ts +1 -0
- package/dist/datatypes/numbers/floats.test.js +71 -0
- package/dist/datatypes/numbers/sints.d.ts +5 -0
- package/dist/datatypes/numbers/sints.js +72 -0
- package/dist/datatypes/numbers/sints.test.d.ts +1 -0
- package/dist/datatypes/numbers/sints.test.js +87 -0
- package/dist/datatypes/numbers/uints.d.ts +5 -0
- package/dist/datatypes/numbers/uints.js +76 -0
- package/dist/datatypes/numbers/uints.test.d.ts +1 -0
- package/dist/datatypes/numbers/uints.test.js +87 -0
- package/dist/datatypes/transforms/pipe.d.ts +11 -0
- package/dist/datatypes/transforms/pipe.js +4 -0
- package/dist/datatypes/transforms/pipe.test.d.ts +1 -0
- package/dist/datatypes/transforms/pipe.test.js +13 -0
- package/dist/datatypes/transforms/readOffset.d.ts +2 -0
- package/dist/datatypes/transforms/readOffset.js +18 -0
- package/dist/datatypes/transforms/transform.d.ts +2 -0
- package/dist/datatypes/transforms/transform.js +13 -0
- package/dist/datatypes/utilities/constant.d.ts +2 -0
- package/dist/datatypes/utilities/constant.js +13 -0
- package/dist/datatypes/utilities/remember.d.ts +5 -0
- package/dist/datatypes/utilities/remember.js +26 -0
- package/dist/datatypes/utils.test.d.ts +7 -0
- package/dist/datatypes/utils.test.js +33 -0
- package/dist/datatypes/values/buffer.d.ts +2 -0
- package/dist/datatypes/values/buffer.js +18 -0
- package/dist/datatypes/values/buffer.test.d.ts +1 -0
- package/dist/datatypes/values/buffer.test.js +43 -0
- package/dist/datatypes/values/byteliteral.d.ts +2 -0
- package/dist/datatypes/values/byteliteral.js +20 -0
- package/dist/datatypes/values/string.d.ts +2 -0
- package/dist/datatypes/values/string.js +22 -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 +4 -0
- package/dist/index.js +3 -0
- package/dist/read.d.ts +7 -0
- package/dist/read.js +10 -0
- package/dist/types.d.ts +18 -0
- package/dist/types.js +1 -0
- package/dist/utils.test.d.ts +1 -0
- package/dist/utils.test.js +1 -0
- package/dist/write.d.ts +3 -0
- package/dist/write.js +43 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1 +1,131 @@
|
|
|
1
1
|
# Structo
|
|
2
|
+
|
|
3
|
+
Define binary objects in zod style schemas
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import * as st from "@nnilky/structo";
|
|
7
|
+
|
|
8
|
+
type Vec3 = st.InferOutput<typeof Vec3>
|
|
9
|
+
const Vec3 = st.object({
|
|
10
|
+
x: st.f64(),
|
|
11
|
+
y: st.f64(),
|
|
12
|
+
z: st.f64(),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
type Entity = st.InferOutput<typeof Entity>
|
|
16
|
+
const Entity = st.object({
|
|
17
|
+
id: st.u64(),
|
|
18
|
+
position: Vec3,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- Lightweight, base size is <1KB and each datatype is a few hundred bytes
|
|
24
|
+
- Fast, from [benchmarks](./benchmark) only 2-
|
|
25
|
+
- Supports Web/Node.js compatible
|
|
26
|
+
- Easily implemented
|
|
27
|
+
|
|
28
|
+
Each serializer is completely seperate from the base library, meaning you only pay for what you use.
|
|
29
|
+
|
|
30
|
+
## Implementing your own Serializer
|
|
31
|
+
|
|
32
|
+
Implementing your own serializer is incredibly simple, heres the `f64` serializer for example
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
export function f64(endian: "little" | "big" = "little"): st.Serializer<number> {
|
|
36
|
+
return {
|
|
37
|
+
size: 8,
|
|
38
|
+
write: (ctx, value) => {
|
|
39
|
+
ctx.alloc(8);
|
|
40
|
+
ctx.view.setFloat64(ctx.offset, value, endian === "little");
|
|
41
|
+
ctx.offset += 8;
|
|
42
|
+
},
|
|
43
|
+
read: (ctx) => {
|
|
44
|
+
const value = ctx.view.getFloat64(ctx.offset, endian === "little");
|
|
45
|
+
ctx.offset += 8;
|
|
46
|
+
return value;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The `alloc()` in the write function is a utility provided to you to ensure you have enough space ahead of you in the buffer to write to.
|
|
53
|
+
|
|
54
|
+
When reading and writing, you must increment the `ctx.offset` so the next value can be read from. However,
|
|
55
|
+
|
|
56
|
+
The `size` attribute is optional, but if included can be used to do allocations in advance.
|
|
57
|
+
|
|
58
|
+
> Important: You must modify the ctx object, do not spread over it since this will break the reference
|
|
59
|
+
|
|
60
|
+
### Container Example
|
|
61
|
+
|
|
62
|
+
Here is the list serializer
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
export function list<T>(options: {
|
|
66
|
+
type: st.Serializer<T>;
|
|
67
|
+
length: st.Serializer<number>;
|
|
68
|
+
}): st.Serializer<T[]> {
|
|
69
|
+
const lengthType = options.length;
|
|
70
|
+
const valueType = options.type;
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
write: (ctx, value) => {
|
|
74
|
+
lengthType.write(ctx, value.length);
|
|
75
|
+
for (const v of value) {
|
|
76
|
+
lengthType.write(ctx, v);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
read: (ctx) => {
|
|
80
|
+
const size = lengthType.read(ctx);
|
|
81
|
+
|
|
82
|
+
const arr = []
|
|
83
|
+
for (let i = 0; i < size; i++) {
|
|
84
|
+
arr.push(valueType.read(ctx))
|
|
85
|
+
}
|
|
86
|
+
return arr;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Transforms
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const Bits = st.pipe(
|
|
96
|
+
st.u32()
|
|
97
|
+
st.transform(v => v * 8)
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Transforms are utility that let you modify a read/written value. They allow you to processing declaratively.
|
|
102
|
+
|
|
103
|
+
### Writing your Own
|
|
104
|
+
|
|
105
|
+
Here is the `transform` transformation function, a `transform` just a function that takes a type and returns a serializer.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
export function transform<T>(callback: (value: T) => T) {
|
|
109
|
+
return (type: st.Serializer<T>): st.Serializer<T> => ({
|
|
110
|
+
size: type.size,
|
|
111
|
+
read: (ctx) => {
|
|
112
|
+
const value = type.read(ctx);
|
|
113
|
+
return callback(value);
|
|
114
|
+
},
|
|
115
|
+
write: (ctx, value) => {
|
|
116
|
+
let outValue = callback(value);
|
|
117
|
+
type.write(ctx, outValue);
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## FaQ
|
|
124
|
+
|
|
125
|
+
- Why do I have to do `import * as st from "@nnilky/structo"`?
|
|
126
|
+
- By using an `* as` import, bundles can erase the names as runtime without having to worry about runtime effects
|
|
127
|
+
- additionally, by putting it under a namespace it reduces con
|
|
128
|
+
- Will data streaming be implemented?
|
|
129
|
+
- Sadly this library isn't built for data streaming, the exposed API for serializers need the full array to be accessible
|
|
130
|
+
- If you want data streaming, your going to have to fork this and re-implement the standard types
|
|
131
|
+
- What's the difference`
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function array(size, type) {
|
|
2
|
+
const { read: readType, write: writeType, size: typeSize } = type;
|
|
3
|
+
return {
|
|
4
|
+
size: type.size ? size * type.size : undefined,
|
|
5
|
+
write: (ctx, value) => {
|
|
6
|
+
if (value.length !== size)
|
|
7
|
+
throw new Error("Invalid Size");
|
|
8
|
+
if (typeSize)
|
|
9
|
+
ctx.alloc(size * typeSize);
|
|
10
|
+
for (let i = 0; i < size; i++) {
|
|
11
|
+
writeType(ctx, value[i]);
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
read: (ctx) => {
|
|
15
|
+
const arr = new Array(size);
|
|
16
|
+
for (let i = 0; i < size; i++) {
|
|
17
|
+
arr[i] = readType(ctx);
|
|
18
|
+
}
|
|
19
|
+
return arr;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, it } from "bun:test";
|
|
2
|
+
import { expectEncode, expectError } from "../utils.test";
|
|
3
|
+
import * as st from "../../index";
|
|
4
|
+
describe("st.array", () => {
|
|
5
|
+
it("encode correctly", () => {
|
|
6
|
+
const spec = st.array(4, st.u16());
|
|
7
|
+
expectEncode(spec, [1, 2, 3, 4]);
|
|
8
|
+
});
|
|
9
|
+
it("works on empty lists", () => {
|
|
10
|
+
const spec = st.array(0, st.u16());
|
|
11
|
+
expectEncode(spec, []);
|
|
12
|
+
});
|
|
13
|
+
it("throws error on invalid size", () => {
|
|
14
|
+
const spec = st.array(2, st.u16());
|
|
15
|
+
expectError(() => st.write(spec, [1]));
|
|
16
|
+
});
|
|
17
|
+
it("throws error on invalid value", () => {
|
|
18
|
+
const spec = st.array(2, st.u16());
|
|
19
|
+
expectError(() => st.write(spec, [1, -1]));
|
|
20
|
+
expectError(() => st.write(spec, [2 ** 16 + 1, 0]));
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { InferInput, Serializer } from "../../types";
|
|
2
|
+
type InferObject<T> = T extends Record<string, Serializer<any>> ? {
|
|
3
|
+
[Key in keyof T]: InferInput<T[Key]>;
|
|
4
|
+
} : never;
|
|
5
|
+
/**
|
|
6
|
+
* `fastObject` is equivelent to object but it uses eval to improve performance
|
|
7
|
+
*
|
|
8
|
+
* This means that is should be avoided in scenarios where CSP is required (so can't be the default), but should be fine for all other cases
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* st.fastObject({
|
|
12
|
+
* name: st.string(st.u32()),
|
|
13
|
+
* age: st.u8(),
|
|
14
|
+
* createdAt: st.f64(),
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function fastObject<T extends Record<string, Serializer<any>>>(definition: T): Serializer<InferObject<T>>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const definitionSymbol = Symbol();
|
|
2
|
+
/**
|
|
3
|
+
* `fastObject` is equivelent to object but it uses eval to improve performance
|
|
4
|
+
*
|
|
5
|
+
* This means that is should be avoided in scenarios where CSP is required (so can't be the default), but should be fine for all other cases
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* st.fastObject({
|
|
9
|
+
* name: st.string(st.u32()),
|
|
10
|
+
* age: st.u8(),
|
|
11
|
+
* createdAt: st.f64(),
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function fastObject(definition) {
|
|
16
|
+
let serializers = [];
|
|
17
|
+
let writeBody = "";
|
|
18
|
+
let readBody = "";
|
|
19
|
+
function generateSerializers(serializer, keys) {
|
|
20
|
+
if (definitionSymbol in serializer) {
|
|
21
|
+
const definition = serializer[definitionSymbol];
|
|
22
|
+
readBody += `${keys[keys.length - 1]}: {`;
|
|
23
|
+
for (const key of Object.keys(definition)) {
|
|
24
|
+
generateSerializers(definition[key], [...keys, key]);
|
|
25
|
+
}
|
|
26
|
+
readBody += `},`;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
let path = "";
|
|
30
|
+
for (const key of keys) {
|
|
31
|
+
if (key.match(/[a-zA-Z_][a-zA-Z0-9_]*/)) {
|
|
32
|
+
path += `.${key}`;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
path += `[${JSON.stringify(key)}]`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
let name = `s${Object.keys(serializers).length}`;
|
|
39
|
+
serializers.push([name, serializer]);
|
|
40
|
+
writeBody += `${name}(c, v${path});`;
|
|
41
|
+
readBody += `${keys[keys.length - 1]}: ${name}(c),`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const key of Object.keys(definition)) {
|
|
45
|
+
generateSerializers(definition[key], [key]);
|
|
46
|
+
}
|
|
47
|
+
const writeFactory = new Function(...serializers.map((v) => v[0]), `return (c, v) => {${writeBody}}`);
|
|
48
|
+
const readFactory = new Function(...serializers.map((v) => v[0]), `return (c, v) => ({${readBody}})`);
|
|
49
|
+
const write = writeFactory(...serializers.map((v) => v[1].write));
|
|
50
|
+
const read = readFactory(...serializers.map((v) => v[1].read));
|
|
51
|
+
let size = Object.values(definition).reduce((total, v) => total + v.size, 0);
|
|
52
|
+
return {
|
|
53
|
+
//@ts-ignore
|
|
54
|
+
[definitionSymbol]: definition,
|
|
55
|
+
size: isNaN(size) ? undefined : size,
|
|
56
|
+
write,
|
|
57
|
+
read,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function list(options) {
|
|
2
|
+
const { read: readLength, write: writeLength } = options.length;
|
|
3
|
+
const { read: readType, write: writeType, size: sizeType } = options.type;
|
|
4
|
+
return {
|
|
5
|
+
write: (ctx, value) => {
|
|
6
|
+
writeLength(ctx, value.length);
|
|
7
|
+
if (sizeType)
|
|
8
|
+
ctx.alloc(value.length * sizeType);
|
|
9
|
+
for (const v of value) {
|
|
10
|
+
writeType(ctx, v);
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
read: (ctx) => {
|
|
14
|
+
const size = readLength(ctx);
|
|
15
|
+
const arr = new Array(size);
|
|
16
|
+
for (let i = 0; i < size; i++) {
|
|
17
|
+
arr[i] = readType(ctx);
|
|
18
|
+
}
|
|
19
|
+
return arr;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it } from "bun:test";
|
|
2
|
+
import { expectEncode, expectEncodeSnapshot, expectError } from "../utils.test";
|
|
3
|
+
import * as st from "../../index";
|
|
4
|
+
describe("st.list", () => {
|
|
5
|
+
it("encode correctly", () => {
|
|
6
|
+
const spec = st.list({ type: st.u16(), length: st.u8() });
|
|
7
|
+
expectEncode(spec, [1, 2, 3, 4]);
|
|
8
|
+
});
|
|
9
|
+
it("works on empty lists", () => {
|
|
10
|
+
const spec = st.list({ type: st.u16(), length: st.u8() });
|
|
11
|
+
expectEncode(spec, []);
|
|
12
|
+
});
|
|
13
|
+
it("throws error on too large array", () => {
|
|
14
|
+
const spec = st.list({ type: st.u16(), length: st.u8() });
|
|
15
|
+
expectError(() => {
|
|
16
|
+
st.write(spec, Array.from({ length: 10000 }, () => 0));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
it("throws error on invalid value", () => {
|
|
20
|
+
const spec = st.list({ type: st.u16(), length: st.u8() });
|
|
21
|
+
expectError(() => {
|
|
22
|
+
st.write(spec, [-1]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
it(`matches snapshots`, () => {
|
|
26
|
+
expectEncodeSnapshot(st.list({
|
|
27
|
+
type: st.string(st.u16()),
|
|
28
|
+
length: st.s32(),
|
|
29
|
+
}), ["foo", "bar", "baz"]);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { InferInput, InferOutput, Serializer } from "../../types";
|
|
2
|
+
type InferObjectInfer<T> = T extends Record<string, Serializer<any>> ? {
|
|
3
|
+
[Key in keyof T]: InferInput<T[Key]>;
|
|
4
|
+
} : never;
|
|
5
|
+
type InferObjectOutput<T> = T extends Record<string, Serializer<any>> ? {
|
|
6
|
+
[Key in keyof T]: InferOutput<T[Key]>;
|
|
7
|
+
} : never;
|
|
8
|
+
/**
|
|
9
|
+
* `object` is equivelent to a C struct, values are stored in the order defined
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* st.object({
|
|
13
|
+
* name: st.string(st.u32()),
|
|
14
|
+
* age: st.u8(),
|
|
15
|
+
* createdAt: st.f64(),
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function object<T extends Record<string, Serializer<any>>>(definition: T): Serializer<InferObjectInfer<T>, InferObjectOutput<T>>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `object` is equivelent to a C struct, values are stored in the order defined
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* st.object({
|
|
6
|
+
* name: st.string(st.u32()),
|
|
7
|
+
* age: st.u8(),
|
|
8
|
+
* createdAt: st.f64(),
|
|
9
|
+
* })
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export function object(definition) {
|
|
13
|
+
const entires = Object.entries(definition);
|
|
14
|
+
// Use the fact: number + undefined = NaN
|
|
15
|
+
// Check for NaN afterwards
|
|
16
|
+
let computedSize = Object.values(definition).reduce((total, v) => total + v.size, 0);
|
|
17
|
+
const size = isNaN(computedSize) ? undefined : computedSize;
|
|
18
|
+
return {
|
|
19
|
+
size,
|
|
20
|
+
write: (ctx, value) => {
|
|
21
|
+
if (size)
|
|
22
|
+
ctx.alloc(size);
|
|
23
|
+
for (let i = 0; i < entires.length; i++) {
|
|
24
|
+
entires[i][1].write(ctx, value[entires[i][0]]);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
read: (ctx) => {
|
|
28
|
+
const output = new Array(entires.length);
|
|
29
|
+
for (let i = 0; i < entires.length; i++) {
|
|
30
|
+
output[i] = [entires[i][0], entires[i][1].read(ctx)];
|
|
31
|
+
}
|
|
32
|
+
return Object.fromEntries(output);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//@ts-ignore TODO
|
|
2
|
+
import { describe, it, expect } from "bun:test";
|
|
3
|
+
import { bytes, expectEncode, expectEncodeSnapshot } from "../utils.test";
|
|
4
|
+
import * as st from "../../index";
|
|
5
|
+
describe("st.object", () => {
|
|
6
|
+
const test = st.object({
|
|
7
|
+
a: st.u8(),
|
|
8
|
+
b: st.u8(),
|
|
9
|
+
});
|
|
10
|
+
it("encodes correctly", () => {
|
|
11
|
+
st.write(test, { b: 2, a: 1 });
|
|
12
|
+
});
|
|
13
|
+
it("encode in correct order", () => {
|
|
14
|
+
const data = st.write(test, { b: 2, a: 1 });
|
|
15
|
+
const arr = new Uint8Array(data);
|
|
16
|
+
expect(arr[0]).toBe(1);
|
|
17
|
+
expect(arr[1]).toBe(2);
|
|
18
|
+
});
|
|
19
|
+
it("encodes empty", () => {
|
|
20
|
+
expectEncode(st.object({}), {});
|
|
21
|
+
});
|
|
22
|
+
it(`matches snapshots`, () => {
|
|
23
|
+
expectEncodeSnapshot(st.object({
|
|
24
|
+
number: st.u32(),
|
|
25
|
+
puppy: st.string(st.s32()),
|
|
26
|
+
buffer: st.buffer(4),
|
|
27
|
+
}), {
|
|
28
|
+
number: 1,
|
|
29
|
+
puppy: "woof woof bark bark",
|
|
30
|
+
buffer: bytes([19, 87, 19, 83]),
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function sizedBuffer(length) {
|
|
2
|
+
return {
|
|
3
|
+
write: (ctx, value) => {
|
|
4
|
+
length.write(ctx, value.byteLength);
|
|
5
|
+
const bytes = new Uint8Array(value);
|
|
6
|
+
ctx.alloc(value.byteLength);
|
|
7
|
+
new Uint8Array(ctx.view.buffer).set(bytes, ctx.offset);
|
|
8
|
+
ctx.offset += value.byteLength;
|
|
9
|
+
},
|
|
10
|
+
read: (ctx) => {
|
|
11
|
+
const size = length.read(ctx);
|
|
12
|
+
const slice = ctx.buffer.slice(ctx.offset, ctx.offset + size);
|
|
13
|
+
ctx.offset += size;
|
|
14
|
+
return slice;
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, it } from "bun:test";
|
|
2
|
+
import { bytes, expectEncode, expectError } from "../utils.test";
|
|
3
|
+
import * as st from "../../index";
|
|
4
|
+
describe("st.buffer", () => {
|
|
5
|
+
it("encode correctly", () => {
|
|
6
|
+
const buffer = st.sizedBuffer(st.u8());
|
|
7
|
+
expectEncode(buffer, bytes([1, 2]));
|
|
8
|
+
});
|
|
9
|
+
it("encodes empty correctly", () => {
|
|
10
|
+
const spec = st.sizedBuffer(st.u8());
|
|
11
|
+
expectEncode(spec, bytes([]));
|
|
12
|
+
});
|
|
13
|
+
it("holds large data", () => {
|
|
14
|
+
const size = 1024 * 1024 * 8; // 8MB
|
|
15
|
+
const spec = st.sizedBuffer(st.u32());
|
|
16
|
+
const data = new Uint8Array(size);
|
|
17
|
+
data.set([3, 4], 1000);
|
|
18
|
+
data.set([3, 4], 2000);
|
|
19
|
+
expectEncode(spec, data.buffer);
|
|
20
|
+
});
|
|
21
|
+
it("errors on invalid length", () => {
|
|
22
|
+
const spec = st.sizedBuffer(st.u8());
|
|
23
|
+
expectError(() => {
|
|
24
|
+
st.write(spec, bytes(Array.from({ length: 256 }, () => 0)));
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { u64Bigint, s64Bigint } from "./numbers/bigints";
|
|
2
|
+
export { f32, f64 } from "./numbers/floats";
|
|
3
|
+
export { s8, s16, s32, s64 } from "./numbers/sints";
|
|
4
|
+
export { u8, u16, u32, u64 } from "./numbers/uints";
|
|
5
|
+
export { array } from "./containers/array";
|
|
6
|
+
export { fastObject } from "./containers/fastObject";
|
|
7
|
+
export { object } from "./containers/object";
|
|
8
|
+
export { list } from "./containers/list";
|
|
9
|
+
export { sizedBuffer } from "./containers/sizedbuffer";
|
|
10
|
+
export { string } from "./values/string";
|
|
11
|
+
export { buffer } from "./values/buffer";
|
|
12
|
+
export { byteLiteral } from "./values/byteliteral";
|
|
13
|
+
export { createRememberedValue } from "./utilities/remember";
|
|
14
|
+
export { type Transform as Pipeline, pipe } from "./transforms/pipe";
|
|
15
|
+
export { positionOffset } from "./transforms/readOffset";
|
|
16
|
+
export { transform } from "./transforms/transform";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { u64Bigint, s64Bigint } from "./numbers/bigints";
|
|
2
|
+
export { f32, f64 } from "./numbers/floats";
|
|
3
|
+
export { s8, s16, s32, s64 } from "./numbers/sints";
|
|
4
|
+
export { u8, u16, u32, u64 } from "./numbers/uints";
|
|
5
|
+
export { array } from "./containers/array";
|
|
6
|
+
export { fastObject } from "./containers/fastObject";
|
|
7
|
+
export { object } from "./containers/object";
|
|
8
|
+
export { list } from "./containers/list";
|
|
9
|
+
export { sizedBuffer } from "./containers/sizedbuffer";
|
|
10
|
+
export { string } from "./values/string";
|
|
11
|
+
export { buffer } from "./values/buffer";
|
|
12
|
+
export { byteLiteral } from "./values/byteliteral";
|
|
13
|
+
export { createRememberedValue } from "./utilities/remember";
|
|
14
|
+
export { pipe } from "./transforms/pipe";
|
|
15
|
+
export { positionOffset } from "./transforms/readOffset";
|
|
16
|
+
export { transform } from "./transforms/transform";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const checkValue = (value, start, end) => {
|
|
2
|
+
if (value < start || value >= end) {
|
|
3
|
+
throw new Error("Out of Range");
|
|
4
|
+
}
|
|
5
|
+
};
|
|
6
|
+
export function u64Bigint(endian = "little") {
|
|
7
|
+
return {
|
|
8
|
+
size: 8,
|
|
9
|
+
write: (ctx, value) => {
|
|
10
|
+
checkValue(value, 0n, 2n ** 64n);
|
|
11
|
+
ctx.alloc(8);
|
|
12
|
+
ctx.view.setBigUint64(ctx.offset, BigInt(value), endian === "little");
|
|
13
|
+
ctx.offset += 8;
|
|
14
|
+
},
|
|
15
|
+
read: (ctx) => {
|
|
16
|
+
const value = ctx.view.getBigUint64(ctx.offset, endian === "little");
|
|
17
|
+
ctx.offset += 8;
|
|
18
|
+
return value;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function s64Bigint(endian = "little") {
|
|
23
|
+
return {
|
|
24
|
+
size: 8,
|
|
25
|
+
write: (ctx, value) => {
|
|
26
|
+
checkValue(value, -(2n ** 63n), 2n ** 63n);
|
|
27
|
+
ctx.alloc(8);
|
|
28
|
+
ctx.view.setBigInt64(ctx.offset, BigInt(value), endian === "little");
|
|
29
|
+
ctx.offset += 8;
|
|
30
|
+
},
|
|
31
|
+
read: (ctx) => {
|
|
32
|
+
const value = ctx.view.getBigInt64(ctx.offset, endian === "little");
|
|
33
|
+
ctx.offset += 8;
|
|
34
|
+
return value;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import { expectEncode, expectEncodeSnapshot, expectError, randbigint } from "../utils.test";
|
|
3
|
+
import * as st from "../../index";
|
|
4
|
+
function test_bigint(options) {
|
|
5
|
+
const { name, range: [start, end], serializer, size, } = options;
|
|
6
|
+
describe(name, () => {
|
|
7
|
+
it("works in bounds", () => {
|
|
8
|
+
expectEncode(serializer, start);
|
|
9
|
+
for (let i = 0; i < 100; i++) {
|
|
10
|
+
expectEncode(serializer, randbigint(start, end));
|
|
11
|
+
}
|
|
12
|
+
expectEncode(serializer, end);
|
|
13
|
+
});
|
|
14
|
+
it("outside bounds", () => {
|
|
15
|
+
expectError(() => st.write(serializer, start - 1n));
|
|
16
|
+
expectError(() => st.write(serializer, end + 1n));
|
|
17
|
+
});
|
|
18
|
+
it(`is right size`, () => {
|
|
19
|
+
const expectValueSize = (value) => {
|
|
20
|
+
const data = st.write(serializer, value);
|
|
21
|
+
expect(data.byteLength).toBe(size);
|
|
22
|
+
};
|
|
23
|
+
for (let i = 0; i < 100; i++) {
|
|
24
|
+
expectValueSize(randbigint(start, end));
|
|
25
|
+
}
|
|
26
|
+
expectValueSize(start);
|
|
27
|
+
expectValueSize(end);
|
|
28
|
+
});
|
|
29
|
+
it(`matches snapshots`, () => {
|
|
30
|
+
expectEncodeSnapshot(serializer, 0n);
|
|
31
|
+
expectEncodeSnapshot(serializer, start);
|
|
32
|
+
expectEncodeSnapshot(serializer, start + 10n);
|
|
33
|
+
expectEncodeSnapshot(serializer, end - 10n);
|
|
34
|
+
expectEncodeSnapshot(serializer, end);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
test_bigint({
|
|
39
|
+
name: "st.u64(little)",
|
|
40
|
+
serializer: st.u64Bigint("little"),
|
|
41
|
+
range: [0n, 18446744073709551615n],
|
|
42
|
+
size: 8,
|
|
43
|
+
});
|
|
44
|
+
test_bigint({
|
|
45
|
+
name: "st.u64(big)",
|
|
46
|
+
serializer: st.u64Bigint("big"),
|
|
47
|
+
range: [0n, 18446744073709551615n],
|
|
48
|
+
size: 8,
|
|
49
|
+
});
|
|
50
|
+
test_bigint({
|
|
51
|
+
name: "st.s64(little)",
|
|
52
|
+
serializer: st.s64Bigint("little"),
|
|
53
|
+
range: [-9223372036854775808n, 9223372036854775807n],
|
|
54
|
+
size: 8,
|
|
55
|
+
});
|
|
56
|
+
test_bigint({
|
|
57
|
+
name: "st.s64(big)",
|
|
58
|
+
serializer: st.s64Bigint("big"),
|
|
59
|
+
range: [-9223372036854775808n, 9223372036854775807n],
|
|
60
|
+
size: 8,
|
|
61
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function f32(endian = "little") {
|
|
2
|
+
return {
|
|
3
|
+
size: 4,
|
|
4
|
+
write: (ctx, value) => {
|
|
5
|
+
ctx.alloc(4);
|
|
6
|
+
ctx.view.setFloat32(ctx.offset, value, endian === "little");
|
|
7
|
+
ctx.offset += 4;
|
|
8
|
+
},
|
|
9
|
+
read: (ctx) => {
|
|
10
|
+
const value = ctx.view.getFloat32(ctx.offset, endian === "little");
|
|
11
|
+
ctx.offset += 4;
|
|
12
|
+
return value;
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function f64(endian = "little") {
|
|
17
|
+
return {
|
|
18
|
+
size: 8,
|
|
19
|
+
write: (ctx, value) => {
|
|
20
|
+
ctx.alloc(8);
|
|
21
|
+
ctx.view.setFloat64(ctx.offset, value, endian === "little");
|
|
22
|
+
ctx.offset += 8;
|
|
23
|
+
},
|
|
24
|
+
read: (ctx) => {
|
|
25
|
+
const value = ctx.view.getFloat64(ctx.offset, endian === "little");
|
|
26
|
+
ctx.offset += 8;
|
|
27
|
+
return value;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|