@yume-chan/struct 0.0.9 → 0.0.13

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 (151) hide show
  1. package/CHANGELOG.json +65 -0
  2. package/CHANGELOG.md +39 -0
  3. package/LICENSE +21 -21
  4. package/README.md +769 -709
  5. package/esm/basic/definition.d.ts +44 -0
  6. package/esm/basic/definition.d.ts.map +1 -0
  7. package/esm/basic/definition.js +13 -9
  8. package/esm/basic/definition.js.map +1 -1
  9. package/esm/basic/field-value.d.ts +38 -0
  10. package/esm/basic/field-value.d.ts.map +1 -0
  11. package/esm/basic/field-value.js +13 -7
  12. package/esm/basic/field-value.js.map +1 -1
  13. package/esm/basic/index.d.ts +6 -0
  14. package/esm/basic/index.d.ts.map +1 -0
  15. package/esm/basic/index.js +5 -4
  16. package/esm/basic/index.js.map +1 -1
  17. package/esm/basic/options.d.ts +10 -0
  18. package/esm/basic/options.d.ts.map +1 -0
  19. package/esm/basic/{context.js → options.js} +1 -1
  20. package/esm/basic/options.js.map +1 -0
  21. package/esm/basic/stream.d.ts +19 -0
  22. package/esm/basic/stream.d.ts.map +1 -0
  23. package/esm/basic/stream.js +2 -0
  24. package/esm/basic/stream.js.map +1 -0
  25. package/{dts → esm}/basic/struct-value.d.ts +3 -3
  26. package/esm/basic/struct-value.d.ts.map +1 -0
  27. package/esm/basic/struct-value.js +7 -15
  28. package/esm/basic/struct-value.js.map +1 -1
  29. package/esm/index.d.ts +14 -0
  30. package/esm/index.d.ts.map +1 -0
  31. package/esm/index.js +5 -5
  32. package/esm/index.js.map +1 -1
  33. package/{dts → esm}/struct.d.ts +25 -22
  34. package/esm/struct.d.ts.map +1 -0
  35. package/esm/struct.js +75 -52
  36. package/esm/struct.js.map +1 -1
  37. package/esm/syncbird.d.ts +65 -0
  38. package/esm/syncbird.d.ts.map +1 -0
  39. package/esm/syncbird.js +38 -0
  40. package/esm/syncbird.js.map +1 -0
  41. package/esm/types/bigint.d.ts +25 -0
  42. package/esm/types/bigint.d.ts.map +1 -0
  43. package/esm/types/bigint.js +45 -0
  44. package/esm/types/bigint.js.map +1 -0
  45. package/esm/types/buffer/base.d.ts +63 -0
  46. package/esm/types/buffer/base.d.ts.map +1 -0
  47. package/esm/types/buffer/base.js +100 -0
  48. package/esm/types/buffer/base.js.map +1 -0
  49. package/esm/types/buffer/fixed-length.d.ts +8 -0
  50. package/esm/types/buffer/fixed-length.d.ts.map +1 -0
  51. package/esm/types/buffer/fixed-length.js +8 -0
  52. package/esm/types/buffer/fixed-length.js.map +1 -0
  53. package/esm/types/buffer/index.d.ts +4 -0
  54. package/esm/types/buffer/index.d.ts.map +1 -0
  55. package/esm/types/buffer/index.js +4 -0
  56. package/esm/types/buffer/index.js.map +1 -0
  57. package/esm/types/buffer/variable-length.d.ts +43 -0
  58. package/esm/types/buffer/variable-length.d.ts.map +1 -0
  59. package/esm/types/buffer/variable-length.js +76 -0
  60. package/esm/types/buffer/variable-length.js.map +1 -0
  61. package/esm/types/index.d.ts +4 -0
  62. package/esm/types/index.d.ts.map +1 -0
  63. package/esm/types/index.js +3 -4
  64. package/esm/types/index.js.map +1 -1
  65. package/esm/types/number.d.ts +32 -0
  66. package/esm/types/number.d.ts.map +1 -0
  67. package/esm/types/number.js +26 -18
  68. package/esm/types/number.js.map +1 -1
  69. package/{dts → esm}/utils.d.ts +3 -2
  70. package/{dts → esm}/utils.d.ts.map +1 -1
  71. package/esm/utils.js +16 -0
  72. package/esm/utils.js.map +1 -1
  73. package/package.json +46 -49
  74. package/src/basic/definition.ts +70 -69
  75. package/src/basic/field-value.ts +67 -73
  76. package/src/basic/index.ts +5 -4
  77. package/src/basic/options.ts +19 -0
  78. package/src/basic/stream.ts +19 -0
  79. package/src/basic/struct-value.ts +39 -45
  80. package/src/index.ts +17 -5
  81. package/src/struct.ts +609 -575
  82. package/src/syncbird.ts +131 -0
  83. package/src/types/bigint.ts +102 -0
  84. package/src/types/buffer/base.ts +176 -0
  85. package/src/types/buffer/fixed-length.ts +17 -0
  86. package/src/types/buffer/index.ts +3 -0
  87. package/src/types/buffer/variable-length.ts +156 -0
  88. package/src/types/index.ts +3 -4
  89. package/src/types/number.ts +115 -100
  90. package/src/utils.ts +70 -51
  91. package/cjs/basic/context.js +0 -7
  92. package/cjs/basic/context.js.map +0 -1
  93. package/cjs/basic/definition.js +0 -25
  94. package/cjs/basic/definition.js.map +0 -1
  95. package/cjs/basic/field-value.js +0 -40
  96. package/cjs/basic/field-value.js.map +0 -1
  97. package/cjs/basic/index.js +0 -8
  98. package/cjs/basic/index.js.map +0 -1
  99. package/cjs/basic/struct-value.js +0 -46
  100. package/cjs/basic/struct-value.js.map +0 -1
  101. package/cjs/index.js +0 -11
  102. package/cjs/index.js.map +0 -1
  103. package/cjs/struct.js +0 -177
  104. package/cjs/struct.js.map +0 -1
  105. package/cjs/types/array-buffer.js +0 -118
  106. package/cjs/types/array-buffer.js.map +0 -1
  107. package/cjs/types/fixed-length-array-buffer.js +0 -12
  108. package/cjs/types/fixed-length-array-buffer.js.map +0 -1
  109. package/cjs/types/index.js +0 -8
  110. package/cjs/types/index.js.map +0 -1
  111. package/cjs/types/number.js +0 -52
  112. package/cjs/types/number.js.map +0 -1
  113. package/cjs/types/variable-length-array-buffer.js +0 -77
  114. package/cjs/types/variable-length-array-buffer.js.map +0 -1
  115. package/cjs/utils.js +0 -11
  116. package/cjs/utils.js.map +0 -1
  117. package/dts/basic/context.d.ts +0 -36
  118. package/dts/basic/context.d.ts.map +0 -1
  119. package/dts/basic/definition.d.ts +0 -48
  120. package/dts/basic/definition.d.ts.map +0 -1
  121. package/dts/basic/field-value.d.ts +0 -40
  122. package/dts/basic/field-value.d.ts.map +0 -1
  123. package/dts/basic/index.d.ts +0 -5
  124. package/dts/basic/index.d.ts.map +0 -1
  125. package/dts/basic/struct-value.d.ts.map +0 -1
  126. package/dts/index.d.ts +0 -6
  127. package/dts/index.d.ts.map +0 -1
  128. package/dts/struct.d.ts.map +0 -1
  129. package/dts/types/array-buffer.d.ts +0 -70
  130. package/dts/types/array-buffer.d.ts.map +0 -1
  131. package/dts/types/fixed-length-array-buffer.d.ts +0 -8
  132. package/dts/types/fixed-length-array-buffer.d.ts.map +0 -1
  133. package/dts/types/index.d.ts +0 -5
  134. package/dts/types/index.d.ts.map +0 -1
  135. package/dts/types/number.d.ts +0 -33
  136. package/dts/types/number.d.ts.map +0 -1
  137. package/dts/types/variable-length-array-buffer.d.ts +0 -32
  138. package/dts/types/variable-length-array-buffer.d.ts.map +0 -1
  139. package/esm/basic/context.js.map +0 -1
  140. package/esm/types/array-buffer.js +0 -109
  141. package/esm/types/array-buffer.js.map +0 -1
  142. package/esm/types/fixed-length-array-buffer.js +0 -8
  143. package/esm/types/fixed-length-array-buffer.js.map +0 -1
  144. package/esm/types/variable-length-array-buffer.js +0 -71
  145. package/esm/types/variable-length-array-buffer.js.map +0 -1
  146. package/src/basic/context.ts +0 -42
  147. package/src/types/array-buffer.ts +0 -184
  148. package/src/types/fixed-length-array-buffer.ts +0 -17
  149. package/src/types/variable-length-array-buffer.ts +0 -145
  150. package/tsconfig.cjs.json +0 -11
  151. package/tsconfig.esm.json +0 -15
@@ -1,69 +1,70 @@
1
- import type { ValueOrPromise } from '../utils';
2
- import type { StructDeserializationContext, StructOptions, StructSerializationContext } from './context';
3
- import type { StructFieldValue } from './field-value';
4
- import type { StructValue } from './struct-value';
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 `StructFieldDefinition`s.
10
- *
11
- * When `Struct#create` or `Struct#deserialize` are called, each field's definition
12
- * crates its own type of `StructFieldValue` to manage the field value in that `Struct` instance.
13
- *
14
- * One `StructFieldDefinition` can represents multiple similar types, just returns the corresponding
15
- * `StructFieldValue` when `createValue` was called.
16
- *
17
- * @template TOptions TypeScript type of this definition's `options`.
18
- * @template TValue TypeScript type of this field.
19
- * @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
20
- */
21
- export abstract class StructFieldDefinition<
22
- TOptions = void,
23
- TValue = unknown,
24
- TOmitInitKey extends PropertyKey = never,
25
- > {
26
- /**
27
- * When `T` is a type initiated `StructFieldDefinition`,
28
- * use `T['TValue']` to retrieve its `TValue` type parameter.
29
- */
30
- public readonly TValue!: TValue;
31
-
32
- /**
33
- * When `T` is a type initiated `StructFieldDefinition`,
34
- * use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
35
- */
36
- public readonly TOmitInitKey!: TOmitInitKey;
37
-
38
- public readonly options: TOptions;
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 `StructFieldValue#getSize`
48
- */
49
- public abstract getSize(): number;
50
-
51
- /**
52
- * When implemented in derived classes, creates a `StructFieldValue` from a given `value`.
53
- */
54
- public abstract create(
55
- options: Readonly<StructOptions>,
56
- context: StructSerializationContext,
57
- struct: StructValue,
58
- value: TValue,
59
- ): StructFieldValue<this>;
60
-
61
- /**
62
- * When implemented in derived classes, creates a `StructFieldValue` by parsing `context`.
63
- */
64
- public abstract deserialize(
65
- options: Readonly<StructOptions>,
66
- context: StructDeserializationContext,
67
- struct: StructValue,
68
- ): ValueOrPromise<StructFieldValue<this>>;
69
- }
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,73 +1,67 @@
1
- import type { StructOptions, StructSerializationContext } 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 serialization context of the associated `Struct` instance */
21
- public readonly context: StructSerializationContext;
22
-
23
- /** Gets the associated `Struct` instance */
24
- public readonly struct: StructValue;
25
-
26
- protected value: TDefinition['TValue'];
27
-
28
- public constructor(
29
- definition: TDefinition,
30
- options: Readonly<StructOptions>,
31
- context: StructSerializationContext,
32
- struct: StructValue,
33
- value: TDefinition['TValue'],
34
- ) {
35
- this.definition = definition;
36
- this.options = options;
37
- this.context = context;
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, returns the current value of this field
53
- */
54
- public get(): TDefinition['TValue'] {
55
- return this.value;
56
- }
57
-
58
- /**
59
- * When implemented in derived classes, update the current value of this field
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
- context: StructSerializationContext
72
- ): void;
73
- }
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,4 +1,5 @@
1
- export * from './context';
2
- export * from './definition';
3
- export * from './field-value';
4
- export * from './struct-value';
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';
@@ -0,0 +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
+ };
@@ -0,0 +1,19 @@
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,45 +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
- // TODO: TypeScript 4.2 will allow this behavior
22
- // https://github.com/microsoft/TypeScript/pull/26797
23
- // @ts-expect-error Type 'symbol' cannot be used as an index type. ts(2538)
24
- this.fieldValues[key] = value;
25
-
26
- Object.defineProperty(this.value, key, {
27
- configurable: true,
28
- enumerable: true,
29
- get() { return value.get(); },
30
- set(v) { value.set(v); },
31
- });
32
- }
33
-
34
- /**
35
- * Gets a previously `StructFieldValue` for `key`
36
- *
37
- * @param key The field name
38
- */
39
- public get(key: PropertyKey): StructFieldValue {
40
- // TODO: TypeScript 4.2 will allow this behavior
41
- // https://github.com/microsoft/TypeScript/pull/26797
42
- // @ts-expect-error Type 'symbol' cannot be used as an index type. ts(2538)
43
- return this.fieldValues[key];
44
- }
45
- }
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
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,17 @@
1
- export * from './basic';
2
- export * from './struct';
3
- export { Struct as default } from './struct';
4
- export * from './types';
5
- export * from './utils';
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';