@huaqiu/dsh-auth 0.7.8 → 0.7.9
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/lib/client.js +23 -7
- package/lib/client.js.map +1 -1
- package/lib/index.mjs +40 -7
- package/package.json +2 -2
- package/src/client/client.ts +24 -6
- package/src/client/ui/sidebar-action.tsx +10 -1
- package/src/routes.ts +20 -2
- package/src/service.ts +58 -9
package/lib/index.mjs
CHANGED
|
@@ -498,6 +498,21 @@ function originOf(url) {
|
|
|
498
498
|
*/
|
|
499
499
|
const LOGOUT_WAIT_MS = 3e4;
|
|
500
500
|
/**
|
|
501
|
+
* A host login that did not complete. Distinct from a genuine server fault so
|
|
502
|
+
* the routes layer can answer 401 (still signed out) instead of 500 — a user
|
|
503
|
+
* cancelling their own login must not look like an internal error.
|
|
504
|
+
*/
|
|
505
|
+
var HostLoginIncompleteError = class extends Error {
|
|
506
|
+
reason;
|
|
507
|
+
constructor(reason, cause) {
|
|
508
|
+
const status = cause?.status;
|
|
509
|
+
const detail = status !== void 0 ? `HTTP ${status}` : String(cause ?? "unknown");
|
|
510
|
+
super(reason === "timeout" ? "host login not completed: the EDA dialog did not answer in time" : `host login not completed: ${detail}`);
|
|
511
|
+
this.name = "HostLoginIncompleteError";
|
|
512
|
+
this.reason = reason;
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
/**
|
|
501
516
|
* Returns the persisted session, or null if absent/unreadable. Best-effort: a
|
|
502
517
|
* corrupt or unreadable file is treated as "no session" rather than thrown.
|
|
503
518
|
*/
|
|
@@ -604,12 +619,17 @@ var InMemoryHuaqiuAuthService = class {
|
|
|
604
619
|
getUserInfo: async () => this.resolve(),
|
|
605
620
|
login: async () => {
|
|
606
621
|
if (!this.host.enabled) return;
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
622
|
+
let res;
|
|
623
|
+
try {
|
|
624
|
+
res = await this.doFetch(`${this.host.baseUrl}${this.hostLoginPath}`, {
|
|
625
|
+
method: "POST",
|
|
626
|
+
headers: { accept: "application/json" },
|
|
627
|
+
signal: AbortSignal.timeout(this.loginWaitMs)
|
|
628
|
+
});
|
|
629
|
+
} catch (err) {
|
|
630
|
+
throw new HostLoginIncompleteError(err.name === "TimeoutError" ? "timeout" : "failed", err);
|
|
631
|
+
}
|
|
632
|
+
if (!res.ok) throw new HostLoginIncompleteError(res.status === 401 ? "dismissed" : "failed", res);
|
|
613
633
|
this.host.clear();
|
|
614
634
|
this.stale = false;
|
|
615
635
|
this.validator.invalidate();
|
|
@@ -1033,7 +1053,20 @@ function createAuthHandler(service) {
|
|
|
1033
1053
|
return;
|
|
1034
1054
|
}
|
|
1035
1055
|
if (req.method === "POST" && pathname === `/api/v1/huaqiu/auth/login`) {
|
|
1036
|
-
|
|
1056
|
+
try {
|
|
1057
|
+
await service.auth.login();
|
|
1058
|
+
} catch (err) {
|
|
1059
|
+
if (err instanceof HostLoginIncompleteError) {
|
|
1060
|
+
sendJson(res, 401, {
|
|
1061
|
+
ok: false,
|
|
1062
|
+
authenticated: false,
|
|
1063
|
+
reason: err.reason,
|
|
1064
|
+
error: err.message
|
|
1065
|
+
});
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
throw err;
|
|
1069
|
+
}
|
|
1037
1070
|
const user = await service.auth.getUserInfo();
|
|
1038
1071
|
sendJson(res, 200, {
|
|
1039
1072
|
ok: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huaqiu/dsh-auth",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/index.mjs",
|
|
6
6
|
"types": "./lib/index.d.mts",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@deepseek-ai/dsh-home-paths": "^0.1.0-rc.0",
|
|
36
|
-
"@huaqiu/dsh-plugin-log": "0.7.
|
|
36
|
+
"@huaqiu/dsh-plugin-log": "0.7.9"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"lib",
|
package/src/client/client.ts
CHANGED
|
@@ -141,6 +141,17 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
141
141
|
let hostMode = false
|
|
142
142
|
let hostSession: AuthTokenPayload | null = null
|
|
143
143
|
let hostSessionLoaded = false
|
|
144
|
+
/**
|
|
145
|
+
* The host login trigger in flight. In host mode `auth.login()` POSTs to the
|
|
146
|
+
* node half, which asks EDA (KiCad) to open its modal dialog and answers only
|
|
147
|
+
* once the user dismisses it. Several surfaces call this (sidebar entry, HIT
|
|
148
|
+
* cards, a click burst from an impatient user), so it is single-flighted:
|
|
149
|
+
* each extra POST would occupy one of the webview's few connections to DSH
|
|
150
|
+
* for the whole dialog, and once that pool is exhausted the next click never
|
|
151
|
+
* reaches the host — which is what "the login button stopped opening the
|
|
152
|
+
* dialog" looks like from the outside.
|
|
153
|
+
*/
|
|
154
|
+
let inflightLogin: Promise<void> | null = null
|
|
144
155
|
/**
|
|
145
156
|
* Why the last host-session resolution failed, if it did. Transport errors
|
|
146
157
|
* must NOT masquerade as a healthy `signed-out`: the dialog opener surfaces
|
|
@@ -251,12 +262,19 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
251
262
|
// blocks until the dialog completes; then re-resolve and emit (only on
|
|
252
263
|
// a real change, so a redundant trigger never re-renders every card).
|
|
253
264
|
if (hostMode) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
265
|
+
// One trigger per dialog (see `inflightLogin`).
|
|
266
|
+
if (inflightLogin !== null) return inflightLogin
|
|
267
|
+
const attempt = (async (): Promise<void> => {
|
|
268
|
+
const prevToken = hostSession?.token
|
|
269
|
+
await transport.triggerLogin()
|
|
270
|
+
hostSessionLoaded = false
|
|
271
|
+
await resolveHost()
|
|
272
|
+
if (hostSession?.token !== prevToken) emit(hostSession)
|
|
273
|
+
})()
|
|
274
|
+
inflightLogin = attempt.finally(() => {
|
|
275
|
+
inflightLogin = null
|
|
276
|
+
})
|
|
277
|
+
return inflightLogin
|
|
260
278
|
}
|
|
261
279
|
openIframe(options ?? {})
|
|
262
280
|
},
|
|
@@ -904,7 +904,16 @@ export const HuaqiuAuthSidebarAction = memo(function HuaqiuAuthSidebarAction({ w
|
|
|
904
904
|
// the DOM (backdrop + card + iframe) and closes itself on
|
|
905
905
|
// backdrop click, Escape, the × button, or the embed's
|
|
906
906
|
// `close_dialog` postMessage.
|
|
907
|
-
|
|
907
|
+
//
|
|
908
|
+
// In HOST mode there is no iframe: the promise settles only when
|
|
909
|
+
// the EDA dialog closes, and a dismissal rejects (the host route
|
|
910
|
+
// answers 401 right away instead of holding the connection). That
|
|
911
|
+
// rejection is the normal "user changed their mind" outcome — the
|
|
912
|
+
// entry simply stays on「Login」— so it must not surface as an
|
|
913
|
+
// unhandled rejection.
|
|
914
|
+
void auth
|
|
915
|
+
.login({ lang: locale, theme: dark ? 'dark' : 'light' })
|
|
916
|
+
.catch(() => { /* host dialog dismissed — still signed out */ })
|
|
908
917
|
return
|
|
909
918
|
}
|
|
910
919
|
setMenuOpen((open) => !open)
|
package/src/routes.ts
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
20
20
|
import { getLogger, type PluginLogger } from '@huaqiu/dsh-plugin-log'
|
|
21
21
|
import type { HuaqiuAuthService, HuaqiuUserInfo } from './service.js'
|
|
22
|
+
import { HostLoginIncompleteError } from './service.js'
|
|
22
23
|
import { aiUsageFromPayload, type AiUsage } from './subscription-balances.js'
|
|
23
24
|
|
|
24
25
|
// Lazy so test suites that `vi.mock('@deepseek-ai/dsh-home-paths', …)` before
|
|
@@ -291,8 +292,25 @@ export function createAuthHandler(service: HuaqiuAuthService): AuthHandler {
|
|
|
291
292
|
|
|
292
293
|
if (req.method === 'POST' && pathname === `${AUTH_ROUTE_PREFIX}/login`) {
|
|
293
294
|
// Host-mode login: forward to HQ Edge, which asks EDA (KiCad) to open
|
|
294
|
-
// its TriggerLoginDialog.
|
|
295
|
-
|
|
295
|
+
// its TriggerLoginDialog. The host answers as soon as the dialog
|
|
296
|
+
// closes, so this route settles promptly on both outcomes — the browser
|
|
297
|
+
// half is single-flighted and must never be left holding the promise.
|
|
298
|
+
try {
|
|
299
|
+
await service.auth.login()
|
|
300
|
+
} catch (err) {
|
|
301
|
+
if (err instanceof HostLoginIncompleteError) {
|
|
302
|
+
// A dismissed dialog is the operator's own choice: report it as
|
|
303
|
+
// "still signed out" (401), never as an internal error (500).
|
|
304
|
+
sendJson(res, 401, {
|
|
305
|
+
ok: false,
|
|
306
|
+
authenticated: false,
|
|
307
|
+
reason: err.reason,
|
|
308
|
+
error: err.message,
|
|
309
|
+
})
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
throw err
|
|
313
|
+
}
|
|
296
314
|
const user = await service.auth.getUserInfo()
|
|
297
315
|
sendJson(res, 200, { ok: true, authenticated: user !== null, user })
|
|
298
316
|
return
|
package/src/service.ts
CHANGED
|
@@ -147,6 +147,40 @@ function originOf(url: string): string | null {
|
|
|
147
147
|
*/
|
|
148
148
|
const LOGOUT_WAIT_MS = 30_000
|
|
149
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Why a host-mode login did not produce a credential.
|
|
152
|
+
*
|
|
153
|
+
* - `dismissed` — the operator closed the EDA login dialog. This is a NORMAL
|
|
154
|
+
* outcome, not a fault: the host answers 401 the moment the dialog closes (it
|
|
155
|
+
* no longer holds the HTTP request for the whole 5-minute wait, which used to
|
|
156
|
+
* exhaust the webview's connections to DSH and made the 6th click do nothing).
|
|
157
|
+
* - `timeout` — the host never answered within `loginWaitMs`.
|
|
158
|
+
* - `failed` — the host answered with some other error status.
|
|
159
|
+
*/
|
|
160
|
+
export type HostLoginFailureReason = 'dismissed' | 'timeout' | 'failed'
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A host login that did not complete. Distinct from a genuine server fault so
|
|
164
|
+
* the routes layer can answer 401 (still signed out) instead of 500 — a user
|
|
165
|
+
* cancelling their own login must not look like an internal error.
|
|
166
|
+
*/
|
|
167
|
+
export class HostLoginIncompleteError extends Error {
|
|
168
|
+
readonly reason: HostLoginFailureReason
|
|
169
|
+
constructor(reason: HostLoginFailureReason, cause?: unknown) {
|
|
170
|
+
// Keep the transport's own wording when there is no HTTP status (an
|
|
171
|
+
// aborted wait / a refused connection), so the log still says what broke.
|
|
172
|
+
const status = (cause as { status?: number } | undefined)?.status
|
|
173
|
+
const detail = status !== undefined ? `HTTP ${status}` : String(cause ?? 'unknown')
|
|
174
|
+
super(
|
|
175
|
+
reason === 'timeout'
|
|
176
|
+
? 'host login not completed: the EDA dialog did not answer in time'
|
|
177
|
+
: `host login not completed: ${detail}`,
|
|
178
|
+
)
|
|
179
|
+
this.name = 'HostLoginIncompleteError'
|
|
180
|
+
this.reason = reason
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
150
184
|
/**
|
|
151
185
|
* Returns the persisted session, or null if absent/unreadable. Best-effort: a
|
|
152
186
|
* corrupt or unreadable file is treated as "no session" rather than thrown.
|
|
@@ -296,16 +330,31 @@ export class InMemoryHuaqiuAuthService implements HuaqiuAuthService {
|
|
|
296
330
|
if (!this.host.enabled) return
|
|
297
331
|
// Host mode: the browser-side iframe is suppressed, so clicking login
|
|
298
332
|
// must ask the host (hq-edge → EDA TriggerLoginDialog) to open the EDA
|
|
299
|
-
// login dialog. The host route
|
|
300
|
-
//
|
|
301
|
-
//
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
333
|
+
// login dialog. The host route answers as soon as the dialog closes —
|
|
334
|
+
// with 401 when the operator dismissed it (bounded by `loginWaitMs`), so
|
|
335
|
+
// a cancellation must not be mistaken for a server fault. On success we
|
|
336
|
+
// drop the cached host session so the next resolve() re-fetches the
|
|
337
|
+
// fresh token.
|
|
338
|
+
let res: Response
|
|
339
|
+
try {
|
|
340
|
+
res = await this.doFetch(`${this.host.baseUrl}${this.hostLoginPath}`, {
|
|
341
|
+
method: 'POST',
|
|
342
|
+
headers: { accept: 'application/json' },
|
|
343
|
+
signal: AbortSignal.timeout(this.loginWaitMs),
|
|
344
|
+
})
|
|
345
|
+
} catch (err) {
|
|
346
|
+
// `AbortSignal.timeout` rejects with a TimeoutError/AbortError; a
|
|
347
|
+
// network failure is an unreachable host — either way no dialog ever
|
|
348
|
+
// completed, so this is still "not completed", not a crash.
|
|
349
|
+
throw new HostLoginIncompleteError(
|
|
350
|
+
(err as { name?: string }).name === 'TimeoutError' ? 'timeout' : 'failed',
|
|
351
|
+
err,
|
|
352
|
+
)
|
|
353
|
+
}
|
|
307
354
|
if (!res.ok) {
|
|
308
|
-
|
|
355
|
+
// 401 = the dialog was dismissed (or the wait expired) — the operator
|
|
356
|
+
// is still signed out, which the UI already renders.
|
|
357
|
+
throw new HostLoginIncompleteError(res.status === 401 ? 'dismissed' : 'failed', res)
|
|
309
358
|
}
|
|
310
359
|
this.host.clear()
|
|
311
360
|
this.stale = false
|