@yume-chan/struct 0.0.5 → 0.0.10

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 (121) hide show
  1. package/CHANGELOG.json +23 -0
  2. package/CHANGELOG.md +16 -0
  3. package/LICENSE +21 -21
  4. package/README.md +720 -643
  5. package/dts/basic/context.d.ts +10 -17
  6. package/dts/basic/context.d.ts.map +1 -1
  7. package/dts/basic/definition.d.ts +23 -23
  8. package/dts/basic/definition.d.ts.map +1 -1
  9. package/dts/basic/field-value.d.ts +38 -0
  10. package/dts/basic/field-value.d.ts.map +1 -0
  11. package/dts/basic/index.d.ts +2 -2
  12. package/dts/basic/index.d.ts.map +1 -1
  13. package/dts/basic/struct-value.d.ts +24 -0
  14. package/dts/basic/struct-value.d.ts.map +1 -0
  15. package/dts/index.d.ts +1 -0
  16. package/dts/index.d.ts.map +1 -1
  17. package/dts/struct.d.ts +44 -47
  18. package/dts/struct.d.ts.map +1 -1
  19. package/dts/syncbird.d.ts +20 -0
  20. package/dts/syncbird.d.ts.map +1 -0
  21. package/dts/types/array-buffer.d.ts +22 -20
  22. package/dts/types/array-buffer.d.ts.map +1 -1
  23. package/dts/types/fixed-length-array-buffer.d.ts +1 -3
  24. package/dts/types/fixed-length-array-buffer.d.ts.map +1 -1
  25. package/dts/types/index.d.ts +0 -1
  26. package/dts/types/index.d.ts.map +1 -1
  27. package/dts/types/number.d.ts +11 -10
  28. package/dts/types/number.d.ts.map +1 -1
  29. package/dts/types/variable-length-array-buffer.d.ts +21 -19
  30. package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
  31. package/dts/{types/utils.d.ts → utils.d.ts} +3 -0
  32. package/dts/utils.d.ts.map +1 -0
  33. package/esm/basic/context.js +1 -1
  34. package/esm/basic/context.js.map +1 -1
  35. package/esm/basic/definition.js +9 -11
  36. package/esm/basic/definition.js.map +1 -1
  37. package/esm/basic/field-value.js +35 -0
  38. package/esm/basic/field-value.js.map +1 -0
  39. package/esm/basic/index.js +2 -2
  40. package/esm/basic/index.js.map +1 -1
  41. package/esm/basic/struct-value.js +36 -0
  42. package/esm/basic/struct-value.js.map +1 -0
  43. package/esm/index.js +1 -0
  44. package/esm/index.js.map +1 -1
  45. package/esm/struct.js +103 -176
  46. package/esm/struct.js.map +1 -1
  47. package/esm/syncbird.js +25 -0
  48. package/esm/syncbird.js.map +1 -0
  49. package/esm/types/array-buffer.js +83 -107
  50. package/esm/types/array-buffer.js.map +1 -1
  51. package/esm/types/fixed-length-array-buffer.js +5 -15
  52. package/esm/types/fixed-length-array-buffer.js.map +1 -1
  53. package/esm/types/index.js +0 -1
  54. package/esm/types/index.js.map +1 -1
  55. package/esm/types/number.js +35 -53
  56. package/esm/types/number.js.map +1 -1
  57. package/esm/types/variable-length-array-buffer.js +57 -68
  58. package/esm/types/variable-length-array-buffer.js.map +1 -1
  59. package/esm/utils.js +17 -0
  60. package/esm/utils.js.map +1 -0
  61. package/package.json +47 -50
  62. package/src/basic/context.ts +32 -40
  63. package/src/basic/definition.ts +72 -69
  64. package/src/basic/{runtime-value.ts → field-value.ts} +67 -72
  65. package/src/basic/index.ts +4 -4
  66. package/src/basic/struct-value.ts +39 -0
  67. package/src/index.ts +5 -4
  68. package/src/struct.ts +595 -579
  69. package/src/syncbird.ts +53 -0
  70. package/src/types/array-buffer.ts +194 -171
  71. package/src/types/fixed-length-array-buffer.ts +17 -27
  72. package/src/types/index.ts +4 -5
  73. package/src/types/number.ts +114 -101
  74. package/src/types/variable-length-array-buffer.ts +142 -121
  75. package/src/{types/utils.ts → utils.ts} +62 -47
  76. package/cjs/basic/context.js +0 -7
  77. package/cjs/basic/context.js.map +0 -1
  78. package/cjs/basic/definition.js +0 -26
  79. package/cjs/basic/definition.js.map +0 -1
  80. package/cjs/basic/index.js +0 -8
  81. package/cjs/basic/index.js.map +0 -1
  82. package/cjs/basic/runtime-object.js +0 -34
  83. package/cjs/basic/runtime-object.js.map +0 -1
  84. package/cjs/basic/runtime-value.js +0 -41
  85. package/cjs/basic/runtime-value.js.map +0 -1
  86. package/cjs/index.js +0 -10
  87. package/cjs/index.js.map +0 -1
  88. package/cjs/struct.js +0 -259
  89. package/cjs/struct.js.map +0 -1
  90. package/cjs/types/array-buffer.js +0 -137
  91. package/cjs/types/array-buffer.js.map +0 -1
  92. package/cjs/types/fixed-length-array-buffer.js +0 -21
  93. package/cjs/types/fixed-length-array-buffer.js.map +0 -1
  94. package/cjs/types/index.js +0 -9
  95. package/cjs/types/index.js.map +0 -1
  96. package/cjs/types/number.js +0 -68
  97. package/cjs/types/number.js.map +0 -1
  98. package/cjs/types/utils.js +0 -11
  99. package/cjs/types/utils.js.map +0 -1
  100. package/cjs/types/variable-length-array-buffer.js +0 -85
  101. package/cjs/types/variable-length-array-buffer.js.map +0 -1
  102. package/dts/basic/runtime-object.d.ts +0 -17
  103. package/dts/basic/runtime-object.d.ts.map +0 -1
  104. package/dts/basic/runtime-value.d.ts +0 -39
  105. package/dts/basic/runtime-value.d.ts.map +0 -1
  106. package/dts/types/utils.d.ts.map +0 -1
  107. package/esm/basic/runtime-object.js +0 -28
  108. package/esm/basic/runtime-object.js.map +0 -1
  109. package/esm/basic/runtime-value.js +0 -38
  110. package/esm/basic/runtime-value.js.map +0 -1
  111. package/esm/types/utils.js +0 -7
  112. package/esm/types/utils.js.map +0 -1
  113. package/src/basic/context.spec.ts +0 -9
  114. package/src/basic/definition.spec.ts +0 -26
  115. package/src/basic/runtime-object.spec.ts +0 -38
  116. package/src/basic/runtime-object.ts +0 -36
  117. package/src/basic/runtime-value.spec.ts +0 -71
  118. package/src/types/array-buffer.spec.ts +0 -88
  119. package/src/types/number.spec.ts +0 -262
  120. package/tsconfig.cjs.json +0 -11
  121. package/tsconfig.esm.json +0 -15
@@ -1,69 +1,72 @@
1
- import type { StructDeserializationContext, StructOptions, StructSerializationContext } from './context';
2
- import type { FieldRuntimeValue } from './runtime-value';
3
-
4
- type ValueOrPromise<T> = T | Promise<T>;
5
-
6
- /**
7
- * A field definition is a bridge between its type and its runtime value.
8
- *
9
- * `Struct` record fields in a list of `FieldDefinition`s.
10
- *
11
- * When `Struct#create` or `Struct#deserialize` are called, each field's definition
12
- * crates its own type of `FieldRuntimeValue` to manage the field value in that `Struct` instance.
13
- *
14
- * One `FieldDefinition` can represents multiple similar types, just returns the corresponding
15
- * `FieldRuntimeValue` when `createValue` was called.
16
- *
17
- * @template TOptions TypeScript type of this definition's `options`.
18
- * @template TValueType TypeScript type of this field.
19
- * @template TOmitInit Optionally remove some fields from the init type. Should be a union of string literal types.
20
- */
21
- export abstract class FieldDefinition<
22
- TOptions = void,
23
- TValueType = unknown,
24
- TOmitInit = never,
25
- > {
26
- public readonly options: TOptions;
27
-
28
- /**
29
- * When `T` is a type initiated `FieldDefinition`,
30
- * use `T['valueType']` to retrieve its `TValueType` type parameter.
31
- */
32
- public readonly valueType!: TValueType;
33
-
34
- /**
35
- * When `T` is a type initiated `FieldDefinition`,
36
- * use `T['omitInitType']` to retrieve its `TOmitInit` type parameter.
37
- */
38
- public readonly omitInitType!: TOmitInit;
39
-
40
- public constructor(options: TOptions) {
41
- this.options = options;
42
- }
43
-
44
- /**
45
- * When implemented in derived classes, returns the size (or minimal size if it's dynamic) of this field.
46
- *
47
- * Actual size can be retrieved from `FieldRuntimeValue#getSize`
48
- */
49
- public abstract getSize(): number;
50
-
51
- /**
52
- * When implemented in derived classes, creates a `FieldRuntimeValue` by parsing the `context`.
53
- */
54
- public abstract deserialize(
55
- options: Readonly<StructOptions>,
56
- context: StructDeserializationContext,
57
- object: any,
58
- ): ValueOrPromise<FieldRuntimeValue<FieldDefinition<TOptions, TValueType, TOmitInit>>>;
59
-
60
- /**
61
- * When implemented in derived classes, creates a `FieldRuntimeValue` from a given `value`.
62
- */
63
- public abstract createValue(
64
- options: Readonly<StructOptions>,
65
- context: StructSerializationContext,
66
- object: any,
67
- value: TValueType,
68
- ): FieldRuntimeValue<FieldDefinition<TOptions, TValueType, TOmitInit>>;
69
- }
1
+ import type { StructAsyncDeserializeStream, StructDeserializeStream, StructOptions } from './context';
2
+ import type { StructFieldValue } from './field-value';
3
+ import type { StructValue } from './struct-value';
4
+
5
+ /**
6
+ * A field definition is a bridge between its type and its runtime value.
7
+ *
8
+ * `Struct` record fields in a list of `StructFieldDefinition`s.
9
+ *
10
+ * When `Struct#create` or `Struct#deserialize` are called, each field's definition
11
+ * crates its own type of `StructFieldValue` to manage the field value in that `Struct` instance.
12
+ *
13
+ * One `StructFieldDefinition` can represents multiple similar types, just returns the corresponding
14
+ * `StructFieldValue` when `createValue` was called.
15
+ *
16
+ * @template TOptions TypeScript type of this definition's `options`.
17
+ * @template TValue TypeScript type of this field.
18
+ * @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
19
+ */
20
+ export abstract class StructFieldDefinition<
21
+ TOptions = void,
22
+ TValue = unknown,
23
+ TOmitInitKey extends PropertyKey = never,
24
+ > {
25
+ /**
26
+ * When `T` is a type initiated `StructFieldDefinition`,
27
+ * use `T['TValue']` to retrieve its `TValue` type parameter.
28
+ */
29
+ public readonly TValue!: TValue;
30
+
31
+ /**
32
+ * When `T` is a type initiated `StructFieldDefinition`,
33
+ * use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
34
+ */
35
+ public readonly TOmitInitKey!: TOmitInitKey;
36
+
37
+ public readonly options: TOptions;
38
+
39
+ public constructor(options: TOptions) {
40
+ this.options = options;
41
+ }
42
+
43
+ /**
44
+ * When implemented in derived classes, returns the size (or minimal size if it's dynamic) of this field.
45
+ *
46
+ * Actual size can be retrieved from `StructFieldValue#getSize`
47
+ */
48
+ public abstract getSize(): number;
49
+
50
+ /**
51
+ * When implemented in derived classes, creates a `StructFieldValue` from a given `value`.
52
+ */
53
+ public abstract create(
54
+ options: Readonly<StructOptions>,
55
+ struct: StructValue,
56
+ value: TValue,
57
+ ): StructFieldValue<this>;
58
+
59
+ /**
60
+ * When implemented in derived classes, creates a `StructFieldValue` by parsing `context`.
61
+ */
62
+ public abstract deserialize(
63
+ options: Readonly<StructOptions>,
64
+ stream: StructDeserializeStream,
65
+ struct: StructValue,
66
+ ): StructFieldValue<this>;
67
+ public abstract deserialize(
68
+ options: Readonly<StructOptions>,
69
+ stream: StructAsyncDeserializeStream,
70
+ struct: StructValue,
71
+ ): Promise<StructFieldValue<this>>;
72
+ }
@@ -1,72 +1,67 @@
1
- import type { StructOptions, StructSerializationContext } from './context';
2
- import type { FieldDefinition } from './definition';
3
-
4
- /**
5
- * Field runtime value manages one field of one `Struct` instance.
6
- *
7
- * If one `FieldDefinition` needs to change other field's semantics
8
- * It can override other fields' `FieldRuntimeValue` in its own `FieldRuntimeValue`'s constructor
9
- */
10
- export abstract class FieldRuntimeValue<
11
- TDefinition extends FieldDefinition<any, any, any> = FieldDefinition<any, any, any>
12
- > {
13
- /** Gets the definition associated with this runtime value */
14
- public readonly definition: TDefinition;
15
-
16
- /** Gets the options of the associated `Struct` */
17
- public readonly options: Readonly<StructOptions>;
18
-
19
- /** Gets the serialization context of the associated `Struct` instance */
20
- public readonly context: StructSerializationContext;
21
-
22
- /** Gets the associated `Struct` instance */
23
- public readonly object: any;
24
-
25
- protected value: TDefinition['valueType'];
26
-
27
- public constructor(
28
- definition: TDefinition,
29
- options: Readonly<StructOptions>,
30
- context: StructSerializationContext,
31
- object: any,
32
- value: TDefinition['valueType'],
33
- ) {
34
- this.definition = definition;
35
- this.options = options;
36
- this.context = context;
37
- this.object = object;
38
- this.value = value;
39
- }
40
-
41
- /**
42
- * Gets the actual size of this field. By default, the return value of its `definition.getSize()`
43
- *
44
- * When overridden in derived classes, can have custom logic to calculate the actual size.
45
- */
46
- public getSize(): number {
47
- return this.definition.getSize();
48
- }
49
-
50
- /**
51
- * When implemented in derived classes, returns the current value of this field
52
- */
53
- public get(): TDefinition['valueType'] {
54
- return this.value;
55
- }
56
-
57
- /**
58
- * When implemented in derived classes, update the current value of this field
59
- */
60
- public set(value: TDefinition['valueType']): void {
61
- this.value = value;
62
- }
63
-
64
- /**
65
- * When implemented in derived classes, serializes this field into `dataView` at `offset`
66
- */
67
- public abstract serialize(
68
- dataView: DataView,
69
- offset: number,
70
- context: StructSerializationContext
71
- ): void;
72
- }
1
+ import type { StructOptions } from './context';
2
+ import type { StructFieldDefinition } from './definition';
3
+ import type { StructValue } from './struct-value';
4
+
5
+ /**
6
+ * Field runtime value manages one field of one `Struct` instance.
7
+ *
8
+ * If one `StructFieldDefinition` needs to change other field's semantics
9
+ * It can override other fields' `StructFieldValue` in its own `StructFieldValue`'s constructor
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, returns the current value of this field
48
+ */
49
+ public get(): TDefinition['TValue'] {
50
+ return this.value;
51
+ }
52
+
53
+ /**
54
+ * When implemented in derived classes, update the current value of this field
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,4 +1,4 @@
1
- export * from './context';
2
- export * from './definition';
3
- export * from './runtime-value';
4
- export * from './runtime-object';
1
+ export * from './context';
2
+ export * from './definition';
3
+ export * from './field-value';
4
+ export * from './struct-value';
@@ -0,0 +1,39 @@
1
+ import type { StructFieldValue } from './field-value';
2
+
3
+ /**
4
+ * Manages the initialization process of a struct value
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 a previously `StructFieldValue` for `key`
33
+ *
34
+ * @param key The field name
35
+ */
36
+ public get(key: PropertyKey): StructFieldValue {
37
+ return this.fieldValues[key];
38
+ }
39
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- export * from './basic';
2
- export * from './struct';
3
- export { Struct as default } from './struct';
4
- export * from './types';
1
+ export * from './basic';
2
+ export * from './struct';
3
+ export { Struct as default } from './struct';
4
+ export * from './types';
5
+ export * from './utils';