@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,197 @@
1
+ import type { CreateTypeOptions, SchemaLike } from "../../type/create_type.js";
2
+ import type { MetadataMap } from "../protocol/wire_format/metadata.js";
3
+ import type { CallRequestEnvelope } from "../protocol/wire_format/messages.js";
4
+ import type { Resolver } from "../../schemas/resolver.js";
5
+ import type { Type } from "../../schemas/type.js";
6
+ import type { Message } from "./message_definition.js";
7
+ /**
8
+ * Represents the definition of an Avro protocol, which includes the protocol name, optional namespace,
9
+ * optional custom types, and a map of message definitions.
10
+ */
11
+ export interface ProtocolDefinition {
12
+ /** The name of the protocol. */
13
+ protocol: string;
14
+ /** Optional namespace for the protocol. */
15
+ namespace?: string;
16
+ /** Optional array of custom schema types defined in the protocol. */
17
+ types?: SchemaLike[];
18
+ /** Optional record of message names to their definitions. */
19
+ messages?: Record<string, MessageDefinition>;
20
+ }
21
+ /**
22
+ * Defines a message within an Avro protocol, specifying the request parameters, response type,
23
+ * possible errors, and whether it's one-way.
24
+ */
25
+ export interface MessageDefinition {
26
+ /** Optional documentation for the message. */
27
+ doc?: string;
28
+ /** Array of schemas for the request parameters. */
29
+ request: SchemaLike[];
30
+ /** Schema for the response. */
31
+ response: SchemaLike;
32
+ /** Optional array of schemas for possible error types. */
33
+ errors?: SchemaLike[];
34
+ /** Optional flag indicating if the message is one-way (no response expected). */
35
+ "one-way"?: boolean;
36
+ }
37
+ /**
38
+ * Options for creating protocol types, extending CreateTypeOptions.
39
+ */
40
+ export interface ProtocolOptions extends CreateTypeOptions {
41
+ }
42
+ /**
43
+ * Options for configuring message transport behavior in RPC communications.
44
+ * These options control aspects such as buffering, framing, transport mode,
45
+ * and protocol exposure for schema discovery.
46
+ */
47
+ export interface MessageTransportOptions {
48
+ /**
49
+ * The size of the buffer used for reading/writing data. When undefined,
50
+ * the transport implementation determines the buffer size, often defaulting
51
+ * to a reasonable value or unlimited capacity.
52
+ */
53
+ bufferSize?: number;
54
+ /**
55
+ * The maximum size of each frame in the message stream. Defaults to 8192.
56
+ */
57
+ frameSize?: number;
58
+ /**
59
+ * The transport mode: "stateless" for request-response (e.g., HTTP),
60
+ * or "stateful" for persistent connections (e.g., WebSockets). Defaults to "stateless".
61
+ */
62
+ mode?: "stateless" | "stateful";
63
+ /**
64
+ * Custom decoder for call request envelopes. Defaults to decodeCallRequestEnvelope.
65
+ */
66
+ requestDecoder?: CallRequestEnvelopeDecoder;
67
+ /**
68
+ * When enabled, allows clients to discover the server's protocol schema
69
+ * even when their protocol hash is unknown. This facilitates schema evolution
70
+ * and interoperability by providing the server protocol in handshake responses.
71
+ * Enable this option to support server schema discovery in development or
72
+ * trusted environments. Defaults to false.
73
+ */
74
+ exposeProtocol?: boolean;
75
+ /**
76
+ * Maximum size in bytes for incoming request payloads. This prevents DoS attacks
77
+ * by limiting memory usage. Defaults to 10MB (10 * 1024 * 1024).
78
+ */
79
+ maxRequestSize?: number;
80
+ /**
81
+ * Timeout in milliseconds for processing a single request. This prevents
82
+ * resource exhaustion from hanging connections. Defaults to 30000 (30 seconds).
83
+ */
84
+ requestTimeout?: number;
85
+ }
86
+ /**
87
+ * Context provided to message handlers during RPC processing.
88
+ */
89
+ export interface ProtocolHandlerContext {
90
+ /** Metadata map associated with the message. */
91
+ metadata: MetadataMap;
92
+ }
93
+ /**
94
+ * Options for decoding call request envelopes.
95
+ */
96
+ export interface CallRequestEnvelopeOptions {
97
+ /** Whether to expect a handshake in the envelope. */
98
+ expectHandshake?: boolean;
99
+ }
100
+ /**
101
+ * Function type for decoding call request envelopes from a buffer.
102
+ */
103
+ export type CallRequestEnvelopeDecoder = (buffer: ArrayBuffer, options?: CallRequestEnvelopeOptions) => Promise<CallRequestEnvelope>;
104
+ /**
105
+ * Interface for a message listener in the RPC system.
106
+ */
107
+ export interface MessageListener {
108
+ /**
109
+ * Protocol that produced this listener so handlers can inspect it.
110
+ */
111
+ readonly protocol: ProtocolLike;
112
+ }
113
+ /**
114
+ * Function type for handling incoming messages.
115
+ */
116
+ export type MessageHandler = (request: unknown, listener: MessageListener, context: ProtocolHandlerContext) => Promise<unknown> | unknown;
117
+ /**
118
+ * Entry containing resolvers for request, response, and error schemas.
119
+ */
120
+ export interface ResolverEntry {
121
+ /** Optional resolver for the response schema. */
122
+ response?: Resolver;
123
+ /** Optional resolver for error schemas. */
124
+ error?: Resolver;
125
+ /** Optional resolver for the request schema. */
126
+ request?: Resolver;
127
+ }
128
+ /**
129
+ * Represents an Avro RPC protocol instance, providing access to message definitions,
130
+ * handlers, resolvers for schema compatibility, and protocol metadata.
131
+ */
132
+ export interface ProtocolLike {
133
+ /** The unique hash key identifying this protocol for compatibility checks. */
134
+ hashKey: string;
135
+ /** Returns the hash bytes used for protocol identification. */
136
+ getHashBytes(): Uint8Array;
137
+ /**
138
+ * Return a shallow copy of the protocol messages map. Mutating the returned
139
+ * map has no effect on the owning protocol.
140
+ */
141
+ getMessages(): ReadonlyMap<string, Message>;
142
+ /**
143
+ * Returns the message definition for `name`. The exposed message remains
144
+ * immutable from the protocol's perspective.
145
+ */
146
+ getMessage(name: string): Message | undefined;
147
+ /**
148
+ * Returns a fresh array of message names to prevent external consumers from
149
+ * mutating the internal tracking array.
150
+ */
151
+ getMessageNames(): readonly string[];
152
+ /** Retrieves the message definition for the given message name. */
153
+ getMessageDefinition(name: string): MessageDefinition | undefined;
154
+ /** Gets the handler function for the specified message name. */
155
+ getHandler(name: string): MessageHandler | undefined;
156
+ /** Checks if resolvers are available for listening to messages from a protocol with the given hash key. */
157
+ hasListenerResolvers(hashKey: string): boolean;
158
+ /** Checks if resolvers are available for emitting messages to a protocol with the given hash key. */
159
+ hasEmitterResolvers(hashKey: string): boolean;
160
+ /** Ensures that resolvers for emitting messages to the remote protocol are set up. */
161
+ ensureEmitterResolvers(hashKey: string, remote: ProtocolLike): void;
162
+ /** Ensures that resolvers for listening to messages from the emitter protocol are set up. */
163
+ ensureListenerResolvers(hashKey: string, emitterProtocol: ProtocolLike): void;
164
+ /** Retrieves resolvers for emitting the specified message to a protocol with the given hash key. */
165
+ getEmitterResolvers(hashKey: string, messageName: string): ResolverEntry | undefined;
166
+ /** Retrieves resolvers for listening to the specified message from a protocol with the given hash key. */
167
+ getListenerResolvers(hashKey: string, messageName: string): ResolverEntry | undefined;
168
+ /** Returns a string representation of the protocol. */
169
+ toString(): string;
170
+ /** Gets the name of the protocol. */
171
+ getName(): string;
172
+ /** Returns an array of named types defined in the protocol. */
173
+ getNamedTypes(): Type[];
174
+ /** Retrieves metadata information about the protocol. */
175
+ getProtocolInfo(): ProtocolInfo;
176
+ }
177
+ /**
178
+ * Metadata information about an Avro RPC protocol.
179
+ */
180
+ export interface ProtocolInfo {
181
+ /** The name of the protocol. */
182
+ name: string;
183
+ /** Optional namespace for the protocol. */
184
+ namespace?: string;
185
+ /** The unique hash key for the protocol. */
186
+ hashKey: string;
187
+ /** The hash bytes for protocol identification. */
188
+ hashBytes: Uint8Array;
189
+ /**
190
+ * Fresh array to guarantee the caller can't mutate the recorded list.
191
+ */
192
+ messageNames: readonly string[];
193
+ }
194
+ /**
195
+ * A factory function type for creating a ProtocolLike instance from a protocol definition.
196
+ */
197
+ export type ProtocolFactory<T extends ProtocolLike = ProtocolLike> = (definition: ProtocolDefinition) => T;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,43 @@
1
+ import type { HandshakeRequestInit, HandshakeRequestMessage, HandshakeResponseInit, HandshakeResponseMessage } from "../protocol/wire_format/handshake.js";
2
+ import type { MessageTransportOptions, ProtocolLike } from "../definitions/protocol_definitions.js";
3
+ /**
4
+ * Abstract base class for message endpoints in Avro RPC.
5
+ * Extends EventTarget to dispatch events like errors, handshakes, and end-of-transmission.
6
+ */
7
+ export declare abstract class MessageEndpoint extends EventTarget {
8
+ /**
9
+ * The Avro protocol definition that this endpoint adheres to.
10
+ */
11
+ readonly protocol: ProtocolLike;
12
+ /**
13
+ * The size of the internal buffer used for reading and writing messages.
14
+ */
15
+ protected readonly bufferSize: number;
16
+ /**
17
+ * The maximum size of individual frames in the message transport protocol.
18
+ */
19
+ protected readonly frameSize: number;
20
+ /**
21
+ * Initializes the endpoint with protocol and transport options.
22
+ * @param protocol The Avro protocol definition.
23
+ * @param opts Transport configuration options.
24
+ */
25
+ protected constructor(protocol: ProtocolLike, opts: MessageTransportOptions);
26
+ /**
27
+ * Dispatches an error event with the given error.
28
+ * Converts non-Error values to Error instances and uses ErrorEvent if available.
29
+ * @param err The error to dispatch.
30
+ */
31
+ protected dispatchError(err: unknown): void;
32
+ /**
33
+ * Dispatches a handshake event with request and response details.
34
+ * @param request The handshake request data.
35
+ * @param response The handshake response data.
36
+ */
37
+ protected dispatchHandshake(request: HandshakeRequestInit | HandshakeRequestMessage, response: HandshakeResponseInit | HandshakeResponseMessage): void;
38
+ /**
39
+ * Dispatches an end-of-transmission event with the number of pending operations.
40
+ * @param pending The number of pending operations.
41
+ */
42
+ protected dispatchEot(pending: number): void;
43
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Abstract base class for message endpoints in Avro RPC.
3
+ * Extends EventTarget to dispatch events like errors, handshakes, and end-of-transmission.
4
+ */
5
+ export class MessageEndpoint extends EventTarget {
6
+ /**
7
+ * Initializes the endpoint with protocol and transport options.
8
+ * @param protocol The Avro protocol definition.
9
+ * @param opts Transport configuration options.
10
+ */
11
+ constructor(protocol, opts) {
12
+ super();
13
+ /**
14
+ * The Avro protocol definition that this endpoint adheres to.
15
+ */
16
+ Object.defineProperty(this, "protocol", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ /**
23
+ * The size of the internal buffer used for reading and writing messages.
24
+ */
25
+ Object.defineProperty(this, "bufferSize", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ /**
32
+ * The maximum size of individual frames in the message transport protocol.
33
+ */
34
+ Object.defineProperty(this, "frameSize", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ this.protocol = protocol;
41
+ this.bufferSize = opts.bufferSize ?? 2048;
42
+ this.frameSize = opts.frameSize ?? 2048;
43
+ }
44
+ /**
45
+ * Dispatches an error event with the given error.
46
+ * Converts non-Error values to Error instances and uses ErrorEvent if available.
47
+ * @param err The error to dispatch.
48
+ */
49
+ dispatchError(err) {
50
+ let error;
51
+ if (err instanceof Error) {
52
+ error = err;
53
+ }
54
+ else {
55
+ error = new Error(String(err));
56
+ }
57
+ if (typeof ErrorEvent === "function") {
58
+ this.dispatchEvent(new ErrorEvent("error", { error }));
59
+ }
60
+ else {
61
+ this.dispatchEvent(new CustomEvent("error", { detail: error }));
62
+ }
63
+ }
64
+ /**
65
+ * Dispatches a handshake event with request and response details.
66
+ * @param request The handshake request data.
67
+ * @param response The handshake response data.
68
+ */
69
+ dispatchHandshake(request, response) {
70
+ this.dispatchEvent(new CustomEvent("handshake", {
71
+ detail: { request, response },
72
+ }));
73
+ }
74
+ /**
75
+ * Dispatches an end-of-transmission event with the number of pending operations.
76
+ * @param pending The number of pending operations.
77
+ */
78
+ dispatchEot(pending) {
79
+ this.dispatchEvent(new CustomEvent("eot", { detail: { pending } }));
80
+ }
81
+ }
@@ -0,0 +1,96 @@
1
+ import type { HandshakeRequestInit, HandshakeResponseMessage } from "../protocol/wire_format/handshake.js";
2
+ import type { MessageTransportOptions, ProtocolFactory, ProtocolLike } from "../definitions/protocol_definitions.js";
3
+ import { MessageEndpoint } from "./base.js";
4
+ import type { Message } from "../definitions/message_definition.js";
5
+ import type { BinaryDuplexLike } from "../protocol/transports/transport_helpers.js";
6
+ /**
7
+ * Abstract base class for message emitters in Avro RPC.
8
+ * Manages server hash, destruction state, and handshake protocol for RPC communication.
9
+ */
10
+ export declare abstract class MessageEmitter extends MessageEndpoint {
11
+ #private;
12
+ /**
13
+ * Initializes the emitter with protocol and options, setting initial server hash.
14
+ * @param protocol The Avro protocol.
15
+ * @param opts Transport options.
16
+ */
17
+ protected constructor(protocol: ProtocolLike, opts: MessageTransportOptions);
18
+ /**
19
+ * Gets whether the emitter has been destroyed.
20
+ */
21
+ get destroyed(): boolean;
22
+ /**
23
+ * Destroys the emitter, marking it as destroyed and dispatching end-of-transmission.
24
+ */
25
+ destroy(): void;
26
+ /**
27
+ * Gets the current server hash bytes.
28
+ */
29
+ protected get serverHash(): Uint8Array;
30
+ /**
31
+ * Updates the server hash with new bytes and recomputes the hex key.
32
+ * @param bytes The new server hash bytes.
33
+ */
34
+ protected updateServerHash(bytes: Uint8Array): void;
35
+ /**
36
+ * Gets the current server hash as a hex string key.
37
+ */
38
+ protected get serverHashKey(): string;
39
+ /**
40
+ * Builds a handshake request object with client/server hashes and optional protocol.
41
+ * @param omitProtocol Whether to omit the client protocol string.
42
+ * @returns The handshake request data.
43
+ */
44
+ protected buildHandshakeRequest(omitProtocol: boolean): {
45
+ clientHash: Uint8Array;
46
+ clientProtocol: string | null;
47
+ serverHash: Uint8Array;
48
+ };
49
+ /**
50
+ * Finalizes the handshake by dispatching events, handling errors, and determining retry.
51
+ * Updates server hash if provided and checks for protocol mismatches.
52
+ * @param request The handshake request.
53
+ * @param response The handshake response.
54
+ * @returns Object indicating if retry is needed and the server hash.
55
+ */
56
+ protected finalizeHandshake(request: HandshakeRequestInit, response: HandshakeResponseMessage): {
57
+ retry: boolean;
58
+ serverHash: Uint8Array;
59
+ };
60
+ /**
61
+ * Sends an RPC message with the given request data.
62
+ * @param message The message definition.
63
+ * @param request The request payload.
64
+ * @returns Promise resolving to the response or undefined for one-way calls.
65
+ */
66
+ abstract send(message: Message, request: unknown): Promise<unknown>;
67
+ }
68
+ /**
69
+ * Stateless implementation of MessageEmitter for Avro RPC.
70
+ * Creates a new transport connection for each RPC call, enabling stateless communication.
71
+ * Handles encoding requests, performing handshakes, decoding responses, and tracking pending calls.
72
+ */
73
+ export declare class StatelessEmitter extends MessageEmitter {
74
+ #private;
75
+ /**
76
+ * Initializes the stateless emitter with protocol, transport factory, options, and protocol factory.
77
+ * @param protocol The Avro protocol.
78
+ * @param transportFactory Factory function to create binary duplex transports.
79
+ * @param opts Transport options.
80
+ * @param protocolFactory Factory to create protocols from JSON.
81
+ */
82
+ constructor(protocol: ProtocolLike, transportFactory: () => Promise<BinaryDuplexLike>, opts: MessageTransportOptions, protocolFactory: ProtocolFactory);
83
+ /**
84
+ * Destroys the emitter, rejecting all pending calls and clearing the set.
85
+ */
86
+ destroy(): void;
87
+ /**
88
+ * Sends an RPC message, handling one-way and request-response calls.
89
+ * For one-way calls, performs the call and returns undefined.
90
+ * For request-response, tracks the pending call and returns the result.
91
+ * @param message The message to send.
92
+ * @param request The request data.
93
+ * @returns Promise resolving to response or undefined.
94
+ */
95
+ send(message: Message, request: unknown): Promise<unknown>;
96
+ }
@@ -0,0 +1,245 @@
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 _MessageEmitter_serverHash, _MessageEmitter_serverHashKey, _MessageEmitter_destroyed, _StatelessEmitter_instances, _StatelessEmitter_transportFactory, _StatelessEmitter_pending, _StatelessEmitter_nextId, _StatelessEmitter_protocolFactory, _StatelessEmitter_trackPending, _StatelessEmitter_performCall;
13
+ import { decodeCallResponseEnvelope, encodeCallRequest, } from "../protocol/wire_format/messages.js";
14
+ import { frameMessage } from "../protocol/wire_format/framing.js";
15
+ import { bytesToHex, metadataWithId, toArrayBuffer, } from "../protocol/protocol_helpers.js";
16
+ import { MessageEndpoint } from "./base.js";
17
+ import { toBinaryDuplex } from "../protocol/transports/transport_helpers.js";
18
+ import { readResponsePayload } from "./helpers.js";
19
+ import { drainReadable, readSingleMessage } from "./helpers.js";
20
+ /**
21
+ * Abstract base class for message emitters in Avro RPC.
22
+ * Manages server hash, destruction state, and handshake protocol for RPC communication.
23
+ */
24
+ export class MessageEmitter extends MessageEndpoint {
25
+ /**
26
+ * Initializes the emitter with protocol and options, setting initial server hash.
27
+ * @param protocol The Avro protocol.
28
+ * @param opts Transport options.
29
+ */
30
+ constructor(protocol, opts) {
31
+ super(protocol, opts);
32
+ _MessageEmitter_serverHash.set(this, void 0);
33
+ _MessageEmitter_serverHashKey.set(this, void 0);
34
+ _MessageEmitter_destroyed.set(this, false);
35
+ __classPrivateFieldSet(this, _MessageEmitter_serverHash, protocol.getHashBytes(), "f");
36
+ __classPrivateFieldSet(this, _MessageEmitter_serverHashKey, protocol.hashKey, "f");
37
+ }
38
+ /**
39
+ * Gets whether the emitter has been destroyed.
40
+ */
41
+ get destroyed() {
42
+ return __classPrivateFieldGet(this, _MessageEmitter_destroyed, "f");
43
+ }
44
+ /**
45
+ * Destroys the emitter, marking it as destroyed and dispatching end-of-transmission.
46
+ */
47
+ destroy() {
48
+ if (!__classPrivateFieldGet(this, _MessageEmitter_destroyed, "f")) {
49
+ __classPrivateFieldSet(this, _MessageEmitter_destroyed, true, "f");
50
+ this.dispatchEot(0);
51
+ }
52
+ }
53
+ /**
54
+ * Gets the current server hash bytes.
55
+ */
56
+ get serverHash() {
57
+ return __classPrivateFieldGet(this, _MessageEmitter_serverHash, "f");
58
+ }
59
+ /**
60
+ * Updates the server hash with new bytes and recomputes the hex key.
61
+ * @param bytes The new server hash bytes.
62
+ */
63
+ updateServerHash(bytes) {
64
+ __classPrivateFieldSet(this, _MessageEmitter_serverHash, bytes.slice(), "f");
65
+ __classPrivateFieldSet(this, _MessageEmitter_serverHashKey, bytesToHex(bytes), "f");
66
+ }
67
+ /**
68
+ * Gets the current server hash as a hex string key.
69
+ */
70
+ get serverHashKey() {
71
+ return __classPrivateFieldGet(this, _MessageEmitter_serverHashKey, "f");
72
+ }
73
+ /**
74
+ * Builds a handshake request object with client/server hashes and optional protocol.
75
+ * @param omitProtocol Whether to omit the client protocol string.
76
+ * @returns The handshake request data.
77
+ */
78
+ buildHandshakeRequest(omitProtocol) {
79
+ return {
80
+ clientHash: this.protocol.getHashBytes(),
81
+ clientProtocol: omitProtocol ? null : this.protocol.toString(),
82
+ serverHash: __classPrivateFieldGet(this, _MessageEmitter_serverHash, "f"),
83
+ };
84
+ }
85
+ /**
86
+ * Finalizes the handshake by dispatching events, handling errors, and determining retry.
87
+ * Updates server hash if provided and checks for protocol mismatches.
88
+ * @param request The handshake request.
89
+ * @param response The handshake response.
90
+ * @returns Object indicating if retry is needed and the server hash.
91
+ */
92
+ finalizeHandshake(request, response) {
93
+ this.dispatchHandshake(request, response);
94
+ if (response.match === "NONE") {
95
+ const reason = response.meta?.get("error");
96
+ if (reason) {
97
+ throw new Error(new TextDecoder().decode(reason));
98
+ }
99
+ }
100
+ if (!response.serverHash) {
101
+ return { retry: false, serverHash: __classPrivateFieldGet(this, _MessageEmitter_serverHash, "f") };
102
+ }
103
+ this.updateServerHash(response.serverHash);
104
+ let retry = false;
105
+ if (response.match === "NONE" && request.clientProtocol === null) {
106
+ retry = true;
107
+ }
108
+ return {
109
+ retry,
110
+ serverHash: response.serverHash,
111
+ };
112
+ }
113
+ }
114
+ _MessageEmitter_serverHash = new WeakMap(), _MessageEmitter_serverHashKey = new WeakMap(), _MessageEmitter_destroyed = new WeakMap();
115
+ /**
116
+ * Stateless implementation of MessageEmitter for Avro RPC.
117
+ * Creates a new transport connection for each RPC call, enabling stateless communication.
118
+ * Handles encoding requests, performing handshakes, decoding responses, and tracking pending calls.
119
+ */
120
+ export class StatelessEmitter extends MessageEmitter {
121
+ /**
122
+ * Initializes the stateless emitter with protocol, transport factory, options, and protocol factory.
123
+ * @param protocol The Avro protocol.
124
+ * @param transportFactory Factory function to create binary duplex transports.
125
+ * @param opts Transport options.
126
+ * @param protocolFactory Factory to create protocols from JSON.
127
+ */
128
+ constructor(protocol, transportFactory, opts, protocolFactory) {
129
+ super(protocol, opts);
130
+ _StatelessEmitter_instances.add(this);
131
+ _StatelessEmitter_transportFactory.set(this, void 0);
132
+ _StatelessEmitter_pending.set(this, new Set());
133
+ _StatelessEmitter_nextId.set(this, 1);
134
+ _StatelessEmitter_protocolFactory.set(this, void 0);
135
+ __classPrivateFieldSet(this, _StatelessEmitter_transportFactory, transportFactory, "f");
136
+ __classPrivateFieldSet(this, _StatelessEmitter_protocolFactory, protocolFactory, "f");
137
+ }
138
+ /**
139
+ * Destroys the emitter, rejecting all pending calls and clearing the set.
140
+ */
141
+ destroy() {
142
+ if (this.destroyed) {
143
+ return;
144
+ }
145
+ for (const pending of __classPrivateFieldGet(this, _StatelessEmitter_pending, "f")) {
146
+ pending.reject(new Error("emitter destroyed"));
147
+ }
148
+ __classPrivateFieldGet(this, _StatelessEmitter_pending, "f").clear();
149
+ super.destroy();
150
+ }
151
+ /**
152
+ * Sends an RPC message, handling one-way and request-response calls.
153
+ * For one-way calls, performs the call and returns undefined.
154
+ * For request-response, tracks the pending call and returns the result.
155
+ * @param message The message to send.
156
+ * @param request The request data.
157
+ * @returns Promise resolving to response or undefined.
158
+ */
159
+ async send(message, request) {
160
+ if (this.destroyed) {
161
+ throw new Error("emitter destroyed");
162
+ }
163
+ if (message.oneWay) {
164
+ await __classPrivateFieldGet(this, _StatelessEmitter_instances, "m", _StatelessEmitter_performCall).call(this, message, request, true);
165
+ return undefined;
166
+ }
167
+ const promise = __classPrivateFieldGet(this, _StatelessEmitter_instances, "m", _StatelessEmitter_performCall).call(this, message, request, false);
168
+ return await __classPrivateFieldGet(this, _StatelessEmitter_instances, "m", _StatelessEmitter_trackPending).call(this, promise);
169
+ }
170
+ }
171
+ _StatelessEmitter_transportFactory = new WeakMap(), _StatelessEmitter_pending = new WeakMap(), _StatelessEmitter_nextId = new WeakMap(), _StatelessEmitter_protocolFactory = new WeakMap(), _StatelessEmitter_instances = new WeakSet(), _StatelessEmitter_trackPending =
172
+ /**
173
+ * Tracks a pending call by wrapping it in a new promise and managing resolve/reject.
174
+ * Adds to pending set and removes on completion.
175
+ * @param promise The call promise to track.
176
+ * @returns Wrapped promise that resolves/rejects based on the original.
177
+ */
178
+ async function _StatelessEmitter_trackPending(promise) {
179
+ let resolve;
180
+ let reject;
181
+ const wrapped = new Promise((res, rej) => {
182
+ resolve = res;
183
+ reject = rej;
184
+ });
185
+ const pending = { resolve, reject };
186
+ __classPrivateFieldGet(this, _StatelessEmitter_pending, "f").add(pending);
187
+ promise
188
+ .then((value) => {
189
+ if (__classPrivateFieldGet(this, _StatelessEmitter_pending, "f").delete(pending)) {
190
+ pending.resolve(value);
191
+ }
192
+ })
193
+ .catch((err) => {
194
+ if (__classPrivateFieldGet(this, _StatelessEmitter_pending, "f").delete(pending)) {
195
+ pending.reject(err);
196
+ }
197
+ });
198
+ return await wrapped;
199
+ }, _StatelessEmitter_performCall =
200
+ /**
201
+ * Performs the actual RPC call by creating transport, encoding request, handshaking, and decoding response.
202
+ * Retries on handshake failure if needed.
203
+ * @param message The RPC message.
204
+ * @param request The request payload.
205
+ * @param oneWay Whether this is a one-way call.
206
+ * @returns Promise resolving to response payload or undefined for one-way.
207
+ */
208
+ async function _StatelessEmitter_performCall(message, request, oneWay) {
209
+ var _a, _b;
210
+ let omitProtocol = true;
211
+ while (true) {
212
+ const transport = toBinaryDuplex(await __classPrivateFieldGet(this, _StatelessEmitter_transportFactory, "f").call(this));
213
+ const metadata = metadataWithId((__classPrivateFieldSet(this, _StatelessEmitter_nextId, (_b = __classPrivateFieldGet(this, _StatelessEmitter_nextId, "f"), _a = _b++, _b), "f"), _a));
214
+ const handshake = this.buildHandshakeRequest(omitProtocol);
215
+ const encoded = await encodeCallRequest({
216
+ handshake,
217
+ metadata,
218
+ messageName: message.name,
219
+ request,
220
+ requestType: message.requestType,
221
+ });
222
+ const framed = frameMessage(encoded, { frameSize: this.frameSize });
223
+ await transport.writable.write(framed);
224
+ await transport.writable.close();
225
+ if (oneWay) {
226
+ await drainReadable(transport.readable);
227
+ return undefined;
228
+ }
229
+ const payload = await readSingleMessage(transport.readable);
230
+ const envelope = await decodeCallResponseEnvelope(toArrayBuffer(payload), { expectHandshake: true });
231
+ const handshakeResponse = envelope.handshake;
232
+ const outcome = this.finalizeHandshake(handshake, handshakeResponse);
233
+ if (handshakeResponse.serverProtocol) {
234
+ const remote = __classPrivateFieldGet(this, _StatelessEmitter_protocolFactory, "f").call(this, JSON.parse(handshakeResponse.serverProtocol));
235
+ this.protocol.ensureEmitterResolvers(bytesToHex(outcome.serverHash), remote);
236
+ }
237
+ if (outcome.retry) {
238
+ omitProtocol = false;
239
+ continue;
240
+ }
241
+ const resolvers = this.protocol.getEmitterResolvers(bytesToHex(outcome.serverHash), message.name);
242
+ const payloadValue = await readResponsePayload(envelope, message, resolvers);
243
+ return payloadValue;
244
+ }
245
+ };