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/README.md
CHANGED
|
@@ -1,177 +1,116 @@
|
|
|
1
|
-
# Pulse SDK
|
|
1
|
+
# Pulse TypeScript SDK
|
|
2
2
|
|
|
3
|
-
TypeScript client for Pulse Broker
|
|
3
|
+
Official TypeScript client for [Pulse Broker](https://github.com/marcosrosa/pulse).
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
Install from npm (published package):
|
|
8
|
-
|
|
9
7
|
```bash
|
|
10
8
|
npm install pulse-sdk
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
Or install locally from the repository (for development):
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
To compile the TypeScript sources (optional for development/CI):
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
npm run build
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
Run the lightweight integration tests (a test gRPC server is started automatically):
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
npm test
|
|
29
|
-
```
|
|
30
|
-
|
|
31
11
|
## Configuration
|
|
32
12
|
|
|
33
|
-
The SDK
|
|
13
|
+
The SDK looks for a `pulse.yaml` (or `pulse.yml`) file in your project root. If not found, it defaults to `localhost:5555` (HTTP) and `localhost:5556` (gRPC).
|
|
34
14
|
|
|
35
|
-
|
|
36
|
-
2. Environment variables: `PULSE_GRPC_URL`, `PULSE_EVENT_TYPES` (comma-separated), `PULSE_CONSUMER_NAME`.
|
|
37
|
-
3. File: a `pulse.yml` or `pulse.yaml` file placed at the process working directory or in `~/.pulse/pulse.yml`.
|
|
38
|
-
|
|
39
|
-
The SDK exposes two helpers:
|
|
40
|
-
|
|
41
|
-
- `loadConfig(configPath?: string): PulseConfig` — reads `pulse.yml` / env and returns a merged config object.
|
|
42
|
-
- `initFromConfig(cfg: PulseConfig): Promise<void>` — calls the broker `CreateTopic` RPC for every topic declared in `cfg.topics`. This lets the SDK create any required topics automatically at startup (useful for tests or first-run).
|
|
43
|
-
|
|
44
|
-
Example `pulse.yml`:
|
|
15
|
+
### Example `pulse.yaml`
|
|
45
16
|
|
|
46
17
|
```yaml
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
18
|
+
# Connection Settings
|
|
19
|
+
broker:
|
|
20
|
+
host: "localhost"
|
|
21
|
+
http_port: 5555
|
|
22
|
+
grpc_port: 5556
|
|
23
|
+
timeout_ms: 5000
|
|
24
|
+
|
|
25
|
+
# Client Defaults
|
|
26
|
+
client:
|
|
27
|
+
id: "my-typescript-app"
|
|
28
|
+
auto_commit: true # Automatically commit offsets after successful processing
|
|
29
|
+
max_retries: 3
|
|
30
|
+
|
|
31
|
+
# Topic Configuration
|
|
52
32
|
topics:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
main().catch(console.error);
|
|
33
|
+
- name: "events"
|
|
34
|
+
create_if_missing: true
|
|
35
|
+
config:
|
|
36
|
+
fifo: false
|
|
37
|
+
retention_bytes: 1073741824 # 1GB
|
|
38
|
+
consume:
|
|
39
|
+
auto_commit: true
|
|
40
|
+
|
|
41
|
+
- name: "transactions"
|
|
42
|
+
create_if_missing: true
|
|
43
|
+
config:
|
|
44
|
+
fifo: true
|
|
45
|
+
consume:
|
|
46
|
+
auto_commit: false # Manual commit required
|
|
47
|
+
|
|
48
|
+
- name: "logs"
|
|
49
|
+
create_if_missing: true
|
|
50
|
+
config:
|
|
51
|
+
fifo: false
|
|
52
|
+
consume:
|
|
53
|
+
auto_commit: true
|
|
77
54
|
```
|
|
78
55
|
|
|
79
|
-
|
|
80
|
-
- `initFromConfig` calls the gRPC `CreateTopic` RPC. If the broker responds with an error for a topic that already exists, the SDK logs a warning and continues.
|
|
81
|
-
- Ensure the broker is running and reachable at `cfg.grpcUrl` before calling `initFromConfig`.
|
|
82
|
-
- The SDK bundles `pulse.proto` inside the package so consumers do not need to copy proto files into their projects.
|
|
56
|
+
## Usage
|
|
83
57
|
|
|
84
|
-
|
|
58
|
+
### Producer
|
|
85
59
|
|
|
86
|
-
|
|
60
|
+
You can send objects (automatically serialized to JSON), strings, or raw buffers.
|
|
87
61
|
|
|
88
|
-
```
|
|
89
|
-
import { Producer
|
|
62
|
+
```typescript
|
|
63
|
+
import { Producer } from 'pulse-sdk';
|
|
90
64
|
|
|
91
|
-
|
|
65
|
+
// Initialize (uses pulse.yaml or defaults)
|
|
66
|
+
// You can override settings: new Producer("10.0.0.1", 9090)
|
|
67
|
+
const producer = new Producer();
|
|
92
68
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
await producer.send(
|
|
69
|
+
async function main() {
|
|
70
|
+
// Send JSON
|
|
71
|
+
await producer.send("events", { type: "user_created", id: 123 });
|
|
96
72
|
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
consumer.on('events', (msg) => {
|
|
100
|
-
console.log('received', msg.payload);
|
|
101
|
-
});
|
|
102
|
-
await consumer.start('events', 'my-consumer');
|
|
103
|
-
```
|
|
73
|
+
// Send String
|
|
74
|
+
await producer.send("logs", "raw log line");
|
|
104
75
|
|
|
105
|
-
|
|
76
|
+
// Send Buffer
|
|
77
|
+
await producer.send("logs", Buffer.from("binary data"));
|
|
106
78
|
|
|
107
|
-
|
|
79
|
+
producer.close();
|
|
80
|
+
}
|
|
108
81
|
|
|
109
|
-
|
|
110
|
-
const cfg = { grpcUrl: '10.0.0.5:50052', eventTypes: ['events'] };
|
|
111
|
-
const producer = new Producer(cfg);
|
|
82
|
+
main();
|
|
112
83
|
```
|
|
113
84
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
- The SDK dynamically loads `pulse.proto` at runtime using `@grpc/proto-loader` and `@grpc/grpc-js`.
|
|
117
|
-
- The protobuf field `bytes payload` is used to carry the message body; the SDK serializes JSON payloads into that field by default.
|
|
118
|
-
- `Consumer.start()` opens a server stream (`Consume`) and dispatches incoming messages to handlers registered via `consumer.on(topic, handler)`.
|
|
119
|
-
|
|
120
|
-
## Examples
|
|
85
|
+
### Consumer
|
|
121
86
|
|
|
122
|
-
|
|
87
|
+
Use the `consumer` function to register message handlers, and `run` to start the loop.
|
|
123
88
|
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
await producer.send('events', { type: 'login', userId: 1 });
|
|
127
|
-
await producer.send('events', { type: 'logout', userId: 1 });
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
Consumer (simple processing):
|
|
131
|
-
|
|
132
|
-
```ts
|
|
133
|
-
const cfg = loadConfig();
|
|
134
|
-
const consumer = new Consumer(cfg);
|
|
89
|
+
```typescript
|
|
90
|
+
import { consumer, run, commit, Message } from 'pulse-sdk';
|
|
135
91
|
|
|
136
|
-
|
|
137
|
-
|
|
92
|
+
// Simple Consumer (uses auto_commit from config)
|
|
93
|
+
consumer("events", async (msg: Message) => {
|
|
94
|
+
console.log(`Received event:`, msg.payload);
|
|
95
|
+
// msg.payload is an object if JSON, string if String, else Buffer
|
|
138
96
|
});
|
|
139
97
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
- Environment variable: `PULSE_GROUPED=true|false`.
|
|
159
|
-
- Programmatically: pass `grouped` in the `PulseConfig` passed to `Producer`/`Consumer`.
|
|
160
|
-
- **Default:** `grouped` defaults to `true`.
|
|
161
|
-
- **Grouped=false behavior:** When `grouped` is `false`, the SDK will ensure consumers use unique consumer IDs (if you pass the default client name), so multiple consumers in the same process each receive all messages independently (useful for testing or when you want duplicate consumption).
|
|
162
|
-
- **Example `pulse.yml` entry:**
|
|
163
|
-
|
|
164
|
-
```yaml
|
|
165
|
-
grpcUrl: localhost:5556
|
|
166
|
-
grouped: true
|
|
98
|
+
// Manual Commit Consumer
|
|
99
|
+
// Override config params directly in the options object
|
|
100
|
+
consumer("transactions", async (msg: Message) => {
|
|
101
|
+
try {
|
|
102
|
+
await processPayment(msg.payload);
|
|
103
|
+
await commit(); // Manually commit offset
|
|
104
|
+
console.log(`Processed transaction ${msg.offset}`);
|
|
105
|
+
} catch (e) {
|
|
106
|
+
console.error(`Failed to process: ${e}`);
|
|
107
|
+
}
|
|
108
|
+
}, { autoCommit: false });
|
|
109
|
+
|
|
110
|
+
// Start all consumers
|
|
111
|
+
run();
|
|
112
|
+
|
|
113
|
+
async function processPayment(data: any) {
|
|
114
|
+
// ... implementation
|
|
115
|
+
}
|
|
167
116
|
```
|
|
168
|
-
|
|
169
|
-
The test-suite includes integration tests that validate both `grouped=true` and `grouped=false` behaviour.
|
|
170
|
-
|
|
171
|
-
## Contributing
|
|
172
|
-
|
|
173
|
-
Please open a PR with tests. The existing tests validate basic Producer/Consumer behaviour.
|
|
174
|
-
|
|
175
|
-
---
|
|
176
|
-
|
|
177
|
-
If you want this README expanded to mirror the Python SDK more closely (topic-level config, manual offset commits, longer examples), tell me which sections to add and I will update it.
|
package/dist/config.d.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
|
+
export interface BrokerConfig {
|
|
2
|
+
host: string;
|
|
3
|
+
http_port: number;
|
|
4
|
+
grpc_port: number;
|
|
5
|
+
timeout_ms: number;
|
|
6
|
+
}
|
|
7
|
+
export interface ClientConfig {
|
|
8
|
+
id: string;
|
|
9
|
+
auto_commit: boolean;
|
|
10
|
+
max_retries: number;
|
|
11
|
+
}
|
|
1
12
|
export interface TopicConfig {
|
|
2
13
|
name: string;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
14
|
+
create_if_missing?: boolean;
|
|
15
|
+
config?: {
|
|
16
|
+
fifo?: boolean;
|
|
17
|
+
retention_bytes?: number;
|
|
18
|
+
};
|
|
6
19
|
}
|
|
7
20
|
export interface PulseConfig {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
grouped?: boolean;
|
|
12
|
-
autoCommit?: boolean;
|
|
13
|
-
topics?: TopicConfig[];
|
|
21
|
+
broker: BrokerConfig;
|
|
22
|
+
client: ClientConfig;
|
|
23
|
+
topics: TopicConfig[];
|
|
14
24
|
}
|
|
15
25
|
export declare function loadConfig(configPath?: string): PulseConfig;
|
|
16
|
-
export declare function
|
|
26
|
+
export declare function getConfig(): PulseConfig;
|
package/dist/config.js
CHANGED
|
@@ -4,78 +4,62 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.loadConfig = loadConfig;
|
|
7
|
-
exports.
|
|
7
|
+
exports.getConfig = getConfig;
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const os_1 = __importDefault(require("os"));
|
|
11
11
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
const DEFAULT_CONFIG = {
|
|
13
|
+
broker: {
|
|
14
|
+
host: 'localhost',
|
|
15
|
+
http_port: 5555,
|
|
16
|
+
grpc_port: 5556,
|
|
17
|
+
timeout_ms: 5000,
|
|
18
|
+
},
|
|
19
|
+
client: {
|
|
20
|
+
id: 'typescript-client',
|
|
21
|
+
auto_commit: true,
|
|
22
|
+
max_retries: 3,
|
|
23
|
+
},
|
|
24
|
+
topics: [],
|
|
18
25
|
};
|
|
19
26
|
function loadConfig(configPath) {
|
|
20
27
|
const candidates = [
|
|
21
28
|
configPath,
|
|
22
|
-
path_1.default.resolve(process.cwd(), 'pulse.yml'),
|
|
23
29
|
path_1.default.resolve(process.cwd(), 'pulse.yaml'),
|
|
30
|
+
path_1.default.resolve(process.cwd(), 'pulse.yml'),
|
|
24
31
|
path_1.default.join(os_1.default.homedir(), '.pulse', 'pulse.yml'),
|
|
25
32
|
].filter(Boolean);
|
|
26
33
|
let fileCfg = {};
|
|
27
34
|
for (const p of candidates) {
|
|
28
35
|
if (p && fs_1.default.existsSync(p)) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
try {
|
|
37
|
+
const raw = fs_1.default.readFileSync(p, 'utf8');
|
|
38
|
+
fileCfg = js_yaml_1.default.load(raw) || {};
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
console.warn(`Failed to load config from ${p}:`, e);
|
|
43
|
+
}
|
|
33
44
|
}
|
|
34
45
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// Ensure eventTypes array exists
|
|
48
|
-
if (!merged.eventTypes)
|
|
49
|
-
merged.eventTypes = DEFAULTS.eventTypes;
|
|
50
|
-
if (merged.grouped === undefined)
|
|
51
|
-
merged.grouped = true;
|
|
52
|
-
if (merged.autoCommit === undefined)
|
|
53
|
-
merged.autoCommit = true;
|
|
54
|
-
return merged;
|
|
46
|
+
// Deep merge defaults with file config
|
|
47
|
+
const config = JSON.parse(JSON.stringify(DEFAULT_CONFIG)); // Deep copy
|
|
48
|
+
if (fileCfg.broker) {
|
|
49
|
+
config.broker = { ...config.broker, ...fileCfg.broker };
|
|
50
|
+
}
|
|
51
|
+
if (fileCfg.client) {
|
|
52
|
+
config.client = { ...config.client, ...fileCfg.client };
|
|
53
|
+
}
|
|
54
|
+
if (fileCfg.topics) {
|
|
55
|
+
config.topics = fileCfg.topics;
|
|
56
|
+
}
|
|
57
|
+
return config;
|
|
55
58
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (!
|
|
59
|
-
|
|
60
|
-
const client = (0, client_1.createClient)(cfg.grpcUrl);
|
|
61
|
-
for (const t of cfg.topics) {
|
|
62
|
-
const req = {
|
|
63
|
-
topic: t.name || t['name'],
|
|
64
|
-
fifo: !!t.fifo,
|
|
65
|
-
retention_bytes: t.retention_bytes || 0,
|
|
66
|
-
retention_time: t.retention_time || 0,
|
|
67
|
-
};
|
|
68
|
-
await new Promise((resolve, reject) => {
|
|
69
|
-
client.CreateTopic(req, (err, res) => {
|
|
70
|
-
if (err) {
|
|
71
|
-
// If topic exists the server may return an error; log and continue
|
|
72
|
-
console.warn('CreateTopic error for', req.topic, err.message || err);
|
|
73
|
-
resolve();
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
resolve();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
});
|
|
59
|
+
let _config = null;
|
|
60
|
+
function getConfig() {
|
|
61
|
+
if (!_config) {
|
|
62
|
+
_config = loadConfig();
|
|
80
63
|
}
|
|
64
|
+
return _config;
|
|
81
65
|
}
|
package/dist/consumer.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
on(eventType: string, handler: EventHandler): void;
|
|
10
|
-
start(topic?: string, consumerName?: string): Promise<void>;
|
|
11
|
-
close(): void;
|
|
1
|
+
import { Message, commit } from './message';
|
|
2
|
+
export { commit, Message };
|
|
3
|
+
interface ConsumerOptions {
|
|
4
|
+
host?: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
consumerGroup?: string;
|
|
7
|
+
autoCommit?: boolean;
|
|
8
|
+
grouped?: boolean;
|
|
12
9
|
}
|
|
10
|
+
export declare function stop(): void;
|
|
11
|
+
export declare function consumer(topic: string, handler: (msg: Message) => void | Promise<void>, options?: ConsumerOptions): void;
|
|
12
|
+
export declare function run(): Promise<void>;
|