dsh-quick-phrases 0.10.2 → 0.10.3

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.
Files changed (2) hide show
  1. package/index.js +138 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -9,8 +9,145 @@
9
9
  * client bundle has no bridge that reads the Host row's config, so changing a
10
10
  * value here does not drive the client behavior. Adjust the behavior in the
11
11
  * "Settings -> Quick phrases" section, which persists to localStorage.
12
+ *
13
+ * WHY THE LOADER BELOW IS ANCHOR-BASED (instead of `import z from '...'`):
14
+ * This bundle is linked into the DSH profile with `link:` (a node_modules
15
+ * symlink). Node resolves bare specifiers from the module's *real* path, so a
16
+ * plain import looks for node_modules beside the realpath and dies with
17
+ * ERR_MODULE_NOT_FOUND even though the package is installed in the profile.
18
+ * createRequire() anchored at a manifest that really exists on disk fixes it.
19
+ *
20
+ * WHY RESOLVE -> SWAP .cjs -> .mjs -> SEQUENTIAL await import():
21
+ * schemastery is dual-format. Requiring its CJS entry while its ESM entry is
22
+ * imported elsewhere in the shared Host module graph can trip Node 24's
23
+ * ERR_REQUIRE_ESM_RACE_CONDITION. createRequire is used for PATH RESOLUTION
24
+ * ONLY: resolve the CJS path, switch it to the ESM file, then load it with a
25
+ * sequential dynamic import (never Promise.all) under a top-level await so the
26
+ * schema is fully built before plugin activation continues. Same pattern as
27
+ * dsh-smart-provider/lib/dsh.js + schema.js.
28
+ */
29
+
30
+ import { createRequire } from 'node:module'
31
+ import { join, dirname } from 'node:path'
32
+ import { existsSync } from 'node:fs'
33
+ import { pathToFileURL } from 'node:url'
34
+
35
+ /**
36
+ * Candidate package.json paths for the Electron *resources* manifest, derived
37
+ * from the running DSH executable. The executable lives at:
38
+ * macOS <app>/Contents/MacOS/DSH NEXT -> <app>/Contents/Resources
39
+ * Linux <install>/dsh -> <install>/resources
40
+ * Windows <install>\DSH NEXT.exe -> <install>/resources
41
+ * @returns {string[]} absolute candidate paths, best guess first
42
+ */
43
+ function resourcesFromExecutable() {
44
+ const exe = process.env.DSH_NODE_EXECUTABLE
45
+ if (!exe) return []
46
+ // macOS bundle: walk out of Contents/MacOS to Contents/Resources.
47
+ const macContents = dirname(dirname(exe)) // .../Contents
48
+ const macResources = join(macContents, 'Resources', 'app.asar', 'package.json')
49
+ const genericResources = join(dirname(exe), 'resources', 'app.asar', 'package.json')
50
+ return [macResources, genericResources]
51
+ }
52
+
53
+ /** Well-known install roots per platform (last resort only). */
54
+ function knownInstallRoots() {
55
+ if (process.platform === 'darwin') {
56
+ return [
57
+ '/Applications/DSH NEXT.app/Contents/Resources/app.asar/package.json',
58
+ join(process.env.HOME ?? '', 'Applications', 'DSH NEXT.app', 'Contents', 'Resources', 'app.asar', 'package.json'),
59
+ ]
60
+ }
61
+ if (process.platform === 'win32') {
62
+ const localAppData = process.env.LOCALAPPDATA ?? ''
63
+ return [
64
+ join(localAppData, 'Programs', 'DSH NEXT', 'resources', 'app.asar', 'package.json'),
65
+ 'C:\\Program Files\\DSH NEXT\\resources\\app.asar\\package.json',
66
+ ]
67
+ }
68
+ return [
69
+ '/opt/DSH NEXT/resources/app.asar/package.json',
70
+ '/usr/lib/dsh-next/resources/app.asar/package.json',
71
+ ]
72
+ }
73
+
74
+ /**
75
+ * Candidate package.json anchors, most specific first. Mirrors
76
+ * dsh-smart-provider/lib/dsh.js anchors() exactly, except this bundle lives at
77
+ * the package root so its own manifest is './package.json' (the reference
78
+ * module lives in lib/ and uses '../package.json').
79
+ * Only existing paths are returned, so failures name real candidates; if none
80
+ * exist the ranked candidates are still returned for the error message.
81
+ * @returns {string[]} existing package.json paths (or all candidates)
12
82
  */
13
- import z from '@deepseek-ai/schemastery'
83
+ function anchors() {
84
+ const list = []
85
+ const profileDir = process.env.DSH_PROFILE_DIR
86
+ if (profileDir) list.push(join(profileDir, 'package.json'))
87
+ const home = process.env.DSH_HOME
88
+ const profile = process.env.DSH_PROFILE
89
+ if (home && profile) list.push(join(home, 'profiles', profile, 'package.json'))
90
+ // Inside the Electron app, process.resourcesPath is authoritative.
91
+ if (typeof process.resourcesPath === 'string') {
92
+ list.push(join(process.resourcesPath, 'app.asar', 'package.json'))
93
+ }
94
+ list.push(...resourcesFromExecutable())
95
+ list.push(...knownInstallRoots())
96
+ // Last resort: this bundle's own manifest (real disk path). Covers a
97
+ // symlinked workspace bundle where profile env vars are not exported and
98
+ // the app.asar manifest is not a real directory; the local node_modules
99
+ // symlink then resolves the package. Profile anchors still win above.
100
+ list.push(new URL('./package.json', import.meta.url).pathname)
101
+ const unique = [...new Set(list.filter(Boolean))]
102
+ const existing = unique.filter(path => {
103
+ try { return existsSync(path) } catch { return false }
104
+ })
105
+ // If nothing exists yet, still return the ranked candidates so the error
106
+ // message can explain what was tried instead of printing an empty list.
107
+ return existing.length ? existing : unique
108
+ }
109
+
110
+ /**
111
+ * Load @deepseek-ai/schemastery through the anchored manifest list above.
112
+ * createRequire() is used for path resolution only; the resolved CJS entry is
113
+ * swapped to its ESM sibling and loaded with a sequential dynamic import to
114
+ * avoid Node 24's ERR_REQUIRE_ESM_RACE_CONDITION.
115
+ * @returns {Promise<object>} the schemastery namespace (object() builder)
116
+ */
117
+ async function loadSchemastery() {
118
+ const failures = []
119
+ for (const anchor of anchors()) {
120
+ let esm
121
+ try {
122
+ const resolved = createRequire(anchor).resolve('@deepseek-ai/schemastery')
123
+ esm = resolved.endsWith('.cjs') ? resolved.slice(0, -4) + '.mjs' : resolved
124
+ if (!existsSync(esm)) throw new Error('ESM entry missing: ' + esm)
125
+ } catch (error) {
126
+ failures.push(`${anchor}: resolve failed: ${error?.code ?? error?.message ?? 'resolve failed'}`)
127
+ continue
128
+ }
129
+ let loaded
130
+ try {
131
+ // Sequential await: one anchor at a time; the ESM graph finishes before
132
+ // the next anchor is tried. Never Promise.all here (Node 24 race).
133
+ loaded = await import(pathToFileURL(esm).href)
134
+ } catch (error) {
135
+ failures.push(`${anchor}: import failed: ${error?.code ?? error?.message ?? 'import failed'}`)
136
+ continue
137
+ }
138
+ for (const candidate of [loaded, loaded?.default]) {
139
+ if (candidate && typeof candidate.object === 'function') return candidate
140
+ }
141
+ failures.push(`${anchor}: resolved without a usable object() builder`)
142
+ }
143
+ throw new Error(
144
+ `dsh-quick-phrases could not load @deepseek-ai/schemastery. Tried: ${failures.join('; ')}`,
145
+ )
146
+ }
147
+
148
+ // Top-level await: Config must be fully built before this module is evaluated
149
+ // by the host, otherwise plugin activation races ahead of the schema.
150
+ const z = await loadSchemastery()
14
151
 
15
152
  export const Config = z.object({
16
153
  // Where the phrase row lives; mirrors client.js PLACEMENTS.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-quick-phrases",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "DSH 快捷短语:输入框旁一排短语按钮,点按即发送;短语行支持显示整排 / 收成胶囊 / 彻底隐藏三态,管理入口可换齿轮图标或隐藏,输入框有内容时自动收起,单条短语可隐藏;管理页可增删改、拖动排序,显示名称与发送内容分离,显示位置可选。",
5
5
  "license": "MIT",
6
6
  "author": {