@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.
- package/LICENSE +201 -0
- package/README.md +270 -0
- package/dist/apis/admin/alter-client-quotas.d.ts +33 -0
- package/dist/apis/admin/alter-client-quotas.js +64 -0
- package/dist/apis/admin/alter-configs.d.ts +26 -0
- package/dist/apis/admin/alter-configs.js +57 -0
- package/dist/apis/admin/alter-partition-reassignments.d.ts +30 -0
- package/dist/apis/admin/alter-partition-reassignments.js +68 -0
- package/dist/apis/admin/alter-partition.d.ts +39 -0
- package/dist/apis/admin/alter-partition.js +87 -0
- package/dist/apis/admin/alter-replica-log-dirs.d.ts +26 -0
- package/dist/apis/admin/alter-replica-log-dirs.js +55 -0
- package/dist/apis/admin/alter-user-scram-credentials.d.ts +27 -0
- package/dist/apis/admin/alter-user-scram-credentials.js +60 -0
- package/dist/apis/admin/consumer-group-describe.d.ts +41 -0
- package/dist/apis/admin/consumer-group-describe.js +103 -0
- package/dist/apis/admin/create-acls.d.ts +24 -0
- package/dist/apis/admin/create-acls.js +55 -0
- package/dist/apis/admin/create-delegation-token.d.ts +24 -0
- package/dist/apis/admin/create-delegation-token.js +54 -0
- package/dist/apis/admin/create-partitions.d.ts +24 -0
- package/dist/apis/admin/create-partitions.js +54 -0
- package/dist/apis/admin/create-topics.d.ts +42 -0
- package/dist/apis/admin/create-topics.js +86 -0
- package/dist/apis/admin/delete-acls.d.ts +36 -0
- package/dist/apis/admin/delete-acls.js +82 -0
- package/dist/apis/admin/delete-groups.d.ts +14 -0
- package/dist/apis/admin/delete-groups.js +40 -0
- package/dist/apis/admin/delete-records.d.ts +27 -0
- package/dist/apis/admin/delete-records.js +59 -0
- package/dist/apis/admin/delete-topics.d.ts +21 -0
- package/dist/apis/admin/delete-topics.js +50 -0
- package/dist/apis/admin/describe-acls.d.ts +25 -0
- package/dist/apis/admin/describe-acls.js +66 -0
- package/dist/apis/admin/describe-client-quotas.d.ts +30 -0
- package/dist/apis/admin/describe-client-quotas.js +57 -0
- package/dist/apis/admin/describe-cluster.d.ts +23 -0
- package/dist/apis/admin/describe-cluster.js +50 -0
- package/dist/apis/admin/describe-configs.d.ts +38 -0
- package/dist/apis/admin/describe-configs.js +85 -0
- package/dist/apis/admin/describe-delegation-token.d.ts +31 -0
- package/dist/apis/admin/describe-delegation-token.js +62 -0
- package/dist/apis/admin/describe-groups.d.ts +28 -0
- package/dist/apis/admin/describe-groups.js +67 -0
- package/dist/apis/admin/describe-log-dirs.d.ts +32 -0
- package/dist/apis/admin/describe-log-dirs.js +75 -0
- package/dist/apis/admin/describe-producers.d.ts +33 -0
- package/dist/apis/admin/describe-producers.js +70 -0
- package/dist/apis/admin/describe-quorum.d.ts +50 -0
- package/dist/apis/admin/describe-quorum.js +116 -0
- package/dist/apis/admin/describe-topic-partitions.d.ts +42 -0
- package/dist/apis/admin/describe-topic-partitions.js +94 -0
- package/dist/apis/admin/describe-transactions.d.ts +24 -0
- package/dist/apis/admin/describe-transactions.js +59 -0
- package/dist/apis/admin/describe-user-scram-credentials.d.ts +26 -0
- package/dist/apis/admin/describe-user-scram-credentials.js +62 -0
- package/dist/apis/admin/envelope.d.ts +10 -0
- package/dist/apis/admin/envelope.js +32 -0
- package/dist/apis/admin/expire-delegation-token.d.ts +11 -0
- package/dist/apis/admin/expire-delegation-token.js +29 -0
- package/dist/apis/admin/incremental-alter-configs.d.ts +27 -0
- package/dist/apis/admin/incremental-alter-configs.js +58 -0
- package/dist/apis/admin/index.d.ts +37 -0
- package/dist/apis/admin/index.js +37 -0
- package/dist/apis/admin/list-groups.d.ts +18 -0
- package/dist/apis/admin/list-groups.js +43 -0
- package/dist/apis/admin/list-partition-reassignments.d.ts +27 -0
- package/dist/apis/admin/list-partition-reassignments.js +56 -0
- package/dist/apis/admin/list-transactions.d.ts +18 -0
- package/dist/apis/admin/list-transactions.js +45 -0
- package/dist/apis/admin/offset-delete.d.ts +26 -0
- package/dist/apis/admin/offset-delete.js +59 -0
- package/dist/apis/admin/renew-delegation-token.d.ts +11 -0
- package/dist/apis/admin/renew-delegation-token.js +29 -0
- package/dist/apis/admin/unregister-broker.d.ts +12 -0
- package/dist/apis/admin/unregister-broker.js +28 -0
- package/dist/apis/admin/update-features.d.ts +23 -0
- package/dist/apis/admin/update-features.js +60 -0
- package/dist/apis/consumer/consumer-group-heartbeat.d.ts +27 -0
- package/dist/apis/consumer/consumer-group-heartbeat.js +70 -0
- package/dist/apis/consumer/fetch.d.ts +46 -0
- package/dist/apis/consumer/fetch.js +121 -0
- package/dist/apis/consumer/heartbeat.d.ts +11 -0
- package/dist/apis/consumer/heartbeat.js +34 -0
- package/dist/apis/consumer/index.d.ts +9 -0
- package/dist/apis/consumer/index.js +9 -0
- package/dist/apis/consumer/join-group.d.ts +27 -0
- package/dist/apis/consumer/join-group.js +71 -0
- package/dist/apis/consumer/leave-group.d.ts +22 -0
- package/dist/apis/consumer/leave-group.js +57 -0
- package/dist/apis/consumer/list-offsets.d.ts +30 -0
- package/dist/apis/consumer/list-offsets.js +68 -0
- package/dist/apis/consumer/offset-commit.d.ts +29 -0
- package/dist/apis/consumer/offset-commit.js +68 -0
- package/dist/apis/consumer/offset-fetch.d.ts +37 -0
- package/dist/apis/consumer/offset-fetch.js +81 -0
- package/dist/apis/consumer/sync-group.d.ts +18 -0
- package/dist/apis/consumer/sync-group.js +49 -0
- package/dist/apis/definitions.d.ts +16 -0
- package/dist/apis/definitions.js +12 -0
- package/dist/apis/enumerations.d.ts +114 -0
- package/dist/apis/enumerations.js +78 -0
- package/dist/apis/index.d.ts +8 -0
- package/dist/apis/index.js +10 -0
- package/dist/apis/metadata/api-versions.d.ts +17 -0
- package/dist/apis/metadata/api-versions.js +41 -0
- package/dist/apis/metadata/find-coordinator.d.ts +19 -0
- package/dist/apis/metadata/find-coordinator.js +50 -0
- package/dist/apis/metadata/index.d.ts +3 -0
- package/dist/apis/metadata/index.js +3 -0
- package/dist/apis/metadata/metadata.d.ts +37 -0
- package/dist/apis/metadata/metadata.js +92 -0
- package/dist/apis/producer/add-offsets-to-txn.d.ts +10 -0
- package/dist/apis/producer/add-offsets-to-txn.js +34 -0
- package/dist/apis/producer/add-partitions-to-txn.d.ts +34 -0
- package/dist/apis/producer/add-partitions-to-txn.js +79 -0
- package/dist/apis/producer/end-txn.d.ts +10 -0
- package/dist/apis/producer/end-txn.js +34 -0
- package/dist/apis/producer/index.d.ts +6 -0
- package/dist/apis/producer/index.js +6 -0
- package/dist/apis/producer/init-producer-id.d.ts +21 -0
- package/dist/apis/producer/init-producer-id.js +38 -0
- package/dist/apis/producer/produce.d.ts +29 -0
- package/dist/apis/producer/produce.js +104 -0
- package/dist/apis/producer/txn-offset-commit.d.ts +29 -0
- package/dist/apis/producer/txn-offset-commit.js +77 -0
- package/dist/apis/security/index.d.ts +2 -0
- package/dist/apis/security/index.js +2 -0
- package/dist/apis/security/sasl-authenticate.d.ts +15 -0
- package/dist/apis/security/sasl-authenticate.js +30 -0
- package/dist/apis/security/sasl-handshake.d.ts +10 -0
- package/dist/apis/security/sasl-handshake.js +28 -0
- package/dist/apis/telemetry/get-telemetry-subscriptions.d.ts +18 -0
- package/dist/apis/telemetry/get-telemetry-subscriptions.js +46 -0
- package/dist/apis/telemetry/index.d.ts +3 -0
- package/dist/apis/telemetry/index.js +3 -0
- package/dist/apis/telemetry/list-client-metrics-resources.d.ts +14 -0
- package/dist/apis/telemetry/list-client-metrics-resources.js +32 -0
- package/dist/apis/telemetry/push-telemetry.d.ts +10 -0
- package/dist/apis/telemetry/push-telemetry.js +36 -0
- package/dist/clients/admin/admin.d.ts +18 -0
- package/dist/clients/admin/admin.js +322 -0
- package/dist/clients/admin/index.d.ts +3 -0
- package/dist/clients/admin/index.js +3 -0
- package/dist/clients/admin/options.d.ts +135 -0
- package/dist/clients/admin/options.js +81 -0
- package/dist/clients/admin/types.d.ts +56 -0
- package/dist/clients/admin/types.js +1 -0
- package/dist/clients/base/base.d.ts +48 -0
- package/dist/clients/base/base.js +242 -0
- package/dist/clients/base/index.d.ts +3 -0
- package/dist/clients/base/index.js +3 -0
- package/dist/clients/base/options.d.ts +115 -0
- package/dist/clients/base/options.js +59 -0
- package/dist/clients/base/types.d.ts +38 -0
- package/dist/clients/base/types.js +1 -0
- package/dist/clients/callbacks.d.ts +8 -0
- package/dist/clients/callbacks.js +42 -0
- package/dist/clients/consumer/consumer.d.ts +33 -0
- package/dist/clients/consumer/consumer.js +767 -0
- package/dist/clients/consumer/index.d.ts +5 -0
- package/dist/clients/consumer/index.js +5 -0
- package/dist/clients/consumer/messages-stream.d.ts +56 -0
- package/dist/clients/consumer/messages-stream.js +404 -0
- package/dist/clients/consumer/options.d.ts +521 -0
- package/dist/clients/consumer/options.js +177 -0
- package/dist/clients/consumer/topics-map.d.ts +8 -0
- package/dist/clients/consumer/topics-map.js +48 -0
- package/dist/clients/consumer/types.d.ts +74 -0
- package/dist/clients/consumer/types.js +11 -0
- package/dist/clients/index.d.ts +6 -0
- package/dist/clients/index.js +6 -0
- package/dist/clients/producer/index.d.ts +3 -0
- package/dist/clients/producer/index.js +3 -0
- package/dist/clients/producer/options.d.ts +122 -0
- package/dist/clients/producer/options.js +47 -0
- package/dist/clients/producer/producer.d.ts +13 -0
- package/dist/clients/producer/producer.js +272 -0
- package/dist/clients/producer/types.d.ts +29 -0
- package/dist/clients/producer/types.js +1 -0
- package/dist/clients/serde.d.ts +40 -0
- package/dist/clients/serde.js +49 -0
- package/dist/errors.d.ts +67 -0
- package/dist/errors.js +160 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +10 -0
- package/dist/network/connection-pool.d.ts +11 -0
- package/dist/network/connection-pool.js +101 -0
- package/dist/network/connection.d.ts +49 -0
- package/dist/network/connection.js +319 -0
- package/dist/network/index.d.ts +2 -0
- package/dist/network/index.js +2 -0
- package/dist/protocol/apis.d.ts +2 -0
- package/dist/protocol/apis.js +191 -0
- package/dist/protocol/compression.d.ts +70 -0
- package/dist/protocol/compression.js +114 -0
- package/dist/protocol/crc32c.d.ts +2 -0
- package/dist/protocol/crc32c.js +83 -0
- package/dist/protocol/definitions.d.ts +12 -0
- package/dist/protocol/definitions.js +11 -0
- package/dist/protocol/dynamic-buffer.d.ts +65 -0
- package/dist/protocol/dynamic-buffer.js +557 -0
- package/dist/protocol/errors.d.ts +8 -0
- package/dist/protocol/errors.js +908 -0
- package/dist/protocol/index.d.ts +14 -0
- package/dist/protocol/index.js +14 -0
- package/dist/protocol/murmur2.d.ts +1 -0
- package/dist/protocol/murmur2.js +55 -0
- package/dist/protocol/reader.d.ts +58 -0
- package/dist/protocol/reader.js +296 -0
- package/dist/protocol/records.d.ts +110 -0
- package/dist/protocol/records.js +149 -0
- package/dist/protocol/sasl/plain.d.ts +3 -0
- package/dist/protocol/sasl/plain.js +3 -0
- package/dist/protocol/sasl/scram-sha.d.ts +28 -0
- package/dist/protocol/sasl/scram-sha.js +104 -0
- package/dist/protocol/varint.d.ts +12 -0
- package/dist/protocol/varint.js +36 -0
- package/dist/protocol/writer.d.ts +48 -0
- package/dist/protocol/writer.js +223 -0
- package/dist/utils.d.ts +20 -0
- package/dist/utils.js +127 -0
- package/package.json +75 -0
|
@@ -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
|
+
}
|