@truenas/api-client 2.0.0 → 2.0.1

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.cjs CHANGED
@@ -3306,7 +3306,7 @@ var VersionDiscovery = class {
3306
3306
  operators.map((versionStrings) => this.selectVersion(hostname, versionStrings)),
3307
3307
  operators.catchError((error) => {
3308
3308
  this.versionCache.delete(hostname);
3309
- this.logger.error("Version discovery failed", { hostname, error });
3309
+ this.logger.warn("Version discovery failed", { hostname, error });
3310
3310
  return rxjs.throwError(() => this.classify(error, hostname));
3311
3311
  }),
3312
3312
  operators.shareReplay(1)
@@ -3377,7 +3377,7 @@ var VersionDiscovery = class {
3377
3377
  });
3378
3378
  const parsedVersions = versionStrings.map(parseApiVersion).filter((v) => v !== null);
3379
3379
  if (parsedVersions.length === 0) {
3380
- this.logger.error("No valid versions in response", {
3380
+ this.logger.warn("No valid versions in response", {
3381
3381
  hostname,
3382
3382
  versionStrings
3383
3383
  });
@@ -3479,29 +3479,34 @@ async function createTrueNasClient(opts) {
3479
3479
  `Cannot create client for system ${uuid}: hostnames array is empty`
3480
3480
  );
3481
3481
  }
3482
- const primaryHostname = hostnames[0];
3483
3482
  const versionDiscovery = new VersionDiscovery(logger);
3484
3483
  logger.info("Creating versioned API client", {
3485
3484
  uuid: uuid.slice(0, 8),
3486
- hostname: primaryHostname,
3485
+ hostnames: hostnames.join(", "),
3487
3486
  systemName
3488
3487
  });
3489
3488
  let version;
3490
3489
  try {
3491
- version = await rxjs.firstValueFrom(
3492
- versionDiscovery.discoverVersion(primaryHostname)
3490
+ const winner = await discoverVersionFromAnyHostname(
3491
+ hostnames,
3492
+ versionDiscovery
3493
3493
  );
3494
+ version = winner.version;
3494
3495
  logger.info("API version discovered, instantiating client", {
3495
3496
  uuid: uuid.slice(0, 8),
3497
+ // Which hostname answered is log context only. The client is built with
3498
+ // the full hostname list regardless — the websocket connection races all
3499
+ // of them anyway.
3500
+ hostname: winner.hostname,
3496
3501
  version: version.version,
3497
3502
  websocketPath: version.websocketPath
3498
3503
  });
3499
3504
  } catch (error) {
3500
3505
  const errorMessage = errorMessageOrDefault(error, "Unknown error");
3501
3506
  if (!(error instanceof VersionDiscoveryNetworkError)) {
3502
- logger.error("Version discovery failed", {
3507
+ logger.error("Version discovery failed on every hostname", {
3503
3508
  uuid: uuid.slice(0, 8),
3504
- hostname: primaryHostname,
3509
+ hostnames: hostnames.join(", "),
3505
3510
  error: errorMessage,
3506
3511
  errorType: error instanceof Error ? error.constructor.name : typeof error
3507
3512
  });
@@ -3512,7 +3517,7 @@ async function createTrueNasClient(opts) {
3512
3517
  if (!fallbackVersion) {
3513
3518
  logger.error("Invalid fallback version configuration", {
3514
3519
  uuid: uuid.slice(0, 8),
3515
- hostname: primaryHostname,
3520
+ hostnames: hostnames.join(", "),
3516
3521
  fallbackVersion: fallbackVersionString
3517
3522
  });
3518
3523
  throw error;
@@ -3521,7 +3526,7 @@ async function createTrueNasClient(opts) {
3521
3526
  "Version discovery failed with a network error (possible CORS or network issue), falling back to assumed version",
3522
3527
  {
3523
3528
  uuid: uuid.slice(0, 8),
3524
- hostname: primaryHostname,
3529
+ hostnames: hostnames.join(", "),
3525
3530
  fallbackVersion: fallbackVersionString,
3526
3531
  originalError: errorMessage,
3527
3532
  warning: "A network error has multiple causes (CORS, network down, DNS failure). The connection may still fail during the WebSocket handshake."
@@ -3531,6 +3536,27 @@ async function createTrueNasClient(opts) {
3531
3536
  }
3532
3537
  return instantiateClientForVersion(version, opts, logger);
3533
3538
  }
3539
+ async function discoverVersionFromAnyHostname(hostnames, versionDiscovery) {
3540
+ const attempts = hostnames.map(
3541
+ (hostname) => rxjs.firstValueFrom(versionDiscovery.discoverVersion(hostname)).then(
3542
+ (version) => ({ hostname, version })
3543
+ )
3544
+ );
3545
+ try {
3546
+ return await Promise.any(attempts);
3547
+ } catch (error) {
3548
+ const failures = error instanceof AggregateError ? error.errors : [error];
3549
+ throw selectRepresentativeFailure(failures);
3550
+ }
3551
+ }
3552
+ function selectRepresentativeFailure(failures) {
3553
+ const isVersionError = (error) => (
3554
+ // cases: valid response, but the given versions won't work for us
3555
+ error instanceof VersionTooOldError || error instanceof VersionTooNewError || error instanceof NoCompatibleVersionsError || error instanceof VersionEndpointNotFoundError
3556
+ );
3557
+ const isNetworkError = (error) => error instanceof VersionDiscoveryNetworkError;
3558
+ return failures.find(isVersionError) ?? failures.find(isNetworkError) ?? failures[0];
3559
+ }
3534
3560
  var CLIENT_BY_VERSION_KEY = {
3535
3561
  "25.10": TrueNasApiClientV2510,
3536
3562
  "26": TrueNasApiClientV26