@platformatic/kafka 0.1.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 (223) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +270 -0
  3. package/dist/apis/admin/alter-client-quotas.d.ts +33 -0
  4. package/dist/apis/admin/alter-client-quotas.js +64 -0
  5. package/dist/apis/admin/alter-configs.d.ts +26 -0
  6. package/dist/apis/admin/alter-configs.js +57 -0
  7. package/dist/apis/admin/alter-partition-reassignments.d.ts +30 -0
  8. package/dist/apis/admin/alter-partition-reassignments.js +68 -0
  9. package/dist/apis/admin/alter-partition.d.ts +39 -0
  10. package/dist/apis/admin/alter-partition.js +87 -0
  11. package/dist/apis/admin/alter-replica-log-dirs.d.ts +26 -0
  12. package/dist/apis/admin/alter-replica-log-dirs.js +55 -0
  13. package/dist/apis/admin/alter-user-scram-credentials.d.ts +27 -0
  14. package/dist/apis/admin/alter-user-scram-credentials.js +60 -0
  15. package/dist/apis/admin/consumer-group-describe.d.ts +41 -0
  16. package/dist/apis/admin/consumer-group-describe.js +103 -0
  17. package/dist/apis/admin/create-acls.d.ts +24 -0
  18. package/dist/apis/admin/create-acls.js +55 -0
  19. package/dist/apis/admin/create-delegation-token.d.ts +24 -0
  20. package/dist/apis/admin/create-delegation-token.js +54 -0
  21. package/dist/apis/admin/create-partitions.d.ts +24 -0
  22. package/dist/apis/admin/create-partitions.js +54 -0
  23. package/dist/apis/admin/create-topics.d.ts +42 -0
  24. package/dist/apis/admin/create-topics.js +86 -0
  25. package/dist/apis/admin/delete-acls.d.ts +36 -0
  26. package/dist/apis/admin/delete-acls.js +82 -0
  27. package/dist/apis/admin/delete-groups.d.ts +14 -0
  28. package/dist/apis/admin/delete-groups.js +40 -0
  29. package/dist/apis/admin/delete-records.d.ts +27 -0
  30. package/dist/apis/admin/delete-records.js +59 -0
  31. package/dist/apis/admin/delete-topics.d.ts +21 -0
  32. package/dist/apis/admin/delete-topics.js +50 -0
  33. package/dist/apis/admin/describe-acls.d.ts +25 -0
  34. package/dist/apis/admin/describe-acls.js +66 -0
  35. package/dist/apis/admin/describe-client-quotas.d.ts +30 -0
  36. package/dist/apis/admin/describe-client-quotas.js +57 -0
  37. package/dist/apis/admin/describe-cluster.d.ts +23 -0
  38. package/dist/apis/admin/describe-cluster.js +50 -0
  39. package/dist/apis/admin/describe-configs.d.ts +38 -0
  40. package/dist/apis/admin/describe-configs.js +85 -0
  41. package/dist/apis/admin/describe-delegation-token.d.ts +31 -0
  42. package/dist/apis/admin/describe-delegation-token.js +62 -0
  43. package/dist/apis/admin/describe-groups.d.ts +28 -0
  44. package/dist/apis/admin/describe-groups.js +67 -0
  45. package/dist/apis/admin/describe-log-dirs.d.ts +32 -0
  46. package/dist/apis/admin/describe-log-dirs.js +75 -0
  47. package/dist/apis/admin/describe-producers.d.ts +33 -0
  48. package/dist/apis/admin/describe-producers.js +70 -0
  49. package/dist/apis/admin/describe-quorum.d.ts +50 -0
  50. package/dist/apis/admin/describe-quorum.js +116 -0
  51. package/dist/apis/admin/describe-topic-partitions.d.ts +42 -0
  52. package/dist/apis/admin/describe-topic-partitions.js +94 -0
  53. package/dist/apis/admin/describe-transactions.d.ts +24 -0
  54. package/dist/apis/admin/describe-transactions.js +59 -0
  55. package/dist/apis/admin/describe-user-scram-credentials.d.ts +26 -0
  56. package/dist/apis/admin/describe-user-scram-credentials.js +62 -0
  57. package/dist/apis/admin/envelope.d.ts +10 -0
  58. package/dist/apis/admin/envelope.js +32 -0
  59. package/dist/apis/admin/expire-delegation-token.d.ts +11 -0
  60. package/dist/apis/admin/expire-delegation-token.js +29 -0
  61. package/dist/apis/admin/incremental-alter-configs.d.ts +27 -0
  62. package/dist/apis/admin/incremental-alter-configs.js +58 -0
  63. package/dist/apis/admin/index.d.ts +37 -0
  64. package/dist/apis/admin/index.js +37 -0
  65. package/dist/apis/admin/list-groups.d.ts +18 -0
  66. package/dist/apis/admin/list-groups.js +43 -0
  67. package/dist/apis/admin/list-partition-reassignments.d.ts +27 -0
  68. package/dist/apis/admin/list-partition-reassignments.js +56 -0
  69. package/dist/apis/admin/list-transactions.d.ts +18 -0
  70. package/dist/apis/admin/list-transactions.js +45 -0
  71. package/dist/apis/admin/offset-delete.d.ts +26 -0
  72. package/dist/apis/admin/offset-delete.js +59 -0
  73. package/dist/apis/admin/renew-delegation-token.d.ts +11 -0
  74. package/dist/apis/admin/renew-delegation-token.js +29 -0
  75. package/dist/apis/admin/unregister-broker.d.ts +12 -0
  76. package/dist/apis/admin/unregister-broker.js +28 -0
  77. package/dist/apis/admin/update-features.d.ts +23 -0
  78. package/dist/apis/admin/update-features.js +60 -0
  79. package/dist/apis/consumer/consumer-group-heartbeat.d.ts +27 -0
  80. package/dist/apis/consumer/consumer-group-heartbeat.js +70 -0
  81. package/dist/apis/consumer/fetch.d.ts +46 -0
  82. package/dist/apis/consumer/fetch.js +121 -0
  83. package/dist/apis/consumer/heartbeat.d.ts +11 -0
  84. package/dist/apis/consumer/heartbeat.js +34 -0
  85. package/dist/apis/consumer/index.d.ts +9 -0
  86. package/dist/apis/consumer/index.js +9 -0
  87. package/dist/apis/consumer/join-group.d.ts +27 -0
  88. package/dist/apis/consumer/join-group.js +71 -0
  89. package/dist/apis/consumer/leave-group.d.ts +22 -0
  90. package/dist/apis/consumer/leave-group.js +57 -0
  91. package/dist/apis/consumer/list-offsets.d.ts +30 -0
  92. package/dist/apis/consumer/list-offsets.js +68 -0
  93. package/dist/apis/consumer/offset-commit.d.ts +29 -0
  94. package/dist/apis/consumer/offset-commit.js +68 -0
  95. package/dist/apis/consumer/offset-fetch.d.ts +37 -0
  96. package/dist/apis/consumer/offset-fetch.js +81 -0
  97. package/dist/apis/consumer/sync-group.d.ts +18 -0
  98. package/dist/apis/consumer/sync-group.js +49 -0
  99. package/dist/apis/definitions.d.ts +16 -0
  100. package/dist/apis/definitions.js +12 -0
  101. package/dist/apis/enumerations.d.ts +114 -0
  102. package/dist/apis/enumerations.js +78 -0
  103. package/dist/apis/index.d.ts +8 -0
  104. package/dist/apis/index.js +10 -0
  105. package/dist/apis/metadata/api-versions.d.ts +17 -0
  106. package/dist/apis/metadata/api-versions.js +41 -0
  107. package/dist/apis/metadata/find-coordinator.d.ts +19 -0
  108. package/dist/apis/metadata/find-coordinator.js +50 -0
  109. package/dist/apis/metadata/index.d.ts +3 -0
  110. package/dist/apis/metadata/index.js +3 -0
  111. package/dist/apis/metadata/metadata.d.ts +37 -0
  112. package/dist/apis/metadata/metadata.js +92 -0
  113. package/dist/apis/producer/add-offsets-to-txn.d.ts +10 -0
  114. package/dist/apis/producer/add-offsets-to-txn.js +34 -0
  115. package/dist/apis/producer/add-partitions-to-txn.d.ts +34 -0
  116. package/dist/apis/producer/add-partitions-to-txn.js +79 -0
  117. package/dist/apis/producer/end-txn.d.ts +10 -0
  118. package/dist/apis/producer/end-txn.js +34 -0
  119. package/dist/apis/producer/index.d.ts +6 -0
  120. package/dist/apis/producer/index.js +6 -0
  121. package/dist/apis/producer/init-producer-id.d.ts +21 -0
  122. package/dist/apis/producer/init-producer-id.js +38 -0
  123. package/dist/apis/producer/produce.d.ts +29 -0
  124. package/dist/apis/producer/produce.js +104 -0
  125. package/dist/apis/producer/txn-offset-commit.d.ts +29 -0
  126. package/dist/apis/producer/txn-offset-commit.js +77 -0
  127. package/dist/apis/security/index.d.ts +2 -0
  128. package/dist/apis/security/index.js +2 -0
  129. package/dist/apis/security/sasl-authenticate.d.ts +15 -0
  130. package/dist/apis/security/sasl-authenticate.js +30 -0
  131. package/dist/apis/security/sasl-handshake.d.ts +10 -0
  132. package/dist/apis/security/sasl-handshake.js +28 -0
  133. package/dist/apis/telemetry/get-telemetry-subscriptions.d.ts +18 -0
  134. package/dist/apis/telemetry/get-telemetry-subscriptions.js +46 -0
  135. package/dist/apis/telemetry/index.d.ts +3 -0
  136. package/dist/apis/telemetry/index.js +3 -0
  137. package/dist/apis/telemetry/list-client-metrics-resources.d.ts +14 -0
  138. package/dist/apis/telemetry/list-client-metrics-resources.js +32 -0
  139. package/dist/apis/telemetry/push-telemetry.d.ts +10 -0
  140. package/dist/apis/telemetry/push-telemetry.js +36 -0
  141. package/dist/clients/admin/admin.d.ts +18 -0
  142. package/dist/clients/admin/admin.js +322 -0
  143. package/dist/clients/admin/index.d.ts +3 -0
  144. package/dist/clients/admin/index.js +3 -0
  145. package/dist/clients/admin/options.d.ts +135 -0
  146. package/dist/clients/admin/options.js +81 -0
  147. package/dist/clients/admin/types.d.ts +56 -0
  148. package/dist/clients/admin/types.js +1 -0
  149. package/dist/clients/base/base.d.ts +48 -0
  150. package/dist/clients/base/base.js +242 -0
  151. package/dist/clients/base/index.d.ts +3 -0
  152. package/dist/clients/base/index.js +3 -0
  153. package/dist/clients/base/options.d.ts +115 -0
  154. package/dist/clients/base/options.js +59 -0
  155. package/dist/clients/base/types.d.ts +38 -0
  156. package/dist/clients/base/types.js +1 -0
  157. package/dist/clients/callbacks.d.ts +8 -0
  158. package/dist/clients/callbacks.js +42 -0
  159. package/dist/clients/consumer/consumer.d.ts +33 -0
  160. package/dist/clients/consumer/consumer.js +767 -0
  161. package/dist/clients/consumer/index.d.ts +5 -0
  162. package/dist/clients/consumer/index.js +5 -0
  163. package/dist/clients/consumer/messages-stream.d.ts +56 -0
  164. package/dist/clients/consumer/messages-stream.js +404 -0
  165. package/dist/clients/consumer/options.d.ts +521 -0
  166. package/dist/clients/consumer/options.js +177 -0
  167. package/dist/clients/consumer/topics-map.d.ts +8 -0
  168. package/dist/clients/consumer/topics-map.js +48 -0
  169. package/dist/clients/consumer/types.d.ts +74 -0
  170. package/dist/clients/consumer/types.js +11 -0
  171. package/dist/clients/index.d.ts +6 -0
  172. package/dist/clients/index.js +6 -0
  173. package/dist/clients/producer/index.d.ts +3 -0
  174. package/dist/clients/producer/index.js +3 -0
  175. package/dist/clients/producer/options.d.ts +122 -0
  176. package/dist/clients/producer/options.js +47 -0
  177. package/dist/clients/producer/producer.d.ts +13 -0
  178. package/dist/clients/producer/producer.js +272 -0
  179. package/dist/clients/producer/types.d.ts +29 -0
  180. package/dist/clients/producer/types.js +1 -0
  181. package/dist/clients/serde.d.ts +40 -0
  182. package/dist/clients/serde.js +49 -0
  183. package/dist/errors.d.ts +67 -0
  184. package/dist/errors.js +160 -0
  185. package/dist/index.d.ts +6 -0
  186. package/dist/index.js +10 -0
  187. package/dist/network/connection-pool.d.ts +11 -0
  188. package/dist/network/connection-pool.js +101 -0
  189. package/dist/network/connection.d.ts +49 -0
  190. package/dist/network/connection.js +319 -0
  191. package/dist/network/index.d.ts +2 -0
  192. package/dist/network/index.js +2 -0
  193. package/dist/protocol/apis.d.ts +2 -0
  194. package/dist/protocol/apis.js +191 -0
  195. package/dist/protocol/compression.d.ts +70 -0
  196. package/dist/protocol/compression.js +114 -0
  197. package/dist/protocol/crc32c.d.ts +2 -0
  198. package/dist/protocol/crc32c.js +83 -0
  199. package/dist/protocol/definitions.d.ts +12 -0
  200. package/dist/protocol/definitions.js +11 -0
  201. package/dist/protocol/dynamic-buffer.d.ts +65 -0
  202. package/dist/protocol/dynamic-buffer.js +557 -0
  203. package/dist/protocol/errors.d.ts +8 -0
  204. package/dist/protocol/errors.js +908 -0
  205. package/dist/protocol/index.d.ts +14 -0
  206. package/dist/protocol/index.js +14 -0
  207. package/dist/protocol/murmur2.d.ts +1 -0
  208. package/dist/protocol/murmur2.js +55 -0
  209. package/dist/protocol/reader.d.ts +58 -0
  210. package/dist/protocol/reader.js +296 -0
  211. package/dist/protocol/records.d.ts +110 -0
  212. package/dist/protocol/records.js +149 -0
  213. package/dist/protocol/sasl/plain.d.ts +3 -0
  214. package/dist/protocol/sasl/plain.js +3 -0
  215. package/dist/protocol/sasl/scram-sha.d.ts +28 -0
  216. package/dist/protocol/sasl/scram-sha.js +104 -0
  217. package/dist/protocol/varint.d.ts +12 -0
  218. package/dist/protocol/varint.js +36 -0
  219. package/dist/protocol/writer.d.ts +48 -0
  220. package/dist/protocol/writer.js +223 -0
  221. package/dist/utils.d.ts +20 -0
  222. package/dist/utils.js +127 -0
  223. package/package.json +75 -0
@@ -0,0 +1,5 @@
1
+ export * from './consumer.ts';
2
+ export * from './messages-stream.ts';
3
+ export * from './options.ts';
4
+ export * from './topics-map.ts';
5
+ export * from './types.ts';
@@ -0,0 +1,5 @@
1
+ export * from "./consumer.js";
2
+ export * from "./messages-stream.js";
3
+ export * from "./options.js";
4
+ export * from "./topics-map.js";
5
+ export * from "./types.js";
@@ -0,0 +1,56 @@
1
+ import { Readable } from 'node:stream';
2
+ import { type Message } from '../../protocol/records.ts';
3
+ import { kInspect } from '../base/base.ts';
4
+ import { type CallbackWithPromise } from '../callbacks.ts';
5
+ import { type Consumer } from './consumer.ts';
6
+ import { type CommitOptionsPartition, type ConsumeOptions } from './types.ts';
7
+ export declare function noopDeserializer(data?: Buffer): Buffer | undefined;
8
+ export declare class MessagesStream<Key, Value, HeaderKey, HeaderValue> extends Readable {
9
+ #private;
10
+ constructor(consumer: Consumer<Key, Value, HeaderKey, HeaderValue>, options: ConsumeOptions<Key, Value, HeaderKey, HeaderValue>);
11
+ close(callback?: CallbackWithPromise<void>): void;
12
+ close(): Promise<void>;
13
+ addListener(event: 'autocommit', listener: (err: Error, offsets: CommitOptionsPartition[]) => void): this;
14
+ addListener(event: 'data', listener: (message: Message<Key, Value, HeaderKey, HeaderValue>) => void): this;
15
+ addListener(event: 'close', listener: () => void): this;
16
+ addListener(event: 'end', listener: () => void): this;
17
+ addListener(event: 'error', listener: (err: Error) => void): this;
18
+ addListener(event: 'pause', listener: () => void): this;
19
+ addListener(event: 'readable', listener: () => void): this;
20
+ addListener(event: 'resume', listener: () => void): this;
21
+ on(event: 'autocommit', listener: (err: Error, offsets: CommitOptionsPartition[]) => void): this;
22
+ on(event: 'data', listener: (message: Message<Key, Value, HeaderKey, HeaderValue>) => void): this;
23
+ on(event: 'close', listener: () => void): this;
24
+ on(event: 'end', listener: () => void): this;
25
+ on(event: 'error', listener: (err: Error) => void): this;
26
+ on(event: 'pause', listener: () => void): this;
27
+ on(event: 'readable', listener: () => void): this;
28
+ on(event: 'resume', listener: () => void): this;
29
+ once(event: 'autocommit', listener: (err: Error, offsets: CommitOptionsPartition[]) => void): this;
30
+ once(event: 'data', listener: (message: Message<Key, Value, HeaderKey, HeaderValue>) => void): this;
31
+ once(event: 'close', listener: () => void): this;
32
+ once(event: 'end', listener: () => void): this;
33
+ once(event: 'error', listener: (err: Error) => void): this;
34
+ once(event: 'pause', listener: () => void): this;
35
+ once(event: 'readable', listener: () => void): this;
36
+ once(event: 'resume', listener: () => void): this;
37
+ prependListener(event: 'data', listener: (message: Message<Key, Value, HeaderKey, HeaderValue>) => void): this;
38
+ prependListener(event: 'close', listener: () => void): this;
39
+ prependListener(event: 'end', listener: () => void): this;
40
+ prependListener(event: 'error', listener: (err: Error) => void): this;
41
+ prependListener(event: 'pause', listener: () => void): this;
42
+ prependListener(event: 'readable', listener: () => void): this;
43
+ prependListener(event: 'resume', listener: () => void): this;
44
+ prependOnceListener(event: 'data', listener: (message: Message<Key, Value, HeaderKey, HeaderValue>) => void): this;
45
+ prependOnceListener(event: 'close', listener: () => void): this;
46
+ prependOnceListener(event: 'end', listener: () => void): this;
47
+ prependOnceListener(event: 'error', listener: (err: Error) => void): this;
48
+ prependOnceListener(event: 'pause', listener: () => void): this;
49
+ prependOnceListener(event: 'readable', listener: () => void): this;
50
+ prependOnceListener(event: 'resume', listener: () => void): this;
51
+ [Symbol.asyncIterator](): NodeJS.AsyncIterator<Message<Key, Value, HeaderKey, HeaderValue>>;
52
+ _construct(callback: (error?: Error) => void): void;
53
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
54
+ _read(): void;
55
+ [kInspect](...args: unknown[]): void;
56
+ }
@@ -0,0 +1,404 @@
1
+ import { Readable } from 'node:stream';
2
+ import { ListOffsetTimestamps } from "../../apis/enumerations.js";
3
+ import { UserError } from "../../errors.js";
4
+ import { kInspect } from "../base/base.js";
5
+ import { createPromisifiedCallback, kCallbackPromise, noopCallback } from "../callbacks.js";
6
+ import { MessagesStreamFallbackModes, MessagesStreamModes } from "./types.js";
7
+ // Don't move this function as being in the same file will enable V8 to remove.
8
+ // For futher info, ask Matteo.
9
+ /* c8 ignore next 3 */
10
+ export function noopDeserializer(data) {
11
+ return data;
12
+ }
13
+ export class MessagesStream extends Readable {
14
+ #consumer;
15
+ #mode;
16
+ #fallbackMode;
17
+ #options;
18
+ #topics;
19
+ #offsetsToFetch;
20
+ #offsetsToCommit;
21
+ #inflightNodes;
22
+ #keyDeserializer;
23
+ #valueDeserializer;
24
+ #headerKeyDeserializer;
25
+ #headerValueDeserializer;
26
+ #autocommitEnabled;
27
+ #autocommitInterval;
28
+ #autocommitInflight;
29
+ #shouldClose;
30
+ #closeCallbacks;
31
+ constructor(consumer, options) {
32
+ const { autocommit, mode, fallbackMode, offsets, deserializers, ..._options } = options;
33
+ if (offsets && mode !== MessagesStreamModes.MANUAL) {
34
+ throw new UserError('Cannot specify offsets when the stream mode is not MANUAL.');
35
+ }
36
+ if (!offsets && mode === MessagesStreamModes.MANUAL) {
37
+ throw new UserError('Must specify offsets when the stream mode is MANUAL.');
38
+ }
39
+ /* c8 ignore next 14 */
40
+ super({ objectMode: true, highWaterMark: options.highWaterMark ?? 1024 });
41
+ this.#consumer = consumer;
42
+ this.#mode = mode ?? MessagesStreamModes.LATEST;
43
+ this.#fallbackMode = fallbackMode ?? MessagesStreamFallbackModes.LATEST;
44
+ this.#offsetsToCommit = new Map();
45
+ this.#topics = structuredClone(options.topics);
46
+ this.#inflightNodes = new Set();
47
+ this.#keyDeserializer = deserializers?.key ?? noopDeserializer;
48
+ this.#valueDeserializer = deserializers?.value ?? noopDeserializer;
49
+ this.#headerKeyDeserializer = deserializers?.headerKey ?? noopDeserializer;
50
+ this.#headerValueDeserializer = deserializers?.headerValue ?? noopDeserializer;
51
+ this.#autocommitEnabled = !!options.autocommit;
52
+ this.#autocommitInflight = false;
53
+ this.#shouldClose = false;
54
+ this.#closeCallbacks = [];
55
+ // Restore offsets
56
+ this.#offsetsToFetch = new Map();
57
+ if (offsets) {
58
+ for (const { topic, partition, offset } of offsets) {
59
+ this.#offsetsToFetch.set(`${topic}:${partition}`, offset);
60
+ }
61
+ }
62
+ // Clone the rest of the options so the user can never mutate them
63
+ this.#options = structuredClone(_options);
64
+ // Start the autocommit interval
65
+ /* c8 ignore next */
66
+ if (typeof autocommit === 'number' && autocommit > 0) {
67
+ this.#autocommitInterval = setInterval(this.#autocommit.bind(this), autocommit);
68
+ }
69
+ else {
70
+ this.#autocommitInterval = null;
71
+ }
72
+ // When the consumer joins a group, we need to fetch again as the assignments
73
+ // will have changed so we may have gone from last with no assignments to
74
+ // having some.
75
+ this.#consumer.on('consumer:group:join', () => {
76
+ this.#refreshOffsets((error) => {
77
+ /* c8 ignore next 4 */
78
+ if (error) {
79
+ this.destroy(error);
80
+ return;
81
+ }
82
+ this.#fetch();
83
+ });
84
+ });
85
+ }
86
+ close(callback) {
87
+ if (!callback) {
88
+ callback = createPromisifiedCallback();
89
+ }
90
+ if (this.closed || this.destroyed) {
91
+ callback(null);
92
+ return callback[kCallbackPromise];
93
+ }
94
+ this.#closeCallbacks.push(callback);
95
+ if (this.#shouldClose) {
96
+ this.#invokeCloseCallbacks(null);
97
+ return callback[kCallbackPromise];
98
+ }
99
+ this.#shouldClose = true;
100
+ this.push(null);
101
+ if (this.#autocommitInterval) {
102
+ clearInterval(this.#autocommitInterval);
103
+ }
104
+ if (this.readableFlowing === null || this.isPaused()) {
105
+ this.removeAllListeners('data');
106
+ this.removeAllListeners('readable');
107
+ this.resume();
108
+ }
109
+ /* c8 ignore next 3 */
110
+ this.once('error', (error) => {
111
+ callback(error);
112
+ });
113
+ this.once('close', () => {
114
+ // We have offsets that were enqueued to be committed. Perform the operation
115
+ if (this.#offsetsToCommit.size > 0) {
116
+ this.#autocommit();
117
+ }
118
+ // We have offsets that are being committed. These are awaited despite of the force parameters
119
+ if (this.#autocommitInflight) {
120
+ this.once('autocommit', error => {
121
+ this.#invokeCloseCallbacks(error);
122
+ });
123
+ return;
124
+ }
125
+ this.#invokeCloseCallbacks(null);
126
+ });
127
+ return callback[kCallbackPromise];
128
+ }
129
+ /* c8 ignore next 3 */
130
+ addListener(event, listener) {
131
+ return super.addListener(event, listener);
132
+ }
133
+ /* c8 ignore next 3 */
134
+ on(event, listener) {
135
+ return super.on(event, listener);
136
+ }
137
+ /* c8 ignore next 3 */
138
+ once(event, listener) {
139
+ return super.once(event, listener);
140
+ }
141
+ /* c8 ignore next 3 */
142
+ prependListener(event, listener) {
143
+ return super.prependListener(event, listener);
144
+ }
145
+ /* c8 ignore next 3 */
146
+ prependOnceListener(event, listener) {
147
+ return super.prependOnceListener(event, listener);
148
+ }
149
+ [Symbol.asyncIterator]() {
150
+ return super[Symbol.asyncIterator]();
151
+ }
152
+ _construct(callback) {
153
+ this.#refreshOffsets(callback);
154
+ }
155
+ _destroy(error, callback) {
156
+ if (this.#autocommitInterval) {
157
+ clearInterval(this.#autocommitInterval);
158
+ }
159
+ callback(error);
160
+ }
161
+ _read() {
162
+ this.#fetch();
163
+ }
164
+ #fetch() {
165
+ /* c8 ignore next 4 */
166
+ if (this.#shouldClose || this.closed || this.destroyed) {
167
+ this.push(null);
168
+ return;
169
+ }
170
+ this.#consumer.metadata({ topics: this.#consumer.topics.current }, (error, metadata) => {
171
+ if (error) {
172
+ // The stream has been closed, ignore any error
173
+ /* c8 ignore next 4 */
174
+ if (this.#shouldClose) {
175
+ this.push(null);
176
+ return;
177
+ }
178
+ this.destroy(error);
179
+ return;
180
+ }
181
+ /* c8 ignore next 4 */
182
+ if (this.#shouldClose || this.closed || this.destroyed) {
183
+ this.push(null);
184
+ return;
185
+ }
186
+ const requests = new Map();
187
+ const topicIds = new Map();
188
+ // Group topic-partitions by the destination broker
189
+ for (const topic of this.#topics) {
190
+ const assignment = this.#assignmentsForTopic(topic);
191
+ // This consumer has no assignment for the topic, continue
192
+ if (!assignment) {
193
+ continue;
194
+ }
195
+ const partitions = assignment.partitions;
196
+ for (const partition of partitions) {
197
+ const leader = metadata.topics.get(topic).partitions[partition].leader;
198
+ if (this.#inflightNodes.has(leader)) {
199
+ continue;
200
+ }
201
+ let leaderRequests = requests.get(leader);
202
+ if (!leaderRequests) {
203
+ leaderRequests = [];
204
+ requests.set(leader, leaderRequests);
205
+ }
206
+ const topicId = metadata.topics.get(topic).id;
207
+ topicIds.set(topicId, topic);
208
+ leaderRequests.push({
209
+ topicId,
210
+ partitions: [
211
+ {
212
+ partition,
213
+ /* c8 ignore next */
214
+ fetchOffset: this.#offsetsToFetch.get(`${topic}:${partition}`) ?? 0n,
215
+ partitionMaxBytes: this.#options.maxBytes,
216
+ currentLeaderEpoch: -1,
217
+ lastFetchedEpoch: -1
218
+ }
219
+ ]
220
+ });
221
+ }
222
+ }
223
+ for (const [leader, leaderRequests] of requests) {
224
+ this.#inflightNodes.add(leader);
225
+ this.#consumer.fetch({ ...this.#options, node: leader, topics: leaderRequests }, (error, response) => {
226
+ this.#inflightNodes.delete(leader);
227
+ if (error) {
228
+ // The stream has been closed, ignore the error
229
+ /* c8 ignore next 4 */
230
+ if (this.#shouldClose) {
231
+ this.push(null);
232
+ return;
233
+ }
234
+ this.destroy(error);
235
+ return;
236
+ }
237
+ if (this.#shouldClose || this.closed || this.destroyed) {
238
+ // When it's the last inflight, we finally close the stream.
239
+ // This is done to avoid the user exiting from consmuming metrics like for-await and still see the process up.
240
+ if (this.#inflightNodes.size === 0) {
241
+ this.push(null);
242
+ }
243
+ return;
244
+ }
245
+ this.#pushRecords(metadata, topicIds, response);
246
+ });
247
+ }
248
+ });
249
+ }
250
+ #pushRecords(metadata, topicIds, response) {
251
+ const autocommit = this.#autocommitEnabled;
252
+ let canPush = true;
253
+ const keyDeserializer = this.#keyDeserializer;
254
+ const valueDeserializer = this.#valueDeserializer;
255
+ const headerKeyDeserializer = this.#headerKeyDeserializer;
256
+ const headerValueDeserializer = this.#headerValueDeserializer;
257
+ // Parse results
258
+ for (const topicResponse of response.responses) {
259
+ const topic = topicIds.get(topicResponse.topicId);
260
+ for (const { records, partitionIndex: partition } of topicResponse.partitions) {
261
+ if (!records) {
262
+ continue;
263
+ }
264
+ const firstTimestamp = records.firstTimestamp;
265
+ const leaderEpoch = metadata.topics.get(topic).partitions[partition].leaderEpoch;
266
+ for (const record of records.records) {
267
+ const key = keyDeserializer(record.key);
268
+ const value = valueDeserializer(record.value);
269
+ const headers = new Map();
270
+ for (const [headerKey, headerValue] of record.headers) {
271
+ headers.set(headerKeyDeserializer(headerKey), headerValueDeserializer(headerValue));
272
+ }
273
+ canPush = this.push({
274
+ key,
275
+ value,
276
+ headers,
277
+ topic,
278
+ partition,
279
+ timestamp: firstTimestamp + record.timestampDelta,
280
+ offset: records.firstOffset + BigInt(record.offsetDelta),
281
+ commit: autocommit
282
+ ? noopCallback
283
+ : this.#commit.bind(this, topic, partition, records.firstOffset + BigInt(record.offsetDelta), leaderEpoch)
284
+ });
285
+ }
286
+ // Track the last read offset
287
+ const lastOffset = records.firstOffset + BigInt(records.lastOffsetDelta);
288
+ this.#offsetsToFetch.set(`${topic}:${partition}`, lastOffset + 1n);
289
+ // Autocommit if needed
290
+ if (autocommit) {
291
+ this.#offsetsToCommit.set(`${topic}:${partition}`, { topic, partition, offset: lastOffset, leaderEpoch });
292
+ }
293
+ }
294
+ }
295
+ if (this.#autocommitEnabled && !this.#autocommitInterval) {
296
+ this.#autocommit();
297
+ }
298
+ if (canPush && !(this.#shouldClose || this.closed || this.destroyed)) {
299
+ process.nextTick(() => {
300
+ this.#fetch();
301
+ });
302
+ }
303
+ }
304
+ // This could optimized to only schedule once per tick on a topic-partition and only commit the latest offset
305
+ #commit(topic, partition, offset, leaderEpoch, callback) {
306
+ if (!callback) {
307
+ callback = createPromisifiedCallback();
308
+ }
309
+ this.#consumer.commit({ offsets: [{ topic, partition, offset, leaderEpoch }] }, callback);
310
+ return callback[kCallbackPromise];
311
+ }
312
+ #autocommit() {
313
+ if (this.#offsetsToCommit.size === 0) {
314
+ return;
315
+ }
316
+ this.#autocommitInflight = true;
317
+ const offsets = Array.from(this.#offsetsToCommit.values());
318
+ this.#offsetsToCommit.clear();
319
+ this.#consumer.commit({ offsets }, error => {
320
+ this.#autocommitInflight = false;
321
+ if (error) {
322
+ this.emit('autocommit', error);
323
+ this.destroy(error);
324
+ return;
325
+ }
326
+ this.emit('autocommit', null, offsets);
327
+ });
328
+ }
329
+ #refreshOffsets(callback) {
330
+ // List topic offsets
331
+ this.#consumer.listOffsets({
332
+ topics: this.#topics,
333
+ timestamp: this.#mode === MessagesStreamModes.EARLIEST ||
334
+ (this.#mode !== MessagesStreamModes.LATEST && this.#fallbackMode === MessagesStreamFallbackModes.EARLIEST)
335
+ ? ListOffsetTimestamps.EARLIEST
336
+ : ListOffsetTimestamps.LATEST
337
+ }, (error, offsets) => {
338
+ if (error) {
339
+ callback(error);
340
+ return;
341
+ }
342
+ if (this.#mode !== MessagesStreamModes.COMMITTED) {
343
+ this.#assignOffsets(offsets, new Map(), callback);
344
+ return;
345
+ }
346
+ // Now restore group offsets
347
+ const topics = [];
348
+ for (const topic of this.#topics) {
349
+ const assignment = this.#assignmentsForTopic(topic);
350
+ if (!assignment) {
351
+ continue;
352
+ }
353
+ topics.push(assignment);
354
+ }
355
+ if (!topics.length) {
356
+ this.#assignOffsets(offsets, new Map(), callback);
357
+ return;
358
+ }
359
+ this.#consumer.listCommittedOffsets({ topics }, (error, commits) => {
360
+ if (error) {
361
+ callback(error);
362
+ return;
363
+ }
364
+ this.#assignOffsets(offsets, commits, callback);
365
+ });
366
+ });
367
+ }
368
+ #assignOffsets(offsets, commits, callback) {
369
+ for (const [topic, partitions] of offsets) {
370
+ for (let i = 0; i < partitions.length; i++) {
371
+ if (!this.#offsetsToFetch.has(`${topic}:${i}`)) {
372
+ this.#offsetsToFetch.set(`${topic}:${i}`, partitions[i]);
373
+ }
374
+ }
375
+ }
376
+ for (const [topic, partitions] of commits) {
377
+ for (let i = 0; i < partitions.length; i++) {
378
+ const offset = partitions[i];
379
+ if (offset >= 0n) {
380
+ this.#offsetsToFetch.set(`${topic}:${i}`, offset + 1n);
381
+ }
382
+ else if (this.#fallbackMode === MessagesStreamFallbackModes.FAIL) {
383
+ callback(new UserError(`Topic ${topic} has no committed offset on partition ${i} for group ${this.#consumer.groupId}.`, { topic, partition: i, groupId: this.#consumer.groupId }));
384
+ return;
385
+ }
386
+ }
387
+ }
388
+ callback(null);
389
+ }
390
+ #assignmentsForTopic(topic) {
391
+ return this.#consumer.assignments.find(assignment => assignment.topic === topic);
392
+ }
393
+ #invokeCloseCallbacks(error) {
394
+ for (const callback of this.#closeCallbacks) {
395
+ callback(error);
396
+ }
397
+ this.#closeCallbacks = [];
398
+ }
399
+ // This is a private API used to debug during development
400
+ /* c8 ignore next 3 */
401
+ [kInspect](...args) {
402
+ this.#consumer[kInspect](...args);
403
+ }
404
+ }