@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.
package/dist/index.mjs CHANGED
@@ -1243,9 +1243,23 @@ var ServiceRegistry = class _ServiceRegistry {
1243
1243
  (ctx, emit) => {
1244
1244
  const { serviceName, serviceInstanceId } = ctx;
1245
1245
  const serviceInstances = this.instances.get(serviceName);
1246
- const instance = serviceInstances?.find(
1246
+ let instance = serviceInstances?.find(
1247
1247
  (i) => i.uuid === serviceInstanceId
1248
1248
  );
1249
+ if (!instance && serviceName && serviceInstanceId) {
1250
+ const bootstrapPlaceholder = serviceInstances?.find(
1251
+ (candidate) => candidate.isBootstrapPlaceholder && (!ctx.serviceTransportId || candidate.transports.some(
1252
+ (transport) => transport.uuid === ctx.serviceTransportId
1253
+ ))
1254
+ );
1255
+ if (bootstrapPlaceholder) {
1256
+ instance = this.adoptBootstrapPlaceholderInstanceId(
1257
+ serviceName,
1258
+ bootstrapPlaceholder.uuid,
1259
+ serviceInstanceId
1260
+ );
1261
+ }
1262
+ }
1249
1263
  if (!instance) {
1250
1264
  return false;
1251
1265
  }
@@ -2517,6 +2531,72 @@ var ServiceRegistry = class _ServiceRegistry {
2517
2531
  instances.filter((instance) => !instance.isBootstrapPlaceholder)
2518
2532
  );
2519
2533
  }
2534
+ adoptBootstrapPlaceholderInstanceId(serviceName, placeholderInstanceId, resolvedInstanceId) {
2535
+ if (!serviceName || !placeholderInstanceId || !resolvedInstanceId) {
2536
+ return void 0;
2537
+ }
2538
+ const instances = this.instances.get(serviceName);
2539
+ if (!instances?.length) {
2540
+ return void 0;
2541
+ }
2542
+ const resolvedInstance = instances.find(
2543
+ (instance) => instance.uuid === resolvedInstanceId
2544
+ );
2545
+ if (resolvedInstance) {
2546
+ return resolvedInstance;
2547
+ }
2548
+ const placeholder = instances.find(
2549
+ (instance) => instance.uuid === placeholderInstanceId && instance.isBootstrapPlaceholder
2550
+ );
2551
+ if (!placeholder) {
2552
+ return void 0;
2553
+ }
2554
+ const wasDependee = this.dependeeByInstance.has(placeholderInstanceId);
2555
+ const dependeeServiceName = this.dependeeByInstance.get(placeholderInstanceId) ?? serviceName;
2556
+ const requiredForReadiness = this.readinessDependeeByInstance.has(
2557
+ placeholderInstanceId
2558
+ );
2559
+ const lastHeartbeatAt = this.lastHeartbeatAtByInstance.get(placeholderInstanceId) ?? Date.now();
2560
+ const missedHeartbeats = this.missedHeartbeatsByInstance.get(placeholderInstanceId) ?? 0;
2561
+ const inFlight = this.runtimeStatusFallbackInFlightByInstance.has(
2562
+ placeholderInstanceId
2563
+ );
2564
+ this.dependeeByInstance.delete(placeholderInstanceId);
2565
+ this.readinessDependeeByInstance.delete(placeholderInstanceId);
2566
+ this.lastHeartbeatAtByInstance.delete(placeholderInstanceId);
2567
+ this.missedHeartbeatsByInstance.delete(placeholderInstanceId);
2568
+ this.runtimeStatusFallbackInFlightByInstance.delete(placeholderInstanceId);
2569
+ placeholder.uuid = resolvedInstanceId;
2570
+ placeholder.isBootstrapPlaceholder = false;
2571
+ placeholder.transports = placeholder.transports.map((transport) => ({
2572
+ ...transport,
2573
+ serviceInstanceId: resolvedInstanceId
2574
+ }));
2575
+ if (wasDependee) {
2576
+ this.dependeeByInstance.set(resolvedInstanceId, dependeeServiceName);
2577
+ if (this.dependeesByService.has(dependeeServiceName)) {
2578
+ this.dependeesByService.get(dependeeServiceName).delete(
2579
+ placeholderInstanceId
2580
+ );
2581
+ this.dependeesByService.get(dependeeServiceName).add(resolvedInstanceId);
2582
+ }
2583
+ this.lastHeartbeatAtByInstance.set(resolvedInstanceId, lastHeartbeatAt);
2584
+ this.missedHeartbeatsByInstance.set(resolvedInstanceId, missedHeartbeats);
2585
+ }
2586
+ if (requiredForReadiness) {
2587
+ this.readinessDependeeByInstance.set(resolvedInstanceId, serviceName);
2588
+ if (this.readinessDependeesByService.has(serviceName)) {
2589
+ this.readinessDependeesByService.get(serviceName).delete(
2590
+ placeholderInstanceId
2591
+ );
2592
+ this.readinessDependeesByService.get(serviceName).add(resolvedInstanceId);
2593
+ }
2594
+ }
2595
+ if (inFlight) {
2596
+ this.runtimeStatusFallbackInFlightByInstance.add(resolvedInstanceId);
2597
+ }
2598
+ return placeholder;
2599
+ }
2520
2600
  getHeartbeatMisses(serviceInstanceId, now = Date.now()) {
2521
2601
  const observedMisses = this.missedHeartbeatsByInstance.get(serviceInstanceId) ?? 0;
2522
2602
  const lastHeartbeatAt = this.lastHeartbeatAtByInstance.get(serviceInstanceId) ?? 0;
@@ -8631,6 +8711,15 @@ function readConfiguredPort(value) {
8631
8711
  function buildBootstrapUrl(protocol, address, port) {
8632
8712
  return `${protocol}://${address}:${port}`;
8633
8713
  }
8714
+ function readExplicitPortFromOrigin(raw) {
8715
+ const match = raw.match(
8716
+ /^[a-z]+:\/\/(?:\[[^\]]+\]|[^\/?#:]+):(\d+)(?:\/)?$/i
8717
+ );
8718
+ if (!match?.[1]) {
8719
+ return void 0;
8720
+ }
8721
+ return readConfiguredPort(match[1]);
8722
+ }
8634
8723
  function resolveInjectedBootstrapUrl(injectedGlobalKey) {
8635
8724
  if (typeof globalThis === "undefined") {
8636
8725
  return void 0;
@@ -8720,7 +8809,8 @@ function resolveBootstrapEndpoint(options) {
8720
8809
  "Bootstrap URL must be an origin without a path component."
8721
8810
  );
8722
8811
  }
8723
- const port2 = parsed2.port ? readConfiguredPort(parsed2.port) : fallbackPort;
8812
+ const explicitPort = readExplicitPortFromOrigin(raw);
8813
+ const port2 = explicitPort ?? (parsed2.port ? readConfiguredPort(parsed2.port) : fallbackPort);
8724
8814
  if (!port2) {
8725
8815
  throw new Error(
8726
8816
  "Bootstrap URL must include a port or CADENZA_DB_PORT must be provided."