@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,605 @@
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 _ReadableTap_lengthHint;
13
+ import { bigIntToSafeNumber } from "./conversion.js";
14
+ import { compareUint8Arrays } from "./compare_bytes.js";
15
+ import { readUIntLE } from "./read_uint_le.js";
16
+ import { invert } from "./manipulate_bytes.js";
17
+ import { decode, encode } from "./text_encoding.js";
18
+ import { InMemoryReadableBuffer, InMemoryWritableBuffer, } from "./buffers/in_memory_buffer.js";
19
+ function assertValidPosition(pos) {
20
+ if (!Number.isFinite(pos) || !Number.isInteger(pos) || pos < 0 ||
21
+ Math.abs(pos) > Number.MAX_SAFE_INTEGER) {
22
+ throw new RangeError("Tap position must be an integer within the safe number range.");
23
+ }
24
+ }
25
+ function isIReadableBuffer(value) {
26
+ return typeof value === "object" && value !== null &&
27
+ typeof value.read === "function" &&
28
+ typeof value.canReadMore === "function";
29
+ }
30
+ function isIWritable(value) {
31
+ return typeof value === "object" && value !== null &&
32
+ typeof value.appendBytes === "function" &&
33
+ typeof value.isValid === "function";
34
+ }
35
+ /** Abstract base class for tap implementations that manage buffer position. */
36
+ export class TapBase {
37
+ /** Initializes the tap with the given position. */
38
+ constructor(pos) {
39
+ /** The current position in the buffer. */
40
+ Object.defineProperty(this, "pos", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ this.pos = pos;
47
+ }
48
+ /**
49
+ * Returns the current cursor position within the buffer.
50
+ */
51
+ getPos() {
52
+ return this.pos;
53
+ }
54
+ /**
55
+ * Resets the cursor to the beginning of the buffer.
56
+ */
57
+ _testOnlyResetPos() {
58
+ this.pos = 0;
59
+ }
60
+ }
61
+ /**
62
+ * Binary tap that exposes Avro-compatible read helpers on top of a readable buffer.
63
+ */
64
+ export class ReadableTap extends TapBase {
65
+ /** Creates a new ReadableTap with the given buffer and initial position. */
66
+ constructor(buf, pos = 0) {
67
+ assertValidPosition(pos);
68
+ let buffer;
69
+ let lengthHint;
70
+ if (buf instanceof ArrayBuffer) {
71
+ buffer = new InMemoryReadableBuffer(buf);
72
+ lengthHint = buf.byteLength;
73
+ }
74
+ else if (isIReadableBuffer(buf)) {
75
+ buffer = buf;
76
+ }
77
+ else {
78
+ throw new TypeError("ReadableTap requires an ArrayBuffer or IReadableBuffer.");
79
+ }
80
+ super(pos);
81
+ /** The readable buffer backing this tap. */
82
+ Object.defineProperty(this, "buffer", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: void 0
87
+ });
88
+ _ReadableTap_lengthHint.set(this, void 0);
89
+ this.buffer = buffer;
90
+ __classPrivateFieldSet(this, _ReadableTap_lengthHint, lengthHint, "f");
91
+ }
92
+ /**
93
+ * Returns whether the cursor is positioned within the buffer bounds.
94
+ */
95
+ async isValid() {
96
+ try {
97
+ const probe = await this.buffer.read(this.pos, 0);
98
+ return probe !== undefined;
99
+ }
100
+ catch (err) {
101
+ if (err instanceof RangeError) {
102
+ return false;
103
+ }
104
+ throw err;
105
+ }
106
+ }
107
+ /**
108
+ * Returns whether more data can be read from the current position without advancing the cursor.
109
+ */
110
+ async canReadMore() {
111
+ return await this.buffer.canReadMore(this.pos);
112
+ }
113
+ /**
114
+ * Returns a defensive copy of the current buffer for testing purposes.
115
+ */
116
+ async _testOnlyBuf() {
117
+ const readLength = __classPrivateFieldGet(this, _ReadableTap_lengthHint, "f") ?? this.pos;
118
+ if (readLength <= 0) {
119
+ return new Uint8Array();
120
+ }
121
+ const bytes = await this.buffer.read(0, readLength);
122
+ if (bytes) {
123
+ return bytes.slice();
124
+ }
125
+ else {
126
+ return new Uint8Array();
127
+ }
128
+ }
129
+ /**
130
+ * Returns the buffer contents from the start up to the current cursor.
131
+ * @throws RangeError if the cursor has advanced past the buffer length.
132
+ */
133
+ async getValue() {
134
+ const bytes = await this.buffer.read(0, this.pos);
135
+ if (!bytes) {
136
+ throw new RangeError("Tap position exceeds buffer length.");
137
+ }
138
+ return bytes;
139
+ }
140
+ /** Retrieves the byte at the specified position in the buffer. */
141
+ async getByteAt(position) {
142
+ const bytes = await this.buffer.read(position, 1);
143
+ if (!bytes) {
144
+ throw new RangeError("Attempt to read beyond buffer bounds.");
145
+ }
146
+ return bytes[0];
147
+ }
148
+ /**
149
+ * Reads the next byte as a boolean value and advances the cursor by one byte.
150
+ */
151
+ async readBoolean() {
152
+ const value = await this.getByteAt(this.pos);
153
+ this.pos += 1;
154
+ return !!value;
155
+ }
156
+ /**
157
+ * Skips a boolean value by advancing the cursor by one byte.
158
+ */
159
+ skipBoolean() {
160
+ this.pos++;
161
+ }
162
+ /**
163
+ * Reads a variable-length zig-zag encoded 32-bit signed integer.
164
+ */
165
+ async readInt() {
166
+ return bigIntToSafeNumber(await this.readLong(), "readInt value");
167
+ }
168
+ /**
169
+ * Reads a variable-length zig-zag encoded 64-bit signed integer as bigint.
170
+ */
171
+ async readLong() {
172
+ let pos = this.pos;
173
+ let shift = 0n;
174
+ let result = 0n;
175
+ let byte;
176
+ do {
177
+ byte = await this.getByteAt(pos++);
178
+ result |= BigInt(byte & 0x7f) << shift;
179
+ shift += 7n;
180
+ } while ((byte & 0x80) !== 0 && shift < 70n);
181
+ while ((byte & 0x80) !== 0) {
182
+ byte = await this.getByteAt(pos++);
183
+ result |= BigInt(byte & 0x7f) << shift;
184
+ shift += 7n;
185
+ }
186
+ this.pos = pos;
187
+ return (result >> 1n) ^ -(result & 1n);
188
+ }
189
+ /**
190
+ * Skips a zig-zag encoded 32-bit integer by delegating to `skipLong`.
191
+ */
192
+ async skipInt() {
193
+ await this.skipLong();
194
+ }
195
+ /**
196
+ * Skips a zig-zag encoded 64-bit integer, advancing past continuation bytes.
197
+ */
198
+ async skipLong() {
199
+ let pos = this.pos;
200
+ while ((await this.getByteAt(pos++)) & 0x80) {
201
+ /* no-op */
202
+ }
203
+ this.pos = pos;
204
+ }
205
+ /**
206
+ * Reads a 32-bit little-endian floating point number.
207
+ * @throws RangeError if the read would exceed the buffer.
208
+ */
209
+ async readFloat() {
210
+ const pos = this.pos;
211
+ this.pos += 4;
212
+ const bytes = await this.buffer.read(pos, 4);
213
+ if (!bytes) {
214
+ throw new RangeError("Attempt to read beyond buffer bounds.");
215
+ }
216
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
217
+ return view.getFloat32(0, true);
218
+ }
219
+ /**
220
+ * Skips a 32-bit floating point value by advancing four bytes.
221
+ */
222
+ skipFloat() {
223
+ this.pos += 4;
224
+ }
225
+ /**
226
+ * Reads a 64-bit little-endian floating point number.
227
+ * @throws RangeError if the read would exceed the buffer.
228
+ */
229
+ async readDouble() {
230
+ const pos = this.pos;
231
+ this.pos += 8;
232
+ const bytes = await this.buffer.read(pos, 8);
233
+ if (!bytes) {
234
+ throw new RangeError("Attempt to read beyond buffer bounds.");
235
+ }
236
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
237
+ return view.getFloat64(0, true);
238
+ }
239
+ /**
240
+ * Skips a 64-bit floating point value by advancing eight bytes.
241
+ */
242
+ skipDouble() {
243
+ this.pos += 8;
244
+ }
245
+ /**
246
+ * Reads a fixed-length byte sequence into a new buffer.
247
+ * @param len Number of bytes to read.
248
+ * @throws RangeError if the read exceeds the buffer.
249
+ */
250
+ async readFixed(len) {
251
+ const pos = this.pos;
252
+ this.pos += len;
253
+ const bytes = await this.buffer.read(pos, len);
254
+ if (!bytes) {
255
+ throw new RangeError("Attempt to read beyond buffer bounds.");
256
+ }
257
+ return bytes;
258
+ }
259
+ /**
260
+ * Skips a fixed-length byte sequence.
261
+ * @param len Number of bytes to skip.
262
+ */
263
+ skipFixed(len) {
264
+ this.pos += len;
265
+ }
266
+ /**
267
+ * Reads a length-prefixed byte sequence.
268
+ * @throws RangeError if insufficient data remains.
269
+ */
270
+ async readBytes() {
271
+ const length = bigIntToSafeNumber(await this.readLong(), "readBytes length");
272
+ return await this.readFixed(length);
273
+ }
274
+ /**
275
+ * Skips a length-prefixed byte sequence.
276
+ */
277
+ async skipBytes() {
278
+ const len = bigIntToSafeNumber(await this.readLong(), "skipBytes length");
279
+ this.pos += len;
280
+ }
281
+ /**
282
+ * Skips a length-prefixed UTF-8 string.
283
+ */
284
+ async skipString() {
285
+ const len = bigIntToSafeNumber(await this.readLong(), "skipString length");
286
+ this.pos += len;
287
+ }
288
+ /**
289
+ * Reads a length-prefixed UTF-8 string.
290
+ * @throws RangeError when the buffer is exhausted prematurely.
291
+ */
292
+ async readString() {
293
+ const len = bigIntToSafeNumber(await this.readLong(), "readString length");
294
+ const bytes = await this.readFixed(len);
295
+ return decode(bytes);
296
+ }
297
+ /**
298
+ * Compares the next boolean value with the one from another tap.
299
+ * @param tap Tap to compare against; both cursors advance.
300
+ * @returns 0 when equal, negative when this tap's value is false and the other true, positive otherwise.
301
+ */
302
+ async matchBoolean(tap) {
303
+ const val1 = await this.readBoolean();
304
+ const val2 = await tap.readBoolean();
305
+ return Number(val1) - Number(val2);
306
+ }
307
+ /**
308
+ * Compares the next zig-zag encoded 32-bit integer with another tap.
309
+ * @returns Comparison result using -1/0/1 semantics.
310
+ */
311
+ async matchInt(tap) {
312
+ return await this.matchLong(tap);
313
+ }
314
+ /**
315
+ * Compares the next zig-zag encoded 64-bit integer with another tap.
316
+ * @returns Comparison result using -1/0/1 semantics.
317
+ */
318
+ async matchLong(tap) {
319
+ const n1 = await this.readLong();
320
+ const n2 = await tap.readLong();
321
+ return n1 === n2 ? 0 : (n1 < n2 ? -1 : 1);
322
+ }
323
+ /**
324
+ * Compares the next 32-bit float value with another tap.
325
+ * @returns Comparison result using -1/0/1 semantics.
326
+ */
327
+ async matchFloat(tap) {
328
+ const n1 = await this.readFloat();
329
+ const n2 = await tap.readFloat();
330
+ return n1 === n2 ? 0 : (n1 < n2 ? -1 : 1);
331
+ }
332
+ /**
333
+ * Compares the next 64-bit float value with another tap.
334
+ * @returns Comparison result using -1/0/1 semantics.
335
+ */
336
+ async matchDouble(tap) {
337
+ const n1 = await this.readDouble();
338
+ const n2 = await tap.readDouble();
339
+ return n1 === n2 ? 0 : (n1 < n2 ? -1 : 1);
340
+ }
341
+ /**
342
+ * Compares fixed-length byte sequences from this tap and another tap.
343
+ * @param tap Tap to compare against; both cursors advance by `len`.
344
+ * @param len Number of bytes to compare.
345
+ * @returns Comparison result using -1/0/1 semantics.
346
+ */
347
+ async matchFixed(tap, len) {
348
+ const fixed1 = await this.readFixed(len);
349
+ const fixed2 = await tap.readFixed(len);
350
+ return compareUint8Arrays(fixed1, fixed2);
351
+ }
352
+ /**
353
+ * Compares length-prefixed byte sequences from this tap and another tap.
354
+ * @returns Comparison result using -1/0/1 semantics.
355
+ */
356
+ async matchBytes(tap) {
357
+ return await this.matchString(tap);
358
+ }
359
+ /**
360
+ * Compares length-prefixed UTF-8 strings read from this tap and another tap.
361
+ * @returns Comparison result using -1/0/1 semantics.
362
+ */
363
+ async matchString(tap) {
364
+ const l1 = bigIntToSafeNumber(await this.readLong(), "matchString length this");
365
+ const bytes1 = await this.readFixed(l1);
366
+ const l2 = bigIntToSafeNumber(await tap.readLong(), "matchString length tap");
367
+ const bytes2 = await tap.readFixed(l2);
368
+ return compareUint8Arrays(bytes1, bytes2);
369
+ }
370
+ /**
371
+ * Decodes the next zig-zag encoded long into an 8-byte two's complement buffer.
372
+ */
373
+ async unpackLongBytes() {
374
+ const res = new Uint8Array(8);
375
+ let n = 0;
376
+ let i = 0; // Byte index in target buffer.
377
+ let j = 6; // Bit offset in current target buffer byte.
378
+ let pos = this.pos;
379
+ let b = await this.getByteAt(pos++);
380
+ const neg = b & 1;
381
+ res.fill(0);
382
+ // Accumulate the zig-zag decoded bits into the 8-byte buffer, emitting a byte once filled.
383
+ n |= (b & 0x7f) >> 1;
384
+ while (b & 0x80) {
385
+ b = await this.getByteAt(pos++);
386
+ n |= (b & 0x7f) << j;
387
+ j += 7;
388
+ if (j >= 8) {
389
+ j -= 8;
390
+ res[i++] = n;
391
+ n >>= 8;
392
+ }
393
+ }
394
+ res[i] = n;
395
+ if (neg) {
396
+ // The low bit captured the sign; invert to restore the original negative value.
397
+ invert(res, 8);
398
+ }
399
+ this.pos = pos;
400
+ return res;
401
+ }
402
+ }
403
+ _ReadableTap_lengthHint = new WeakMap();
404
+ /**
405
+ * Binary tap that exposes Avro-compatible write helpers on top of a writable buffer.
406
+ */
407
+ export class WritableTap extends TapBase {
408
+ /**
409
+ * Creates a new WritableTap instance.
410
+ * @param buf The buffer to write to, either an ArrayBuffer or IWritableBuffer.
411
+ * @param pos The initial position in the buffer (default 0).
412
+ */
413
+ constructor(buf, pos = 0) {
414
+ assertValidPosition(pos);
415
+ let buffer;
416
+ if (buf instanceof ArrayBuffer) {
417
+ buffer = new InMemoryWritableBuffer(buf, pos);
418
+ }
419
+ else if (isIWritable(buf)) {
420
+ buffer = buf;
421
+ }
422
+ else {
423
+ throw new TypeError("WritableTap requires an ArrayBuffer or IWritableBuffer.");
424
+ }
425
+ super(pos);
426
+ /** The writable buffer backing this tap. */
427
+ Object.defineProperty(this, "buffer", {
428
+ enumerable: true,
429
+ configurable: true,
430
+ writable: true,
431
+ value: void 0
432
+ });
433
+ this.buffer = buffer;
434
+ }
435
+ /**
436
+ * Appends raw bytes to the buffer and advances the cursor.
437
+ * @param bytes The bytes to append.
438
+ */
439
+ async appendRawBytes(bytes) {
440
+ if (bytes.length === 0) {
441
+ return;
442
+ }
443
+ this.pos += bytes.length;
444
+ await this.buffer.appendBytes(bytes);
445
+ }
446
+ /** Checks if the buffer is valid. */
447
+ async isValid() {
448
+ return await this.buffer.isValid();
449
+ }
450
+ /**
451
+ * Writes a boolean value as a single byte and advances the cursor.
452
+ * @param value Boolean value to write.
453
+ */
454
+ async writeBoolean(value) {
455
+ await this.appendRawBytes(Uint8Array.of(value ? 1 : 0));
456
+ }
457
+ /**
458
+ * Writes a zig-zag encoded 32-bit signed integer.
459
+ * @param n Integer value to write.
460
+ */
461
+ async writeInt(n) {
462
+ await this.writeLong(BigInt(n));
463
+ }
464
+ /**
465
+ * Writes a zig-zag encoded 64-bit signed integer.
466
+ * @param value BigInt value to write.
467
+ */
468
+ async writeLong(value) {
469
+ let n = value;
470
+ if (n < 0n) {
471
+ n = ((-n) << 1n) - 1n;
472
+ }
473
+ else {
474
+ n <<= 1n;
475
+ }
476
+ const bytes = [];
477
+ while (n >= 0x80n) {
478
+ bytes.push(Number(n & 0x7fn) | 0x80);
479
+ n >>= 7n;
480
+ }
481
+ bytes.push(Number(n));
482
+ await this.appendRawBytes(Uint8Array.from(bytes));
483
+ }
484
+ /**
485
+ * Writes a 32-bit little-endian floating point number.
486
+ * @param value Float to write.
487
+ */
488
+ async writeFloat(value) {
489
+ const buffer = new ArrayBuffer(4);
490
+ const view = new DataView(buffer);
491
+ view.setFloat32(0, value, true);
492
+ await this.appendRawBytes(new Uint8Array(buffer));
493
+ }
494
+ /**
495
+ * Writes a 64-bit little-endian floating point number.
496
+ * @param value Double precision value to write.
497
+ */
498
+ async writeDouble(value) {
499
+ const buffer = new ArrayBuffer(8);
500
+ const view = new DataView(buffer);
501
+ view.setFloat64(0, value, true);
502
+ await this.appendRawBytes(new Uint8Array(buffer));
503
+ }
504
+ /**
505
+ * Writes a fixed-length byte sequence from the provided buffer.
506
+ * @param buf Source buffer to copy from.
507
+ * @param len Optional number of bytes to write; defaults to the buffer length.
508
+ */
509
+ async writeFixed(buf, len) {
510
+ const length = len ?? buf.length;
511
+ if (length === 0) {
512
+ return;
513
+ }
514
+ await this.appendRawBytes(buf.slice(0, length));
515
+ }
516
+ /**
517
+ * Writes a length-prefixed byte sequence backed by the provided buffer.
518
+ * @param buf Bytes to write.
519
+ */
520
+ async writeBytes(buf) {
521
+ const len = buf.length;
522
+ await this.writeLong(BigInt(len));
523
+ await this.writeFixed(buf, len);
524
+ }
525
+ /**
526
+ * Writes a length-prefixed UTF-8 string.
527
+ * @param str String to encode and write.
528
+ */
529
+ async writeString(str) {
530
+ const encoded = encode(str);
531
+ const len = encoded.length;
532
+ await this.writeLong(BigInt(len));
533
+ await this.writeFixed(encoded, len);
534
+ }
535
+ /**
536
+ * Writes a binary string as raw bytes without a length prefix.
537
+ * @param str Source string containing binary data.
538
+ * @param len Number of bytes from the string to write.
539
+ */
540
+ async writeBinary(str, len) {
541
+ if (len <= 0) {
542
+ return;
543
+ }
544
+ const bytes = new Uint8Array(len);
545
+ for (let i = 0; i < len; i++) {
546
+ bytes[i] = str.charCodeAt(i) & 0xff;
547
+ }
548
+ await this.appendRawBytes(bytes);
549
+ }
550
+ /**
551
+ * Encodes an 8-byte two's complement integer into zig-zag encoded varint bytes.
552
+ * @param arr Buffer containing the 8-byte value to encode; reused during processing.
553
+ */
554
+ async packLongBytes(arr) {
555
+ const bufView = new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
556
+ const neg = (bufView.getUint8(7) & 0x80) >> 7;
557
+ let j = 1;
558
+ let k = 0;
559
+ let m = 3;
560
+ let n;
561
+ if (neg) {
562
+ // Temporarily convert to magnitude so the zig-zag representation can strip the sign bit.
563
+ invert(arr, 8);
564
+ n = 1;
565
+ }
566
+ else {
567
+ n = 0;
568
+ }
569
+ const parts = [
570
+ readUIntLE(bufView, 0, 3),
571
+ readUIntLE(bufView, 3, 3),
572
+ readUIntLE(bufView, 6, 2),
573
+ ];
574
+ while (m && !parts[--m]) {
575
+ /* skip trailing zeros */
576
+ }
577
+ const emitted = [];
578
+ // Pack 24-bit chunks into the zig-zag bucket, flushing continuation bytes as needed.
579
+ while (k < m) {
580
+ n |= parts[k++] << j;
581
+ j += 24;
582
+ while (j > 7) {
583
+ emitted.push((n & 0x7f) | 0x80);
584
+ n >>= 7;
585
+ j -= 7;
586
+ }
587
+ }
588
+ n |= parts[m] << j;
589
+ do {
590
+ const byte = n & 0x7f;
591
+ n >>= 7;
592
+ if (n) {
593
+ emitted.push(byte | 0x80);
594
+ }
595
+ else {
596
+ emitted.push(byte);
597
+ }
598
+ } while (n);
599
+ if (neg) {
600
+ // Restore the original negative representation in the supplied buffer.
601
+ invert(arr, 8);
602
+ }
603
+ await this.appendRawBytes(Uint8Array.from(emitted));
604
+ }
605
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Encodes a string into a Uint8Array using UTF-8 encoding.
3
+ * @param input The string to encode.
4
+ * @returns A Uint8Array representing the encoded string.
5
+ */
6
+ export declare const encode: (input: string) => Uint8Array;
7
+ /**
8
+ * Decodes a Uint8Array into a string using UTF-8 encoding.
9
+ * @param bytes The Uint8Array to decode.
10
+ * @returns The decoded string.
11
+ */
12
+ export declare const decode: (bytes: Uint8Array) => string;
@@ -0,0 +1,14 @@
1
+ const encoder = new TextEncoder();
2
+ const decoder = new TextDecoder();
3
+ /**
4
+ * Encodes a string into a Uint8Array using UTF-8 encoding.
5
+ * @param input The string to encode.
6
+ * @returns A Uint8Array representing the encoded string.
7
+ */
8
+ export const encode = (input) => encoder.encode(input);
9
+ /**
10
+ * Decodes a Uint8Array into a string using UTF-8 encoding.
11
+ * @param bytes The Uint8Array to decode.
12
+ * @returns The decoded string.
13
+ */
14
+ export const decode = (bytes) => decoder.decode(bytes);
@@ -0,0 +1,54 @@
1
+ import { Type } from "../schemas/type.js";
2
+ /**
3
+ * Represents an Avro schema definition.
4
+ * Can be a pre-constructed Type, a type name (string), a schema object,
5
+ * or an array representing a union.
6
+ */
7
+ export type SchemaLike = Type | string | SchemaObject | SchemaLike[];
8
+ /**
9
+ * Represents an Avro schema object, which defines the structure of data in Avro format.
10
+ * It can represent primitive types, complex types like records and arrays, or named types.
11
+ */
12
+ export interface SchemaObject {
13
+ /** The type of the Avro schema, such as "record", "array", "enum", "fixed", or a primitive type like "string". */
14
+ type: unknown;
15
+ /** The name of the schema for named types like records, enums, and fixed types. */
16
+ name?: unknown;
17
+ /** The namespace for the schema, used to qualify named types and avoid naming conflicts. */
18
+ namespace?: unknown;
19
+ /** Alternative names (aliases) for the schema, allowing backward compatibility or multiple references. */
20
+ aliases?: unknown;
21
+ /** The fields of a record schema, an array of field definitions each specifying name, type, and optional default. */
22
+ fields?: unknown;
23
+ /** The symbols for an enum schema, an array of string values representing the possible enumeration values. */
24
+ symbols?: unknown;
25
+ /** The size in bytes for a fixed schema, specifying the exact length of the fixed-size data. */
26
+ size?: unknown;
27
+ /** The type of items in an array schema, defining the schema for each element in the array. */
28
+ items?: unknown;
29
+ /** The type of values in a map schema, defining the schema for the values associated with string keys. */
30
+ values?: unknown;
31
+ /** The default value for a field in a record or for an enum, used when the field is missing in the data. */
32
+ default?: unknown;
33
+ /** The logical type annotation, such as "date", "decimal", or "uuid", providing semantic meaning to the underlying type. */
34
+ logicalType?: unknown;
35
+ /** Allows additional arbitrary properties in the schema object for extensibility. */
36
+ [key: string]: unknown;
37
+ }
38
+ /**
39
+ * Options for creating an Avro type from a schema.
40
+ */
41
+ export interface CreateTypeOptions {
42
+ /**
43
+ * Default namespace to use for named types.
44
+ */
45
+ namespace?: string;
46
+ /**
47
+ * Registry of shared named types.
48
+ */
49
+ registry?: Map<string, Type>;
50
+ }
51
+ /**
52
+ * Constructs an Avro {@link Type} from a schema definition.
53
+ */
54
+ export declare function createType(schema: SchemaLike, options?: CreateTypeOptions): Type;