@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,285 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Protocol_name, _Protocol_namespace, _Protocol_messages, _Protocol_types, _Protocol_parent, _Protocol_handlers, _Protocol_hashBytes, _Protocol_hashKey, _Protocol_emitterResolvers, _Protocol_listenerResolvers;
13
+ import { createType } from "../type/create_type.js";
14
+ import { NamedType } from "../schemas/complex/named_type.js";
15
+ import { md5FromString } from "../internal/crypto/md5.js";
16
+ import { Message } from "./definitions/message_definition.js";
17
+ import { bytesToHex, normalizeProtocolName, stringifyProtocol, } from "./protocol/protocol_helpers.js";
18
+ import { StatelessListener } from "./message_endpoint/listener.js";
19
+ import { StatelessEmitter } from "./message_endpoint/emitter.js";
20
+ /**
21
+ * Core Avro RPC protocol representation that validates schemas, hashes the
22
+ * protocol definition, and coordinates message handlers and transports.
23
+ */
24
+ export class Protocol {
25
+ constructor(name, namespace, messages, types, parent) {
26
+ _Protocol_name.set(this, void 0);
27
+ _Protocol_namespace.set(this, void 0);
28
+ _Protocol_messages.set(this, void 0);
29
+ _Protocol_types.set(this, void 0);
30
+ _Protocol_parent.set(this, void 0);
31
+ _Protocol_handlers.set(this, new Map());
32
+ _Protocol_hashBytes.set(this, void 0);
33
+ _Protocol_hashKey.set(this, void 0);
34
+ _Protocol_emitterResolvers.set(this, new Map());
35
+ _Protocol_listenerResolvers.set(this, new Map());
36
+ __classPrivateFieldSet(this, _Protocol_name, name, "f");
37
+ __classPrivateFieldSet(this, _Protocol_namespace, namespace, "f");
38
+ __classPrivateFieldSet(this, _Protocol_messages, messages, "f");
39
+ __classPrivateFieldSet(this, _Protocol_types, types, "f");
40
+ __classPrivateFieldSet(this, _Protocol_parent, parent, "f");
41
+ const canonical = stringifyProtocol(this);
42
+ __classPrivateFieldSet(this, _Protocol_hashBytes, md5FromString(canonical), "f");
43
+ __classPrivateFieldSet(this, _Protocol_hashKey, bytesToHex(__classPrivateFieldGet(this, _Protocol_hashBytes, "f")), "f");
44
+ }
45
+ /**
46
+ * Creates a new Protocol instance from a definition.
47
+ * @param attrs The protocol definition object.
48
+ * @param opts Protocol creation options.
49
+ * @returns A new Protocol instance.
50
+ */
51
+ static create(attrs, opts = {}) {
52
+ const name = normalizeProtocolName(attrs.protocol, attrs.namespace ?? opts.namespace);
53
+ const registry = opts.registry ?? new Map();
54
+ const createOpts = {
55
+ namespace: attrs.namespace ?? opts.namespace,
56
+ registry,
57
+ };
58
+ if (attrs.types) {
59
+ for (const schema of attrs.types) {
60
+ createType(schema, createOpts);
61
+ }
62
+ }
63
+ const messages = new Map();
64
+ if (attrs.messages) {
65
+ for (const [messageName, definition] of Object.entries(attrs.messages)) {
66
+ messages.set(messageName, new Message(messageName, definition, createOpts));
67
+ }
68
+ }
69
+ return new Protocol(name, attrs.namespace ?? opts.namespace, messages, registry);
70
+ }
71
+ /**
72
+ * Creates a subprotocol sharing the same context but without a parent reference.
73
+ * @returns A new Protocol instance.
74
+ */
75
+ subprotocol() {
76
+ return new Protocol(__classPrivateFieldGet(this, _Protocol_name, "f"), __classPrivateFieldGet(this, _Protocol_namespace, "f"), __classPrivateFieldGet(this, _Protocol_messages, "f"), __classPrivateFieldGet(this, _Protocol_types, "f"), this);
77
+ }
78
+ /**
79
+ * Gets the protocol name.
80
+ */
81
+ getName() {
82
+ return __classPrivateFieldGet(this, _Protocol_name, "f");
83
+ }
84
+ /**
85
+ * Returns the string representation of the protocol (JSON).
86
+ */
87
+ toString() {
88
+ return stringifyProtocol(this);
89
+ }
90
+ /**
91
+ * Gets the MD5 hash of the protocol as a hex string.
92
+ */
93
+ get hashKey() {
94
+ return __classPrivateFieldGet(this, _Protocol_hashKey, "f");
95
+ }
96
+ /**
97
+ * Gets the MD5 hash of the protocol as bytes.
98
+ */
99
+ getHashBytes() {
100
+ return __classPrivateFieldGet(this, _Protocol_hashBytes, "f").slice();
101
+ }
102
+ /**
103
+ * Gets all messages defined in the protocol.
104
+ */
105
+ getMessages() {
106
+ return __classPrivateFieldGet(this, _Protocol_messages, "f");
107
+ }
108
+ /**
109
+ * Gets all named types defined in the protocol.
110
+ */
111
+ getNamedTypes() {
112
+ const named = [];
113
+ for (const type of __classPrivateFieldGet(this, _Protocol_types, "f").values()) {
114
+ if (type instanceof NamedType) {
115
+ named.push(type);
116
+ }
117
+ }
118
+ return named;
119
+ }
120
+ /**
121
+ * Gets a specific message by name.
122
+ */
123
+ getMessage(name) {
124
+ return __classPrivateFieldGet(this, _Protocol_messages, "f").get(name);
125
+ }
126
+ /**
127
+ * Gets the names of all messages in the protocol.
128
+ */
129
+ getMessageNames() {
130
+ return Array.from(__classPrivateFieldGet(this, _Protocol_messages, "f").keys());
131
+ }
132
+ /**
133
+ * Gets the definition of a specific message.
134
+ */
135
+ getMessageDefinition(name) {
136
+ const message = __classPrivateFieldGet(this, _Protocol_messages, "f").get(name);
137
+ if (!message) {
138
+ return undefined;
139
+ }
140
+ const json = message.toJSON();
141
+ // Type assertion is safe here since Message.toJSON() returns a structure
142
+ // compatible with MessageDefinition
143
+ return json;
144
+ }
145
+ /**
146
+ * Gets summary information about the protocol.
147
+ */
148
+ getProtocolInfo() {
149
+ return {
150
+ name: __classPrivateFieldGet(this, _Protocol_name, "f"),
151
+ namespace: __classPrivateFieldGet(this, _Protocol_namespace, "f"),
152
+ hashKey: __classPrivateFieldGet(this, _Protocol_hashKey, "f"),
153
+ hashBytes: __classPrivateFieldGet(this, _Protocol_hashBytes, "f"),
154
+ messageNames: this.getMessageNames(),
155
+ };
156
+ }
157
+ /**
158
+ * Gets a type definition by name.
159
+ */
160
+ getType(name) {
161
+ return __classPrivateFieldGet(this, _Protocol_types, "f").get(name);
162
+ }
163
+ /**
164
+ * Registers a message handler for a specific message name.
165
+ */
166
+ on(name, handler) {
167
+ if (!__classPrivateFieldGet(this, _Protocol_messages, "f").has(name)) {
168
+ throw new Error(`unknown message: ${name}`);
169
+ }
170
+ __classPrivateFieldGet(this, _Protocol_handlers, "f").set(name, handler);
171
+ return this;
172
+ }
173
+ /**
174
+ * Emits a message using the provided emitter.
175
+ * @param name The name of the message to emit.
176
+ * @param request The request payload.
177
+ * @param emitter The emitter to use.
178
+ */
179
+ async emit(name, request, emitter) {
180
+ const message = __classPrivateFieldGet(this, _Protocol_messages, "f").get(name);
181
+ if (!message) {
182
+ throw new Error(`unknown message: ${name}`);
183
+ }
184
+ if (emitter.protocol.hashKey !== this.hashKey) {
185
+ throw new Error("invalid emitter");
186
+ }
187
+ return await emitter.send(message, request);
188
+ }
189
+ /**
190
+ * Creates a new message emitter for this protocol.
191
+ * @param transport The transport factory or instance.
192
+ * @param opts Transport options.
193
+ */
194
+ createEmitter(transport, opts = {}) {
195
+ if (typeof transport === "function") {
196
+ return new StatelessEmitter(this, transport, opts, Protocol.create);
197
+ }
198
+ throw new Error("Stateful transports are not supported yet.");
199
+ }
200
+ /**
201
+ * Creates a new message listener for this protocol.
202
+ * @param transport The transport instance.
203
+ * @param opts Transport options.
204
+ */
205
+ createListener(transport, opts = {}) {
206
+ if (opts.mode && opts.mode !== "stateless") {
207
+ throw new Error("Stateful listeners are not supported yet.");
208
+ }
209
+ return new StatelessListener(this, transport, opts, Protocol.create);
210
+ }
211
+ /**
212
+ * Gets the handler registered for a specific message.
213
+ */
214
+ getHandler(name) {
215
+ const handler = __classPrivateFieldGet(this, _Protocol_handlers, "f").get(name);
216
+ if (handler) {
217
+ return handler;
218
+ }
219
+ return __classPrivateFieldGet(this, _Protocol_parent, "f")?.getHandler(name);
220
+ }
221
+ /**
222
+ * Gets schema resolvers for an emitter (client) given a server hash.
223
+ */
224
+ getEmitterResolvers(hashKey, messageName) {
225
+ return __classPrivateFieldGet(this, _Protocol_emitterResolvers, "f").get(hashKey)?.get(messageName);
226
+ }
227
+ /**
228
+ * Gets schema resolvers for a listener (server) given a client hash.
229
+ */
230
+ getListenerResolvers(hashKey, messageName) {
231
+ return __classPrivateFieldGet(this, _Protocol_listenerResolvers, "f").get(hashKey)?.get(messageName);
232
+ }
233
+ /**
234
+ * Checks if resolvers exist for a given client hash.
235
+ */
236
+ hasListenerResolvers(hashKey) {
237
+ return __classPrivateFieldGet(this, _Protocol_listenerResolvers, "f").has(hashKey);
238
+ }
239
+ /**
240
+ * Checks if resolvers exist for a given server hash.
241
+ */
242
+ hasEmitterResolvers(hashKey) {
243
+ return __classPrivateFieldGet(this, _Protocol_emitterResolvers, "f").has(hashKey);
244
+ }
245
+ /**
246
+ * Ensures resolvers exist for communicating with a remote protocol (client-side).
247
+ */
248
+ ensureEmitterResolvers(hashKey, remote) {
249
+ if (__classPrivateFieldGet(this, _Protocol_emitterResolvers, "f").has(hashKey)) {
250
+ return;
251
+ }
252
+ const resolvers = new Map();
253
+ for (const [name, localMessage] of __classPrivateFieldGet(this, _Protocol_messages, "f").entries()) {
254
+ const remoteMessage = remote.getMessages().get(name);
255
+ if (!remoteMessage) {
256
+ throw new Error(`missing server message: ${name}`);
257
+ }
258
+ resolvers.set(name, {
259
+ response: localMessage.responseType.createResolver(remoteMessage.responseType),
260
+ error: localMessage.errorType.createResolver(remoteMessage.errorType),
261
+ });
262
+ }
263
+ __classPrivateFieldGet(this, _Protocol_emitterResolvers, "f").set(hashKey, resolvers);
264
+ }
265
+ /**
266
+ * Ensures resolvers exist for communicating with a remote protocol (server-side).
267
+ */
268
+ ensureListenerResolvers(hashKey, emitterProtocol) {
269
+ if (__classPrivateFieldGet(this, _Protocol_listenerResolvers, "f").has(hashKey)) {
270
+ return;
271
+ }
272
+ const resolvers = new Map();
273
+ for (const [name, serverMessage] of __classPrivateFieldGet(this, _Protocol_messages, "f").entries()) {
274
+ const clientMessage = emitterProtocol.getMessages().get(name);
275
+ if (!clientMessage) {
276
+ throw new Error(`missing client message: ${name}`);
277
+ }
278
+ resolvers.set(name, {
279
+ request: serverMessage.requestType.createResolver(clientMessage.requestType),
280
+ });
281
+ }
282
+ __classPrivateFieldGet(this, _Protocol_listenerResolvers, "f").set(hashKey, resolvers);
283
+ }
284
+ }
285
+ _Protocol_name = new WeakMap(), _Protocol_namespace = new WeakMap(), _Protocol_messages = new WeakMap(), _Protocol_types = new WeakMap(), _Protocol_parent = new WeakMap(), _Protocol_handlers = new WeakMap(), _Protocol_hashBytes = new WeakMap(), _Protocol_hashKey = new WeakMap(), _Protocol_emitterResolvers = new WeakMap(), _Protocol_listenerResolvers = new WeakMap();
@@ -0,0 +1,41 @@
1
+ import { Type } from "./type.js";
2
+ import { Resolver } from "./resolver.js";
3
+ /**
4
+ * A callback function invoked when a validation error occurs.
5
+ * @param path - The path to the invalid value in the schema.
6
+ * @param invalidValue - The value that failed validation.
7
+ * @param schemaType - The schema type that the value was validated against.
8
+ */
9
+ export type ErrorHook = (path: string[], invalidValue: unknown, schemaType: Type) => void;
10
+ /**
11
+ * Options for the `isValid` method.
12
+ * @property {ErrorHook} [errorHook] - Optional callback to handle validation errors.
13
+ */
14
+ export type IsValidOptions = {
15
+ errorHook?: ErrorHook;
16
+ };
17
+ /**
18
+ * Base implementation of Type<T> providing common serialization operations.
19
+ * Subclasses must implement the remaining abstract methods.
20
+ */
21
+ export declare abstract class BaseType<T = unknown> extends Type<T> {
22
+ /**
23
+ * Deserializes an ArrayBuffer into a value using the schema.
24
+ * @param buffer The ArrayBuffer to deserialize.
25
+ * @returns The deserialized value.
26
+ */
27
+ fromBuffer(buffer: ArrayBuffer): Promise<T>;
28
+ /**
29
+ * Validates if a value conforms to the schema.
30
+ * @param value The value to validate.
31
+ * @param opts Optional validation options.
32
+ * @returns True if valid, false otherwise.
33
+ */
34
+ isValid(value: unknown, opts?: IsValidOptions): boolean;
35
+ /**
36
+ * Creates a resolver for schema evolution from a writer type to this reader type.
37
+ * @param writerType The writer schema type.
38
+ * @returns A resolver for reading the writer type as this type.
39
+ */
40
+ createResolver(writerType: Type): Resolver;
41
+ }
@@ -0,0 +1,49 @@
1
+ import { ReadableTap } from "../serialization/tap.js";
2
+ import { Type } from "./type.js";
3
+ import { Resolver } from "./resolver.js";
4
+ import { safeStringify } from "./json.js";
5
+ /**
6
+ * Base implementation of Type<T> providing common serialization operations.
7
+ * Subclasses must implement the remaining abstract methods.
8
+ */
9
+ export class BaseType extends Type {
10
+ /**
11
+ * Deserializes an ArrayBuffer into a value using the schema.
12
+ * @param buffer The ArrayBuffer to deserialize.
13
+ * @returns The deserialized value.
14
+ */
15
+ async fromBuffer(buffer) {
16
+ const tap = new ReadableTap(buffer);
17
+ const value = await this.read(tap);
18
+ if (!await tap.isValid() || tap.getPos() !== buffer.byteLength) {
19
+ throw new Error("Insufficient data for type");
20
+ }
21
+ return value;
22
+ }
23
+ /**
24
+ * Validates if a value conforms to the schema.
25
+ * @param value The value to validate.
26
+ * @param opts Optional validation options.
27
+ * @returns True if valid, false otherwise.
28
+ */
29
+ isValid(value, opts) {
30
+ return this.check(value, opts?.errorHook, []);
31
+ }
32
+ /**
33
+ * Creates a resolver for schema evolution from a writer type to this reader type.
34
+ * @param writerType The writer schema type.
35
+ * @returns A resolver for reading the writer type as this type.
36
+ */
37
+ createResolver(writerType) {
38
+ if (this.constructor === writerType.constructor) {
39
+ return new class extends Resolver {
40
+ async read(tap) {
41
+ return await this.readerType.read(tap);
42
+ }
43
+ }(this);
44
+ }
45
+ else {
46
+ throw new Error(`Schema evolution not supported from writer type: ${safeStringify(writerType.toJSON())} to reader type: ${safeStringify(this.toJSON())}`);
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,100 @@
1
+ import { type ReadableTapLike, type WritableTapLike } from "../../serialization/tap.js";
2
+ import { BaseType } from "../base_type.js";
3
+ import { Resolver } from "../resolver.js";
4
+ import type { JSONType, Type } from "../type.js";
5
+ import { type ErrorHook } from "../error.js";
6
+ /**
7
+ * Helper function to read an array from a tap.
8
+ * @param tap The tap to read from.
9
+ * @param readElement Function to read a single element.
10
+ * @param collect Function to collect each read element.
11
+ */
12
+ export declare function readArrayInto<T>(tap: ReadableTapLike, readElement: (tap: ReadableTapLike) => Promise<T>, collect: (value: T) => void): Promise<void>;
13
+ /**
14
+ * Parameters for creating an ArrayType.
15
+ */
16
+ export interface ArrayTypeParams<T> {
17
+ /** The type of items in the array. */
18
+ items: Type<T>;
19
+ }
20
+ /**
21
+ * Avro `array` type for homogeneous collections of items described by a schema.
22
+ */
23
+ export declare class ArrayType<T = unknown> extends BaseType<T[]> {
24
+ #private;
25
+ /**
26
+ * Creates a new ArrayType.
27
+ * @param params The array type parameters.
28
+ */
29
+ constructor(params: ArrayTypeParams<T>);
30
+ /**
31
+ * Gets the type of items in the array.
32
+ */
33
+ getItemsType(): Type<T>;
34
+ /**
35
+ * Overrides the base check method to validate array values.
36
+ * @param value The value to check.
37
+ * @param errorHook Optional error hook for validation errors.
38
+ * @param path The current path in the schema.
39
+ * @returns True if the value is a valid array of items, false otherwise.
40
+ */
41
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
42
+ /**
43
+ * Overrides the base write method to serialize an array.
44
+ * @param tap The tap to write to.
45
+ * @param value The array to write.
46
+ */
47
+ write(tap: WritableTapLike, value: T[]): Promise<void>;
48
+ /**
49
+ * Overrides the base skip method to skip over an array in the tap.
50
+ * @param tap The tap to skip from.
51
+ */
52
+ skip(tap: ReadableTapLike): Promise<void>;
53
+ /**
54
+ * Overrides the base read method to deserialize an array.
55
+ * @param tap The tap to read from.
56
+ * @returns The deserialized array.
57
+ */
58
+ read(tap: ReadableTapLike): Promise<T[]>;
59
+ /**
60
+ * Overrides the base toBuffer method to serialize an array to a buffer.
61
+ * @param value The array to serialize.
62
+ * @returns The serialized buffer.
63
+ */
64
+ toBuffer(value: T[]): Promise<ArrayBuffer>;
65
+ /**
66
+ * Overrides the base cloneFromValue method to clone an array.
67
+ * @param value The value to clone.
68
+ * @returns The cloned array.
69
+ */
70
+ cloneFromValue(value: unknown): T[];
71
+ /**
72
+ * Overrides the base compare method to compare two arrays.
73
+ * @param val1 The first array.
74
+ * @param val2 The second array.
75
+ * @returns Negative if val1 < val2, 0 if equal, positive if val1 > val2.
76
+ */
77
+ compare(val1: T[], val2: T[]): number;
78
+ /**
79
+ * Overrides the base random method to generate a random array.
80
+ * @returns A randomly generated array.
81
+ */
82
+ random(): T[];
83
+ /**
84
+ * Returns the JSON schema representation of this array type.
85
+ */
86
+ toJSON(): JSONType;
87
+ /**
88
+ * Compares two serialized arrays for ordering.
89
+ * @param tap1 The first tap to compare.
90
+ * @param tap2 The second tap to compare.
91
+ * @returns A negative number if tap1 < tap2, zero if equal, positive if tap1 > tap2.
92
+ */
93
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
94
+ /**
95
+ * Creates a resolver for schema evolution between array types.
96
+ * @param writerType The writer's array type.
97
+ * @returns A resolver for reading data written with the writer type.
98
+ */
99
+ createResolver(writerType: Type): Resolver;
100
+ }