@nnilky/structo 1.0.8 → 1.1.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/README.md +4 -4
- package/dist/datatypes/containers/array.d.ts +1 -1
- package/dist/datatypes/containers/array.js +9 -12
- package/dist/datatypes/containers/array.test.js +2 -2
- package/dist/datatypes/containers/exhuastiveArray.d.ts +1 -1
- package/dist/datatypes/containers/exhuastiveArray.js +7 -7
- package/dist/datatypes/containers/exhuastiveArray.test.js +2 -2
- package/dist/datatypes/containers/fastObject.d.ts +1 -1
- package/dist/datatypes/containers/fastObject.test.js +2 -2
- package/dist/datatypes/containers/list.d.ts +1 -1
- package/dist/datatypes/containers/list.js +7 -7
- package/dist/datatypes/containers/list.test.js +2 -2
- package/dist/datatypes/containers/object.d.ts +1 -1
- package/dist/datatypes/containers/object.js +9 -8
- package/dist/datatypes/containers/object.test.js +2 -2
- package/dist/datatypes/containers/taggedUnion.d.ts +1 -1
- package/dist/datatypes/containers/taggedUnion.test.js +2 -2
- package/dist/datatypes/index.d.ts +13 -13
- package/dist/datatypes/index.js +13 -13
- package/dist/datatypes/numbers/bigints.d.ts +1 -1
- package/dist/datatypes/numbers/bigints.js +2 -2
- package/dist/datatypes/numbers/bigints.test.js +2 -2
- package/dist/datatypes/numbers/floats.d.ts +1 -1
- package/dist/datatypes/numbers/floats.js +12 -9
- package/dist/datatypes/numbers/floats.test.js +2 -2
- package/dist/datatypes/numbers/sints.d.ts +1 -1
- package/dist/datatypes/numbers/sints.js +13 -10
- package/dist/datatypes/numbers/sints.test.js +2 -2
- package/dist/datatypes/numbers/uints.d.ts +1 -1
- package/dist/datatypes/numbers/uints.js +13 -10
- package/dist/datatypes/numbers/uints.test.js +2 -2
- package/dist/datatypes/values/bytes.d.ts +2 -2
- package/dist/datatypes/values/bytes.js +6 -6
- package/dist/datatypes/values/bytes.test.js +4 -4
- package/dist/datatypes/values/sizedbytes.d.ts +2 -2
- package/dist/datatypes/values/sizedbytes.js +3 -4
- package/dist/datatypes/values/sizedbytes.test.js +3 -3
- package/dist/datatypes/values/string.d.ts +1 -1
- package/dist/datatypes/values/string.js +2 -3
- package/dist/datatypes/values/string.test.js +2 -2
- package/dist/index.d.ts +6 -6
- package/dist/index.js +5 -5
- package/dist/read.d.ts +2 -3
- package/dist/read.js +10 -12
- package/dist/transforms/encode.d.ts +1 -1
- package/dist/transforms/enum.d.ts +1 -1
- package/dist/transforms/enum.js +1 -1
- package/dist/transforms/enum.test.js +2 -2
- package/dist/transforms/index.d.ts +11 -11
- package/dist/transforms/index.js +11 -11
- package/dist/transforms/literal.d.ts +1 -1
- package/dist/transforms/literal.js +1 -1
- package/dist/transforms/literal.test.js +2 -2
- package/dist/transforms/modify.d.ts +1 -1
- package/dist/transforms/modify.test.js +2 -2
- package/dist/transforms/offset.d.ts +1 -1
- package/dist/transforms/offset.test.js +2 -2
- package/dist/transforms/pipe.d.ts +1 -1
- package/dist/transforms/toAscii.d.ts +1 -1
- package/dist/transforms/toAscii.js +3 -3
- package/dist/transforms/toAscii.test.js +2 -2
- package/dist/transforms/toBase64.d.ts +1 -1
- package/dist/transforms/toBase64.js +3 -3
- package/dist/transforms/toBase64.test.js +2 -2
- package/dist/transforms/toBytes.d.ts +1 -1
- package/dist/transforms/toBytes.js +3 -3
- package/dist/transforms/toBytes.test.js +2 -2
- package/dist/transforms/toHex.d.ts +3 -3
- package/dist/transforms/toHex.js +5 -5
- package/dist/transforms/toHex.test.js +2 -2
- package/dist/transforms/toTypedArray.d.ts +11 -6
- package/dist/transforms/toTypedArray.js +11 -6
- package/dist/transforms/toTypedArray.test.js +20 -11
- package/dist/types.d.ts +11 -9
- package/dist/utilities/index.d.ts +3 -3
- package/dist/utilities/index.js +3 -3
- package/dist/utilities/lazy.d.ts +1 -1
- package/dist/utilities/lazy.test.js +2 -2
- package/dist/utilities/reference.d.ts +1 -1
- package/dist/utilities/reference.test.js +2 -2
- package/dist/utilities/remember.d.ts +2 -2
- package/dist/utilities/remember.js +3 -3
- package/dist/utilities/remember.test.js +2 -2
- package/dist/utils.test.d.ts +2 -2
- package/dist/utils.test.js +2 -4
- package/dist/write.d.ts +2 -2
- package/dist/write.js +47 -34
- package/package.json +7 -2
- package/dist/datatypes/containers/sizedbytes.d.ts +0 -2
- package/dist/datatypes/containers/sizedbytes.js +0 -17
- package/dist/datatypes/containers/sizedbytes.test.d.ts +0 -1
- package/dist/datatypes/containers/sizedbytes.test.js +0 -27
- package/dist/datatypes/containers/string.d.ts +0 -2
- package/dist/datatypes/containers/string.js +0 -24
- package/dist/datatypes/containers/string.test.d.ts +0 -1
- package/dist/datatypes/containers/string.test.js +0 -24
- package/dist/datatypes/transforms/encode.d.ts +0 -2
- package/dist/datatypes/transforms/encode.js +0 -7
- package/dist/datatypes/transforms/fixedOffset.d.ts +0 -2
- package/dist/datatypes/transforms/fixedOffset.js +0 -18
- package/dist/datatypes/transforms/modify.d.ts +0 -2
- package/dist/datatypes/transforms/modify.js +0 -7
- package/dist/datatypes/transforms/noAdvance.d.ts +0 -16
- package/dist/datatypes/transforms/noAdvance.js +0 -30
- package/dist/datatypes/transforms/pipe.d.ts +0 -11
- package/dist/datatypes/transforms/pipe.js +0 -16
- package/dist/datatypes/utilities/remember.d.ts +0 -15
- package/dist/datatypes/utilities/remember.js +0 -36
- package/dist/datatypes/utilities/remember.test.d.ts +0 -1
- package/dist/datatypes/utilities/remember.test.js +0 -44
- package/dist/datatypes/utils.test.d.ts +0 -8
- package/dist/datatypes/utils.test.js +0 -37
- package/dist/datatypes/values/byteliteral.d.ts +0 -2
- package/dist/datatypes/values/byteliteral.js +0 -20
- package/dist/datatypes/values/json.d.ts +0 -10
- package/dist/datatypes/values/json.js +0 -21
- package/dist/transforms/fixedOffset.d.ts +0 -2
- package/dist/transforms/fixedOffset.js +0 -18
- package/dist/transforms/noAdvance.d.ts +0 -16
- package/dist/transforms/noAdvance.js +0 -30
- package/dist/transforms/noAdvance.test.d.ts +0 -1
- package/dist/transforms/noAdvance.test.js +0 -17
package/README.md
CHANGED
|
@@ -20,14 +20,14 @@ const Entity = st.object({
|
|
|
20
20
|
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
- Extremely Fast! [benchmarks](./benchmark) show equivelent performance to hand written serializers
|
|
24
|
+
- Designed to be both Web & Node.js compatible
|
|
23
25
|
- Lightweight, base size is <1KB and each datatype is a few hundred bytes
|
|
24
|
-
-
|
|
25
|
-
- Designed for both Web/Node.js compatible
|
|
26
|
-
- Easily implement your own serializers
|
|
26
|
+
- Easily extendable with your own datatypes
|
|
27
27
|
|
|
28
28
|
Each serializer is completely seperate from the base library, meaning you only pay for what you use.
|
|
29
29
|
|
|
30
|
-
## Implementing your
|
|
30
|
+
## Implementing your datatype
|
|
31
31
|
|
|
32
32
|
Implementing your own serializer is incredibly simple, heres the `f64` serializer for example
|
|
33
33
|
|
|
@@ -7,31 +7,28 @@
|
|
|
7
7
|
*
|
|
8
8
|
*/
|
|
9
9
|
export function array(size, type) {
|
|
10
|
-
const SHOW_ERRORS =
|
|
11
|
-
const { read: readType, write: writeType, size: typeSize } = type;
|
|
10
|
+
const SHOW_ERRORS = false;
|
|
12
11
|
return {
|
|
13
12
|
size: type.size ? size * type.size : undefined,
|
|
14
13
|
write: (ctx, value) => {
|
|
15
14
|
if (value.length !== size)
|
|
16
15
|
throw new Error("Invalid Size");
|
|
17
|
-
if (
|
|
18
|
-
ctx.
|
|
16
|
+
if (type.size)
|
|
17
|
+
ctx.reserve(size * type.size);
|
|
19
18
|
for (let i = 0; i < size; i++) {
|
|
20
|
-
if (SHOW_ERRORS)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (SHOW_ERRORS)
|
|
24
|
-
ctx.stack.pop();
|
|
19
|
+
// if (SHOW_ERRORS) ctx.path.push(`[${i}]`);
|
|
20
|
+
type.write(ctx, value[i]);
|
|
21
|
+
// if (SHOW_ERRORS) ctx.path.pop();
|
|
25
22
|
}
|
|
26
23
|
},
|
|
27
24
|
read: (ctx) => {
|
|
28
25
|
const arr = new Array(size);
|
|
29
26
|
for (let i = 0; i < size; i++) {
|
|
30
27
|
if (SHOW_ERRORS)
|
|
31
|
-
ctx.
|
|
32
|
-
arr[i] =
|
|
28
|
+
ctx.path.push(`[${i}]`);
|
|
29
|
+
arr[i] = type.read(ctx);
|
|
33
30
|
if (SHOW_ERRORS)
|
|
34
|
-
ctx.
|
|
31
|
+
ctx.path.pop();
|
|
35
32
|
}
|
|
36
33
|
return arr;
|
|
37
34
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import { encodeTest, encodeFailTest } from "../../utils.test";
|
|
3
|
-
import * as st from "../../index";
|
|
2
|
+
import { encodeTest, encodeFailTest } from "../../utils.test.js";
|
|
3
|
+
import * as st from "../../index.js";
|
|
4
4
|
describe("st.array", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
6
|
encodeTest(st.array(4, st.u16()), [1, 2, 3, 4]);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as st from "../../index";
|
|
1
|
+
import * as st from "../../index.js";
|
|
2
2
|
/**
|
|
3
3
|
* exhuastiveArray is read until the end of the data
|
|
4
4
|
*
|
|
@@ -12,23 +12,23 @@ export function exhuastiveArray(type) {
|
|
|
12
12
|
let arr = [];
|
|
13
13
|
let i = 0;
|
|
14
14
|
while (ctx.offset < ctx.view.byteLength) {
|
|
15
|
-
const TRACK_STACK = i++ <
|
|
15
|
+
const TRACK_STACK = i++ < 512;
|
|
16
16
|
if (TRACK_STACK)
|
|
17
|
-
ctx.
|
|
17
|
+
ctx.path.push(`[${arr.length - 1}]`);
|
|
18
18
|
arr.push(type.read(ctx));
|
|
19
19
|
if (TRACK_STACK)
|
|
20
|
-
ctx.
|
|
20
|
+
ctx.path.pop();
|
|
21
21
|
}
|
|
22
22
|
return arr;
|
|
23
23
|
},
|
|
24
24
|
write(ctx, value) {
|
|
25
|
-
const TRACK_STACK = value.length <
|
|
25
|
+
const TRACK_STACK = value.length < 512;
|
|
26
26
|
for (let i = 0; i < value.length; i++) {
|
|
27
27
|
if (TRACK_STACK)
|
|
28
|
-
ctx.
|
|
28
|
+
ctx.path.push(`[${i}]`);
|
|
29
29
|
type.write(ctx, value[i]);
|
|
30
30
|
if (TRACK_STACK)
|
|
31
|
-
ctx.
|
|
31
|
+
ctx.path.pop();
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import { encodeTest, encodeSnapshotTest, encodeFailTest } from "../../utils.test";
|
|
3
|
-
import * as st from "../../index";
|
|
2
|
+
import { encodeTest, encodeSnapshotTest, encodeFailTest } from "../../utils.test.js";
|
|
3
|
+
import * as st from "../../index.js";
|
|
4
4
|
describe("st.exhuastiveArray", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
6
|
const spec = st.exhuastiveArray(st.u32());
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import { bytes, encodeTest, encodeSnapshotTest } from "../../utils.test";
|
|
4
|
-
import * as st from "../../index";
|
|
3
|
+
import { bytes, encodeTest, encodeSnapshotTest } from "../../utils.test.js";
|
|
4
|
+
import * as st from "../../index.js";
|
|
5
5
|
describe("st.fastObject", () => {
|
|
6
6
|
const spec = st.fastObject({
|
|
7
7
|
a: st.u8(),
|
|
@@ -12,29 +12,29 @@ export function list(length, type) {
|
|
|
12
12
|
return {
|
|
13
13
|
write: (ctx, value) => {
|
|
14
14
|
length.write(ctx, value.length);
|
|
15
|
-
const TRACK_STACK = value.length <
|
|
15
|
+
const TRACK_STACK = value.length < 1000;
|
|
16
16
|
if (type.size) {
|
|
17
|
-
ctx.
|
|
17
|
+
ctx.reserve(value.length * type.size);
|
|
18
18
|
}
|
|
19
19
|
for (let i = 0; i < value.length; i++) {
|
|
20
20
|
if (TRACK_STACK)
|
|
21
|
-
ctx.
|
|
21
|
+
ctx.path.push(`[${i}]`);
|
|
22
22
|
const v = value[i];
|
|
23
23
|
type.write(ctx, v);
|
|
24
24
|
if (TRACK_STACK)
|
|
25
|
-
ctx.
|
|
25
|
+
ctx.path.pop();
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
read: (ctx) => {
|
|
29
29
|
const size = length.read(ctx);
|
|
30
|
-
const TRACK_STACK = size <
|
|
30
|
+
const TRACK_STACK = size < 1000;
|
|
31
31
|
const arr = new Array(size);
|
|
32
32
|
for (let i = 0; i < size; i++) {
|
|
33
33
|
if (TRACK_STACK)
|
|
34
|
-
ctx.
|
|
34
|
+
ctx.path.push(`[${i}]`);
|
|
35
35
|
arr[i] = type.read(ctx);
|
|
36
36
|
if (TRACK_STACK)
|
|
37
|
-
ctx.
|
|
37
|
+
ctx.path.pop();
|
|
38
38
|
}
|
|
39
39
|
return arr;
|
|
40
40
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it } from "bun:test";
|
|
2
|
-
import { encodeTest, encodeSnapshotTest, encodeFailTest } from "../../utils.test";
|
|
3
|
-
import * as st from "../../index";
|
|
2
|
+
import { encodeTest, encodeSnapshotTest, encodeFailTest } from "../../utils.test.js";
|
|
3
|
+
import * as st from "../../index.js";
|
|
4
4
|
describe("st.list", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
6
|
const spec = st.list(st.u8(), st.u32());
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferInput, InferOutput, Serializer } from "../../types";
|
|
1
|
+
import type { InferInput, InferOutput, Serializer } from "../../types.js";
|
|
2
2
|
type InferObjectInfer<T> = T extends Record<string, Serializer<any>> ? {
|
|
3
3
|
[Key in keyof T]: InferInput<T[Key]>;
|
|
4
4
|
} : never;
|
|
@@ -19,23 +19,24 @@ export function object(definition) {
|
|
|
19
19
|
size,
|
|
20
20
|
write: (ctx, value) => {
|
|
21
21
|
if (size)
|
|
22
|
-
ctx.
|
|
22
|
+
ctx.reserve(size);
|
|
23
23
|
for (let i = 0; i < entires.length; i++) {
|
|
24
24
|
const [key, serializer] = entires[i];
|
|
25
|
-
ctx.
|
|
25
|
+
// ctx.path.push(`.${key}`);
|
|
26
26
|
serializer.write(ctx, value[key]);
|
|
27
|
-
ctx.
|
|
27
|
+
// ctx.path.pop();
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
read: (ctx) => {
|
|
31
|
-
const output =
|
|
31
|
+
const output = {};
|
|
32
32
|
for (let i = 0; i < entires.length; i++) {
|
|
33
33
|
const [key, serializer] = entires[i];
|
|
34
|
-
ctx.
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
// ctx.path.push(`.${key}`);
|
|
35
|
+
//@ts-expect-error
|
|
36
|
+
output[key] = serializer.read(ctx);
|
|
37
|
+
// ctx.path.pop();
|
|
37
38
|
}
|
|
38
|
-
return
|
|
39
|
+
return output;
|
|
39
40
|
},
|
|
40
41
|
};
|
|
41
42
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect, expectTypeOf } from "bun:test";
|
|
3
|
-
import { bytes, encodeTest, encodeSnapshotTest } from "../../utils.test";
|
|
4
|
-
import * as st from "../../index";
|
|
3
|
+
import { bytes, encodeTest, encodeSnapshotTest } from "../../utils.test.js";
|
|
4
|
+
import * as st from "../../index.js";
|
|
5
5
|
describe("st.object", () => {
|
|
6
6
|
it("encodes correctly", () => {
|
|
7
7
|
encodeTest(st.object({ a: st.u8(), b: st.u8() }), //
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expectTypeOf, it } from "bun:test";
|
|
2
|
-
import { encodeTest, expectEncodeSize, encodeSnapshotTest, encodeFailTest } from "../../utils.test";
|
|
3
|
-
import * as st from "../../index";
|
|
2
|
+
import { encodeTest, expectEncodeSize, encodeSnapshotTest, encodeFailTest, } from "../../utils.test.js";
|
|
3
|
+
import * as st from "../../index.js";
|
|
4
4
|
describe("st.taggedUnion", () => {
|
|
5
5
|
it("encode correctly", () => {
|
|
6
6
|
const spec = st.taggedUnion(st.u8(), {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export { u64Bigint, s64Bigint } from "./numbers/bigints";
|
|
2
|
-
export { f16, 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 { taggedUnion } from "./containers/taggedUnion";
|
|
10
|
-
export { exhuastiveArray } from "./containers/exhuastiveArray";
|
|
11
|
-
export { bytes } from "./values/bytes";
|
|
12
|
-
export { sizedBytes } from "./values/sizedbytes";
|
|
13
|
-
export { string } from "./values/string";
|
|
1
|
+
export { u64Bigint, s64Bigint } from "./numbers/bigints.js";
|
|
2
|
+
export { f16, f32, f64 } from "./numbers/floats.js";
|
|
3
|
+
export { s8, s16, s32, s64 } from "./numbers/sints.js";
|
|
4
|
+
export { u8, u16, u32, u64 } from "./numbers/uints.js";
|
|
5
|
+
export { array } from "./containers/array.js";
|
|
6
|
+
export { fastObject } from "./containers/fastObject.js";
|
|
7
|
+
export { object } from "./containers/object.js";
|
|
8
|
+
export { list } from "./containers/list.js";
|
|
9
|
+
export { taggedUnion } from "./containers/taggedUnion.js";
|
|
10
|
+
export { exhuastiveArray } from "./containers/exhuastiveArray.js";
|
|
11
|
+
export { bytes } from "./values/bytes.js";
|
|
12
|
+
export { sizedBytes } from "./values/sizedbytes.js";
|
|
13
|
+
export { string } from "./values/string.js";
|
package/dist/datatypes/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export { u64Bigint, s64Bigint } from "./numbers/bigints";
|
|
2
|
-
export { f16, 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 { taggedUnion } from "./containers/taggedUnion";
|
|
10
|
-
export { exhuastiveArray } from "./containers/exhuastiveArray";
|
|
11
|
-
export { bytes } from "./values/bytes";
|
|
12
|
-
export { sizedBytes } from "./values/sizedbytes";
|
|
13
|
-
export { string } from "./values/string";
|
|
1
|
+
export { u64Bigint, s64Bigint } from "./numbers/bigints.js";
|
|
2
|
+
export { f16, f32, f64 } from "./numbers/floats.js";
|
|
3
|
+
export { s8, s16, s32, s64 } from "./numbers/sints.js";
|
|
4
|
+
export { u8, u16, u32, u64 } from "./numbers/uints.js";
|
|
5
|
+
export { array } from "./containers/array.js";
|
|
6
|
+
export { fastObject } from "./containers/fastObject.js";
|
|
7
|
+
export { object } from "./containers/object.js";
|
|
8
|
+
export { list } from "./containers/list.js";
|
|
9
|
+
export { taggedUnion } from "./containers/taggedUnion.js";
|
|
10
|
+
export { exhuastiveArray } from "./containers/exhuastiveArray.js";
|
|
11
|
+
export { bytes } from "./values/bytes.js";
|
|
12
|
+
export { sizedBytes } from "./values/sizedbytes.js";
|
|
13
|
+
export { string } from "./values/string.js";
|
|
@@ -13,7 +13,7 @@ export function u64Bigint(endian = "little") {
|
|
|
13
13
|
size: 8,
|
|
14
14
|
write: (ctx, value) => {
|
|
15
15
|
checkValue(value, 0n, 2n ** 64n);
|
|
16
|
-
ctx.
|
|
16
|
+
ctx.reserve(8);
|
|
17
17
|
ctx.view.setBigUint64(ctx.offset, BigInt(value), endian === "little");
|
|
18
18
|
ctx.offset += 8;
|
|
19
19
|
},
|
|
@@ -34,7 +34,7 @@ export function s64Bigint(endian = "little") {
|
|
|
34
34
|
size: 8,
|
|
35
35
|
write: (ctx, value) => {
|
|
36
36
|
checkValue(value, -(2n ** 63n), 2n ** 63n);
|
|
37
|
-
ctx.
|
|
37
|
+
ctx.reserve(8);
|
|
38
38
|
ctx.view.setBigInt64(ctx.offset, BigInt(value), endian === "little");
|
|
39
39
|
ctx.offset += 8;
|
|
40
40
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import { encodeTest, encodeSnapshotTest, randbigint, encodeFailTest } from "../../utils.test";
|
|
3
|
-
import * as st from "../../index";
|
|
2
|
+
import { encodeTest, encodeSnapshotTest, randbigint, encodeFailTest } from "../../utils.test.js";
|
|
3
|
+
import * as st from "../../index.js";
|
|
4
4
|
function test_bigint(options) {
|
|
5
5
|
const { name, range: [start, end], serializer, size, } = options;
|
|
6
6
|
describe(name, () => {
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/** 16bit float */
|
|
2
2
|
export function f16(endian = "little") {
|
|
3
|
+
const littleEndian = endian === "little";
|
|
3
4
|
return {
|
|
4
5
|
size: 2,
|
|
5
6
|
write: (ctx, value) => {
|
|
6
|
-
ctx.
|
|
7
|
-
ctx.view.setFloat16(ctx.offset, value,
|
|
7
|
+
ctx.reserve(2);
|
|
8
|
+
ctx.view.setFloat16(ctx.offset, value, littleEndian);
|
|
8
9
|
ctx.offset += 2;
|
|
9
10
|
},
|
|
10
11
|
read: (ctx) => {
|
|
11
|
-
const value = ctx.view.getFloat16(ctx.offset,
|
|
12
|
+
const value = ctx.view.getFloat16(ctx.offset, littleEndian);
|
|
12
13
|
ctx.offset += 2;
|
|
13
14
|
return value;
|
|
14
15
|
},
|
|
@@ -16,15 +17,16 @@ export function f16(endian = "little") {
|
|
|
16
17
|
}
|
|
17
18
|
/** 32bit float */
|
|
18
19
|
export function f32(endian = "little") {
|
|
20
|
+
const littleEndian = endian === "little";
|
|
19
21
|
return {
|
|
20
22
|
size: 4,
|
|
21
23
|
write: (ctx, value) => {
|
|
22
|
-
ctx.
|
|
23
|
-
ctx.view.setFloat32(ctx.offset, value,
|
|
24
|
+
ctx.reserve(4);
|
|
25
|
+
ctx.view.setFloat32(ctx.offset, value, littleEndian);
|
|
24
26
|
ctx.offset += 4;
|
|
25
27
|
},
|
|
26
28
|
read: (ctx) => {
|
|
27
|
-
const value = ctx.view.getFloat32(ctx.offset,
|
|
29
|
+
const value = ctx.view.getFloat32(ctx.offset, littleEndian);
|
|
28
30
|
ctx.offset += 4;
|
|
29
31
|
return value;
|
|
30
32
|
},
|
|
@@ -32,15 +34,16 @@ export function f32(endian = "little") {
|
|
|
32
34
|
}
|
|
33
35
|
/** 64bit float */
|
|
34
36
|
export function f64(endian = "little") {
|
|
37
|
+
const littleEndian = endian === "little";
|
|
35
38
|
return {
|
|
36
39
|
size: 8,
|
|
37
40
|
write: (ctx, value) => {
|
|
38
|
-
ctx.
|
|
39
|
-
ctx.view.setFloat64(ctx.offset, value,
|
|
41
|
+
ctx.reserve(8);
|
|
42
|
+
ctx.view.setFloat64(ctx.offset, value, littleEndian);
|
|
40
43
|
ctx.offset += 8;
|
|
41
44
|
},
|
|
42
45
|
read: (ctx) => {
|
|
43
|
-
const value = ctx.view.getFloat64(ctx.offset,
|
|
46
|
+
const value = ctx.view.getFloat64(ctx.offset, littleEndian);
|
|
44
47
|
ctx.offset += 8;
|
|
45
48
|
return value;
|
|
46
49
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import { encodeTest, expectEncodeSize } from "../../utils.test";
|
|
4
|
-
import * as st from "../../index";
|
|
3
|
+
import { encodeTest, expectEncodeSize } from "../../utils.test.js";
|
|
4
|
+
import * as st from "../../index.js";
|
|
5
5
|
function test_float(options) {
|
|
6
6
|
const { name, serializer, size } = options;
|
|
7
7
|
const expectEncodeApproximately = (value) => {
|
|
@@ -12,7 +12,7 @@ export function s8() {
|
|
|
12
12
|
size: 1,
|
|
13
13
|
write: (ctx, value) => {
|
|
14
14
|
checkValue(value, -(2 ** 7), 2 ** 7);
|
|
15
|
-
ctx.
|
|
15
|
+
ctx.reserve(1);
|
|
16
16
|
ctx.view.setInt8(ctx.offset, value);
|
|
17
17
|
ctx.offset += 1;
|
|
18
18
|
},
|
|
@@ -25,16 +25,17 @@ export function s8() {
|
|
|
25
25
|
}
|
|
26
26
|
/** 16bit signed integer */
|
|
27
27
|
export function s16(endian = "little") {
|
|
28
|
+
const littleEndian = endian === "little";
|
|
28
29
|
return {
|
|
29
30
|
size: 2,
|
|
30
31
|
write: (ctx, value) => {
|
|
31
32
|
checkValue(value, -(2 ** 15), 2 ** 15);
|
|
32
|
-
ctx.
|
|
33
|
-
ctx.view.setInt16(ctx.offset, value,
|
|
33
|
+
ctx.reserve(2);
|
|
34
|
+
ctx.view.setInt16(ctx.offset, value, littleEndian);
|
|
34
35
|
ctx.offset += 2;
|
|
35
36
|
},
|
|
36
37
|
read: (ctx) => {
|
|
37
|
-
const value = ctx.view.getInt16(ctx.offset,
|
|
38
|
+
const value = ctx.view.getInt16(ctx.offset, littleEndian);
|
|
38
39
|
ctx.offset += 2;
|
|
39
40
|
return value;
|
|
40
41
|
},
|
|
@@ -42,16 +43,17 @@ export function s16(endian = "little") {
|
|
|
42
43
|
}
|
|
43
44
|
/** 32bit signed integer */
|
|
44
45
|
export function s32(endian = "little") {
|
|
46
|
+
const littleEndian = endian === "little";
|
|
45
47
|
return {
|
|
46
48
|
size: 4,
|
|
47
49
|
write: (ctx, value) => {
|
|
48
50
|
checkValue(value, -(2 ** 31), 2 ** 31);
|
|
49
|
-
ctx.
|
|
50
|
-
ctx.view.setInt32(ctx.offset, value,
|
|
51
|
+
ctx.reserve(4);
|
|
52
|
+
ctx.view.setInt32(ctx.offset, value, littleEndian);
|
|
51
53
|
ctx.offset += 4;
|
|
52
54
|
},
|
|
53
55
|
read: (ctx) => {
|
|
54
|
-
const value = ctx.view.getInt32(ctx.offset,
|
|
56
|
+
const value = ctx.view.getInt32(ctx.offset, littleEndian);
|
|
55
57
|
ctx.offset += 4;
|
|
56
58
|
return value;
|
|
57
59
|
},
|
|
@@ -59,16 +61,17 @@ export function s32(endian = "little") {
|
|
|
59
61
|
}
|
|
60
62
|
/** 64bit signed integer */
|
|
61
63
|
export function s64(endian = "little") {
|
|
64
|
+
const littleEndian = endian === "little";
|
|
62
65
|
return {
|
|
63
66
|
size: 8,
|
|
64
67
|
write: (ctx, value) => {
|
|
65
68
|
checkValue(value, -(2 ** 63), 2 ** 63);
|
|
66
|
-
ctx.
|
|
67
|
-
ctx.view.setBigInt64(ctx.offset, BigInt(value),
|
|
69
|
+
ctx.reserve(8);
|
|
70
|
+
ctx.view.setBigInt64(ctx.offset, BigInt(value), littleEndian);
|
|
68
71
|
ctx.offset += 8;
|
|
69
72
|
},
|
|
70
73
|
read: (ctx) => {
|
|
71
|
-
const value = Number(ctx.view.getBigInt64(ctx.offset,
|
|
74
|
+
const value = Number(ctx.view.getBigInt64(ctx.offset, littleEndian));
|
|
72
75
|
ctx.offset += 8;
|
|
73
76
|
return value;
|
|
74
77
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import { encodeTest, encodeSnapshotTest, expectError, randint, encodeFailTest, } from "../../utils.test";
|
|
4
|
-
import * as st from "../../index";
|
|
3
|
+
import { encodeTest, encodeSnapshotTest, expectError, randint, encodeFailTest, } from "../../utils.test.js";
|
|
4
|
+
import * as st from "../../index.js";
|
|
5
5
|
function test_sint(options) {
|
|
6
6
|
const { name, range: [start, end], serializer, size, disableRangeCheck, } = options;
|
|
7
7
|
describe(name, () => {
|
|
@@ -7,7 +7,7 @@ export function u8() {
|
|
|
7
7
|
throw new Error("Not Integer");
|
|
8
8
|
if (value < 0 || value >= 2 ** 8)
|
|
9
9
|
throw new Error("Out of Range");
|
|
10
|
-
ctx.
|
|
10
|
+
ctx.reserve(1);
|
|
11
11
|
ctx.view.setUint8(ctx.offset, value);
|
|
12
12
|
ctx.offset += 1;
|
|
13
13
|
},
|
|
@@ -20,6 +20,7 @@ export function u8() {
|
|
|
20
20
|
}
|
|
21
21
|
/** 16bit unsigned integer */
|
|
22
22
|
export function u16(endian = "little") {
|
|
23
|
+
const littleEndian = endian === "little";
|
|
23
24
|
return {
|
|
24
25
|
size: 2,
|
|
25
26
|
write: (ctx, value) => {
|
|
@@ -27,12 +28,12 @@ export function u16(endian = "little") {
|
|
|
27
28
|
throw new Error("Not Integer");
|
|
28
29
|
if (value < 0 || value >= 2 ** 16)
|
|
29
30
|
throw new Error("Out of Range");
|
|
30
|
-
ctx.
|
|
31
|
-
ctx.view.setUint16(ctx.offset, value,
|
|
31
|
+
ctx.reserve(2);
|
|
32
|
+
ctx.view.setUint16(ctx.offset, value, littleEndian);
|
|
32
33
|
ctx.offset += 2;
|
|
33
34
|
},
|
|
34
35
|
read: (ctx) => {
|
|
35
|
-
const value = ctx.view.getUint16(ctx.offset,
|
|
36
|
+
const value = ctx.view.getUint16(ctx.offset, littleEndian);
|
|
36
37
|
ctx.offset += 2;
|
|
37
38
|
return value;
|
|
38
39
|
},
|
|
@@ -40,6 +41,7 @@ export function u16(endian = "little") {
|
|
|
40
41
|
}
|
|
41
42
|
/** 32bit unsigned integer */
|
|
42
43
|
export function u32(endian = "little") {
|
|
44
|
+
const littleEndian = endian === "little";
|
|
43
45
|
return {
|
|
44
46
|
size: 4,
|
|
45
47
|
write: (ctx, value) => {
|
|
@@ -47,12 +49,12 @@ export function u32(endian = "little") {
|
|
|
47
49
|
throw new Error("Not Integer");
|
|
48
50
|
if (value < 0 || value >= 2 ** 32)
|
|
49
51
|
throw new Error("Out of Range");
|
|
50
|
-
ctx.
|
|
51
|
-
ctx.view.setUint32(ctx.offset, value,
|
|
52
|
+
ctx.reserve(4);
|
|
53
|
+
ctx.view.setUint32(ctx.offset, value, littleEndian);
|
|
52
54
|
ctx.offset += 4;
|
|
53
55
|
},
|
|
54
56
|
read: (ctx) => {
|
|
55
|
-
const value = ctx.view.getUint32(ctx.offset,
|
|
57
|
+
const value = ctx.view.getUint32(ctx.offset, littleEndian);
|
|
56
58
|
ctx.offset += 4;
|
|
57
59
|
return value;
|
|
58
60
|
},
|
|
@@ -60,6 +62,7 @@ export function u32(endian = "little") {
|
|
|
60
62
|
}
|
|
61
63
|
/** 64bit unsigned integer */
|
|
62
64
|
export function u64(endian = "little") {
|
|
65
|
+
const littleEndian = endian === "little";
|
|
63
66
|
return {
|
|
64
67
|
size: 8,
|
|
65
68
|
write: (ctx, value) => {
|
|
@@ -67,12 +70,12 @@ export function u64(endian = "little") {
|
|
|
67
70
|
throw new Error("Not Integer");
|
|
68
71
|
if (value < 0 || value >= 2 ** 64)
|
|
69
72
|
throw new Error("Out of Range");
|
|
70
|
-
ctx.
|
|
71
|
-
ctx.view.setBigUint64(ctx.offset, BigInt(value),
|
|
73
|
+
ctx.reserve(8);
|
|
74
|
+
ctx.view.setBigUint64(ctx.offset, BigInt(value), littleEndian);
|
|
72
75
|
ctx.offset += 8;
|
|
73
76
|
},
|
|
74
77
|
read: (ctx) => {
|
|
75
|
-
const value = Number(ctx.view.getBigUint64(ctx.offset,
|
|
78
|
+
const value = Number(ctx.view.getBigUint64(ctx.offset, littleEndian));
|
|
76
79
|
ctx.offset += 8;
|
|
77
80
|
return value;
|
|
78
81
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//@ts-ignore TODO
|
|
2
2
|
import { describe, it, expect } from "bun:test";
|
|
3
|
-
import { encodeTest, encodeSnapshotTest, expectError, randint, encodeFailTest, } from "../../utils.test";
|
|
4
|
-
import * as st from "../../index";
|
|
3
|
+
import { encodeTest, encodeSnapshotTest, expectError, randint, encodeFailTest, } from "../../utils.test.js";
|
|
4
|
+
import * as st from "../../index.js";
|
|
5
5
|
function test_uint(options) {
|
|
6
6
|
const { name, range: [start, end], serializer, size, disableMaxCheck, } = options;
|
|
7
7
|
describe(name, () => {
|