@truenas/api-client 9.0.2 → 9.0.4

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.
@@ -3205,13 +3205,14 @@ function socketScheme(protocol) {
3205
3205
  var tenSeconds = 10 * 1e3;
3206
3206
  var twentySeconds = 20 * 1e3;
3207
3207
  var TrueNasConnection = class {
3208
- constructor(initialEnabled, hostnames, systemUuid, websocketPath, systemName, retryDelay = tenSeconds, maxRetry = 3, logger = noopLogger, protocol = "https:") {
3208
+ constructor(initialEnabled, hostnames, systemUuid, websocketPath, systemName, retryDelay = tenSeconds, maxRetry = 3, logger = noopLogger, protocol = "https:", interceptor) {
3209
3209
  this.systemUuid = systemUuid;
3210
3210
  this.websocketPath = websocketPath;
3211
3211
  this.systemName = systemName;
3212
3212
  this.retryDelay = retryDelay;
3213
3213
  this.maxRetry = maxRetry;
3214
3214
  this.logger = logger;
3215
+ this.interceptor = interceptor;
3215
3216
  // compatibility properties
3216
3217
  this.opened = new rxjs.BehaviorSubject(false);
3217
3218
  this.closed = new rxjs.Subject();
@@ -3269,6 +3270,7 @@ var TrueNasConnection = class {
3269
3270
  const initMessage = createJsonRpcMessage("core.set_options", [
3270
3271
  { legacy_jobs: false }
3271
3272
  ]);
3273
+ this.interceptor?.outgoing?.(initMessage);
3272
3274
  connection.ws.next(initMessage);
3273
3275
  return connection.ws.messages().pipe(
3274
3276
  rxjs.filter((msg) => msg.id === initMessage.id),
@@ -3367,22 +3369,27 @@ var TrueNasConnection = class {
3367
3369
  rxjs.map((conn) => conn.state === "error"),
3368
3370
  rxjs.takeUntil(this.closeConnection)
3369
3371
  );
3370
- /**
3371
- * observable which always emits messages from the current socket.
3372
- */
3373
- this.messages$ = this.ws$.pipe(
3372
+ /** What the appliance sends, on whichever socket is current. */
3373
+ this.socketMessages$ = this.ws$.pipe(
3374
3374
  rxjs.filter((ws) => ws !== null),
3375
3375
  rxjs.switchMap((ws) => ws.messages().pipe(
3376
3376
  rxjs.catchError(() => rxjs.EMPTY)
3377
3377
  ))
3378
3378
  );
3379
+ this.messages$ = interceptor?.injected$ ? rxjs.merge(this.socketMessages$, interceptor.injected$.pipe(rxjs.observeOn(rxjs.asapScheduler), rxjs.share())) : this.socketMessages$;
3379
3380
  this.endpoint$.next(resolveEndpoint(hostnames, protocol));
3380
3381
  this.ws$.pipe(
3381
3382
  rxjs.switchMap((ws) => ws ? rxjs.interval(twentySeconds).pipe(rxjs.map(() => ws)) : rxjs.EMPTY),
3382
3383
  rxjs.takeUntil(this.closeConnection)
3383
3384
  ).subscribe((ws) => {
3384
- ws.next(createJsonRpcMessage("core.ping"));
3385
+ const ping = createJsonRpcMessage("core.ping");
3386
+ this.interceptor?.outgoing?.(ping);
3387
+ ws.next(ping);
3385
3388
  });
3389
+ if (interceptor?.incoming) {
3390
+ const incoming = interceptor.incoming.bind(interceptor);
3391
+ this.messages$.pipe(rxjs.takeUntil(this.closeConnection)).subscribe((message) => incoming(message));
3392
+ }
3386
3393
  this.opened$.subscribe((val) => this.opened.next(val));
3387
3394
  this.closed$.subscribe(() => this.closed.next());
3388
3395
  this.hostname$.pipe(rxjs.filter(Boolean)).subscribe((name) => this.hostname.next(name));
@@ -3451,6 +3458,9 @@ var TrueNasConnection = class {
3451
3458
  * the next `closed$` emission.
3452
3459
  */
3453
3460
  send(message) {
3461
+ if (this.interceptor?.outgoing?.(message) === false) {
3462
+ return rxjs.Subscription.EMPTY;
3463
+ }
3454
3464
  return this.ws$.pipe(
3455
3465
  rxjs.filter((ws) => ws !== null),
3456
3466
  rxjs.take(1)
@@ -3648,7 +3658,8 @@ var TrueNasApiClient = class {
3648
3658
  this.reconnect.retryDelay,
3649
3659
  this.reconnect.maxRetry,
3650
3660
  this.logger,
3651
- this.protocol
3661
+ this.protocol,
3662
+ this.reconnect.interceptor
3652
3663
  );
3653
3664
  }
3654
3665
  /**
@@ -4308,6 +4319,11 @@ async function createTrueNasClient(opts) {
4308
4319
  `Cannot create client for system ${uuid}: hostnames array is empty`
4309
4320
  );
4310
4321
  }
4322
+ if (opts.websocketPath !== void 0 && !opts.websocketPath.startsWith("/")) {
4323
+ throw new Error(
4324
+ `Cannot create client for system ${uuid}: websocketPath must be an absolute path, got '${opts.websocketPath}'.`
4325
+ );
4326
+ }
4311
4327
  logger.info("Creating versioned API client", {
4312
4328
  uuid: uuid.slice(0, 8),
4313
4329
  hostnames: hostnames.join(", "),
@@ -4337,7 +4353,7 @@ async function createTrueNasClient(opts) {
4337
4353
  logger.info("API version supplied by the caller, skipping discovery", {
4338
4354
  uuid: uuid.slice(0, 8),
4339
4355
  version: known.version,
4340
- websocketPath: known.websocketPath
4356
+ websocketPath: opts.websocketPath ?? known.websocketPath
4341
4357
  });
4342
4358
  return instantiateClientForVersion(known, opts, logger);
4343
4359
  }
@@ -4356,7 +4372,7 @@ async function createTrueNasClient(opts) {
4356
4372
  // of them anyway.
4357
4373
  hostname: winner.hostname,
4358
4374
  version: version.version,
4359
- websocketPath: version.websocketPath
4375
+ websocketPath: opts.websocketPath ?? version.websocketPath
4360
4376
  });
4361
4377
  } catch (error) {
4362
4378
  const errorMessage = errorMessageOrDefault(error, "Unknown error");
@@ -4482,6 +4498,7 @@ function clientClassFor(version) {
4482
4498
  function instantiateClientForVersion(version, opts, logger) {
4483
4499
  const { uuid, hostnames, enabled, systemName } = opts;
4484
4500
  const versionKey = clientVersionKey(version);
4501
+ const endpoint = opts.websocketPath === void 0 ? version : { ...version, websocketPath: opts.websocketPath };
4485
4502
  const Client = CLIENT_BY_VERSION_KEY[versionKey];
4486
4503
  if (!Client) {
4487
4504
  logger.error("No client implementation for version", {
@@ -4496,17 +4513,18 @@ function instantiateClientForVersion(version, opts, logger) {
4496
4513
  logger.info(`Instantiating ${Client.name}`, {
4497
4514
  uuid: uuid.slice(0, 8),
4498
4515
  version: version.version,
4499
- versionKey
4516
+ versionKey,
4517
+ websocketPath: endpoint.websocketPath
4500
4518
  });
4501
4519
  return new Client(
4502
4520
  uuid,
4503
4521
  hostnames,
4504
- version,
4522
+ endpoint,
4505
4523
  enabled,
4506
4524
  systemName,
4507
4525
  logger,
4508
4526
  opts.protocol,
4509
- { retryDelay: opts.retryDelay, maxRetry: opts.maxRetry }
4527
+ { retryDelay: opts.retryDelay, maxRetry: opts.maxRetry, interceptor: opts.interceptor }
4510
4528
  );
4511
4529
  }
4512
4530
  function errorMessageOrDefault(error, fallback) {
@@ -4568,5 +4586,5 @@ exports.v25_10_4_exports = v25_10_4_exports;
4568
4586
  exports.v25_10_5_exports = v25_10_5_exports;
4569
4587
  exports.v27_0_0_exports = v27_0_0_exports;
4570
4588
  exports.v28_0_0_exports = v28_0_0_exports;
4571
- //# sourceMappingURL=chunk-MJFFBJJE.cjs.map
4572
- //# sourceMappingURL=chunk-MJFFBJJE.cjs.map
4589
+ //# sourceMappingURL=chunk-WAVCPALL.cjs.map
4590
+ //# sourceMappingURL=chunk-WAVCPALL.cjs.map