@huaqiu/dsh-auth 0.6.27 → 0.6.29

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.mjs CHANGED
@@ -208,6 +208,22 @@ function normalizeHostUser(data) {
208
208
  const HUAQIU_API_KEY_REF = "HUAQIU_API_KEY";
209
209
  /** Default poll interval: 5 s, matching the DSH browser half's host refresh. */
210
210
  const DEFAULT_HUAQIU_API_KEY_SYNC_MS = 5e3;
211
+ /** Whether a store exposes the in-memory (non-persistent) mirror channel. */
212
+ function isVolatileStore(store) {
213
+ return typeof store.setVolatile === "function";
214
+ }
215
+ /** Mirror a value, preferring the in-memory channel when the store supports it. */
216
+ async function writeVolatile(store, ref, value) {
217
+ const volatile = store;
218
+ if (typeof volatile.setVolatile === "function") await volatile.setVolatile(ref, value);
219
+ else await store.set(ref, value);
220
+ }
221
+ /** Clear a value, preferring the in-memory channel when the store supports it. */
222
+ async function clearVolatile(store, ref) {
223
+ const volatile = store;
224
+ if (typeof volatile.unsetVolatile === "function") await volatile.unsetVolatile(ref);
225
+ else await store.unset(ref);
226
+ }
211
227
  var HuaqiuApiKeySync = class {
212
228
  baseUrl;
213
229
  path;
@@ -279,16 +295,22 @@ var HuaqiuApiKeySync = class {
279
295
  if (info !== null && info.authenticated) {
280
296
  if (info.token !== this.lastToken) {
281
297
  this.lastToken = info.token;
282
- await store.set(HUAQIU_API_KEY_REF, info.token);
283
- this.log.info("huaqiu api-key credential synced", { userId: info.id });
298
+ await writeVolatile(store, HUAQIU_API_KEY_REF, info.token);
299
+ this.log.info("huaqiu api-key credential synced", {
300
+ userId: info.id,
301
+ inMemory: isVolatileStore(store)
302
+ });
284
303
  }
285
304
  return;
286
305
  }
287
306
  if (res !== null && res.ok) {
288
307
  if (this.lastToken !== null) {
289
308
  this.lastToken = null;
290
- await store.unset(HUAQIU_API_KEY_REF);
291
- this.log.info("huaqiu api-key credential cleared", { explicit: true });
309
+ await clearVolatile(store, HUAQIU_API_KEY_REF);
310
+ this.log.info("huaqiu api-key credential cleared", {
311
+ explicit: true,
312
+ inMemory: isVolatileStore(store)
313
+ });
292
314
  }
293
315
  return;
294
316
  }
@@ -1060,10 +1082,15 @@ function apply(ctx, config) {
1060
1082
  const sync = new HuaqiuApiKeySync(resolved.hqEdgeBaseUrl ?? "", resolved.hostAuthPath ?? "/api/v1/auth/token", () => {
1061
1083
  const credentials = ctx.get("credentials");
1062
1084
  if (credentials === void 0) return null;
1063
- return {
1085
+ const store = {
1064
1086
  set: (ref, value) => credentials.set(ref, value),
1065
1087
  unset: (ref) => credentials.unset(ref)
1066
1088
  };
1089
+ if (typeof credentials.setVolatile === "function") {
1090
+ store.setVolatile = (ref, value) => credentials.setVolatile(ref, value);
1091
+ store.unsetVolatile = (ref) => credentials.unsetVolatile(ref);
1092
+ }
1093
+ return store;
1067
1094
  }, {
1068
1095
  debug: (msg, ctx2) => log.debug(msg, ctx2),
1069
1096
  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.6.27",
3
+ "version": "0.6.29",
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.6.27"
36
+ "@huaqiu/dsh-plugin-log": "0.6.29"
37
37
  },
38
38
  "files": [
39
39
  "lib",
package/src/apiKeySync.ts CHANGED
@@ -35,6 +35,37 @@ export interface CredentialStore {
35
35
  unset(ref: string): Promise<void>
36
36
  }
37
37
 
38
+ /**
39
+ * A {@link CredentialStore} that can mirror a value in memory without
40
+ * persistence. When present, `HuaqiuApiKeySync` prefers it over the durable
41
+ * `set`/`unset` so the rotating HQ Edge token never reaches `.credentials.yaml`
42
+ * or its cross-process lock. The durable path remains the fallback for bundles
43
+ * whose mounted provider has not been upgraded to expose `setVolatile`.
44
+ */
45
+ export interface VolatileCredentialStore {
46
+ setVolatile(ref: string, value: string): Promise<void>
47
+ unsetVolatile(ref: string): Promise<void>
48
+ }
49
+
50
+ /** Whether a store exposes the in-memory (non-persistent) mirror channel. */
51
+ function isVolatileStore(store: CredentialStore): boolean {
52
+ return typeof (store as Partial<VolatileCredentialStore>).setVolatile === 'function'
53
+ }
54
+
55
+ /** Mirror a value, preferring the in-memory channel when the store supports it. */
56
+ async function writeVolatile(store: CredentialStore, ref: string, value: string): Promise<void> {
57
+ const volatile = store as Partial<VolatileCredentialStore>
58
+ if (typeof volatile.setVolatile === 'function') await volatile.setVolatile(ref, value)
59
+ else await store.set(ref, value)
60
+ }
61
+
62
+ /** Clear a value, preferring the in-memory channel when the store supports it. */
63
+ async function clearVolatile(store: CredentialStore, ref: string): Promise<void> {
64
+ const volatile = store as Partial<VolatileCredentialStore>
65
+ if (typeof volatile.unsetVolatile === 'function') await volatile.unsetVolatile(ref)
66
+ else await store.unset(ref)
67
+ }
68
+
38
69
  /**
39
70
  * Resolves the credential store lazily on every sync round.
40
71
  *
@@ -132,8 +163,8 @@ export class HuaqiuApiKeySync {
132
163
  if (info !== null && info.authenticated) {
133
164
  if (info.token !== this.lastToken) {
134
165
  this.lastToken = info.token
135
- await store.set(HUAQIU_API_KEY_REF, info.token)
136
- this.log.info('huaqiu api-key credential synced', { userId: info.id })
166
+ await writeVolatile(store, HUAQIU_API_KEY_REF, info.token)
167
+ this.log.info('huaqiu api-key credential synced', { userId: info.id, inMemory: isVolatileStore(store) })
137
168
  }
138
169
  return
139
170
  }
@@ -144,8 +175,8 @@ export class HuaqiuApiKeySync {
144
175
  if (res !== null && res.ok) {
145
176
  if (this.lastToken !== null) {
146
177
  this.lastToken = null
147
- await store.unset(HUAQIU_API_KEY_REF)
148
- this.log.info('huaqiu api-key credential cleared', { explicit: true })
178
+ await clearVolatile(store, HUAQIU_API_KEY_REF)
179
+ this.log.info('huaqiu api-key credential cleared', { explicit: true, inMemory: isVolatileStore(store) })
149
180
  }
150
181
  return
151
182
  }
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * mounts the browser→node credential routes on `ctx.webServer`.
6
6
  */
7
7
  import type { Context } from '@deepseek-ai/cordis'
8
- import type { CredentialRef } from '@deepseek-ai/dsh-credentials'
8
+ import type { CredentialProvider, CredentialRef } from '@deepseek-ai/dsh-credentials'
9
9
  import type {} from '@deepseek-ai/dsh-host-webserver'
10
10
  import { getLogger } from '@huaqiu/dsh-plugin-log'
11
11
  import {
@@ -13,7 +13,12 @@ import {
13
13
  resolveHostConfig,
14
14
  type HuaqiuAuthConfig,
15
15
  } from './host.js'
16
- import { HuaqiuApiKeySync, HUAQIU_API_KEY_REF } from './apiKeySync.js'
16
+ import {
17
+ HuaqiuApiKeySync,
18
+ HUAQIU_API_KEY_REF,
19
+ type CredentialStore,
20
+ type VolatileCredentialStore,
21
+ } from './apiKeySync.js'
17
22
  import { InMemoryHuaqiuAuthService, type HuaqiuAuthService } from './service.js'
18
23
  import { AUTH_ROUTE_PREFIX, createAuthHandler } from './routes.js'
19
24
 
@@ -86,15 +91,26 @@ export function apply(ctx: Context, config?: Partial<HuaqiuAuthConfig>): void {
86
91
  // ... without inject" in that window; `ctx.get('credentials')`
87
92
  // returns undefined instead, so the round is skipped and the next
88
93
  // poll retries once the service is live (self-healing, no restart).
94
+ //
95
+ // The store prefers the mounted provider's in-memory `setVolatile`
96
+ // channel when it exists, so the HQ Edge token mirrors into RAM only —
97
+ // no `.credentials.yaml` write, no cross-process lock. When the mounted
98
+ // provider predates that channel the durable `set`/`unset` fallback is
99
+ // used (the previous persistent behavior).
89
100
  () => {
90
101
  const credentials = ctx.get('credentials') as
91
- | { set(ref: string, value: string): Promise<void>; unset(ref: string): Promise<void> }
102
+ | (CredentialProvider & Partial<VolatileCredentialStore>)
92
103
  | undefined
93
104
  if (credentials === undefined) return null
94
- return {
105
+ const store: CredentialStore & Partial<VolatileCredentialStore> = {
95
106
  set: (ref, value) => credentials.set(ref as CredentialRef, value),
96
107
  unset: (ref) => credentials.unset(ref as CredentialRef),
97
108
  }
109
+ if (typeof credentials.setVolatile === 'function') {
110
+ store.setVolatile = (ref, value) => credentials.setVolatile!(ref as CredentialRef, value)
111
+ store.unsetVolatile = (ref) => credentials.unsetVolatile!(ref as CredentialRef)
112
+ }
113
+ return store
98
114
  },
99
115
  {
100
116
  debug: (msg, ctx2) => log.debug(msg, ctx2),