dsh-mobile-plugin 0.5.79 → 0.5.81

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.
@@ -34,8 +34,26 @@ const HEALTH_POLL_INTERVAL_MS = 10000;
34
34
  * @param {() => Promise<number>} deps.resolveLanPort
35
35
  * @param {(cfg: object) => Promise<object>} deps.buildConfig 把它组装的配置交给谁下发(`lib/impl.js` 组装)
36
36
  */
37
- function createSidecarLifecycle(deps) {
38
- const { ctx, lanPortInUse, resolveLanPort, buildConfig } = deps;
37
+ function createSidecarLifecycle(deps = {}) {
38
+ const { ctx } = deps;
39
+ // 传输进程归**稳定外壳**持有时,配置来源会在每一次换代后由新的一代重新注入:所以这里
40
+ // 做成可变对象,构造期给的值只是初值(外壳起的那一次用)。换代只 `setProviders`+`sync`,
41
+ // 配置签名不变 → worker 的 configure 不重建隧道、不重建局域网,连接跨代存活。
42
+ const providers = {
43
+ lanPortInUse: typeof deps.lanPortInUse === 'function' ? deps.lanPortInUse : () => 0,
44
+ resolveLanPort: typeof deps.resolveLanPort === 'function' ? deps.resolveLanPort : () => Promise.resolve(0),
45
+ buildConfig: typeof deps.buildConfig === 'function' ? deps.buildConfig : () => ({}),
46
+ onLanState: typeof deps.onLanState === 'function' ? deps.onLanState : null,
47
+ pluginLibDir: typeof deps.pluginLibDir === 'string' && deps.pluginLibDir ? deps.pluginLibDir : __dirname,
48
+ };
49
+ const setProviders = (next) => {
50
+ if (!next || typeof next !== 'object') return;
51
+ if (typeof next.lanPortInUse === 'function') providers.lanPortInUse = next.lanPortInUse;
52
+ if (typeof next.resolveLanPort === 'function') providers.resolveLanPort = next.resolveLanPort;
53
+ if (typeof next.buildConfig === 'function') providers.buildConfig = next.buildConfig;
54
+ if (typeof next.onLanState === 'function') providers.onLanState = next.onLanState;
55
+ if (typeof next.pluginLibDir === 'string' && next.pluginLibDir) providers.pluginLibDir = next.pluginLibDir;
56
+ };
39
57
 
40
58
  const sidecarToken = randomBytes(32).toString('hex')
41
59
  // `let` 而非 `const`:sidecar 自重启要换掉整个实例(见 restartSidecar)。
@@ -104,7 +122,7 @@ function createSidecarLifecycle(deps) {
104
122
  const applyTunnelState = (message) => {
105
123
  if (!message || !message.tunnel) return
106
124
  sidecarSnapshot = { ...sidecarSnapshot, tunnel: message.tunnel, ...(message.lan ? { lan: message.lan } : {}) }
107
- if (message.lan) deps.onLanState?.(message.lan)
125
+ if (message.lan) providers.onLanState?.(message.lan)
108
126
  noteTunnelConnected(message.tunnel.state === 'connected')
109
127
  // 同一份历史里「择路选中了非队首」这件事也在这条路上报上来,写回由装配层订阅。
110
128
  noteRouteAdopted(message.tunnel.route)
@@ -133,15 +151,15 @@ function createSidecarLifecycle(deps) {
133
151
  // 我们最近一次**下发给 worker** 的局域网端口(0 = 还没下发过)。它是 getOwnLanPort 的判据。
134
152
  let configuredLanPort = 0
135
153
  const applySidecarConfig = async () => {
136
- await resolveLanPort()
137
- const config = buildConfig(lanPortInUse())
154
+ await providers.resolveLanPort()
155
+ const config = providers.buildConfig(providers.lanPortInUse())
138
156
  configuredLanPort = Number.isInteger(config.lanPort) ? config.lanPort : 0
139
157
  // 失败必须落账(`/api/mobile/v1/relay/diagnostics` 的 `apply` 那一格):所有下发都经过
140
158
  // 这里 —— 手机改地址、电脑面板改地址、启动时那次 —— 而调用方几乎都 `.catch(() => …)`
141
159
  // 掉。换地址失败的现场是「保存成功、然后永久离线」,台账是唯一能说清原因的地方。
142
160
  try {
143
161
  sidecarSnapshot = await sidecar.configure(config)
144
- if (sidecarSnapshot?.lan) deps.onLanState?.(sidecarSnapshot.lan)
162
+ if (sidecarSnapshot?.lan) providers.onLanState?.(sidecarSnapshot.lan)
145
163
  } catch (error) {
146
164
  applyLedger.recordFailure(error, config.relayUrl)
147
165
  throw error
@@ -180,11 +198,12 @@ function createSidecarLifecycle(deps) {
180
198
  // 本代 `lib/` 目录。默认用本文件位置推(同目录就是 lib/),允许调用方覆盖成
181
199
  // 「当前活跃的那一代」——代际目录里的副本推出来的是它自己,两者通常相同,
182
200
  // 但显式传入能让「清缓存清的是不是在跑的那一份」这件事变得确定。
183
- const PLUGIN_LIB_DIR = (deps.pluginLibDir || __dirname).replace(/[\\/]+$/, '') + path.sep
201
+ const pluginLibDirPrefix = () => String(providers.pluginLibDir || __dirname).replace(/[\\/]+$/, '') + path.sep
184
202
  const purgePluginLibCache = () => {
185
203
  let purged = 0
204
+ const prefix = pluginLibDirPrefix()
186
205
  for (const key of Object.keys(require.cache)) {
187
- if (!key.startsWith(PLUGIN_LIB_DIR)) continue
206
+ if (!key.startsWith(prefix)) continue
188
207
  delete require.cache[key]
189
208
  purged += 1
190
209
  }
@@ -230,7 +249,13 @@ function createSidecarLifecycle(deps) {
230
249
  // 隧道/局域网状态的权威回写点(状态跳变的即时回写见上面的 applyTunnelState)。
231
250
  const onTunnelUp = (fn) => { tunnelUpSubscribers.add(fn); return () => tunnelUpSubscribers.delete(fn) }
232
251
 
252
+ // 幂等:外壳持有传输进程,换代时新的一代也会调一次 start(),但 worker 只能有一份——
253
+ // 已启动就直接返回空 disposer,绝不重新 fork(那等于换代把隧道断了)。
254
+ let started = false
255
+ let healthTimer = null
233
256
  const start = () => {
257
+ if (started) return () => {}
258
+ started = true
234
259
  let disposed = false
235
260
  const boot = async () => {
236
261
  try {
@@ -254,7 +279,7 @@ function createSidecarLifecycle(deps) {
254
279
  stateFromPoll = false
255
280
  sidecarState = `error: ${error instanceof Error ? error.message : String(error)}`
256
281
  })
257
- const healthTimer = setInterval(async () => {
282
+ healthTimer = setInterval(async () => {
258
283
  try {
259
284
  const st = await sidecar.request('/status')
260
285
  // 轮询拿到的才是权威状态,必须回写 sidecarSnapshot:`configure()` 返回的是
@@ -262,7 +287,7 @@ function createSidecarLifecycle(deps) {
262
287
  // 'connecting'。原来这次轮询只用来判断隧道连接跳变,结果被直接丢弃,于是
263
288
  // /api/relay/status 永远谎报局域网"没起来",尽管 0.0.0.0:lanPort 早在监听。
264
289
  if (st) sidecarSnapshot = st
265
- if (st?.lan) deps.onLanState?.(st.lan)
290
+ if (st?.lan) providers.onLanState?.(st.lan)
266
291
  // 轮询通了就等于「worker 就在那儿、这一下请求也成了」:若上一格的 error 是轮询自己
267
292
  // 写上去的,这里必须抹回 healthy —— 抖一下不该永久挂一行红字,等用户来点重启。
268
293
  if (stateFromPoll) { sidecarState = 'healthy'; stateFromPoll = false }
@@ -281,12 +306,28 @@ function createSidecarLifecycle(deps) {
281
306
  markSidecarDown(error)
282
307
  }
283
308
  }, HEALTH_POLL_INTERVAL_MS)
284
- return () => { disposed = true; void sidecar.stop(); clearInterval(healthTimer) }
309
+ return () => {
310
+ if (!started) return
311
+ started = false
312
+ disposed = true
313
+ if (healthTimer !== null) { clearInterval(healthTimer); healthTimer = null }
314
+ void sidecar.stop()
315
+ }
316
+ }
317
+ /** 外壳卸载时才收掉传输进程;换代(换 providers)不调用它,隧道因此跨代存活。 */
318
+ const stop = async () => {
319
+ started = false
320
+ if (healthTimer !== null) { clearInterval(healthTimer); healthTimer = null }
321
+ try { await sidecar.stop() } catch { /* 已经没了 */ }
285
322
  }
286
323
 
287
324
  return {
288
325
  sidecarToken,
289
326
  start,
327
+ stop,
328
+ setProviders,
329
+ /** 传输进程是否已启动(外壳持有、换代时用来判断「只需补推配置」还是「首次启动」)。 */
330
+ isStarted: () => started,
290
331
  syncSidecar,
291
332
  restartSidecar,
292
333
  onTunnelUp,
@@ -40,7 +40,11 @@ function chooseTailcatTarget({ tunnelPort }) {
40
40
  }
41
41
 
42
42
  function mountTailcat(options) {
43
- const { mount, warn, state, saveState, lanEnabled, lanPort, webPort, webHost, hostName, ctx, name } = options;
43
+ const { mount, warn, saveState, lanEnabled, lanPort, webPort, webHost, hostName, ctx, name } = options;
44
+ // 状态来源可由调用方在运行期提供(归外壳持有时,换代换的就是这一格):老的直接传 `state`
45
+ // 的用法保留为兜底,测试与旧调用方行为不变。
46
+ const readState = typeof options.getState === 'function' ? options.getState : () => options.state;
47
+ const persistState = typeof saveState === 'function' ? saveState : () => {};
44
48
  // 落点代理与入口各是一条回环监听,都得活着;任何一个起不来就只是这块不可用(`mount` 兜住)。
45
49
  const forwarder = mount('隧道落点代理', () =>
46
50
  createTunnelForwarder({ upstreamPort: webPort, upstreamHost: webHost, log: message => warn(message) }),
@@ -90,10 +94,11 @@ function mountTailcat(options) {
90
94
  .then(([port]) => {
91
95
  if (closed || !port) return;
92
96
  session = createTailcatSession({
93
- readState: () => state,
97
+ readState: () => readState(),
94
98
  writeState: patch => {
95
- Object.assign(state, patch);
96
- saveState(state);
99
+ const current = readState();
100
+ Object.assign(current, patch);
101
+ persistState(current);
97
102
  },
98
103
  localPort: port,
99
104
  hostName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.79",
3
+ "version": "0.5.81",
4
4
  "description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
5
5
  "license": "MIT",
6
6
  "icon": "./icon.png",
@@ -91,7 +91,6 @@
91
91
  "peerDependencies": {
92
92
  "@deepseek-ai/cordis": ">=4.0.1",
93
93
  "@deepseek-ai/dsh-client-locale": "^0.1.1-rc.2 || ^0.2.0-rc.1 || ^0.2.1-rc.2 || ^0.2.2-rc.1",
94
- "@deepseek-ai/dsh-client-runtime": ">=0.1.1-rc.2",
95
94
  "@deepseek-ai/dsh-settings": "^0.1.1-rc.2 || ^0.2.0-rc.1 || ^0.2.1-rc.2 || ^0.2.2-rc.1",
96
95
  "@deepseek-ai/dsh-tools": "^0.1.1-rc.2 || ^0.2.0-rc.1 || ^0.2.1-rc.2 || ^0.2.2-rc.1",
97
96
  "@deepseek-ai/schemastery": "^3.18.1"
@@ -105,7 +104,7 @@
105
104
  "immediately": true,
106
105
  "inject": [
107
106
  "@deepseek-ai/dsh-client-locale",
108
- "@deepseek-ai/dsh-client-runtime"
107
+ "@deepseek-ai/dsh-client-ui-renderer"
109
108
  ]
110
109
  }
111
110
  },