dsh-open-desktop 0.4.1 → 0.4.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/host/exec.js +14 -4
- package/host/paths.js +1 -0
- package/host/refresh.js +17 -8
- package/package.json +1 -1
package/host/exec.js
CHANGED
|
@@ -27,23 +27,33 @@ async function confine(ctx, argv, workspaceRoot) {
|
|
|
27
27
|
return argv
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export async function runCommand(ctx, commandLine, { workspaceRoot = '', timeoutMs =
|
|
30
|
+
export async function runCommand(ctx, commandLine, { workspaceRoot = '', timeoutMs = 30_000 } = {}) {
|
|
31
31
|
const parts = tokenize(commandLine)
|
|
32
32
|
if (parts.length === 0) throw Object.assign(new Error('命令为空'), { code: 'bad-command' })
|
|
33
33
|
const argv = await confine(ctx, parts, workspaceRoot)
|
|
34
|
+
const limit = Math.min(120_000, Math.max(1000, Number(timeoutMs) || 30_000))
|
|
34
35
|
return new Promise((resolve, reject) => {
|
|
35
36
|
execFile(
|
|
36
37
|
argv[0],
|
|
37
38
|
argv.slice(1),
|
|
38
39
|
{
|
|
39
|
-
timeout:
|
|
40
|
+
timeout: limit,
|
|
40
41
|
maxBuffer: 512 * 1024,
|
|
41
42
|
cwd: workspaceRoot && workspaceRoot !== '' ? workspaceRoot : undefined,
|
|
42
43
|
env: { ...process.env, LANG: 'en_US.UTF-8' },
|
|
43
44
|
},
|
|
44
|
-
(error, stdout) => {
|
|
45
|
+
(error, stdout, stderr) => {
|
|
45
46
|
if (error) {
|
|
46
|
-
|
|
47
|
+
const stderrText = String((stderr && String(stderr).trim()) || (error && error.stderr) || '').trim()
|
|
48
|
+
const timedOut = error.killed === true || error.signal === 'SIGTERM' || error.code === 'ETIMEDOUT'
|
|
49
|
+
const firstLine = stderrText.split('\n').map((line) => line.trim()).filter(Boolean)[0] || ''
|
|
50
|
+
const reason = timedOut
|
|
51
|
+
? `超时(${limit}ms 未完成,已终止)`
|
|
52
|
+
: (firstLine.slice(0, 300) || String(error.code || '未知错误'))
|
|
53
|
+
reject(Object.assign(
|
|
54
|
+
new Error(`命令「${String(commandLine).slice(0, 120)}」执行失败:${reason}`),
|
|
55
|
+
{ code: 'script-failed', stderr: stderrText.slice(0, 4000) },
|
|
56
|
+
))
|
|
47
57
|
return
|
|
48
58
|
}
|
|
49
59
|
resolve(String(stdout ?? ''))
|
package/host/paths.js
CHANGED
|
@@ -69,6 +69,7 @@ export function normalizeRefresh(raw) {
|
|
|
69
69
|
if (file !== '') refresh.file = file
|
|
70
70
|
if (url !== '') refresh.url = url
|
|
71
71
|
if (source.params && typeof source.params === 'object') refresh.params = { ...source.params }
|
|
72
|
+
if (Number.isFinite(Number(source.timeoutMs))) refresh.timeoutMs = clampInt(Number(source.timeoutMs), 1000, 120_000, 30_000)
|
|
72
73
|
return refresh
|
|
73
74
|
}
|
|
74
75
|
|
package/host/refresh.js
CHANGED
|
@@ -36,6 +36,7 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
36
36
|
const generations = new Map()
|
|
37
37
|
const signatures = new Map()
|
|
38
38
|
const running = new Set()
|
|
39
|
+
const queued = new Set()
|
|
39
40
|
const templates = inject?.templates ?? TEMPLATES
|
|
40
41
|
const pathOf = inject?.pathOf ?? (() => '')
|
|
41
42
|
const onCommand = inject?.runCommand ?? ((command, options) => runCommand(ctx, command, options))
|
|
@@ -66,6 +67,8 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
66
67
|
return 'none'
|
|
67
68
|
}
|
|
68
69
|
const snapshot = () => ({ paused, data: Object.fromEntries([...data.entries()]) })
|
|
70
|
+
// 命令类刷新默认 30s:要大于脚本自己的网络超时(如 12s),否则脚本兜底前就被 Host 杀掉。
|
|
71
|
+
const timeoutOf = (card) => Math.max(1000, Math.min(120_000, Number(card?.refresh?.timeoutMs) || 30_000))
|
|
69
72
|
|
|
70
73
|
const resolveCardFile = (cardId, file) => {
|
|
71
74
|
const dir = pathOf(cardId)
|
|
@@ -74,16 +77,17 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
74
77
|
|
|
75
78
|
const pull = async (card, spec) => {
|
|
76
79
|
const workspaceRoot = pathOf(card.id)
|
|
80
|
+
const timeoutMs = timeoutOf(card)
|
|
77
81
|
if (spec.template) {
|
|
78
82
|
const template = templates[spec.template] ?? null
|
|
79
83
|
if (template === null) throw Object.assign(new Error(`没有 ${spec.template} 这个模板`), { code: 'unknown-template' })
|
|
80
84
|
return template.fetch(card, {
|
|
81
|
-
timeoutMs
|
|
82
|
-
runCommand: (command, options) => onCommand(command, { workspaceRoot, timeoutMs
|
|
85
|
+
timeoutMs,
|
|
86
|
+
runCommand: (command, options) => onCommand(command, { workspaceRoot, timeoutMs, ...options }),
|
|
83
87
|
})
|
|
84
88
|
}
|
|
85
89
|
if (spec.command) {
|
|
86
|
-
const output = await onCommand(spec.command, { workspaceRoot, timeoutMs
|
|
90
|
+
const output = await onCommand(spec.command, { workspaceRoot, timeoutMs })
|
|
87
91
|
return parseValue(output)
|
|
88
92
|
}
|
|
89
93
|
if (spec.file) {
|
|
@@ -91,7 +95,7 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
91
95
|
return parseValue(text)
|
|
92
96
|
}
|
|
93
97
|
if (spec.url) {
|
|
94
|
-
const response = await fetchImpl(spec.url, { signal: AbortSignal.timeout(
|
|
98
|
+
const response = await fetchImpl(spec.url, { signal: AbortSignal.timeout(timeoutMs), headers: { accept: 'application/json' } })
|
|
95
99
|
if (!response.ok) throw Object.assign(new Error(`HTTP ${response.status}`), { code: 'http-status' })
|
|
96
100
|
return response.json()
|
|
97
101
|
}
|
|
@@ -100,7 +104,8 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
100
104
|
|
|
101
105
|
const runOne = async (card) => {
|
|
102
106
|
if (paused || started === false) return
|
|
103
|
-
|
|
107
|
+
// 正在刷新时再次点击「重试」:排队一次,等当前这次结束后立刻再跑,而不是静默丢弃
|
|
108
|
+
if (running.has(card.id)) { queued.add(card.id); return }
|
|
104
109
|
if (!hasProvider(card)) {
|
|
105
110
|
if (data.delete(card.id)) emit()
|
|
106
111
|
return
|
|
@@ -117,12 +122,16 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
117
122
|
fetchedAt: previous?.fetchedAt ?? 0,
|
|
118
123
|
ttl: previous?.ttl ?? everyMs * 2,
|
|
119
124
|
sourceRef: sourceRefOf(card),
|
|
120
|
-
error: { code: String(error?.code ?? 'fetch-failed'), message: String(error?.message ?? error).slice(0,
|
|
125
|
+
error: { code: String(error?.code ?? 'fetch-failed'), message: String(error?.message ?? error).slice(0, 800), at: Date.now() },
|
|
121
126
|
})
|
|
122
127
|
} finally {
|
|
123
128
|
running.delete(card.id)
|
|
124
129
|
}
|
|
125
130
|
emit()
|
|
131
|
+
if (queued.delete(card.id)) {
|
|
132
|
+
const latest = cards?.get?.(card.id) ?? card
|
|
133
|
+
void runOne(latest)
|
|
134
|
+
}
|
|
126
135
|
}
|
|
127
136
|
|
|
128
137
|
const clearTimer = (id) => {
|
|
@@ -171,9 +180,9 @@ export function createRefresh({ ctx, cards, inject } = {}) {
|
|
|
171
180
|
|
|
172
181
|
return {
|
|
173
182
|
start: (list) => { started = true; sync(list) },
|
|
174
|
-
stop: () => { started = false; for (const id of [...timers.keys()]) clearTimer(id); timers.clear(); signatures.clear() },
|
|
183
|
+
stop: () => { started = false; queued.clear(); for (const id of [...timers.keys()]) clearTimer(id); timers.clear(); signatures.clear() },
|
|
175
184
|
sync,
|
|
176
|
-
pause: () => { paused = true; for (const id of [...timers.keys()]) clearTimer(id); signatures.clear(); emit() },
|
|
185
|
+
pause: () => { paused = true; queued.clear(); for (const id of [...timers.keys()]) clearTimer(id); signatures.clear(); emit() },
|
|
177
186
|
resume: () => { paused = false; sync(cards?.list?.() ?? []); emit() },
|
|
178
187
|
snapshot,
|
|
179
188
|
refresh: (list) => { for (const card of Array.isArray(list) ? list : []) void runOne(card) },
|