@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.
Files changed (122) hide show
  1. package/README.md +4 -4
  2. package/dist/datatypes/containers/array.d.ts +1 -1
  3. package/dist/datatypes/containers/array.js +9 -12
  4. package/dist/datatypes/containers/array.test.js +2 -2
  5. package/dist/datatypes/containers/exhuastiveArray.d.ts +1 -1
  6. package/dist/datatypes/containers/exhuastiveArray.js +7 -7
  7. package/dist/datatypes/containers/exhuastiveArray.test.js +2 -2
  8. package/dist/datatypes/containers/fastObject.d.ts +1 -1
  9. package/dist/datatypes/containers/fastObject.test.js +2 -2
  10. package/dist/datatypes/containers/list.d.ts +1 -1
  11. package/dist/datatypes/containers/list.js +7 -7
  12. package/dist/datatypes/containers/list.test.js +2 -2
  13. package/dist/datatypes/containers/object.d.ts +1 -1
  14. package/dist/datatypes/containers/object.js +9 -8
  15. package/dist/datatypes/containers/object.test.js +2 -2
  16. package/dist/datatypes/containers/taggedUnion.d.ts +1 -1
  17. package/dist/datatypes/containers/taggedUnion.test.js +2 -2
  18. package/dist/datatypes/index.d.ts +13 -13
  19. package/dist/datatypes/index.js +13 -13
  20. package/dist/datatypes/numbers/bigints.d.ts +1 -1
  21. package/dist/datatypes/numbers/bigints.js +2 -2
  22. package/dist/datatypes/numbers/bigints.test.js +2 -2
  23. package/dist/datatypes/numbers/floats.d.ts +1 -1
  24. package/dist/datatypes/numbers/floats.js +12 -9
  25. package/dist/datatypes/numbers/floats.test.js +2 -2
  26. package/dist/datatypes/numbers/sints.d.ts +1 -1
  27. package/dist/datatypes/numbers/sints.js +13 -10
  28. package/dist/datatypes/numbers/sints.test.js +2 -2
  29. package/dist/datatypes/numbers/uints.d.ts +1 -1
  30. package/dist/datatypes/numbers/uints.js +13 -10
  31. package/dist/datatypes/numbers/uints.test.js +2 -2
  32. package/dist/datatypes/values/bytes.d.ts +2 -2
  33. package/dist/datatypes/values/bytes.js +6 -6
  34. package/dist/datatypes/values/bytes.test.js +4 -4
  35. package/dist/datatypes/values/sizedbytes.d.ts +2 -2
  36. package/dist/datatypes/values/sizedbytes.js +3 -4
  37. package/dist/datatypes/values/sizedbytes.test.js +3 -3
  38. package/dist/datatypes/values/string.d.ts +1 -1
  39. package/dist/datatypes/values/string.js +2 -3
  40. package/dist/datatypes/values/string.test.js +2 -2
  41. package/dist/index.d.ts +6 -6
  42. package/dist/index.js +5 -5
  43. package/dist/read.d.ts +2 -3
  44. package/dist/read.js +10 -12
  45. package/dist/transforms/encode.d.ts +1 -1
  46. package/dist/transforms/enum.d.ts +1 -1
  47. package/dist/transforms/enum.js +1 -1
  48. package/dist/transforms/enum.test.js +2 -2
  49. package/dist/transforms/index.d.ts +11 -11
  50. package/dist/transforms/index.js +11 -11
  51. package/dist/transforms/literal.d.ts +1 -1
  52. package/dist/transforms/literal.js +1 -1
  53. package/dist/transforms/literal.test.js +2 -2
  54. package/dist/transforms/modify.d.ts +1 -1
  55. package/dist/transforms/modify.test.js +2 -2
  56. package/dist/transforms/offset.d.ts +1 -1
  57. package/dist/transforms/offset.test.js +2 -2
  58. package/dist/transforms/pipe.d.ts +1 -1
  59. package/dist/transforms/toAscii.d.ts +1 -1
  60. package/dist/transforms/toAscii.js +3 -3
  61. package/dist/transforms/toAscii.test.js +2 -2
  62. package/dist/transforms/toBase64.d.ts +1 -1
  63. package/dist/transforms/toBase64.js +3 -3
  64. package/dist/transforms/toBase64.test.js +2 -2
  65. package/dist/transforms/toBytes.d.ts +1 -1
  66. package/dist/transforms/toBytes.js +3 -3
  67. package/dist/transforms/toBytes.test.js +2 -2
  68. package/dist/transforms/toHex.d.ts +3 -3
  69. package/dist/transforms/toHex.js +5 -5
  70. package/dist/transforms/toHex.test.js +2 -2
  71. package/dist/transforms/toTypedArray.d.ts +11 -6
  72. package/dist/transforms/toTypedArray.js +11 -6
  73. package/dist/transforms/toTypedArray.test.js +20 -11
  74. package/dist/types.d.ts +11 -9
  75. package/dist/utilities/index.d.ts +3 -3
  76. package/dist/utilities/index.js +3 -3
  77. package/dist/utilities/lazy.d.ts +1 -1
  78. package/dist/utilities/lazy.test.js +2 -2
  79. package/dist/utilities/reference.d.ts +1 -1
  80. package/dist/utilities/reference.test.js +2 -2
  81. package/dist/utilities/remember.d.ts +2 -2
  82. package/dist/utilities/remember.js +3 -3
  83. package/dist/utilities/remember.test.js +2 -2
  84. package/dist/utils.test.d.ts +2 -2
  85. package/dist/utils.test.js +2 -4
  86. package/dist/write.d.ts +2 -2
  87. package/dist/write.js +47 -34
  88. package/package.json +7 -2
  89. package/dist/datatypes/containers/sizedbytes.d.ts +0 -2
  90. package/dist/datatypes/containers/sizedbytes.js +0 -17
  91. package/dist/datatypes/containers/sizedbytes.test.d.ts +0 -1
  92. package/dist/datatypes/containers/sizedbytes.test.js +0 -27
  93. package/dist/datatypes/containers/string.d.ts +0 -2
  94. package/dist/datatypes/containers/string.js +0 -24
  95. package/dist/datatypes/containers/string.test.d.ts +0 -1
  96. package/dist/datatypes/containers/string.test.js +0 -24
  97. package/dist/datatypes/transforms/encode.d.ts +0 -2
  98. package/dist/datatypes/transforms/encode.js +0 -7
  99. package/dist/datatypes/transforms/fixedOffset.d.ts +0 -2
  100. package/dist/datatypes/transforms/fixedOffset.js +0 -18
  101. package/dist/datatypes/transforms/modify.d.ts +0 -2
  102. package/dist/datatypes/transforms/modify.js +0 -7
  103. package/dist/datatypes/transforms/noAdvance.d.ts +0 -16
  104. package/dist/datatypes/transforms/noAdvance.js +0 -30
  105. package/dist/datatypes/transforms/pipe.d.ts +0 -11
  106. package/dist/datatypes/transforms/pipe.js +0 -16
  107. package/dist/datatypes/utilities/remember.d.ts +0 -15
  108. package/dist/datatypes/utilities/remember.js +0 -36
  109. package/dist/datatypes/utilities/remember.test.d.ts +0 -1
  110. package/dist/datatypes/utilities/remember.test.js +0 -44
  111. package/dist/datatypes/utils.test.d.ts +0 -8
  112. package/dist/datatypes/utils.test.js +0 -37
  113. package/dist/datatypes/values/byteliteral.d.ts +0 -2
  114. package/dist/datatypes/values/byteliteral.js +0 -20
  115. package/dist/datatypes/values/json.d.ts +0 -10
  116. package/dist/datatypes/values/json.js +0 -21
  117. package/dist/transforms/fixedOffset.d.ts +0 -2
  118. package/dist/transforms/fixedOffset.js +0 -18
  119. package/dist/transforms/noAdvance.d.ts +0 -16
  120. package/dist/transforms/noAdvance.js +0 -30
  121. package/dist/transforms/noAdvance.test.d.ts +0 -1
  122. 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
- - Fast![benchmarks](./benchmark) show only 1.5-5x slower than ideal implementaiton
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 own Serializer
30
+ ## Implementing your datatype
31
31
 
32
32
  Implementing your own serializer is incredibly simple, heres the `f64` serializer for example
33
33
 
@@ -1,4 +1,4 @@
1
- import type { Serializer } from "../../types";
1
+ import type { Serializer } from "../../types.js";
2
2
  /**
3
3
  * `st.array` is a fixed length array, akin to a C array.
4
4
  *
@@ -7,31 +7,28 @@
7
7
  *
8
8
  */
9
9
  export function array(size, type) {
10
- const SHOW_ERRORS = size < 4096;
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 (typeSize)
18
- ctx.alloc(size * typeSize);
16
+ if (type.size)
17
+ ctx.reserve(size * type.size);
19
18
  for (let i = 0; i < size; i++) {
20
- if (SHOW_ERRORS)
21
- ctx.stack.push(`[${i}]`);
22
- writeType(ctx, value[i]);
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.stack.push(`[${i}]`);
32
- arr[i] = readType(ctx);
28
+ ctx.path.push(`[${i}]`);
29
+ arr[i] = type.read(ctx);
33
30
  if (SHOW_ERRORS)
34
- ctx.stack.pop();
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
  *
@@ -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++ < 4096;
15
+ const TRACK_STACK = i++ < 512;
16
16
  if (TRACK_STACK)
17
- ctx.stack.push(`[${arr.length - 1}]`);
17
+ ctx.path.push(`[${arr.length - 1}]`);
18
18
  arr.push(type.read(ctx));
19
19
  if (TRACK_STACK)
20
- ctx.stack.pop();
20
+ ctx.path.pop();
21
21
  }
22
22
  return arr;
23
23
  },
24
24
  write(ctx, value) {
25
- const TRACK_STACK = value.length < 4096;
25
+ const TRACK_STACK = value.length < 512;
26
26
  for (let i = 0; i < value.length; i++) {
27
27
  if (TRACK_STACK)
28
- ctx.stack.push(`[${i}]`);
28
+ ctx.path.push(`[${i}]`);
29
29
  type.write(ctx, value[i]);
30
30
  if (TRACK_STACK)
31
- ctx.stack.pop();
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,4 +1,4 @@
1
- import type { InferInput, Serializer } from "../../types";
1
+ import type { InferInput, Serializer } from "../../types.js";
2
2
  type InferObject<T> = T extends Record<string, Serializer<any>> ? {
3
3
  [Key in keyof T]: InferInput<T[Key]>;
4
4
  } : never;
@@ -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(),
@@ -1,4 +1,4 @@
1
- import type { Serializer } from "../../types";
1
+ import type { Serializer } from "../../types.js";
2
2
  /**
3
3
  * `st.list` is a dynamically sized array
4
4
  *
@@ -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 < 4096;
15
+ const TRACK_STACK = value.length < 1000;
16
16
  if (type.size) {
17
- ctx.alloc(value.length * type.size);
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.stack.push(`[${i}]`);
21
+ ctx.path.push(`[${i}]`);
22
22
  const v = value[i];
23
23
  type.write(ctx, v);
24
24
  if (TRACK_STACK)
25
- ctx.stack.pop();
25
+ ctx.path.pop();
26
26
  }
27
27
  },
28
28
  read: (ctx) => {
29
29
  const size = length.read(ctx);
30
- const TRACK_STACK = size < 4096;
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.stack.push(`[${i}]`);
34
+ ctx.path.push(`[${i}]`);
35
35
  arr[i] = type.read(ctx);
36
36
  if (TRACK_STACK)
37
- ctx.stack.pop();
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.alloc(size);
22
+ ctx.reserve(size);
23
23
  for (let i = 0; i < entires.length; i++) {
24
24
  const [key, serializer] = entires[i];
25
- ctx.stack.push(`.${key}`);
25
+ // ctx.path.push(`.${key}`);
26
26
  serializer.write(ctx, value[key]);
27
- ctx.stack.pop();
27
+ // ctx.path.pop();
28
28
  }
29
29
  },
30
30
  read: (ctx) => {
31
- const output = new Array(entires.length);
31
+ const output = {};
32
32
  for (let i = 0; i < entires.length; i++) {
33
33
  const [key, serializer] = entires[i];
34
- ctx.stack.push(`.${key}`);
35
- output[i] = [key, serializer.read(ctx)];
36
- ctx.stack.pop();
34
+ // ctx.path.push(`.${key}`);
35
+ //@ts-expect-error
36
+ output[key] = serializer.read(ctx);
37
+ // ctx.path.pop();
37
38
  }
38
- return Object.fromEntries(output);
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,4 +1,4 @@
1
- import type { InferInput, InferOutput, Serializer } from "../../types";
1
+ import type { InferInput, InferOutput, Serializer } from "../../types.js";
2
2
  /**
3
3
  * `st.taggedUnion` is a union type with a kind
4
4
  *
@@ -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";
@@ -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";
@@ -1,4 +1,4 @@
1
- import type { Serializer } from "../../types";
1
+ import type { Serializer } from "../../types.js";
2
2
  /**
3
3
  * 64bit unsigned interger
4
4
  *
@@ -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.alloc(8);
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.alloc(8);
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,4 +1,4 @@
1
- import type { Serializer } from "../../types";
1
+ import type { Serializer } from "../../types.js";
2
2
  /** 16bit float */
3
3
  export declare function f16(endian?: "little" | "big"): Serializer<number>;
4
4
  /** 32bit float */
@@ -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.alloc(2);
7
- ctx.view.setFloat16(ctx.offset, value, endian === "little");
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, endian === "little");
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.alloc(4);
23
- ctx.view.setFloat32(ctx.offset, value, endian === "little");
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, endian === "little");
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.alloc(8);
39
- ctx.view.setFloat64(ctx.offset, value, endian === "little");
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, endian === "little");
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) => {
@@ -1,4 +1,4 @@
1
- import type { Serializer } from "../../types";
1
+ import type { Serializer } from "../../types.js";
2
2
  /** 8bit signed integer */
3
3
  export declare function s8(): Serializer<number>;
4
4
  /** 16bit signed integer */
@@ -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.alloc(1);
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.alloc(2);
33
- ctx.view.setInt16(ctx.offset, value, endian === "little");
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, endian === "little");
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.alloc(4);
50
- ctx.view.setInt32(ctx.offset, value, endian === "little");
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, endian === "little");
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.alloc(8);
67
- ctx.view.setBigInt64(ctx.offset, BigInt(value), endian === "little");
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, endian === "little"));
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, () => {
@@ -1,4 +1,4 @@
1
- import type { Serializer } from "../../types";
1
+ import type { Serializer } from "../../types.js";
2
2
  /** 8bit unsigned integer */
3
3
  export declare function u8(): Serializer<number>;
4
4
  /** 16bit unsigned integer */
@@ -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.alloc(1);
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.alloc(2);
31
- ctx.view.setUint16(ctx.offset, value, endian === "little");
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, endian === "little");
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.alloc(4);
51
- ctx.view.setUint32(ctx.offset, value, endian === "little");
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, endian === "little");
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.alloc(8);
71
- ctx.view.setBigUint64(ctx.offset, BigInt(value), endian === "little");
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, endian === "little"));
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, () => {