@yume-chan/struct 0.0.15 → 0.0.17

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 (68) hide show
  1. package/CHANGELOG.json +27 -0
  2. package/CHANGELOG.md +16 -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 +56 -43
  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 +13 -14
  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 +23 -19
  50. package/src/basic/struct-value.ts +61 -39
  51. package/src/index.ts +17 -17
  52. package/src/struct.ts +631 -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/esm/syncbird.d.ts +0 -65
  65. package/esm/syncbird.d.ts.map +0 -1
  66. package/esm/syncbird.js +0 -38
  67. package/esm/syncbird.js.map +0 -1
  68. package/src/syncbird.ts +0 -131
@@ -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,23 @@
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
+ // TODO: allow over reading (returning a `Uint8Array`, an `offset` and a `length`) to avoid copying
4
+
5
+ export interface StructDeserializeStream {
6
+ /**
7
+ * Read data from the underlying data source.
8
+ *
9
+ * The stream must return exactly `length` bytes or data. If that's not possible
10
+ * (due to end of file or other error condition), it must throw an error.
11
+ */
12
+ read(length: number): Uint8Array;
13
+ }
14
+
15
+ export interface StructAsyncDeserializeStream {
16
+ /**
17
+ * Read data from the underlying data source.
18
+ *
19
+ * The stream must return exactly `length` bytes or data. If that's not possible
20
+ * (due to end of file or other error condition), it must throw an error.
21
+ */
22
+ read(length: number): ValueOrPromise<Uint8Array>;
23
+ }
@@ -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';