@thready-web/dsh-bot-plugin 0.1.0-beta.7 → 0.1.0-beta.9
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/dist/context.js +2 -0
- package/dist/desk.js +158 -1
- package/dist/http.js +42 -5
- package/dist/work-ledger.js +123 -0
- package/dist/work-mock.js +3 -0
- package/dist/work-tools.js +19 -0
- package/dist/work-worker.js +73 -1
- package/dist/workspace-files.js +319 -0
- package/package.json +1 -1
- package/public/assets/SourceEditor-CJwK846Z.css +1 -0
- package/public/assets/SourceEditor-Cu3oRqA0.js +669 -0
- package/public/assets/abap-CRCWOmpq.js +1 -0
- package/public/assets/apex-DnsZk_dE.js +1 -0
- package/public/assets/azcli-1IWB1ccx.js +1 -0
- package/public/assets/bat-DPkNLes8.js +1 -0
- package/public/assets/bicep-Corcdgou.js +2 -0
- package/public/assets/cameligo-CGrWLZr3.js +1 -0
- package/public/assets/clojure-D9WOWImG.js +1 -0
- package/public/assets/codicon-DCmgc-ay.ttf +0 -0
- package/public/assets/coffee-B7EJu28W.js +1 -0
- package/public/assets/cpp-SEyurbux.js +1 -0
- package/public/assets/csharp-BoL64M5l.js +1 -0
- package/public/assets/csp-C46ZqvIl.js +1 -0
- package/public/assets/css-DQU6DXDx.js +3 -0
- package/public/assets/css.worker-DYpkewsP.js +79 -0
- package/public/assets/cssMode-FWS1gu61.js +4 -0
- package/public/assets/cypher-D84EuPTj.js +1 -0
- package/public/assets/dart-D8lhlL1r.js +1 -0
- package/public/assets/dockerfile-DLk6rpji.js +1 -0
- package/public/assets/ecl-BO6FnfXk.js +1 -0
- package/public/assets/editor.worker-B4pQIWZD.js +12 -0
- package/public/assets/elixir-BRjLKONM.js +1 -0
- package/public/assets/flow9-Cac8vKd7.js +1 -0
- package/public/assets/freemarker2-U6v7W5T8.js +3 -0
- package/public/assets/fsharp-fd1GTHhf.js +1 -0
- package/public/assets/go-O9LJTZXk.js +1 -0
- package/public/assets/graphql-LQdxqEYJ.js +1 -0
- package/public/assets/handlebars-Dz33dYnR.js +1 -0
- package/public/assets/hcl-DxDQ3s82.js +1 -0
- package/public/assets/html-Bg_jMpt6.js +1 -0
- package/public/assets/html.worker-tnXziJOq.js +456 -0
- package/public/assets/htmlMode-CzB3QL-e.js +4 -0
- package/public/assets/index-Ah9i9dFa.js +63 -0
- package/public/assets/index-PsHINv2D.css +1 -0
- package/public/assets/ini-BvajGCUy.js +1 -0
- package/public/assets/java-SYsfObOQ.js +1 -0
- package/public/assets/javascript-DMkhf8OX.js +1 -0
- package/public/assets/json.worker-aFakAbRR.js +44 -0
- package/public/assets/jsonMode-DRHhZtb7.js +10 -0
- package/public/assets/julia-DQXNmw_w.js +1 -0
- package/public/assets/kotlin-qQ0MG-9I.js +1 -0
- package/public/assets/less-GGFNNJHn.js +2 -0
- package/public/assets/lexon-Canl7DCW.js +1 -0
- package/public/assets/liquid-C-8-PQ92.js +1 -0
- package/public/assets/lua-D28Ae8-K.js +1 -0
- package/public/assets/m3-DPitgjJI.js +1 -0
- package/public/assets/markdown-B811l8j2.js +1 -0
- package/public/assets/mdx-z749XIEc.js +1 -0
- package/public/assets/mips-CdjsipkG.js +1 -0
- package/public/assets/msdax-CYqgjx_P.js +1 -0
- package/public/assets/mysql-BHd6q0vd.js +1 -0
- package/public/assets/objective-c-B1aVtJYH.js +1 -0
- package/public/assets/pascal-BhNW15KB.js +1 -0
- package/public/assets/pascaligo-5jv8CcQD.js +1 -0
- package/public/assets/perl-DlYyT36c.js +1 -0
- package/public/assets/pgsql-Dy0bjov7.js +1 -0
- package/public/assets/php-120yhfDK.js +1 -0
- package/public/assets/pla-CjnFlu4u.js +1 -0
- package/public/assets/postiats-CQpG440k.js +1 -0
- package/public/assets/powerquery-DdJtto1Z.js +1 -0
- package/public/assets/powershell-Bu_VLpJB.js +1 -0
- package/public/assets/protobuf-IBS6jZEB.js +2 -0
- package/public/assets/pug-kFxLfcjb.js +1 -0
- package/public/assets/python-BHKY4qLy.js +1 -0
- package/public/assets/qsharp-q7JyzKFN.js +1 -0
- package/public/assets/r-BIFz-_sK.js +1 -0
- package/public/assets/razor-CNY--QcM.js +1 -0
- package/public/assets/redis-CHOsPHWR.js +1 -0
- package/public/assets/redshift-CBifECDb.js +1 -0
- package/public/assets/restructuredtext-CghPJEOS.js +1 -0
- package/public/assets/ruby-CYWGW-b1.js +1 -0
- package/public/assets/rust-DMDD0SHb.js +1 -0
- package/public/assets/sb-BYAiYHFx.js +1 -0
- package/public/assets/scala-Bqvq8jcR.js +1 -0
- package/public/assets/scheme-Dhb-2j9p.js +1 -0
- package/public/assets/scss-CTwUZ5N7.js +3 -0
- package/public/assets/shell-CsDZo4DB.js +1 -0
- package/public/assets/solidity-CME5AdoB.js +1 -0
- package/public/assets/sophia-RYC1BQQz.js +1 -0
- package/public/assets/sparql-KEyrF7De.js +1 -0
- package/public/assets/sql-BdTr02Mf.js +1 -0
- package/public/assets/st-C7iG7M4S.js +1 -0
- package/public/assets/swift-D7IUmUK8.js +1 -0
- package/public/assets/systemverilog-DgMryOEJ.js +1 -0
- package/public/assets/tcl-PloMZuKG.js +1 -0
- package/public/assets/ts.worker-Bij5J2kb.js +51316 -0
- package/public/assets/tsMode-BdcCVhTZ.js +11 -0
- package/public/assets/twig-BfRIq3la.js +1 -0
- package/public/assets/typescript-BD5fPxu4.js +1 -0
- package/public/assets/typespec-CzxlYoT_.js +1 -0
- package/public/assets/vb-BwAE3J76.js +1 -0
- package/public/assets/wgsl-B_1kOXbF.js +298 -0
- package/public/assets/xml-DuYJr6Y5.js +1 -0
- package/public/assets/yaml-DMyywRAi.js +1 -0
- package/public/index.html +2 -2
- package/public/assets/index-BL0MiCSK.js +0 -63
- package/public/assets/index-DS3-wcOY.css +0 -1
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/** Workbench file browser. Workspace and the process home are readable. DSH_HOME stays closed. */
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { closeSync, constants, createReadStream, createWriteStream, fstatSync, lstatSync, openSync, readdirSync, readSync, realpathSync, renameSync, unlinkSync, } from 'node:fs';
|
|
4
|
+
import { basename, join, resolve, sep } from 'node:path';
|
|
5
|
+
export const MAX_UPLOAD_BYTES = 50 * 1024 * 1024;
|
|
6
|
+
export const MAX_TEXT_BYTES = 1024 * 1024;
|
|
7
|
+
/**
|
|
8
|
+
* Real paths the browser may use. A workspace that is DSH_HOME, or that contains it, is unusable.
|
|
9
|
+
* The workspace directory itself stays allowed when it lives at <DSH_HOME>/workspace.
|
|
10
|
+
*/
|
|
11
|
+
export function resolveFileRoots(input) {
|
|
12
|
+
const home = realDir(input.home);
|
|
13
|
+
const dshHome = realDir(input.dshHome);
|
|
14
|
+
const workspaceRaw = realDir(input.workspace);
|
|
15
|
+
let workspace = workspaceRaw;
|
|
16
|
+
let workspaceReason = workspaceRaw ? '' : 'missing';
|
|
17
|
+
if (workspaceRaw && dshHome && (workspaceRaw === dshHome || inside(workspaceRaw, dshHome))) {
|
|
18
|
+
workspace = null;
|
|
19
|
+
workspaceReason = 'overlap';
|
|
20
|
+
}
|
|
21
|
+
return { roots: { workspace, workspaceReason, home }, dshHome };
|
|
22
|
+
}
|
|
23
|
+
export function listDirectory(filePath, access) {
|
|
24
|
+
const located = locate(filePath, access);
|
|
25
|
+
if (!located.ok)
|
|
26
|
+
return located;
|
|
27
|
+
let stat;
|
|
28
|
+
try {
|
|
29
|
+
stat = lstatSync(located.value.path);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return { ok: false, error: 'missing' };
|
|
33
|
+
}
|
|
34
|
+
if (!stat.isDirectory())
|
|
35
|
+
return { ok: false, error: 'not_directory' };
|
|
36
|
+
let names;
|
|
37
|
+
try {
|
|
38
|
+
names = readdirSync(located.value.path);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return { ok: false, error: 'denied' };
|
|
42
|
+
}
|
|
43
|
+
const items = [];
|
|
44
|
+
for (const name of names) {
|
|
45
|
+
if (name.startsWith('.upload-') && name.endsWith('.tmp'))
|
|
46
|
+
continue;
|
|
47
|
+
const child = join(located.value.path, name);
|
|
48
|
+
let childStat;
|
|
49
|
+
try {
|
|
50
|
+
childStat = lstatSync(child);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
let real;
|
|
56
|
+
try {
|
|
57
|
+
real = realpathSync(child);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!rootOf(real, access))
|
|
63
|
+
continue;
|
|
64
|
+
const mtime = new Date(childStat.mtimeMs).toISOString();
|
|
65
|
+
if (childStat.isSymbolicLink()) {
|
|
66
|
+
items.push({ name, kind: 'link', size: 0, mtime });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (childStat.isDirectory()) {
|
|
70
|
+
items.push({ name, kind: 'dir', size: 0, mtime });
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (childStat.isFile())
|
|
74
|
+
items.push({ name, kind: 'file', size: childStat.size, mtime });
|
|
75
|
+
}
|
|
76
|
+
items.sort((a, b) => {
|
|
77
|
+
if (a.kind === 'dir' && b.kind !== 'dir')
|
|
78
|
+
return -1;
|
|
79
|
+
if (a.kind !== 'dir' && b.kind === 'dir')
|
|
80
|
+
return 1;
|
|
81
|
+
return a.name.localeCompare(b.name);
|
|
82
|
+
});
|
|
83
|
+
return { ok: true, value: { path: located.value.path, root: located.value.root, items } };
|
|
84
|
+
}
|
|
85
|
+
export function readFileText(filePath, access) {
|
|
86
|
+
const opened = openFile(filePath, access);
|
|
87
|
+
if (!opened.ok)
|
|
88
|
+
return opened;
|
|
89
|
+
try {
|
|
90
|
+
const size = opened.value.size;
|
|
91
|
+
if (size === 0)
|
|
92
|
+
return { ok: true, value: { text: '', eof: true } };
|
|
93
|
+
const bytes = readFd(opened.value.fd, Math.min(size, MAX_TEXT_BYTES));
|
|
94
|
+
if (bytes.includes(0))
|
|
95
|
+
return { ok: false, error: 'binary' };
|
|
96
|
+
return { ok: true, value: { text: bytes.toString('utf8'), eof: size <= MAX_TEXT_BYTES } };
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
closeSync(opened.value.fd);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export function openDownload(filePath, access) {
|
|
103
|
+
const opened = openFile(filePath, access);
|
|
104
|
+
if (!opened.ok)
|
|
105
|
+
return opened;
|
|
106
|
+
const stream = createReadStream('', { fd: opened.value.fd, autoClose: true, start: 0 });
|
|
107
|
+
return {
|
|
108
|
+
ok: true,
|
|
109
|
+
value: { name: basename(opened.value.path), size: opened.value.size, stream },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Write one file into an allowed directory. `maxBytes` stops the copy without destroying the request socket.
|
|
114
|
+
*/
|
|
115
|
+
export function saveUploadFile(dir, name, source, access, options) {
|
|
116
|
+
const safe = safeFileName(name);
|
|
117
|
+
if (!safe)
|
|
118
|
+
return Promise.resolve({ ok: false, error: 'bad_name' });
|
|
119
|
+
const located = locate(dir, access);
|
|
120
|
+
if (!located.ok)
|
|
121
|
+
return Promise.resolve(located);
|
|
122
|
+
let dirStat;
|
|
123
|
+
try {
|
|
124
|
+
dirStat = lstatSync(located.value.path);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return Promise.resolve({ ok: false, error: 'missing' });
|
|
128
|
+
}
|
|
129
|
+
if (!dirStat.isDirectory())
|
|
130
|
+
return Promise.resolve({ ok: false, error: 'not_directory' });
|
|
131
|
+
const dest = join(located.value.path, safe);
|
|
132
|
+
let existing = null;
|
|
133
|
+
try {
|
|
134
|
+
existing = lstatSync(dest);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
existing = null;
|
|
138
|
+
}
|
|
139
|
+
if (existing?.isSymbolicLink())
|
|
140
|
+
return Promise.resolve({ ok: false, error: 'denied' });
|
|
141
|
+
if (existing?.isDirectory())
|
|
142
|
+
return Promise.resolve({ ok: false, error: 'exists' });
|
|
143
|
+
if (existing && !options.overwrite)
|
|
144
|
+
return Promise.resolve({ ok: false, error: 'exists' });
|
|
145
|
+
const tmp = join(located.value.path, `.upload-${randomBytes(8).toString('hex')}.tmp`);
|
|
146
|
+
return writeBounded(source, tmp, options.maxBytes).then(written => {
|
|
147
|
+
if (!written.ok)
|
|
148
|
+
return written;
|
|
149
|
+
try {
|
|
150
|
+
renameSync(tmp, dest);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
try {
|
|
154
|
+
unlinkSync(tmp);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
/* the failed temp is hidden from listings */
|
|
158
|
+
}
|
|
159
|
+
return { ok: false, error: 'denied' };
|
|
160
|
+
}
|
|
161
|
+
return { ok: true, value: { path: dest, size: written.value } };
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
export function safeFileName(value) {
|
|
165
|
+
if (value.length === 0 || value.length > 255)
|
|
166
|
+
return null;
|
|
167
|
+
if (value === '.' || value === '..')
|
|
168
|
+
return null;
|
|
169
|
+
if (/[/\\\u0000]/.test(value))
|
|
170
|
+
return null;
|
|
171
|
+
if (basename(value) !== value)
|
|
172
|
+
return null;
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
function realDir(filePath) {
|
|
176
|
+
if (filePath.trim() === '')
|
|
177
|
+
return null;
|
|
178
|
+
try {
|
|
179
|
+
return realpathSync(resolve(filePath));
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function inside(root, target) {
|
|
186
|
+
return target === root || target.startsWith(root + sep);
|
|
187
|
+
}
|
|
188
|
+
function rootOf(filePath, access) {
|
|
189
|
+
const inWorkspace = access.roots.workspace != null && inside(access.roots.workspace, filePath);
|
|
190
|
+
const inHome = access.roots.home != null && inside(access.roots.home, filePath);
|
|
191
|
+
if (!inWorkspace && !inHome)
|
|
192
|
+
return null;
|
|
193
|
+
if (access.dshHome && inside(access.dshHome, filePath) && !inWorkspace)
|
|
194
|
+
return null;
|
|
195
|
+
return inWorkspace ? 'workspace' : 'home';
|
|
196
|
+
}
|
|
197
|
+
function locate(filePath, access) {
|
|
198
|
+
if (filePath.length === 0 || filePath.length > 4096 || /[\u0000]/.test(filePath) || !filePath.startsWith('/')) {
|
|
199
|
+
return { ok: false, error: 'bad_path' };
|
|
200
|
+
}
|
|
201
|
+
let resolved;
|
|
202
|
+
try {
|
|
203
|
+
resolved = realpathSync(filePath);
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
const code = error.code;
|
|
207
|
+
return { ok: false, error: code === 'ENOENT' ? 'missing' : 'denied' };
|
|
208
|
+
}
|
|
209
|
+
const root = rootOf(resolved, access);
|
|
210
|
+
if (!root)
|
|
211
|
+
return { ok: false, error: 'denied' };
|
|
212
|
+
return { ok: true, value: { path: resolved, root } };
|
|
213
|
+
}
|
|
214
|
+
function openFile(filePath, access) {
|
|
215
|
+
const located = locate(filePath, access);
|
|
216
|
+
if (!located.ok)
|
|
217
|
+
return located;
|
|
218
|
+
try {
|
|
219
|
+
if (!lstatSync(located.value.path).isFile())
|
|
220
|
+
return { ok: false, error: 'not_file' };
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
return { ok: false, error: 'missing' };
|
|
224
|
+
}
|
|
225
|
+
let fd;
|
|
226
|
+
try {
|
|
227
|
+
fd = openSync(located.value.path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return { ok: false, error: 'denied' };
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
const opened = process.platform === 'linux' ? realpathSync(`/proc/self/fd/${fd}`) : located.value.path;
|
|
234
|
+
const stat = fstatSync(fd);
|
|
235
|
+
if (!stat.isFile() || !rootOf(opened, access)) {
|
|
236
|
+
closeSync(fd);
|
|
237
|
+
return { ok: false, error: stat.isFile() ? 'denied' : 'not_file' };
|
|
238
|
+
}
|
|
239
|
+
return { ok: true, value: { fd, path: opened, size: stat.size } };
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
try {
|
|
243
|
+
closeSync(fd);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
/* already closed */
|
|
247
|
+
}
|
|
248
|
+
return { ok: false, error: 'denied' };
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function readFd(fd, size) {
|
|
252
|
+
const bytes = Buffer.alloc(size);
|
|
253
|
+
let offset = 0;
|
|
254
|
+
while (offset < size) {
|
|
255
|
+
const read = readSync(fd, bytes, offset, size - offset, offset);
|
|
256
|
+
if (read === 0)
|
|
257
|
+
break;
|
|
258
|
+
offset += read;
|
|
259
|
+
}
|
|
260
|
+
return bytes.subarray(0, offset);
|
|
261
|
+
}
|
|
262
|
+
function writeBounded(source, tmp, maxBytes) {
|
|
263
|
+
return new Promise(resolveWrite => {
|
|
264
|
+
const out = createWriteStream(tmp, { flags: 'wx', mode: 0o644 });
|
|
265
|
+
let size = 0;
|
|
266
|
+
let settled = false;
|
|
267
|
+
const fail = (error) => {
|
|
268
|
+
if (settled)
|
|
269
|
+
return;
|
|
270
|
+
settled = true;
|
|
271
|
+
source.pause();
|
|
272
|
+
source.removeListener('data', onData);
|
|
273
|
+
const done = () => {
|
|
274
|
+
try {
|
|
275
|
+
unlinkSync(tmp);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
/* hidden temp, or the file was never created */
|
|
279
|
+
}
|
|
280
|
+
resolveWrite({ ok: false, error });
|
|
281
|
+
};
|
|
282
|
+
if (out.closed)
|
|
283
|
+
done();
|
|
284
|
+
else {
|
|
285
|
+
out.once('close', done);
|
|
286
|
+
out.destroy();
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
const onData = (chunk) => {
|
|
290
|
+
if (settled)
|
|
291
|
+
return;
|
|
292
|
+
size += chunk.length;
|
|
293
|
+
if (size > maxBytes) {
|
|
294
|
+
fail('too_large');
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (!out.write(chunk))
|
|
298
|
+
source.pause();
|
|
299
|
+
};
|
|
300
|
+
out.on('drain', () => {
|
|
301
|
+
if (!settled)
|
|
302
|
+
source.resume();
|
|
303
|
+
});
|
|
304
|
+
out.on('error', () => fail('denied'));
|
|
305
|
+
source.on('data', onData);
|
|
306
|
+
source.on('end', () => {
|
|
307
|
+
if (settled)
|
|
308
|
+
return;
|
|
309
|
+
out.end(() => {
|
|
310
|
+
if (settled)
|
|
311
|
+
return;
|
|
312
|
+
settled = true;
|
|
313
|
+
resolveWrite({ ok: true, value: size });
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
source.on('error', () => fail('denied'));
|
|
317
|
+
source.resume();
|
|
318
|
+
});
|
|
319
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thready-web/dsh-bot-plugin",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH bot plugin: session persona, memory, work, hash routing, and the workbench. Pinned to @deepseek-ai/dsh 0.2.2-rc.1.",
|
|
6
6
|
"license": "MIT",
|