kafka-ts 0.0.6-beta.5 → 0.0.6-beta.7

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/dist/broker.d.ts CHANGED
@@ -21,7 +21,7 @@ export declare class Broker {
21
21
  sendRequest: SendRequest;
22
22
  constructor(options: BrokerOptions);
23
23
  connect(): Promise<this>;
24
- ensureConnected: () => Promise<this>;
24
+ ensureConnected(): Promise<void>;
25
25
  disconnect(): Promise<void>;
26
26
  private validateApiVersions;
27
27
  private saslHandshake;
package/dist/broker.js CHANGED
@@ -4,7 +4,6 @@ exports.Broker = void 0;
4
4
  const api_1 = require("./api");
5
5
  const connection_1 = require("./connection");
6
6
  const error_1 = require("./utils/error");
7
- const memo_1 = require("./utils/memo");
8
7
  class Broker {
9
8
  options;
10
9
  connection;
@@ -25,7 +24,11 @@ class Broker {
25
24
  await this.saslAuthenticate();
26
25
  return this;
27
26
  }
28
- ensureConnected = (0, memo_1.memo)(() => this.connect());
27
+ async ensureConnected() {
28
+ if (!this.connection.isConnected()) {
29
+ await this.connect();
30
+ }
31
+ }
29
32
  async disconnect() {
30
33
  await this.connection.disconnect();
31
34
  }
package/dist/cluster.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare class Cluster {
17
17
  private brokerMetadata;
18
18
  constructor(options: ClusterOptions);
19
19
  connect(): Promise<void>;
20
+ ensureConnected(): Promise<void>;
20
21
  disconnect(): Promise<void>;
21
22
  setSeedBroker: (nodeId: number) => Promise<void>;
22
23
  sendRequest: SendRequest;
package/dist/cluster.js CHANGED
@@ -7,7 +7,7 @@ const error_1 = require("./utils/error");
7
7
  const logger_1 = require("./utils/logger");
8
8
  class Cluster {
9
9
  options;
10
- seedBroker = new broker_1.Broker({ clientId: null, sasl: null, ssl: null, options: { port: 9092 } });
10
+ seedBroker;
11
11
  brokerById = {};
12
12
  brokerMetadata = {};
13
13
  constructor(options) {
@@ -23,11 +23,20 @@ class Cluster {
23
23
  });
24
24
  this.brokerMetadata = Object.fromEntries(metadata.brokers.map((options) => [options.nodeId, options]));
25
25
  }
26
+ async ensureConnected() {
27
+ if (!this.seedBroker) {
28
+ return this.connect();
29
+ }
30
+ await Promise.all([this.seedBroker, ...Object.values(this.brokerById)].map((x) => x.ensureConnected()));
31
+ }
26
32
  async disconnect() {
27
- await Promise.all([this.seedBroker.disconnect(), ...Object.values(this.brokerById).map((x) => x.disconnect())]);
33
+ await Promise.all([
34
+ this.seedBroker?.disconnect(),
35
+ ...Object.values(this.brokerById).map((x) => x.disconnect()),
36
+ ]);
28
37
  }
29
38
  setSeedBroker = async (nodeId) => {
30
- await this.seedBroker.disconnect();
39
+ await this.seedBroker?.disconnect();
31
40
  this.seedBroker = await this.acquireBroker(nodeId);
32
41
  };
33
42
  sendRequest = (...args) => this.seedBroker.sendRequest(...args);
@@ -15,6 +15,7 @@ export declare class Connection {
15
15
  private lastCorrelationId;
16
16
  private chunks;
17
17
  constructor(options: ConnectionOptions);
18
+ isConnected(): boolean;
18
19
  connect(): Promise<void>;
19
20
  disconnect(): Promise<void>;
20
21
  sendRequest<Request, Response>(api: Api<Request, Response>, body: Request): Promise<Response>;
@@ -55,6 +55,9 @@ class Connection {
55
55
  constructor(options) {
56
56
  this.options = options;
57
57
  }
58
+ isConnected() {
59
+ return !this.socket.pending && !this.socket.destroyed;
60
+ }
58
61
  async connect() {
59
62
  this.queue = {};
60
63
  this.chunks = [];
@@ -16,7 +16,6 @@ const partitioner_1 = require("../distributors/partitioner");
16
16
  const metadata_1 = require("../metadata");
17
17
  const delay_1 = require("../utils/delay");
18
18
  const error_1 = require("../utils/error");
19
- const memo_1 = require("../utils/memo");
20
19
  const tracer_1 = require("../utils/tracer");
21
20
  const trace = (0, tracer_1.createTracer)('Producer');
22
21
  class Producer {
@@ -105,10 +104,12 @@ class Producer {
105
104
  async close() {
106
105
  await this.cluster.disconnect();
107
106
  }
108
- ensureConnected = (0, memo_1.memo)(async () => {
109
- await this.cluster.connect();
110
- await this.initProducerId();
111
- });
107
+ async ensureConnected() {
108
+ await this.cluster.ensureConnected();
109
+ if (!this.producerId) {
110
+ await this.initProducerId();
111
+ }
112
+ }
112
113
  async initProducerId() {
113
114
  try {
114
115
  const result = await this.cluster.sendRequest(api_1.API.INIT_PRODUCER_ID, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kafka-ts",
3
- "version": "0.0.6-beta.5",
3
+ "version": "0.0.6-beta.7",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",
@@ -1 +0,0 @@
1
- export declare const memo: <T extends (...args: any[]) => any>(fn: T) => (...args: Parameters<T>) => ReturnType<T>;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.memo = void 0;
4
- const memo = (fn) => {
5
- const cache = {};
6
- return (...args) => {
7
- const key = JSON.stringify(args);
8
- if (cache[key]) {
9
- return cache[key];
10
- }
11
- const result = fn(...args);
12
- cache[key] = result;
13
- return result;
14
- };
15
- };
16
- exports.memo = memo;