@agentunion/fastaun 0.4.11 → 0.4.13

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/client.js CHANGED
@@ -226,6 +226,22 @@ function _v2BytesEqual(a, b) {
226
226
  function formatCaughtError(error) {
227
227
  return error instanceof Error ? error : String(error);
228
228
  }
229
+ const RELOGIN_REFRESH_ERRORS = new Set([
230
+ 'missing refresh_token',
231
+ 'invalid_or_expired_refresh_token',
232
+ 'refresh not supported',
233
+ ]);
234
+ function authErrorRequiresRelogin(error) {
235
+ const data = error.data;
236
+ if (isJsonObject(data)) {
237
+ if (data.relogin_required === true)
238
+ return true;
239
+ const code = String(data.error ?? '').trim().toLowerCase();
240
+ if (RELOGIN_REFRESH_ERRORS.has(code))
241
+ return true;
242
+ }
243
+ return RELOGIN_REFRESH_ERRORS.has(error.message.trim().toLowerCase());
244
+ }
229
245
  function normalizeDeliveryModeConfig(raw, opts = {}) {
230
246
  const defaultMode = String(opts.defaultMode ?? 'fanout').trim().toLowerCase() || 'fanout';
231
247
  const defaultRouting = String(opts.defaultRouting ?? 'round_robin').trim().toLowerCase() || 'round_robin';
@@ -2624,6 +2640,18 @@ export class AUNClient {
2624
2640
  }
2625
2641
  catch (exc) {
2626
2642
  if (exc instanceof AuthError) {
2643
+ if (authErrorRequiresRelogin(exc)) {
2644
+ this._clientLog.warn(`token refresh requires relogin, stopping refresh loop and triggering reconnect: ${exc.message}`);
2645
+ await this._dispatcher.publish('token.refresh_exhausted', {
2646
+ aid: this._identity?.aid ?? null,
2647
+ consecutive_failures: 1,
2648
+ last_error: String(exc),
2649
+ relogin_required: true,
2650
+ });
2651
+ this._tokenRefreshFailures = 0;
2652
+ await this._handleTransportDisconnect(new Error('token refresh relogin required, triggering reconnect'));
2653
+ return;
2654
+ }
2627
2655
  this._tokenRefreshFailures++;
2628
2656
  if (this._tokenRefreshFailures >= 3) {
2629
2657
  this._clientLog.warn(`token refresh failed ${this._tokenRefreshFailures} consecutive times, stopping refresh loop and triggering reconnect`);
@@ -2633,7 +2661,7 @@ export class AUNClient {
2633
2661
  last_error: String(exc),
2634
2662
  });
2635
2663
  this._tokenRefreshFailures = 0;
2636
- this._handleTransportDisconnect(new Error('token refresh exhausted, triggering reconnect'));
2664
+ await this._handleTransportDisconnect(new Error('token refresh exhausted, triggering reconnect'));
2637
2665
  return;
2638
2666
  }
2639
2667
  this._clientLog.debug(`token refresh failed (${this._tokenRefreshFailures}/3), will retry: ${exc}`);
@@ -2750,10 +2778,12 @@ export class AUNClient {
2750
2778
  eventPayload.code = disconnectInfo.code;
2751
2779
  }
2752
2780
  await this._dispatcher.publish('state_change', eventPayload);
2781
+ this._reconnectActive = false;
2753
2782
  return;
2754
2783
  }
2755
2784
  // 1000 = 正常关闭, 1006 = 网络异常断开(无 close frame),其他 code = 服务端主动关闭
2756
2785
  const serverInitiated = closeCode !== undefined && closeCode !== 1000 && closeCode !== 1006;
2786
+ this._reconnectActive = false;
2757
2787
  this._startReconnect(serverInitiated);
2758
2788
  }
2759
2789
  /** 启动重连循环(默认无限重试 + 指数退避 + 固定上限抖动,仅在不可重试错误、close() 或 max_attempts 耗尽时终止) */
@@ -2818,6 +2848,24 @@ export class AUNClient {
2818
2848
  if (this._sessionParams === null) {
2819
2849
  throw new StateError('missing connect params for reconnect');
2820
2850
  }
2851
+ // 重连前同步 identity 里的 token 状态到 sessionParams,防止用过期 token 死循环 4001
2852
+ {
2853
+ const identity = this._identity;
2854
+ if (identity) {
2855
+ const cachedToken = String(identity.access_token ?? '');
2856
+ const expiresAt = this._auth.getAccessTokenExpiry(identity);
2857
+ if (cachedToken && (expiresAt === null || expiresAt > Date.now() / 1000 + 30)) {
2858
+ this._sessionParams.access_token = cachedToken;
2859
+ }
2860
+ else {
2861
+ this._clientLog.debug(`reconnect: cached token expired or missing for aid=${this._aid ?? ''}, clearing to trigger re-login`);
2862
+ this._sessionParams.access_token = '';
2863
+ }
2864
+ }
2865
+ else {
2866
+ this._sessionParams.access_token = '';
2867
+ }
2868
+ }
2821
2869
  await this._connectOnce(this._sessionParams, true);
2822
2870
  // 重连成功,退出循环
2823
2871
  this._clientLog.debug(`reconnect success: attempt=${attempt}, aid=${this._aid ?? ''}`);