dsh-sub2api 0.10.0 → 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 +24 -0
- package/lib/config.js +1 -1
- package/lib/sub2api/api.js +78 -5
- package/lib/sub2api/provision.js +16 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
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
|
+
|
|
17
|
+
## 0.10.1
|
|
18
|
+
|
|
19
|
+
- **A key without a group is replaced instead of reused.** After the domain move
|
|
20
|
+
an account could hold a `dsh` key with no group; the plugin matched it by name,
|
|
21
|
+
reported "复用已有密钥" as a success, and then every request failed with 403
|
|
22
|
+
"API Key is not assigned to any group". The stale key is now deleted and a
|
|
23
|
+
grouped one created, so the login actually produces a working key.
|
|
24
|
+
- The check normalises `group_id: null` — the gateway sends null, not undefined,
|
|
25
|
+
and a bare `=== undefined` test would have missed exactly this case.
|
|
26
|
+
|
|
3
27
|
## 0.10.0
|
|
4
28
|
|
|
5
29
|
- **The gateway and the pay centre moved.** Defaults are now
|
package/lib/config.js
CHANGED
package/lib/sub2api/api.js
CHANGED
|
@@ -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
|
-
|
|
45
|
-
|
|
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
|
|
219
|
-
|
|
220
|
-
|
|
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/lib/sub2api/provision.js
CHANGED
|
@@ -55,9 +55,17 @@ export async function provisionAccount(deps) {
|
|
|
55
55
|
);
|
|
56
56
|
|
|
57
57
|
let key = existing?.key;
|
|
58
|
-
|
|
58
|
+
// `?? undefined` normalises both null and undefined; the gateway sends null.
|
|
59
|
+
const rawGroup = existing?.group_id;
|
|
60
|
+
let groupId = rawGroup === null || rawGroup === undefined ? undefined : rawGroup;
|
|
59
61
|
|
|
60
|
-
|
|
62
|
+
// A key that exists but carries no group is unusable: the gateway answers
|
|
63
|
+
// every request with 403 "API Key is not assigned to any group". Reusing it
|
|
64
|
+
// because the name matches would look like a successful login and fail on the
|
|
65
|
+
// first message, so an ungrouped key is treated as one that needs replacing.
|
|
66
|
+
const ungrouped = typeof key === "string" && key !== "" && groupId === undefined;
|
|
67
|
+
|
|
68
|
+
if (typeof key === "string" && key !== "" && !ungrouped) {
|
|
61
69
|
steps.push(step("key", "API 密钥", true, `复用已有密钥 ${keyName}`));
|
|
62
70
|
} else {
|
|
63
71
|
const chosenGroup = chooseGroup({
|
|
@@ -65,6 +73,12 @@ export async function provisionAccount(deps) {
|
|
|
65
73
|
groups,
|
|
66
74
|
allowed: profile.ok ? profile.value?.allowed_groups : undefined,
|
|
67
75
|
});
|
|
76
|
+
if (ungrouped) {
|
|
77
|
+
// Keep the account tidy: the old key cannot be used, and leaving it means
|
|
78
|
+
// the next login picks the same broken one again.
|
|
79
|
+
await attempt(() => api.deleteKey?.(accessToken, existing.id));
|
|
80
|
+
steps.push(step("key", "API 密钥", true, `已替换未分组的旧密钥 ${keyName}`));
|
|
81
|
+
}
|
|
68
82
|
const created = await attempt(() =>
|
|
69
83
|
api.createKey(accessToken, { name: keyName, groupId: chosenGroup }),
|
|
70
84
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sub2api",
|
|
3
|
-
"version": "0.10.
|
|
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",
|