@yume-chan/struct 0.0.13 → 0.0.16

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 (69) hide show
  1. package/CHANGELOG.json +33 -0
  2. package/CHANGELOG.md +21 -1
  3. package/LICENSE +21 -21
  4. package/README.md +767 -769
  5. package/esm/basic/definition.d.ts +3 -3
  6. package/esm/basic/definition.d.ts.map +1 -1
  7. package/esm/basic/definition.js +0 -1
  8. package/esm/basic/definition.js.map +1 -1
  9. package/esm/basic/field-value.d.ts +1 -0
  10. package/esm/basic/field-value.d.ts.map +1 -1
  11. package/esm/basic/field-value.js +4 -0
  12. package/esm/basic/field-value.js.map +1 -1
  13. package/esm/basic/stream.d.ts +2 -1
  14. package/esm/basic/stream.d.ts.map +1 -1
  15. package/esm/basic/struct-value.d.ts +7 -5
  16. package/esm/basic/struct-value.d.ts.map +1 -1
  17. package/esm/basic/struct-value.js +30 -14
  18. package/esm/basic/struct-value.js.map +1 -1
  19. package/esm/struct.d.ts +11 -11
  20. package/esm/struct.d.ts.map +1 -1
  21. package/esm/struct.js +55 -41
  22. package/esm/struct.js.map +1 -1
  23. package/esm/sync-promise.d.ts +13 -0
  24. package/esm/sync-promise.d.ts.map +1 -0
  25. package/esm/sync-promise.js +71 -0
  26. package/esm/sync-promise.js.map +1 -0
  27. package/esm/types/bigint.d.ts.map +1 -1
  28. package/esm/types/bigint.js +7 -5
  29. package/esm/types/bigint.js.map +1 -1
  30. package/esm/types/buffer/base.d.ts +4 -3
  31. package/esm/types/buffer/base.d.ts.map +1 -1
  32. package/esm/types/buffer/base.js +9 -7
  33. package/esm/types/buffer/base.js.map +1 -1
  34. package/esm/types/buffer/fixed-length.d.ts +1 -1
  35. package/esm/types/buffer/fixed-length.d.ts.map +1 -1
  36. package/esm/types/buffer/fixed-length.js.map +1 -1
  37. package/esm/types/buffer/variable-length.d.ts +2 -2
  38. package/esm/types/buffer/variable-length.d.ts.map +1 -1
  39. package/esm/types/buffer/variable-length.js.map +1 -1
  40. package/esm/types/number.d.ts +6 -5
  41. package/esm/types/number.d.ts.map +1 -1
  42. package/esm/types/number.js +25 -14
  43. package/esm/types/number.js.map +1 -1
  44. package/package.json +11 -11
  45. package/src/basic/definition.ts +68 -70
  46. package/src/basic/field-value.ts +72 -67
  47. package/src/basic/index.ts +5 -5
  48. package/src/basic/options.ts +19 -19
  49. package/src/basic/stream.ts +21 -19
  50. package/src/basic/struct-value.ts +61 -39
  51. package/src/index.ts +17 -17
  52. package/src/struct.ts +632 -609
  53. package/src/sync-promise.ts +114 -0
  54. package/src/types/bigint.ts +103 -102
  55. package/src/types/buffer/base.ts +179 -176
  56. package/src/types/buffer/fixed-length.ts +20 -17
  57. package/src/types/buffer/index.ts +3 -3
  58. package/src/types/buffer/variable-length.ts +158 -156
  59. package/src/types/index.ts +3 -3
  60. package/src/types/number.ts +127 -115
  61. package/src/utils.ts +70 -70
  62. package/tsconfig.build.json +3 -0
  63. package/tsconfig.build.tsbuildinfo +1 -0
  64. package/tsconfig.test.json +8 -0
  65. package/esm/syncbird.d.ts +0 -65
  66. package/esm/syncbird.d.ts.map +0 -1
  67. package/esm/syncbird.js +0 -38
  68. package/esm/syncbird.js.map +0 -1
  69. package/src/syncbird.ts +0 -131
@@ -1,70 +1,68 @@
1
- // cspell: ignore Syncbird
2
-
3
- import type { StructAsyncDeserializeStream, StructDeserializeStream } from "./stream.js";
4
- import type { StructFieldValue } from "./field-value.js";
5
- import type { StructValue } from "./struct-value.js";
6
- import type { StructOptions } from "./options.js";
7
-
8
- /**
9
- * A field definition defines how to deserialize a field.
10
- *
11
- * @template TOptions TypeScript type of this definition's `options`.
12
- * @template TValue TypeScript type of this field.
13
- * @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
14
- */
15
- export abstract class StructFieldDefinition<
16
- TOptions = void,
17
- TValue = unknown,
18
- TOmitInitKey extends PropertyKey = never,
19
- > {
20
- /**
21
- * When `T` is a type initiated `StructFieldDefinition`,
22
- * use `T['TValue']` to retrieve its `TValue` type parameter.
23
- */
24
- public readonly TValue!: TValue;
25
-
26
- /**
27
- * When `T` is a type initiated `StructFieldDefinition`,
28
- * use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
29
- */
30
- public readonly TOmitInitKey!: TOmitInitKey;
31
-
32
- public readonly options: TOptions;
33
-
34
- public constructor(options: TOptions) {
35
- this.options = options;
36
- }
37
-
38
- /**
39
- * When implemented in derived classes, returns the size (or minimal size if it's dynamic) of this field.
40
- *
41
- * Actual size can be retrieved from `StructFieldValue#getSize`
42
- */
43
- public abstract getSize(): number;
44
-
45
- /**
46
- * When implemented in derived classes, creates a `StructFieldValue` from a given `value`.
47
- */
48
- public abstract create(
49
- options: Readonly<StructOptions>,
50
- struct: StructValue,
51
- value: TValue,
52
- ): StructFieldValue<this>;
53
-
54
- /**
55
- * When implemented in derived classes,It must be synchronous (returns a value) or asynchronous (returns a `Promise`) depending
56
- * on the type of `stream`. reads and creates a `StructFieldValue` from `stream`.
57
- *
58
- * `Syncbird` can be used to make the implementation easier.
59
- */
60
- public abstract deserialize(
61
- options: Readonly<StructOptions>,
62
- stream: StructDeserializeStream,
63
- struct: StructValue,
64
- ): StructFieldValue<this>;
65
- public abstract deserialize(
66
- options: Readonly<StructOptions>,
67
- stream: StructAsyncDeserializeStream,
68
- struct: StructValue,
69
- ): Promise<StructFieldValue<this>>;
70
- }
1
+ import type { StructAsyncDeserializeStream, StructDeserializeStream } from "./stream.js";
2
+ import type { StructFieldValue } from "./field-value.js";
3
+ import type { StructValue } from "./struct-value.js";
4
+ import type { StructOptions } from "./options.js";
5
+
6
+ /**
7
+ * A field definition defines how to deserialize a field.
8
+ *
9
+ * @template TOptions TypeScript type of this definition's `options`.
10
+ * @template TValue TypeScript type of this field.
11
+ * @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
12
+ */
13
+ export abstract class StructFieldDefinition<
14
+ TOptions = void,
15
+ TValue = unknown,
16
+ TOmitInitKey extends PropertyKey = never,
17
+ > {
18
+ /**
19
+ * When `T` is a type initiated `StructFieldDefinition`,
20
+ * use `T['TValue']` to retrieve its `TValue` type parameter.
21
+ */
22
+ public readonly TValue!: TValue;
23
+
24
+ /**
25
+ * When `T` is a type initiated `StructFieldDefinition`,
26
+ * use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
27
+ */
28
+ public readonly TOmitInitKey!: TOmitInitKey;
29
+
30
+ public readonly options: TOptions;
31
+
32
+ public constructor(options: TOptions) {
33
+ this.options = options;
34
+ }
35
+
36
+ /**
37
+ * When implemented in derived classes, returns the size (or minimal size if it's dynamic) of this field.
38
+ *
39
+ * Actual size can be retrieved from `StructFieldValue#getSize`
40
+ */
41
+ public abstract getSize(): number;
42
+
43
+ /**
44
+ * When implemented in derived classes, creates a `StructFieldValue` from a given `value`.
45
+ */
46
+ public abstract create(
47
+ options: Readonly<StructOptions>,
48
+ structValue: StructValue,
49
+ value: TValue,
50
+ ): StructFieldValue<this>;
51
+
52
+ /**
53
+ * When implemented in derived classes,It must be synchronous (returns a value) or asynchronous (returns a `Promise`) depending
54
+ * on the type of `stream`. reads and creates a `StructFieldValue` from `stream`.
55
+ *
56
+ * `SyncPromise` can be used to simplify implementation.
57
+ */
58
+ public abstract deserialize(
59
+ options: Readonly<StructOptions>,
60
+ stream: StructDeserializeStream,
61
+ structValue: StructValue,
62
+ ): StructFieldValue<this>;
63
+ public abstract deserialize(
64
+ options: Readonly<StructOptions>,
65
+ stream: StructAsyncDeserializeStream,
66
+ struct: StructValue,
67
+ ): Promise<StructFieldValue<this>>;
68
+ }
@@ -1,67 +1,72 @@
1
- import type { StructFieldDefinition } from "./definition.js";
2
- import type { StructOptions } from "./options.js";
3
- import type { StructValue } from "./struct-value.js";
4
-
5
- /**
6
- * A field value defines how to serialize a field.
7
- *
8
- * It may contains extra metadata about the value which are essential or
9
- * helpful for the serialization process.
10
- */
11
- export abstract class StructFieldValue<
12
- TDefinition extends StructFieldDefinition<any, any, any> = StructFieldDefinition<any, any, any>
13
- > {
14
- /** Gets the definition associated with this runtime value */
15
- public readonly definition: TDefinition;
16
-
17
- /** Gets the options of the associated `Struct` */
18
- public readonly options: Readonly<StructOptions>;
19
-
20
- /** Gets the associated `Struct` instance */
21
- public readonly struct: StructValue;
22
-
23
- protected value: TDefinition['TValue'];
24
-
25
- public constructor(
26
- definition: TDefinition,
27
- options: Readonly<StructOptions>,
28
- struct: StructValue,
29
- value: TDefinition['TValue'],
30
- ) {
31
- this.definition = definition;
32
- this.options = options;
33
- this.struct = struct;
34
- this.value = value;
35
- }
36
-
37
- /**
38
- * Gets size of this field. By default, it returns its `definition`'s size.
39
- *
40
- * When overridden in derived classes, can have custom logic to calculate the actual size.
41
- */
42
- public getSize(): number {
43
- return this.definition.getSize();
44
- }
45
-
46
- /**
47
- * When implemented in derived classes, reads current field's value.
48
- */
49
- public get(): TDefinition['TValue'] {
50
- return this.value;
51
- }
52
-
53
- /**
54
- * When implemented in derived classes, updates current field's value.
55
- */
56
- public set(value: TDefinition['TValue']): void {
57
- this.value = value;
58
- }
59
-
60
- /**
61
- * When implemented in derived classes, serializes this field into `dataView` at `offset`
62
- */
63
- public abstract serialize(
64
- dataView: DataView,
65
- offset: number,
66
- ): void;
67
- }
1
+ import type { StructFieldDefinition } from "./definition.js";
2
+ import type { StructOptions } from "./options.js";
3
+ import type { StructValue } from "./struct-value.js";
4
+
5
+ /**
6
+ * A field value defines how to serialize a field.
7
+ *
8
+ * It may contains extra metadata about the value which are essential or
9
+ * helpful for the serialization process.
10
+ */
11
+ export abstract class StructFieldValue<
12
+ TDefinition extends StructFieldDefinition<any, any, any> = StructFieldDefinition<any, any, any>
13
+ > {
14
+ /** Gets the definition associated with this runtime value */
15
+ public readonly definition: TDefinition;
16
+
17
+ /** Gets the options of the associated `Struct` */
18
+ public readonly options: Readonly<StructOptions>;
19
+
20
+ /** Gets the associated `Struct` instance */
21
+ public readonly struct: StructValue;
22
+
23
+ public get hasCustomAccessors(): boolean {
24
+ return this.get !== StructFieldValue.prototype.get ||
25
+ this.set !== StructFieldValue.prototype.set;
26
+ }
27
+
28
+ protected value: TDefinition['TValue'];
29
+
30
+ public constructor(
31
+ definition: TDefinition,
32
+ options: Readonly<StructOptions>,
33
+ struct: StructValue,
34
+ value: TDefinition['TValue'],
35
+ ) {
36
+ this.definition = definition;
37
+ this.options = options;
38
+ this.struct = struct;
39
+ this.value = value;
40
+ }
41
+
42
+ /**
43
+ * Gets size of this field. By default, it returns its `definition`'s size.
44
+ *
45
+ * When overridden in derived classes, can have custom logic to calculate the actual size.
46
+ */
47
+ public getSize(): number {
48
+ return this.definition.getSize();
49
+ }
50
+
51
+ /**
52
+ * When implemented in derived classes, reads current field's value.
53
+ */
54
+ public get(): TDefinition['TValue'] {
55
+ return this.value;
56
+ }
57
+
58
+ /**
59
+ * When implemented in derived classes, updates current field's value.
60
+ */
61
+ public set(value: TDefinition['TValue']): void {
62
+ this.value = value;
63
+ }
64
+
65
+ /**
66
+ * When implemented in derived classes, serializes this field into `dataView` at `offset`
67
+ */
68
+ public abstract serialize(
69
+ dataView: DataView,
70
+ offset: number,
71
+ ): void;
72
+ }
@@ -1,5 +1,5 @@
1
- export * from './definition.js';
2
- export * from './field-value.js';
3
- export * from './options.js';
4
- export * from './stream.js';
5
- export * from './struct-value.js';
1
+ export * from './definition.js';
2
+ export * from './field-value.js';
3
+ export * from './options.js';
4
+ export * from './stream.js';
5
+ export * from './struct-value.js';
@@ -1,19 +1,19 @@
1
- export interface StructOptions {
2
- /**
3
- * Whether all multi-byte fields in this struct are little-endian encoded.
4
- *
5
- * @default false
6
- */
7
- littleEndian: boolean;
8
-
9
- // TODO: StructOptions: investigate whether this is necessary
10
- // I can't think about any other options which need to be struct wide.
11
- // Even endianness can be set on a per-field basis (because it's not meaningful
12
- // for some field types like `Uint8Array`, and very rarely, a struct may contain
13
- // mixed endianness).
14
- // It's just more common and a little more convenient to have it here.
15
- }
16
-
17
- export const StructDefaultOptions: Readonly<StructOptions> = {
18
- littleEndian: false,
19
- };
1
+ export interface StructOptions {
2
+ /**
3
+ * Whether all multi-byte fields in this struct are little-endian encoded.
4
+ *
5
+ * @default false
6
+ */
7
+ littleEndian: boolean;
8
+
9
+ // TODO: StructOptions: investigate whether this is necessary
10
+ // I can't think about any other options which need to be struct wide.
11
+ // Even endianness can be set on a per-field basis (because it's not meaningful
12
+ // for some field types like `Uint8Array`, and very rarely, a struct may contain
13
+ // mixed endianness).
14
+ // It's just more common and a little more convenient to have it here.
15
+ }
16
+
17
+ export const StructDefaultOptions: Readonly<StructOptions> = {
18
+ littleEndian: false,
19
+ };
@@ -1,19 +1,21 @@
1
- export interface StructDeserializeStream {
2
- /**
3
- * Read data from the underlying data source.
4
- *
5
- * The stream must return exactly `length` bytes or data. If that's not possible
6
- * (due to end of file or other error condition), it must throw an error.
7
- */
8
- read(length: number): Uint8Array;
9
- }
10
-
11
- export interface StructAsyncDeserializeStream {
12
- /**
13
- * Read data from the underlying data source.
14
- *
15
- * The stream must return exactly `length` bytes or data. If that's not possible
16
- * (due to end of file or other error condition), it must throw an error.
17
- */
18
- read(length: number): Promise<Uint8Array>;
19
- }
1
+ import type { ValueOrPromise } from "../utils.js";
2
+
3
+ export interface StructDeserializeStream {
4
+ /**
5
+ * Read data from the underlying data source.
6
+ *
7
+ * The stream must return exactly `length` bytes or data. If that's not possible
8
+ * (due to end of file or other error condition), it must throw an error.
9
+ */
10
+ read(length: number): Uint8Array;
11
+ }
12
+
13
+ export interface StructAsyncDeserializeStream {
14
+ /**
15
+ * Read data from the underlying data source.
16
+ *
17
+ * The stream must return exactly `length` bytes or data. If that's not possible
18
+ * (due to end of file or other error condition), it must throw an error.
19
+ */
20
+ read(length: number): ValueOrPromise<Uint8Array>;
21
+ }
@@ -1,39 +1,61 @@
1
- import type { StructFieldValue } from "./field-value.js";
2
-
3
- /**
4
- * A struct value is a map between keys in a struct and their field values.
5
- */
6
- export class StructValue {
7
- /** @internal */ readonly fieldValues: Record<PropertyKey, StructFieldValue> = {};
8
-
9
- /**
10
- * Gets the result struct value object
11
- */
12
- public readonly value: Record<PropertyKey, unknown> = {};
13
-
14
- /**
15
- * Sets a `StructFieldValue` for `key`
16
- *
17
- * @param key The field name
18
- * @param value The associated `StructFieldValue`
19
- */
20
- public set(key: PropertyKey, value: StructFieldValue): void {
21
- this.fieldValues[key] = value;
22
-
23
- Object.defineProperty(this.value, key, {
24
- configurable: true,
25
- enumerable: true,
26
- get() { return value.get(); },
27
- set(v) { value.set(v); },
28
- });
29
- }
30
-
31
- /**
32
- * Gets the `StructFieldValue` for `key`
33
- *
34
- * @param key The field name
35
- */
36
- public get(key: PropertyKey): StructFieldValue {
37
- return this.fieldValues[key]!;
38
- }
39
- }
1
+ import type { StructFieldValue } from "./field-value.js";
2
+
3
+ export const STRUCT_VALUE_SYMBOL = Symbol("struct-value");
4
+
5
+ /**
6
+ * A struct value is a map between keys in a struct and their field values.
7
+ */
8
+ export class StructValue {
9
+ /** @internal */ readonly fieldValues: Record<PropertyKey, StructFieldValue> = {};
10
+
11
+ /**
12
+ * Gets the result struct value object
13
+ */
14
+ public readonly value: Record<PropertyKey, unknown>;
15
+
16
+ public constructor(prototype: any) {
17
+ // PERF: `Object.create(extra)` is 50% faster
18
+ // than `Object.defineProperties(this.value, extra)`
19
+ this.value = Object.create(prototype);
20
+
21
+ // PERF: `Object.defineProperty` is slow
22
+ // but we need it to be non-enumerable
23
+ Object.defineProperty(
24
+ this.value,
25
+ STRUCT_VALUE_SYMBOL,
26
+ { enumerable: false, value: this }
27
+ );
28
+ }
29
+
30
+ /**
31
+ * Sets a `StructFieldValue` for `key`
32
+ *
33
+ * @param name The field name
34
+ * @param fieldValue The associated `StructFieldValue`
35
+ */
36
+ public set(name: PropertyKey, fieldValue: StructFieldValue): void {
37
+ this.fieldValues[name] = fieldValue;
38
+
39
+ // PERF: `Object.defineProperty` is slow
40
+ // use normal property when possible
41
+ if (fieldValue.hasCustomAccessors) {
42
+ Object.defineProperty(this.value, name, {
43
+ configurable: true,
44
+ enumerable: true,
45
+ get() { return fieldValue.get(); },
46
+ set(v) { fieldValue.set(v); },
47
+ });
48
+ } else {
49
+ this.value[name] = fieldValue.get();
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Gets the `StructFieldValue` for `key`
55
+ *
56
+ * @param name The field name
57
+ */
58
+ public get(name: PropertyKey): StructFieldValue {
59
+ return this.fieldValues[name]!;
60
+ }
61
+ }
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- declare global {
2
- interface ArrayBuffer {
3
- // Disallow assigning `Uint8Array` to `Arraybuffer`
4
- __brand: never;
5
- }
6
-
7
- interface SharedArrayBuffer {
8
- // Allow `SharedArrayBuffer` to be assigned to `ArrayBuffer`
9
- __brand: never;
10
- }
11
- }
12
-
13
- export * from './basic/index.js';
14
- export * from './struct.js';
15
- export { Struct as default } from './struct.js';
16
- export * from './types/index.js';
17
- export * from './utils.js';
1
+ declare global {
2
+ interface ArrayBuffer {
3
+ // Disallow assigning `Uint8Array` to `Arraybuffer`
4
+ __brand: never;
5
+ }
6
+
7
+ interface SharedArrayBuffer {
8
+ // Allow `SharedArrayBuffer` to be assigned to `ArrayBuffer`
9
+ __brand: never;
10
+ }
11
+ }
12
+
13
+ export * from './basic/index.js';
14
+ export * from './struct.js';
15
+ export { Struct as default } from './struct.js';
16
+ export * from './types/index.js';
17
+ export * from './utils.js';