yukari-dsh-plugin 0.0.0-stage → 0.7.2
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 +17 -2
- package/client.js +8340 -0
- package/cordis.patch.yml +95 -0
- package/host/browser/index.js +397 -0
- package/host/browser/lib/browser.js +885 -0
- package/host/browser/lib/cdp.js +302 -0
- package/host/browser/lib/libraries.js +309 -0
- package/host/browser/lib/page-scripts.js +243 -0
- package/host/browser/lib/runtime.js +351 -0
- package/host/browser/lib/url-guard.js +122 -0
- package/host/browser/locale/en.json +6 -0
- package/host/browser/locale/zh.json +6 -0
- package/host/chats/index.js +254 -0
- package/host/chats/lib/admin.js +893 -0
- package/host/chats/lib/cleanup.js +272 -0
- package/host/chats/lib/paths.js +108 -0
- package/host/chats/lib/preview.js +74 -0
- package/host/chats/locale/en.json +6 -0
- package/host/chats/locale/zh.json +6 -0
- package/host/files/index.js +403 -0
- package/host/files/locale/en.json +6 -0
- package/host/files/locale/zh.json +6 -0
- package/host/github/index.js +121 -0
- package/host/github/lib/api.js +310 -0
- package/host/github/lib/atomic.js +42 -0
- package/host/github/lib/controller.js +450 -0
- package/host/github/lib/git.js +389 -0
- package/host/github/lib/redact.js +22 -0
- package/host/github/lib/signing.js +124 -0
- package/host/github/lib/store.js +181 -0
- package/host/github/locale/en.json +6 -0
- package/host/github/locale/zh.json +6 -0
- package/host/presets/index.js +450 -0
- package/host/presets/lib/definition.js +267 -0
- package/host/presets/lib/sources.js +278 -0
- package/host/presets/lib/store.js +136 -0
- package/host/presets/locale/en.json +6 -0
- package/host/presets/locale/zh.json +6 -0
- package/host/proxy/index.js +236 -0
- package/host/proxy/lib/controller.js +1151 -0
- package/host/proxy/lib/egress.js +120 -0
- package/host/proxy/lib/local-proxy.js +422 -0
- package/host/proxy/lib/shadowsocks.js +335 -0
- package/host/proxy/lib/ss-uri.js +501 -0
- package/host/proxy/lib/subscriptions.js +140 -0
- package/host/proxy/locale/en.json +6 -0
- package/host/proxy/locale/zh.json +6 -0
- package/host/purge/index.js +538 -0
- package/host/purge/lib/preset.js +140 -0
- package/host/purge/lib/prompts.js +95 -0
- package/host/purge/lib/retry-gate.js +97 -0
- package/host/purge/lib/rewriter.js +322 -0
- package/host/purge/lib/settings.js +54 -0
- package/host/purge/lib/stream-retry.js +255 -0
- package/host/purge/locale/en.json +6 -0
- package/host/purge/locale/zh.json +6 -0
- package/host/purge/presets/unrestricted/agent.cordis.yml +273 -0
- package/host/purge/presets/unrestricted/preset.json +75 -0
- package/host/purge/presets/unrestricted/preset.yml +3 -0
- package/host/rewind/index.js +127 -0
- package/host/rewind/lib/controller.js +449 -0
- package/host/rewind/lib/marker.js +53 -0
- package/host/rewind/lib/safe-path.js +47 -0
- package/host/rewind/lib/store.js +585 -0
- package/host/rewind/lib/surface.js +108 -0
- package/host/rewind/locale/en.json +6 -0
- package/host/rewind/locale/zh.json +6 -0
- package/host/rules/index.js +624 -0
- package/host/rules/lib/paths.js +87 -0
- package/host/rules/lib/render.js +92 -0
- package/host/rules/lib/scope.js +146 -0
- package/host/rules/lib/settings.js +55 -0
- package/host/rules/lib/store.js +222 -0
- package/host/rules/locale/en.json +6 -0
- package/host/rules/locale/zh.json +6 -0
- package/host/runtime/index.js +577 -0
- package/host/runtime/lib/npm.js +293 -0
- package/host/runtime/lib/restart.js +105 -0
- package/host/runtime/lib/update-guard.js +70 -0
- package/host/runtime/lib/versions.js +188 -0
- package/host/runtime/locale/en.json +6 -0
- package/host/runtime/locale/zh.json +6 -0
- package/host/shared/busy.js +40 -0
- package/host/shared/home.js +27 -0
- package/host/shared/http.js +159 -0
- package/host/shared/migrate.js +60 -0
- package/host/shared/mounted.js +54 -0
- package/host/shared/zip-writer.js +319 -0
- package/host/shared/zip.js +180 -0
- package/host/sites/index.js +258 -0
- package/host/sites/lib/sites.js +630 -0
- package/host/sites/locale/en.json +6 -0
- package/host/sites/locale/zh.json +6 -0
- package/host/skills/index.js +176 -0
- package/host/skills/lib/frontmatter.js +149 -0
- package/host/skills/lib/library.js +408 -0
- package/host/skills/lib/upload.js +115 -0
- package/host/skills/locale/en.json +6 -0
- package/host/skills/locale/zh.json +6 -0
- package/icon.png +0 -0
- package/index.js +702 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +113 -4
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A very small Chrome DevTools Protocol client.
|
|
3
|
+
*
|
|
4
|
+
* The Harness runs on Node's built-in `fetch`/`WebSocket`, so browser control
|
|
5
|
+
* needs no npm dependency: connect to the browser endpoint Chrome prints on
|
|
6
|
+
* stderr, attach to a page target, and speak JSON over that one socket.
|
|
7
|
+
* @module @local/browser-control/cdp
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Raised when the browser or a protocol call refuses a request. */
|
|
11
|
+
export class CdpError extends Error {
|
|
12
|
+
/**
|
|
13
|
+
* @param message - human readable failure.
|
|
14
|
+
* @param code - optional CDP error code.
|
|
15
|
+
*/
|
|
16
|
+
constructor(message, code) {
|
|
17
|
+
super(message)
|
|
18
|
+
this.name = 'CdpError'
|
|
19
|
+
this.code = code
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Delay helper. */
|
|
24
|
+
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One protocol connection: either the browser endpoint or, with a
|
|
28
|
+
* `sessionId`, a flattened page target.
|
|
29
|
+
*/
|
|
30
|
+
export class CdpClient {
|
|
31
|
+
#socket
|
|
32
|
+
#nextId = 1
|
|
33
|
+
#pending = new Map()
|
|
34
|
+
#events = new Set()
|
|
35
|
+
#waiters = new Set()
|
|
36
|
+
#closeListeners = new Set()
|
|
37
|
+
#closed = false
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param socket - an open WebSocket.
|
|
41
|
+
*/
|
|
42
|
+
constructor(socket) {
|
|
43
|
+
this.#socket = socket
|
|
44
|
+
socket.addEventListener('message', (event) => this.#receive(event.data))
|
|
45
|
+
socket.addEventListener('close', () => this.#shutdown('浏览器连接已断开'))
|
|
46
|
+
socket.addEventListener('error', () => this.#shutdown('浏览器连接出错'))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Open a protocol connection.
|
|
51
|
+
* @param url - `ws://` DevTools endpoint.
|
|
52
|
+
* @param timeoutMs - connection budget.
|
|
53
|
+
* @returns the connected client.
|
|
54
|
+
*/
|
|
55
|
+
static async connect(url, timeoutMs = 20000) {
|
|
56
|
+
if (typeof globalThis.WebSocket !== 'function') {
|
|
57
|
+
throw new CdpError('this Node.js runtime has no global WebSocket (Node 22 or newer is required)')
|
|
58
|
+
}
|
|
59
|
+
const socket = new WebSocket(url)
|
|
60
|
+
await new Promise((resolve, reject) => {
|
|
61
|
+
const timer = setTimeout(() => {
|
|
62
|
+
try { socket.close() } catch { /* ignore */ }
|
|
63
|
+
reject(new CdpError(`timed out after ${timeoutMs}ms connecting to ${url}`))
|
|
64
|
+
}, timeoutMs)
|
|
65
|
+
socket.addEventListener('open', () => { clearTimeout(timer); resolve() }, { once: true })
|
|
66
|
+
socket.addEventListener('error', () => {
|
|
67
|
+
clearTimeout(timer)
|
|
68
|
+
reject(new CdpError(`cannot connect to the browser endpoint ${url}`))
|
|
69
|
+
}, { once: true })
|
|
70
|
+
})
|
|
71
|
+
return new CdpClient(socket)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Whether the socket is still usable. */
|
|
75
|
+
get open() {
|
|
76
|
+
return !this.#closed
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Send one protocol command.
|
|
81
|
+
* @param method - CDP method name.
|
|
82
|
+
* @param params - method parameters.
|
|
83
|
+
* @param sessionId - flattened page session, when addressing a page.
|
|
84
|
+
* @returns the method result.
|
|
85
|
+
*/
|
|
86
|
+
send(method, params = {}, sessionId) {
|
|
87
|
+
if (this.#closed) return Promise.reject(new CdpError('浏览器连接已关闭'))
|
|
88
|
+
const id = this.#nextId++
|
|
89
|
+
const message = { id, method, params }
|
|
90
|
+
if (sessionId !== undefined) message.sessionId = sessionId
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
this.#pending.delete(id)
|
|
94
|
+
reject(new CdpError(`${method} timed out`))
|
|
95
|
+
}, 60000)
|
|
96
|
+
this.#pending.set(id, { resolve, reject, timer, method })
|
|
97
|
+
try {
|
|
98
|
+
this.#socket.send(JSON.stringify(message))
|
|
99
|
+
} catch (error) {
|
|
100
|
+
clearTimeout(timer)
|
|
101
|
+
this.#pending.delete(id)
|
|
102
|
+
reject(new CdpError(`${method} could not be sent: ${error instanceof Error ? error.message : String(error)}`))
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Subscribe to protocol events.
|
|
109
|
+
* @param listener - receives `(method, params, sessionId)`.
|
|
110
|
+
* @returns an unsubscribe function.
|
|
111
|
+
*/
|
|
112
|
+
onEvent(listener) {
|
|
113
|
+
this.#events.add(listener)
|
|
114
|
+
return () => this.#events.delete(listener)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Notify the owner when the connection dies, so it can drop stale state.
|
|
119
|
+
* @param listener - receives the failure text.
|
|
120
|
+
* @returns an unsubscribe function.
|
|
121
|
+
*/
|
|
122
|
+
onClose(listener) {
|
|
123
|
+
if (this.#closed) {
|
|
124
|
+
listener('浏览器连接已关闭')
|
|
125
|
+
return () => {}
|
|
126
|
+
}
|
|
127
|
+
this.#closeListeners.add(listener)
|
|
128
|
+
return () => this.#closeListeners.delete(listener)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Resolve on the first matching protocol event.
|
|
133
|
+
* @param method - exact event name.
|
|
134
|
+
* @param predicate - extra filter over the event params and session.
|
|
135
|
+
* @param timeoutMs - budget before the wait rejects.
|
|
136
|
+
* @returns the event params.
|
|
137
|
+
*/
|
|
138
|
+
once(method, predicate = () => true, timeoutMs = 20000) {
|
|
139
|
+
if (this.#closed) return Promise.reject(new CdpError(`${method}: 浏览器连接已断开`))
|
|
140
|
+
return new Promise((resolve, reject) => {
|
|
141
|
+
const waiter = { method, reject, off: undefined, timer: undefined }
|
|
142
|
+
const off = this.onEvent((name, params, sessionId) => {
|
|
143
|
+
if (name !== method) return
|
|
144
|
+
if (!predicate(params, sessionId)) return
|
|
145
|
+
clearTimeout(waiter.timer)
|
|
146
|
+
this.#waiters.delete(waiter)
|
|
147
|
+
off()
|
|
148
|
+
resolve(params)
|
|
149
|
+
})
|
|
150
|
+
waiter.off = off
|
|
151
|
+
waiter.timer = setTimeout(() => {
|
|
152
|
+
this.#waiters.delete(waiter)
|
|
153
|
+
off()
|
|
154
|
+
reject(new CdpError(`${method} did not arrive within ${timeoutMs}ms`))
|
|
155
|
+
}, timeoutMs)
|
|
156
|
+
this.#waiters.add(waiter)
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Close the socket and reject every outstanding command and wait. */
|
|
161
|
+
close() {
|
|
162
|
+
if (this.#closed) return
|
|
163
|
+
this.#shutdown('浏览器连接已关闭')
|
|
164
|
+
try { this.#socket.close() } catch { /* ignore */ }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** @param reason - failure text; the connection cannot be reused afterwards. */
|
|
168
|
+
#shutdown(reason) {
|
|
169
|
+
if (this.#closed) return
|
|
170
|
+
this.#closed = true
|
|
171
|
+
this.#failAll(reason)
|
|
172
|
+
for (const listener of [...this.#closeListeners]) {
|
|
173
|
+
try {
|
|
174
|
+
listener(reason)
|
|
175
|
+
} catch {
|
|
176
|
+
// One owner callback must not keep the other listeners from running.
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
this.#closeListeners.clear()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** @param raw - JSON frame from the socket. */
|
|
183
|
+
#receive(raw) {
|
|
184
|
+
let message
|
|
185
|
+
try {
|
|
186
|
+
message = JSON.parse(typeof raw === 'string' ? raw : String(raw))
|
|
187
|
+
} catch {
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
if (message.id !== undefined && this.#pending.has(message.id)) {
|
|
191
|
+
const pending = this.#pending.get(message.id)
|
|
192
|
+
this.#pending.delete(message.id)
|
|
193
|
+
clearTimeout(pending.timer)
|
|
194
|
+
if (message.error) {
|
|
195
|
+
pending.reject(new CdpError(`${pending.method}: ${message.error.message ?? 'protocol error'}`, message.error.code))
|
|
196
|
+
} else {
|
|
197
|
+
pending.resolve(message.result ?? {})
|
|
198
|
+
}
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
if (message.method) {
|
|
202
|
+
for (const listener of [...this.#events]) {
|
|
203
|
+
try {
|
|
204
|
+
listener(message.method, message.params ?? {}, message.sessionId)
|
|
205
|
+
} catch {
|
|
206
|
+
// A listener fault must not break protocol delivery.
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** @param reason - failure text for outstanding commands and event waits. */
|
|
213
|
+
#failAll(reason) {
|
|
214
|
+
for (const [id, pending] of this.#pending) {
|
|
215
|
+
clearTimeout(pending.timer)
|
|
216
|
+
this.#pending.delete(id)
|
|
217
|
+
pending.reject(new CdpError(`${pending.method}: ${reason}`))
|
|
218
|
+
}
|
|
219
|
+
for (const waiter of [...this.#waiters]) {
|
|
220
|
+
clearTimeout(waiter.timer)
|
|
221
|
+
this.#waiters.delete(waiter)
|
|
222
|
+
waiter.off()
|
|
223
|
+
waiter.reject(new CdpError(`${waiter.method}: ${reason}`))
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Start a Chromium process with the DevTools protocol enabled.
|
|
230
|
+
*
|
|
231
|
+
* @param options - executable, arguments, environment, and start budget.
|
|
232
|
+
* @returns the process handle, its browser endpoint, and a stop function.
|
|
233
|
+
*/
|
|
234
|
+
export async function launchChromium(options) {
|
|
235
|
+
const { spawn } = await import('node:child_process')
|
|
236
|
+
const { executablePath, args = [], env = {}, timeoutMs = 30000 } = options
|
|
237
|
+
let child
|
|
238
|
+
try {
|
|
239
|
+
child = spawn(executablePath, args, {
|
|
240
|
+
env: { ...process.env, ...env },
|
|
241
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
242
|
+
})
|
|
243
|
+
} catch (error) {
|
|
244
|
+
throw new CdpError(`无法启动浏览器进程 ${executablePath}:${error instanceof Error ? error.message : String(error)}`)
|
|
245
|
+
}
|
|
246
|
+
let log = ''
|
|
247
|
+
let endpoint = ''
|
|
248
|
+
let exited = null
|
|
249
|
+
let startError = null
|
|
250
|
+
// 没有 error 监听时,ENOENT/EACCES 会变成 uncaughtException,宿主的
|
|
251
|
+
// fail-loud 处理器会直接结束整个进程;这里把它降级成一次工具调用失败。
|
|
252
|
+
child.on('error', (error) => { startError = error })
|
|
253
|
+
child.on('exit', (code, signal) => { exited = { code, signal } })
|
|
254
|
+
const onChunk = (chunk) => {
|
|
255
|
+
log += chunk.toString()
|
|
256
|
+
if (log.length > 20000) log = log.slice(-20000)
|
|
257
|
+
if (endpoint === '') {
|
|
258
|
+
const match = /DevTools listening on (ws:\/\/\S+)/.exec(log)
|
|
259
|
+
if (match) endpoint = match[1]
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
child.stdout?.on('data', onChunk)
|
|
263
|
+
child.stderr?.on('data', onChunk)
|
|
264
|
+
|
|
265
|
+
const started = Date.now()
|
|
266
|
+
while (endpoint === '' && startError === null && Date.now() - started < timeoutMs) {
|
|
267
|
+
if (exited) {
|
|
268
|
+
throw new CdpError(`browser exited before it became ready (code ${exited.code ?? 'null'}, signal ${exited.signal ?? 'none'}): ${summarize(log)}`)
|
|
269
|
+
}
|
|
270
|
+
await sleep(100)
|
|
271
|
+
}
|
|
272
|
+
if (startError !== null) {
|
|
273
|
+
try { child.kill('SIGKILL') } catch { /* it never started */ }
|
|
274
|
+
throw new CdpError(`无法启动浏览器进程 ${executablePath}:${startError.message}`)
|
|
275
|
+
}
|
|
276
|
+
if (endpoint === '') {
|
|
277
|
+
try { child.kill('SIGKILL') } catch { /* ignore */ }
|
|
278
|
+
throw new CdpError(`browser did not expose a DevTools endpoint within ${timeoutMs}ms: ${summarize(log)}`)
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
process: child,
|
|
282
|
+
endpoint,
|
|
283
|
+
log: () => log,
|
|
284
|
+
stop: async () => {
|
|
285
|
+
if (exited) return
|
|
286
|
+
try { child.kill('SIGTERM') } catch { /* ignore */ }
|
|
287
|
+
for (let i = 0; i < 30 && !exited; i++) await sleep(100)
|
|
288
|
+
if (!exited) {
|
|
289
|
+
try { child.kill('SIGKILL') } catch { /* ignore */ }
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Keep the last interesting browser log lines for a failure message. */
|
|
296
|
+
function summarize(log) {
|
|
297
|
+
const lines = log
|
|
298
|
+
.split('\n')
|
|
299
|
+
.map((line) => line.trim())
|
|
300
|
+
.filter((line) => line.length > 0 && !/dbus|Fontconfig|zygote_host|vaapi|InitializeSandbox|DevTools listening/u.test(line))
|
|
301
|
+
return lines.slice(-3).join(' | ') || 'no diagnostics'
|
|
302
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rootless runtime libraries for the downloaded browser.
|
|
3
|
+
*
|
|
4
|
+
* Chrome for Testing links against roughly two dozen system libraries that a
|
|
5
|
+
* slim container does not ship, and installing them needs root. Instead of
|
|
6
|
+
* asking the operator for that, the plugin resolves the needed Debian packages
|
|
7
|
+
* from the distribution index, downloads them, and unpacks them into its own
|
|
8
|
+
* directory under DSH_HOME. Launching the browser with `LD_LIBRARY_PATH`
|
|
9
|
+
* pointing there needs no privileges and never touches the host filesystem.
|
|
10
|
+
*
|
|
11
|
+
* Core libraries (libc, libstdc++) are deliberately left to the host: mixing a
|
|
12
|
+
* second glibc into the search path is a reliable way to crash the browser.
|
|
13
|
+
* @module @local/browser-control/libraries
|
|
14
|
+
*/
|
|
15
|
+
import { execFile } from 'node:child_process'
|
|
16
|
+
import { createWriteStream } from 'node:fs'
|
|
17
|
+
import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
18
|
+
import path from 'node:path'
|
|
19
|
+
import { promisify } from 'node:util'
|
|
20
|
+
import { pipeline } from 'node:stream/promises'
|
|
21
|
+
|
|
22
|
+
const execFileAsync = promisify(execFile)
|
|
23
|
+
|
|
24
|
+
/** Shared-library soname → Debian package that provides it. */
|
|
25
|
+
export const SONAME_PACKAGES = {
|
|
26
|
+
'libglib-2.0.so.0': 'libglib2.0-0',
|
|
27
|
+
'libgobject-2.0.so.0': 'libglib2.0-0',
|
|
28
|
+
'libgio-2.0.so.0': 'libglib2.0-0',
|
|
29
|
+
'libnspr4.so': 'libnspr4',
|
|
30
|
+
'libnss3.so': 'libnss3',
|
|
31
|
+
'libnssutil3.so': 'libnss3',
|
|
32
|
+
'libsmime3.so': 'libnss3',
|
|
33
|
+
'libatk-1.0.so.0': 'libatk1.0-0',
|
|
34
|
+
'libatk-bridge-2.0.so.0': 'libatk-bridge2.0-0',
|
|
35
|
+
'libatspi.so.0': 'libatspi2.0-0',
|
|
36
|
+
'libdbus-1.so.3': 'libdbus-1-3',
|
|
37
|
+
'libcups.so.2': 'libcups2',
|
|
38
|
+
'libxcb.so.1': 'libxcb1',
|
|
39
|
+
'libxkbcommon.so.0': 'libxkbcommon0',
|
|
40
|
+
'libasound.so.2': 'libasound2',
|
|
41
|
+
'libgbm.so.1': 'libgbm1',
|
|
42
|
+
'libdrm.so.2': 'libdrm2',
|
|
43
|
+
'libxshmfence.so.1': 'libxshmfence1',
|
|
44
|
+
'libexpat.so.1': 'libexpat1',
|
|
45
|
+
'libfreetype.so.6': 'libfreetype6',
|
|
46
|
+
'libfontconfig.so.1': 'libfontconfig1',
|
|
47
|
+
'libharfbuzz.so.0': 'libharfbuzz0b',
|
|
48
|
+
'libpng16.so.16': 'libpng16-16',
|
|
49
|
+
'libEGL.so.1': 'libegl1',
|
|
50
|
+
'libGL.so.1': 'libgl1',
|
|
51
|
+
'libX11.so.6': 'libx11-6',
|
|
52
|
+
'libXext.so.6': 'libxext6',
|
|
53
|
+
'libXcomposite.so.1': 'libxcomposite1',
|
|
54
|
+
'libXdamage.so.1': 'libxdamage1',
|
|
55
|
+
'libXfixes.so.3': 'libxfixes3',
|
|
56
|
+
'libXrandr.so.2': 'libxrandr2',
|
|
57
|
+
'libcairo.so.2': 'libcairo2',
|
|
58
|
+
'libpango-1.0.so.0': 'libpango-1.0-0',
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Fonts that make screenshots readable instead of boxes. */
|
|
62
|
+
export const FONT_PACKAGES = ['fonts-liberation']
|
|
63
|
+
|
|
64
|
+
/** Optional CJK fonts; a page of Chinese text is unreadable without them. */
|
|
65
|
+
export const CJK_FONT_PACKAGES = ['fonts-noto-cjk']
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Packages that must come from the host, never from the side-by-side sysroot.
|
|
69
|
+
* A second libc on the search path breaks the browser in confusing ways.
|
|
70
|
+
*/
|
|
71
|
+
export const HOST_PROVIDED = new Set(['libc6', 'libgcc-s1', 'libstdc++6', 'zlib1g', 'libtinfo6'])
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Collect missing shared-library names from loader or `ldd` output.
|
|
75
|
+
* @param text - stderr, or `ldd` stdout.
|
|
76
|
+
* @returns unique sonames, in first-seen order.
|
|
77
|
+
*/
|
|
78
|
+
export function missingLibraries(text) {
|
|
79
|
+
const found = []
|
|
80
|
+
for (const line of String(text ?? '').split('\n')) {
|
|
81
|
+
const notFound = /^\s*(\S+)\s*=>\s*not found/m.exec(line)
|
|
82
|
+
const loader = /error while loading shared libraries:\s*([^:\s]+)/.exec(line)
|
|
83
|
+
const name = notFound?.[1] ?? loader?.[1]
|
|
84
|
+
if (name !== undefined && !found.includes(name)) found.push(name)
|
|
85
|
+
}
|
|
86
|
+
return found
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Turn missing sonames into the packages that provide them.
|
|
91
|
+
* @param sonames - names from the loader.
|
|
92
|
+
* @returns sorted Debian package names.
|
|
93
|
+
*/
|
|
94
|
+
export function packagesFor(sonames, extras = []) {
|
|
95
|
+
const packages = new Set([...FONT_PACKAGES, ...extras])
|
|
96
|
+
for (const soname of sonames) {
|
|
97
|
+
const known = SONAME_PACKAGES[soname]
|
|
98
|
+
if (known !== undefined) packages.add(known)
|
|
99
|
+
}
|
|
100
|
+
return [...packages].sort()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Shell command that installs those packages on a Debian/Ubuntu image. */
|
|
104
|
+
export function installCommand(packages) {
|
|
105
|
+
if (!Array.isArray(packages) || packages.length === 0) return ''
|
|
106
|
+
return `apt-get update && apt-get install -y --no-install-recommends ${packages.join(' ')}`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Debian architecture name for a Node architecture. */
|
|
110
|
+
export function debArchitecture(arch = process.arch) {
|
|
111
|
+
if (arch === 'arm64') return 'arm64'
|
|
112
|
+
if (arch === 'x64') return 'amd64'
|
|
113
|
+
throw new Error(`no Debian packages are available for ${arch}`)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Multiarch library directory inside the sysroot. */
|
|
117
|
+
export function debTriplet(arch = process.arch) {
|
|
118
|
+
if (arch === 'arm64') return 'aarch64-linux-gnu'
|
|
119
|
+
if (arch === 'x64') return 'x86_64-linux-gnu'
|
|
120
|
+
throw new Error(`no multiarch triplet for ${arch}`)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Parse a Debian `Packages` index into the fields the resolver needs.
|
|
125
|
+
* @param text - the decompressed index.
|
|
126
|
+
* @returns package name → `{ filename, depends }`.
|
|
127
|
+
*/
|
|
128
|
+
export function parsePackagesIndex(text) {
|
|
129
|
+
const packages = new Map()
|
|
130
|
+
for (const stanza of String(text ?? '').split('\n\n')) {
|
|
131
|
+
if (stanza.trim() === '') continue
|
|
132
|
+
const fields = {}
|
|
133
|
+
let key = null
|
|
134
|
+
for (const line of stanza.split('\n')) {
|
|
135
|
+
if (line.startsWith(' ') && key !== null) continue
|
|
136
|
+
const separator = line.indexOf(':')
|
|
137
|
+
if (separator <= 0) continue
|
|
138
|
+
key = line.slice(0, separator)
|
|
139
|
+
fields[key] = line.slice(separator + 1).trim()
|
|
140
|
+
}
|
|
141
|
+
const name = fields.Package
|
|
142
|
+
if (typeof name !== 'string' || name === '') continue
|
|
143
|
+
packages.set(name, { filename: fields.Filename, depends: `${fields['Pre-Depends'] ?? ''},${fields.Depends ?? ''}` })
|
|
144
|
+
}
|
|
145
|
+
return packages
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Close the dependency set of a few root packages.
|
|
150
|
+
* @param index - parsed index.
|
|
151
|
+
* @param roots - package names to start from.
|
|
152
|
+
* @returns sorted package names that have a downloadable file.
|
|
153
|
+
*/
|
|
154
|
+
export function resolveClosure(index, roots) {
|
|
155
|
+
const seen = new Set()
|
|
156
|
+
const queue = [...roots]
|
|
157
|
+
while (queue.length > 0) {
|
|
158
|
+
const name = queue.pop()
|
|
159
|
+
if (seen.has(name) || HOST_PROVIDED.has(name)) continue
|
|
160
|
+
const entry = index.get(name)
|
|
161
|
+
if (entry === undefined || typeof entry.filename !== 'string') continue
|
|
162
|
+
seen.add(name)
|
|
163
|
+
for (const alternative of entry.depends.split(',')) {
|
|
164
|
+
const first = alternative.split('|')[0].trim().split(' ')[0].trim()
|
|
165
|
+
if (first !== '' && !seen.has(first)) queue.push(first)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return [...seen].sort()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Download one URL to a file, replacing it. */
|
|
172
|
+
async function download(url, destination, fetchImpl, timeoutMs = 120000) {
|
|
173
|
+
const response = await fetchImpl(url, { redirect: 'follow', signal: AbortSignal.timeout(timeoutMs) })
|
|
174
|
+
if (!response.ok) throw new Error(`download failed with HTTP ${response.status}: ${url}`)
|
|
175
|
+
await mkdir(path.dirname(destination), { recursive: true })
|
|
176
|
+
const temporary = `${destination}.part`
|
|
177
|
+
await pipeline(response.body, createWriteStream(temporary))
|
|
178
|
+
await rename(temporary, destination)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Debian release codename and suite for this host, when it is Debian-like. */
|
|
182
|
+
export async function debianSuite(options = {}) {
|
|
183
|
+
if (options.suite !== undefined) return options.suite
|
|
184
|
+
try {
|
|
185
|
+
const text = await readFile(options.osRelease ?? '/etc/os-release', 'utf8')
|
|
186
|
+
const codename = /^VERSION_CODENAME=(.+)$/m.exec(text)?.[1]?.replace(/"/g, '').trim()
|
|
187
|
+
if (codename !== undefined && codename !== '') return codename
|
|
188
|
+
} catch { /* not a distribution file */ }
|
|
189
|
+
return 'bookworm'
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Directory holding the side-by-side runtime.
|
|
194
|
+
* @param home - DSH_HOME.
|
|
195
|
+
*/
|
|
196
|
+
export function libraryRoot(home) {
|
|
197
|
+
return path.join(home, 'browser-runtime', 'sysroot')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Environment additions that make the sysroot visible to the browser.
|
|
202
|
+
* @param home - DSH_HOME.
|
|
203
|
+
* @param arch - Node architecture.
|
|
204
|
+
* @returns `{ LD_LIBRARY_PATH?, FONTCONFIG_FILE? }`, empty when not installed.
|
|
205
|
+
*/
|
|
206
|
+
export async function libraryEnvironment(home, arch = process.arch) {
|
|
207
|
+
const root = libraryRoot(home)
|
|
208
|
+
const triplet = debTriplet(arch)
|
|
209
|
+
const candidates = [path.join(root, 'usr/lib', triplet), path.join(root, 'lib', triplet)]
|
|
210
|
+
const dirs = []
|
|
211
|
+
for (const candidate of candidates) {
|
|
212
|
+
try {
|
|
213
|
+
if ((await stat(candidate)).isDirectory()) dirs.push(candidate)
|
|
214
|
+
} catch { /* not extracted */ }
|
|
215
|
+
}
|
|
216
|
+
if (dirs.length === 0) return {}
|
|
217
|
+
const env = { LD_LIBRARY_PATH: dirs.join(':') }
|
|
218
|
+
const fontsConfig = path.join(root, 'etc', 'fonts', 'yukari-fonts.conf')
|
|
219
|
+
try {
|
|
220
|
+
await stat(fontsConfig)
|
|
221
|
+
env.FONTCONFIG_FILE = fontsConfig
|
|
222
|
+
} catch { /* fonts are optional */ }
|
|
223
|
+
return env
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Download and unpack everything the browser needs into DSH_HOME.
|
|
228
|
+
* @param options - executablePath, home, missing sonames, and seams.
|
|
229
|
+
* @returns `{ packages, environment }` describing what was installed.
|
|
230
|
+
*/
|
|
231
|
+
export async function installRuntimeLibraries(options) {
|
|
232
|
+
const home = options.home
|
|
233
|
+
const arch = options.arch ?? process.arch
|
|
234
|
+
const fetchImpl = options.fetch ?? globalThis.fetch
|
|
235
|
+
const execute = options.execFile ?? execFileAsync
|
|
236
|
+
const log = options.log ?? (() => {})
|
|
237
|
+
const root = libraryRoot(home)
|
|
238
|
+
const triplet = debTriplet(arch)
|
|
239
|
+
const debianArch = debArchitecture(arch)
|
|
240
|
+
const suite = await debianSuite(options)
|
|
241
|
+
const indexDir = path.join(root, '.index')
|
|
242
|
+
const indexFile = path.join(indexDir, `Packages-${suite}-${debianArch}.gz`)
|
|
243
|
+
const debDir = path.join(root, '.debs')
|
|
244
|
+
await mkdir(indexDir, { recursive: true })
|
|
245
|
+
await mkdir(debDir, { recursive: true })
|
|
246
|
+
|
|
247
|
+
const fresh = await stat(indexFile).then((info) => Date.now() - info.mtimeMs < 7 * 86400000).catch(() => false)
|
|
248
|
+
if (!fresh) {
|
|
249
|
+
log(`fetching the ${suite}/${debianArch} package index`)
|
|
250
|
+
const mirror = options.mirror ?? 'https://deb.debian.org/debian'
|
|
251
|
+
await download(`${mirror}/dists/${suite}/main/binary-${debianArch}/Packages.gz`, indexFile, fetchImpl, 120000)
|
|
252
|
+
}
|
|
253
|
+
const { gunzipSync } = await import('node:zlib')
|
|
254
|
+
const index = parsePackagesIndex(gunzipSync(await readFile(indexFile)).toString('utf8'))
|
|
255
|
+
|
|
256
|
+
const extras = options.cjkFonts === false ? [] : CJK_FONT_PACKAGES
|
|
257
|
+
const roots = packagesFor(options.missing ?? [], extras)
|
|
258
|
+
const wanted = resolveClosure(index, roots)
|
|
259
|
+
if (wanted.length === 0) throw new Error('the Debian index resolved no packages for the missing libraries')
|
|
260
|
+
|
|
261
|
+
for (const name of wanted) {
|
|
262
|
+
const entry = index.get(name)
|
|
263
|
+
const file = path.join(debDir, path.basename(entry.filename))
|
|
264
|
+
try {
|
|
265
|
+
await stat(file)
|
|
266
|
+
} catch {
|
|
267
|
+
log(`fetching ${name}`)
|
|
268
|
+
await download(`https://deb.debian.org/debian/${entry.filename}`, file, fetchImpl, 180000)
|
|
269
|
+
}
|
|
270
|
+
await execute('dpkg-deb', ['-x', file, root], { timeout: 120000 }).catch((error) => {
|
|
271
|
+
throw new Error(`cannot unpack ${name}: ${error instanceof Error ? error.message : String(error)} (dpkg-deb is required)`)
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const fontDirs = [path.join(root, 'usr/share/fonts')]
|
|
276
|
+
const present = []
|
|
277
|
+
for (const dir of fontDirs) {
|
|
278
|
+
try {
|
|
279
|
+
if ((await readdir(dir)).length > 0) present.push(dir)
|
|
280
|
+
} catch { /* no fonts extracted */ }
|
|
281
|
+
}
|
|
282
|
+
const fontsConfig = path.join(root, 'etc', 'fonts', 'yukari-fonts.conf')
|
|
283
|
+
if (present.length > 0) {
|
|
284
|
+
await mkdir(path.dirname(fontsConfig), { recursive: true })
|
|
285
|
+
const cacheDir = path.join(root, 'fontcache')
|
|
286
|
+
await mkdir(cacheDir, { recursive: true })
|
|
287
|
+
const body = [
|
|
288
|
+
'<?xml version="1.0"?>',
|
|
289
|
+
'<!DOCTYPE fontconfig SYSTEM "fonts.dtd">',
|
|
290
|
+
'<fontconfig>',
|
|
291
|
+
...present.map((dir) => ` <dir>${dir}</dir>`),
|
|
292
|
+
` <cachedir>${cacheDir}</cachedir>`,
|
|
293
|
+
'</fontconfig>',
|
|
294
|
+
'',
|
|
295
|
+
].join('\n')
|
|
296
|
+
await writeFile(fontsConfig, body, 'utf8')
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const environment = await libraryEnvironment(home, arch)
|
|
300
|
+
if (environment.LD_LIBRARY_PATH === undefined) {
|
|
301
|
+
throw new Error(`libraries were unpacked but ${triplet} is not present under ${root}`)
|
|
302
|
+
}
|
|
303
|
+
return { packages: wanted.length, environment, root }
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Remove the downloaded sysroot (used when a retry wants a clean start). */
|
|
307
|
+
export async function clearRuntimeLibraries(home) {
|
|
308
|
+
await rm(libraryRoot(home), { recursive: true, force: true }).catch(() => {})
|
|
309
|
+
}
|