service-bridge 1.0.8-dev.19 → 1.0.9-dev.22

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.
Files changed (3) hide show
  1. package/README.md +10 -13
  2. package/dist/index.js +11 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -97,7 +97,6 @@ import { servicebridge } from "service-bridge";
97
97
  const sb = servicebridge(
98
98
  process.env.SERVICEBRIDGE_URL ?? "localhost:14445",
99
99
  process.env.SERVICEBRIDGE_SERVICE_KEY!,
100
- "payments",
101
100
  );
102
101
 
103
102
  sb.handleRpc("charge", async (payload: { orderId: string; amount: number }) => {
@@ -115,7 +114,6 @@ import { servicebridge } from "service-bridge";
115
114
  const sb = servicebridge(
116
115
  process.env.SERVICEBRIDGE_URL ?? "localhost:14445",
117
116
  process.env.SERVICEBRIDGE_SERVICE_KEY!,
118
- "orders",
119
117
  );
120
118
 
121
119
  const result = await sb.rpc<{ ok: boolean; txId: string }>("payments/charge", {
@@ -153,7 +151,7 @@ import { servicebridge } from "service-bridge";
153
151
 
154
152
  // --- Payments service (worker) ---
155
153
 
156
- const payments = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!, "payments");
154
+ const payments = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!);
157
155
 
158
156
  payments.handleRpc("charge", async (payload: { orderId: string; amount: number }, ctx) => {
159
157
  await ctx?.stream.write({ status: "charging", orderId: payload.orderId }, "progress");
@@ -170,7 +168,7 @@ await payments.serve({ host: "localhost" });
170
168
  ```ts
171
169
  // --- Orders service (caller + event publisher) ---
172
170
 
173
- const orders = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!, "orders");
171
+ const orders = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!);
174
172
 
175
173
  // Call payments, then publish event
176
174
  const charge = await orders.rpc<{ ok: boolean; txId: string }>("payments/charge", {
@@ -190,7 +188,7 @@ await orders.event("orders.completed", {
190
188
  ```ts
191
189
  // --- Notifications service (event consumer) ---
192
190
 
193
- const notifications = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!, "notifications");
191
+ const notifications = servicebridge("localhost:14445", process.env.SERVICEBRIDGE_SERVICE_KEY!);
194
192
 
195
193
  notifications.handleEvent("orders.*", async (payload, ctx) => {
196
194
  const body = payload as { orderId: string; txId: string };
@@ -273,18 +271,19 @@ across all three SDKs. Parity differences are naming-only (language idioms):
273
271
  - Handler hints: timeout/retryable/concurrency/prefetch are advisory in all SDKs
274
272
  - Shared `serve()` fields across SDKs: host, max in-flight, instance ID, weight, and per-serve TLS override
275
273
 
276
- ### `servicebridge(url, serviceKey, serviceName?, opts?)`
274
+ ### `servicebridge(url, serviceKey, opts?)`
277
275
 
278
276
  ```ts
279
277
  function servicebridge(
280
278
  url: string,
281
279
  serviceKey: string,
282
- service?: string,
283
- globalOpts?: ServiceBridgeOpts,
280
+ serviceOrOpts?: string | ServiceBridgeOpts,
281
+ maybeGlobalOpts?: ServiceBridgeOpts,
284
282
  ): ServiceBridgeService
285
283
  ```
286
284
 
287
285
  Creates an SDK client instance.
286
+ Service identity is resolved by the runtime from `serviceKey`; passing a third `service` argument is legacy-only.
288
287
 
289
288
  `ServiceBridgeOpts`:
290
289
 
@@ -540,7 +539,7 @@ Registers an event consumer handler. Chainable.
540
539
 
541
540
  | Option | Type | Description |
542
541
  |---|---|---|
543
- | `groupName` | `string` | Consumer group name. Default: `<service>.<pattern>`. |
542
+ | `groupName` | `string` | Consumer group name. Default: `<service-key-id>.<pattern>`. |
544
543
  | `concurrency` | `number` | Advisory concurrency hint (currently not hard-enforced). |
545
544
  | `prefetch` | `number` | Advisory prefetch hint (currently not hard-enforced). |
546
545
  | `retryPolicyJson` | `string` | Retry policy JSON string. |
@@ -766,7 +765,7 @@ import express from "express";
766
765
  import { servicebridge } from "service-bridge";
767
766
  import { servicebridgeMiddleware, registerExpressRoutes } from "service-bridge/express";
768
767
 
769
- const sb = servicebridge(process.env.SERVICEBRIDGE_URL!, process.env.SERVICEBRIDGE_SERVICE_KEY!, "api");
768
+ const sb = servicebridge(process.env.SERVICEBRIDGE_URL!, process.env.SERVICEBRIDGE_SERVICE_KEY!);
770
769
  const app = express();
771
770
 
772
771
  app.use(servicebridgeMiddleware({
@@ -822,7 +821,7 @@ import Fastify from "fastify";
822
821
  import { servicebridge } from "service-bridge";
823
822
  import { servicebridgePlugin, wrapHandler } from "service-bridge/fastify";
824
823
 
825
- const sb = servicebridge(process.env.SERVICEBRIDGE_URL!, process.env.SERVICEBRIDGE_SERVICE_KEY!, "api");
824
+ const sb = servicebridge(process.env.SERVICEBRIDGE_URL!, process.env.SERVICEBRIDGE_SERVICE_KEY!);
826
825
  const app = Fastify();
827
826
 
828
827
  await app.register(servicebridgePlugin, {
@@ -895,13 +894,11 @@ The SDK requires values you pass into `servicebridge(...)`. Common setup:
895
894
  |---|---|---|---|
896
895
  | `SERVICEBRIDGE_URL` | yes | `localhost:14445` | gRPC control plane URL |
897
896
  | `SERVICEBRIDGE_SERVICE_KEY` | yes | `sbv2.<id>.<secret>.<ca>` | Service authentication key (sbv2 only) |
898
- | `SERVICEBRIDGE_SERVICE` | yes (worker mode) | `orders` | Service name in registry |
899
897
 
900
898
  ```ts
901
899
  const sb = servicebridge(
902
900
  process.env.SERVICEBRIDGE_URL ?? "localhost:14445",
903
901
  process.env.SERVICEBRIDGE_SERVICE_KEY!,
904
- process.env.SERVICEBRIDGE_SERVICE ?? "orders",
905
902
  );
906
903
  ```
907
904
 
package/dist/index.js CHANGED
@@ -458,7 +458,11 @@ function ensureSbResolverRegistered() {
458
458
  }
459
459
  registerResolver("sb", SbResolver);
460
460
  }
461
- function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
461
+ function servicebridge(url, serviceKey, serviceOrOpts = {}, maybeGlobalOpts = {}) {
462
+ const service = typeof serviceOrOpts === "string" ? serviceOrOpts.trim() : "";
463
+ const globalOpts = typeof serviceOrOpts === "string" ? maybeGlobalOpts : serviceOrOpts ?? {};
464
+ const parsedServiceKey = parseServiceKeyV2(serviceKey);
465
+ const defaultConsumerPrefix = service || parsedServiceKey.keyId || "consumer";
462
466
  const meta = new grpc.Metadata;
463
467
  meta.add("x-service-key", serviceKey);
464
468
  const rawUrl = url.trim();
@@ -570,7 +574,7 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
570
574
  fn: op.fn,
571
575
  service_name: service,
572
576
  started_at: String(op.startedAt),
573
- duration_ms: String(computeDurationMs(op.startedAt)),
577
+ duration_ms: String(op.durationMs),
574
578
  success: op.success,
575
579
  error: op.error ?? "",
576
580
  input: op.inputBuf,
@@ -840,6 +844,7 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
840
844
  });
841
845
  }
842
846
  function reportCallAsync(traceId, spanId, fn, startedAt, inputBuf, success, attempt, outputBuf, error = "") {
847
+ const durationMs = computeDurationMs(startedAt);
843
848
  if (!isOnline) {
844
849
  enqueueOffline({
845
850
  type: "reportCall",
@@ -847,6 +852,7 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
847
852
  spanId,
848
853
  fn,
849
854
  startedAt,
855
+ durationMs,
850
856
  inputBuf,
851
857
  success,
852
858
  attempt,
@@ -860,6 +866,7 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
860
866
  spanId,
861
867
  fn,
862
868
  startedAt,
869
+ durationMs,
863
870
  inputBuf,
864
871
  success,
865
872
  attempt,
@@ -875,6 +882,7 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
875
882
  spanId,
876
883
  fn,
877
884
  startedAt,
885
+ durationMs,
878
886
  inputBuf,
879
887
  success,
880
888
  attempt,
@@ -1511,7 +1519,7 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
1511
1519
  },
1512
1520
  handleEvent(pattern, handler, opts) {
1513
1521
  const normalizedOpts = opts ?? {};
1514
- const groupName = normalizedOpts.groupName || `${service}.${pattern}`;
1522
+ const groupName = normalizedOpts.groupName || `${defaultConsumerPrefix}.${pattern}`;
1515
1523
  if (eventHandlers.has(groupName)) {
1516
1524
  throw new Error(`Duplicate event consumer group "${groupName}". ` + "Use a distinct groupName for each handleEvent() registration.");
1517
1525
  }
@@ -1529,9 +1537,6 @@ function servicebridge(url, serviceKey, service = "", globalOpts = {}) {
1529
1537
  if (fnHandlers.size === 0 && eventHandlers.size === 0) {
1530
1538
  throw new Error("No handlers registered. Call handleRpc() or handleEvent() before serve().");
1531
1539
  }
1532
- if (!service.trim()) {
1533
- throw new Error("serve() requires a non-empty service name");
1534
- }
1535
1540
  if (opts.maxInFlight != null && (!Number.isInteger(opts.maxInFlight) || opts.maxInFlight < 1)) {
1536
1541
  throw new Error("serve() maxInFlight must be a positive integer");
1537
1542
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "service-bridge",
3
- "version": "1.0.8-dev.19",
3
+ "version": "1.0.9-dev.22",
4
4
  "type": "module",
5
5
  "description": "ServiceBridge SDK for Node.js — production-ready RPC, durable events, workflows, jobs, and distributed tracing. One Go runtime + PostgreSQL replaces Istio, RabbitMQ, Temporal, and Jaeger.",
6
6
  "keywords": [