@huaqiu/dsh-auth 0.5.8 → 0.5.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/index.d.mts +14 -2
- package/lib/index.mjs +17 -8
- package/package.json +2 -2
- package/src/apiKeySync.ts +24 -3
- package/src/index.ts +15 -3
package/lib/index.d.mts
CHANGED
|
@@ -236,6 +236,18 @@ interface CredentialStore {
|
|
|
236
236
|
set(ref: string, value: string): Promise<void>;
|
|
237
237
|
unset(ref: string): Promise<void>;
|
|
238
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Resolves the credential store lazily on every sync round.
|
|
241
|
+
*
|
|
242
|
+
* The DSH `credentials` service may not be activated yet when this plugin
|
|
243
|
+
* boots (service activation order), and direct `ctx.credentials` property
|
|
244
|
+
* access throws "cannot get property ... without inject" in that window.
|
|
245
|
+
* Resolving through `ctx.get('credentials')` per round returns `undefined`
|
|
246
|
+
* while the service is unavailable — the round is skipped and retried on the
|
|
247
|
+
* next poll, so the sync self-heals once the service activates, without a
|
|
248
|
+
* restart (cordis guidance: "switch to `ctx.get('x')` with an absence check").
|
|
249
|
+
*/
|
|
250
|
+
type CredentialStoreProvider = () => CredentialStore | null;
|
|
239
251
|
/** Logger surface used by the sync (kept small so tests can pass a spy). */
|
|
240
252
|
interface SyncLogger {
|
|
241
253
|
debug(msg: string, ctx?: Record<string, unknown>): void;
|
|
@@ -247,7 +259,7 @@ declare class HuaqiuApiKeySync {
|
|
|
247
259
|
readonly baseUrl: string;
|
|
248
260
|
/** Token path on the host; default "/api/v1/auth/token". */
|
|
249
261
|
readonly path: string;
|
|
250
|
-
private readonly
|
|
262
|
+
private readonly storeProvider;
|
|
251
263
|
private readonly log;
|
|
252
264
|
private readonly intervalMs;
|
|
253
265
|
private readonly doFetch;
|
|
@@ -260,7 +272,7 @@ declare class HuaqiuApiKeySync {
|
|
|
260
272
|
/** HQ Edge base URL ("" → disabled, matches HostSessionResolver.enabled). */
|
|
261
273
|
baseUrl: string,
|
|
262
274
|
/** Token path on the host; default "/api/v1/auth/token". */
|
|
263
|
-
path: string,
|
|
275
|
+
path: string, storeProvider: CredentialStoreProvider, log: SyncLogger, intervalMs?: number, doFetch?: typeof fetch);
|
|
264
276
|
/** Host-mode sync is active iff a base URL was configured. */
|
|
265
277
|
get enabled(): boolean;
|
|
266
278
|
/** Kick off an immediate sync, then poll on a non-overlapping interval. */
|
package/lib/index.mjs
CHANGED
|
@@ -205,7 +205,7 @@ const DEFAULT_HUAQIU_API_KEY_SYNC_MS = 5e3;
|
|
|
205
205
|
var HuaqiuApiKeySync = class {
|
|
206
206
|
baseUrl;
|
|
207
207
|
path;
|
|
208
|
-
|
|
208
|
+
storeProvider;
|
|
209
209
|
log;
|
|
210
210
|
intervalMs;
|
|
211
211
|
doFetch;
|
|
@@ -214,10 +214,10 @@ var HuaqiuApiKeySync = class {
|
|
|
214
214
|
inFlight = false;
|
|
215
215
|
/** Last token mirrored into the credential store (change detection). */
|
|
216
216
|
lastToken = null;
|
|
217
|
-
constructor(baseUrl, path,
|
|
217
|
+
constructor(baseUrl, path, storeProvider, log, intervalMs = DEFAULT_HUAQIU_API_KEY_SYNC_MS, doFetch = globalThis.fetch.bind(globalThis)) {
|
|
218
218
|
this.baseUrl = baseUrl;
|
|
219
219
|
this.path = path;
|
|
220
|
-
this.
|
|
220
|
+
this.storeProvider = storeProvider;
|
|
221
221
|
this.log = log;
|
|
222
222
|
this.intervalMs = intervalMs;
|
|
223
223
|
this.doFetch = doFetch;
|
|
@@ -249,6 +249,11 @@ var HuaqiuApiKeySync = class {
|
|
|
249
249
|
* rounds deterministically without timers.
|
|
250
250
|
*/
|
|
251
251
|
async syncNow() {
|
|
252
|
+
const store = this.storeProvider();
|
|
253
|
+
if (store === null) {
|
|
254
|
+
this.log.debug("huaqiu api-key sync skipped: credentials service unavailable");
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
252
257
|
const url = `${this.baseUrl}${this.path}`;
|
|
253
258
|
let res = null;
|
|
254
259
|
let info = null;
|
|
@@ -268,7 +273,7 @@ var HuaqiuApiKeySync = class {
|
|
|
268
273
|
if (info !== null && info.authenticated) {
|
|
269
274
|
if (info.token !== this.lastToken) {
|
|
270
275
|
this.lastToken = info.token;
|
|
271
|
-
await
|
|
276
|
+
await store.set(HUAQIU_API_KEY_REF, info.token);
|
|
272
277
|
this.log.info("huaqiu api-key credential synced", { userId: info.id });
|
|
273
278
|
}
|
|
274
279
|
return;
|
|
@@ -276,7 +281,7 @@ var HuaqiuApiKeySync = class {
|
|
|
276
281
|
if (res !== null && res.ok) {
|
|
277
282
|
if (this.lastToken !== null) {
|
|
278
283
|
this.lastToken = null;
|
|
279
|
-
await
|
|
284
|
+
await store.unset(HUAQIU_API_KEY_REF);
|
|
280
285
|
this.log.info("huaqiu api-key credential cleared", { explicit: true });
|
|
281
286
|
}
|
|
282
287
|
return;
|
|
@@ -871,9 +876,13 @@ function apply(ctx, config) {
|
|
|
871
876
|
}));
|
|
872
877
|
if (service.hostMode) ctx.effect(() => {
|
|
873
878
|
const resolved = resolveHostConfig(config);
|
|
874
|
-
const sync = new HuaqiuApiKeySync(resolved.hqEdgeBaseUrl ?? "", resolved.hostAuthPath ?? "/api/v1/auth/token", {
|
|
875
|
-
|
|
876
|
-
|
|
879
|
+
const sync = new HuaqiuApiKeySync(resolved.hqEdgeBaseUrl ?? "", resolved.hostAuthPath ?? "/api/v1/auth/token", () => {
|
|
880
|
+
const credentials = ctx.get("credentials");
|
|
881
|
+
if (credentials === void 0) return null;
|
|
882
|
+
return {
|
|
883
|
+
set: (ref, value) => credentials.set(ref, value),
|
|
884
|
+
unset: (ref) => credentials.unset(ref)
|
|
885
|
+
};
|
|
877
886
|
}, {
|
|
878
887
|
debug: (msg, ctx2) => log.debug(msg, ctx2),
|
|
879
888
|
info: (msg, ctx2) => log.info(msg, ctx2),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huaqiu/dsh-auth",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.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.5.
|
|
36
|
+
"@huaqiu/dsh-plugin-log": "0.5.9"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"lib",
|
package/src/apiKeySync.ts
CHANGED
|
@@ -35,6 +35,19 @@ export interface CredentialStore {
|
|
|
35
35
|
unset(ref: string): Promise<void>
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the credential store lazily on every sync round.
|
|
40
|
+
*
|
|
41
|
+
* The DSH `credentials` service may not be activated yet when this plugin
|
|
42
|
+
* boots (service activation order), and direct `ctx.credentials` property
|
|
43
|
+
* access throws "cannot get property ... without inject" in that window.
|
|
44
|
+
* Resolving through `ctx.get('credentials')` per round returns `undefined`
|
|
45
|
+
* while the service is unavailable — the round is skipped and retried on the
|
|
46
|
+
* next poll, so the sync self-heals once the service activates, without a
|
|
47
|
+
* restart (cordis guidance: "switch to `ctx.get('x')` with an absence check").
|
|
48
|
+
*/
|
|
49
|
+
export type CredentialStoreProvider = () => CredentialStore | null
|
|
50
|
+
|
|
38
51
|
/** Logger surface used by the sync (kept small so tests can pass a spy). */
|
|
39
52
|
export interface SyncLogger {
|
|
40
53
|
debug(msg: string, ctx?: Record<string, unknown>): void
|
|
@@ -54,7 +67,7 @@ export class HuaqiuApiKeySync {
|
|
|
54
67
|
readonly baseUrl: string,
|
|
55
68
|
/** Token path on the host; default "/api/v1/auth/token". */
|
|
56
69
|
readonly path: string,
|
|
57
|
-
private readonly
|
|
70
|
+
private readonly storeProvider: CredentialStoreProvider,
|
|
58
71
|
private readonly log: SyncLogger,
|
|
59
72
|
private readonly intervalMs: number = DEFAULT_HUAQIU_API_KEY_SYNC_MS,
|
|
60
73
|
private readonly doFetch: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
@@ -91,6 +104,14 @@ export class HuaqiuApiKeySync {
|
|
|
91
104
|
* rounds deterministically without timers.
|
|
92
105
|
*/
|
|
93
106
|
async syncNow(): Promise<void> {
|
|
107
|
+
// Resolve the credential store lazily: the `credentials` service may not
|
|
108
|
+
// be activated yet while this plugin boots (activation order). Skipping
|
|
109
|
+
// the round is safe — the next poll retries once the service is live.
|
|
110
|
+
const store = this.storeProvider()
|
|
111
|
+
if (store === null) {
|
|
112
|
+
this.log.debug('huaqiu api-key sync skipped: credentials service unavailable')
|
|
113
|
+
return
|
|
114
|
+
}
|
|
94
115
|
const url = `${this.baseUrl}${this.path}`
|
|
95
116
|
let res: Response | null = null
|
|
96
117
|
let info: ResolvedHostUser | null = null
|
|
@@ -111,7 +132,7 @@ export class HuaqiuApiKeySync {
|
|
|
111
132
|
if (info !== null && info.authenticated) {
|
|
112
133
|
if (info.token !== this.lastToken) {
|
|
113
134
|
this.lastToken = info.token
|
|
114
|
-
await
|
|
135
|
+
await store.set(HUAQIU_API_KEY_REF, info.token)
|
|
115
136
|
this.log.info('huaqiu api-key credential synced', { userId: info.id })
|
|
116
137
|
}
|
|
117
138
|
return
|
|
@@ -123,7 +144,7 @@ export class HuaqiuApiKeySync {
|
|
|
123
144
|
if (res !== null && res.ok) {
|
|
124
145
|
if (this.lastToken !== null) {
|
|
125
146
|
this.lastToken = null
|
|
126
|
-
await
|
|
147
|
+
await store.unset(HUAQIU_API_KEY_REF)
|
|
127
148
|
this.log.info('huaqiu api-key credential cleared', { explicit: true })
|
|
128
149
|
}
|
|
129
150
|
return
|
package/src/index.ts
CHANGED
|
@@ -80,9 +80,21 @@ export function apply(ctx: Context, config?: Partial<HuaqiuAuthConfig>): void {
|
|
|
80
80
|
const sync = new HuaqiuApiKeySync(
|
|
81
81
|
resolved.hqEdgeBaseUrl ?? '',
|
|
82
82
|
resolved.hostAuthPath ?? DEFAULT_HOST_AUTH_PATH,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
// Lazy per-round resolution: the `credentials` service may not be
|
|
84
|
+
// activated yet while this plugin boots (activation order). Direct
|
|
85
|
+
// `ctx.credentials` property access would throw "cannot get property
|
|
86
|
+
// ... without inject" in that window; `ctx.get('credentials')`
|
|
87
|
+
// returns undefined instead, so the round is skipped and the next
|
|
88
|
+
// poll retries once the service is live (self-healing, no restart).
|
|
89
|
+
() => {
|
|
90
|
+
const credentials = ctx.get('credentials') as
|
|
91
|
+
| { set(ref: string, value: string): Promise<void>; unset(ref: string): Promise<void> }
|
|
92
|
+
| undefined
|
|
93
|
+
if (credentials === undefined) return null
|
|
94
|
+
return {
|
|
95
|
+
set: (ref, value) => credentials.set(ref as CredentialRef, value),
|
|
96
|
+
unset: (ref) => credentials.unset(ref as CredentialRef),
|
|
97
|
+
}
|
|
86
98
|
},
|
|
87
99
|
{
|
|
88
100
|
debug: (msg, ctx2) => log.debug(msg, ctx2),
|