@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,175 @@
1
+ import { createType } from "../../../type/create_type.js";
2
+ import { ReadableTap } from "../../../serialization/tap.js";
3
+ import { concatUint8Arrays } from "../../../internal/collections/array_utils.js";
4
+ import { cloneMetadataMap, toRequiredMetadataMap, } from "./metadata.js";
5
+ import { encodeHandshakeRequest, encodeHandshakeResponse, readHandshakeRequestFromTap, readHandshakeResponseFromTap, } from "./handshake.js";
6
+ const MAP_OF_BYTES_TYPE = createType({
7
+ type: "map",
8
+ values: "bytes",
9
+ });
10
+ const STRING_TYPE = createType("string");
11
+ const BOOLEAN_TYPE = createType("boolean");
12
+ async function readOptionalHandshake(buffer, reader, expectHandshake) {
13
+ const tap = new ReadableTap(buffer);
14
+ if (expectHandshake) {
15
+ const handshake = await reader(tap);
16
+ return { handshake, tap };
17
+ }
18
+ const initialPos = tap.getPos();
19
+ try {
20
+ const handshake = await reader(tap);
21
+ return { handshake, tap };
22
+ }
23
+ catch {
24
+ return { tap: new ReadableTap(buffer, initialPos) };
25
+ }
26
+ }
27
+ /**
28
+ * Encodes a call request message into a binary format for transmission.
29
+ *
30
+ * The encoded message includes optional handshake data, metadata, message name,
31
+ * and the request payload. The format follows the Avro RPC wire protocol.
32
+ *
33
+ * @param init - The call request initialization object containing handshake,
34
+ * metadata, message name, request data, and request type.
35
+ * @returns A promise that resolves to the encoded message as a Uint8Array.
36
+ */
37
+ export async function encodeCallRequest(init) {
38
+ const parts = [];
39
+ if (init.handshake) {
40
+ parts.push(await encodeHandshakeRequest(init.handshake));
41
+ }
42
+ const metadata = toRequiredMetadataMap(init.metadata);
43
+ parts.push(new Uint8Array(await MAP_OF_BYTES_TYPE.toBuffer(metadata)));
44
+ parts.push(new Uint8Array(await STRING_TYPE.toBuffer(init.messageName)));
45
+ parts.push(new Uint8Array(await init.requestType.toBuffer(init.request)));
46
+ return concatUint8Arrays(parts);
47
+ }
48
+ /**
49
+ * Decodes a binary call request message into a structured object.
50
+ *
51
+ * This function parses the complete message including handshake (if expected),
52
+ * metadata, message name, and request payload.
53
+ *
54
+ * @param buffer - The binary data containing the encoded call request.
55
+ * @param options - Decoding options including the request type and whether
56
+ * to expect handshake data.
57
+ * @returns A promise that resolves to the decoded call request message.
58
+ */
59
+ export async function decodeCallRequest(buffer, options) {
60
+ const { requestType, expectHandshake = false } = options;
61
+ const envelope = await decodeCallRequestEnvelope(buffer, {
62
+ expectHandshake,
63
+ });
64
+ const request = await requestType.read(envelope.bodyTap);
65
+ return {
66
+ handshake: envelope.handshake,
67
+ metadata: envelope.metadata,
68
+ messageName: envelope.messageName,
69
+ request,
70
+ };
71
+ }
72
+ /**
73
+ * Decodes the envelope of a call request message without parsing the request payload.
74
+ *
75
+ * This function is useful for inspecting message metadata and headers before
76
+ * deciding whether to parse the full request payload. The request data remains
77
+ * in the bodyTap for later reading.
78
+ *
79
+ * @param buffer - The binary data containing the encoded call request.
80
+ * @param options - Options specifying whether to expect handshake data.
81
+ * @returns A promise that resolves to the call request envelope.
82
+ */
83
+ export async function decodeCallRequestEnvelope(buffer, options = {}) {
84
+ const { expectHandshake = false } = options;
85
+ const { handshake: handshakeResult, tap: handshakeTap } = await readOptionalHandshake(buffer, readHandshakeRequestFromTap, expectHandshake);
86
+ const handshake = handshakeResult;
87
+ const tap = handshakeTap;
88
+ const metadataMap = await MAP_OF_BYTES_TYPE.read(tap);
89
+ const metadata = cloneMetadataMap(metadataMap);
90
+ const messageName = await STRING_TYPE.read(tap);
91
+ return {
92
+ handshake,
93
+ metadata,
94
+ messageName,
95
+ bodyTap: tap,
96
+ };
97
+ }
98
+ /**
99
+ * Encodes a call response message into a binary format for transmission.
100
+ *
101
+ * The encoded message includes optional handshake data, metadata, error flag,
102
+ * and either the response payload or error payload based on the isError flag.
103
+ *
104
+ * @param init - The call response initialization object containing handshake,
105
+ * metadata, error flag, payload, and response/error types.
106
+ * @returns A promise that resolves to the encoded message as a Uint8Array.
107
+ */
108
+ export async function encodeCallResponse(init) {
109
+ const parts = [];
110
+ if (init.handshake) {
111
+ parts.push(await encodeHandshakeResponse(init.handshake));
112
+ }
113
+ const metadata = toRequiredMetadataMap(init.metadata);
114
+ parts.push(new Uint8Array(await MAP_OF_BYTES_TYPE.toBuffer(metadata)));
115
+ parts.push(new Uint8Array(await BOOLEAN_TYPE.toBuffer(init.isError)));
116
+ if (init.isError) {
117
+ parts.push(new Uint8Array(await init.errorType.toBuffer(init.payload)));
118
+ }
119
+ else {
120
+ parts.push(new Uint8Array(await init.responseType.toBuffer(init.payload)));
121
+ }
122
+ return concatUint8Arrays(parts);
123
+ }
124
+ /**
125
+ * Decodes a binary call response message into a structured object.
126
+ *
127
+ * This function parses the complete message including handshake (if expected),
128
+ * metadata, error flag, and response or error payload based on the error flag.
129
+ *
130
+ * @param buffer - The binary data containing the encoded call response.
131
+ * @param options - Decoding options including response and error types, and
132
+ * whether to expect handshake data.
133
+ * @returns A promise that resolves to the decoded call response message.
134
+ */
135
+ export async function decodeCallResponse(buffer, options) {
136
+ const { responseType, errorType, expectHandshake = false } = options;
137
+ const envelope = await decodeCallResponseEnvelope(buffer, {
138
+ expectHandshake,
139
+ });
140
+ const payload = envelope.isError
141
+ ? await errorType.read(envelope.bodyTap)
142
+ : await responseType.read(envelope.bodyTap);
143
+ return {
144
+ handshake: envelope.handshake,
145
+ metadata: envelope.metadata,
146
+ isError: envelope.isError,
147
+ payload,
148
+ };
149
+ }
150
+ /**
151
+ * Decodes the envelope of a call response message without parsing the response payload.
152
+ *
153
+ * This function is useful for inspecting message metadata and error status before
154
+ * deciding whether to parse the full response or error payload. The payload data
155
+ * remains in the bodyTap for later reading.
156
+ *
157
+ * @param buffer - The binary data containing the encoded call response.
158
+ * @param options - Options specifying whether to expect handshake data.
159
+ * @returns A promise that resolves to the call response envelope.
160
+ */
161
+ export async function decodeCallResponseEnvelope(buffer, options = {}) {
162
+ const { expectHandshake = false } = options;
163
+ const { handshake: handshakeResult, tap: handshakeTap } = await readOptionalHandshake(buffer, readHandshakeResponseFromTap, expectHandshake);
164
+ const handshake = handshakeResult;
165
+ const tap = handshakeTap;
166
+ const metadataMap = await MAP_OF_BYTES_TYPE.read(tap);
167
+ const metadata = cloneMetadataMap(metadataMap);
168
+ const isError = await BOOLEAN_TYPE.read(tap);
169
+ return {
170
+ handshake,
171
+ metadata,
172
+ isError,
173
+ bodyTap: tap,
174
+ };
175
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Represents a metadata value as a byte array in Avro RPC metadata.
3
+ */
4
+ export type MetadataValue = Uint8Array;
5
+ /**
6
+ * Initialization types for Avro RPC metadata, allowing various input formats
7
+ * that can be converted to a MetadataMap.
8
+ */
9
+ export type MetadataInit = Map<string, MetadataValue> | Iterable<[string, MetadataValue]> | Record<string, MetadataValue>;
10
+ /**
11
+ * A map of string keys to MetadataValue byte arrays, representing the standard
12
+ * form of Avro RPC metadata.
13
+ */
14
+ export type MetadataMap = Map<string, MetadataValue>;
15
+ /**
16
+ * Checks if the given value is an iterable metadata initialization type,
17
+ * useful for distinguishing between Map and Record inputs in Avro RPC metadata.
18
+ */
19
+ export declare function isIterableMetadata(value: MetadataInit | undefined | null): value is Iterable<[string, MetadataValue]>;
20
+ /**
21
+ * Clones a Uint8Array to ensure immutability of metadata values in Avro RPC.
22
+ * Throws if the value is not a Uint8Array.
23
+ */
24
+ export declare function cloneBytes(value: Uint8Array, field: string): Uint8Array;
25
+ /**
26
+ * Converts a MetadataInit to a MetadataMap, cloning all byte values for
27
+ * immutability in Avro RPC metadata handling.
28
+ */
29
+ export declare function toMetadataMap(init: MetadataInit): MetadataMap;
30
+ /**
31
+ * Converts an optional MetadataInit to a MetadataMap or null, used for
32
+ * optional metadata fields in Avro RPC messages.
33
+ */
34
+ export declare function toOptionalMetadataMap(init: MetadataInit | undefined | null): MetadataMap | null;
35
+ /**
36
+ * Converts an optional MetadataInit to a MetadataMap, defaulting to an empty
37
+ * map if null or undefined, for required metadata in Avro RPC.
38
+ */
39
+ export declare function toRequiredMetadataMap(init: MetadataInit | undefined | null): MetadataMap;
40
+ /**
41
+ * Creates a deep clone of a MetadataMap, cloning all byte values to ensure
42
+ * immutability in Avro RPC metadata operations.
43
+ */
44
+ export declare function cloneMetadataMap(map: MetadataMap): MetadataMap;
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Checks if the given value is an iterable metadata initialization type,
3
+ * useful for distinguishing between Map and Record inputs in Avro RPC metadata.
4
+ */
5
+ export function isIterableMetadata(value) {
6
+ if (value === undefined || value === null) {
7
+ return false;
8
+ }
9
+ if (value instanceof Map) {
10
+ return true;
11
+ }
12
+ return typeof value[Symbol.iterator] === "function";
13
+ }
14
+ /**
15
+ * Clones a Uint8Array to ensure immutability of metadata values in Avro RPC.
16
+ * Throws if the value is not a Uint8Array.
17
+ */
18
+ export function cloneBytes(value, field) {
19
+ if (!(value instanceof Uint8Array)) {
20
+ throw new TypeError(`${field} must be a Uint8Array.`);
21
+ }
22
+ return new Uint8Array(value);
23
+ }
24
+ /**
25
+ * Converts a MetadataInit to a MetadataMap, cloning all byte values for
26
+ * immutability in Avro RPC metadata handling.
27
+ */
28
+ export function toMetadataMap(init) {
29
+ const map = new Map();
30
+ if (isIterableMetadata(init)) {
31
+ for (const [key, value] of init) {
32
+ if (typeof key !== "string") {
33
+ throw new TypeError("Metadata keys must be strings.");
34
+ }
35
+ map.set(key, cloneBytes(value, `Metadata value for '${key}'`));
36
+ }
37
+ return map;
38
+ }
39
+ for (const [key, value] of Object.entries(init)) {
40
+ map.set(key, cloneBytes(value, `Metadata value for '${key}'`));
41
+ }
42
+ return map;
43
+ }
44
+ /**
45
+ * Converts an optional MetadataInit to a MetadataMap or null, used for
46
+ * optional metadata fields in Avro RPC messages.
47
+ */
48
+ export function toOptionalMetadataMap(init) {
49
+ if (init === undefined || init === null) {
50
+ return null;
51
+ }
52
+ return toMetadataMap(init);
53
+ }
54
+ /**
55
+ * Converts an optional MetadataInit to a MetadataMap, defaulting to an empty
56
+ * map if null or undefined, for required metadata in Avro RPC.
57
+ */
58
+ export function toRequiredMetadataMap(init) {
59
+ if (init === undefined || init === null) {
60
+ return new Map();
61
+ }
62
+ return toMetadataMap(init);
63
+ }
64
+ /**
65
+ * Creates a deep clone of a MetadataMap, cloning all byte values to ensure
66
+ * immutability in Avro RPC metadata operations.
67
+ */
68
+ export function cloneMetadataMap(map) {
69
+ const clone = new Map();
70
+ for (const [key, value] of map.entries()) {
71
+ clone.set(key, cloneBytes(value, `Metadata value for '${key}'`));
72
+ }
73
+ return clone;
74
+ }
@@ -0,0 +1,121 @@
1
+ import type { Type } from "../schemas/type.js";
2
+ import { Message } from "./definitions/message_definition.js";
3
+ import type { MessageDefinition, MessageHandler, MessageTransportOptions, ProtocolDefinition, ProtocolLike, ProtocolOptions, ResolverEntry } from "./definitions/protocol_definitions.js";
4
+ import type { MessageListener } from "./message_endpoint/listener.js";
5
+ import type { MessageEmitter } from "./message_endpoint/emitter.js";
6
+ import type { BinaryDuplexLike, StatelessTransportFactory } from "./protocol/transports/transport_helpers.js";
7
+ /**
8
+ * Core Avro RPC protocol representation that validates schemas, hashes the
9
+ * protocol definition, and coordinates message handlers and transports.
10
+ */
11
+ export declare class Protocol implements ProtocolLike {
12
+ #private;
13
+ private constructor();
14
+ /**
15
+ * Creates a new Protocol instance from a definition.
16
+ * @param attrs The protocol definition object.
17
+ * @param opts Protocol creation options.
18
+ * @returns A new Protocol instance.
19
+ */
20
+ static create(attrs: ProtocolDefinition, opts?: ProtocolOptions): Protocol;
21
+ /**
22
+ * Creates a subprotocol sharing the same context but without a parent reference.
23
+ * @returns A new Protocol instance.
24
+ */
25
+ subprotocol(): Protocol;
26
+ /**
27
+ * Gets the protocol name.
28
+ */
29
+ getName(): string;
30
+ /**
31
+ * Returns the string representation of the protocol (JSON).
32
+ */
33
+ toString(): string;
34
+ /**
35
+ * Gets the MD5 hash of the protocol as a hex string.
36
+ */
37
+ get hashKey(): string;
38
+ /**
39
+ * Gets the MD5 hash of the protocol as bytes.
40
+ */
41
+ getHashBytes(): Uint8Array;
42
+ /**
43
+ * Gets all messages defined in the protocol.
44
+ */
45
+ getMessages(): Map<string, Message>;
46
+ /**
47
+ * Gets all named types defined in the protocol.
48
+ */
49
+ getNamedTypes(): Type[];
50
+ /**
51
+ * Gets a specific message by name.
52
+ */
53
+ getMessage(name: string): Message | undefined;
54
+ /**
55
+ * Gets the names of all messages in the protocol.
56
+ */
57
+ getMessageNames(): readonly string[];
58
+ /**
59
+ * Gets the definition of a specific message.
60
+ */
61
+ getMessageDefinition(name: string): MessageDefinition | undefined;
62
+ /**
63
+ * Gets summary information about the protocol.
64
+ */
65
+ getProtocolInfo(): import("./definitions/protocol_definitions.js").ProtocolInfo;
66
+ /**
67
+ * Gets a type definition by name.
68
+ */
69
+ getType(name: string): Type | undefined;
70
+ /**
71
+ * Registers a message handler for a specific message name.
72
+ */
73
+ on(name: string, handler: MessageHandler): this;
74
+ /**
75
+ * Emits a message using the provided emitter.
76
+ * @param name The name of the message to emit.
77
+ * @param request The request payload.
78
+ * @param emitter The emitter to use.
79
+ */
80
+ emit(name: string, request: unknown, emitter: MessageEmitter): Promise<unknown>;
81
+ /**
82
+ * Creates a new message emitter for this protocol.
83
+ * @param transport The transport factory or instance.
84
+ * @param opts Transport options.
85
+ */
86
+ createEmitter(transport: BinaryDuplexLike | StatelessTransportFactory, opts?: MessageTransportOptions): MessageEmitter;
87
+ /**
88
+ * Creates a new message listener for this protocol.
89
+ * @param transport The transport instance.
90
+ * @param opts Transport options.
91
+ */
92
+ createListener(transport: BinaryDuplexLike, opts?: MessageTransportOptions): MessageListener;
93
+ /**
94
+ * Gets the handler registered for a specific message.
95
+ */
96
+ getHandler(name: string): MessageHandler | undefined;
97
+ /**
98
+ * Gets schema resolvers for an emitter (client) given a server hash.
99
+ */
100
+ getEmitterResolvers(hashKey: string, messageName: string): ResolverEntry | undefined;
101
+ /**
102
+ * Gets schema resolvers for a listener (server) given a client hash.
103
+ */
104
+ getListenerResolvers(hashKey: string, messageName: string): ResolverEntry | undefined;
105
+ /**
106
+ * Checks if resolvers exist for a given client hash.
107
+ */
108
+ hasListenerResolvers(hashKey: string): boolean;
109
+ /**
110
+ * Checks if resolvers exist for a given server hash.
111
+ */
112
+ hasEmitterResolvers(hashKey: string): boolean;
113
+ /**
114
+ * Ensures resolvers exist for communicating with a remote protocol (client-side).
115
+ */
116
+ ensureEmitterResolvers(hashKey: string, remote: ProtocolLike): void;
117
+ /**
118
+ * Ensures resolvers exist for communicating with a remote protocol (server-side).
119
+ */
120
+ ensureListenerResolvers(hashKey: string, emitterProtocol: ProtocolLike): void;
121
+ }