kafka-ts 0.0.1-beta.3 → 0.0.1-beta.4
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/examples/src/consumer.ts +7 -1
- package/examples/src/create-topic.ts +3 -3
- package/examples/src/producer.ts +2 -2
- package/examples/src/replicator.ts +2 -1
- package/package.json +1 -1
- package/src/__snapshots__/cluster.test.ts.snap +266 -9
- package/src/api/fetch.ts +78 -28
- package/src/cluster.test.ts +8 -5
- package/src/cluster.ts +42 -37
- package/src/codecs/gzip.ts +9 -0
- package/src/codecs/index.ts +16 -0
- package/src/codecs/none.ts +6 -0
- package/src/codecs/types.ts +4 -0
- package/src/connection.ts +3 -2
- package/src/consumer/consumer-group.ts +8 -6
- package/src/consumer/consumer.ts +35 -24
- package/src/consumer/fetch-manager.ts +25 -19
- package/src/consumer/fetcher.ts +4 -4
- package/src/consumer/processor.ts +3 -1
- package/src/index.ts +2 -0
- package/src/producer/producer.ts +3 -3
- package/src/utils/api.ts +1 -1
- package/src/utils/decoder.ts +13 -3
- package/src/utils/logger.ts +37 -0
- package/src/utils/mutex.ts +31 -0
- package/src/utils/tracer.ts +6 -4
- package/src/utils/debug.ts +0 -9
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const createMutex = () => {
|
|
2
|
+
const queue: (() => void)[] = [];
|
|
3
|
+
let isLocked = false;
|
|
4
|
+
|
|
5
|
+
const acquire = () => {
|
|
6
|
+
return new Promise<void>((resolve) => {
|
|
7
|
+
if (!isLocked) {
|
|
8
|
+
isLocked = true;
|
|
9
|
+
return resolve();
|
|
10
|
+
}
|
|
11
|
+
queue.push(resolve);
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const release = () => {
|
|
16
|
+
isLocked = false;
|
|
17
|
+
const next = queue.shift();
|
|
18
|
+
next && next();
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const exclusive = async (fn: () => Promise<void>) => {
|
|
22
|
+
await acquire();
|
|
23
|
+
try {
|
|
24
|
+
await fn();
|
|
25
|
+
} finally {
|
|
26
|
+
release();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return { exclusive };
|
|
31
|
+
};
|
package/src/utils/tracer.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { log } from './logger';
|
|
2
2
|
|
|
3
3
|
export const createTracer =
|
|
4
4
|
(module: string, attributes?: Record<string, unknown>) =>
|
|
@@ -12,9 +12,11 @@ export const createTracer =
|
|
|
12
12
|
const metadata = fn?.(...args);
|
|
13
13
|
|
|
14
14
|
const onEnd = <T>(result: T): T => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
log.debug(`[${module}.${propertyKey}] ${metadata?.message ?? ''} +${Date.now() - startTime}ms`, {
|
|
16
|
+
...attributes,
|
|
17
|
+
...metadata,
|
|
18
|
+
...result && { result},
|
|
19
|
+
});
|
|
18
20
|
return result;
|
|
19
21
|
};
|
|
20
22
|
|
package/src/utils/debug.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export const serializer = (_: string, value: unknown) => (typeof value === 'bigint' ? value.toString() : value);
|
|
2
|
-
|
|
3
|
-
export const createDebugger = (module: string) => (func: string, message: string, data?: unknown) => {
|
|
4
|
-
if (!process.env.DEBUG?.includes('kafka-ts')) return;
|
|
5
|
-
console.debug(
|
|
6
|
-
`[${module}] ${func}: ${message}`,
|
|
7
|
-
data && `(${data instanceof Error ? data : JSON.stringify(data, serializer, 4)})`,
|
|
8
|
-
);
|
|
9
|
-
};
|