dsh-balance-newapi 2.2.4 → 2.2.5

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.
Files changed (3) hide show
  1. package/README.md +7 -0
  2. package/index.js +46 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -251,6 +251,13 @@ docker compose restart models-shim
251
251
 
252
252
  ## 十、更新记录
253
253
 
254
+ ### 2.2.5
255
+
256
+ - 请求超时或连接被断开时,自动换新连接重试一次(余额刷新 30 分钟才一次,keep-alive 连接常已被中间设备回收,复用死连接只会白等到超时)。
257
+ - 超时 6 秒 → 10 秒。
258
+ - 注册 provider 失败不再被永久拉黑:只有「id 已被注册」才标记,其它错误下一轮重试(此前 dsh-pet 重载后一次失败就永远显示「不支持余额查询」)。
259
+ - provider 同步周期 60 秒 → 30 秒,缩短重载后的空窗。
260
+
254
261
  ### 2.2.4
255
262
 
256
263
  - 发布到 npm(包名 `dsh-balance-newapi`),此后由插件市场检测版本、一键更新。
package/index.js CHANGED
@@ -37,7 +37,7 @@ export const inject = ['petApi7340']
37
37
  const BUILTIN = {
38
38
  base_url: 'https://codeman.betteryou.top',
39
39
  cache_seconds: 15,
40
- timeout_seconds: 6,
40
+ timeout_seconds: 10,
41
41
  provider_keys: {},
42
42
  extra_key_files: [],
43
43
  }
@@ -68,7 +68,7 @@ function config() {
68
68
 
69
69
  if (!data.base_url) throw new Error('配置缺少 base_url')
70
70
  data.cache_seconds ??= 15
71
- data.timeout_seconds ??= 6
71
+ data.timeout_seconds ??= 10
72
72
  data.extra_key_files ??= []
73
73
  data.provider_keys ??= {}
74
74
  cached = { mtime, data }
@@ -469,16 +469,42 @@ function endpoint(provider) {
469
469
 
470
470
  async function request(provider, token, what) {
471
471
  const cfg = config()
472
- const res = await fetch(endpoint(provider), {
473
- headers: { authorization: `Bearer ${token}`, accept: 'application/json' },
474
- redirect: 'manual',
475
- signal: AbortSignal.timeout(cfg.timeout_seconds * 1000),
476
- })
477
- if (res.status === 401) throw new Error(`${what}被服务端拒绝(401)`)
478
- if (!res.ok) throw new Error(`/balance HTTP ${res.status}`)
479
- const body = await res.json()
480
- if (!body.ok) throw new Error(body.error || '服务端返回失败')
481
- return body
472
+ const once = async () => {
473
+ const res = await fetch(endpoint(provider), {
474
+ headers: { authorization: `Bearer ${token}`, accept: 'application/json' },
475
+ redirect: 'manual',
476
+ signal: AbortSignal.timeout(cfg.timeout_seconds * 1000),
477
+ })
478
+ if (res.status === 401) throw new Error(`${what}被服务端拒绝(401)`)
479
+ if (!res.ok) throw new Error(`/balance HTTP ${res.status}`)
480
+ const body = await res.json()
481
+ if (!body.ok) throw new Error(body.error || '服务端返回失败')
482
+ return body
483
+ }
484
+ try {
485
+ return await once()
486
+ } catch (err) {
487
+ // 只对「连接层」失败重试一次:keep-alive 连接闲置久了会被中间的 NAT/负载均衡悄悄回收,
488
+ // 复用这条死连接时首包发出去没有回音,只能等 AbortSignal 到点。
489
+ // 余额刷新 30 分钟才一次,正好踩在这个区间上——重试会用一条新连接,几毫秒就回来。
490
+ // 业务错误(401 / HTTP 状态码 / 服务端 ok:false)一律不重试:那些再试也一样。
491
+ if (!isTransportError(err)) throw err
492
+ console.warn(
493
+ `[dsh-balance-newapi] ${provider} 首次请求失败(${err.name || 'Error'}: ${err.message}),换新连接重试一次`,
494
+ )
495
+ return await once()
496
+ }
497
+ }
498
+
499
+ /** 是否属于「连接断了 / 超时」这类重试一次就可能成功的错误。 */
500
+ function isTransportError(err) {
501
+ const name = err?.name || ''
502
+ const msg = String(err?.message || '')
503
+ const code = String(err?.cause?.code || '')
504
+ if (name === 'TimeoutError' || name === 'AbortError') return true
505
+ return /fetch failed|network|socket|ECONNRESET|ECONNREFUSED|EPIPE|ETIMEDOUT|UND_ERR|terminated/i.test(
506
+ `${msg} ${code}`,
507
+ )
482
508
  }
483
509
 
484
510
  function cacheKey(provider, entry) {
@@ -679,9 +705,13 @@ function start(ctx, api) {
679
705
  registered.add(id)
680
706
  console.log(`[dsh-balance-newapi] 已登记 provider: ${id}`)
681
707
  } catch (err) {
682
- // 其它原因(id 被占、定义非法)只报一次,别再每轮重试
683
- registered.add(id)
684
- console.warn(`[dsh-balance-newapi] 跳过 ${id}:${err.message}`)
708
+ const msg = String(err?.message || '')
709
+ // 「已被注册」说明这个 id 确实在我们名下,标记后不再重复尝试。
710
+ // **其余错误一律不标记**,下一轮 sync 再试:标记了就再也不会注册,
711
+ // 表现为永久的「该服务商不支持余额查询」(日志里那 21 次 unsupported 就是这样来的:
712
+ // dsh-pet 重载后旧的 api 引用失效,register 抛错一次就被永久拉黑)。
713
+ if (msg.includes('已被注册')) registered.add(id)
714
+ console.warn(`[dsh-balance-newapi] 注册 ${id} 失败:${msg}`)
685
715
  }
686
716
  }
687
717
 
@@ -707,7 +737,7 @@ function start(ctx, api) {
707
737
  }
708
738
 
709
739
  sync()
710
- const timer = setInterval(sync, 60_000)
740
+ const timer = setInterval(sync, 30_000)
711
741
  timer.unref?.()
712
742
 
713
743
  ctx.effect(() => () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-balance-newapi",
3
- "version": "2.2.4",
3
+ "version": "2.2.5",
4
4
  "description": "dsh-pet 余额插件:从 dsh 里当前供应商的 apiKey 查对应 new-api 账号余额,身份只由 key 决定;零依赖单文件,包里不含任何密钥。",
5
5
  "type": "module",
6
6
  "main": "index.js",