dsh-remote 1.1.2 → 1.2.1
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/README.en.md +17 -3
- package/README.md +17 -3
- package/lib/binding.js +26 -2
- package/lib/boot.js +4 -31
- package/lib/client.js +600 -103
- package/lib/connect-summary.js +52 -0
- package/lib/forwards.js +285 -35
- package/lib/index.js +20 -4573
- package/lib/local-workspace.js +103 -0
- package/lib/main.js +4660 -0
- package/lib/openssh.js +59 -8
- package/lib/remote-git.js +6 -4
- package/lib/routes-forwards.js +24 -4
- package/lib/routes-fs.js +26 -22
- package/lib/self-heal.js +58 -5
- package/lib/sshconfig.js +6 -0
- package/lib/telemetry.js +64 -25
- package/lib/update.js +95 -33
- package/lib/vendor/jsdiff/SOURCE.md +1 -1
- package/lib/vendor/jsdiff/diff/base.js +1 -1
- package/lib/vendor/jsdiff/diff/line.js +1 -1
- package/lib/vendor/jsdiff/patch/create.js +147 -18
- package/lib/vendor/jsdiff/util/params.js +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Local execution-world adapter for the shared workspace UI. Never used by rw_*
|
|
2
|
+
// Agent tools. Every filesystem operation is rooted in a server-resolved session
|
|
3
|
+
// directory; neither an active SSH machine nor a client-supplied root is used.
|
|
4
|
+
import fs from 'node:fs/promises'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { spawn } from 'node:child_process'
|
|
7
|
+
|
|
8
|
+
export const sameLocalPath = (a, b, grammar = path) => grammar.relative(a, b) === ''
|
|
9
|
+
const denied = () => Object.assign(new Error('Path is outside this local workspace (including symlink targets)'), { httpStatus: 403, code: 'LOCAL_WORKSPACE_PATH' })
|
|
10
|
+
const inside = (root, target) => { const rel = path.relative(root, target); return !path.isAbsolute(rel) && rel !== '..' && !rel.startsWith('..' + path.sep) }
|
|
11
|
+
const attrs = stat => ({
|
|
12
|
+
size: stat.size, mtime: stat.mtimeMs / 1000, mode: stat.mode,
|
|
13
|
+
isDirectory: () => stat.isDirectory(), isFile: () => stat.isFile(), isSymbolicLink: () => stat.isSymbolicLink(),
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export async function createLocalWorkspacePool(directory) {
|
|
17
|
+
const root = path.resolve(directory), canonical = await fs.realpath(root)
|
|
18
|
+
if (!(await fs.stat(canonical)).isDirectory()) throw Object.assign(new Error('Local workspace directory is unavailable'), { httpStatus: 404 })
|
|
19
|
+
const resolve = async (input, { mutation = false } = {}) => {
|
|
20
|
+
if (typeof input !== 'string' || !input || input.includes('\0') || !path.isAbsolute(input)) throw denied()
|
|
21
|
+
const target = path.resolve(input)
|
|
22
|
+
if (!inside(root, target) && !inside(canonical, target)) throw denied()
|
|
23
|
+
if (mutation && (sameLocalPath(root, target) || sameLocalPath(canonical, target))) throw denied()
|
|
24
|
+
// Missing children may be created, but their nearest existing ancestor must
|
|
25
|
+
// still be inside the canonical project. No symlink escape on read or save.
|
|
26
|
+
let ancestor = target
|
|
27
|
+
for (;;) {
|
|
28
|
+
try { if (!inside(canonical, await fs.realpath(ancestor))) throw denied(); break }
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (error.code !== 'ENOENT') throw error
|
|
31
|
+
const parent = path.dirname(ancestor)
|
|
32
|
+
if (parent === ancestor) throw denied()
|
|
33
|
+
// A dangling symlink must not be treated as an ordinary missing child.
|
|
34
|
+
try { if ((await fs.lstat(ancestor)).isSymbolicLink()) throw denied() } catch (e) { if (e.code !== 'ENOENT') throw e }
|
|
35
|
+
ancestor = parent
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return target
|
|
39
|
+
}
|
|
40
|
+
const regularFile = async p => {
|
|
41
|
+
const target = await resolve(p)
|
|
42
|
+
if (!(await fs.stat(target)).isFile()) throw Object.assign(new Error('Only regular local files can be previewed'), { httpStatus: 415 })
|
|
43
|
+
return target
|
|
44
|
+
}
|
|
45
|
+
const sftp = {
|
|
46
|
+
async stat(p) { return attrs(await fs.stat(await resolve(p))) },
|
|
47
|
+
async lstat(p) { return attrs(await fs.lstat(await resolve(p))) },
|
|
48
|
+
async realpath(p) { return fs.realpath(await resolve(p)) },
|
|
49
|
+
async readdir(p) {
|
|
50
|
+
const dir = await resolve(p), names = await fs.readdir(dir)
|
|
51
|
+
return Promise.all(names.map(async filename => ({ filename, attrs: attrs(await fs.lstat(path.join(dir, filename))) })))
|
|
52
|
+
},
|
|
53
|
+
async readFile(p) { return fs.readFile(await regularFile(p)) },
|
|
54
|
+
async readPartial(p, offset, length) {
|
|
55
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(length) || length < 0 || length > 4 * 1024 * 1024) throw new Error('Invalid bounded read')
|
|
56
|
+
const file = await fs.open(await regularFile(p), 'r')
|
|
57
|
+
try { const buffer = Buffer.alloc(length); const { bytesRead } = await file.read(buffer, 0, length, offset); return buffer.subarray(0, bytesRead) }
|
|
58
|
+
finally { await file.close() }
|
|
59
|
+
},
|
|
60
|
+
async mkdir(p) { return fs.mkdir(await resolve(p, { mutation: true })) },
|
|
61
|
+
async rmdir(p) { return fs.rmdir(await resolve(p, { mutation: true })) },
|
|
62
|
+
async unlink(p) { return fs.unlink(await resolve(p, { mutation: true })) },
|
|
63
|
+
async writeFile(p, bytes, options) { return fs.writeFile(await resolve(p, { mutation: true }), bytes, options) },
|
|
64
|
+
async setstat(p, data) { if (data.mode != null) await fs.chmod(await resolve(p, { mutation: true }), data.mode) },
|
|
65
|
+
async rename(from, to) {
|
|
66
|
+
const source = await resolve(from, { mutation: true }), dest = await resolve(to, { mutation: true })
|
|
67
|
+
const stat = await fs.lstat(source)
|
|
68
|
+
if (stat.isFile()) { await fs.link(source, dest); await fs.unlink(source); return }
|
|
69
|
+
try { await fs.lstat(dest); throw Object.assign(new Error('Destination already exists'), { code: 'EEXIST' }) }
|
|
70
|
+
catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
71
|
+
return fs.rename(source, dest)
|
|
72
|
+
},
|
|
73
|
+
async renameOverwrite(from, to) { return fs.rename(await resolve(from, { mutation: true }), await resolve(to, { mutation: true })) },
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
checkPath: resolve,
|
|
77
|
+
async mkdirTree(p) { return fs.mkdir(await resolve(p, { mutation: true }), { recursive: true }) },
|
|
78
|
+
async removeTree(p) { return fs.rm(await resolve(p, { mutation: true }), { recursive: true }) },
|
|
79
|
+
config: { local: true }, platform: process.platform === 'win32' ? 'windows' : 'posix', local: true,
|
|
80
|
+
async detect() {}, async sftp() { return sftp },
|
|
81
|
+
// Git receives an argv array, never a POSIX command string on Windows.
|
|
82
|
+
execFile(file, args, { signal, timeoutMs = 15000, onData } = {}) {
|
|
83
|
+
if (file !== 'git') throw new Error('Only read-only Git inspection is available through this adapter')
|
|
84
|
+
return new Promise(resolveResult => {
|
|
85
|
+
let stdout = '', stderr = '', timedOut = false, settled = false
|
|
86
|
+
const env = { ...process.env }
|
|
87
|
+
for (const key of ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_COMMON_DIR', 'GIT_OBJECT_DIRECTORY', 'GIT_ALTERNATE_OBJECT_DIRECTORIES', 'GIT_NAMESPACE', 'GIT_PREFIX']) delete env[key]
|
|
88
|
+
const child = spawn(file, args, { cwd: root, env, shell: false, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
89
|
+
const append = (channel, chunk) => { const text = chunk.toString(); if (channel === 'stdout') stdout = (stdout + text).slice(0, 256 * 1024); else stderr = (stderr + text).slice(0, 256 * 1024); onData?.(channel, text) }
|
|
90
|
+
child.stdout.setEncoding('utf8'); child.stderr.setEncoding('utf8')
|
|
91
|
+
child.stdout.on('data', data => append('stdout', data)); child.stderr.on('data', data => append('stderr', data))
|
|
92
|
+
const stop = () => child.kill()
|
|
93
|
+
const timer = setTimeout(() => { timedOut = true; stop() }, timeoutMs)
|
|
94
|
+
const done = (code, error) => {
|
|
95
|
+
if (settled) return; settled = true; clearTimeout(timer); signal?.removeEventListener('abort', stop)
|
|
96
|
+
resolveResult({ code: code ?? 1, stdout, stderr: error ? String(error.message || error) : stderr, ...(timedOut ? { signal: 'TIMEOUT' } : {}) })
|
|
97
|
+
}
|
|
98
|
+
child.on('error', error => done(error.code === 'ENOENT' ? 127 : 1, error)); child.on('close', code => done(code))
|
|
99
|
+
if (signal?.aborted) stop(); else signal?.addEventListener('abort', stop, { once: true })
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|