@truenas/api-client 3.0.6 → 3.0.7

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
@@ -2836,6 +2836,7 @@ var noopLogger = {
2836
2836
  };
2837
2837
 
2838
2838
  // src/utils/truenas-connection.utils.ts
2839
+ var policyViolationCloseCode = 1008;
2839
2840
  function isHttpStatusError(reason) {
2840
2841
  return ["404", "502", "503"].some((code) => reason.includes(code));
2841
2842
  }
@@ -2944,7 +2945,25 @@ var TrueNasConnection = class {
2944
2945
  rxjs.switchMap((enabled) => {
2945
2946
  if (enabled) {
2946
2947
  return this.connect().pipe(
2947
- rxjs.map((conn) => makeActiveConnection(conn.ws, conn.hostname))
2948
+ rxjs.map((conn) => makeActiveConnection(conn.ws, conn.hostname)),
2949
+ // A refusal becomes a value here rather than an error, so it never
2950
+ // reaches the `retry` below and nothing reconnects on its own.
2951
+ //
2952
+ // Handled at this depth on purpose: letting it out completes the
2953
+ // pipeline and unsubscribes `enabledChange$`, and `setEnabled` is the
2954
+ // documented way an app asks for another attempt once the network
2955
+ // changes. Not retrying is a statement about what this client does on
2956
+ // its own; it is not a reason to refuse the caller asking again.
2957
+ rxjs.catchError(
2958
+ (err) => isTerminalClose(err) ? rxjs.of(
2959
+ makeConnectionError(
2960
+ err.message,
2961
+ err.hostname,
2962
+ err.closeCode,
2963
+ err.closeReason
2964
+ )
2965
+ ) : rxjs.throwError(() => err)
2966
+ )
2948
2967
  );
2949
2968
  }
2950
2969
  return rxjs.of(closedConnection);
@@ -2970,7 +2989,14 @@ var TrueNasConnection = class {
2970
2989
  rxjs.catchError((err) => {
2971
2990
  this.logger.error("All connections failed - retrying", { message: err?.message, hostname: err?.hostname });
2972
2991
  return rxjs.concat(
2973
- rxjs.of(makeConnectionError(err.message, err.hostname)),
2992
+ rxjs.of(
2993
+ makeConnectionError(
2994
+ err.message,
2995
+ err.hostname,
2996
+ err.closeCode,
2997
+ err.closeReason
2998
+ )
2999
+ ),
2974
3000
  // since this error will immediately get caught by `retry` there's no reason to build a real error.
2975
3001
  rxjs.throwError(() => null)
2976
3002
  );
@@ -3079,6 +3105,11 @@ var TrueNasConnection = class {
3079
3105
  /**
3080
3106
  * enables or disables the connection gate. the app calls this when its `SystemState`
3081
3107
  * changes (mapping `SystemState.Active -> true`, everything else -> `false`).
3108
+ *
3109
+ * This is also how a caller asks for another attempt after the appliance has
3110
+ * refused the client — nothing reconnects on its own from there. The gate is
3111
+ * `distinctUntilChanged`, so re-asserting `true` while it is already `true`
3112
+ * does nothing: the round trip through `false` is what asks again.
3082
3113
  */
3083
3114
  setEnabled(enabled) {
3084
3115
  this.enabled$.next(enabled);
@@ -3136,7 +3167,9 @@ var TrueNasConnection = class {
3136
3167
  errorMessage = getWebSocketError(event.code);
3137
3168
  }
3138
3169
  this.connectionAttempts.next(this.connectionAttempts.value + 1);
3139
- subscriber.error(makeConnectionError(errorMessage, hostname));
3170
+ subscriber.error(
3171
+ makeConnectionError(errorMessage, hostname, event.code, reason)
3172
+ );
3140
3173
  }
3141
3174
  }
3142
3175
  });
@@ -3186,12 +3219,15 @@ var makeActiveConnection = (ws, hostname) => ({
3186
3219
  hostname,
3187
3220
  state: "active"
3188
3221
  });
3189
- var makeConnectionError = (message, hostname) => ({
3222
+ var makeConnectionError = (message, hostname, closeCode, closeReason) => ({
3190
3223
  name: "ConnectionError",
3191
3224
  message,
3192
3225
  hostname,
3226
+ closeCode,
3227
+ closeReason,
3193
3228
  state: "error"
3194
3229
  });
3230
+ var isTerminalClose = (err) => err?.closeCode === policyViolationCloseCode;
3195
3231
  var closedConnection = { state: "closed" };
3196
3232
 
3197
3233
  // src/client/truenas-api-client.ts