@yume-chan/struct 0.0.15 → 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 +21 -0
  2. package/CHANGELOG.md +11 -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
@@ -0,0 +1,114 @@
1
+ export interface SyncPromise<T> {
2
+ then<TResult1 = T, TResult2 = never>(
3
+ onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
4
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
5
+ ): SyncPromise<TResult1 | TResult2>;
6
+
7
+ valueOrPromise(): T | PromiseLike<T>;
8
+ }
9
+
10
+ interface SyncPromiseStatic {
11
+ reject<T = never>(reason?: any): SyncPromise<T>;
12
+
13
+ resolve(): SyncPromise<void>;
14
+ resolve<T>(value: T | PromiseLike<T>): SyncPromise<T>;
15
+
16
+ try<T>(executor: () => T | PromiseLike<T>): SyncPromise<T>;
17
+ }
18
+
19
+ export const SyncPromise: SyncPromiseStatic = {
20
+ reject<T = never>(reason?: any): SyncPromise<T> {
21
+ return new RejectedSyncPromise(reason);
22
+ },
23
+ resolve<T>(value?: T | PromiseLike<T>): SyncPromise<T> {
24
+ if (
25
+ typeof value === 'object' &&
26
+ value !== null &&
27
+ typeof (value as PromiseLike<T>).then === 'function'
28
+ ) {
29
+ if (
30
+ value instanceof PendingSyncPromise ||
31
+ value instanceof ResolvedSyncPromise ||
32
+ value instanceof RejectedSyncPromise
33
+ ) {
34
+ return value;
35
+ }
36
+
37
+ return new PendingSyncPromise(value as PromiseLike<T>);
38
+ } else {
39
+ return new ResolvedSyncPromise(value as T);
40
+ }
41
+ },
42
+ try<T>(executor: () => T | PromiseLike<T>): SyncPromise<T> {
43
+ try {
44
+ return SyncPromise.resolve(executor());
45
+ } catch (e) {
46
+ return SyncPromise.reject(e);
47
+ }
48
+ }
49
+ };
50
+
51
+ class PendingSyncPromise<T> implements SyncPromise<T> {
52
+ private promise: PromiseLike<T>;
53
+
54
+ public constructor(promise: PromiseLike<T>) {
55
+ this.promise = promise;
56
+ }
57
+
58
+ public then<TResult1 = T, TResult2 = never>(
59
+ onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
60
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
61
+ ) {
62
+ return new PendingSyncPromise<TResult1 | TResult2>(
63
+ this.promise.then(onfulfilled, onrejected)
64
+ );
65
+ }
66
+
67
+ public valueOrPromise(): T | PromiseLike<T> {
68
+ return this.promise;
69
+ }
70
+ }
71
+
72
+ class ResolvedSyncPromise<T> implements SyncPromise<T> {
73
+ private value: T;
74
+
75
+ public constructor(value: T) {
76
+ this.value = value;
77
+ }
78
+
79
+ public then<TResult1 = T, TResult2 = never>(
80
+ onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
81
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
82
+ ) {
83
+ if (!onfulfilled) {
84
+ return this as any;
85
+ }
86
+ return SyncPromise.try(() => onfulfilled(this.value));
87
+ }
88
+
89
+ public valueOrPromise(): T | PromiseLike<T> {
90
+ return this.value;
91
+ }
92
+ }
93
+
94
+ class RejectedSyncPromise<T> implements SyncPromise<T> {
95
+ private reason: any;
96
+
97
+ public constructor(reason: any) {
98
+ this.reason = reason;
99
+ }
100
+
101
+ public then<TResult1 = T, TResult2 = never>(
102
+ onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
103
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
104
+ ) {
105
+ if (!onrejected) {
106
+ return this as any;
107
+ }
108
+ return SyncPromise.try(() => onrejected(this.reason));
109
+ }
110
+
111
+ public valueOrPromise(): T | PromiseLike<T> {
112
+ throw this.reason;
113
+ }
114
+ }
@@ -1,102 +1,103 @@
1
- // cspell: ignore syncbird
2
-
3
- import { getBigInt64, getBigUint64, setBigInt64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback.js';
4
- import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from "../basic/index.js";
5
- import { Syncbird } from "../syncbird.js";
6
- import type { ValueOrPromise } from "../utils.js";
7
-
8
- type DataViewBigInt64Getter = (dataView: DataView, byteOffset: number, littleEndian: boolean | undefined) => bigint;
9
-
10
- type DataViewBigInt64Setter = (dataView: DataView, byteOffset: number, value: bigint, littleEndian: boolean | undefined) => void;
11
-
12
- export class BigIntFieldType {
13
- public readonly TTypeScriptType!: bigint;
14
-
15
- public readonly size: number;
16
-
17
- public readonly getter: DataViewBigInt64Getter;
18
-
19
- public readonly setter: DataViewBigInt64Setter;
20
-
21
- public constructor(
22
- size: number,
23
- getter: DataViewBigInt64Getter,
24
- setter: DataViewBigInt64Setter,
25
- ) {
26
- this.size = size;
27
- this.getter = getter;
28
- this.setter = setter;
29
- }
30
-
31
- public static readonly Int64 = new BigIntFieldType(8, getBigInt64, setBigInt64);
32
-
33
- public static readonly Uint64 = new BigIntFieldType(8, getBigUint64, setBigUint64);
34
- }
35
-
36
- export class BigIntFieldDefinition<
37
- TType extends BigIntFieldType = BigIntFieldType,
38
- TTypeScriptType = TType["TTypeScriptType"],
39
- > extends StructFieldDefinition<
40
- void,
41
- TTypeScriptType
42
- > {
43
- public readonly type: TType;
44
-
45
- public constructor(type: TType, _typescriptType?: TTypeScriptType) {
46
- super();
47
- this.type = type;
48
- }
49
-
50
- public getSize(): number {
51
- return this.type.size;
52
- }
53
-
54
- public create(
55
- options: Readonly<StructOptions>,
56
- struct: StructValue,
57
- value: TTypeScriptType,
58
- ): BigIntFieldValue<this> {
59
- return new BigIntFieldValue(this, options, struct, value);
60
- }
61
-
62
- public override deserialize(
63
- options: Readonly<StructOptions>,
64
- stream: StructDeserializeStream,
65
- struct: StructValue,
66
- ): BigIntFieldValue<this>;
67
- public override deserialize(
68
- options: Readonly<StructOptions>,
69
- stream: StructAsyncDeserializeStream,
70
- struct: StructValue,
71
- ): Promise<BigIntFieldValue<this>>;
72
- public override deserialize(
73
- options: Readonly<StructOptions>,
74
- stream: StructDeserializeStream | StructAsyncDeserializeStream,
75
- struct: StructValue,
76
- ): ValueOrPromise<BigIntFieldValue<this>> {
77
- return Syncbird.try(() => {
78
- return stream.read(this.getSize());
79
- }).then(array => {
80
- const view = new DataView(array.buffer, array.byteOffset, array.byteLength);
81
- const value = this.type.getter(
82
- view,
83
- 0,
84
- options.littleEndian
85
- );
86
- return this.create(options, struct, value as any);
87
- }).valueOrPromise();
88
- }
89
- }
90
-
91
- export class BigIntFieldValue<
92
- TDefinition extends BigIntFieldDefinition<BigIntFieldType, any>,
93
- > extends StructFieldValue<TDefinition> {
94
- public serialize(dataView: DataView, offset: number): void {
95
- this.definition.type.setter(
96
- dataView,
97
- offset,
98
- this.value!,
99
- this.options.littleEndian
100
- );
101
- }
102
- }
1
+ import { getBigInt64, getBigUint64, setBigInt64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback.js';
2
+ import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from "../basic/index.js";
3
+ import { SyncPromise } from "../sync-promise.js";
4
+ import type { ValueOrPromise } from "../utils.js";
5
+
6
+ type DataViewBigInt64Getter = (dataView: DataView, byteOffset: number, littleEndian: boolean | undefined) => bigint;
7
+
8
+ type DataViewBigInt64Setter = (dataView: DataView, byteOffset: number, value: bigint, littleEndian: boolean | undefined) => void;
9
+
10
+ export class BigIntFieldType {
11
+ public readonly TTypeScriptType!: bigint;
12
+
13
+ public readonly size: number;
14
+
15
+ public readonly getter: DataViewBigInt64Getter;
16
+
17
+ public readonly setter: DataViewBigInt64Setter;
18
+
19
+ public constructor(
20
+ size: number,
21
+ getter: DataViewBigInt64Getter,
22
+ setter: DataViewBigInt64Setter,
23
+ ) {
24
+ this.size = size;
25
+ this.getter = getter;
26
+ this.setter = setter;
27
+ }
28
+
29
+ public static readonly Int64 = new BigIntFieldType(8, getBigInt64, setBigInt64);
30
+
31
+ public static readonly Uint64 = new BigIntFieldType(8, getBigUint64, setBigUint64);
32
+ }
33
+
34
+ export class BigIntFieldDefinition<
35
+ TType extends BigIntFieldType = BigIntFieldType,
36
+ TTypeScriptType = TType["TTypeScriptType"],
37
+ > extends StructFieldDefinition<
38
+ void,
39
+ TTypeScriptType
40
+ > {
41
+ public readonly type: TType;
42
+
43
+ public constructor(type: TType, _typescriptType?: TTypeScriptType) {
44
+ super();
45
+ this.type = type;
46
+ }
47
+
48
+ public getSize(): number {
49
+ return this.type.size;
50
+ }
51
+
52
+ public create(
53
+ options: Readonly<StructOptions>,
54
+ struct: StructValue,
55
+ value: TTypeScriptType,
56
+ ): BigIntFieldValue<this> {
57
+ return new BigIntFieldValue(this, options, struct, value);
58
+ }
59
+
60
+ public override deserialize(
61
+ options: Readonly<StructOptions>,
62
+ stream: StructDeserializeStream,
63
+ struct: StructValue,
64
+ ): BigIntFieldValue<this>;
65
+ public override deserialize(
66
+ options: Readonly<StructOptions>,
67
+ stream: StructAsyncDeserializeStream,
68
+ struct: StructValue,
69
+ ): Promise<BigIntFieldValue<this>>;
70
+ public override deserialize(
71
+ options: Readonly<StructOptions>,
72
+ stream: StructDeserializeStream | StructAsyncDeserializeStream,
73
+ struct: StructValue,
74
+ ): ValueOrPromise<BigIntFieldValue<this>> {
75
+ return SyncPromise
76
+ .try(() => {
77
+ return stream.read(this.getSize());
78
+ })
79
+ .then(array => {
80
+ const view = new DataView(array.buffer, array.byteOffset, array.byteLength);
81
+ const value = this.type.getter(
82
+ view,
83
+ 0,
84
+ options.littleEndian
85
+ );
86
+ return this.create(options, struct, value as any);
87
+ })
88
+ .valueOrPromise();
89
+ }
90
+ }
91
+
92
+ export class BigIntFieldValue<
93
+ TDefinition extends BigIntFieldDefinition<BigIntFieldType, any>,
94
+ > extends StructFieldValue<TDefinition> {
95
+ public serialize(dataView: DataView, offset: number): void {
96
+ this.definition.type.setter(
97
+ dataView,
98
+ offset,
99
+ this.value!,
100
+ this.options.littleEndian
101
+ );
102
+ }
103
+ }