@thready-web/dsh-bot-plugin 0.1.0-beta.8 → 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/desk.js +158 -1
- package/dist/http.js +42 -5
- 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
package/dist/desk.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/** Workbench routes inside the DSH process. The browser calls /dsh-bot/api. */
|
|
2
2
|
import { mkdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
3
4
|
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
4
6
|
import { openLedger, validIdempotencyKey } from "./desk-ledger.js";
|
|
5
7
|
import { openSessionPreviews } from "./session-previews.js";
|
|
6
8
|
import { purgeSessionFiles, sessionDirectory, writeBotMarker } from "./session-files.js";
|
|
9
|
+
import { MAX_UPLOAD_BYTES, listDirectory, openDownload, readFileText, resolveFileRoots, saveUploadFile, } from "./workspace-files.js";
|
|
7
10
|
/**
|
|
8
11
|
* Working directory for workbench-created sessions. THREADY_SESSION_CWD wins;
|
|
9
12
|
* otherwise <DSH_HOME>/workspace, so the model does not start inside DSH_HOME
|
|
@@ -45,6 +48,7 @@ const SECRET_KEY = /token|secret|password|authorization|api[-_]?key|cookie/i;
|
|
|
45
48
|
* @param {(sessionId: string) => boolean} [options.stillAttached]
|
|
46
49
|
* @param {(endpoint: string, args: Record<string, unknown>, req: DeskRequest) => Promise<{ status: number, body: RpcEnvelope }>} options.rpc
|
|
47
50
|
* @param {(req: DeskRequest) => Promise<Record<string, unknown>>} options.usage
|
|
51
|
+
* @param {{ home: string, dshHome: string }} [options.files] Browser roots. Defaults to the process home and DSH_HOME.
|
|
48
52
|
*/
|
|
49
53
|
export function createDesk(options) {
|
|
50
54
|
const ledger = openLedger(options.ledgerFile);
|
|
@@ -65,7 +69,14 @@ export function createDesk(options) {
|
|
|
65
69
|
if (sub === null)
|
|
66
70
|
return false;
|
|
67
71
|
if (method === 'GET' &&
|
|
68
|
-
(sub === '/health' ||
|
|
72
|
+
(sub === '/health' ||
|
|
73
|
+
sub === '/sessions' ||
|
|
74
|
+
sub === '/tasks' ||
|
|
75
|
+
sub === '/usage' ||
|
|
76
|
+
sub === '/file' ||
|
|
77
|
+
sub === '/files' ||
|
|
78
|
+
sub === '/files/roots' ||
|
|
79
|
+
sub === '/files/text'))
|
|
69
80
|
return true;
|
|
70
81
|
if (method === 'POST' && (sub === '/sessions' || sub === '/sessions/previews' || sub === '/tasks'))
|
|
71
82
|
return true;
|
|
@@ -114,8 +125,20 @@ export function createDesk(options) {
|
|
|
114
125
|
return { status: 200, body: redactSecrets(await options.usage(req)) };
|
|
115
126
|
if (method === 'GET' && sub === '/file')
|
|
116
127
|
return filePreview(req, url);
|
|
128
|
+
if (method === 'GET' && sub === '/files/roots')
|
|
129
|
+
return fileRoots(req, url.searchParams.get('sessionId') ?? '');
|
|
130
|
+
if (method === 'GET' && sub === '/files')
|
|
131
|
+
return fileList(req, url.searchParams.get('sessionId') ?? '', url.searchParams.get('path') ?? '');
|
|
132
|
+
if (method === 'GET' && sub === '/files/text')
|
|
133
|
+
return fileText(req, url.searchParams.get('sessionId') ?? '', url.searchParams.get('path') ?? '');
|
|
117
134
|
return { status: 404, body: { error: 'not_found' } };
|
|
118
135
|
},
|
|
136
|
+
openDownload(req, url) {
|
|
137
|
+
return downloadFile(req, url.searchParams.get('sessionId') ?? '', url.searchParams.get('path') ?? '');
|
|
138
|
+
},
|
|
139
|
+
saveUpload(req, url) {
|
|
140
|
+
return uploadFile(req, url.searchParams.get('sessionId') ?? '', url.searchParams.get('dir') ?? '', url.searchParams.get('name') ?? '', url.searchParams.get('overwrite') === '1');
|
|
141
|
+
},
|
|
119
142
|
};
|
|
120
143
|
/**
|
|
121
144
|
* @param {DeskRequest} req
|
|
@@ -399,6 +422,118 @@ export function createDesk(options) {
|
|
|
399
422
|
});
|
|
400
423
|
return { status: result.body?.result?.ok === true ? 200 : 502, body: { task: updated } };
|
|
401
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* @param {DeskRequest} req
|
|
427
|
+
* @param {string} sessionId
|
|
428
|
+
*/
|
|
429
|
+
async function fileRoots(req, sessionId) {
|
|
430
|
+
const ready = await browserAccess(req, sessionId);
|
|
431
|
+
if ('error' in ready)
|
|
432
|
+
return fileFailure(ready.error);
|
|
433
|
+
return { status: 200, body: ready.access.roots };
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* @param {DeskRequest} req
|
|
437
|
+
* @param {string} sessionId
|
|
438
|
+
* @param {string} filePath
|
|
439
|
+
*/
|
|
440
|
+
async function fileList(req, sessionId, filePath) {
|
|
441
|
+
const ready = await browserAccess(req, sessionId);
|
|
442
|
+
if ('error' in ready)
|
|
443
|
+
return fileFailure(ready.error);
|
|
444
|
+
const listed = listDirectory(filePath, ready.access);
|
|
445
|
+
if (!listed.ok)
|
|
446
|
+
return fileFailure(listed.error);
|
|
447
|
+
return { status: 200, body: listed.value };
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* @param {DeskRequest} req
|
|
451
|
+
* @param {string} sessionId
|
|
452
|
+
* @param {string} filePath
|
|
453
|
+
*/
|
|
454
|
+
async function fileText(req, sessionId, filePath) {
|
|
455
|
+
const ready = await browserAccess(req, sessionId);
|
|
456
|
+
if ('error' in ready)
|
|
457
|
+
return fileFailure(ready.error);
|
|
458
|
+
const text = readFileText(filePath, ready.access);
|
|
459
|
+
if (!text.ok)
|
|
460
|
+
return fileFailure(text.error);
|
|
461
|
+
return { status: 200, body: text.value };
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* @param {DeskRequest} req
|
|
465
|
+
* @param {string} sessionId
|
|
466
|
+
* @param {string} filePath
|
|
467
|
+
*/
|
|
468
|
+
async function downloadFile(req, sessionId, filePath) {
|
|
469
|
+
const ready = await browserAccess(req, sessionId);
|
|
470
|
+
if ('error' in ready)
|
|
471
|
+
return fileFailure(ready.error);
|
|
472
|
+
const opened = openDownload(filePath, ready.access);
|
|
473
|
+
if (!opened.ok)
|
|
474
|
+
return fileFailure(opened.error);
|
|
475
|
+
return { status: 200, ...opened.value };
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* @param {DeskRequest} req
|
|
479
|
+
* @param {string} sessionId
|
|
480
|
+
* @param {string} dir
|
|
481
|
+
* @param {string} name
|
|
482
|
+
* @param {boolean} overwrite
|
|
483
|
+
*/
|
|
484
|
+
async function uploadFile(req, sessionId, dir, name, overwrite) {
|
|
485
|
+
const declared = Number(headerValue(req, 'content-length'));
|
|
486
|
+
if (Number.isFinite(declared) && declared > MAX_UPLOAD_BYTES)
|
|
487
|
+
return fileFailure('too_large');
|
|
488
|
+
if (!(req instanceof Readable))
|
|
489
|
+
return fileFailure('bad_path');
|
|
490
|
+
const ready = await browserAccess(req, sessionId);
|
|
491
|
+
if ('error' in ready) {
|
|
492
|
+
req.resume();
|
|
493
|
+
return fileFailure(ready.error);
|
|
494
|
+
}
|
|
495
|
+
const saved = await saveUploadFile(dir, name, req, ready.access, { overwrite, maxBytes: MAX_UPLOAD_BYTES });
|
|
496
|
+
if (!saved.ok) {
|
|
497
|
+
if (saved.error !== 'too_large')
|
|
498
|
+
req.resume();
|
|
499
|
+
return fileFailure(saved.error);
|
|
500
|
+
}
|
|
501
|
+
return { status: 201, body: saved.value };
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* @param {DeskRequest} req
|
|
505
|
+
* @param {string} sessionId
|
|
506
|
+
*/
|
|
507
|
+
async function browserAccess(req, sessionId) {
|
|
508
|
+
const cwd = await workspaceCwd(req, sessionId);
|
|
509
|
+
if (typeof cwd !== 'string')
|
|
510
|
+
return cwd;
|
|
511
|
+
const files = options.files ?? { home: homedir(), dshHome: process.env.DSH_HOME ?? '' };
|
|
512
|
+
return { access: resolveFileRoots({ workspace: cwd, home: files.home, dshHome: files.dshHome }) };
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* @param {DeskRequest} req
|
|
516
|
+
* @param {string} sessionId
|
|
517
|
+
* @returns {Promise<string | { error: 'invalid_session' }>}
|
|
518
|
+
*/
|
|
519
|
+
async function workspaceCwd(req, sessionId) {
|
|
520
|
+
if (!PREVIEW_SESSION.test(sessionId))
|
|
521
|
+
return { error: 'invalid_session' };
|
|
522
|
+
let cwd = options.sessionCwd;
|
|
523
|
+
try {
|
|
524
|
+
const listed = await options.rpc('session/list', { _request: {} }, req);
|
|
525
|
+
const items = listed.body?.result?.value?.items;
|
|
526
|
+
if (Array.isArray(items)) {
|
|
527
|
+
const item = items.find(entry => entry?.sessionId === sessionId);
|
|
528
|
+
if (typeof item?.cwd === 'string' && item.cwd !== '')
|
|
529
|
+
cwd = item.cwd;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
catch {
|
|
533
|
+
/* the configured workspace remains */
|
|
534
|
+
}
|
|
535
|
+
return cwd;
|
|
536
|
+
}
|
|
402
537
|
/**
|
|
403
538
|
* @param {DeskRequest} req
|
|
404
539
|
* @param {URL} url
|
|
@@ -560,6 +695,28 @@ function idempotencyOf(req, body) {
|
|
|
560
695
|
return header;
|
|
561
696
|
return typeof body.idempotencyKey === 'string' ? body.idempotencyKey : '';
|
|
562
697
|
}
|
|
698
|
+
function fileFailure(error) {
|
|
699
|
+
const status = error === 'denied'
|
|
700
|
+
? 403
|
|
701
|
+
: error === 'missing'
|
|
702
|
+
? 404
|
|
703
|
+
: error === 'binary'
|
|
704
|
+
? 415
|
|
705
|
+
: error === 'too_large'
|
|
706
|
+
? 413
|
|
707
|
+
: error === 'exists'
|
|
708
|
+
? 409
|
|
709
|
+
: 400;
|
|
710
|
+
return { status, body: { error } };
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* @param {DeskRequest} req
|
|
714
|
+
* @param {string} name
|
|
715
|
+
*/
|
|
716
|
+
function headerValue(req, name) {
|
|
717
|
+
const value = req.headers[name];
|
|
718
|
+
return Array.isArray(value) ? (value[0] ?? '') : (value ?? '');
|
|
719
|
+
}
|
|
563
720
|
/**
|
|
564
721
|
* Keep a preview path inside a session workspace: relative, no parent segments.
|
|
565
722
|
* @param {string} value
|
package/dist/http.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createReadStream, statSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
2
3
|
import { dirname, extname, join, resolve, sep } from 'node:path';
|
|
3
4
|
import { fileURLToPath } from 'node:url';
|
|
4
5
|
import { createDesk, defaultSessionCwd } from "./desk.js";
|
|
@@ -49,6 +50,7 @@ export function registerHttp(ctx, engine, getRuntime = () => null, getWork = ()
|
|
|
49
50
|
stillAttached: sessionId => attachedSession(ctx, sessionId),
|
|
50
51
|
rpc: (endpoint, args, req) => hostRpc(req, endpoint, args),
|
|
51
52
|
usage: req => hostUsage(req),
|
|
53
|
+
files: { home: homedir(), dshHome: process.env.DSH_HOME ?? '' },
|
|
52
54
|
});
|
|
53
55
|
/**
|
|
54
56
|
* @param {import('node:http').IncomingMessage} req
|
|
@@ -107,6 +109,16 @@ export function registerHttp(ctx, engine, getRuntime = () => null, getWork = ()
|
|
|
107
109
|
? '/session-foundations/api' + rawPath.slice('/dsh-bot/api'.length)
|
|
108
110
|
: rawPath;
|
|
109
111
|
try {
|
|
112
|
+
if (desk && req.method === 'GET' && path === '/session-foundations/api/files/raw') {
|
|
113
|
+
return pipeDownload(res, await desk.openDownload(req, url), url.searchParams.get('inline') === '1');
|
|
114
|
+
}
|
|
115
|
+
if (desk && req.method === 'POST' && path === '/session-foundations/api/files') {
|
|
116
|
+
const outcome = await desk.saveUpload(req, url);
|
|
117
|
+
send(res, outcome.status, outcome.body);
|
|
118
|
+
if (outcome.status === 413)
|
|
119
|
+
res.on('finish', () => req.destroy());
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
110
122
|
if (desk && desk.owns(req.method ?? 'GET', path)) {
|
|
111
123
|
const outcome = await desk.handle(req.method ?? 'GET', path, url, req);
|
|
112
124
|
return send(res, outcome.status, outcome.body);
|
|
@@ -599,11 +611,35 @@ function reject(ctx, req) {
|
|
|
599
611
|
return 403;
|
|
600
612
|
return 0;
|
|
601
613
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
614
|
+
function imageMedia(name) {
|
|
615
|
+
const ext = name.split('.').pop()?.toLowerCase() ?? '';
|
|
616
|
+
if (ext === 'png')
|
|
617
|
+
return 'image/png';
|
|
618
|
+
if (ext === 'jpg' || ext === 'jpeg')
|
|
619
|
+
return 'image/jpeg';
|
|
620
|
+
if (ext === 'gif')
|
|
621
|
+
return 'image/gif';
|
|
622
|
+
if (ext === 'webp')
|
|
623
|
+
return 'image/webp';
|
|
624
|
+
return null;
|
|
625
|
+
}
|
|
626
|
+
function pipeDownload(res, opened, inline = false) {
|
|
627
|
+
if (!('stream' in opened))
|
|
628
|
+
return send(res, opened.status, opened.body);
|
|
629
|
+
const encoded = encodeURIComponent(opened.name);
|
|
630
|
+
const image = inline ? imageMedia(opened.name) : null;
|
|
631
|
+
res.writeHead(opened.status, {
|
|
632
|
+
'content-type': image ?? 'application/octet-stream',
|
|
633
|
+
'content-length': opened.size,
|
|
634
|
+
'content-disposition': image ? `inline; filename*=UTF-8''${encoded}` : `attachment; filename*=UTF-8''${encoded}`,
|
|
635
|
+
'x-content-type-options': 'nosniff',
|
|
636
|
+
'cache-control': 'private, no-store',
|
|
637
|
+
'content-security-policy': "default-src 'none'; sandbox",
|
|
638
|
+
});
|
|
639
|
+
opened.stream.on('error', () => res.destroy());
|
|
640
|
+
res.on('close', () => opened.stream.destroy());
|
|
641
|
+
opened.stream.pipe(res);
|
|
642
|
+
}
|
|
607
643
|
function send(res, status, body) {
|
|
608
644
|
if (status === 0)
|
|
609
645
|
return;
|
|
@@ -784,6 +820,7 @@ const DESK_TYPES = new Map([
|
|
|
784
820
|
['.png', 'image/png'],
|
|
785
821
|
['.webp', 'image/webp'],
|
|
786
822
|
['.woff2', 'font/woff2'],
|
|
823
|
+
['.ttf', 'font/ttf'],
|
|
787
824
|
]);
|
|
788
825
|
/** Workbench files built into public/. API routes are handled before this. */
|
|
789
826
|
function serveDesk(req, res, pathname) {
|
|
@@ -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",
|