pulse-sdk 0.0.1 → 0.0.3
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 +50 -3
- package/dist/config.d.ts +10 -1
- package/dist/config.js +67 -3
- package/dist/proto/client.js +10 -1
- package/dist/proto/pulse.proto +64 -0
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -28,11 +28,58 @@ Run the lightweight integration tests (a test gRPC server is started automatical
|
|
|
28
28
|
npm test
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
## Configuration
|
|
31
|
+
## Configuration
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
The SDK supports configuration via (in priority order):
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
1. Programmatic: pass a `PulseConfig` object when creating `Producer` or `Consumer`.
|
|
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`:
|
|
45
|
+
|
|
46
|
+
```yaml
|
|
47
|
+
grpcUrl: localhost:5556
|
|
48
|
+
eventTypes:
|
|
49
|
+
- events
|
|
50
|
+
- transactions
|
|
51
|
+
consumerName: my-consumer
|
|
52
|
+
topics:
|
|
53
|
+
- name: events
|
|
54
|
+
fifo: false
|
|
55
|
+
- name: transactions
|
|
56
|
+
fifo: true
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Usage example (auto-create topics then start):
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { loadConfig, initFromConfig, Producer, Consumer } from 'pulse-sdk';
|
|
63
|
+
|
|
64
|
+
async function main() {
|
|
65
|
+
const cfg = loadConfig(); // reads pulse.yml or environment
|
|
66
|
+
await initFromConfig(cfg); // create topics listed in cfg.topics (no-op if none)
|
|
67
|
+
|
|
68
|
+
const producer = new Producer(cfg);
|
|
69
|
+
await producer.send('events', { type: 'user.created', id: 1 });
|
|
70
|
+
|
|
71
|
+
const consumer = new Consumer(cfg);
|
|
72
|
+
consumer.on('events', (msg) => console.log('received', msg.payload));
|
|
73
|
+
await consumer.start('events', cfg.consumerName || 'default');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
main().catch(console.error);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Notes:
|
|
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.
|
|
36
83
|
|
|
37
84
|
## Quick Start
|
|
38
85
|
|
package/dist/config.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
export interface TopicConfig {
|
|
2
|
+
name: string;
|
|
3
|
+
fifo?: boolean;
|
|
4
|
+
retention_bytes?: number;
|
|
5
|
+
retention_time?: number;
|
|
6
|
+
}
|
|
1
7
|
export interface PulseConfig {
|
|
2
8
|
grpcUrl: string;
|
|
3
9
|
eventTypes: string[];
|
|
10
|
+
consumerName?: string;
|
|
11
|
+
topics?: TopicConfig[];
|
|
4
12
|
}
|
|
5
|
-
export declare function loadConfig(
|
|
13
|
+
export declare function loadConfig(configPath?: string): PulseConfig;
|
|
14
|
+
export declare function initFromConfig(cfg: PulseConfig): Promise<void>;
|
package/dist/config.js
CHANGED
|
@@ -1,7 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.loadConfig = loadConfig;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
exports.initFromConfig = initFromConfig;
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const os_1 = __importDefault(require("os"));
|
|
11
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
12
|
+
const client_1 = require("./proto/client");
|
|
13
|
+
const DEFAULTS = {
|
|
14
|
+
grpcUrl: 'localhost:50052',
|
|
15
|
+
eventTypes: ['events'],
|
|
16
|
+
};
|
|
17
|
+
function loadConfig(configPath) {
|
|
18
|
+
const candidates = [
|
|
19
|
+
configPath,
|
|
20
|
+
path_1.default.resolve(process.cwd(), 'pulse.yml'),
|
|
21
|
+
path_1.default.resolve(process.cwd(), 'pulse.yaml'),
|
|
22
|
+
path_1.default.join(os_1.default.homedir(), '.pulse', 'pulse.yml'),
|
|
23
|
+
].filter(Boolean);
|
|
24
|
+
let fileCfg = {};
|
|
25
|
+
for (const p of candidates) {
|
|
26
|
+
if (p && fs_1.default.existsSync(p)) {
|
|
27
|
+
const raw = fs_1.default.readFileSync(p, 'utf8');
|
|
28
|
+
const parsed = js_yaml_1.default.load(raw);
|
|
29
|
+
fileCfg = parsed || {};
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const envCfg = {};
|
|
34
|
+
if (process.env.PULSE_GRPC_URL)
|
|
35
|
+
envCfg.grpcUrl = process.env.PULSE_GRPC_URL;
|
|
36
|
+
if (process.env.PULSE_EVENT_TYPES)
|
|
37
|
+
envCfg.eventTypes = process.env.PULSE_EVENT_TYPES.split(',');
|
|
38
|
+
if (process.env.PULSE_CONSUMER_NAME)
|
|
39
|
+
envCfg.consumerName = process.env.PULSE_CONSUMER_NAME;
|
|
40
|
+
const merged = Object.assign({}, DEFAULTS, fileCfg, envCfg);
|
|
41
|
+
// Ensure eventTypes array exists
|
|
42
|
+
if (!merged.eventTypes)
|
|
43
|
+
merged.eventTypes = DEFAULTS.eventTypes;
|
|
44
|
+
return merged;
|
|
45
|
+
}
|
|
46
|
+
// Initialize topics from config using gRPC CreateTopic RPC.
|
|
47
|
+
async function initFromConfig(cfg) {
|
|
48
|
+
if (!cfg.topics || cfg.topics.length === 0)
|
|
49
|
+
return;
|
|
50
|
+
const client = (0, client_1.createClient)(cfg.grpcUrl);
|
|
51
|
+
for (const t of cfg.topics) {
|
|
52
|
+
const req = {
|
|
53
|
+
topic: t.name || t['name'],
|
|
54
|
+
fifo: !!t.fifo,
|
|
55
|
+
retention_bytes: t.retention_bytes || 0,
|
|
56
|
+
retention_time: t.retention_time || 0,
|
|
57
|
+
};
|
|
58
|
+
await new Promise((resolve, reject) => {
|
|
59
|
+
client.CreateTopic(req, (err, res) => {
|
|
60
|
+
if (err) {
|
|
61
|
+
// If topic exists the server may return an error; log and continue
|
|
62
|
+
console.warn('CreateTopic error for', req.topic, err.message || err);
|
|
63
|
+
resolve();
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
resolve();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
7
71
|
}
|
package/dist/proto/client.js
CHANGED
|
@@ -37,10 +37,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.createClient = createClient;
|
|
40
|
+
const fs_1 = __importDefault(require("fs"));
|
|
40
41
|
const path_1 = __importDefault(require("path"));
|
|
41
42
|
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
42
43
|
const protoLoader = __importStar(require("@grpc/proto-loader"));
|
|
43
|
-
|
|
44
|
+
// Prefer the proto bundled with the package (dist/proto/pulse.proto).
|
|
45
|
+
// In development (when running from source), fall back to the repository proto.
|
|
46
|
+
const bundledProto = path_1.default.resolve(__dirname, 'pulse.proto');
|
|
47
|
+
const repoProto = path_1.default.resolve(__dirname, '../../../../internal/api/proto/pulse.proto');
|
|
48
|
+
let PROTO_PATH = bundledProto;
|
|
49
|
+
if (!fs_1.default.existsSync(bundledProto)) {
|
|
50
|
+
// fallback to repo proto for local development
|
|
51
|
+
PROTO_PATH = repoProto;
|
|
52
|
+
}
|
|
44
53
|
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
|
|
45
54
|
keepCase: true,
|
|
46
55
|
longs: String,
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package pulse.v1;
|
|
4
|
+
|
|
5
|
+
option go_package = "pulse/pkg/proto";
|
|
6
|
+
|
|
7
|
+
service PulseService {
|
|
8
|
+
// Publish sends a message to a topic.
|
|
9
|
+
rpc Publish(PublishRequest) returns (PublishResponse);
|
|
10
|
+
|
|
11
|
+
// Consume reads messages from a topic as a stream.
|
|
12
|
+
rpc Consume(ConsumeRequest) returns (stream ConsumeResponse);
|
|
13
|
+
|
|
14
|
+
// CommitOffset commits the offset for a consumer group.
|
|
15
|
+
rpc CommitOffset(CommitOffsetRequest) returns (CommitOffsetResponse);
|
|
16
|
+
|
|
17
|
+
// CreateTopic creates a new topic.
|
|
18
|
+
rpc CreateTopic(CreateTopicRequest) returns (CreateTopicResponse);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
message PublishRequest {
|
|
22
|
+
string topic = 1;
|
|
23
|
+
bytes payload = 2;
|
|
24
|
+
map<string, string> headers = 3;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
message PublishResponse {
|
|
28
|
+
string id = 1;
|
|
29
|
+
uint64 offset = 2;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
message ConsumeRequest {
|
|
33
|
+
string topic = 1;
|
|
34
|
+
string consumer_name = 2;
|
|
35
|
+
uint64 offset = 3; // Optional: start from specific offset. If 0, continues from last committed.
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
message ConsumeResponse {
|
|
39
|
+
uint64 offset = 1;
|
|
40
|
+
int64 timestamp = 2;
|
|
41
|
+
bytes payload = 3;
|
|
42
|
+
map<string, string> headers = 4;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
message CommitOffsetRequest {
|
|
46
|
+
string topic = 1;
|
|
47
|
+
string consumer_name = 2;
|
|
48
|
+
uint64 offset = 3;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
message CommitOffsetResponse {
|
|
52
|
+
bool success = 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
message CreateTopicRequest {
|
|
56
|
+
string topic = 1;
|
|
57
|
+
bool fifo = 2;
|
|
58
|
+
int64 retention_bytes = 3;
|
|
59
|
+
int64 retention_time = 4; // Nanoseconds
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
message CreateTopicResponse {
|
|
63
|
+
bool success = 1;
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pulse-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Pulse SDK for Node.js/TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
+
"postbuild": "node scripts/copy-proto.js",
|
|
9
10
|
"test": "jest"
|
|
10
11
|
},
|
|
11
12
|
"author": "",
|
|
@@ -22,12 +23,14 @@
|
|
|
22
23
|
"typescript": "^5.0.0",
|
|
23
24
|
"jest": "^29.0.0",
|
|
24
25
|
"ts-jest": "^29.0.0",
|
|
25
|
-
"@types/jest": "^29.0.0"
|
|
26
|
+
"@types/jest": "^29.0.0",
|
|
27
|
+
"@types/js-yaml": "^4.0.5"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|
|
28
30
|
"@grpc/grpc-js": "^1.8.0",
|
|
29
31
|
"protobufjs": "^7.2.0",
|
|
30
|
-
"@grpc/proto-loader": "^0.7.0"
|
|
32
|
+
"@grpc/proto-loader": "^0.7.0",
|
|
33
|
+
"js-yaml": "^4.1.0"
|
|
31
34
|
},
|
|
32
35
|
"jest": {
|
|
33
36
|
"preset": "ts-jest",
|