dsh-mobile-plugin 0.5.92 → 0.5.93

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.
@@ -0,0 +1,75 @@
1
+ // 外壳在进程启动时加载了哪些文件:顺着入口的真实引用算出来,不靠手写清单。
2
+ /**
3
+ * 「外壳换不掉」只取决于 Node 在启动时缓存了哪些模块,所以这份清单必须从引用关系里算。
4
+ *
5
+ * 手写的那份一定会落后:少一个文件,那一版更新就会**静默不生效**(界面说「已是最新」、
6
+ * 跑着的还是旧代码),多一个文件则会每次开面板都劝人重启。更新面板的「要不要重启」与开发
7
+ * 脚本的「这次动了外壳没有」都从这里取,两处不再各记一套。
8
+ *
9
+ * 只跟相对引用走(`from './x'`、`import('./x')`、`require('./x')`),第三方包不算:它们装在
10
+ * 包里自己的 node_modules,换代时整棵树不动。
11
+ */
12
+
13
+ import fs from 'node:fs'
14
+ import path from 'node:path'
15
+
16
+ const ENTRY = 'lib/index.js'
17
+ /** 三种相对引用写法。`from` 那条覆盖 `import … from` 与 `export … from`,第二条是 `import './x'` 这种只求副作用的写法。 */
18
+ const REFS = [
19
+ /from\s*['"](\.[^'"]+)['"]/g,
20
+ /import\s*['"](\.[^'"]+)['"]/g,
21
+ /import\s*\(\s*['"](\.[^'"]+)['"]\s*\)/g,
22
+ /require\s*\(\s*['"](\.[^'"]+)['"]\s*\)/g,
23
+ ]
24
+
25
+ /** 引用写法可能省扩展名或指向目录:按 Node 的次序试一遍,取第一个真的存在的。 */
26
+ function resolveFile(dir, spec) {
27
+ const base = path.resolve(dir, spec)
28
+ for (const candidate of [base, `${base}.js`, `${base}.cjs`, `${base}.mjs`, path.join(base, 'index.js')]) {
29
+ try {
30
+ if (fs.statSync(candidate).isFile()) return candidate
31
+ } catch {
32
+ // 不存在就试下一个。
33
+ }
34
+ }
35
+ return null
36
+ }
37
+
38
+ /**
39
+ * 这份包里外壳实际加载的文件(相对包根、正斜杠、排好序)。
40
+ *
41
+ * 入口不在(老一代、装坏了、`--only` 拷了一半)时返回空数组:调用方据此说「算不出指纹」,
42
+ * 而不是拿一份残缺清单去比。
43
+ */
44
+ export function shellFiles(pkgDir) {
45
+ if (!pkgDir) return []
46
+ const root = path.resolve(pkgDir)
47
+ const entry = path.join(root, ENTRY)
48
+ try {
49
+ if (!fs.statSync(entry).isFile()) return []
50
+ } catch {
51
+ return []
52
+ }
53
+ const seen = new Set()
54
+ const pending = [entry]
55
+ while (pending.length) {
56
+ const file = pending.pop()
57
+ if (seen.has(file)) continue
58
+ seen.add(file)
59
+ let source
60
+ try {
61
+ source = fs.readFileSync(file, 'utf8')
62
+ } catch {
63
+ continue
64
+ }
65
+ for (const pattern of REFS) {
66
+ pattern.lastIndex = 0
67
+ let match
68
+ while ((match = pattern.exec(source))) {
69
+ const hit = resolveFile(path.dirname(file), match[1])
70
+ if (hit && hit.startsWith(`${root}${path.sep}`)) pending.push(hit)
71
+ }
72
+ }
73
+ }
74
+ return [...seen].map(file => path.relative(root, file).split(path.sep).join('/')).sort()
75
+ }
@@ -13,8 +13,8 @@
13
13
  * 兜底那条路不下载、不解包、不校验、不写 profile,只回答「有没有新版、装完了没、盘上是什么
14
14
  * 版本」。
15
15
  *
16
- * 外壳指纹包含入口、外壳目录和外壳持有的 Tailcat 静态依赖,由换代守卫逐文件钉住;
17
- * 常驻模块必须同步进来,否则换代沿用旧代码时面板收不到重启提示。
16
+ * 外壳指纹盖住入口以及它静态引用到的全部模块(含 Tailcat 那几份常驻依赖),清单顺着引用
17
+ * 算出来、由换代守卫逐文件钉住;常驻模块必须同步进来,否则换代沿用旧代码时面板收不到重启提示。
18
18
  */
19
19
 
20
20
  import fs from 'node:fs'
@@ -23,6 +23,7 @@ import { createHash } from 'node:crypto'
23
23
  import { createRequire } from 'node:module'
24
24
  import { pathToFileURL } from 'node:url'
25
25
  import { installedPackageDir, installedVersion, profileNodeModules, compareVersion } from './manager.js'
26
+ import { shellFiles } from './loaded-files.js'
26
27
 
27
28
  const require_ = createRequire(import.meta.url)
28
29
  const { dshHome } = require_('../shared/paths.js')
@@ -69,25 +70,27 @@ function installedPackageDirSafe() {
69
70
  * 那层还是旧的。这本身通常无害(外壳的接口是加法式的),但用户有权知道,所以这里算一个
70
71
  * 指纹,把「盘上的外壳和跑着的外壳不一样」如实报给面板。
71
72
  *
72
- * 缺文件按空串算:拿不到指纹时报「没变」,免得每次开面板都喊一句要重启。
73
+ * 清单不手写:外壳加载了哪些文件由 `loaded-files.js` 顺着入口的引用算,见那边的说明。
74
+ * 算不出(入口不在)时报 `null`,调用方据此说「算不出」,免得每次开面板都喊一句要重启。
73
75
  */
74
76
  export function shellFingerprint(pkgDir) {
75
77
  if (!pkgDir) return null
76
- const files = ['lib/index.js', 'lib/shell/runtime.js', 'lib/shell/manager.js', 'lib/shell/updater.js', 'lib/shell/device-ota.js', 'lib/shell/ota-package.js', 'lib/shell/ota-signing.js', 'lib/shell/ota-public-key.js', 'lib/shell/half-route.js', 'lib/shell/half-view.js', 'lib/shell/self-check-after-swap.js', 'lib/shell/transport-host.js', 'lib/shell/first-install.js',
77
- 'lib/tailcat/wiring.cjs', 'lib/tailcat/session.cjs', 'lib/tailcat/ingress.cjs', 'lib/tailcat/host-process.cjs', 'lib/relay/tunnel-forwarder.cjs', 'lib/shared/via-header.js']
78
+ const files = shellFiles(pkgDir)
79
+ if (!files.length) return null
78
80
  const hash = createHash('sha256')
79
- let seen = 0
80
81
  for (const rel of files) {
81
82
  try {
82
83
  hash.update(rel).update(fs.readFileSync(path.join(pkgDir, rel)))
83
- seen += 1
84
84
  } catch {
85
85
  // 缺一个文件就少算一个:宁愿指纹弱一点,也不要在这里抛。
86
86
  }
87
87
  }
88
- return seen === 0 ? null : hash.digest('hex').slice(0, 16)
88
+ return hash.digest('hex').slice(0, 16)
89
89
  }
90
90
 
91
+ /** 外壳实际加载的文件清单(面板与开发脚本共用这一份,别在别处再抄一套)。 */
92
+ export { shellFiles }
93
+
91
94
  /** 本插件的 npm 包名(市场按它找依赖)。 */
92
95
  export const PACKAGE_NAME = 'dsh-mobile-plugin'
93
96
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.92",
3
+ "version": "0.5.93",
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",