dsh-sub2api 0.10.1 → 0.10.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.2
4
+
5
+ - **One retry against the current host when the configured one cannot be
6
+ reached.** A profile that still points at a retired domain would otherwise
7
+ fail every call with a transport error the user cannot act on. The retry only
8
+ happens for an unreachable host, and only when that host is one this plugin
9
+ used to ship: a 401 or 404 is a real answer and is never re-sent elsewhere, and
10
+ a self-hosted gateway keeps its own result.
11
+ - Both request paths are covered: the account API and the `/v1` gateway used for
12
+ models and billing. An explicit base passed by a caller is respected and never
13
+ redirected.
14
+ - `scripts/measure-context.mjs` defaults to the current host; it still pointed at
15
+ the retired one and would have failed on a fresh clone.
16
+
3
17
  ## 0.10.1
4
18
 
5
19
  - **A key without a group is replaced instead of reused.** After the domain move
package/lib/config.js CHANGED
@@ -66,7 +66,7 @@ export function field(config, key, fallback) {
66
66
  * completely alone.
67
67
  * @type {Record<string, string>}
68
68
  */
69
- const MOVED_HOSTS = {
69
+ export const MOVED_HOSTS = {
70
70
  "sub.pscly.cc": "sub.svpai.net",
71
71
  "pay.pscly.cc": "pay.svpai.net",
72
72
  };
@@ -13,6 +13,27 @@
13
13
 
14
14
  import { KEY_PAGE_SIZE } from "../constants.js";
15
15
  import { Sub2ApiError, requestJson } from "../util.js";
16
+ import { MOVED_HOSTS } from "../config.js";
17
+
18
+ /**
19
+ * Whether a failure means "never reached the server", as opposed to a considered
20
+ * answer from it.
21
+ *
22
+ * Retrying a 401 or a 404 against another host would mask real problems; a
23
+ * connection that never opened is the only case where a different host can help.
24
+ * @param {unknown} error
25
+ * @returns {boolean}
26
+ */
27
+ function isTransportFailureLike(error) {
28
+ const code = String(error?.code ?? "");
29
+ if (code === "transport" || code === "TRANSPORT" || code === "TIMEOUT") return true;
30
+ const status = Number(error?.status);
31
+ if (Number.isFinite(status) && status > 0) return false;
32
+ const message = String(error?.message ?? "");
33
+ return /fetch failed|could not be reached|ECONNREFUSED|ENOTFOUND|ETIMEDOUT|EAI_AGAIN|socket hang up/i.test(
34
+ message,
35
+ );
36
+ }
16
37
 
17
38
  export class Sub2ApiApi {
18
39
  /**
@@ -41,8 +62,8 @@ export class Sub2ApiApi {
41
62
  async #call(path, options = {}) {
42
63
  const headers = {};
43
64
  if (options.token) headers.Authorization = `Bearer ${options.token}`;
44
- try {
45
- const { payload } = await requestJson(this.#url(path), {
65
+ const attempt = (base) =>
66
+ requestJson(`${base}${path}`, {
46
67
  method: options.method ?? "GET",
47
68
  headers,
48
69
  body: options.body,
@@ -50,12 +71,50 @@ export class Sub2ApiApi {
50
71
  signal: options.signal,
51
72
  fetchImpl: this.deps.fetchImpl,
52
73
  });
74
+
75
+ try {
76
+ const { payload } = await attempt(this.#base());
53
77
  return payload;
54
78
  } catch (error) {
79
+ // A moved host is not something the plugin can fix after the fact, but a
80
+ // stale profile pointing at a retired one is common enough to be worth one
81
+ // retry against the current default. Only a transport failure qualifies:
82
+ // a 4xx is a real answer and retrying it elsewhere would be wrong.
83
+ const fallback = this.#relocatedBase();
84
+ if (fallback !== undefined && isTransportFailureLike(error)) {
85
+ const { payload } = await attempt(fallback).catch((retryError) => {
86
+ throw annotate(retryError, path);
87
+ });
88
+ return payload;
89
+ }
55
90
  throw annotate(error, path);
56
91
  }
57
92
  }
58
93
 
94
+ /** The configured base, normalised. */
95
+ #base() {
96
+ return String(this.deps.apiBase()).replace(/\/+$/, "");
97
+ }
98
+
99
+ /**
100
+ * Where to try when the configured base cannot be reached at all.
101
+ *
102
+ * Only offered when the configured host is one this plugin used to ship: a
103
+ * self-hosted gateway keeps its own answer, failure included.
104
+ * @returns {string | undefined}
105
+ */
106
+ #relocatedBase() {
107
+ const current = this.#base();
108
+ let host;
109
+ try {
110
+ host = new URL(current).hostname.toLowerCase();
111
+ } catch {
112
+ return undefined;
113
+ }
114
+ const moved = MOVED_HOSTS[host];
115
+ return moved === undefined ? undefined : `https://${moved}`;
116
+ }
117
+
59
118
  // ---------------------------------------------------------------- auth
60
119
 
61
120
  /**
@@ -215,16 +274,30 @@ export class Sub2ApiApi {
215
274
  }
216
275
 
217
276
  async #gateway(path, apiKey, signal, baseUrl) {
218
- const root = baseUrl === undefined || baseUrl === "" ? this.deps.apiBase() : String(baseUrl).replace(/\/+$/, "");
219
- try {
220
- const { payload } = await requestJson(`${root}${path}`, {
277
+ const explicit =
278
+ baseUrl === undefined || baseUrl === "" ? undefined : String(baseUrl).replace(/\/+$/, "");
279
+ const attempt = (root) =>
280
+ requestJson(`${root}${path}`, {
221
281
  headers: { Authorization: `Bearer ${apiKey}` },
222
282
  timeoutMs: this.deps.timeoutMs(),
223
283
  signal,
224
284
  fetchImpl: this.deps.fetchImpl,
225
285
  });
286
+
287
+ try {
288
+ const { payload } = await attempt(explicit ?? this.#base());
226
289
  return payload;
227
290
  } catch (error) {
291
+ // Same rule as #call: only an unreachable host earns one retry, and only
292
+ // when the route came from configuration rather than a caller's explicit
293
+ // base (an explicit base is the caller's own choice to keep).
294
+ const fallback = explicit === undefined ? this.#relocatedBase() : undefined;
295
+ if (fallback !== undefined && isTransportFailureLike(error)) {
296
+ const { payload } = await attempt(fallback).catch((retryError) => {
297
+ throw annotate(retryError, path);
298
+ });
299
+ return payload;
300
+ }
228
301
  throw annotate(error, path);
229
302
  }
230
303
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sub2api",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "Official Psapi provider for DeepSeek Harness: email sign-in or sign-up, automatic key and model provisioning, one panel for balance and recharge",
5
5
  "type": "module",
6
6
  "license": "GPL-3.0-only",