dsh-plugin-dingtalk-bridge 0.0.0-stage → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +183 -2
- package/client.js +2519 -0
- package/cordis.patch.yml +23 -0
- package/docs/images/auth-device.png +0 -0
- package/docs/images/auth-loopback.png +0 -0
- package/docs/images/auth-qr.png +0 -0
- package/docs/images/confirm-modal.png +0 -0
- package/docs/images/duplicate-hint.png +0 -0
- package/docs/images/group-at-reply.png +0 -0
- package/docs/images/help-view.png +0 -0
- package/docs/images/icon-source.png +0 -0
- package/docs/images/icon.png +0 -0
- package/docs/images/inbox-panel.png +0 -0
- package/docs/images/inbox-real.png +0 -0
- package/docs/images/settings.png +0 -0
- package/docs/images/sources-collapsed.png +0 -0
- package/docs/images/sources-expanded.png +0 -0
- package/docs/images/window-setting.png +0 -0
- package/docs//344/275/277/347/224/250/350/257/264/346/230/216.md +1841 -0
- package/icon-128.png +0 -0
- package/icon-64.png +0 -0
- package/icon.png +0 -0
- package/icon.svg +6 -0
- package/index.js +37 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +78 -4
- package/src/host/config.js +147 -0
- package/src/host/dws.js +277 -0
- package/src/host/service.js +2352 -0
- package/src/host/suggest.js +307 -0
package/src/host/dws.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { delimiter, isAbsolute, join, dirname } from 'node:path'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* dws CLI 的最小封装:
|
|
7
|
+
* - Windows 上 dws 是 .cmd 包装脚本:优先解析出它内部的 node 入口脚本,直接用 node 启动,
|
|
8
|
+
* 彻底绕开 cmd.exe 的参数引用问题(含空格的中文回复文本会被 cmd 拆断);
|
|
9
|
+
* - 解析失败时退回 cmd.exe /c 方式;
|
|
10
|
+
* - 统一 UTF-8 代码页,输出解码带 GBK 兜底;
|
|
11
|
+
* - 提供行读取器(dws event consume 的 NDJSON 流)与宽松 JSON 解析。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** 把子进程输出块解码为文本,UTF-8 优先,非法序列回退 GBK。 */
|
|
15
|
+
export function toText(chunk) {
|
|
16
|
+
if (typeof chunk === 'string') return chunk
|
|
17
|
+
try {
|
|
18
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(chunk)
|
|
19
|
+
} catch {
|
|
20
|
+
try {
|
|
21
|
+
return new TextDecoder('gbk').decode(chunk)
|
|
22
|
+
} catch {
|
|
23
|
+
return Buffer.from(chunk).toString('utf8')
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 解析实际执行的命令:留空即 `dws`。 */
|
|
29
|
+
export function resolveDwsCommand(configured) {
|
|
30
|
+
const value = typeof configured === 'string' ? configured.trim() : ''
|
|
31
|
+
return value === '' ? 'dws' : value
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 在 PATH 里找可执行文件(含 PATHEXT 后缀),找不到返回 ''。 */
|
|
35
|
+
function whichSync(command) {
|
|
36
|
+
if (isAbsolute(command) || command.includes('/') || command.includes('\\')) {
|
|
37
|
+
return existsSync(command) ? command : ''
|
|
38
|
+
}
|
|
39
|
+
const pathValue = process.env.PATH || process.env.Path || ''
|
|
40
|
+
// Windows 上同名文件可能同时有 `dws`(bash 垫片)与 `dws.cmd`,必须优先 .cmd/.exe。
|
|
41
|
+
const exts = process.platform === 'win32' ? ['.cmd', '.bat', '.exe', ''] : ['']
|
|
42
|
+
for (const dir of pathValue.split(delimiter)) {
|
|
43
|
+
if (dir.trim() === '') continue
|
|
44
|
+
for (const ext of exts) {
|
|
45
|
+
const candidate = join(dir, command + ext)
|
|
46
|
+
if (existsSync(candidate)) return candidate
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return ''
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 若目标是 npm/pnpm 生成的 .cmd 垫片,解析出真正的 node 入口脚本,
|
|
54
|
+
* 这样可以直接 spawn(node, [script, ...args]),参数原文透传不再经过 cmd.exe。
|
|
55
|
+
* @returns {{command:string, prefix:string[]} | null}
|
|
56
|
+
*/
|
|
57
|
+
function resolveNodeShim(command) {
|
|
58
|
+
if (process.platform !== 'win32') return null
|
|
59
|
+
const target = whichSync(command)
|
|
60
|
+
if (target === '' || !/\.(cmd|bat)$/i.test(target)) return null
|
|
61
|
+
let text = ''
|
|
62
|
+
try {
|
|
63
|
+
text = readFileSync(target, 'utf8')
|
|
64
|
+
} catch {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
// 匹配 "%dp0%\path\to\entry.js" 或 "%~dp0\entry.js" 这类写法。
|
|
68
|
+
const match = /"?%[~]?dp0%?\\?([^"\r\n]+?\.(?:js|mjs|cjs))"?/.exec(text)
|
|
69
|
+
if (!match) return null
|
|
70
|
+
const scriptPath = join(dirname(target), match[1])
|
|
71
|
+
if (!existsSync(scriptPath)) return null
|
|
72
|
+
const siblingNode = join(dirname(target), 'node.exe')
|
|
73
|
+
const nodePath = existsSync(siblingNode) ? siblingNode : process.execPath
|
|
74
|
+
return { command: nodePath, prefix: [scriptPath] }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** cmd.exe 单参数引用:含空格或特殊字符时用双引号包裹,内层引号按 cmd 规则转义为 \"。 */
|
|
78
|
+
function quoteForCmd(value) {
|
|
79
|
+
const text = String(value)
|
|
80
|
+
if (text === '') return '""'
|
|
81
|
+
if (!/[\s"&|<>^]/.test(text)) return text
|
|
82
|
+
return `"${text.replace(/"/g, '\\"')}"`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 规范化参数列表(数组或对象展开后的形式)。 */
|
|
86
|
+
export function normalizeArgs(args) {
|
|
87
|
+
if (Array.isArray(args)) return args.map((item) => String(item))
|
|
88
|
+
if (args && typeof args === 'object') {
|
|
89
|
+
const out = []
|
|
90
|
+
for (const [key, value] of Object.entries(args)) {
|
|
91
|
+
if (value === undefined || value === null || value === false) continue
|
|
92
|
+
if (value === true) out.push(key)
|
|
93
|
+
else if (Array.isArray(value)) out.push(key, value.map((v) => String(v)).join(','))
|
|
94
|
+
else out.push(key, String(value))
|
|
95
|
+
}
|
|
96
|
+
return out
|
|
97
|
+
}
|
|
98
|
+
return []
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 启动 dws 子进程(不等待)。 */
|
|
102
|
+
export function spawnDws(command, args, options = {}) {
|
|
103
|
+
const resolved = resolveDwsCommand(command)
|
|
104
|
+
const argv = normalizeArgs(args)
|
|
105
|
+
const env = { ...process.env, ...(options.env || {}) }
|
|
106
|
+
const spawnOptions = {
|
|
107
|
+
env,
|
|
108
|
+
windowsHide: true,
|
|
109
|
+
stdio: [options.stdin === false ? 'ignore' : 'pipe', 'pipe', 'pipe'],
|
|
110
|
+
cwd: options.cwd || process.cwd(),
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (process.platform === 'win32' && !/\.exe$/i.test(resolved)) {
|
|
114
|
+
// 首选:直接跑 .cmd 垫片背后的 node 入口,参数原样透传(含空格的中文不会丢)。
|
|
115
|
+
const shim = resolveNodeShim(resolved)
|
|
116
|
+
if (shim) return spawn(shim.command, [...shim.prefix, ...argv], spawnOptions)
|
|
117
|
+
|
|
118
|
+
const line = [quoteForCmd(resolved), ...argv.map(quoteForCmd)].join(' ')
|
|
119
|
+
const shell = process.env.ComSpec || 'cmd.exe'
|
|
120
|
+
return spawn(shell, ['/d', '/s', '/c', `chcp 65001>nul & ${line}`], spawnOptions)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return spawn(resolved, argv, spawnOptions)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 结束进程树(Windows 必须走 taskkill,否则 dws 的子进程会成为孤儿)。 */
|
|
127
|
+
export function killTree(child) {
|
|
128
|
+
if (!child || child.killed === true || child.exitCode !== null) return
|
|
129
|
+
try {
|
|
130
|
+
if (process.platform === 'win32' && Number.isFinite(child.pid)) {
|
|
131
|
+
spawn(process.env.ComSpec || 'cmd.exe', ['/d', '/s', '/c', `taskkill /pid ${child.pid} /T /F`], {
|
|
132
|
+
windowsHide: true,
|
|
133
|
+
stdio: 'ignore',
|
|
134
|
+
})
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
child.kill('SIGTERM')
|
|
138
|
+
} catch {
|
|
139
|
+
// 进程可能已经退出,忽略。
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 执行一次 dws 命令并收集输出。
|
|
145
|
+
* @returns {Promise<{code:number|null, stdout:string, stderr:string, timedOut:boolean, error?:string}>}
|
|
146
|
+
*/
|
|
147
|
+
export function runDws(command, args, options = {}) {
|
|
148
|
+
const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : 30000
|
|
149
|
+
|
|
150
|
+
return new Promise((resolve) => {
|
|
151
|
+
let child
|
|
152
|
+
try {
|
|
153
|
+
child = spawnDws(command, args, options)
|
|
154
|
+
} catch (error) {
|
|
155
|
+
resolve({ code: null, stdout: '', stderr: '', timedOut: false, error: String(error?.message || error) })
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let stdout = ''
|
|
160
|
+
let stderr = ''
|
|
161
|
+
let timedOut = false
|
|
162
|
+
let settled = false
|
|
163
|
+
|
|
164
|
+
const finish = (code) => {
|
|
165
|
+
if (settled) return
|
|
166
|
+
settled = true
|
|
167
|
+
clearTimeout(timer)
|
|
168
|
+
resolve({ code, stdout, stderr, timedOut })
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const timer = setTimeout(() => {
|
|
172
|
+
timedOut = true
|
|
173
|
+
killTree(child)
|
|
174
|
+
finish(null)
|
|
175
|
+
}, timeoutMs)
|
|
176
|
+
|
|
177
|
+
child.stdout?.on('data', (chunk) => {
|
|
178
|
+
stdout += toText(chunk)
|
|
179
|
+
})
|
|
180
|
+
child.stderr?.on('data', (chunk) => {
|
|
181
|
+
stderr += toText(chunk)
|
|
182
|
+
})
|
|
183
|
+
child.on('error', (error) => {
|
|
184
|
+
stderr += `\n${String(error?.message || error)}`
|
|
185
|
+
finish(null)
|
|
186
|
+
})
|
|
187
|
+
child.on('close', (code) => {
|
|
188
|
+
finish(code)
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
// dws 的交互式命令(如 device flow)不会读 stdin,这里直接关闭避免挂起。
|
|
192
|
+
try {
|
|
193
|
+
child.stdin?.end()
|
|
194
|
+
} catch {
|
|
195
|
+
// 忽略。
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** 逐行切分流式输出(dws event consume -f ndjson)。 */
|
|
201
|
+
export function createLineReader(onLine) {
|
|
202
|
+
let buffer = ''
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
push(chunk) {
|
|
206
|
+
buffer += chunk
|
|
207
|
+
while (true) {
|
|
208
|
+
const index = buffer.indexOf('\n')
|
|
209
|
+
if (index < 0) break
|
|
210
|
+
const line = buffer.slice(0, index).replace(/\r$/, '')
|
|
211
|
+
buffer = buffer.slice(index + 1)
|
|
212
|
+
if (line.trim() !== '') {
|
|
213
|
+
try {
|
|
214
|
+
onLine(line)
|
|
215
|
+
} catch {
|
|
216
|
+
// 单行处理失败不影响后续消息。
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
flush() {
|
|
222
|
+
const line = buffer.trim()
|
|
223
|
+
buffer = ''
|
|
224
|
+
if (line !== '') {
|
|
225
|
+
try {
|
|
226
|
+
onLine(line)
|
|
227
|
+
} catch {
|
|
228
|
+
// 忽略。
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 宽松 JSON 解析:忽略日志前缀,截取首个 `{` 到末个 `}`。 */
|
|
236
|
+
export function parseLooseJson(text) {
|
|
237
|
+
const raw = typeof text === 'string' ? text : ''
|
|
238
|
+
const start = raw.indexOf('{')
|
|
239
|
+
const end = raw.lastIndexOf('}')
|
|
240
|
+
if (start < 0 || end <= start) return null
|
|
241
|
+
try {
|
|
242
|
+
return JSON.parse(raw.slice(start, end + 1))
|
|
243
|
+
} catch {
|
|
244
|
+
return null
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** 从命令输出里取一段可读的错误信息。 */
|
|
249
|
+
export function describeFailure(result) {
|
|
250
|
+
const payload = parseLooseJson(result?.stdout) || parseLooseJson(result?.stderr)
|
|
251
|
+
const candidates = [
|
|
252
|
+
payload?.error?.message,
|
|
253
|
+
payload?.message,
|
|
254
|
+
typeof payload?.error === 'string' ? payload.error : '',
|
|
255
|
+
(result?.stderr || '').trim(),
|
|
256
|
+
(result?.stdout || '').trim(),
|
|
257
|
+
]
|
|
258
|
+
for (const item of candidates) {
|
|
259
|
+
if (typeof item === 'string' && item.trim() !== '') {
|
|
260
|
+
return item.trim().split('\n').slice(-6).join('\n')
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (result?.timedOut) return 'dws 命令执行超时'
|
|
264
|
+
return `dws 命令退出码 ${result?.code}`
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** device flow 输出解析(登录引导用,保留与参考插件一致的容错)。 */
|
|
268
|
+
export function parseDeviceFlow(output) {
|
|
269
|
+
const text = typeof output === 'string' ? output : ''
|
|
270
|
+
const code = /授权码[::]\s*([A-Z0-9-]+)/i.exec(text)?.[1] || ''
|
|
271
|
+
const url =
|
|
272
|
+
/(https?:\/\/[^\s]*login\.dingtalk\.com[^\s]*)/i.exec(text)?.[1] ||
|
|
273
|
+
/(https?:\/\/[^\s]*open-dev\.dingtalk\.com[^\s]*)/i.exec(text)?.[1] ||
|
|
274
|
+
''
|
|
275
|
+
if (code === '' && url === '') return null
|
|
276
|
+
return { code, url }
|
|
277
|
+
}
|