@haneullabs/bcs 0.1.0 → 2.0.3

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 (88) hide show
  1. package/CHANGELOG.md +43 -9
  2. package/dist/bcs-type.d.mts +138 -0
  3. package/dist/bcs-type.d.mts.map +1 -0
  4. package/dist/bcs-type.mjs +269 -0
  5. package/dist/bcs-type.mjs.map +1 -0
  6. package/dist/bcs.d.mts +184 -0
  7. package/dist/bcs.d.mts.map +1 -0
  8. package/dist/bcs.mjs +250 -0
  9. package/dist/bcs.mjs.map +1 -0
  10. package/dist/index.d.mts +8 -0
  11. package/dist/index.mjs +8 -0
  12. package/dist/reader.d.mts +96 -0
  13. package/dist/reader.d.mts.map +1 -0
  14. package/dist/reader.mjs +142 -0
  15. package/dist/reader.mjs.map +1 -0
  16. package/dist/types.d.mts +20 -0
  17. package/dist/types.d.mts.map +1 -0
  18. package/dist/types.mjs +1 -0
  19. package/dist/uleb.d.mts +9 -0
  20. package/dist/uleb.d.mts.map +1 -0
  21. package/dist/uleb.mjs +36 -0
  22. package/dist/uleb.mjs.map +1 -0
  23. package/dist/utils.d.mts +23 -0
  24. package/dist/utils.d.mts.map +1 -0
  25. package/dist/utils.mjs +56 -0
  26. package/dist/utils.mjs.map +1 -0
  27. package/dist/writer.d.mts +126 -0
  28. package/dist/writer.d.mts.map +1 -0
  29. package/dist/writer.mjs +179 -0
  30. package/dist/writer.mjs.map +1 -0
  31. package/docs/index.md +352 -0
  32. package/docs/llms-index.md +6 -0
  33. package/package.json +21 -25
  34. package/src/bcs.ts +32 -7
  35. package/src/index.ts +21 -34
  36. package/src/uleb.ts +0 -1
  37. package/dist/cjs/bcs-type.d.ts +0 -127
  38. package/dist/cjs/bcs-type.js +0 -386
  39. package/dist/cjs/bcs-type.js.map +0 -7
  40. package/dist/cjs/bcs.d.ts +0 -175
  41. package/dist/cjs/bcs.js +0 -406
  42. package/dist/cjs/bcs.js.map +0 -7
  43. package/dist/cjs/index.d.ts +0 -22
  44. package/dist/cjs/index.js +0 -59
  45. package/dist/cjs/index.js.map +0 -7
  46. package/dist/cjs/package.json +0 -5
  47. package/dist/cjs/reader.d.ts +0 -92
  48. package/dist/cjs/reader.js +0 -136
  49. package/dist/cjs/reader.js.map +0 -7
  50. package/dist/cjs/types.d.ts +0 -28
  51. package/dist/cjs/types.js +0 -17
  52. package/dist/cjs/types.js.map +0 -7
  53. package/dist/cjs/uleb.d.ts +0 -5
  54. package/dist/cjs/uleb.js +0 -66
  55. package/dist/cjs/uleb.js.map +0 -7
  56. package/dist/cjs/utils.d.ts +0 -18
  57. package/dist/cjs/utils.js +0 -74
  58. package/dist/cjs/utils.js.map +0 -7
  59. package/dist/cjs/writer.d.ts +0 -117
  60. package/dist/cjs/writer.js +0 -196
  61. package/dist/cjs/writer.js.map +0 -7
  62. package/dist/esm/bcs-type.d.ts +0 -127
  63. package/dist/esm/bcs-type.js +0 -366
  64. package/dist/esm/bcs-type.js.map +0 -7
  65. package/dist/esm/bcs.d.ts +0 -175
  66. package/dist/esm/bcs.js +0 -397
  67. package/dist/esm/bcs.js.map +0 -7
  68. package/dist/esm/index.d.ts +0 -22
  69. package/dist/esm/index.js +0 -46
  70. package/dist/esm/index.js.map +0 -7
  71. package/dist/esm/package.json +0 -5
  72. package/dist/esm/reader.d.ts +0 -92
  73. package/dist/esm/reader.js +0 -116
  74. package/dist/esm/reader.js.map +0 -7
  75. package/dist/esm/types.d.ts +0 -28
  76. package/dist/esm/types.js +0 -1
  77. package/dist/esm/types.js.map +0 -7
  78. package/dist/esm/uleb.d.ts +0 -5
  79. package/dist/esm/uleb.js +0 -46
  80. package/dist/esm/uleb.js.map +0 -7
  81. package/dist/esm/utils.d.ts +0 -18
  82. package/dist/esm/utils.js +0 -54
  83. package/dist/esm/utils.js.map +0 -7
  84. package/dist/esm/writer.d.ts +0 -117
  85. package/dist/esm/writer.js +0 -176
  86. package/dist/esm/writer.js.map +0 -7
  87. package/dist/tsconfig.esm.tsbuildinfo +0 -1
  88. package/dist/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # Change Log
2
2
 
3
+ ## 2.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 43e69f8: Add embedded LLM-friendly docs to published packages
8
+
9
+ ## 2.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 99d1e00: Add default export condition
14
+ - Updated dependencies [99d1e00]
15
+ - @mysten/utils@0.3.1
16
+
17
+ ## 2.0.1
18
+
19
+ ### Patch Changes
20
+
21
+ - Updated dependencies [339d1e0]
22
+ - @mysten/utils@0.3.0
23
+
24
+ ## 2.0.0
25
+
26
+ ### Major Changes
27
+
28
+ - e00788c: Remove deprecated exports: `toB58`, `fromB58`, `toB64`, `fromB64`, `toHEX`, `fromHEX`.
29
+ Use `toBase58`, `fromBase58`, `toBase64`, `fromBase64`, `toHex`, `fromHex` instead.
30
+
31
+ ### Minor Changes
32
+
33
+ - e00788c: Add automatic sorting of map entries by serialized key bytes in `bcs.map()` to match
34
+ Rust's BTreeMap BCS serialization. Also exports `compareBcsBytes` helper for lexicographic byte
35
+ comparison.
36
+
3
37
  ## 1.9.2
4
38
 
5
39
  ### Patch Changes
@@ -35,7 +69,7 @@
35
69
  ### Patch Changes
36
70
 
37
71
  - Updated dependencies [ea1ac70]
38
- - @haneullabs/utils@0.2.0
72
+ - @mysten/utils@0.2.0
39
73
 
40
74
  ## 1.7.0
41
75
 
@@ -52,14 +86,14 @@
52
86
 
53
87
  - 1c4a82d: update links in package.json
54
88
  - Updated dependencies [1c4a82d]
55
- - @haneullabs/utils@0.1.1
89
+ - @mysten/utils@0.1.1
56
90
 
57
91
  ## 1.6.3
58
92
 
59
93
  ### Patch Changes
60
94
 
61
95
  - Updated dependencies [a00522b]
62
- - @haneullabs/utils@0.1.0
96
+ - @mysten/utils@0.1.0
63
97
 
64
98
  ## 1.6.2
65
99
 
@@ -67,7 +101,7 @@
67
101
 
68
102
  - bb7c03a: Update dependencies
69
103
  - Updated dependencies [bb7c03a]
70
- - @haneullabs/utils@0.0.1
104
+ - @mysten/utils@0.0.1
71
105
 
72
106
  ## 1.6.1
73
107
 
@@ -166,10 +200,10 @@
166
200
 
167
201
  ### Major Changes
168
202
 
169
- - a92b03de42: The Typescript SDK has been renamed to `@haneullabs/haneul` and includes many new features
203
+ - a92b03de42: The Typescript SDK has been renamed to `@mysten/sui` and includes many new features
170
204
  and breaking changes. See the
171
- [full migration guide](https://sdk.haneullabs.com/typescript/migrations/haneul-1.0) for details on
172
- how to upgrade.
205
+ [full migration guide](https://sdk.mystenlabs.com/sui/migrations/sui-1.0) for details on how to
206
+ upgrade.
173
207
 
174
208
  ## 0.11.1
175
209
 
@@ -199,7 +233,7 @@
199
233
 
200
234
  ### Patch Changes
201
235
 
202
- - dd362ec1d6: Update docs url to sdk.haneullabs.com
236
+ - dd362ec1d6: Update docs url to sdk.mystenlabs.com
203
237
 
204
238
  ## 0.9.0
205
239
 
@@ -218,7 +252,7 @@
218
252
 
219
253
  ### Minor Changes
220
254
 
221
- - 1bc430161: Add new type-safe schema builder. See https://sdk.haneullabs.com/bcs for updated
255
+ - 1bc430161: Add new type-safe schema builder. See https://sdk.mystenlabs.com/bcs for updated
222
256
  documentation
223
257
  - e4484852b: Add isSerializedBcs helper
224
258
 
@@ -0,0 +1,138 @@
1
+ import { BcsReader } from "./reader.mjs";
2
+ import { EnumInputShape, EnumOutputShape, JoinString } from "./types.mjs";
3
+ import { BcsWriter, BcsWriterOptions } from "./writer.mjs";
4
+
5
+ //#region src/bcs-type.d.ts
6
+ interface BcsTypeOptions<T, Input = T, Name extends string = string> {
7
+ name?: Name;
8
+ validate?: (value: Input) => void;
9
+ }
10
+ declare class BcsType<T, Input = T, const Name extends string = string> {
11
+ #private;
12
+ $inferType: T;
13
+ $inferInput: Input;
14
+ name: Name;
15
+ read: (reader: BcsReader) => T;
16
+ serializedSize: (value: Input, options?: BcsWriterOptions) => number | null;
17
+ validate: (value: Input) => void;
18
+ constructor(options: {
19
+ name: Name;
20
+ read: (reader: BcsReader) => T;
21
+ write: (value: Input, writer: BcsWriter) => void;
22
+ serialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
23
+ serializedSize?: (value: Input) => number | null;
24
+ validate?: (value: Input) => void;
25
+ } & BcsTypeOptions<T, Input, Name>);
26
+ write(value: Input, writer: BcsWriter): void;
27
+ serialize(value: Input, options?: BcsWriterOptions): SerializedBcs<T, Input>;
28
+ parse(bytes: Uint8Array): T;
29
+ fromHex(hex: string): T;
30
+ fromBase58(b64: string): T;
31
+ fromBase64(b64: string): T;
32
+ transform<T2 = T, Input2 = Input, NewName extends string = Name>({
33
+ name,
34
+ input,
35
+ output,
36
+ validate
37
+ }: {
38
+ input?: (val: Input2) => Input;
39
+ output?: (value: T) => T2;
40
+ } & BcsTypeOptions<T2, Input2, NewName>): BcsType<T2, Input2, NewName>;
41
+ }
42
+ declare const SERIALIZED_BCS_BRAND: never;
43
+ declare function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown>;
44
+ declare class SerializedBcs<T, Input = T> {
45
+ #private;
46
+ [SERIALIZED_BCS_BRAND]: boolean;
47
+ constructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>);
48
+ toBytes(): Uint8Array<ArrayBuffer>;
49
+ toHex(): string;
50
+ toBase64(): string;
51
+ toBase58(): string;
52
+ parse(): T;
53
+ }
54
+ declare function fixedSizeBcsType<T, Input = T, const Name extends string = string>({
55
+ size,
56
+ ...options
57
+ }: {
58
+ name: Name;
59
+ size: number;
60
+ read: (reader: BcsReader) => T;
61
+ write: (value: Input, writer: BcsWriter) => void;
62
+ } & BcsTypeOptions<T, Input, Name>): BcsType<T, Input, Name>;
63
+ declare function uIntBcsType<const Name extends string = string>({
64
+ readMethod,
65
+ writeMethod,
66
+ ...options
67
+ }: {
68
+ name: Name;
69
+ size: number;
70
+ readMethod: `read${8 | 16 | 32}`;
71
+ writeMethod: `write${8 | 16 | 32}`;
72
+ maxValue: number;
73
+ } & BcsTypeOptions<number, number, Name>): BcsType<number, number, Name>;
74
+ declare function bigUIntBcsType<const Name extends string = string>({
75
+ readMethod,
76
+ writeMethod,
77
+ ...options
78
+ }: {
79
+ name: Name;
80
+ size: number;
81
+ readMethod: `read${64 | 128 | 256}`;
82
+ writeMethod: `write${64 | 128 | 256}`;
83
+ maxValue: bigint;
84
+ } & BcsTypeOptions<string, string | number | bigint>): BcsType<string, string | number | bigint, Name>;
85
+ declare function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({
86
+ serialize,
87
+ ...options
88
+ }: {
89
+ name: Name;
90
+ read: (reader: BcsReader) => T;
91
+ serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
92
+ } & BcsTypeOptions<T, Input>): BcsType<T, Input, string>;
93
+ declare function stringLikeBcsType<const Name extends string = string>({
94
+ toBytes,
95
+ fromBytes,
96
+ ...options
97
+ }: {
98
+ name: Name;
99
+ toBytes: (value: string) => Uint8Array;
100
+ fromBytes: (bytes: Uint8Array) => string;
101
+ serializedSize?: (value: string) => number | null;
102
+ } & BcsTypeOptions<string, string, Name>): BcsType<string, string, Name>;
103
+ declare function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>): BcsType<T, Input, string>;
104
+ interface BcsStructOptions<T extends Record<string, BcsType<any>>, Name extends string = string> extends Omit<BcsTypeOptions<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never }, Name>, 'name'> {
105
+ name: Name;
106
+ fields: T;
107
+ }
108
+ declare class BcsStruct<T extends Record<string, BcsType<any>>, const Name extends string = string> extends BcsType<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never }, Name> {
109
+ constructor({
110
+ name,
111
+ fields,
112
+ ...options
113
+ }: BcsStructOptions<T, Name>);
114
+ }
115
+ interface BcsEnumOptions<T extends Record<string, BcsType<any> | null>, Name extends string = string> extends Omit<BcsTypeOptions<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null }>, Name>, 'name'> {
116
+ name: Name;
117
+ fields: T;
118
+ }
119
+ declare class BcsEnum<T extends Record<string, BcsType<any> | null>, const Name extends string = string> extends BcsType<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null }>, Name> {
120
+ constructor({
121
+ fields,
122
+ ...options
123
+ }: BcsEnumOptions<T, Name>);
124
+ }
125
+ interface BcsTupleOptions<T extends readonly BcsType<any>[], Name extends string> extends Omit<BcsTypeOptions<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never }, { [K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never }, Name>, 'name'> {
126
+ name?: Name;
127
+ fields: T;
128
+ }
129
+ declare class BcsTuple<const T extends readonly BcsType<any>[], const Name extends string = `(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`> extends BcsType<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never }, { [K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never }, Name> {
130
+ constructor({
131
+ fields,
132
+ name,
133
+ ...options
134
+ }: BcsTupleOptions<T, Name>);
135
+ }
136
+ //#endregion
137
+ export { BcsEnum, BcsEnumOptions, BcsStruct, BcsStructOptions, BcsTuple, BcsTupleOptions, BcsType, BcsTypeOptions, SerializedBcs, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, isSerializedBcs, lazyBcsType, stringLikeBcsType, uIntBcsType };
138
+ //# sourceMappingURL=bcs-type.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bcs-type.d.mts","names":[],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js";
3
+ import { BcsWriter } from "./writer.mjs";
4
+ import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
5
+
6
+ //#region src/bcs-type.ts
7
+ var BcsType = class BcsType {
8
+ #write;
9
+ #serialize;
10
+ constructor(options) {
11
+ this.name = options.name;
12
+ this.read = options.read;
13
+ this.serializedSize = options.serializedSize ?? (() => null);
14
+ this.#write = options.write;
15
+ this.#serialize = options.serialize ?? ((value, options$1) => {
16
+ const writer = new BcsWriter({
17
+ initialSize: this.serializedSize(value) ?? void 0,
18
+ ...options$1
19
+ });
20
+ this.#write(value, writer);
21
+ return writer.toBytes();
22
+ });
23
+ this.validate = options.validate ?? (() => {});
24
+ }
25
+ write(value, writer) {
26
+ this.validate(value);
27
+ this.#write(value, writer);
28
+ }
29
+ serialize(value, options) {
30
+ this.validate(value);
31
+ return new SerializedBcs(this, this.#serialize(value, options));
32
+ }
33
+ parse(bytes) {
34
+ const reader = new BcsReader(bytes);
35
+ return this.read(reader);
36
+ }
37
+ fromHex(hex) {
38
+ return this.parse(fromHex(hex));
39
+ }
40
+ fromBase58(b64) {
41
+ return this.parse(fromBase58(b64));
42
+ }
43
+ fromBase64(b64) {
44
+ return this.parse(fromBase64(b64));
45
+ }
46
+ transform({ name, input, output, validate }) {
47
+ return new BcsType({
48
+ name: name ?? this.name,
49
+ read: (reader) => output ? output(this.read(reader)) : this.read(reader),
50
+ write: (value, writer) => this.#write(input ? input(value) : value, writer),
51
+ serializedSize: (value) => this.serializedSize(input ? input(value) : value),
52
+ serialize: (value, options) => this.#serialize(input ? input(value) : value, options),
53
+ validate: (value) => {
54
+ validate?.(value);
55
+ this.validate(input ? input(value) : value);
56
+ }
57
+ });
58
+ }
59
+ };
60
+ const SERIALIZED_BCS_BRAND = Symbol.for("@haneullabs/serialized-bcs");
61
+ function isSerializedBcs(obj) {
62
+ return !!obj && typeof obj === "object" && obj[SERIALIZED_BCS_BRAND] === true;
63
+ }
64
+ var SerializedBcs = class {
65
+ #schema;
66
+ #bytes;
67
+ get [SERIALIZED_BCS_BRAND]() {
68
+ return true;
69
+ }
70
+ constructor(schema, bytes) {
71
+ this.#schema = schema;
72
+ this.#bytes = bytes;
73
+ }
74
+ toBytes() {
75
+ return this.#bytes;
76
+ }
77
+ toHex() {
78
+ return toHex(this.#bytes);
79
+ }
80
+ toBase64() {
81
+ return toBase64(this.#bytes);
82
+ }
83
+ toBase58() {
84
+ return toBase58(this.#bytes);
85
+ }
86
+ parse() {
87
+ return this.#schema.parse(this.#bytes);
88
+ }
89
+ };
90
+ function fixedSizeBcsType({ size, ...options }) {
91
+ return new BcsType({
92
+ ...options,
93
+ serializedSize: () => size
94
+ });
95
+ }
96
+ function uIntBcsType({ readMethod, writeMethod, ...options }) {
97
+ return fixedSizeBcsType({
98
+ ...options,
99
+ read: (reader) => reader[readMethod](),
100
+ write: (value, writer) => writer[writeMethod](value),
101
+ validate: (value) => {
102
+ if (value < 0 || value > options.maxValue) throw new TypeError(`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`);
103
+ options.validate?.(value);
104
+ }
105
+ });
106
+ }
107
+ function bigUIntBcsType({ readMethod, writeMethod, ...options }) {
108
+ return fixedSizeBcsType({
109
+ ...options,
110
+ read: (reader) => reader[readMethod](),
111
+ write: (value, writer) => writer[writeMethod](BigInt(value)),
112
+ validate: (val) => {
113
+ const value = BigInt(val);
114
+ if (value < 0 || value > options.maxValue) throw new TypeError(`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`);
115
+ options.validate?.(value);
116
+ }
117
+ });
118
+ }
119
+ function dynamicSizeBcsType({ serialize, ...options }) {
120
+ const type = new BcsType({
121
+ ...options,
122
+ serialize,
123
+ write: (value, writer) => {
124
+ for (const byte of type.serialize(value).toBytes()) writer.write8(byte);
125
+ }
126
+ });
127
+ return type;
128
+ }
129
+ function stringLikeBcsType({ toBytes, fromBytes, ...options }) {
130
+ return new BcsType({
131
+ ...options,
132
+ read: (reader) => {
133
+ const length = reader.readULEB();
134
+ return fromBytes(reader.readBytes(length));
135
+ },
136
+ write: (hex, writer) => {
137
+ const bytes = toBytes(hex);
138
+ writer.writeULEB(bytes.length);
139
+ for (let i = 0; i < bytes.length; i++) writer.write8(bytes[i]);
140
+ },
141
+ serialize: (value) => {
142
+ const bytes = toBytes(value);
143
+ const size = ulebEncode(bytes.length);
144
+ const result = new Uint8Array(size.length + bytes.length);
145
+ result.set(size, 0);
146
+ result.set(bytes, size.length);
147
+ return result;
148
+ },
149
+ validate: (value) => {
150
+ if (typeof value !== "string") throw new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);
151
+ options.validate?.(value);
152
+ }
153
+ });
154
+ }
155
+ function lazyBcsType(cb) {
156
+ let lazyType = null;
157
+ function getType() {
158
+ if (!lazyType) lazyType = cb();
159
+ return lazyType;
160
+ }
161
+ return new BcsType({
162
+ name: "lazy",
163
+ read: (data) => getType().read(data),
164
+ serializedSize: (value) => getType().serializedSize(value),
165
+ write: (value, writer) => getType().write(value, writer),
166
+ serialize: (value, options) => getType().serialize(value, options).toBytes()
167
+ });
168
+ }
169
+ var BcsStruct = class extends BcsType {
170
+ constructor({ name, fields, ...options }) {
171
+ const canonicalOrder = Object.entries(fields);
172
+ super({
173
+ name,
174
+ serializedSize: (values) => {
175
+ let total = 0;
176
+ for (const [field, type] of canonicalOrder) {
177
+ const size = type.serializedSize(values[field]);
178
+ if (size == null) return null;
179
+ total += size;
180
+ }
181
+ return total;
182
+ },
183
+ read: (reader) => {
184
+ const result = {};
185
+ for (const [field, type] of canonicalOrder) result[field] = type.read(reader);
186
+ return result;
187
+ },
188
+ write: (value, writer) => {
189
+ for (const [field, type] of canonicalOrder) type.write(value[field], writer);
190
+ },
191
+ ...options,
192
+ validate: (value) => {
193
+ options?.validate?.(value);
194
+ if (typeof value !== "object" || value == null) throw new TypeError(`Expected object, found ${typeof value}`);
195
+ }
196
+ });
197
+ }
198
+ };
199
+ var BcsEnum = class extends BcsType {
200
+ constructor({ fields, ...options }) {
201
+ const canonicalOrder = Object.entries(fields);
202
+ super({
203
+ read: (reader) => {
204
+ const index = reader.readULEB();
205
+ const enumEntry = canonicalOrder[index];
206
+ if (!enumEntry) throw new TypeError(`Unknown value ${index} for enum ${options.name}`);
207
+ const [kind, type] = enumEntry;
208
+ return {
209
+ [kind]: type?.read(reader) ?? true,
210
+ $kind: kind
211
+ };
212
+ },
213
+ write: (value, writer) => {
214
+ const [name, val] = Object.entries(value).filter(([name$1]) => Object.hasOwn(fields, name$1))[0];
215
+ for (let i = 0; i < canonicalOrder.length; i++) {
216
+ const [optionName, optionType] = canonicalOrder[i];
217
+ if (optionName === name) {
218
+ writer.writeULEB(i);
219
+ optionType?.write(val, writer);
220
+ return;
221
+ }
222
+ }
223
+ },
224
+ ...options,
225
+ validate: (value) => {
226
+ options?.validate?.(value);
227
+ if (typeof value !== "object" || value == null) throw new TypeError(`Expected object, found ${typeof value}`);
228
+ const keys = Object.keys(value).filter((k) => value[k] !== void 0 && Object.hasOwn(fields, k));
229
+ if (keys.length !== 1) throw new TypeError(`Expected object with one key, but found ${keys.length} for type ${options.name}}`);
230
+ const [variant] = keys;
231
+ if (!Object.hasOwn(fields, variant)) throw new TypeError(`Invalid enum variant ${variant}`);
232
+ }
233
+ });
234
+ }
235
+ };
236
+ var BcsTuple = class extends BcsType {
237
+ constructor({ fields, name, ...options }) {
238
+ super({
239
+ name: name ?? `(${fields.map((t) => t.name).join(", ")})`,
240
+ serializedSize: (values) => {
241
+ let total = 0;
242
+ for (let i = 0; i < fields.length; i++) {
243
+ const size = fields[i].serializedSize(values[i]);
244
+ if (size == null) return null;
245
+ total += size;
246
+ }
247
+ return total;
248
+ },
249
+ read: (reader) => {
250
+ const result = [];
251
+ for (const field of fields) result.push(field.read(reader));
252
+ return result;
253
+ },
254
+ write: (value, writer) => {
255
+ for (let i = 0; i < fields.length; i++) fields[i].write(value[i], writer);
256
+ },
257
+ ...options,
258
+ validate: (value) => {
259
+ options?.validate?.(value);
260
+ if (!Array.isArray(value)) throw new TypeError(`Expected array, found ${typeof value}`);
261
+ if (value.length !== fields.length) throw new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);
262
+ }
263
+ });
264
+ }
265
+ };
266
+
267
+ //#endregion
268
+ export { BcsEnum, BcsStruct, BcsTuple, BcsType, SerializedBcs, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, isSerializedBcs, lazyBcsType, stringLikeBcsType, uIntBcsType };
269
+ //# sourceMappingURL=bcs-type.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bcs-type.mjs","names":["#write","#serialize","options","#schema","#bytes","name"],"sources":["../src/bcs-type.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromBase64, toBase58, toBase64, fromHex, toHex } from '@haneullabs/utils';\nimport { BcsReader } from './reader.js';\nimport { ulebEncode } from './uleb.js';\nimport type { BcsWriterOptions } from './writer.js';\nimport { BcsWriter } from './writer.js';\nimport type { EnumInputShape, EnumOutputShape, JoinString } from './types.js';\n\nexport interface BcsTypeOptions<T, Input = T, Name extends string = string> {\n\tname?: Name;\n\tvalidate?: (value: Input) => void;\n}\n\nexport class BcsType<T, Input = T, const Name extends string = string> {\n\t$inferType!: T;\n\t$inferInput!: Input;\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserializedSize: (value: Input, options?: BcsWriterOptions) => number | null;\n\tvalidate: (value: Input) => void;\n\t#write: (value: Input, writer: BcsWriter) => void;\n\t#serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\n\tconstructor(\n\t\toptions: {\n\t\t\tname: Name;\n\t\t\tread: (reader: BcsReader) => T;\n\t\t\twrite: (value: Input, writer: BcsWriter) => void;\n\t\t\tserialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\t\t\tserializedSize?: (value: Input) => number | null;\n\t\t\tvalidate?: (value: Input) => void;\n\t\t} & BcsTypeOptions<T, Input, Name>,\n\t) {\n\t\tthis.name = options.name;\n\t\tthis.read = options.read;\n\t\tthis.serializedSize = options.serializedSize ?? (() => null);\n\t\tthis.#write = options.write;\n\t\tthis.#serialize =\n\t\t\toptions.serialize ??\n\t\t\t((value, options) => {\n\t\t\t\tconst writer = new BcsWriter({\n\t\t\t\t\tinitialSize: this.serializedSize(value) ?? undefined,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t\tthis.#write(value, writer);\n\t\t\t\treturn writer.toBytes();\n\t\t\t});\n\n\t\tthis.validate = options.validate ?? (() => {});\n\t}\n\n\twrite(value: Input, writer: BcsWriter) {\n\t\tthis.validate(value);\n\t\tthis.#write(value, writer);\n\t}\n\n\tserialize(value: Input, options?: BcsWriterOptions) {\n\t\tthis.validate(value);\n\t\treturn new SerializedBcs(this, this.#serialize(value, options));\n\t}\n\n\tparse(bytes: Uint8Array): T {\n\t\tconst reader = new BcsReader(bytes);\n\t\treturn this.read(reader);\n\t}\n\n\tfromHex(hex: string) {\n\t\treturn this.parse(fromHex(hex));\n\t}\n\n\tfromBase58(b64: string) {\n\t\treturn this.parse(fromBase58(b64));\n\t}\n\n\tfromBase64(b64: string) {\n\t\treturn this.parse(fromBase64(b64));\n\t}\n\n\ttransform<T2 = T, Input2 = Input, NewName extends string = Name>({\n\t\tname,\n\t\tinput,\n\t\toutput,\n\t\tvalidate,\n\t}: {\n\t\tinput?: (val: Input2) => Input;\n\t\toutput?: (value: T) => T2;\n\t} & BcsTypeOptions<T2, Input2, NewName>) {\n\t\treturn new BcsType<T2, Input2, NewName>({\n\t\t\tname: (name ?? this.name) as NewName,\n\t\t\tread: (reader) => (output ? output(this.read(reader)) : (this.read(reader) as never)),\n\t\t\twrite: (value, writer) => this.#write(input ? input(value) : (value as never), writer),\n\t\t\tserializedSize: (value) => this.serializedSize(input ? input(value) : (value as never)),\n\t\t\tserialize: (value, options) =>\n\t\t\t\tthis.#serialize(input ? input(value) : (value as never), options),\n\t\t\tvalidate: (value) => {\n\t\t\t\tvalidate?.(value);\n\t\t\t\tthis.validate(input ? input(value) : (value as never));\n\t\t\t},\n\t\t});\n\t}\n}\n\nconst SERIALIZED_BCS_BRAND = Symbol.for('@haneullabs/serialized-bcs') as never;\nexport function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown> {\n\treturn !!obj && typeof obj === 'object' && (obj as any)[SERIALIZED_BCS_BRAND] === true;\n}\n\nexport class SerializedBcs<T, Input = T> {\n\t#schema: BcsType<T, Input>;\n\t#bytes: Uint8Array<ArrayBuffer>;\n\n\t// Used to brand SerializedBcs so that they can be identified, even between multiple copies\n\t// of the @haneullabs/bcs package are installed\n\tget [SERIALIZED_BCS_BRAND]() {\n\t\treturn true;\n\t}\n\n\tconstructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>) {\n\t\tthis.#schema = schema;\n\t\tthis.#bytes = bytes;\n\t}\n\n\ttoBytes() {\n\t\treturn this.#bytes;\n\t}\n\n\ttoHex() {\n\t\treturn toHex(this.#bytes);\n\t}\n\n\ttoBase64() {\n\t\treturn toBase64(this.#bytes);\n\t}\n\n\ttoBase58() {\n\t\treturn toBase58(this.#bytes);\n\t}\n\n\tparse() {\n\t\treturn this.#schema.parse(this.#bytes);\n\t}\n}\n\nexport function fixedSizeBcsType<T, Input = T, const Name extends string = string>({\n\tsize,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\tread: (reader: BcsReader) => T;\n\twrite: (value: Input, writer: BcsWriter) => void;\n} & BcsTypeOptions<T, Input, Name>) {\n\treturn new BcsType<T, Input, Name>({\n\t\t...options,\n\t\tserializedSize: () => size,\n\t});\n}\n\nexport function uIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${8 | 16 | 32}`;\n\twriteMethod: `write${8 | 16 | 32}`;\n\tmaxValue: number;\n} & BcsTypeOptions<number, number, Name>) {\n\treturn fixedSizeBcsType<number, number, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](value),\n\t\tvalidate: (value) => {\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function bigUIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${64 | 128 | 256}`;\n\twriteMethod: `write${64 | 128 | 256}`;\n\tmaxValue: bigint;\n} & BcsTypeOptions<string, string | number | bigint>) {\n\treturn fixedSizeBcsType<string, string | number | bigint, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](BigInt(value)),\n\t\tvalidate: (val) => {\n\t\t\tconst value = BigInt(val);\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({\n\tserialize,\n\t...options\n}: {\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n} & BcsTypeOptions<T, Input>) {\n\tconst type = new BcsType<T, Input>({\n\t\t...options,\n\t\tserialize,\n\t\twrite: (value, writer) => {\n\t\t\tfor (const byte of type.serialize(value).toBytes()) {\n\t\t\t\twriter.write8(byte);\n\t\t\t}\n\t\t},\n\t});\n\n\treturn type;\n}\n\nexport function stringLikeBcsType<const Name extends string = string>({\n\ttoBytes,\n\tfromBytes,\n\t...options\n}: {\n\tname: Name;\n\ttoBytes: (value: string) => Uint8Array;\n\tfromBytes: (bytes: Uint8Array) => string;\n\tserializedSize?: (value: string) => number | null;\n} & BcsTypeOptions<string, string, Name>) {\n\treturn new BcsType<string, string, Name>({\n\t\t...options,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst bytes = reader.readBytes(length);\n\n\t\t\treturn fromBytes(bytes);\n\t\t},\n\t\twrite: (hex, writer) => {\n\t\t\tconst bytes = toBytes(hex);\n\t\t\twriter.writeULEB(bytes.length);\n\t\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\t\twriter.write8(bytes[i]);\n\t\t\t}\n\t\t},\n\t\tserialize: (value) => {\n\t\t\tconst bytes = toBytes(value);\n\t\t\tconst size = ulebEncode(bytes.length);\n\t\t\tconst result = new Uint8Array(size.length + bytes.length);\n\t\t\tresult.set(size, 0);\n\t\t\tresult.set(bytes, size.length);\n\n\t\t\treturn result;\n\t\t},\n\t\tvalidate: (value) => {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>) {\n\tlet lazyType: BcsType<T, Input> | null = null;\n\tfunction getType() {\n\t\tif (!lazyType) {\n\t\t\tlazyType = cb();\n\t\t}\n\t\treturn lazyType;\n\t}\n\n\treturn new BcsType<T, Input>({\n\t\tname: 'lazy' as never,\n\t\tread: (data) => getType().read(data),\n\t\tserializedSize: (value) => getType().serializedSize(value),\n\t\twrite: (value, writer) => getType().write(value, writer),\n\t\tserialize: (value, options) => getType().serialize(value, options).toBytes(),\n\t});\n}\n\nexport interface BcsStructOptions<\n\tT extends Record<string, BcsType<any>>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsStruct<\n\tT extends Record<string, BcsType<any>>,\n\tconst Name extends string = string,\n> extends BcsType<\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t},\n\tName\n> {\n\tconstructor({ name, fields, ...options }: BcsStructOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields);\n\n\t\tsuper({\n\t\t\tname,\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\tconst size = type.serializedSize(values[field]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: Record<string, unknown> = {};\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\tresult[field] = type.read(reader);\n\t\t\t\t}\n\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\ttype.write(value[field], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsEnumOptions<\n\tT extends Record<string, BcsType<any> | null>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\tEnumOutputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t}>,\n\t\tEnumInputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t}>,\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsEnum<\n\tT extends Record<string, BcsType<any> | null>,\n\tconst Name extends string = string,\n> extends BcsType<\n\tEnumOutputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true;\n\t}>,\n\tEnumInputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t}>,\n\tName\n> {\n\tconstructor({ fields, ...options }: BcsEnumOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields as object);\n\t\tsuper({\n\t\t\tread: (reader) => {\n\t\t\t\tconst index = reader.readULEB();\n\n\t\t\t\tconst enumEntry = canonicalOrder[index];\n\t\t\t\tif (!enumEntry) {\n\t\t\t\t\tthrow new TypeError(`Unknown value ${index} for enum ${options.name}`);\n\t\t\t\t}\n\n\t\t\t\tconst [kind, type] = enumEntry;\n\n\t\t\t\treturn {\n\t\t\t\t\t[kind]: type?.read(reader) ?? true,\n\t\t\t\t\t$kind: kind,\n\t\t\t\t} as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tconst [name, val] = Object.entries(value).filter(([name]) =>\n\t\t\t\t\tObject.hasOwn(fields, name),\n\t\t\t\t)[0];\n\n\t\t\t\tfor (let i = 0; i < canonicalOrder.length; i++) {\n\t\t\t\t\tconst [optionName, optionType] = canonicalOrder[i];\n\t\t\t\t\tif (optionName === name) {\n\t\t\t\t\t\twriter.writeULEB(i);\n\t\t\t\t\t\toptionType?.write(val, writer);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\n\t\t\t\tconst keys = Object.keys(value).filter(\n\t\t\t\t\t(k) => value[k] !== undefined && Object.hasOwn(fields, k),\n\t\t\t\t);\n\n\t\t\t\tif (keys.length !== 1) {\n\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t`Expected object with one key, but found ${keys.length} for type ${options.name}}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst [variant] = keys;\n\n\t\t\t\tif (!Object.hasOwn(fields, variant)) {\n\t\t\t\t\tthrow new TypeError(`Invalid enum variant ${variant}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsTupleOptions<\n\tT extends readonly BcsType<any>[],\n\tName extends string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname?: Name;\n\tfields: T;\n}\n\nexport class BcsTuple<\n\tconst T extends readonly BcsType<any>[],\n\tconst Name extends string =\n\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n> extends BcsType<\n\t{\n\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t},\n\tName\n> {\n\tconstructor({ fields, name, ...options }: BcsTupleOptions<T, Name>) {\n\t\tsuper({\n\t\t\tname: name ?? (`(${fields.map((t) => t.name).join(', ')})` as never),\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tconst size = fields[i].serializedSize(values[i]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: unknown[] = [];\n\t\t\t\tfor (const field of fields) {\n\t\t\t\t\tresult.push(field.read(reader));\n\t\t\t\t}\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tfields[i].write(value[i], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t\tif (value.length !== fields.length) {\n\t\t\t\t\tthrow new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n"],"mappings":";;;;;;AAeA,IAAa,UAAb,MAAa,QAA0D;CAOtE;CACA;CAEA,YACC,SAQC;AACD,OAAK,OAAO,QAAQ;AACpB,OAAK,OAAO,QAAQ;AACpB,OAAK,iBAAiB,QAAQ,yBAAyB;AACvD,QAAKA,QAAS,QAAQ;AACtB,QAAKC,YACJ,QAAQ,eACN,OAAO,cAAY;GACpB,MAAM,SAAS,IAAI,UAAU;IAC5B,aAAa,KAAK,eAAe,MAAM,IAAI;IAC3C,GAAGC;IACH,CAAC;AACF,SAAKF,MAAO,OAAO,OAAO;AAC1B,UAAO,OAAO,SAAS;;AAGzB,OAAK,WAAW,QAAQ,mBAAmB;;CAG5C,MAAM,OAAc,QAAmB;AACtC,OAAK,SAAS,MAAM;AACpB,QAAKA,MAAO,OAAO,OAAO;;CAG3B,UAAU,OAAc,SAA4B;AACnD,OAAK,SAAS,MAAM;AACpB,SAAO,IAAI,cAAc,MAAM,M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