@cadenza.io/service 2.17.6 → 2.17.8

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.
@@ -1294,9 +1294,23 @@ var ServiceRegistry = class _ServiceRegistry {
1294
1294
  (ctx, emit) => {
1295
1295
  const { serviceName, serviceInstanceId } = ctx;
1296
1296
  const serviceInstances = this.instances.get(serviceName);
1297
- const instance = serviceInstances?.find(
1297
+ let instance = serviceInstances?.find(
1298
1298
  (i) => i.uuid === serviceInstanceId
1299
1299
  );
1300
+ if (!instance && serviceName && serviceInstanceId) {
1301
+ const bootstrapPlaceholder = serviceInstances?.find(
1302
+ (candidate) => candidate.isBootstrapPlaceholder && (!ctx.serviceTransportId || candidate.transports.some(
1303
+ (transport) => transport.uuid === ctx.serviceTransportId
1304
+ ))
1305
+ );
1306
+ if (bootstrapPlaceholder) {
1307
+ instance = this.adoptBootstrapPlaceholderInstanceId(
1308
+ serviceName,
1309
+ bootstrapPlaceholder.uuid,
1310
+ serviceInstanceId
1311
+ );
1312
+ }
1313
+ }
1300
1314
  if (!instance) {
1301
1315
  return false;
1302
1316
  }
@@ -2568,6 +2582,72 @@ var ServiceRegistry = class _ServiceRegistry {
2568
2582
  instances.filter((instance) => !instance.isBootstrapPlaceholder)
2569
2583
  );
2570
2584
  }
2585
+ adoptBootstrapPlaceholderInstanceId(serviceName, placeholderInstanceId, resolvedInstanceId) {
2586
+ if (!serviceName || !placeholderInstanceId || !resolvedInstanceId) {
2587
+ return void 0;
2588
+ }
2589
+ const instances = this.instances.get(serviceName);
2590
+ if (!instances?.length) {
2591
+ return void 0;
2592
+ }
2593
+ const resolvedInstance = instances.find(
2594
+ (instance) => instance.uuid === resolvedInstanceId
2595
+ );
2596
+ if (resolvedInstance) {
2597
+ return resolvedInstance;
2598
+ }
2599
+ const placeholder = instances.find(
2600
+ (instance) => instance.uuid === placeholderInstanceId && instance.isBootstrapPlaceholder
2601
+ );
2602
+ if (!placeholder) {
2603
+ return void 0;
2604
+ }
2605
+ const wasDependee = this.dependeeByInstance.has(placeholderInstanceId);
2606
+ const dependeeServiceName = this.dependeeByInstance.get(placeholderInstanceId) ?? serviceName;
2607
+ const requiredForReadiness = this.readinessDependeeByInstance.has(
2608
+ placeholderInstanceId
2609
+ );
2610
+ const lastHeartbeatAt = this.lastHeartbeatAtByInstance.get(placeholderInstanceId) ?? Date.now();
2611
+ const missedHeartbeats = this.missedHeartbeatsByInstance.get(placeholderInstanceId) ?? 0;
2612
+ const inFlight = this.runtimeStatusFallbackInFlightByInstance.has(
2613
+ placeholderInstanceId
2614
+ );
2615
+ this.dependeeByInstance.delete(placeholderInstanceId);
2616
+ this.readinessDependeeByInstance.delete(placeholderInstanceId);
2617
+ this.lastHeartbeatAtByInstance.delete(placeholderInstanceId);
2618
+ this.missedHeartbeatsByInstance.delete(placeholderInstanceId);
2619
+ this.runtimeStatusFallbackInFlightByInstance.delete(placeholderInstanceId);
2620
+ placeholder.uuid = resolvedInstanceId;
2621
+ placeholder.isBootstrapPlaceholder = false;
2622
+ placeholder.transports = placeholder.transports.map((transport) => ({
2623
+ ...transport,
2624
+ serviceInstanceId: resolvedInstanceId
2625
+ }));
2626
+ if (wasDependee) {
2627
+ this.dependeeByInstance.set(resolvedInstanceId, dependeeServiceName);
2628
+ if (this.dependeesByService.has(dependeeServiceName)) {
2629
+ this.dependeesByService.get(dependeeServiceName).delete(
2630
+ placeholderInstanceId
2631
+ );
2632
+ this.dependeesByService.get(dependeeServiceName).add(resolvedInstanceId);
2633
+ }
2634
+ this.lastHeartbeatAtByInstance.set(resolvedInstanceId, lastHeartbeatAt);
2635
+ this.missedHeartbeatsByInstance.set(resolvedInstanceId, missedHeartbeats);
2636
+ }
2637
+ if (requiredForReadiness) {
2638
+ this.readinessDependeeByInstance.set(resolvedInstanceId, serviceName);
2639
+ if (this.readinessDependeesByService.has(serviceName)) {
2640
+ this.readinessDependeesByService.get(serviceName).delete(
2641
+ placeholderInstanceId
2642
+ );
2643
+ this.readinessDependeesByService.get(serviceName).add(resolvedInstanceId);
2644
+ }
2645
+ }
2646
+ if (inFlight) {
2647
+ this.runtimeStatusFallbackInFlightByInstance.add(resolvedInstanceId);
2648
+ }
2649
+ return placeholder;
2650
+ }
2571
2651
  getHeartbeatMisses(serviceInstanceId, now = Date.now()) {
2572
2652
  const observedMisses = this.missedHeartbeatsByInstance.get(serviceInstanceId) ?? 0;
2573
2653
  const lastHeartbeatAt = this.lastHeartbeatAtByInstance.get(serviceInstanceId) ?? 0;
@@ -6296,6 +6376,15 @@ function readConfiguredPort(value) {
6296
6376
  function buildBootstrapUrl(protocol, address, port) {
6297
6377
  return `${protocol}://${address}:${port}`;
6298
6378
  }
6379
+ function readExplicitPortFromOrigin(raw) {
6380
+ const match = raw.match(
6381
+ /^[a-z]+:\/\/(?:\[[^\]]+\]|[^\/?#:]+):(\d+)(?:\/)?$/i
6382
+ );
6383
+ if (!match?.[1]) {
6384
+ return void 0;
6385
+ }
6386
+ return readConfiguredPort(match[1]);
6387
+ }
6299
6388
  function resolveInjectedBootstrapUrl(injectedGlobalKey) {
6300
6389
  if (typeof globalThis === "undefined") {
6301
6390
  return void 0;
@@ -6385,7 +6474,8 @@ function resolveBootstrapEndpoint(options) {
6385
6474
  "Bootstrap URL must be an origin without a path component."
6386
6475
  );
6387
6476
  }
6388
- const port2 = parsed2.port ? readConfiguredPort(parsed2.port) : fallbackPort;
6477
+ const explicitPort = readExplicitPortFromOrigin(raw);
6478
+ const port2 = explicitPort ?? (parsed2.port ? readConfiguredPort(parsed2.port) : fallbackPort);
6389
6479
  if (!port2) {
6390
6480
  throw new Error(
6391
6481
  "Bootstrap URL must include a port or CADENZA_DB_PORT must be provided."