@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,11 @@
1
+ declare global {
2
+ interface Object {
3
+ /**
4
+ * Determines whether an object has a property with the specified name.
5
+ * @param o An object.
6
+ * @param v A property name.
7
+ */
8
+ hasOwn(o: object, v: PropertyKey): boolean;
9
+ }
10
+ }
11
+ export {};
@@ -0,0 +1,15 @@
1
+ // https://github.com/tc39/proposal-accessible-object-hasownproperty/blob/main/polyfill.js
2
+ if (!Object.hasOwn) {
3
+ Object.defineProperty(Object, "hasOwn", {
4
+ value: function (object, property) {
5
+ if (object == null) {
6
+ throw new TypeError("Cannot convert undefined or null to object");
7
+ }
8
+ return Object.prototype.hasOwnProperty.call(Object(object), property);
9
+ },
10
+ configurable: true,
11
+ enumerable: false,
12
+ writable: true,
13
+ });
14
+ }
15
+ export {};
@@ -0,0 +1,156 @@
1
+ import type { IReadableBuffer } from "./serialization/buffers/buffer.js";
2
+ import { type DecoderRegistry, type ParsedAvroHeader } from "./serialization/avro_file_parser.js";
3
+ export type { DecoderRegistry, ParsedAvroHeader };
4
+ /**
5
+ * Options for reading Avro data, including schema resolution, custom decoders, and close hooks.
6
+ */
7
+ export interface ReaderSchemaOptions {
8
+ /** Optional reader schema used to resolve records written with a different schema. */
9
+ readerSchema?: unknown;
10
+ /** Custom codec decoders. Cannot include "null" or "deflate" as they are built-in. */
11
+ decoders?: DecoderRegistry;
12
+ /** Optional hook to call when closing the reader. */
13
+ closeHook?: () => Promise<void> | void;
14
+ }
15
+ /**
16
+ * Options for URL-based reading.
17
+ */
18
+ export interface FromUrlOptions extends ReaderSchemaOptions {
19
+ /** Cache size for stream buffering in bytes (default: 0 = unlimited) */
20
+ cacheSize?: number;
21
+ /** Custom fetch init options (optional) */
22
+ fetchInit?: RequestInit;
23
+ }
24
+ /**
25
+ * Options for stream-based reading.
26
+ */
27
+ export interface FromStreamOptions extends ReaderSchemaOptions {
28
+ /** Cache size for stream buffering in bytes (default: 0 = unlimited) */
29
+ cacheSize?: number;
30
+ /** Optional hook to call when closing the reader. */
31
+ closeHook?: () => Promise<void> | void;
32
+ }
33
+ /**
34
+ * Options for buffer- or blob-based reading.
35
+ */
36
+ export type FromBufferOptions = ReaderSchemaOptions;
37
+ /**
38
+ * Interface for Avro reader instances that provide access to header and records.
39
+ */
40
+ export interface AvroReaderInstance {
41
+ /**
42
+ * Gets the parsed Avro file header with proper typing.
43
+ *
44
+ * @returns Promise that resolves to the parsed header information.
45
+ */
46
+ getHeader(): Promise<ParsedAvroHeader>;
47
+ /**
48
+ * Closes the reader and releases any resources.
49
+ */
50
+ close(): Promise<void>;
51
+ /**
52
+ * Returns an async iterator over the records in the Avro file.
53
+ */
54
+ iterRecords(): AsyncIterableIterator<unknown>;
55
+ }
56
+ /**
57
+ * Public API for reading Avro object container files from various sources.
58
+ *
59
+ * This class provides static factory methods for creating Avro readers from
60
+ * different data sources while maintaining a consistent interface.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const readerSchema = {
65
+ * type: "record",
66
+ * name: "test.Weather",
67
+ * fields: [
68
+ * { name: "station", type: "string" },
69
+ * { name: "temp", type: "int" },
70
+ * ],
71
+ * };
72
+ *
73
+ * // Read from a buffer (for example when the Avro file is already in memory)
74
+ * const bufferReader = AvroReader.fromBuffer(buffer, { readerSchema });
75
+ * for await (const record of bufferReader.iterRecords()) {
76
+ * console.log(record);
77
+ * }
78
+ *
79
+ * // Read from a blob (e.g., file input)
80
+ * const blobReader = AvroReader.fromBlob(blob, { readerSchema });
81
+ * for await (const record of blobReader.iterRecords()) {
82
+ * console.log(record);
83
+ * }
84
+ *
85
+ * // Read from a URL with unlimited buffering
86
+ * const urlReader = await AvroReader.fromUrl("data.avro", { readerSchema });
87
+ * for await (const record of urlReader.iterRecords()) {
88
+ * console.log(record);
89
+ * }
90
+ *
91
+ * // Read from a URL with limited memory usage
92
+ * const cachedUrlReader = await AvroReader.fromUrl("data.avro", {
93
+ * cacheSize: 1024 * 1024,
94
+ * readerSchema,
95
+ * });
96
+ * for await (const record of cachedUrlReader.iterRecords()) {
97
+ * console.log(record);
98
+ * }
99
+ *
100
+ * // Read from a stream with optional caching
101
+ * const streamReader = AvroReader.fromStream(stream, {
102
+ * cacheSize: 1024,
103
+ * readerSchema,
104
+ * });
105
+ * for await (const record of streamReader.iterRecords()) {
106
+ * console.log(record);
107
+ * }
108
+ * ```
109
+ */
110
+ export declare class AvroReader {
111
+ /**
112
+ * Creates an Avro reader from any readable buffer.
113
+ *
114
+ * This is the core method that all other factory methods delegate to.
115
+ *
116
+ * @param buffer The readable buffer containing Avro data.
117
+ * @returns AvroReaderInstance that provides access to header and records.
118
+ */
119
+ static fromBuffer(buffer: IReadableBuffer, options?: FromBufferOptions): AvroReaderInstance;
120
+ /**
121
+ * Creates an Avro reader from a Blob.
122
+ *
123
+ * Uses BlobReadableBuffer for efficient random access without loading
124
+ * the entire blob into memory.
125
+ *
126
+ * @param blob The blob containing Avro data.
127
+ * @returns AvroReaderInstance that provides access to header and records.
128
+ */
129
+ static fromBlob(blob: Blob, options?: FromBufferOptions): AvroReaderInstance;
130
+ /**
131
+ * Creates an Avro reader from a URL.
132
+ *
133
+ * Fetches the URL and creates a stream-based reader. The caching strategy
134
+ * depends on the cacheSize option:
135
+ * - cacheSize = 0 (default): Uses ForwardOnlyStreamReadableBufferAdapter for unlimited buffering
136
+ * - cacheSize > 0: Uses FixedSizeStreamReadableBufferAdapter with the specified window size
137
+ *
138
+ * @param url The URL to fetch Avro data from.
139
+ * @param options Configuration options for URL reading.
140
+ * @returns Promise that resolves to AvroReaderInstance providing access to header and records.
141
+ */
142
+ static fromUrl(url: string, options?: FromUrlOptions): Promise<AvroReaderInstance>;
143
+ /**
144
+ * Creates an Avro reader from a ReadableStream.
145
+ *
146
+ * Creates a stream-based reader with configurable caching. The caching strategy
147
+ * depends on the cacheSize option:
148
+ * - cacheSize = 0 (default): Uses ForwardOnlyStreamReadableBufferAdapter for unlimited buffering
149
+ * - cacheSize > 0: Uses FixedSizeStreamReadableBufferAdapter with the specified window size
150
+ *
151
+ * @param stream The readable stream containing Avro data.
152
+ * @param options Configuration options for stream reading.
153
+ * @returns AvroReaderInstance that provides access to header and records.
154
+ */
155
+ static fromStream(stream: ReadableStream, options?: FromStreamOptions): AvroReaderInstance;
156
+ }
@@ -0,0 +1,201 @@
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 _AvroReaderInstanceImpl_parser, _AvroReaderInstanceImpl_recordIterator, _AvroReaderInstanceImpl_closeHook, _AvroReaderInstanceImpl_closed;
13
+ import { BlobReadableBuffer } from "./serialization/buffers/blob_readable_buffer.js";
14
+ import { StreamReadableBuffer } from "./serialization/streams/stream_readable_buffer.js";
15
+ import { FixedSizeStreamReadableBufferAdapter } from "./serialization/streams/fixed_size_stream_readable_buffer_adapter.js";
16
+ import { ForwardOnlyStreamReadableBufferAdapter } from "./serialization/streams/forward_only_stream_readable_buffer_adapter.js";
17
+ import { AvroFileParser, } from "./serialization/avro_file_parser.js";
18
+ /**
19
+ * Default cache size: 0 (unlimited, uses forward-only adapter)
20
+ */
21
+ const DEFAULT_CACHE_SIZE = 0;
22
+ /**
23
+ * Internal implementation of AvroReaderInstance that wraps AvroFileParser.
24
+ */
25
+ class AvroReaderInstanceImpl {
26
+ constructor(parser, closeHook) {
27
+ _AvroReaderInstanceImpl_parser.set(this, void 0);
28
+ _AvroReaderInstanceImpl_recordIterator.set(this, void 0);
29
+ _AvroReaderInstanceImpl_closeHook.set(this, void 0);
30
+ _AvroReaderInstanceImpl_closed.set(this, false);
31
+ __classPrivateFieldSet(this, _AvroReaderInstanceImpl_parser, parser, "f");
32
+ __classPrivateFieldSet(this, _AvroReaderInstanceImpl_recordIterator, parser.iterRecords(), "f");
33
+ __classPrivateFieldSet(this, _AvroReaderInstanceImpl_closeHook, closeHook, "f");
34
+ }
35
+ async getHeader() {
36
+ return await __classPrivateFieldGet(this, _AvroReaderInstanceImpl_parser, "f").getHeader();
37
+ }
38
+ iterRecords() {
39
+ return __classPrivateFieldGet(this, _AvroReaderInstanceImpl_recordIterator, "f");
40
+ }
41
+ async close() {
42
+ if (__classPrivateFieldGet(this, _AvroReaderInstanceImpl_closed, "f")) {
43
+ return;
44
+ }
45
+ if (__classPrivateFieldGet(this, _AvroReaderInstanceImpl_closeHook, "f")) {
46
+ try {
47
+ await __classPrivateFieldGet(this, _AvroReaderInstanceImpl_closeHook, "f").call(this);
48
+ }
49
+ catch {
50
+ // Ignore close hook errors
51
+ }
52
+ __classPrivateFieldSet(this, _AvroReaderInstanceImpl_closeHook, undefined, "f");
53
+ }
54
+ __classPrivateFieldSet(this, _AvroReaderInstanceImpl_closed, true, "f");
55
+ }
56
+ }
57
+ _AvroReaderInstanceImpl_parser = new WeakMap(), _AvroReaderInstanceImpl_recordIterator = new WeakMap(), _AvroReaderInstanceImpl_closeHook = new WeakMap(), _AvroReaderInstanceImpl_closed = new WeakMap();
58
+ /**
59
+ * Public API for reading Avro object container files from various sources.
60
+ *
61
+ * This class provides static factory methods for creating Avro readers from
62
+ * different data sources while maintaining a consistent interface.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const readerSchema = {
67
+ * type: "record",
68
+ * name: "test.Weather",
69
+ * fields: [
70
+ * { name: "station", type: "string" },
71
+ * { name: "temp", type: "int" },
72
+ * ],
73
+ * };
74
+ *
75
+ * // Read from a buffer (for example when the Avro file is already in memory)
76
+ * const bufferReader = AvroReader.fromBuffer(buffer, { readerSchema });
77
+ * for await (const record of bufferReader.iterRecords()) {
78
+ * console.log(record);
79
+ * }
80
+ *
81
+ * // Read from a blob (e.g., file input)
82
+ * const blobReader = AvroReader.fromBlob(blob, { readerSchema });
83
+ * for await (const record of blobReader.iterRecords()) {
84
+ * console.log(record);
85
+ * }
86
+ *
87
+ * // Read from a URL with unlimited buffering
88
+ * const urlReader = await AvroReader.fromUrl("data.avro", { readerSchema });
89
+ * for await (const record of urlReader.iterRecords()) {
90
+ * console.log(record);
91
+ * }
92
+ *
93
+ * // Read from a URL with limited memory usage
94
+ * const cachedUrlReader = await AvroReader.fromUrl("data.avro", {
95
+ * cacheSize: 1024 * 1024,
96
+ * readerSchema,
97
+ * });
98
+ * for await (const record of cachedUrlReader.iterRecords()) {
99
+ * console.log(record);
100
+ * }
101
+ *
102
+ * // Read from a stream with optional caching
103
+ * const streamReader = AvroReader.fromStream(stream, {
104
+ * cacheSize: 1024,
105
+ * readerSchema,
106
+ * });
107
+ * for await (const record of streamReader.iterRecords()) {
108
+ * console.log(record);
109
+ * }
110
+ * ```
111
+ */
112
+ export class AvroReader {
113
+ /**
114
+ * Creates an Avro reader from any readable buffer.
115
+ *
116
+ * This is the core method that all other factory methods delegate to.
117
+ *
118
+ * @param buffer The readable buffer containing Avro data.
119
+ * @returns AvroReaderInstance that provides access to header and records.
120
+ */
121
+ static fromBuffer(buffer, options) {
122
+ const parser = new AvroFileParser(buffer, {
123
+ readerSchema: options?.readerSchema,
124
+ decoders: options?.decoders,
125
+ });
126
+ return new AvroReaderInstanceImpl(parser, options?.closeHook);
127
+ }
128
+ /**
129
+ * Creates an Avro reader from a Blob.
130
+ *
131
+ * Uses BlobReadableBuffer for efficient random access without loading
132
+ * the entire blob into memory.
133
+ *
134
+ * @param blob The blob containing Avro data.
135
+ * @returns AvroReaderInstance that provides access to header and records.
136
+ */
137
+ static fromBlob(blob, options) {
138
+ const buffer = new BlobReadableBuffer(blob);
139
+ return AvroReader.fromBuffer(buffer, options);
140
+ }
141
+ /**
142
+ * Creates an Avro reader from a URL.
143
+ *
144
+ * Fetches the URL and creates a stream-based reader. The caching strategy
145
+ * depends on the cacheSize option:
146
+ * - cacheSize = 0 (default): Uses ForwardOnlyStreamReadableBufferAdapter for unlimited buffering
147
+ * - cacheSize > 0: Uses FixedSizeStreamReadableBufferAdapter with the specified window size
148
+ *
149
+ * @param url The URL to fetch Avro data from.
150
+ * @param options Configuration options for URL reading.
151
+ * @returns Promise that resolves to AvroReaderInstance providing access to header and records.
152
+ */
153
+ static async fromUrl(url, options) {
154
+ const response = await fetch(url, options?.fetchInit);
155
+ if (!response.ok) {
156
+ throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
157
+ }
158
+ if (!response.body) {
159
+ throw new Error(`Response body is null for ${url}`);
160
+ }
161
+ const stream = response.body;
162
+ return AvroReader.fromStream(stream, {
163
+ cacheSize: options?.cacheSize,
164
+ readerSchema: options?.readerSchema,
165
+ decoders: options?.decoders,
166
+ closeHook: async () => {
167
+ await stream.cancel();
168
+ },
169
+ });
170
+ }
171
+ /**
172
+ * Creates an Avro reader from a ReadableStream.
173
+ *
174
+ * Creates a stream-based reader with configurable caching. The caching strategy
175
+ * depends on the cacheSize option:
176
+ * - cacheSize = 0 (default): Uses ForwardOnlyStreamReadableBufferAdapter for unlimited buffering
177
+ * - cacheSize > 0: Uses FixedSizeStreamReadableBufferAdapter with the specified window size
178
+ *
179
+ * @param stream The readable stream containing Avro data.
180
+ * @param options Configuration options for stream reading.
181
+ * @returns AvroReaderInstance that provides access to header and records.
182
+ */
183
+ static fromStream(stream, options) {
184
+ const cacheSize = options?.cacheSize ?? DEFAULT_CACHE_SIZE;
185
+ const streamBuffer = new StreamReadableBuffer(stream);
186
+ let buffer;
187
+ if (cacheSize > 0) {
188
+ // Use fixed-size adapter for limited memory usage
189
+ buffer = new FixedSizeStreamReadableBufferAdapter(streamBuffer, cacheSize);
190
+ }
191
+ else {
192
+ // Use forward-only adapter for unlimited buffering
193
+ buffer = new ForwardOnlyStreamReadableBufferAdapter(streamBuffer);
194
+ }
195
+ return AvroReader.fromBuffer(buffer, {
196
+ readerSchema: options?.readerSchema,
197
+ decoders: options?.decoders,
198
+ closeHook: options?.closeHook,
199
+ });
200
+ }
201
+ }
@@ -0,0 +1,75 @@
1
+ import type { IWritableBuffer } from "./serialization/buffers/buffer.js";
2
+ import { type AvroWriterOptions } from "./serialization/avro_file_writer.js";
3
+ export type { AvroWriterOptions } from "./serialization/avro_file_writer.js";
4
+ export type { EncoderRegistry } from "./serialization/encoders/encoder.js";
5
+ /**
6
+ * Interface exposed by Avro writer instances.
7
+ */
8
+ export interface AvroWriterInstance {
9
+ /**
10
+ * Appends a single record to the output.
11
+ */
12
+ append(record: unknown): Promise<void>;
13
+ /**
14
+ * Manually flush pending records to a block.
15
+ * This can be called to force writing of accumulated records before the block size threshold is reached.
16
+ */
17
+ flushBlock(): Promise<void>;
18
+ /**
19
+ * Flushes pending data and closes the writer.
20
+ */
21
+ close(): Promise<void>;
22
+ }
23
+ /**
24
+ * Public API for writing Avro object container files to various destinations.
25
+ *
26
+ * The writer mirrors the structure of {@link AvroReader}, providing factory
27
+ * helpers that adapt different buffer and stream implementations to the
28
+ * internal {@link AvroFileWriter}.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const schema = {
33
+ * type: "record",
34
+ * name: "example.Event",
35
+ * fields: [
36
+ * { name: "id", type: "int" },
37
+ * { name: "message", type: "string" },
38
+ * ],
39
+ * };
40
+ *
41
+ * // Write to an in-memory buffer
42
+ * const memoryBuffer = new InMemoryWritableBuffer(new ArrayBuffer(1024));
43
+ * const bufferWriter = AvroWriter.toBuffer(memoryBuffer, { schema });
44
+ * await bufferWriter.append({ id: 1, message: "hello" });
45
+ * await bufferWriter.close();
46
+ *
47
+ * // Write to a WritableStream (e.g., network, file)
48
+ * const stream = new WritableStream<Uint8Array>({
49
+ * async write(chunk) {
50
+ * await someSink.write(chunk);
51
+ * },
52
+ * });
53
+ * const streamWriter = AvroWriter.toStream(stream, { schema, codec: "deflate" });
54
+ * for (const record of records) {
55
+ * await streamWriter.append(record);
56
+ * }
57
+ * await streamWriter.close();
58
+ * ```
59
+ */
60
+ export declare class AvroWriter {
61
+ /**
62
+ * Creates an Avro writer from any {@link IWritableBuffer}.
63
+ *
64
+ * @param buffer Writable buffer that will receive the Avro container output.
65
+ * @param options Writer configuration including schema, codec, metadata, etc.
66
+ */
67
+ static toBuffer(buffer: IWritableBuffer, options: AvroWriterOptions): AvroWriterInstance;
68
+ /**
69
+ * Creates an Avro writer that emits data to a {@link WritableStream}.
70
+ *
71
+ * @param stream Writable stream that will receive chunked Avro data.
72
+ * @param options Writer configuration including schema, codec, metadata, etc.
73
+ */
74
+ static toStream(stream: WritableStream<Uint8Array>, options: AvroWriterOptions): AvroWriterInstance;
75
+ }
@@ -0,0 +1,105 @@
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 _AvroWriterInstanceImpl_writer, _AvroWriterInstanceImpl_closeHook, _AvroWriterInstanceImpl_closed;
13
+ import { AvroFileWriter, } from "./serialization/avro_file_writer.js";
14
+ import { StreamWritableBuffer } from "./serialization/streams/stream_writable_buffer.js";
15
+ import { StreamWritableBufferAdapter } from "./serialization/streams/stream_writable_buffer_adapter.js";
16
+ class AvroWriterInstanceImpl {
17
+ constructor(writer, closeHook) {
18
+ _AvroWriterInstanceImpl_writer.set(this, void 0);
19
+ _AvroWriterInstanceImpl_closeHook.set(this, void 0);
20
+ _AvroWriterInstanceImpl_closed.set(this, false);
21
+ __classPrivateFieldSet(this, _AvroWriterInstanceImpl_writer, writer, "f");
22
+ __classPrivateFieldSet(this, _AvroWriterInstanceImpl_closeHook, closeHook, "f");
23
+ }
24
+ async append(record) {
25
+ await __classPrivateFieldGet(this, _AvroWriterInstanceImpl_writer, "f").append(record);
26
+ }
27
+ async flushBlock() {
28
+ await __classPrivateFieldGet(this, _AvroWriterInstanceImpl_writer, "f").flushBlock();
29
+ }
30
+ async close() {
31
+ if (__classPrivateFieldGet(this, _AvroWriterInstanceImpl_closed, "f")) {
32
+ return;
33
+ }
34
+ await __classPrivateFieldGet(this, _AvroWriterInstanceImpl_writer, "f").close();
35
+ if (__classPrivateFieldGet(this, _AvroWriterInstanceImpl_closeHook, "f")) {
36
+ await __classPrivateFieldGet(this, _AvroWriterInstanceImpl_closeHook, "f").call(this);
37
+ __classPrivateFieldSet(this, _AvroWriterInstanceImpl_closeHook, undefined, "f");
38
+ }
39
+ __classPrivateFieldSet(this, _AvroWriterInstanceImpl_closed, true, "f");
40
+ }
41
+ }
42
+ _AvroWriterInstanceImpl_writer = new WeakMap(), _AvroWriterInstanceImpl_closeHook = new WeakMap(), _AvroWriterInstanceImpl_closed = new WeakMap();
43
+ /**
44
+ * Public API for writing Avro object container files to various destinations.
45
+ *
46
+ * The writer mirrors the structure of {@link AvroReader}, providing factory
47
+ * helpers that adapt different buffer and stream implementations to the
48
+ * internal {@link AvroFileWriter}.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const schema = {
53
+ * type: "record",
54
+ * name: "example.Event",
55
+ * fields: [
56
+ * { name: "id", type: "int" },
57
+ * { name: "message", type: "string" },
58
+ * ],
59
+ * };
60
+ *
61
+ * // Write to an in-memory buffer
62
+ * const memoryBuffer = new InMemoryWritableBuffer(new ArrayBuffer(1024));
63
+ * const bufferWriter = AvroWriter.toBuffer(memoryBuffer, { schema });
64
+ * await bufferWriter.append({ id: 1, message: "hello" });
65
+ * await bufferWriter.close();
66
+ *
67
+ * // Write to a WritableStream (e.g., network, file)
68
+ * const stream = new WritableStream<Uint8Array>({
69
+ * async write(chunk) {
70
+ * await someSink.write(chunk);
71
+ * },
72
+ * });
73
+ * const streamWriter = AvroWriter.toStream(stream, { schema, codec: "deflate" });
74
+ * for (const record of records) {
75
+ * await streamWriter.append(record);
76
+ * }
77
+ * await streamWriter.close();
78
+ * ```
79
+ */
80
+ export class AvroWriter {
81
+ /**
82
+ * Creates an Avro writer from any {@link IWritableBuffer}.
83
+ *
84
+ * @param buffer Writable buffer that will receive the Avro container output.
85
+ * @param options Writer configuration including schema, codec, metadata, etc.
86
+ */
87
+ static toBuffer(buffer, options) {
88
+ const writer = new AvroFileWriter(buffer, options);
89
+ return new AvroWriterInstanceImpl(writer);
90
+ }
91
+ /**
92
+ * Creates an Avro writer that emits data to a {@link WritableStream}.
93
+ *
94
+ * @param stream Writable stream that will receive chunked Avro data.
95
+ * @param options Writer configuration including schema, codec, metadata, etc.
96
+ */
97
+ static toStream(stream, options) {
98
+ const streamBuffer = new StreamWritableBuffer(stream);
99
+ const adapter = new StreamWritableBufferAdapter(streamBuffer);
100
+ const writer = new AvroFileWriter(adapter, options);
101
+ return new AvroWriterInstanceImpl(writer, async () => {
102
+ await adapter.close();
103
+ });
104
+ }
105
+ }
@@ -0,0 +1,4 @@
1
+ /** Concatenates an array of Uint8Arrays into a single Uint8Array. */
2
+ export declare function concatUint8Arrays(parts: Uint8Array[]): Uint8Array;
3
+ /** Converts an ArrayBuffer or Uint8Array to a Uint8Array. */
4
+ export declare function toUint8Array(data: ArrayBuffer | Uint8Array): Uint8Array;
@@ -0,0 +1,18 @@
1
+ /** Concatenates an array of Uint8Arrays into a single Uint8Array. */
2
+ export function concatUint8Arrays(parts) {
3
+ if (parts.length === 0) {
4
+ return new Uint8Array(0);
5
+ }
6
+ const length = parts.reduce((sum, part) => sum + part.length, 0);
7
+ const result = new Uint8Array(length);
8
+ let offset = 0;
9
+ for (const part of parts) {
10
+ result.set(part, offset);
11
+ offset += part.length;
12
+ }
13
+ return result;
14
+ }
15
+ /** Converts an ArrayBuffer or Uint8Array to a Uint8Array. */
16
+ export function toUint8Array(data) {
17
+ return data instanceof Uint8Array ? data : new Uint8Array(data);
18
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A circular buffer implementation that maintains a rolling window of Uint8Array data.
3
+ * Uses copyWithin for efficient window sliding and provides memory-efficient random access.
4
+ */
5
+ export declare class CircularBuffer {
6
+ #private;
7
+ /**
8
+ * Creates a new circular buffer with the specified capacity.
9
+ * @param capacity Maximum number of bytes the buffer can hold
10
+ * @throws RangeError if capacity is not positive
11
+ */
12
+ constructor(capacity: number);
13
+ /**
14
+ * Maximum capacity of the buffer in bytes.
15
+ */
16
+ capacity(): number;
17
+ /**
18
+ * Current number of bytes in the buffer.
19
+ */
20
+ length(): number;
21
+ /**
22
+ * Position in the overall data stream where the current window starts.
23
+ */
24
+ windowStart(): number;
25
+ /**
26
+ * Position in the overall data stream where the current window ends (exclusive).
27
+ */
28
+ windowEnd(): number;
29
+ /**
30
+ * Clears the buffer, resetting the window to empty.
31
+ */
32
+ clear(): void;
33
+ /**
34
+ * Adds data to the buffer, sliding the window if necessary.
35
+ * @param data Uint8Array to add
36
+ * @throws RangeError if data size exceeds buffer capacity
37
+ */
38
+ push(data: Uint8Array): void;
39
+ /**
40
+ * Retrieves a contiguous sequence of bytes from the buffer.
41
+ * @param start Start position relative to the overall data stream
42
+ * @param size Number of bytes to retrieve
43
+ * @returns Readonly view of the requested bytes (shallow copy, no allocation)
44
+ * @throws RangeError if start is before window start or size is negative
45
+ * @throws RangeError if requested range extends beyond window end
46
+ */
47
+ get(start: number, size: number): Readonly<Uint8Array>;
48
+ }