dsh-embedded-browser-next 0.0.0-stage → 1.0.0
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.en.md +275 -0
- package/README.md +274 -2
- package/cordis.patch.yml +16 -0
- package/lib/browser.js +2634 -0
- package/lib/cdp.js +320 -0
- package/lib/client.js +3991 -0
- package/lib/diagnostics.js +48 -0
- package/lib/downloads.js +73 -0
- package/lib/external.js +96 -0
- package/lib/human.js +145 -0
- package/lib/index.js +609 -0
- package/lib/lazy.js +193 -0
- package/lib/mjpeg.js +154 -0
- package/lib/paths.js +107 -0
- package/lib/residency.js +234 -0
- package/lib/router.js +687 -0
- package/lib/routes.js +446 -0
- package/lib/screencast.js +1629 -0
- package/lib/snapshot-window.js +73 -0
- package/lib/tools.js +740 -0
- package/lib/ws.js +253 -0
- package/package.json +60 -4
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the diagnostic logs go.
|
|
3
|
+
*
|
|
4
|
+
* Both halves used to hard-code an absolute path inside the maintainer's own
|
|
5
|
+
* work directory. On any other machine the directory did not exist, the write
|
|
6
|
+
* threw, and the surrounding `try` swallowed it — so every trace silently
|
|
7
|
+
* disappeared and the one feature meant to explain a silent failure was itself
|
|
8
|
+
* silent. The default is now `$DSH_HOME/logs` (falling back to `~/.dsh/logs`),
|
|
9
|
+
* and each file can still be relocated or disabled by its own variable.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-embedded-browser/lib/diagnostics
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { mkdirSync } from 'node:fs'
|
|
15
|
+
import os from 'node:os'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
|
|
18
|
+
/** `$DSH_HOME` when the host sets it, otherwise the documented default. */
|
|
19
|
+
export function homeDir() {
|
|
20
|
+
const fromEnv = process.env.DSH_HOME
|
|
21
|
+
if (typeof fromEnv === 'string' && fromEnv !== '') return fromEnv
|
|
22
|
+
return path.join(os.homedir(), '.dsh')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Resolve one diagnostic file.
|
|
27
|
+
*
|
|
28
|
+
* The directory is created here because nothing else does: `appendFileSync` into
|
|
29
|
+
* a missing directory throws `ENOENT`, every caller swallows it, and the whole
|
|
30
|
+
* diagnostic feature disappears without a trace — the exact failure this module
|
|
31
|
+
* was written to fix, reproduced one level up.
|
|
32
|
+
*
|
|
33
|
+
* @param name - file name inside the log directory.
|
|
34
|
+
* @param envName - variable that relocates the file; the literal `off` disables it.
|
|
35
|
+
* @returns an absolute path, or an empty string when the file is disabled.
|
|
36
|
+
*/
|
|
37
|
+
export function logFile(name, envName) {
|
|
38
|
+
const override = process.env[envName]
|
|
39
|
+
if (override === 'off') return ''
|
|
40
|
+
if (typeof override === 'string' && override !== '') return override
|
|
41
|
+
const dir = path.join(homeDir(), 'logs')
|
|
42
|
+
try {
|
|
43
|
+
mkdirSync(dir, { recursive: true })
|
|
44
|
+
} catch {
|
|
45
|
+
/* a later write reports the failure where it matters */
|
|
46
|
+
}
|
|
47
|
+
return path.join(dir, name)
|
|
48
|
+
}
|
package/lib/downloads.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writing a download to disk.
|
|
3
|
+
*
|
|
4
|
+
* Both halves need this: the tool layer (the AI asked for a URL) and the panel bridge
|
|
5
|
+
* (a human clicked a download link). The host cancels `will-download` on the sidebar
|
|
6
|
+
* browser's session, so a real browser download never happens — the bytes are fetched
|
|
7
|
+
* inside the page and end up here instead.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-embedded-browser/downloads
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { mkdirSync } from 'node:fs'
|
|
13
|
+
import { writeFile } from 'node:fs/promises'
|
|
14
|
+
import { homedir } from 'node:os'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
|
|
17
|
+
/** Where downloads land when nobody said otherwise. */
|
|
18
|
+
export const DEFAULT_DOWNLOAD_DIR = join(homedir(), 'Downloads', 'dsh-embedded-browser')
|
|
19
|
+
|
|
20
|
+
/** Anything a file name may not contain on this platform, plus path separators. */
|
|
21
|
+
const FORBIDDEN = /[\\/:*?"<>|\u0000-\u001f]+/g
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Turn a site-supplied name into one that cannot escape the download directory.
|
|
25
|
+
*
|
|
26
|
+
* A `Content-Disposition` filename is attacker-controlled text; `../../.bashrc` or an
|
|
27
|
+
* absolute path must never decide where the write lands.
|
|
28
|
+
*/
|
|
29
|
+
export function safeFileName(name, fallback) {
|
|
30
|
+
const trimmed = String(name ?? '').replace(FORBIDDEN, '_').trim()
|
|
31
|
+
const base = trimmed === '' || trimmed === '.' || trimmed === '..' ? '' : trimmed
|
|
32
|
+
if (base !== '') return base.slice(0, 120)
|
|
33
|
+
return String(fallback ?? `download-${Date.now()}`).slice(0, 120)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Name a download when the site did not say.
|
|
38
|
+
*
|
|
39
|
+
* The URL path is the usual source; a query-string endpoint with no extension gets a
|
|
40
|
+
* `.bin`-free name rather than something misleading.
|
|
41
|
+
*/
|
|
42
|
+
export function nameFromUrl(url, fallback) {
|
|
43
|
+
try {
|
|
44
|
+
const parsed = new URL(String(url))
|
|
45
|
+
const last = parsed.pathname.split('/').filter((part) => part !== '').pop() ?? ''
|
|
46
|
+
const decoded = decodeURIComponent(last)
|
|
47
|
+
if (decoded !== '') return safeFileName(decoded, fallback)
|
|
48
|
+
} catch {
|
|
49
|
+
/* not a URL; fall through */
|
|
50
|
+
}
|
|
51
|
+
return safeFileName('', fallback)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Write one downloaded payload into the directory.
|
|
56
|
+
*
|
|
57
|
+
* @param options - `name`, `base64` bytes, and an optional directory.
|
|
58
|
+
* @returns the absolute path, the file name used, and the byte count.
|
|
59
|
+
*/
|
|
60
|
+
export async function saveDownload({ name, base64, dir, url }) {
|
|
61
|
+
const target = typeof dir === 'string' && dir !== '' ? dir : DEFAULT_DOWNLOAD_DIR
|
|
62
|
+
mkdirSync(target, { recursive: true })
|
|
63
|
+
const file = safeFileName(name, url === undefined ? undefined : nameFromUrl(url))
|
|
64
|
+
const data = Buffer.from(String(base64 ?? ''), 'base64')
|
|
65
|
+
const path = join(target, file)
|
|
66
|
+
await writeFile(path, data)
|
|
67
|
+
return { path, name: file, bytes: data.length }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A directory path is a directory path; this only guards against empty input. */
|
|
71
|
+
export function downloadDir(dir) {
|
|
72
|
+
return typeof dir === 'string' && dir !== '' ? dir : DEFAULT_DOWNLOAD_DIR
|
|
73
|
+
}
|
package/lib/external.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handing a page to the human's own browser.
|
|
3
|
+
*
|
|
4
|
+
* The panel's page lives in a leased `<webview>`: as far as logins and rendering
|
|
5
|
+
* go it *is* a browser, but it is not an OS window, and the Desktop bridge cannot
|
|
6
|
+
* re-home a guest into one — `DesktopBrowserBridge` is `acquire`, `release` and
|
|
7
|
+
* `onOpenRequested`, nothing else. So "open this somewhere I can use passkeys and
|
|
8
|
+
* the rest" can only mean a **second** browser, which starts with an empty login
|
|
9
|
+
* store. The panel copy says so; this module only performs the hand-off.
|
|
10
|
+
*
|
|
11
|
+
* Nothing here parses arguments through a shell: the URL goes in as one argv
|
|
12
|
+
* entry, and the scheme is checked first so a page cannot talk the plugin into
|
|
13
|
+
* opening `file:` or a `javascript:` URL.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-embedded-browser/external
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { spawn } from 'node:child_process'
|
|
19
|
+
|
|
20
|
+
/** Schemes a human-facing browser can be asked to open. */
|
|
21
|
+
const OPENABLE = new Set(['http:', 'https:'])
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Validate and canonicalize a URL meant for the system browser.
|
|
25
|
+
*
|
|
26
|
+
* @param value - candidate URL.
|
|
27
|
+
* @returns the absolute `http(s)` URL as the browser should receive it.
|
|
28
|
+
* @throws a 400-tagged error, because every caller is an HTTP route.
|
|
29
|
+
*/
|
|
30
|
+
export function normalizeExternalUrl(value) {
|
|
31
|
+
const bad = (message) => {
|
|
32
|
+
const error = new Error(message)
|
|
33
|
+
error.status = 400
|
|
34
|
+
return error
|
|
35
|
+
}
|
|
36
|
+
if (typeof value !== 'string' || value.trim() === '') throw bad('url is required')
|
|
37
|
+
let parsed
|
|
38
|
+
try {
|
|
39
|
+
parsed = new URL(value.trim())
|
|
40
|
+
} catch {
|
|
41
|
+
throw bad('url must be an absolute URL')
|
|
42
|
+
}
|
|
43
|
+
if (!OPENABLE.has(parsed.protocol)) throw bad(`cannot open ${parsed.protocol} in a browser`)
|
|
44
|
+
// A control character would be interpreted by the shell's own quoting rules on
|
|
45
|
+
// the way out; `URL` already encodes them, so this is the last line of defence.
|
|
46
|
+
if (/[\u0000-\u001f\u007f]/.test(parsed.href)) throw bad('url contains control characters')
|
|
47
|
+
return parsed.href
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The command that opens a URL in the default browser on this platform.
|
|
52
|
+
*
|
|
53
|
+
* @param platform - `process.platform`, injectable so the mapping is testable.
|
|
54
|
+
* @param url - already normalized URL.
|
|
55
|
+
* @returns `[command, args]`.
|
|
56
|
+
*/
|
|
57
|
+
export function openerFor(platform, url) {
|
|
58
|
+
// `rundll32 url.dll,FileProtocolHandler` hands the URL straight to the shell's
|
|
59
|
+
// own protocol handler. Nothing between here and the browser parses it as a
|
|
60
|
+
// command line, which is the whole point.
|
|
61
|
+
//
|
|
62
|
+
// Measured 2026-11-30, the previous form was an injection: `cmd.exe /c start`
|
|
63
|
+
// re-parses its arguments, Node quotes an argument only when it contains a
|
|
64
|
+
// space or a quote, and `new URL().href` percent-encodes exactly those two. So
|
|
65
|
+
// `https://e.test/?a=1&ver` reached `cmd` unquoted: `cmd` split on `&` and ran
|
|
66
|
+
// `ver`. Every multi-parameter URL also lost everything after the first `&`.
|
|
67
|
+
if (platform === 'win32') return ['rundll32.exe', ['url.dll,FileProtocolHandler', url]]
|
|
68
|
+
if (platform === 'darwin') return ['open', [url]]
|
|
69
|
+
return ['xdg-open', [url]]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Open a URL in the human's default browser and return the URL that was opened.
|
|
74
|
+
*
|
|
75
|
+
* Fire-and-forget by design: the panel asked for a hand-off, not for a child to
|
|
76
|
+
* babysit. A browser that fails to launch must not turn into an unhandled `error`
|
|
77
|
+
* event, so the child is detached, unreffed, and its error is swallowed.
|
|
78
|
+
*
|
|
79
|
+
* @param url - candidate URL.
|
|
80
|
+
* @param options - platform and spawn override (tests).
|
|
81
|
+
* @returns the normalized URL.
|
|
82
|
+
*/
|
|
83
|
+
export function systemOpen(url, { platform = process.platform, spawnImpl = spawn } = {}) {
|
|
84
|
+
const target = normalizeExternalUrl(url)
|
|
85
|
+
const [command, args] = openerFor(platform, target)
|
|
86
|
+
try {
|
|
87
|
+
const child = spawnImpl(command, args, { detached: true, stdio: 'ignore', windowsHide: true })
|
|
88
|
+
child.on?.('error', () => {})
|
|
89
|
+
child.unref?.()
|
|
90
|
+
} catch (error) {
|
|
91
|
+
const failure = new Error(`could not open a browser: ${error?.message ?? error}`)
|
|
92
|
+
failure.status = 500
|
|
93
|
+
throw failure
|
|
94
|
+
}
|
|
95
|
+
return target
|
|
96
|
+
}
|
package/lib/human.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Human-in-the-loop broker, one outstanding request per session.
|
|
3
|
+
*
|
|
4
|
+
* The AI can hand its own tab over to the person in front of the DSH Web UI —
|
|
5
|
+
* logging in, scanning a QR code, answering a CAPTCHA. Requests are keyed by
|
|
6
|
+
* session: two sessions can be waiting on two different humans at the same time,
|
|
7
|
+
* and a panel only ever sees the request belonging to the session it renders.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-embedded-browser/human
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Monotonic request ids. */
|
|
13
|
+
let sequence = 0
|
|
14
|
+
|
|
15
|
+
/** JSON-safe projection of one pending request. */
|
|
16
|
+
function project(entry) {
|
|
17
|
+
if (entry === undefined) return undefined
|
|
18
|
+
const { id, sessionId, instruction, createdAt, timeoutMs } = entry
|
|
19
|
+
return { id, sessionId, instruction, createdAt, timeoutMs, expiresAt: createdAt + timeoutMs }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Tracks the outstanding human request of every session.
|
|
24
|
+
*/
|
|
25
|
+
export class HumanBroker {
|
|
26
|
+
/**
|
|
27
|
+
* @param options - change notifier plus the default timeout from config.
|
|
28
|
+
*/
|
|
29
|
+
constructor({ onEvent } = {}) {
|
|
30
|
+
/** sessionId -> { id, sessionId, instruction, createdAt, timeoutMs, waiter, timer } */
|
|
31
|
+
this.pending = new Map()
|
|
32
|
+
this.onEvent = onEvent ?? (() => {})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Current request of one session in a JSON-safe shape.
|
|
37
|
+
*
|
|
38
|
+
* @param sessionId - the session to look up; omitted returns the newest request
|
|
39
|
+
* across sessions (kept for callers that do not know about sessions).
|
|
40
|
+
*/
|
|
41
|
+
snapshot(sessionId) {
|
|
42
|
+
if (sessionId !== undefined) return project(this.pending.get(sessionId))
|
|
43
|
+
let newest
|
|
44
|
+
for (const entry of this.pending.values()) {
|
|
45
|
+
if (newest === undefined || entry.createdAt > newest.createdAt) newest = entry
|
|
46
|
+
}
|
|
47
|
+
return project(newest)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Every outstanding request, oldest first — the sidebar overview's data. */
|
|
51
|
+
snapshotAll() {
|
|
52
|
+
return [...this.pending.values()].sort((a, b) => a.createdAt - b.createdAt).map((entry) => project(entry))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Whether a session is currently waiting on a person. */
|
|
56
|
+
has(sessionId) {
|
|
57
|
+
return this.pending.has(sessionId)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Ask the human of one session to act, resolving when they confirm or the
|
|
62
|
+
* budget elapses.
|
|
63
|
+
*
|
|
64
|
+
* @param sessionId - the session whose tab the human should operate.
|
|
65
|
+
* @param instruction - what to do, phrased for a person.
|
|
66
|
+
* @param options - timeout budget in milliseconds.
|
|
67
|
+
* @returns the outcome, including how long the human took.
|
|
68
|
+
*/
|
|
69
|
+
ask(sessionId, instruction, { timeoutMs = 600_000 } = {}) {
|
|
70
|
+
this.cancel(sessionId, 'superseded')
|
|
71
|
+
const id = `human-${++sequence}`
|
|
72
|
+
const createdAt = Date.now()
|
|
73
|
+
const entry = { id, sessionId, instruction, createdAt, timeoutMs, waiter: undefined, timer: undefined }
|
|
74
|
+
this.pending.set(sessionId, entry)
|
|
75
|
+
const outcome = new Promise((resolve) => {
|
|
76
|
+
entry.timer = setTimeout(() => {
|
|
77
|
+
this.pending.delete(sessionId)
|
|
78
|
+
this.onEvent({ type: 'human-timeout', sessionId, id })
|
|
79
|
+
resolve({ status: 'timeout', waitedMs: Date.now() - createdAt })
|
|
80
|
+
}, timeoutMs)
|
|
81
|
+
entry.timer.unref?.()
|
|
82
|
+
entry.waiter = {
|
|
83
|
+
resolve: (extra) => {
|
|
84
|
+
clearTimeout(entry.timer)
|
|
85
|
+
resolve({ status: 'done', waitedMs: Date.now() - createdAt, ...extra })
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
this.onEvent({ type: 'human-request', sessionId, request: project(entry) })
|
|
90
|
+
return outcome
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Resolve one outstanding request.
|
|
95
|
+
*
|
|
96
|
+
* @param id - request id from the panel; a mismatched id is ignored.
|
|
97
|
+
* @param extra - optional human-supplied reply or note.
|
|
98
|
+
* @returns whether a request was actually settled.
|
|
99
|
+
*/
|
|
100
|
+
done(id, extra = {}) {
|
|
101
|
+
const entry = this.find(id)
|
|
102
|
+
if (entry === undefined) return false
|
|
103
|
+
this.pending.delete(entry.sessionId)
|
|
104
|
+
this.onEvent({ type: 'human-done', sessionId: entry.sessionId, id: entry.id })
|
|
105
|
+
entry.waiter?.resolve(extra)
|
|
106
|
+
return true
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Locate a pending request by id (or the single one when id is unknown). */
|
|
110
|
+
find(id) {
|
|
111
|
+
if (id !== undefined) {
|
|
112
|
+
for (const entry of this.pending.values()) {
|
|
113
|
+
if (entry.id === id) return entry
|
|
114
|
+
}
|
|
115
|
+
return undefined
|
|
116
|
+
}
|
|
117
|
+
for (const entry of this.pending.values()) return entry
|
|
118
|
+
return undefined
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Drop one session's request without a human action.
|
|
123
|
+
*
|
|
124
|
+
* @param sessionId - the session whose request is dropped.
|
|
125
|
+
* @param reason - status reported to the waiting tool call.
|
|
126
|
+
* @returns whether a request was pending.
|
|
127
|
+
*/
|
|
128
|
+
cancel(sessionId, reason = 'cancelled') {
|
|
129
|
+
const entry = this.pending.get(sessionId)
|
|
130
|
+
if (entry === undefined) return false
|
|
131
|
+
this.pending.delete(sessionId)
|
|
132
|
+
clearTimeout(entry.timer)
|
|
133
|
+
entry.waiter?.resolve({ status: reason })
|
|
134
|
+
return true
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Drop every outstanding request (plugin dispose, browser stop). */
|
|
138
|
+
cancelAll(reason = 'cancelled') {
|
|
139
|
+
let dropped = false
|
|
140
|
+
for (const sessionId of [...this.pending.keys()]) {
|
|
141
|
+
if (this.cancel(sessionId, reason)) dropped = true
|
|
142
|
+
}
|
|
143
|
+
return dropped
|
|
144
|
+
}
|
|
145
|
+
}
|