@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.
- package/CHANGELOG.json +23 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -21
- package/README.md +720 -643
- package/dts/basic/context.d.ts +10 -17
- package/dts/basic/context.d.ts.map +1 -1
- package/dts/basic/definition.d.ts +23 -23
- package/dts/basic/definition.d.ts.map +1 -1
- package/dts/basic/field-value.d.ts +38 -0
- package/dts/basic/field-value.d.ts.map +1 -0
- package/dts/basic/index.d.ts +2 -2
- package/dts/basic/index.d.ts.map +1 -1
- package/dts/basic/struct-value.d.ts +24 -0
- package/dts/basic/struct-value.d.ts.map +1 -0
- package/dts/index.d.ts +1 -0
- package/dts/index.d.ts.map +1 -1
- package/dts/struct.d.ts +44 -47
- package/dts/struct.d.ts.map +1 -1
- package/dts/syncbird.d.ts +20 -0
- package/dts/syncbird.d.ts.map +1 -0
- package/dts/types/array-buffer.d.ts +22 -20
- package/dts/types/array-buffer.d.ts.map +1 -1
- package/dts/types/fixed-length-array-buffer.d.ts +1 -3
- package/dts/types/fixed-length-array-buffer.d.ts.map +1 -1
- package/dts/types/index.d.ts +0 -1
- package/dts/types/index.d.ts.map +1 -1
- package/dts/types/number.d.ts +11 -10
- package/dts/types/number.d.ts.map +1 -1
- package/dts/types/variable-length-array-buffer.d.ts +21 -19
- package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
- package/dts/{types/utils.d.ts → utils.d.ts} +3 -0
- package/dts/utils.d.ts.map +1 -0
- package/esm/basic/context.js +1 -1
- package/esm/basic/context.js.map +1 -1
- package/esm/basic/definition.js +9 -11
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.js +35 -0
- package/esm/basic/field-value.js.map +1 -0
- package/esm/basic/index.js +2 -2
- package/esm/basic/index.js.map +1 -1
- package/esm/basic/struct-value.js +36 -0
- package/esm/basic/struct-value.js.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/struct.js +103 -176
- package/esm/struct.js.map +1 -1
- package/esm/syncbird.js +25 -0
- package/esm/syncbird.js.map +1 -0
- package/esm/types/array-buffer.js +83 -107
- package/esm/types/array-buffer.js.map +1 -1
- package/esm/types/fixed-length-array-buffer.js +5 -15
- package/esm/types/fixed-length-array-buffer.js.map +1 -1
- package/esm/types/index.js +0 -1
- package/esm/types/index.js.map +1 -1
- package/esm/types/number.js +35 -53
- package/esm/types/number.js.map +1 -1
- package/esm/types/variable-length-array-buffer.js +57 -68
- package/esm/types/variable-length-array-buffer.js.map +1 -1
- package/esm/utils.js +17 -0
- package/esm/utils.js.map +1 -0
- package/package.json +47 -50
- package/src/basic/context.ts +32 -40
- package/src/basic/definition.ts +72 -69
- package/src/basic/{runtime-value.ts → field-value.ts} +67 -72
- package/src/basic/index.ts +4 -4
- package/src/basic/struct-value.ts +39 -0
- package/src/index.ts +5 -4
- package/src/struct.ts +595 -579
- package/src/syncbird.ts +53 -0
- package/src/types/array-buffer.ts +194 -171
- package/src/types/fixed-length-array-buffer.ts +17 -27
- package/src/types/index.ts +4 -5
- package/src/types/number.ts +114 -101
- package/src/types/variable-length-array-buffer.ts +142 -121
- package/src/{types/utils.ts → utils.ts} +62 -47
- package/cjs/basic/context.js +0 -7
- package/cjs/basic/context.js.map +0 -1
- package/cjs/basic/definition.js +0 -26
- package/cjs/basic/definition.js.map +0 -1
- package/cjs/basic/index.js +0 -8
- package/cjs/basic/index.js.map +0 -1
- package/cjs/basic/runtime-object.js +0 -34
- package/cjs/basic/runtime-object.js.map +0 -1
- package/cjs/basic/runtime-value.js +0 -41
- package/cjs/basic/runtime-value.js.map +0 -1
- package/cjs/index.js +0 -10
- package/cjs/index.js.map +0 -1
- package/cjs/struct.js +0 -259
- package/cjs/struct.js.map +0 -1
- package/cjs/types/array-buffer.js +0 -137
- package/cjs/types/array-buffer.js.map +0 -1
- package/cjs/types/fixed-length-array-buffer.js +0 -21
- package/cjs/types/fixed-length-array-buffer.js.map +0 -1
- package/cjs/types/index.js +0 -9
- package/cjs/types/index.js.map +0 -1
- package/cjs/types/number.js +0 -68
- package/cjs/types/number.js.map +0 -1
- package/cjs/types/utils.js +0 -11
- package/cjs/types/utils.js.map +0 -1
- package/cjs/types/variable-length-array-buffer.js +0 -85
- package/cjs/types/variable-length-array-buffer.js.map +0 -1
- package/dts/basic/runtime-object.d.ts +0 -17
- package/dts/basic/runtime-object.d.ts.map +0 -1
- package/dts/basic/runtime-value.d.ts +0 -39
- package/dts/basic/runtime-value.d.ts.map +0 -1
- package/dts/types/utils.d.ts.map +0 -1
- package/esm/basic/runtime-object.js +0 -28
- package/esm/basic/runtime-object.js.map +0 -1
- package/esm/basic/runtime-value.js +0 -38
- package/esm/basic/runtime-value.js.map +0 -1
- package/esm/types/utils.js +0 -7
- package/esm/types/utils.js.map +0 -1
- package/src/basic/context.spec.ts +0 -9
- package/src/basic/definition.spec.ts +0 -26
- package/src/basic/runtime-object.spec.ts +0 -38
- package/src/basic/runtime-object.ts +0 -36
- package/src/basic/runtime-value.spec.ts +0 -71
- package/src/types/array-buffer.spec.ts +0 -88
- package/src/types/number.spec.ts +0 -262
- package/tsconfig.cjs.json +0 -11
- package/tsconfig.esm.json +0 -15
package/src/basic/definition.ts
CHANGED
|
@@ -1,69 +1,72 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @template
|
|
18
|
-
* @template
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
}
|
package/src/basic/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './context';
|
|
2
|
-
export * from './definition';
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
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';
|