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
package/dist/consumer.js
CHANGED
|
@@ -1,124 +1,173 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Message = exports.commit = void 0;
|
|
4
|
+
exports.stop = stop;
|
|
5
|
+
exports.consumer = consumer;
|
|
6
|
+
exports.run = run;
|
|
7
|
+
const config_1 = require("./config");
|
|
4
8
|
const client_1 = require("./proto/client");
|
|
5
|
-
const consumerManager_1 = require("./consumerManager");
|
|
6
|
-
const crypto_1 = require("crypto");
|
|
7
9
|
const message_1 = require("./message");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return message_1.Message; } });
|
|
11
|
+
Object.defineProperty(exports, "commit", { enumerable: true, get: function () { return message_1.commit; } });
|
|
12
|
+
const crypto_1 = require("crypto");
|
|
13
|
+
const _consumers = [];
|
|
14
|
+
const _cleanupFunctions = [];
|
|
15
|
+
function stop() {
|
|
16
|
+
_cleanupFunctions.forEach(fn => fn());
|
|
17
|
+
_cleanupFunctions.length = 0;
|
|
18
|
+
_consumers.length = 0;
|
|
19
|
+
}
|
|
20
|
+
function consumer(topic, handler, options = {}) {
|
|
21
|
+
const config = (0, config_1.getConfig)();
|
|
22
|
+
const host = options.host || config.broker.host;
|
|
23
|
+
const port = options.port || config.broker.grpc_port;
|
|
24
|
+
const baseGroup = options.consumerGroup || config.client.id;
|
|
25
|
+
const grouped = options.grouped !== undefined ? options.grouped : true;
|
|
26
|
+
let group = baseGroup;
|
|
27
|
+
if (!grouped) {
|
|
28
|
+
group = `${baseGroup}-${(0, crypto_1.randomUUID)().replace(/-/g, '')}`;
|
|
29
|
+
}
|
|
30
|
+
let autoCommit = options.autoCommit;
|
|
31
|
+
if (autoCommit === undefined) {
|
|
32
|
+
autoCommit = config.client.auto_commit;
|
|
33
|
+
// Check topic specific config
|
|
34
|
+
const topicCfg = config.topics?.find(t => t.name === topic);
|
|
35
|
+
// Note: Python SDK checks topic_config["consume"]["auto_commit"] but our config.ts
|
|
36
|
+
// currently only has create_if_missing and config (fifo, retention).
|
|
37
|
+
// We'll stick to global client config for now unless we expand TopicConfig.
|
|
14
38
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
39
|
+
_consumers.push({
|
|
40
|
+
topic,
|
|
41
|
+
host,
|
|
42
|
+
port,
|
|
43
|
+
group,
|
|
44
|
+
autoCommit: autoCommit,
|
|
45
|
+
handler,
|
|
46
|
+
grouped,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async function run() {
|
|
50
|
+
// Group consumers by (topic, host, port, group)
|
|
51
|
+
const groupedMap = new Map();
|
|
52
|
+
for (const c of _consumers) {
|
|
53
|
+
const key = `${c.topic}:${c.host}:${c.port}:${c.group}`;
|
|
54
|
+
if (!groupedMap.has(key)) {
|
|
55
|
+
groupedMap.set(key, {
|
|
56
|
+
topic: c.topic,
|
|
57
|
+
host: c.host,
|
|
58
|
+
port: c.port,
|
|
59
|
+
group: c.group,
|
|
60
|
+
autoCommit: c.autoCommit,
|
|
61
|
+
handlers: [],
|
|
62
|
+
});
|
|
18
63
|
}
|
|
19
|
-
|
|
64
|
+
groupedMap.get(key).handlers.push(c.handler);
|
|
65
|
+
}
|
|
66
|
+
const promises = [];
|
|
67
|
+
for (const groupConfig of groupedMap.values()) {
|
|
68
|
+
promises.push(consumeLoopGroup(groupConfig));
|
|
20
69
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (this.config.autoCommit !== false) {
|
|
42
|
-
try {
|
|
43
|
-
await (0, message_1.commit)();
|
|
44
|
-
}
|
|
45
|
-
catch (_) { /* ignore commit errors */ }
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
})().catch(() => { });
|
|
49
|
-
}, { autoCommit: this.config.autoCommit });
|
|
50
|
-
this.unregisterFns.push(unregister);
|
|
70
|
+
// We don't await promises here to let them run in background,
|
|
71
|
+
// but we could if we wanted to block until they all finish (which they won't).
|
|
72
|
+
// However, to keep the process alive, the user should probably await this or we return a promise that never resolves?
|
|
73
|
+
// Python's run() blocks. In Node, usually we just start things.
|
|
74
|
+
// But if the script ends, the process exits.
|
|
75
|
+
// We'll return a promise that never resolves to simulate blocking if awaited.
|
|
76
|
+
return new Promise(() => { });
|
|
77
|
+
}
|
|
78
|
+
async function consumeLoopGroup(groupConfig) {
|
|
79
|
+
const address = `${groupConfig.host}:${groupConfig.port}`;
|
|
80
|
+
// console.log(`Starting consumer for topic '${groupConfig.topic}' (group: ${groupConfig.group}) on ${address}`);
|
|
81
|
+
let handlerIdx = 0;
|
|
82
|
+
let currentStream = null;
|
|
83
|
+
let retryTimeout = null;
|
|
84
|
+
let isStopped = false;
|
|
85
|
+
const cleanup = () => {
|
|
86
|
+
isStopped = true;
|
|
87
|
+
if (currentStream) {
|
|
88
|
+
try {
|
|
89
|
+
currentStream.cancel();
|
|
51
90
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
// If grouped is explicitly false, and the consumerName equals the configured
|
|
56
|
-
// client name or the default literal, generate a unique consumer id so each
|
|
57
|
-
// consumer receives messages independently (mirrors Python behaviour).
|
|
58
|
-
if (!grouped) {
|
|
59
|
-
const base = this.config.consumerName || 'default';
|
|
60
|
-
if (consumerName === base || consumerName === 'default') {
|
|
61
|
-
consumerName = `${base}-${(0, crypto_1.randomUUID)().replace(/-/g, '')}`;
|
|
91
|
+
catch (e) {
|
|
92
|
+
// Ignore cancel errors
|
|
62
93
|
}
|
|
94
|
+
currentStream = null;
|
|
63
95
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
stream = this.client.Consume(req);
|
|
96
|
+
if (retryTimeout) {
|
|
97
|
+
clearTimeout(retryTimeout);
|
|
98
|
+
retryTimeout = null;
|
|
68
99
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
100
|
+
};
|
|
101
|
+
_cleanupFunctions.push(cleanup);
|
|
102
|
+
const startStream = async () => {
|
|
103
|
+
if (isStopped)
|
|
104
|
+
return;
|
|
105
|
+
try {
|
|
106
|
+
const client = (0, client_1.createClient)(address);
|
|
107
|
+
const req = {
|
|
108
|
+
topic: groupConfig.topic,
|
|
109
|
+
consumer_name: groupConfig.group,
|
|
110
|
+
offset: 0,
|
|
111
|
+
};
|
|
112
|
+
const stream = client.Consume(req);
|
|
113
|
+
currentStream = stream;
|
|
114
|
+
stream.on('data', async (protoMsg) => {
|
|
115
|
+
if (isStopped)
|
|
116
|
+
return;
|
|
117
|
+
// Pause stream to process message sequentially
|
|
118
|
+
stream.pause();
|
|
119
|
+
const msg = new message_1.Message(protoMsg);
|
|
120
|
+
if (groupConfig.handlers.length === 0) {
|
|
121
|
+
stream.resume();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const handler = groupConfig.handlers[handlerIdx % groupConfig.handlers.length];
|
|
125
|
+
handlerIdx++;
|
|
126
|
+
const ctx = {
|
|
127
|
+
stub: client,
|
|
128
|
+
topic: groupConfig.topic,
|
|
129
|
+
consumerGroup: groupConfig.group,
|
|
130
|
+
offset: msg.offset,
|
|
131
|
+
committed: false,
|
|
132
|
+
};
|
|
133
|
+
try {
|
|
134
|
+
await (0, message_1.runWithContext)(ctx, async () => {
|
|
135
|
+
await handler(msg);
|
|
136
|
+
if (groupConfig.autoCommit && !ctx.committed) {
|
|
137
|
+
await (0, message_1.commit)();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
console.error(`Error processing message: ${e}`);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
// Resume stream after processing
|
|
146
|
+
stream.resume();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
stream.on('error', (err) => {
|
|
150
|
+
if (isStopped)
|
|
151
|
+
return;
|
|
152
|
+
// 1 = CANCELLED
|
|
153
|
+
if (err.code === 1)
|
|
154
|
+
return;
|
|
155
|
+
console.error(`Connection lost for ${groupConfig.topic}: ${err.message}. Retrying in 5s...`);
|
|
156
|
+
retryTimeout = setTimeout(startStream, 5000);
|
|
157
|
+
});
|
|
158
|
+
stream.on('end', () => {
|
|
159
|
+
if (isStopped)
|
|
160
|
+
return;
|
|
161
|
+
// console.warn(`Stream ended for ${groupConfig.topic}. Retrying in 5s...`);
|
|
162
|
+
retryTimeout = setTimeout(startStream, 5000);
|
|
163
|
+
});
|
|
76
164
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
(async () => {
|
|
83
|
-
try {
|
|
84
|
-
await (0, message_1.runWithContext)({ stub: this.client, topic: req.topic, consumerName, offset: message.offset }, async () => {
|
|
85
|
-
try {
|
|
86
|
-
const r = h(message);
|
|
87
|
-
if (r && typeof r.then === 'function')
|
|
88
|
-
await r;
|
|
89
|
-
}
|
|
90
|
-
catch (e) { /* handler error ignored here */ }
|
|
91
|
-
if (this.config.autoCommit !== false) {
|
|
92
|
-
try {
|
|
93
|
-
await (0, message_1.commit)();
|
|
94
|
-
}
|
|
95
|
-
catch (_) { /* ignore commit errors */ }
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
catch (err) {
|
|
100
|
-
// ignore
|
|
101
|
-
}
|
|
102
|
-
})().catch(() => { });
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
const p = new Promise((resolve, reject) => {
|
|
106
|
-
stream.on('end', () => resolve());
|
|
107
|
-
stream.on('error', (e) => reject(e));
|
|
108
|
-
});
|
|
109
|
-
// prevent unhandled rejections when callers don't await the returned promise
|
|
110
|
-
p.catch(() => { });
|
|
111
|
-
return p;
|
|
112
|
-
}
|
|
113
|
-
// unregister any shared handlers when this consumer is discarded
|
|
114
|
-
close() {
|
|
115
|
-
for (const u of this.unregisterFns) {
|
|
116
|
-
try {
|
|
117
|
-
u();
|
|
118
|
-
}
|
|
119
|
-
catch (e) { /* ignore */ }
|
|
165
|
+
catch (e) {
|
|
166
|
+
if (isStopped)
|
|
167
|
+
return;
|
|
168
|
+
console.error(`Unexpected error in consumer ${groupConfig.topic}: ${e.message}`);
|
|
169
|
+
retryTimeout = setTimeout(startStream, 5000);
|
|
120
170
|
}
|
|
121
|
-
|
|
122
|
-
|
|
171
|
+
};
|
|
172
|
+
startStream();
|
|
123
173
|
}
|
|
124
|
-
exports.Consumer = Consumer;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/message.d.ts
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
export interface
|
|
2
|
-
offset: number;
|
|
3
|
-
timestamp: number;
|
|
4
|
-
payload: any;
|
|
5
|
-
headers: Record<string, string>;
|
|
6
|
-
}
|
|
7
|
-
interface MessageContext {
|
|
1
|
+
export interface MessageContext {
|
|
8
2
|
stub: any;
|
|
9
3
|
topic: string;
|
|
10
|
-
|
|
4
|
+
consumerGroup: string;
|
|
11
5
|
offset: number;
|
|
12
|
-
committed
|
|
6
|
+
committed: boolean;
|
|
13
7
|
}
|
|
14
|
-
export declare function runWithContext(ctx: MessageContext, fn: () => void):
|
|
15
|
-
export declare function getContext(): MessageContext | undefined;
|
|
8
|
+
export declare function runWithContext(ctx: MessageContext, fn: () => void | Promise<void>): void | Promise<void>;
|
|
16
9
|
export declare function commit(): Promise<void>;
|
|
17
|
-
export declare class Message
|
|
10
|
+
export declare class Message {
|
|
18
11
|
offset: number;
|
|
19
12
|
timestamp: number;
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
private _rawPayload;
|
|
14
|
+
private _headers;
|
|
22
15
|
constructor(protoMsg: any);
|
|
16
|
+
get payload(): any;
|
|
17
|
+
get rawPayload(): Buffer;
|
|
18
|
+
get headers(): Record<string, string>;
|
|
23
19
|
}
|
|
24
|
-
export {};
|
package/dist/message.js
CHANGED
|
@@ -2,76 +2,73 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Message = void 0;
|
|
4
4
|
exports.runWithContext = runWithContext;
|
|
5
|
-
exports.getContext = getContext;
|
|
6
5
|
exports.commit = commit;
|
|
7
6
|
const async_hooks_1 = require("async_hooks");
|
|
8
|
-
const
|
|
7
|
+
const contextStorage = new async_hooks_1.AsyncLocalStorage();
|
|
9
8
|
function runWithContext(ctx, fn) {
|
|
10
|
-
return
|
|
11
|
-
}
|
|
12
|
-
function getContext() {
|
|
13
|
-
return storage.getStore();
|
|
9
|
+
return contextStorage.run(ctx, fn);
|
|
14
10
|
}
|
|
15
11
|
async function commit() {
|
|
16
|
-
const ctx =
|
|
17
|
-
if (!ctx)
|
|
12
|
+
const ctx = contextStorage.getStore();
|
|
13
|
+
if (!ctx) {
|
|
18
14
|
throw new Error('commit() called outside of a consumer handler');
|
|
19
|
-
|
|
15
|
+
}
|
|
16
|
+
if (ctx.committed) {
|
|
20
17
|
return;
|
|
18
|
+
}
|
|
21
19
|
return new Promise((resolve, reject) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
const req = {
|
|
21
|
+
topic: ctx.topic,
|
|
22
|
+
consumer_name: ctx.consumerGroup,
|
|
23
|
+
offset: ctx.offset + 1,
|
|
24
|
+
};
|
|
25
|
+
ctx.stub.CommitOffset(req, (err, res) => {
|
|
26
|
+
if (err) {
|
|
27
|
+
console.error(`Error committing offset: ${err}`);
|
|
28
|
+
return reject(err);
|
|
29
|
+
}
|
|
30
|
+
// console.log(`Committed offset ${req.offset} for ${req.consumer_name}`);
|
|
31
|
+
ctx.committed = true;
|
|
32
|
+
resolve();
|
|
33
|
+
});
|
|
33
34
|
});
|
|
34
35
|
}
|
|
35
36
|
class Message {
|
|
36
37
|
constructor(protoMsg) {
|
|
37
|
-
this.offset = protoMsg.offset;
|
|
38
|
-
this.timestamp = protoMsg.timestamp;
|
|
39
|
-
this.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.headers = {};
|
|
45
|
-
}
|
|
46
|
-
const buf = protoMsg.payload;
|
|
47
|
-
const ptype = this.headers['payload-type'];
|
|
38
|
+
this.offset = typeof protoMsg.offset === 'string' ? parseInt(protoMsg.offset, 10) : protoMsg.offset;
|
|
39
|
+
this.timestamp = typeof protoMsg.timestamp === 'string' ? parseInt(protoMsg.timestamp, 10) : protoMsg.timestamp;
|
|
40
|
+
this._rawPayload = protoMsg.payload;
|
|
41
|
+
this._headers = protoMsg.headers || {};
|
|
42
|
+
}
|
|
43
|
+
get payload() {
|
|
44
|
+
const ptype = this._headers['payload-type'];
|
|
48
45
|
if (ptype === 'json') {
|
|
49
46
|
try {
|
|
50
|
-
|
|
47
|
+
return JSON.parse(this._rawPayload.toString('utf-8'));
|
|
51
48
|
}
|
|
52
49
|
catch (e) {
|
|
53
|
-
this.
|
|
50
|
+
return this._rawPayload;
|
|
54
51
|
}
|
|
55
52
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.payload = buf.toString('utf8');
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
this.payload = buf;
|
|
62
|
-
}
|
|
53
|
+
if (ptype === 'string') {
|
|
54
|
+
return this._rawPayload.toString('utf-8');
|
|
63
55
|
}
|
|
64
|
-
|
|
65
|
-
this.
|
|
56
|
+
if (ptype === 'bytes') {
|
|
57
|
+
return this._rawPayload;
|
|
66
58
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
59
|
+
// Fallback
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(this._rawPayload.toString('utf-8'));
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
return this._rawPayload;
|
|
74
65
|
}
|
|
75
66
|
}
|
|
67
|
+
get rawPayload() {
|
|
68
|
+
return this._rawPayload;
|
|
69
|
+
}
|
|
70
|
+
get headers() {
|
|
71
|
+
return this._headers;
|
|
72
|
+
}
|
|
76
73
|
}
|
|
77
74
|
exports.Message = Message;
|
package/dist/producer.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { PulseConfig } from './config';
|
|
2
|
-
export interface PublishResponse {
|
|
3
|
-
id: string;
|
|
4
|
-
offset: number | string;
|
|
5
|
-
}
|
|
6
1
|
export declare class Producer {
|
|
7
|
-
private config;
|
|
8
2
|
private client;
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
private config;
|
|
4
|
+
constructor(host?: string, port?: number);
|
|
5
|
+
private setupTopics;
|
|
6
|
+
send(topic: string, payload: any): Promise<void>;
|
|
7
|
+
close(): void;
|
|
11
8
|
}
|
package/dist/producer.js
CHANGED
|
@@ -1,25 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Producer = void 0;
|
|
4
|
+
const config_1 = require("./config");
|
|
4
5
|
const client_1 = require("./proto/client");
|
|
5
6
|
class Producer {
|
|
6
|
-
constructor(
|
|
7
|
-
this.config =
|
|
8
|
-
|
|
7
|
+
constructor(host, port) {
|
|
8
|
+
this.config = (0, config_1.getConfig)();
|
|
9
|
+
const h = host || this.config.broker.host;
|
|
10
|
+
const p = port || this.config.broker.grpc_port;
|
|
11
|
+
const address = `${h}:${p}`;
|
|
12
|
+
this.client = (0, client_1.createClient)(address);
|
|
13
|
+
this.setupTopics();
|
|
9
14
|
}
|
|
10
|
-
|
|
15
|
+
setupTopics() {
|
|
16
|
+
const topics = this.config.topics || [];
|
|
17
|
+
for (const topicCfg of topics) {
|
|
18
|
+
if (topicCfg.create_if_missing) {
|
|
19
|
+
const req = {
|
|
20
|
+
topic: topicCfg.name,
|
|
21
|
+
fifo: topicCfg.config?.fifo || false,
|
|
22
|
+
retention_bytes: topicCfg.config?.retention_bytes || 0,
|
|
23
|
+
};
|
|
24
|
+
this.client.CreateTopic(req, (err, res) => {
|
|
25
|
+
// Ignore errors (e.g. topic already exists)
|
|
26
|
+
if (err) {
|
|
27
|
+
// console.warn(`Failed to create topic ${topicCfg.name}:`, err.message);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async send(topic, payload) {
|
|
34
|
+
let data;
|
|
35
|
+
const headers = {};
|
|
36
|
+
if (Buffer.isBuffer(payload)) {
|
|
37
|
+
data = payload;
|
|
38
|
+
headers['payload-type'] = 'bytes';
|
|
39
|
+
}
|
|
40
|
+
else if (typeof payload === 'string') {
|
|
41
|
+
data = Buffer.from(payload, 'utf-8');
|
|
42
|
+
headers['payload-type'] = 'string';
|
|
43
|
+
}
|
|
44
|
+
else if (typeof payload === 'object') {
|
|
45
|
+
data = Buffer.from(JSON.stringify(payload), 'utf-8');
|
|
46
|
+
headers['payload-type'] = 'json';
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error('Payload must be Buffer, string, or object');
|
|
50
|
+
}
|
|
11
51
|
const req = {
|
|
12
52
|
topic,
|
|
13
|
-
payload:
|
|
14
|
-
headers
|
|
53
|
+
payload: data,
|
|
54
|
+
headers,
|
|
15
55
|
};
|
|
16
56
|
return new Promise((resolve, reject) => {
|
|
17
57
|
this.client.Publish(req, (err, res) => {
|
|
18
58
|
if (err)
|
|
19
59
|
return reject(err);
|
|
20
|
-
resolve(
|
|
60
|
+
resolve();
|
|
21
61
|
});
|
|
22
62
|
});
|
|
23
63
|
}
|
|
64
|
+
close() {
|
|
65
|
+
this.client.close();
|
|
66
|
+
}
|
|
24
67
|
}
|
|
25
68
|
exports.Producer = Producer;
|
package/dist/proto/client.d.ts
CHANGED
package/dist/proto/client.js
CHANGED
|
@@ -36,28 +36,42 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.loaded = exports.packageDefinition = void 0;
|
|
39
40
|
exports.createClient = createClient;
|
|
40
41
|
const fs_1 = __importDefault(require("fs"));
|
|
41
42
|
const path_1 = __importDefault(require("path"));
|
|
42
43
|
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
43
44
|
const protoLoader = __importStar(require("@grpc/proto-loader"));
|
|
44
|
-
// Prefer the proto
|
|
45
|
-
|
|
45
|
+
// Prefer the cached proto (downloaded from server), then bundled, then repo.
|
|
46
|
+
const cachedProto = path_1.default.resolve(__dirname, 'pulse.cached.proto');
|
|
46
47
|
const bundledProto = path_1.default.resolve(__dirname, 'pulse.proto');
|
|
47
48
|
const repoProto = path_1.default.resolve(__dirname, '../../../../internal/api/proto/pulse.proto');
|
|
49
|
+
// Try to update proto from server
|
|
50
|
+
// try {
|
|
51
|
+
// const host = process.env.PULSE_HOST || 'localhost';
|
|
52
|
+
// const port = process.env.PULSE_HTTP_PORT || '5555';
|
|
53
|
+
// const url = `http://${host}:${port}/proto`;
|
|
54
|
+
// // Use curl to download with a short timeout (1s)
|
|
55
|
+
// execSync(`curl -s -m 1 -o "${cachedProto}" "${url}"`, { stdio: 'ignore' });
|
|
56
|
+
// } catch (e) {
|
|
57
|
+
// // Failed to fetch, will fall back to existing files
|
|
58
|
+
// }
|
|
48
59
|
let PROTO_PATH = bundledProto;
|
|
49
|
-
if (
|
|
60
|
+
if (fs_1.default.existsSync(cachedProto)) {
|
|
61
|
+
PROTO_PATH = cachedProto;
|
|
62
|
+
}
|
|
63
|
+
else if (!fs_1.default.existsSync(bundledProto)) {
|
|
50
64
|
// fallback to repo proto for local development
|
|
51
65
|
PROTO_PATH = repoProto;
|
|
52
66
|
}
|
|
53
|
-
|
|
67
|
+
exports.packageDefinition = protoLoader.loadSync(PROTO_PATH, {
|
|
54
68
|
keepCase: true,
|
|
55
69
|
longs: String,
|
|
56
70
|
enums: String,
|
|
57
71
|
defaults: true,
|
|
58
72
|
oneofs: true,
|
|
59
73
|
});
|
|
60
|
-
|
|
74
|
+
exports.loaded = grpc.loadPackageDefinition(exports.packageDefinition).pulse.v1;
|
|
61
75
|
function createClient(address) {
|
|
62
|
-
return new loaded.PulseService(address, grpc.credentials.createInsecure());
|
|
76
|
+
return new exports.loaded.PulseService(address, grpc.credentials.createInsecure());
|
|
63
77
|
}
|
package/dist/proto/pulse.proto
CHANGED
|
@@ -56,9 +56,18 @@ message CreateTopicRequest {
|
|
|
56
56
|
string topic = 1;
|
|
57
57
|
bool fifo = 2;
|
|
58
58
|
int64 retention_bytes = 3;
|
|
59
|
-
int64 retention_time = 4;
|
|
59
|
+
int64 retention_time = 4;
|
|
60
|
+
int32 flush_threshold = 5;
|
|
61
|
+
int64 flush_interval = 6;
|
|
62
|
+
int64 segment_size = 7;
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
message CreateTopicResponse {
|
|
63
66
|
bool success = 1;
|
|
64
67
|
}
|
|
68
|
+
|
|
69
|
+
message ListTopicsRequest {}
|
|
70
|
+
|
|
71
|
+
message ListTopicsResponse {
|
|
72
|
+
repeated string topics = 1;
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pulse-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.1.0-main.0",
|
|
4
4
|
"description": "Pulse SDK for Node.js/TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,21 +22,22 @@
|
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"typescript": "^5.0.0",
|
|
24
24
|
"jest": "^29.0.0",
|
|
25
|
-
"ts-jest": "^29.0.0"
|
|
26
|
-
|
|
25
|
+
"ts-jest": "^29.0.0",
|
|
26
|
+
"@types/jest": "^29.0.0",
|
|
27
27
|
"@types/js-yaml": "^4.0.5"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@grpc/grpc-js": "^1.8.0",
|
|
31
31
|
"protobufjs": "^7.2.0",
|
|
32
|
-
"@grpc/proto-loader": "^0.7.0"
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
,
|
|
32
|
+
"@grpc/proto-loader": "^0.7.0",
|
|
33
|
+
"js-yaml": "^4.1.0"
|
|
34
|
+
},
|
|
36
35
|
"jest": {
|
|
37
36
|
"preset": "ts-jest",
|
|
38
37
|
"testEnvironment": "node",
|
|
39
|
-
"testMatch": [
|
|
38
|
+
"testMatch": [
|
|
39
|
+
"**/tests/**/*.test.ts"
|
|
40
|
+
],
|
|
40
41
|
"globalTeardown": "<rootDir>/tests/globalTeardown.js"
|
|
41
42
|
}
|
|
42
43
|
}
|