pulse-sdk 0.0.5 → 0.1.0-main.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/README.md +83 -144
- package/dist/config.d.ts +20 -10
- package/dist/config.js +40 -56
- package/dist/consumer.d.ts +11 -11
- package/dist/consumer.js +159 -110
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/message.d.ts +10 -15
- package/dist/message.js +46 -49
- package/dist/producer.d.ts +5 -8
- package/dist/producer.js +50 -7
- package/dist/proto/client.d.ts +3 -0
- package/dist/proto/client.js +20 -6
- package/dist/proto/pulse.proto +10 -1
- package/package.json +9 -8
- package/dist/consumerManager.d.ts +0 -6
- package/dist/consumerManager.js +0 -200
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type Handler = (msg: any) => any;
|
|
2
|
-
export declare function shutdownAll(): Promise<void>;
|
|
3
|
-
export declare function registerSharedHandler(grpcUrl: string, topic: string, consumerName: string, handler: Handler, opts?: {
|
|
4
|
-
autoCommit?: boolean;
|
|
5
|
-
}): () => void;
|
|
6
|
-
export {};
|
package/dist/consumerManager.js
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.shutdownAll = shutdownAll;
|
|
4
|
-
exports.registerSharedHandler = registerSharedHandler;
|
|
5
|
-
const client_1 = require("./proto/client");
|
|
6
|
-
const message_1 = require("./message");
|
|
7
|
-
let suppressStreamWarnings = false;
|
|
8
|
-
async function shutdownAll() {
|
|
9
|
-
// When shutting down tests/teardown, suppress stream warnings so Jest doesn't
|
|
10
|
-
// complain about logs after tests are finished.
|
|
11
|
-
suppressStreamWarnings = true;
|
|
12
|
-
for (const [k, entry] of Array.from(registry.entries())) {
|
|
13
|
-
try {
|
|
14
|
-
if (entry.stream) {
|
|
15
|
-
try {
|
|
16
|
-
entry.stream.removeAllListeners();
|
|
17
|
-
}
|
|
18
|
-
catch (_) { }
|
|
19
|
-
try {
|
|
20
|
-
if (entry.stream.cancel)
|
|
21
|
-
entry.stream.cancel();
|
|
22
|
-
}
|
|
23
|
-
catch (_) { }
|
|
24
|
-
entry.stream = null;
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
entry.handlers.clear();
|
|
28
|
-
}
|
|
29
|
-
catch (_) { }
|
|
30
|
-
}
|
|
31
|
-
catch (_) {
|
|
32
|
-
// ignore individual errors
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
if (entry.client && typeof entry.client.close === 'function')
|
|
36
|
-
entry.client.close();
|
|
37
|
-
}
|
|
38
|
-
catch (_) { }
|
|
39
|
-
try {
|
|
40
|
-
registry.delete(k);
|
|
41
|
-
}
|
|
42
|
-
catch (_) { }
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const registry = new Map();
|
|
46
|
-
function keyFor(grpcUrl, topic, consumerName) {
|
|
47
|
-
return `${grpcUrl}::${topic}::${consumerName}`;
|
|
48
|
-
}
|
|
49
|
-
function registerSharedHandler(grpcUrl, topic, consumerName, handler, opts) {
|
|
50
|
-
const k = keyFor(grpcUrl, topic, consumerName);
|
|
51
|
-
let entry = registry.get(k);
|
|
52
|
-
if (!entry) {
|
|
53
|
-
const client = (0, client_1.createClient)(grpcUrl);
|
|
54
|
-
entry = { topic, consumerName, client, stream: null, handlers: new Set(), nextIndex: 0, grpcUrl, autoCommit: opts?.autoCommit !== false };
|
|
55
|
-
// add handler before starting the stream to avoid losing early messages
|
|
56
|
-
entry.handlers.add(handler);
|
|
57
|
-
registry.set(k, entry);
|
|
58
|
-
startStream(entry);
|
|
59
|
-
return () => {
|
|
60
|
-
// unregister
|
|
61
|
-
const e = registry.get(k);
|
|
62
|
-
if (!e)
|
|
63
|
-
return;
|
|
64
|
-
e.handlers.delete(handler);
|
|
65
|
-
if (e.handlers.size === 0) {
|
|
66
|
-
try {
|
|
67
|
-
if (e.stream && e.stream.cancel)
|
|
68
|
-
e.stream.cancel();
|
|
69
|
-
}
|
|
70
|
-
catch (err) { }
|
|
71
|
-
registry.delete(k);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
entry.handlers.add(handler);
|
|
76
|
-
return () => {
|
|
77
|
-
// unregister
|
|
78
|
-
const e = registry.get(k);
|
|
79
|
-
if (!e)
|
|
80
|
-
return;
|
|
81
|
-
e.handlers.delete(handler);
|
|
82
|
-
if (e.handlers.size === 0) {
|
|
83
|
-
// cleanup stream
|
|
84
|
-
try {
|
|
85
|
-
if (e.stream && e.stream.cancel)
|
|
86
|
-
e.stream.cancel();
|
|
87
|
-
}
|
|
88
|
-
catch (e) {
|
|
89
|
-
// ignore
|
|
90
|
-
}
|
|
91
|
-
registry.delete(k);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
function startStream(entry) {
|
|
96
|
-
const req = { topic: entry.topic, consumer_name: entry.consumerName, offset: 0 };
|
|
97
|
-
const stream = entry.client.Consume(req);
|
|
98
|
-
entry.stream = stream;
|
|
99
|
-
// debug: console.log('consumerManager.startStream', entry.grpcUrl, entry.topic, entry.consumerName);
|
|
100
|
-
stream.on('data', (msg) => {
|
|
101
|
-
const message = new message_1.Message(msg);
|
|
102
|
-
const handlers = Array.from(entry.handlers);
|
|
103
|
-
if (handlers.length === 0)
|
|
104
|
-
return;
|
|
105
|
-
if (entry.nextIndex === undefined)
|
|
106
|
-
entry.nextIndex = 0;
|
|
107
|
-
const h = handlers[entry.nextIndex % handlers.length];
|
|
108
|
-
entry.nextIndex = (entry.nextIndex + 1) % handlers.length;
|
|
109
|
-
// run handler inside context providing the stub for commit(); support async handlers
|
|
110
|
-
(async () => {
|
|
111
|
-
try {
|
|
112
|
-
await (0, message_1.runWithContext)({ stub: entry.client, topic: entry.topic, consumerName: entry.consumerName, offset: message.offset }, async () => {
|
|
113
|
-
const r = h(message);
|
|
114
|
-
if (r && typeof r.then === 'function')
|
|
115
|
-
await r;
|
|
116
|
-
if (entry.autoCommit) {
|
|
117
|
-
try {
|
|
118
|
-
await (0, message_1.commit)();
|
|
119
|
-
}
|
|
120
|
-
catch (err) { /* ignore commit errors here */ }
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
catch (e) {
|
|
125
|
-
console.warn('handler error', e);
|
|
126
|
-
}
|
|
127
|
-
})().catch(() => { });
|
|
128
|
-
});
|
|
129
|
-
stream.on('error', (e) => {
|
|
130
|
-
// Log once and clean up the registry entry to avoid reconnect storms and test leaks
|
|
131
|
-
if (!suppressStreamWarnings) {
|
|
132
|
-
// Ignore normal client-side cancellations which happen during unregister
|
|
133
|
-
// and shutdown; only log unexpected stream errors.
|
|
134
|
-
try {
|
|
135
|
-
const code = e && typeof e.code !== 'undefined' ? e.code : null;
|
|
136
|
-
if (code !== 1) {
|
|
137
|
-
console.warn('shared consumer stream error for', entry.topic, e);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
catch (_) {
|
|
141
|
-
// if anything goes wrong determining code, log the error
|
|
142
|
-
console.warn('shared consumer stream error for', entry.topic, e);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
if (entry.stream) {
|
|
147
|
-
try {
|
|
148
|
-
entry.stream.removeAllListeners();
|
|
149
|
-
}
|
|
150
|
-
catch (_) { }
|
|
151
|
-
try {
|
|
152
|
-
if (entry.stream.cancel)
|
|
153
|
-
entry.stream.cancel();
|
|
154
|
-
}
|
|
155
|
-
catch (_) { }
|
|
156
|
-
entry.stream = null;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
catch (err) {
|
|
160
|
-
// ignore
|
|
161
|
-
}
|
|
162
|
-
try {
|
|
163
|
-
try {
|
|
164
|
-
if (entry.client && typeof entry.client.close === 'function')
|
|
165
|
-
entry.client.close();
|
|
166
|
-
}
|
|
167
|
-
catch (_) { }
|
|
168
|
-
registry.delete(keyFor(entry.grpcUrl || '', entry.topic, entry.consumerName));
|
|
169
|
-
}
|
|
170
|
-
catch (err) {
|
|
171
|
-
// ignore
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
stream.on('end', () => {
|
|
175
|
-
// stream ended; clean up entry
|
|
176
|
-
try {
|
|
177
|
-
if (entry.stream) {
|
|
178
|
-
try {
|
|
179
|
-
entry.stream.removeAllListeners();
|
|
180
|
-
}
|
|
181
|
-
catch (_) { }
|
|
182
|
-
try {
|
|
183
|
-
if (entry.stream.cancel)
|
|
184
|
-
entry.stream.cancel();
|
|
185
|
-
}
|
|
186
|
-
catch (_) { }
|
|
187
|
-
entry.stream = null;
|
|
188
|
-
}
|
|
189
|
-
try {
|
|
190
|
-
if (entry.client && typeof entry.client.close === 'function')
|
|
191
|
-
entry.client.close();
|
|
192
|
-
}
|
|
193
|
-
catch (_) { }
|
|
194
|
-
registry.delete(keyFor(entry.grpcUrl || '', entry.topic, entry.consumerName));
|
|
195
|
-
}
|
|
196
|
-
catch (err) {
|
|
197
|
-
// ignore
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
}
|