kafka-ts 1.3.1-beta.1 → 1.3.1-beta.2

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 (102) hide show
  1. package/README.md +7 -7
  2. package/dist/api/alter-configs.d.ts +26 -0
  3. package/dist/api/alter-configs.js +33 -0
  4. package/dist/api/api-versions.d.ts +5 -2
  5. package/dist/api/api-versions.js +13 -0
  6. package/dist/api/create-topics.d.ts +14 -12
  7. package/dist/api/create-topics.js +104 -12
  8. package/dist/api/delete-topics.d.ts +10 -8
  9. package/dist/api/delete-topics.js +61 -7
  10. package/dist/api/fetch.d.ts +15 -12
  11. package/dist/api/fetch.js +131 -13
  12. package/dist/api/find-coordinator.d.ts +9 -7
  13. package/dist/api/find-coordinator.js +63 -5
  14. package/dist/api/heartbeat.d.ts +7 -5
  15. package/dist/api/heartbeat.js +42 -4
  16. package/dist/api/index.d.ts +47 -118
  17. package/dist/api/init-producer-id.d.ts +7 -5
  18. package/dist/api/init-producer-id.js +53 -9
  19. package/dist/api/join-group.d.ts +9 -7
  20. package/dist/api/join-group.js +95 -6
  21. package/dist/api/leave-group.d.ts +8 -6
  22. package/dist/api/leave-group.js +49 -6
  23. package/dist/api/list-offsets.d.ts +9 -7
  24. package/dist/api/list-offsets.js +85 -8
  25. package/dist/api/metadata.d.ts +10 -9
  26. package/dist/api/metadata.js +109 -8
  27. package/dist/api/offset-commit.d.ts +10 -8
  28. package/dist/api/offset-commit.js +88 -8
  29. package/dist/api/offset-fetch.d.ts +11 -9
  30. package/dist/api/offset-fetch.js +94 -9
  31. package/dist/api/produce.d.ts +8 -10
  32. package/dist/api/produce.js +132 -38
  33. package/dist/api/sasl-authenticate.d.ts +8 -6
  34. package/dist/api/sasl-authenticate.js +43 -3
  35. package/dist/api/sasl-handshake.d.ts +7 -4
  36. package/dist/api/sasl-handshake.js +10 -0
  37. package/dist/api/sync-group.d.ts +7 -5
  38. package/dist/api/sync-group.js +62 -5
  39. package/dist/broker.js +6 -5
  40. package/dist/cluster.test.js +17 -14
  41. package/dist/connection.d.ts +11 -1
  42. package/dist/connection.js +27 -2
  43. package/dist/consumer/consumer.js +13 -9
  44. package/dist/consumer/metadata.d.ts +24 -0
  45. package/dist/consumer/metadata.js +64 -0
  46. package/dist/distributors/messages-to-topic-partition-leaders.d.ts +17 -0
  47. package/dist/distributors/messages-to-topic-partition-leaders.js +15 -0
  48. package/dist/distributors/messages-to-topic-partition-leaders.test.d.ts +1 -0
  49. package/dist/distributors/messages-to-topic-partition-leaders.test.js +30 -0
  50. package/dist/examples/src/replicator.js +34 -0
  51. package/dist/examples/src/utils/json.js +5 -0
  52. package/dist/request-handler.d.ts +16 -0
  53. package/dist/request-handler.js +67 -0
  54. package/dist/request-handler.test.d.ts +1 -0
  55. package/dist/request-handler.test.js +340 -0
  56. package/dist/src/api/api-versions.js +18 -0
  57. package/dist/src/api/create-topics.js +46 -0
  58. package/dist/src/api/delete-topics.js +26 -0
  59. package/dist/src/api/fetch.js +95 -0
  60. package/dist/src/api/find-coordinator.js +34 -0
  61. package/dist/src/api/heartbeat.js +22 -0
  62. package/dist/src/api/index.js +38 -0
  63. package/dist/src/api/init-producer-id.js +24 -0
  64. package/dist/src/api/join-group.js +48 -0
  65. package/dist/src/api/leave-group.js +30 -0
  66. package/dist/src/api/list-offsets.js +39 -0
  67. package/dist/src/api/metadata.js +47 -0
  68. package/dist/src/api/offset-commit.js +39 -0
  69. package/dist/src/api/offset-fetch.js +44 -0
  70. package/dist/src/api/produce.js +119 -0
  71. package/dist/src/api/sync-group.js +31 -0
  72. package/dist/src/broker.js +35 -0
  73. package/dist/src/connection.js +21 -0
  74. package/dist/src/consumer/consumer-group.js +131 -0
  75. package/dist/src/consumer/consumer.js +103 -0
  76. package/dist/src/consumer/metadata.js +52 -0
  77. package/dist/src/consumer/offset-manager.js +23 -0
  78. package/dist/src/index.js +19 -0
  79. package/dist/src/producer/producer.js +84 -0
  80. package/dist/src/request-handler.js +57 -0
  81. package/dist/src/request-handler.test.js +321 -0
  82. package/dist/src/types.js +2 -0
  83. package/dist/src/utils/api.js +5 -0
  84. package/dist/src/utils/decoder.js +161 -0
  85. package/dist/src/utils/encoder.js +137 -0
  86. package/dist/src/utils/error.js +10 -0
  87. package/dist/utils/api.d.ts +4 -1
  88. package/dist/utils/cached.d.ts +3 -0
  89. package/dist/utils/cached.js +19 -0
  90. package/dist/utils/debug.d.ts +2 -0
  91. package/dist/utils/debug.js +11 -0
  92. package/dist/utils/decoder.d.ts +2 -2
  93. package/dist/utils/decoder.js +14 -1
  94. package/dist/utils/encoder.d.ts +1 -0
  95. package/dist/utils/encoder.js +14 -0
  96. package/dist/utils/lock.d.ts +8 -0
  97. package/dist/utils/lock.js +44 -0
  98. package/dist/utils/memo.d.ts +1 -0
  99. package/dist/utils/memo.js +16 -0
  100. package/dist/utils/mutex.d.ts +3 -0
  101. package/dist/utils/mutex.js +32 -0
  102. package/package.json +1 -1
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDecoder = void 0;
4
+ const createDecoder = ({ buffer, offset = 0 }) => {
5
+ const readInt8 = () => {
6
+ const value = buffer.readInt8(offset);
7
+ offset += 1;
8
+ return value;
9
+ };
10
+ const readInt16 = () => {
11
+ const value = buffer.readInt16BE(offset);
12
+ offset += 2;
13
+ return value;
14
+ };
15
+ const readInt32 = () => {
16
+ const value = buffer.readInt32BE(offset);
17
+ offset += 4;
18
+ return value;
19
+ };
20
+ const readUInt32 = () => {
21
+ const value = buffer.readUInt32BE(offset);
22
+ offset += 4;
23
+ return value;
24
+ };
25
+ const readInt64 = () => {
26
+ const value = buffer.readBigInt64BE(offset);
27
+ offset += 8;
28
+ return value;
29
+ };
30
+ const readString = () => {
31
+ const length = readInt16();
32
+ if (length === -1) {
33
+ return null;
34
+ }
35
+ const value = buffer.toString('utf-8', offset, offset + length);
36
+ offset += length;
37
+ return value;
38
+ };
39
+ const readCompactString = () => {
40
+ const length = readUVarInt() - 1;
41
+ if (length < 0) {
42
+ return null;
43
+ }
44
+ const value = buffer.toString('utf-8', offset, offset + length);
45
+ offset += length;
46
+ return value;
47
+ };
48
+ const readVarInt = () => {
49
+ let currentByte;
50
+ let result = 0;
51
+ let i = 0;
52
+ while (((currentByte = buffer[offset++]) & 0x80) !== 0) {
53
+ result |= (currentByte & 0x7f) << i;
54
+ i += 7;
55
+ if (i > 28) {
56
+ throw new Error("Invalid VarInt, must contain 5 bytes or less");
57
+ }
58
+ }
59
+ result |= currentByte << i;
60
+ return result;
61
+ };
62
+ const readUVarInt = () => {
63
+ const result = readVarInt();
64
+ return result >>> 0;
65
+ };
66
+ const readVarLong = () => {
67
+ let currentByte;
68
+ let result = BigInt(0);
69
+ let i = 0;
70
+ while (((currentByte = buffer[offset++]) & 0x80) !== 0) {
71
+ result |= BigInt(currentByte & 0x7f) << BigInt(i);
72
+ i += 7;
73
+ if (i > 63) {
74
+ throw new Error("Invalid VarLong, must contain 9 bytes or less");
75
+ }
76
+ }
77
+ result |= BigInt(currentByte) << BigInt(i);
78
+ return result;
79
+ };
80
+ const readUUID = () => {
81
+ const value = buffer.toString("hex", offset, offset + 16);
82
+ offset += 16;
83
+ return value;
84
+ };
85
+ const readBoolean = () => {
86
+ const value = buffer.readInt8(offset) === 1;
87
+ offset += 1;
88
+ return value;
89
+ };
90
+ const readArray = (callback) => {
91
+ const length = readInt32();
92
+ const decoder = (0, exports.createDecoder)({ buffer, offset });
93
+ const results = Array.from({ length }).map(() => callback(decoder));
94
+ offset = decoder.offset;
95
+ return results;
96
+ };
97
+ const readCompactArray = (callback) => {
98
+ const length = readUVarInt() - 1;
99
+ const decoder = (0, exports.createDecoder)({ buffer, offset });
100
+ const results = Array.from({ length }).map(() => callback(decoder));
101
+ offset = decoder.offset;
102
+ return results;
103
+ };
104
+ const readRecords = (callback) => {
105
+ const length = readInt32();
106
+ return Array.from({ length }).map(() => {
107
+ const size = Math.ceil((readVarInt() - 1) / 2);
108
+ const child = (0, exports.createDecoder)({ buffer: buffer.subarray(offset, offset + size) });
109
+ offset += size;
110
+ return callback(child);
111
+ });
112
+ };
113
+ const read = (length) => {
114
+ const value = buffer.subarray(offset, offset + length);
115
+ offset += length;
116
+ return value;
117
+ };
118
+ const readBytes = () => {
119
+ const length = readInt32();
120
+ return read(length);
121
+ };
122
+ const readCompactBytes = (divide = false) => {
123
+ let length = readUVarInt() - 1;
124
+ if (length < 0) {
125
+ return null;
126
+ }
127
+ if (divide) {
128
+ length = Math.round(length / 2);
129
+ }
130
+ return read(length);
131
+ };
132
+ const readTagBuffer = () => {
133
+ readUVarInt();
134
+ };
135
+ return {
136
+ readInt8,
137
+ readInt16,
138
+ readInt32,
139
+ readInt64,
140
+ readUInt32,
141
+ readVarInt,
142
+ readUVarInt,
143
+ readVarLong,
144
+ readString,
145
+ readBytes,
146
+ readCompactString,
147
+ readUUID,
148
+ readBoolean,
149
+ readArray,
150
+ readCompactArray,
151
+ read,
152
+ readCompactBytes,
153
+ readTagBuffer,
154
+ readRecords,
155
+ buffer,
156
+ get offset() {
157
+ return offset;
158
+ },
159
+ };
160
+ };
161
+ exports.createDecoder = createDecoder;
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEncoder = void 0;
4
+ const createEncoder = ({ buffer = Buffer.alloc(0) } = {}) => {
5
+ const write = (rightBuffer) => (0, exports.createEncoder)({ buffer: Buffer.concat([buffer, rightBuffer]) });
6
+ const writeInt8 = (value) => {
7
+ const buffer = Buffer.alloc(1);
8
+ buffer.writeInt8(value);
9
+ return write(buffer);
10
+ };
11
+ const writeInt16 = (value) => {
12
+ const buffer = Buffer.alloc(2);
13
+ buffer.writeInt16BE(value);
14
+ return write(buffer);
15
+ };
16
+ const writeInt32 = (value) => {
17
+ const buffer = Buffer.alloc(4);
18
+ buffer.writeInt32BE(value);
19
+ return write(buffer);
20
+ };
21
+ const writeUInt32 = (value) => {
22
+ const buffer = Buffer.alloc(4);
23
+ buffer.writeUInt32BE(value);
24
+ return write(buffer);
25
+ };
26
+ const writeInt64 = (value) => {
27
+ const buffer = Buffer.alloc(8);
28
+ buffer.writeBigInt64BE(value);
29
+ return write(buffer);
30
+ };
31
+ const writeString = (value) => {
32
+ if (value === null) {
33
+ return writeInt16(-1);
34
+ }
35
+ const byteLength = Buffer.byteLength(value, "utf-8");
36
+ const buffer = Buffer.alloc(2 + byteLength);
37
+ buffer.writeInt16BE(byteLength);
38
+ buffer.write(value, 2, byteLength, "utf-8");
39
+ return write(buffer);
40
+ };
41
+ const writeUVarInt = (value) => {
42
+ const byteArray = [];
43
+ while ((value & 0xffffffff) !== 0) {
44
+ byteArray.push((value & 0x7f) | 0x80);
45
+ value >>>= 7;
46
+ }
47
+ byteArray.push(value & 0x7f);
48
+ return write(Buffer.from(byteArray));
49
+ };
50
+ const writeVarInt = (value) => {
51
+ const encodedValue = (value << 1) ^ (value >> 31);
52
+ return writeUVarInt(encodedValue);
53
+ };
54
+ const writeUVarLong = (value) => {
55
+ const byteArray = [];
56
+ while ((value & BigInt(0xffffffffffffffff)) !== BigInt(0)) {
57
+ byteArray.push(Number(value & BigInt(0x7f) | BigInt(0x80)));
58
+ value = value >> BigInt(7);
59
+ }
60
+ byteArray.push(Number(value));
61
+ return write(Buffer.from(byteArray));
62
+ };
63
+ const writeVarLong = (value) => {
64
+ const encodedValue = (value << BigInt(1)) ^ (value >> BigInt(63));
65
+ return writeUVarLong(encodedValue);
66
+ };
67
+ // const writeVarLong = (value: bigint) => {
68
+ // const encodedValue = (value << BigInt(1)) ^ (value >> BigInt(63));
69
+ // return writeUVarInt(Number(encodedValue));
70
+ // };
71
+ const writeCompactString = (value) => {
72
+ if (value === null) {
73
+ return writeUVarInt(0);
74
+ }
75
+ const byteLength = Buffer.byteLength(value, "utf-8");
76
+ const buffer = Buffer.alloc(byteLength);
77
+ buffer.write(value, 0, byteLength, "utf-8");
78
+ return writeUVarInt(byteLength + 1).write(buffer);
79
+ };
80
+ const writeVarIntString = (value) => {
81
+ if (value === null) {
82
+ return writeVarInt(-1);
83
+ }
84
+ return writeVarInt(Buffer.byteLength(value, 'utf-8')).write(Buffer.from(value, 'utf-8'));
85
+ };
86
+ const writeUUID = (value) => {
87
+ if (value === null) {
88
+ return write(Buffer.alloc(16));
89
+ }
90
+ return write(Buffer.from(value, "hex"));
91
+ };
92
+ const writeCompactArray = (arr, callback) => {
93
+ if (arr === null) {
94
+ return writeUVarInt(0);
95
+ }
96
+ const buffers = arr.map((item) => callback((0, exports.createEncoder)(), item).value());
97
+ return writeUVarInt(buffers.length + 1).write(Buffer.concat(buffers));
98
+ };
99
+ const writeArray = (arr, callback) => {
100
+ const buffers = arr.map((item) => callback((0, exports.createEncoder)(), item).value());
101
+ return writeInt32(arr.length).write(Buffer.concat(buffers));
102
+ };
103
+ const writeBoolean = (value) => {
104
+ const buffer = Buffer.alloc(1);
105
+ buffer.writeInt8(value ? 1 : 0);
106
+ return write(buffer);
107
+ };
108
+ const writeBytes = (value) => {
109
+ return writeInt32(value.length).write(value);
110
+ };
111
+ const writeCompactBytes = (value) => {
112
+ return writeUVarInt(value.length + 1).write(value);
113
+ };
114
+ const value = () => buffer;
115
+ return {
116
+ write,
117
+ writeInt8,
118
+ writeInt16,
119
+ writeInt32,
120
+ writeUInt32,
121
+ writeInt64,
122
+ writeCompactString,
123
+ writeVarIntString,
124
+ writeString,
125
+ writeUVarInt,
126
+ writeVarInt,
127
+ writeVarLong,
128
+ writeUUID,
129
+ writeCompactArray,
130
+ writeArray,
131
+ writeBoolean,
132
+ writeBytes,
133
+ writeCompactBytes,
134
+ value,
135
+ };
136
+ };
137
+ exports.createEncoder = createEncoder;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KafkaTSError = void 0;
4
+ class KafkaTSError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "KafkaTSError";
8
+ }
9
+ }
10
+ exports.KafkaTSError = KafkaTSError;
@@ -3,7 +3,10 @@ import { Encoder } from './encoder';
3
3
  export type Api<Request, Response> = {
4
4
  apiKey: number;
5
5
  apiVersion: number;
6
- request: (encoder: Encoder, body: Request) => Encoder;
6
+ requestHeaderVersion: 1 | 2;
7
+ responseHeaderVersion: 0 | 1;
8
+ fallback?: Api<Request, Response>;
9
+ request: (encoder: Encoder, data: Request) => Encoder;
7
10
  response: (buffer: Decoder) => Promise<Response> | Response;
8
11
  };
9
12
  export declare const createApi: <Request, Response>(api: Api<Request, Response>) => Api<Request, Response>;
@@ -0,0 +1,3 @@
1
+ export declare const cached: <F extends (...args: any[]) => any>(func: F, createKey: (...args: Parameters<F>) => string) => F & {
2
+ clear: () => void;
3
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cached = void 0;
4
+ const cached = (func, createKey) => {
5
+ const cache = {};
6
+ const cachedFunc = ((...args) => {
7
+ const key = createKey(...args);
8
+ if (!(key in cache)) {
9
+ cache[key] = func(...args);
10
+ }
11
+ return cache[key];
12
+ });
13
+ cachedFunc.clear = () => {
14
+ for (const key in cache)
15
+ delete cache[key];
16
+ };
17
+ return cachedFunc;
18
+ };
19
+ exports.cached = cached;
@@ -0,0 +1,2 @@
1
+ export declare const serializer: (_: string, value: unknown) => unknown;
2
+ export declare const createDebugger: (module: string) => (func: string, message: string, data?: unknown) => void;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDebugger = exports.serializer = void 0;
4
+ const serializer = (_, value) => (typeof value === 'bigint' ? value.toString() : value);
5
+ exports.serializer = serializer;
6
+ const createDebugger = (module) => (func, message, data) => {
7
+ if (!process.env.DEBUG?.includes('kafka-ts'))
8
+ return;
9
+ console.debug(`[${module}] ${func}: ${message}`, data && `(${data instanceof Error ? data : JSON.stringify(data, exports.serializer, 4)})`);
10
+ };
11
+ exports.createDebugger = createDebugger;
@@ -24,7 +24,7 @@ export declare class Decoder {
24
24
  readVarIntArray<T>(callback: (opts: Decoder) => T): T[];
25
25
  readRecords<T>(callback: (opts: Decoder) => T): T[];
26
26
  read(length?: number): Buffer<ArrayBufferLike>;
27
- readBytes(): Buffer<ArrayBufferLike>;
27
+ readBytes(): Buffer<ArrayBufferLike> | null;
28
28
  readCompactBytes(): Buffer<ArrayBufferLike> | null;
29
- readTagBuffer(): void;
29
+ readTagBuffer(): Record<number, Buffer>;
30
30
  }
@@ -149,6 +149,9 @@ class Decoder {
149
149
  }
150
150
  readBytes() {
151
151
  const length = this.readInt32();
152
+ if (length < 0) {
153
+ return null;
154
+ }
152
155
  return this.read(length);
153
156
  }
154
157
  readCompactBytes() {
@@ -159,7 +162,17 @@ class Decoder {
159
162
  return this.read(length);
160
163
  }
161
164
  readTagBuffer() {
162
- this.readUVarInt();
165
+ const numTags = this.readUVarInt();
166
+ if (!numTags)
167
+ return {};
168
+ const tags = {};
169
+ for (let i = 0; i < numTags; i++) {
170
+ const tagId = this.readUVarInt();
171
+ const size = this.readUVarInt();
172
+ const value = this.read(size);
173
+ tags[tagId] = value;
174
+ }
175
+ return tags;
163
176
  }
164
177
  }
165
178
  exports.Decoder = Decoder;
@@ -25,5 +25,6 @@ export declare class Encoder {
25
25
  writeVarIntArray<T>(arr: T[], callback: (encoder: Encoder, item: T) => void): this;
26
26
  writeBytes(value: Buffer): this;
27
27
  writeCompactBytes(value: Buffer): this;
28
+ writeTagBuffer(tags?: Record<number, Buffer>): this;
28
29
  value(): Buffer<ArrayBufferLike>;
29
30
  }
@@ -152,6 +152,20 @@ class Encoder {
152
152
  this.write(value);
153
153
  return this;
154
154
  }
155
+ writeTagBuffer(tags) {
156
+ if (!tags)
157
+ return this.writeUVarInt(0);
158
+ const entries = Object.entries(tags)
159
+ .map(([k, v]) => [Number(k), v])
160
+ .sort(([a], [b]) => a - b);
161
+ this.writeUVarInt(entries.length);
162
+ for (const [tagId, tagBuffer] of entries) {
163
+ this.writeUVarInt(tagId);
164
+ this.writeUVarInt(tagBuffer.length);
165
+ this.write(tagBuffer);
166
+ }
167
+ return this;
168
+ }
155
169
  value() {
156
170
  return this.buffer.subarray(0, this.offset);
157
171
  }
@@ -0,0 +1,8 @@
1
+ import EventEmitter from 'events';
2
+ export declare class Lock extends EventEmitter {
3
+ private locks;
4
+ constructor();
5
+ acquire(keys: string[], callback: () => Promise<void>): Promise<void>;
6
+ private acquireKey;
7
+ private releaseKey;
8
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Lock = void 0;
7
+ const events_1 = __importDefault(require("events"));
8
+ const logger_1 = require("./logger");
9
+ class Lock extends events_1.default {
10
+ locks = {};
11
+ constructor() {
12
+ super();
13
+ this.setMaxListeners(Infinity);
14
+ }
15
+ async acquire(keys, callback) {
16
+ await Promise.all(keys.map((key) => this.acquireKey(key)));
17
+ try {
18
+ await callback();
19
+ }
20
+ finally {
21
+ keys.forEach((key) => this.releaseKey(key));
22
+ }
23
+ }
24
+ async acquireKey(key) {
25
+ while (this.locks[key]) {
26
+ await new Promise((resolve) => {
27
+ const timeout = setTimeout(() => {
28
+ logger_1.log.warn(`Lock timed out`, { key });
29
+ this.releaseKey(key);
30
+ }, 60_000);
31
+ this.once(`release:${key}`, () => {
32
+ clearTimeout(timeout);
33
+ resolve();
34
+ });
35
+ });
36
+ }
37
+ this.locks[key] = true;
38
+ }
39
+ releaseKey(key) {
40
+ this.locks[key] = false;
41
+ this.emit(`release:${key}`);
42
+ }
43
+ }
44
+ exports.Lock = Lock;
@@ -0,0 +1 @@
1
+ export declare const memo: <T extends (...args: any[]) => any>(fn: T) => (...args: Parameters<T>) => ReturnType<T>;
@@ -0,0 +1,16 @@
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;
@@ -0,0 +1,3 @@
1
+ export declare const createMutex: () => {
2
+ exclusive: (fn: () => Promise<void>) => Promise<void>;
3
+ };
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMutex = void 0;
4
+ const createMutex = () => {
5
+ const queue = [];
6
+ let isLocked = false;
7
+ const acquire = () => {
8
+ return new Promise((resolve) => {
9
+ if (!isLocked) {
10
+ isLocked = true;
11
+ return resolve();
12
+ }
13
+ queue.push(resolve);
14
+ });
15
+ };
16
+ const release = () => {
17
+ isLocked = false;
18
+ const next = queue.shift();
19
+ next && next();
20
+ };
21
+ const exclusive = async (fn) => {
22
+ await acquire();
23
+ try {
24
+ await fn();
25
+ }
26
+ finally {
27
+ release();
28
+ }
29
+ };
30
+ return { exclusive };
31
+ };
32
+ exports.createMutex = createMutex;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kafka-ts",
3
- "version": "1.3.1-beta.1",
3
+ "version": "1.3.1-beta.2",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",