@sachitv/avro-typescript 0.4.0

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 (163) hide show
  1. package/esm/_dnt.polyfills.d.ts +11 -0
  2. package/esm/_dnt.polyfills.js +15 -0
  3. package/esm/avro_reader.d.ts +156 -0
  4. package/esm/avro_reader.js +201 -0
  5. package/esm/avro_writer.d.ts +75 -0
  6. package/esm/avro_writer.js +105 -0
  7. package/esm/internal/collections/array_utils.d.ts +4 -0
  8. package/esm/internal/collections/array_utils.js +18 -0
  9. package/esm/internal/collections/circular_buffer.d.ts +48 -0
  10. package/esm/internal/collections/circular_buffer.js +129 -0
  11. package/esm/internal/crypto/md5.d.ts +12 -0
  12. package/esm/internal/crypto/md5.js +158 -0
  13. package/esm/internal/varint.d.ts +6 -0
  14. package/esm/internal/varint.js +30 -0
  15. package/esm/mod.d.ts +77 -0
  16. package/esm/mod.js +73 -0
  17. package/esm/package.json +3 -0
  18. package/esm/rpc/definitions/message_definition.d.ts +34 -0
  19. package/esm/rpc/definitions/message_definition.js +105 -0
  20. package/esm/rpc/definitions/protocol_definitions.d.ts +197 -0
  21. package/esm/rpc/definitions/protocol_definitions.js +1 -0
  22. package/esm/rpc/message_endpoint/base.d.ts +43 -0
  23. package/esm/rpc/message_endpoint/base.js +81 -0
  24. package/esm/rpc/message_endpoint/emitter.d.ts +96 -0
  25. package/esm/rpc/message_endpoint/emitter.js +245 -0
  26. package/esm/rpc/message_endpoint/helpers.d.ts +37 -0
  27. package/esm/rpc/message_endpoint/helpers.js +68 -0
  28. package/esm/rpc/message_endpoint/listener.d.ts +37 -0
  29. package/esm/rpc/message_endpoint/listener.js +212 -0
  30. package/esm/rpc/protocol/frame_assembler.d.ts +11 -0
  31. package/esm/rpc/protocol/frame_assembler.js +77 -0
  32. package/esm/rpc/protocol/protocol_helpers.d.ts +59 -0
  33. package/esm/rpc/protocol/protocol_helpers.js +123 -0
  34. package/esm/rpc/protocol/transports/fetch.d.ts +9 -0
  35. package/esm/rpc/protocol/transports/fetch.js +30 -0
  36. package/esm/rpc/protocol/transports/in_memory.d.ts +27 -0
  37. package/esm/rpc/protocol/transports/in_memory.js +125 -0
  38. package/esm/rpc/protocol/transports/transport_helpers.d.ts +118 -0
  39. package/esm/rpc/protocol/transports/transport_helpers.js +112 -0
  40. package/esm/rpc/protocol/transports/websocket.d.ts +9 -0
  41. package/esm/rpc/protocol/transports/websocket.js +102 -0
  42. package/esm/rpc/protocol/wire_format/framing.d.ts +44 -0
  43. package/esm/rpc/protocol/wire_format/framing.js +74 -0
  44. package/esm/rpc/protocol/wire_format/handshake.d.ts +110 -0
  45. package/esm/rpc/protocol/wire_format/handshake.js +239 -0
  46. package/esm/rpc/protocol/wire_format/messages.d.ts +192 -0
  47. package/esm/rpc/protocol/wire_format/messages.js +175 -0
  48. package/esm/rpc/protocol/wire_format/metadata.d.ts +44 -0
  49. package/esm/rpc/protocol/wire_format/metadata.js +74 -0
  50. package/esm/rpc/protocol_core.d.ts +121 -0
  51. package/esm/rpc/protocol_core.js +285 -0
  52. package/esm/schemas/base_type.d.ts +41 -0
  53. package/esm/schemas/base_type.js +49 -0
  54. package/esm/schemas/complex/array_type.d.ts +100 -0
  55. package/esm/schemas/complex/array_type.js +299 -0
  56. package/esm/schemas/complex/enum_type.d.ts +81 -0
  57. package/esm/schemas/complex/enum_type.js +217 -0
  58. package/esm/schemas/complex/fixed_type.d.ts +99 -0
  59. package/esm/schemas/complex/fixed_type.js +208 -0
  60. package/esm/schemas/complex/map_type.d.ts +97 -0
  61. package/esm/schemas/complex/map_type.js +290 -0
  62. package/esm/schemas/complex/named_type.d.ts +30 -0
  63. package/esm/schemas/complex/named_type.js +57 -0
  64. package/esm/schemas/complex/record_type.d.ts +165 -0
  65. package/esm/schemas/complex/record_type.js +547 -0
  66. package/esm/schemas/complex/resolve_names.d.ts +32 -0
  67. package/esm/schemas/complex/resolve_names.js +85 -0
  68. package/esm/schemas/complex/union_type.d.ts +116 -0
  69. package/esm/schemas/complex/union_type.js +392 -0
  70. package/esm/schemas/error.d.ts +31 -0
  71. package/esm/schemas/error.js +67 -0
  72. package/esm/schemas/json.d.ts +6 -0
  73. package/esm/schemas/json.js +35 -0
  74. package/esm/schemas/logical/decimal_logical_type.d.ts +49 -0
  75. package/esm/schemas/logical/decimal_logical_type.js +145 -0
  76. package/esm/schemas/logical/duration_logical_type.d.ts +66 -0
  77. package/esm/schemas/logical/duration_logical_type.js +112 -0
  78. package/esm/schemas/logical/logical_type.d.ts +138 -0
  79. package/esm/schemas/logical/logical_type.js +240 -0
  80. package/esm/schemas/logical/temporal_logical_types.d.ts +215 -0
  81. package/esm/schemas/logical/temporal_logical_types.js +364 -0
  82. package/esm/schemas/logical/uuid_logical_type.d.ts +63 -0
  83. package/esm/schemas/logical/uuid_logical_type.js +146 -0
  84. package/esm/schemas/primitive/boolean_type.d.ts +49 -0
  85. package/esm/schemas/primitive/boolean_type.js +75 -0
  86. package/esm/schemas/primitive/bytes_type.d.ts +49 -0
  87. package/esm/schemas/primitive/bytes_type.js +136 -0
  88. package/esm/schemas/primitive/double_type.d.ts +38 -0
  89. package/esm/schemas/primitive/double_type.js +98 -0
  90. package/esm/schemas/primitive/fixed_size_base_type.d.ts +28 -0
  91. package/esm/schemas/primitive/fixed_size_base_type.js +33 -0
  92. package/esm/schemas/primitive/float_type.d.ts +38 -0
  93. package/esm/schemas/primitive/float_type.js +88 -0
  94. package/esm/schemas/primitive/int_type.d.ts +31 -0
  95. package/esm/schemas/primitive/int_type.js +63 -0
  96. package/esm/schemas/primitive/long_type.d.ts +36 -0
  97. package/esm/schemas/primitive/long_type.js +95 -0
  98. package/esm/schemas/primitive/null_type.d.ts +45 -0
  99. package/esm/schemas/primitive/null_type.js +71 -0
  100. package/esm/schemas/primitive/primitive_type.d.ts +17 -0
  101. package/esm/schemas/primitive/primitive_type.js +27 -0
  102. package/esm/schemas/primitive/string_type.d.ts +34 -0
  103. package/esm/schemas/primitive/string_type.js +81 -0
  104. package/esm/schemas/resolver.d.ts +25 -0
  105. package/esm/schemas/resolver.js +25 -0
  106. package/esm/schemas/type.d.ts +101 -0
  107. package/esm/schemas/type.js +6 -0
  108. package/esm/serialization/avro_constants.d.ts +13 -0
  109. package/esm/serialization/avro_constants.js +29 -0
  110. package/esm/serialization/avro_file_parser.d.ts +68 -0
  111. package/esm/serialization/avro_file_parser.js +191 -0
  112. package/esm/serialization/avro_file_writer.d.ts +63 -0
  113. package/esm/serialization/avro_file_writer.js +235 -0
  114. package/esm/serialization/buffers/blob_readable_buffer.d.ts +53 -0
  115. package/esm/serialization/buffers/blob_readable_buffer.js +80 -0
  116. package/esm/serialization/buffers/buffer.d.ts +37 -0
  117. package/esm/serialization/buffers/buffer.js +1 -0
  118. package/esm/serialization/buffers/in_memory_buffer.d.ts +121 -0
  119. package/esm/serialization/buffers/in_memory_buffer.js +206 -0
  120. package/esm/serialization/clamp.d.ts +10 -0
  121. package/esm/serialization/clamp.js +19 -0
  122. package/esm/serialization/compare_bytes.d.ts +9 -0
  123. package/esm/serialization/compare_bytes.js +45 -0
  124. package/esm/serialization/conversion.d.ts +8 -0
  125. package/esm/serialization/conversion.js +15 -0
  126. package/esm/serialization/decoders/decoder.d.ts +16 -0
  127. package/esm/serialization/decoders/decoder.js +1 -0
  128. package/esm/serialization/decoders/deflate_decoder.d.ts +15 -0
  129. package/esm/serialization/decoders/deflate_decoder.js +26 -0
  130. package/esm/serialization/decoders/null_decoder.d.ts +12 -0
  131. package/esm/serialization/decoders/null_decoder.js +13 -0
  132. package/esm/serialization/encoders/deflate_encoder.d.ts +15 -0
  133. package/esm/serialization/encoders/deflate_encoder.js +26 -0
  134. package/esm/serialization/encoders/encoder.d.ts +16 -0
  135. package/esm/serialization/encoders/encoder.js +1 -0
  136. package/esm/serialization/encoders/null_encoder.d.ts +12 -0
  137. package/esm/serialization/encoders/null_encoder.js +13 -0
  138. package/esm/serialization/manipulate_bytes.d.ts +6 -0
  139. package/esm/serialization/manipulate_bytes.js +13 -0
  140. package/esm/serialization/read_uint_le.d.ts +4 -0
  141. package/esm/serialization/read_uint_le.js +14 -0
  142. package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.d.ts +52 -0
  143. package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.js +129 -0
  144. package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.d.ts +36 -0
  145. package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.js +114 -0
  146. package/esm/serialization/streams/stream_readable_buffer.d.ts +41 -0
  147. package/esm/serialization/streams/stream_readable_buffer.js +64 -0
  148. package/esm/serialization/streams/stream_readable_buffer_adapter.d.ts +38 -0
  149. package/esm/serialization/streams/stream_readable_buffer_adapter.js +105 -0
  150. package/esm/serialization/streams/stream_writable_buffer.d.ts +42 -0
  151. package/esm/serialization/streams/stream_writable_buffer.js +64 -0
  152. package/esm/serialization/streams/stream_writable_buffer_adapter.d.ts +36 -0
  153. package/esm/serialization/streams/stream_writable_buffer_adapter.js +65 -0
  154. package/esm/serialization/streams/streams.d.ts +29 -0
  155. package/esm/serialization/streams/streams.js +1 -0
  156. package/esm/serialization/tap.d.ts +427 -0
  157. package/esm/serialization/tap.js +605 -0
  158. package/esm/serialization/text_encoding.d.ts +12 -0
  159. package/esm/serialization/text_encoding.js +14 -0
  160. package/esm/type/create_type.d.ts +54 -0
  161. package/esm/type/create_type.js +401 -0
  162. package/package.json +33 -0
  163. package/sachitv-avro-typescript-0.4.0.tgz +0 -0
@@ -0,0 +1,547 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _a, _RecordField_name, _RecordField_type, _RecordField_aliases, _RecordField_order, _RecordField_hasDefault, _RecordField_defaultValue, _RecordField_isValidOrder, _RecordType_instances, _RecordType_fields, _RecordType_fieldNameToIndex, _RecordType_fieldsThunk, _RecordType_ensureFields, _RecordType_setFields, _RecordType_getOrderValue, _RecordType_isRecord, _RecordType_extractFieldValue, _RecordType_checkField, _RecordType_writeField, _RecordType_getFieldBuffer, _RecordType_cloneField, _RecordType_getComparableValue, _RecordResolver_mappings, _RecordResolver_readerFields;
13
+ import { NamedType } from "./named_type.js";
14
+ import { Resolver } from "../resolver.js";
15
+ import { Type } from "../type.js";
16
+ import { throwInvalidError } from "../error.js";
17
+ import { isValidName } from "./resolve_names.js";
18
+ /**
19
+ * Represents a field in an Avro record type.
20
+ */
21
+ export class RecordField {
22
+ /**
23
+ * Constructs a new RecordField instance.
24
+ */
25
+ constructor(params) {
26
+ _RecordField_name.set(this, void 0);
27
+ _RecordField_type.set(this, void 0);
28
+ _RecordField_aliases.set(this, void 0);
29
+ _RecordField_order.set(this, void 0);
30
+ _RecordField_hasDefault.set(this, void 0);
31
+ _RecordField_defaultValue.set(this, void 0);
32
+ const { name, type, aliases = [], order = "ascending" } = params;
33
+ if (typeof name !== "string" || !isValidName(name)) {
34
+ throw new Error(`Invalid record field name: ${name}`);
35
+ }
36
+ if (!(type instanceof Type)) {
37
+ throw new Error(`Invalid field type for ${name}`);
38
+ }
39
+ if (!__classPrivateFieldGet(_a, _a, "m", _RecordField_isValidOrder).call(_a, order)) {
40
+ throw new Error(`Invalid record field order: ${order}`);
41
+ }
42
+ __classPrivateFieldSet(this, _RecordField_name, name, "f");
43
+ __classPrivateFieldSet(this, _RecordField_type, type, "f");
44
+ __classPrivateFieldSet(this, _RecordField_order, order, "f");
45
+ const aliasSet = new Set();
46
+ const resolvedAliases = [];
47
+ for (const alias of aliases) {
48
+ if (typeof alias !== "string" || !isValidName(alias)) {
49
+ throw new Error(`Invalid record field alias: ${alias}`);
50
+ }
51
+ if (!aliasSet.has(alias)) {
52
+ aliasSet.add(alias);
53
+ resolvedAliases.push(alias);
54
+ }
55
+ }
56
+ __classPrivateFieldSet(this, _RecordField_aliases, resolvedAliases, "f");
57
+ __classPrivateFieldSet(this, _RecordField_hasDefault, Object.prototype.hasOwnProperty.call(params, "default"), "f");
58
+ if (__classPrivateFieldGet(this, _RecordField_hasDefault, "f")) {
59
+ __classPrivateFieldSet(this, _RecordField_defaultValue, __classPrivateFieldGet(this, _RecordField_type, "f").cloneFromValue(params.default), "f");
60
+ }
61
+ }
62
+ /**
63
+ * Gets the name of the field.
64
+ */
65
+ getName() {
66
+ return __classPrivateFieldGet(this, _RecordField_name, "f");
67
+ }
68
+ /**
69
+ * Gets the type of the field.
70
+ */
71
+ getType() {
72
+ return __classPrivateFieldGet(this, _RecordField_type, "f");
73
+ }
74
+ /**
75
+ * Gets the aliases of the field.
76
+ */
77
+ getAliases() {
78
+ return __classPrivateFieldGet(this, _RecordField_aliases, "f").slice();
79
+ }
80
+ /**
81
+ * Gets the order of the field.
82
+ */
83
+ getOrder() {
84
+ return __classPrivateFieldGet(this, _RecordField_order, "f");
85
+ }
86
+ /**
87
+ * Returns true if the field has a default value.
88
+ */
89
+ hasDefault() {
90
+ return __classPrivateFieldGet(this, _RecordField_hasDefault, "f");
91
+ }
92
+ /**
93
+ * Gets the default value for the field.
94
+ * @returns The default value.
95
+ * @throws Error if the field has no default.
96
+ */
97
+ getDefault() {
98
+ if (!__classPrivateFieldGet(this, _RecordField_hasDefault, "f")) {
99
+ throw new Error(`Field '${__classPrivateFieldGet(this, _RecordField_name, "f")}' has no default.`);
100
+ }
101
+ return __classPrivateFieldGet(this, _RecordField_type, "f").cloneFromValue(__classPrivateFieldGet(this, _RecordField_defaultValue, "f"));
102
+ }
103
+ /**
104
+ * Checks if the given name matches the field name or any of its aliases.
105
+ * @param name The name to check.
106
+ * @returns True if the name matches, false otherwise.
107
+ */
108
+ nameMatches(name) {
109
+ return name === __classPrivateFieldGet(this, _RecordField_name, "f") || __classPrivateFieldGet(this, _RecordField_aliases, "f").includes(name);
110
+ }
111
+ }
112
+ _a = RecordField, _RecordField_name = new WeakMap(), _RecordField_type = new WeakMap(), _RecordField_aliases = new WeakMap(), _RecordField_order = new WeakMap(), _RecordField_hasDefault = new WeakMap(), _RecordField_defaultValue = new WeakMap(), _RecordField_isValidOrder = function _RecordField_isValidOrder(order) {
113
+ return order === "ascending" || order === "descending" ||
114
+ order === "ignore";
115
+ };
116
+ /**
117
+ * Avro `record` type supporting ordered fields, aliases, and schema evolution.
118
+ */
119
+ export class RecordType extends NamedType {
120
+ /**
121
+ * Creates a new RecordType.
122
+ * @param params The record type parameters.
123
+ */
124
+ constructor(params) {
125
+ const { fields, ...names } = params;
126
+ super(names);
127
+ _RecordType_instances.add(this);
128
+ _RecordType_fields.set(this, void 0);
129
+ _RecordType_fieldNameToIndex.set(this, void 0);
130
+ _RecordType_fieldsThunk.set(this, void 0);
131
+ __classPrivateFieldSet(this, _RecordType_fields, [], "f");
132
+ __classPrivateFieldSet(this, _RecordType_fieldNameToIndex, new Map(), "f");
133
+ if (typeof fields === "function") {
134
+ // Defer field materialization until the first time the record is used.
135
+ // This mirrors the classic Avro parsing strategy where named types are
136
+ // registered before their fields are resolved, allowing recursive schemas.
137
+ __classPrivateFieldSet(this, _RecordType_fieldsThunk, fields, "f");
138
+ }
139
+ else {
140
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_setFields).call(this, fields);
141
+ }
142
+ }
143
+ /**
144
+ * Gets the fields of the record.
145
+ */
146
+ getFields() {
147
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
148
+ return __classPrivateFieldGet(this, _RecordType_fields, "f").slice();
149
+ }
150
+ /**
151
+ * Gets a specific field by name.
152
+ */
153
+ getField(name) {
154
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
155
+ const index = __classPrivateFieldGet(this, _RecordType_fieldNameToIndex, "f").get(name);
156
+ if (index === undefined) {
157
+ return undefined;
158
+ }
159
+ return __classPrivateFieldGet(this, _RecordType_fields, "f")[index];
160
+ }
161
+ /**
162
+ * Checks if the given value conforms to this record type.
163
+ * @param value The value to check.
164
+ * @param errorHook Optional error hook for validation errors.
165
+ * @param path The current path in the schema for error reporting.
166
+ * @returns True if the value is valid, false otherwise.
167
+ */
168
+ check(value, errorHook, path = []) {
169
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
170
+ if (!__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_isRecord).call(this, value)) {
171
+ if (errorHook) {
172
+ errorHook(path.slice(), value, this);
173
+ }
174
+ return false;
175
+ }
176
+ const record = value;
177
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
178
+ if (!__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_checkField).call(this, field, record, errorHook, path)) {
179
+ return false;
180
+ }
181
+ }
182
+ return true;
183
+ }
184
+ /**
185
+ * Writes the given record value to the tap.
186
+ * @param tap The writable tap to write to.
187
+ * @param value The record value to write.
188
+ */
189
+ async write(tap, value) {
190
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
191
+ if (!__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_isRecord).call(this, value)) {
192
+ throwInvalidError([], value, this);
193
+ }
194
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
195
+ await __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_writeField).call(this, field, value, tap);
196
+ }
197
+ }
198
+ /**
199
+ * Reads a record value from the tap.
200
+ * @param tap The readable tap to read from.
201
+ * @returns The read record value.
202
+ */
203
+ async read(tap) {
204
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
205
+ const result = {};
206
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
207
+ result[field.getName()] = await field.getType().read(tap);
208
+ }
209
+ return result;
210
+ }
211
+ /**
212
+ * Skips a record value in the tap.
213
+ * @param tap The readable tap to skip in.
214
+ */
215
+ async skip(tap) {
216
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
217
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
218
+ await field.getType().skip(tap);
219
+ }
220
+ }
221
+ /**
222
+ * Converts the given record value to a buffer.
223
+ * @param value The record value to convert.
224
+ * @returns The buffer representation of the value.
225
+ */
226
+ async toBuffer(value) {
227
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
228
+ if (!__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_isRecord).call(this, value)) {
229
+ throwInvalidError([], value, this);
230
+ }
231
+ const buffers = [];
232
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
233
+ buffers.push(await __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_getFieldBuffer).call(this, field, value));
234
+ }
235
+ const totalSize = buffers.reduce((sum, buf) => sum + buf.byteLength, 0);
236
+ const combined = new Uint8Array(totalSize);
237
+ let offset = 0;
238
+ for (const buf of buffers) {
239
+ combined.set(buf, offset);
240
+ offset += buf.byteLength;
241
+ }
242
+ return combined.buffer;
243
+ }
244
+ /**
245
+ * Clones a record value, ensuring it conforms to the schema.
246
+ * @param value The value to clone.
247
+ * @returns The cloned record value.
248
+ */
249
+ cloneFromValue(value) {
250
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
251
+ if (!__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_isRecord).call(this, value)) {
252
+ throw new Error("Cannot clone non-record value.");
253
+ }
254
+ const recordValue = value;
255
+ const result = {};
256
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
257
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_cloneField).call(this, field, recordValue, result);
258
+ }
259
+ return result;
260
+ }
261
+ /**
262
+ * Compares two record values for ordering.
263
+ * @param val1 The first record value.
264
+ * @param val2 The second record value.
265
+ * @returns A negative number if val1 < val2, zero if equal, positive if val1 > val2.
266
+ */
267
+ compare(val1, val2) {
268
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
269
+ if (!__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_isRecord).call(this, val1) || !__classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_isRecord).call(this, val2)) {
270
+ throw new Error("Record comparison requires object values.");
271
+ }
272
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
273
+ const order = field.getOrder();
274
+ if (order === "ignore") {
275
+ continue;
276
+ }
277
+ const v1 = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_getComparableValue).call(this, val1, field);
278
+ const v2 = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_getComparableValue).call(this, val2, field);
279
+ let comparison = field.getType().compare(v1, v2);
280
+ if (comparison !== 0) {
281
+ if (order === "descending") {
282
+ comparison = -comparison;
283
+ }
284
+ return comparison;
285
+ }
286
+ }
287
+ return 0;
288
+ }
289
+ /**
290
+ * Generates a random record value conforming to this schema.
291
+ * @returns A random record value.
292
+ */
293
+ random() {
294
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
295
+ const result = {};
296
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
297
+ result[field.getName()] = field.getType().random();
298
+ }
299
+ return result;
300
+ }
301
+ /**
302
+ * Converts this record type to its JSON schema representation.
303
+ * @returns The JSON representation of the record type.
304
+ */
305
+ toJSON() {
306
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
307
+ const fieldsJson = __classPrivateFieldGet(this, _RecordType_fields, "f").map((field) => {
308
+ const fieldJson = {
309
+ name: field.getName(),
310
+ type: field.getType().toJSON(),
311
+ };
312
+ if (field.hasDefault()) {
313
+ fieldJson.default = field.getDefault();
314
+ }
315
+ const aliases = field.getAliases();
316
+ if (aliases.length > 0) {
317
+ fieldJson.aliases = aliases;
318
+ }
319
+ if (field.getOrder() !== "ascending") {
320
+ fieldJson.order = field.getOrder();
321
+ }
322
+ return fieldJson;
323
+ });
324
+ return {
325
+ name: this.getFullName(),
326
+ type: "record",
327
+ fields: fieldsJson,
328
+ };
329
+ }
330
+ /**
331
+ * Matches two record values from the taps for comparison.
332
+ * @param tap1 The first readable tap.
333
+ * @param tap2 The second readable tap.
334
+ * @returns A comparison result.
335
+ */
336
+ async match(tap1, tap2) {
337
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
338
+ for (const field of __classPrivateFieldGet(this, _RecordType_fields, "f")) {
339
+ const order = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_getOrderValue).call(this, field.getOrder());
340
+ const type = field.getType();
341
+ if (order !== 0) {
342
+ const result = (await type.match(tap1, tap2)) * order;
343
+ if (result !== 0) {
344
+ return result;
345
+ }
346
+ }
347
+ else {
348
+ await type.skip(tap1);
349
+ await type.skip(tap2);
350
+ }
351
+ }
352
+ return 0;
353
+ }
354
+ /**
355
+ * Creates a resolver for schema evolution from the writer type to this reader type.
356
+ * @param writerType The writer type to resolve from.
357
+ * @returns A resolver for reading writer data with this schema.
358
+ */
359
+ createResolver(writerType) {
360
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_ensureFields).call(this);
361
+ if (!(writerType instanceof RecordType)) {
362
+ return super.createResolver(writerType);
363
+ }
364
+ const acceptableNames = new Set([
365
+ this.getFullName(),
366
+ ...this.getAliases(),
367
+ ]);
368
+ if (!acceptableNames.has(writerType.getFullName())) {
369
+ throw new Error(`Schema evolution not supported from writer type: ${writerType.getFullName()} to reader type: ${this.getFullName()}`);
370
+ }
371
+ const readerFields = __classPrivateFieldGet(this, _RecordType_fields, "f");
372
+ const writerFields = writerType.getFields();
373
+ const readerNameToIndex = new Map();
374
+ readerFields.forEach((field, index) => {
375
+ readerNameToIndex.set(field.getName(), index);
376
+ field.getAliases().forEach((alias) => {
377
+ readerNameToIndex.set(alias, index);
378
+ });
379
+ });
380
+ const assignedReaderIndexes = new Set();
381
+ const mappings = [];
382
+ writerFields.forEach((writerField) => {
383
+ let readerIndex = readerNameToIndex.get(writerField.getName());
384
+ if (readerIndex === undefined) {
385
+ for (const alias of writerField.getAliases()) {
386
+ const idx = readerNameToIndex.get(alias);
387
+ if (idx !== undefined) {
388
+ readerIndex = idx;
389
+ break;
390
+ }
391
+ }
392
+ }
393
+ if (readerIndex === undefined) {
394
+ mappings.push({
395
+ readerIndex: -1,
396
+ writerField,
397
+ });
398
+ return;
399
+ }
400
+ if (assignedReaderIndexes.has(readerIndex)) {
401
+ throw new Error(`Multiple writer fields map to reader field: ${readerFields[readerIndex].getName()}`);
402
+ }
403
+ const readerField = readerFields[readerIndex];
404
+ const readerType = readerField.getType();
405
+ const writerType = writerField.getType();
406
+ const resolver = readerType === writerType
407
+ ? undefined
408
+ : readerType.createResolver(writerType);
409
+ mappings.push({
410
+ readerIndex,
411
+ writerField,
412
+ resolver,
413
+ });
414
+ assignedReaderIndexes.add(readerIndex);
415
+ });
416
+ readerFields.forEach((field, index) => {
417
+ if (!assignedReaderIndexes.has(index) && !field.hasDefault()) {
418
+ throw new Error(`Field '${field.getName()}' missing from writer schema and has no default.`);
419
+ }
420
+ });
421
+ return new RecordResolver(this, mappings, readerFields);
422
+ }
423
+ }
424
+ _RecordType_fields = new WeakMap(), _RecordType_fieldNameToIndex = new WeakMap(), _RecordType_fieldsThunk = new WeakMap(), _RecordType_instances = new WeakSet(), _RecordType_ensureFields = function _RecordType_ensureFields() {
425
+ if (__classPrivateFieldGet(this, _RecordType_fieldsThunk, "f")) {
426
+ const builder = __classPrivateFieldGet(this, _RecordType_fieldsThunk, "f");
427
+ __classPrivateFieldSet(this, _RecordType_fieldsThunk, undefined, "f");
428
+ const resolved = builder();
429
+ __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_setFields).call(this, resolved);
430
+ }
431
+ }, _RecordType_setFields = function _RecordType_setFields(candidate) {
432
+ if (!Array.isArray(candidate)) {
433
+ throw new Error("RecordType requires a fields array.");
434
+ }
435
+ __classPrivateFieldSet(this, _RecordType_fields, [], "f");
436
+ __classPrivateFieldGet(this, _RecordType_fieldNameToIndex, "f").clear();
437
+ candidate.forEach((fieldParams) => {
438
+ const field = new RecordField(fieldParams);
439
+ if (__classPrivateFieldGet(this, _RecordType_fieldNameToIndex, "f").has(field.getName())) {
440
+ throw new Error(`Duplicate record field name: ${field.getName()}`);
441
+ }
442
+ __classPrivateFieldGet(this, _RecordType_fieldNameToIndex, "f").set(field.getName(), __classPrivateFieldGet(this, _RecordType_fields, "f").length);
443
+ __classPrivateFieldGet(this, _RecordType_fields, "f").push(field);
444
+ });
445
+ }, _RecordType_getOrderValue = function _RecordType_getOrderValue(order) {
446
+ switch (order) {
447
+ case "ascending":
448
+ return 1;
449
+ case "descending":
450
+ return -1;
451
+ case "ignore":
452
+ return 0;
453
+ }
454
+ }, _RecordType_isRecord = function _RecordType_isRecord(value) {
455
+ return typeof value === "object" && value !== null && !Array.isArray(value);
456
+ }, _RecordType_extractFieldValue = function _RecordType_extractFieldValue(record, field) {
457
+ const name = field.getName();
458
+ const hasValue = Object.hasOwn(record, name);
459
+ const fieldValue = hasValue ? record[name] : undefined;
460
+ return { hasValue, fieldValue };
461
+ }, _RecordType_checkField = function _RecordType_checkField(field, record, errorHook, path) {
462
+ const { hasValue, fieldValue } = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_extractFieldValue).call(this, record, field);
463
+ if (!hasValue) {
464
+ if (!field.hasDefault()) {
465
+ if (errorHook) {
466
+ errorHook([...path, field.getName()], undefined, this);
467
+ }
468
+ return false;
469
+ }
470
+ return true;
471
+ }
472
+ const nextPath = errorHook ? [...path, field.getName()] : undefined;
473
+ const valid = field.getType().check(fieldValue, errorHook, nextPath);
474
+ return valid || (errorHook !== undefined);
475
+ }, _RecordType_writeField = async function _RecordType_writeField(field, record, tap) {
476
+ const { hasValue, fieldValue } = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_extractFieldValue).call(this, record, field);
477
+ let toWrite = fieldValue;
478
+ if (!hasValue) {
479
+ if (!field.hasDefault()) {
480
+ throwInvalidError([field.getName()], undefined, this);
481
+ }
482
+ toWrite = field.getDefault();
483
+ }
484
+ await field.getType().write(tap, toWrite);
485
+ }, _RecordType_getFieldBuffer = async function _RecordType_getFieldBuffer(field, record) {
486
+ const { hasValue, fieldValue } = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_extractFieldValue).call(this, record, field);
487
+ let toEncode = fieldValue;
488
+ if (!hasValue) {
489
+ if (!field.hasDefault()) {
490
+ throwInvalidError([field.getName()], undefined, this);
491
+ }
492
+ toEncode = field.getDefault();
493
+ }
494
+ return new Uint8Array(await field.getType().toBuffer(toEncode));
495
+ }, _RecordType_cloneField = function _RecordType_cloneField(field, record, result) {
496
+ const { hasValue, fieldValue } = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_extractFieldValue).call(this, record, field);
497
+ if (!hasValue) {
498
+ if (!field.hasDefault()) {
499
+ throw new Error(`Missing value for record field ${field.getName()} with no default.`);
500
+ }
501
+ result[field.getName()] = field.getDefault();
502
+ return;
503
+ }
504
+ result[field.getName()] = field.getType().cloneFromValue(fieldValue);
505
+ }, _RecordType_getComparableValue = function _RecordType_getComparableValue(record, field) {
506
+ const { hasValue, fieldValue } = __classPrivateFieldGet(this, _RecordType_instances, "m", _RecordType_extractFieldValue).call(this, record, field);
507
+ if (hasValue) {
508
+ return fieldValue;
509
+ }
510
+ if (field.hasDefault()) {
511
+ return field.getDefault();
512
+ }
513
+ throw new Error(`Missing comparable value for field '${field.getName()}' with no default.`);
514
+ };
515
+ class RecordResolver extends Resolver {
516
+ constructor(reader, mappings, readerFields) {
517
+ super(reader);
518
+ _RecordResolver_mappings.set(this, void 0);
519
+ _RecordResolver_readerFields.set(this, void 0);
520
+ __classPrivateFieldSet(this, _RecordResolver_mappings, mappings, "f");
521
+ __classPrivateFieldSet(this, _RecordResolver_readerFields, readerFields, "f");
522
+ }
523
+ async read(tap) {
524
+ const result = {};
525
+ const seen = new Array(__classPrivateFieldGet(this, _RecordResolver_readerFields, "f").length).fill(false);
526
+ for (const mapping of __classPrivateFieldGet(this, _RecordResolver_mappings, "f")) {
527
+ if (mapping.readerIndex === -1) {
528
+ await mapping.writerField.getType().skip(tap);
529
+ continue;
530
+ }
531
+ const value = mapping.resolver
532
+ ? await mapping.resolver.read(tap)
533
+ : await mapping.writerField.getType().read(tap);
534
+ const readerField = __classPrivateFieldGet(this, _RecordResolver_readerFields, "f")[mapping.readerIndex];
535
+ result[readerField.getName()] = value;
536
+ seen[mapping.readerIndex] = true;
537
+ }
538
+ for (let i = 0; i < __classPrivateFieldGet(this, _RecordResolver_readerFields, "f").length; i++) {
539
+ if (!seen[i]) {
540
+ const field = __classPrivateFieldGet(this, _RecordResolver_readerFields, "f")[i];
541
+ result[field.getName()] = field.getDefault();
542
+ }
543
+ }
544
+ return result;
545
+ }
546
+ }
547
+ _RecordResolver_mappings = new WeakMap(), _RecordResolver_readerFields = new WeakMap();
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Checks if the given name is a valid Avro identifier.
3
+ * @param name - The name to validate.
4
+ * @returns True if the name is valid, false otherwise.
5
+ */
6
+ export declare function isValidName(name: string): boolean;
7
+ /**
8
+ * Parameters for resolving Avro names, including namespace qualification and aliases.
9
+ */
10
+ export interface ResolveNamesParams {
11
+ /** The base name of the type. */
12
+ name: string;
13
+ /** Optional namespace to qualify the name. */
14
+ namespace?: string;
15
+ /** Optional list of aliases for the type. */
16
+ aliases?: string[];
17
+ }
18
+ /**
19
+ * Represents the resolved names for an Avro type, including its full name, aliases, and namespace.
20
+ */
21
+ export interface ResolvedNames {
22
+ /** The fully qualified name of the type. */
23
+ fullName: string;
24
+ /** The resolved aliases for the type. */
25
+ aliases: string[];
26
+ /** The namespace of the type. */
27
+ namespace: string;
28
+ }
29
+ /**
30
+ * Resolves the full name, aliases, and namespace for an Avro type based on the provided parameters.
31
+ */
32
+ export declare function resolveNames({ name, namespace, aliases }: ResolveNamesParams): ResolvedNames;
@@ -0,0 +1,85 @@
1
+ const NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
2
+ /**
3
+ * Checks if the given name is a valid Avro identifier.
4
+ * @param name - The name to validate.
5
+ * @returns True if the name is valid, false otherwise.
6
+ */
7
+ export function isValidName(name) {
8
+ return NAME_PATTERN.test(name);
9
+ }
10
+ const PRIMITIVE_TYPE_NAMES = new Set([
11
+ "null",
12
+ "boolean",
13
+ "int",
14
+ "long",
15
+ "float",
16
+ "double",
17
+ "bytes",
18
+ "string",
19
+ ]);
20
+ /**
21
+ * Resolves the full name, aliases, and namespace for an Avro type based on the provided parameters.
22
+ */
23
+ export function resolveNames({ name, namespace, aliases = [] }) {
24
+ const fullName = qualifyName(name, namespace);
25
+ const typeNamespace = getNamespaceFromName(fullName);
26
+ const resolvedAliases = [];
27
+ const seen = new Set();
28
+ seen.add(fullName);
29
+ for (const alias of aliases) {
30
+ const qualified = qualifyAlias(alias, typeNamespace);
31
+ if (!seen.has(qualified)) {
32
+ seen.add(qualified);
33
+ resolvedAliases.push(qualified);
34
+ }
35
+ }
36
+ return {
37
+ fullName,
38
+ aliases: resolvedAliases,
39
+ namespace: typeNamespace ?? "",
40
+ };
41
+ }
42
+ function qualifyName(name, namespace) {
43
+ if (!name) {
44
+ throw new Error("Avro name is required.");
45
+ }
46
+ let qualified = name;
47
+ if (namespace && !name.includes(".")) {
48
+ qualified = `${namespace}.${name}`;
49
+ }
50
+ validateFullName(qualified, false);
51
+ return qualified;
52
+ }
53
+ function qualifyAlias(alias, namespace) {
54
+ if (!alias) {
55
+ throw new Error("Avro alias is required.");
56
+ }
57
+ let qualified = alias;
58
+ if (namespace && !alias.includes(".")) {
59
+ qualified = `${namespace}.${alias}`;
60
+ }
61
+ validateFullName(qualified, true);
62
+ return qualified;
63
+ }
64
+ function validateFullName(name, isAlias) {
65
+ const parts = name.split(".");
66
+ parts.forEach((part) => {
67
+ if (!isValidName(part)) {
68
+ throw new Error(`${isAlias ? "Invalid Avro alias: " : "Invalid Avro name: "}${name}`);
69
+ }
70
+ });
71
+ const tail = parts[parts.length - 1];
72
+ if (PRIMITIVE_TYPE_NAMES.has(tail)) {
73
+ throw new Error(`${isAlias
74
+ ? "Cannot rename primitive Avro alias: "
75
+ : "Cannot rename primitive Avro type: "}${tail}`);
76
+ }
77
+ }
78
+ function getNamespaceFromName(name) {
79
+ const parts = name.split(".");
80
+ if (parts.length <= 1) {
81
+ return undefined;
82
+ }
83
+ parts.pop();
84
+ return parts.join(".");
85
+ }