service-bridge 1.1.1-dev.31 → 1.6.0-dev.33
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 +84 -6
- package/dist/index.js +483 -384
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# service-bridge
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/service-bridge)
|
|
6
6
|
[](../LICENSE)
|
|
7
7
|
[](https://www.typescriptlang.org/)
|
|
8
8
|
[](https://nodejs.org/)
|
|
@@ -218,7 +218,7 @@ Every step above — RPC, event publish, event delivery, workflow execution —
|
|
|
218
218
|
|
|
219
219
|
### Communication
|
|
220
220
|
- **Direct RPC** — zero-hop gRPC calls with retries, deadlines, and mTLS identity
|
|
221
|
-
- **Durable Events** — fan-out delivery, at-least-once guarantees, retries, DLQ, replay, idempotency
|
|
221
|
+
- **Durable Events** — fan-out delivery, guaranteed delivery (RabbitMQ-style), at-least-once guarantees, retries, DLQ, replay, idempotency. If a consumer is offline, the message waits in the server-side queue and is dispatched the moment the consumer reconnects — no retry budget consumed while waiting.
|
|
222
222
|
- **Realtime Streams** — live chunks with replay for AI/progress/log streaming
|
|
223
223
|
- **Service Discovery** — automatic endpoint resolution and round-robin balancing
|
|
224
224
|
- **HTTP Middleware** — Express and Fastify instrumentation with automatic trace propagation
|
|
@@ -335,6 +335,7 @@ Calls a registered RPC handler on another service. Direct gRPC path, no proxy.
|
|
|
335
335
|
| `retryDelay` | `number` | Base retry delay override. |
|
|
336
336
|
| `traceId` | `string` | Explicit trace id. |
|
|
337
337
|
| `parentSpanId` | `string` | Explicit parent span id. |
|
|
338
|
+
| `mode` | `"direct" \| "proxy"` | Transport mode. `"direct"` (default) connects directly to the worker. `"proxy"` routes through the control plane when direct connection is unavailable. |
|
|
338
339
|
|
|
339
340
|
```ts
|
|
340
341
|
const user = await sb.rpc<{ id: string; name: string }>("users/get", { id: "u_1" }, {
|
|
@@ -347,6 +348,8 @@ const user = await sb.rpc<{ id: string; name: string }>("users/get", { id: "u_1"
|
|
|
347
348
|
each attempt has a hard local timeout, retries are finite (`retries + 1` total attempts),
|
|
348
349
|
and after the final failed attempt the root RPC span is closed with `error`.
|
|
349
350
|
|
|
351
|
+
Retry delay uses exponential backoff: `retryDelay * 2^(attempt-1)`.
|
|
352
|
+
|
|
350
353
|
---
|
|
351
354
|
|
|
352
355
|
### `event(topic, payload?, opts?)`
|
|
@@ -407,10 +410,10 @@ await sb.job("billing/collect", {
|
|
|
407
410
|
### `workflow(name, steps, opts?)`
|
|
408
411
|
|
|
409
412
|
```ts
|
|
410
|
-
workflow(name: string, steps: WorkflowStep[], opts?: WorkflowOpts): Promise<
|
|
413
|
+
workflow(name: string, steps: WorkflowStep[], opts?: WorkflowOpts): Promise<string>
|
|
411
414
|
```
|
|
412
415
|
|
|
413
|
-
Registers (or updates) a workflow definition as a DAG of typed steps.
|
|
416
|
+
Registers (or updates) a workflow definition as a DAG of typed steps. Returns the workflow name.
|
|
414
417
|
|
|
415
418
|
`WorkflowStep`:
|
|
416
419
|
|
|
@@ -471,6 +474,12 @@ An alternative to scheduling via `job(target, { via: "workflow" })` — triggers
|
|
|
471
474
|
|
|
472
475
|
Returns `{ runId, traceId }`. Use `traceId` with `watchRun()` to observe execution in real time.
|
|
473
476
|
|
|
477
|
+
`RunWorkflowOpts`:
|
|
478
|
+
|
|
479
|
+
| Option | Type | Description |
|
|
480
|
+
|---|---|---|
|
|
481
|
+
| `traceId` | `string` | Override trace ID for this workflow run. |
|
|
482
|
+
|
|
474
483
|
```ts
|
|
475
484
|
const { runId, traceId } = await sb.runWorkflow("user.onboarding", { userId: "u_123" });
|
|
476
485
|
```
|
|
@@ -503,6 +512,14 @@ handleRpc(
|
|
|
503
512
|
|
|
504
513
|
Registers an RPC handler. Chainable.
|
|
505
514
|
|
|
515
|
+
`RpcContext`:
|
|
516
|
+
|
|
517
|
+
| Field | Type | Description |
|
|
518
|
+
|---|---|---|
|
|
519
|
+
| `traceId` | `string` | Current trace ID. |
|
|
520
|
+
| `spanId` | `string` | Current span ID. |
|
|
521
|
+
| `stream` | `StreamWriter` | Real-time stream writer. |
|
|
522
|
+
|
|
506
523
|
`HandleRpcOpts`:
|
|
507
524
|
|
|
508
525
|
| Option | Type | Description |
|
|
@@ -521,6 +538,13 @@ sb.handleRpc("ai/generate", async (payload: { prompt: string }, ctx) => {
|
|
|
521
538
|
});
|
|
522
539
|
```
|
|
523
540
|
|
|
541
|
+
`StreamWriter`:
|
|
542
|
+
|
|
543
|
+
| Method | Signature | Description |
|
|
544
|
+
|---|---|---|
|
|
545
|
+
| `write` | `write(data: unknown, key?: string): Promise<void>` | Append a real-time chunk to the run stream. |
|
|
546
|
+
| `end` | `end(key?: string): Promise<void>` | No-op placeholder for API symmetry (lifecycle managed by runtime). |
|
|
547
|
+
|
|
524
548
|
---
|
|
525
549
|
|
|
526
550
|
### `handleEvent(pattern, handler, opts?)`
|
|
@@ -547,10 +571,18 @@ Registers an event consumer handler. Chainable.
|
|
|
547
571
|
|
|
548
572
|
Duplicate `groupName` registration throws an error.
|
|
549
573
|
|
|
574
|
+
**Delivery guarantee**: once a message is accepted by the runtime, delivery to each consumer group
|
|
575
|
+
is guaranteed. If the consumer is offline, the message waits in the server-side queue and is
|
|
576
|
+
dispatched automatically the moment the service reconnects and registers its handlers — no retry
|
|
577
|
+
budget is consumed while waiting. After `SERVICEBRIDGE_DELIVERY_TTL_DAYS` (default 7) days without
|
|
578
|
+
a consumer, the delivery moves to DLQ with reason `delivery_ttl_exceeded`.
|
|
579
|
+
|
|
550
580
|
`EventContext` helpers:
|
|
551
581
|
|
|
582
|
+
- `ctx.traceId` — current trace ID
|
|
583
|
+
- `ctx.spanId` — current span ID
|
|
552
584
|
- `ctx.retry(delayMs?)` — ask for redelivery with optional delay
|
|
553
|
-
- `ctx.reject(reason)` —
|
|
585
|
+
- `ctx.reject(reason)` — move to DLQ immediately, bypassing remaining retries
|
|
554
586
|
- `ctx.refs` — metadata (`topic`, `groupName`, `messageId`, `attempt`, `headers`)
|
|
555
587
|
- `ctx.stream.write(...)` — append real-time chunks to run stream
|
|
556
588
|
|
|
@@ -575,7 +607,7 @@ serve(opts?: ServeOpts): Promise<void>
|
|
|
575
607
|
|
|
576
608
|
Starts the worker gRPC server and registers handlers with the control plane.
|
|
577
609
|
The promise resolves once startup/registration is complete (it does not block
|
|
578
|
-
the Node.js process).
|
|
610
|
+
the Node.js process). Throws immediately if no handlers are registered (neither `handleRpc()` nor `handleEvent()` have been called).
|
|
579
611
|
|
|
580
612
|
`ServeOpts`:
|
|
581
613
|
|
|
@@ -865,6 +897,22 @@ Runs a Fastify handler inside the current trace context so downstream SDK calls
|
|
|
865
897
|
|
|
866
898
|
---
|
|
867
899
|
|
|
900
|
+
### Trace Utilities (HTTP Plugins)
|
|
901
|
+
|
|
902
|
+
#### `extractTraceFromHeaders(headers)`
|
|
903
|
+
|
|
904
|
+
```ts
|
|
905
|
+
import { extractTraceFromHeaders } from "service-bridge/express";
|
|
906
|
+
// or
|
|
907
|
+
import { extractTraceFromHeaders } from "service-bridge/fastify";
|
|
908
|
+
|
|
909
|
+
const { traceId, parentSpanId } = extractTraceFromHeaders(req.headers);
|
|
910
|
+
```
|
|
911
|
+
|
|
912
|
+
Extracts trace context from HTTP headers. Supports W3C `traceparent`, `x-trace-id`/`x-span-id` headers, and generates random IDs as fallback. Useful for custom HTTP framework integrations (Hono, Koa, etc.).
|
|
913
|
+
|
|
914
|
+
---
|
|
915
|
+
|
|
868
916
|
## Configuration
|
|
869
917
|
|
|
870
918
|
### TLS behavior
|
|
@@ -1142,6 +1190,36 @@ const mode = session.resolveTransportMode('payment-svc/charge'); // 'proxy'
|
|
|
1142
1190
|
| `TransportMode` | type | `'direct' \| 'proxy'` |
|
|
1143
1191
|
| `HelloAckV2` | interface | Данные HelloAck от сервера |
|
|
1144
1192
|
| `TransportConfigV2` | interface | ConfigPush payload |
|
|
1193
|
+
| `ReconcileRequestV2` | interface | Declarative worker registration request |
|
|
1194
|
+
| `FunctionDeclarationV2` | interface | Function declaration for Reconcile |
|
|
1195
|
+
| `ConsumerGroupDeclarationV2` | interface | Consumer group declaration |
|
|
1196
|
+
| `HttpRouteDeclarationV2` | interface | HTTP route declaration |
|
|
1197
|
+
| `JobDeclarationV2` | interface | Job declaration |
|
|
1198
|
+
| `WorkflowDeclarationV2` | interface | Workflow declaration |
|
|
1199
|
+
| `SubscribeRequestV2` | interface | Registry subscribe request |
|
|
1200
|
+
| `WorkerEndpointV2` | interface | Worker endpoint info |
|
|
1201
|
+
| `IssueCertificateRequestV2` | interface | Certificate request |
|
|
1202
|
+
| `IssueCertificateResponseV2` | interface | Certificate response |
|
|
1203
|
+
| `CircuitBreakerConfigV2` | interface | Circuit breaker config |
|
|
1204
|
+
| `ZoneConfigV2` | interface | Zone-aware config |
|
|
1205
|
+
| `ServiceTransportOverride` | interface | Per-service transport override |
|
|
1206
|
+
| `FunctionTransportOverride` | interface | Per-function transport override |
|
|
1207
|
+
| `ResumeState` | interface | Reconnect resume state |
|
|
1208
|
+
|
|
1209
|
+
Key types available for import:
|
|
1210
|
+
|
|
1211
|
+
```ts
|
|
1212
|
+
import type {
|
|
1213
|
+
WorkflowStep,
|
|
1214
|
+
WorkerTLSOpts,
|
|
1215
|
+
RpcContext,
|
|
1216
|
+
EventContext,
|
|
1217
|
+
StreamWriter,
|
|
1218
|
+
TraceCtx,
|
|
1219
|
+
RetryPolicy,
|
|
1220
|
+
ServiceBridgeErrorSeverity,
|
|
1221
|
+
} from "service-bridge";
|
|
1222
|
+
```
|
|
1145
1223
|
|
|
1146
1224
|
---
|
|
1147
1225
|
|