service-bridge 1.7.0-dev.34 → 1.7.0-dev.50
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 +17 -36
- package/dist/index.js +809 -393
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,7 +116,7 @@ const sb = servicebridge(
|
|
|
116
116
|
process.env.SERVICEBRIDGE_SERVICE_KEY!,
|
|
117
117
|
);
|
|
118
118
|
|
|
119
|
-
const result = await sb.rpc<{ ok: boolean; txId: string }>("
|
|
119
|
+
const result = await sb.rpc<{ ok: boolean; txId: string }>("payment.charge", {
|
|
120
120
|
orderId: "ord_42",
|
|
121
121
|
amount: 4990,
|
|
122
122
|
});
|
|
@@ -171,7 +171,7 @@ await payments.serve({ host: "localhost" });
|
|
|
171
171
|
const orders = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!);
|
|
172
172
|
|
|
173
173
|
// Call payments, then publish event
|
|
174
|
-
const charge = await orders.rpc<{ ok: boolean; txId: string }>("
|
|
174
|
+
const charge = await orders.rpc<{ ok: boolean; txId: string }>("payment.charge", {
|
|
175
175
|
orderId: "ord_42",
|
|
176
176
|
amount: 4990,
|
|
177
177
|
});
|
|
@@ -203,8 +203,8 @@ await notifications.serve({ host: "localhost" });
|
|
|
203
203
|
// --- Orchestrate as a workflow ---
|
|
204
204
|
|
|
205
205
|
await orders.workflow("order.fulfillment", [
|
|
206
|
-
{ id: "reserve", type: "rpc", ref: "inventory
|
|
207
|
-
{ id: "charge", type: "rpc", ref: "
|
|
206
|
+
{ id: "reserve", type: "rpc", ref: "inventory.reserve" },
|
|
207
|
+
{ id: "charge", type: "rpc", ref: "payment.charge", deps: ["reserve"] },
|
|
208
208
|
{ id: "wait_dlv", type: "event_wait", ref: "shipping.delivered", deps: ["charge"] },
|
|
209
209
|
{ id: "notify", type: "event", ref: "orders.fulfilled", deps: ["wait_dlv"] },
|
|
210
210
|
]);
|
|
@@ -324,16 +324,9 @@ type WorkerTLSOpts = {
|
|
|
324
324
|
rpc<T = unknown>(fn: string, payload?: unknown, opts?: RpcOpts): Promise<T>
|
|
325
325
|
```
|
|
326
326
|
|
|
327
|
-
Calls a registered RPC handler on another
|
|
327
|
+
Calls a registered RPC handler on another worker. Direct gRPC path, no proxy.
|
|
328
328
|
|
|
329
|
-
**Function name
|
|
330
|
-
|
|
331
|
-
| Format | Example | When to use |
|
|
332
|
-
|---|---|---|
|
|
333
|
-
| Plain name | `"charge"` | Function name is globally unique across all services. Resolved automatically via service discovery. |
|
|
334
|
-
| Canonical name | `"payments/charge"` | Multiple services expose a function with the same plain name, or you want to be explicit about the target service. |
|
|
335
|
-
|
|
336
|
-
Both formats are interchangeable when the name is unique globally. Canonical format is recommended in production for clarity and to avoid ambiguity as your service count grows.
|
|
329
|
+
**Function name** — `fn` is a single **global function name** (the same string passed to `handleRpc` on the callee), e.g. `payment.charge` or `user.get`. It must be unique in the catalog and **must not contain `/`**.
|
|
337
330
|
|
|
338
331
|
`RpcOpts`:
|
|
339
332
|
|
|
@@ -347,11 +340,9 @@ Both formats are interchangeable when the name is unique globally. Canonical for
|
|
|
347
340
|
| `mode` | `"direct" \| "proxy"` | Transport mode. `"direct"` (default) connects directly to the worker. `"proxy"` routes through the control plane when direct connection is unavailable. |
|
|
348
341
|
|
|
349
342
|
```ts
|
|
350
|
-
|
|
351
|
-
const user = await sb.rpc<{ id: string; name: string }>("get", { id: "u_1" });
|
|
343
|
+
const user = await sb.rpc<{ id: string; name: string }>("user.get", { id: "u_1" });
|
|
352
344
|
|
|
353
|
-
|
|
354
|
-
const user = await sb.rpc<{ id: string; name: string }>("users/get", { id: "u_1" }, {
|
|
345
|
+
const user2 = await sb.rpc<{ id: string; name: string }>("user.get", { id: "u_1" }, {
|
|
355
346
|
timeout: 5000,
|
|
356
347
|
retries: 2,
|
|
357
348
|
});
|
|
@@ -391,16 +382,6 @@ await sb.event("orders.created", { orderId: "ord_42" }, {
|
|
|
391
382
|
|
|
392
383
|
---
|
|
393
384
|
|
|
394
|
-
### `publishEvent(topic, payload?, opts?)`
|
|
395
|
-
|
|
396
|
-
```ts
|
|
397
|
-
publishEvent(topic: string, payload?: unknown, opts?: PublishEventOpts): Promise<string>
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
Publishes an event via the established worker session stream. Requires an active worker session — call after `serve()`. Resolves with `messageId` once the server confirms with `publish_ack`. Times out after 30 s if no ack. Use `event()` when not serving (e.g. caller-only services); use `publishEvent()` from within a worker for lower-latency publishing over the existing session.
|
|
401
|
-
|
|
402
|
-
---
|
|
403
|
-
|
|
404
385
|
### `job(target, opts)`
|
|
405
386
|
|
|
406
387
|
```ts
|
|
@@ -421,7 +402,7 @@ Registers a scheduled or delayed job.
|
|
|
421
402
|
| `retryPolicyJson` | `string` | Retry policy JSON string. |
|
|
422
403
|
|
|
423
404
|
```ts
|
|
424
|
-
await sb.job("billing
|
|
405
|
+
await sb.job("billing.collect", {
|
|
425
406
|
cron: "0 * * * *",
|
|
426
407
|
timezone: "UTC",
|
|
427
408
|
via: "rpc",
|
|
@@ -466,8 +447,8 @@ interface WorkflowOpts {
|
|
|
466
447
|
|
|
467
448
|
```ts
|
|
468
449
|
await sb.workflow("order.fulfillment", [
|
|
469
|
-
{ id: "reserve", type: "rpc", ref: "inventory
|
|
470
|
-
{ id: "charge", type: "rpc", ref: "
|
|
450
|
+
{ id: "reserve", type: "rpc", ref: "inventory.reserve" },
|
|
451
|
+
{ id: "charge", type: "rpc", ref: "payment.charge", deps: ["reserve"] },
|
|
471
452
|
{ id: "wait_5m", type: "sleep", durationMs: 300_000, deps: ["charge"] },
|
|
472
453
|
{ id: "notify", type: "event", ref: "orders.fulfilled", deps: ["wait_5m"] },
|
|
473
454
|
]);
|
|
@@ -554,7 +535,7 @@ Registers an RPC handler. Chainable.
|
|
|
554
535
|
| `allowedCallers` | `string[]` | Allow-list of caller service names. |
|
|
555
536
|
|
|
556
537
|
```ts
|
|
557
|
-
sb.handleRpc("ai
|
|
538
|
+
sb.handleRpc("ai.generate", async (payload: { prompt: string }, ctx) => {
|
|
558
539
|
await ctx?.stream.write({ token: "Hello" }, "output");
|
|
559
540
|
await ctx?.stream.write({ token: " world" }, "output");
|
|
560
541
|
return { text: "Hello world" };
|
|
@@ -830,7 +811,7 @@ app.use(servicebridgeMiddleware({
|
|
|
830
811
|
}));
|
|
831
812
|
|
|
832
813
|
app.get("/users/:id", async (req, res) => {
|
|
833
|
-
const user = await req.servicebridge.rpc("
|
|
814
|
+
const user = await req.servicebridge.rpc("user.get", { id: req.params.id });
|
|
834
815
|
res.json(user);
|
|
835
816
|
});
|
|
836
817
|
```
|
|
@@ -886,7 +867,7 @@ await app.register(servicebridgePlugin, {
|
|
|
886
867
|
});
|
|
887
868
|
|
|
888
869
|
app.get("/users/:id", wrapHandler(async (request, reply) => {
|
|
889
|
-
const user = await request.servicebridge.rpc("
|
|
870
|
+
const user = await request.servicebridge.rpc("user.get", {
|
|
890
871
|
id: (request.params as any).id,
|
|
891
872
|
});
|
|
892
873
|
return reply.send(user);
|
|
@@ -983,7 +964,7 @@ const sb = servicebridge(
|
|
|
983
964
|
import { servicebridge, ServiceBridgeError } from "service-bridge";
|
|
984
965
|
|
|
985
966
|
try {
|
|
986
|
-
await sb.rpc("
|
|
967
|
+
await sb.rpc("payment.charge", { orderId: "ord_1" });
|
|
987
968
|
} catch (e) {
|
|
988
969
|
if (e instanceof ServiceBridgeError) {
|
|
989
970
|
console.error(e.component, e.operation, e.severity, e.retryable, e.code);
|
|
@@ -1171,12 +1152,12 @@ session.onConfigPush({
|
|
|
1171
1152
|
'payment-svc': { mode: 'proxy', fallbackPolicy: 'fallback_to_direct' },
|
|
1172
1153
|
},
|
|
1173
1154
|
functionOverrides: {
|
|
1174
|
-
'payment
|
|
1155
|
+
'payment.charge': { mode: 'proxy', timeoutMs: 5000 },
|
|
1175
1156
|
},
|
|
1176
1157
|
});
|
|
1177
1158
|
|
|
1178
1159
|
// Разрешить транспорт для функции
|
|
1179
|
-
const mode = session.resolveTransportMode('payment
|
|
1160
|
+
const mode = session.resolveTransportMode('payment.charge'); // 'proxy'
|
|
1180
1161
|
```
|
|
1181
1162
|
|
|
1182
1163
|
### Все события сессии
|