@vite-hub/shell 0.1.0 → 0.3.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.md +16 -14
- package/dist/analyze-kSc2pmy2.js +459 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/just-bash-CHpGR1dZ.d.ts +37 -0
- package/dist/providers/just-bash.d.ts +2 -2
- package/dist/providers/just-bash.js +102 -151
- package/dist/{session-CIy2VLp9.js → session-DjjZUIPE.js} +96 -31
- package/dist/{types-2uHfTVHi.d.ts → types-wPMXgnB0.d.ts} +8 -7
- package/dist/workspace.d.ts +3 -3
- package/dist/workspace.js +65 -50
- package/package.json +6 -6
- package/dist/analyze-BXwz20IH.js +0 -247
- package/dist/just-bash-DAyZQWLK.d.ts +0 -27
package/dist/workspace.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { o as
|
|
2
|
-
import {
|
|
1
|
+
import { o as ShellDriver, s as ShellObservation } from "./types-wPMXgnB0.js";
|
|
2
|
+
import { a as ShellNetworkGrantExecutor } from "./just-bash-CHpGR1dZ.js";
|
|
3
3
|
import { ExecutionBudget } from "@vite-hub/runtime";
|
|
4
4
|
import { IFileSystem } from "just-bash";
|
|
5
5
|
//#region src/workspace/types.d.ts
|
|
@@ -70,7 +70,7 @@ interface WorkspaceInspectionCommandOptions {
|
|
|
70
70
|
fs: WorkspaceShellFileSystem;
|
|
71
71
|
maxOutputLength?: number;
|
|
72
72
|
networkGrants?: ShellNetworkGrantExecutor;
|
|
73
|
-
|
|
73
|
+
driver?: ShellDriver;
|
|
74
74
|
signal?: AbortSignal;
|
|
75
75
|
timeout?: number;
|
|
76
76
|
}
|
package/dist/workspace.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as createShellRuntime } from "./session-
|
|
3
|
-
import {
|
|
1
|
+
import { n as splitShellCommandSegments, r as shellErrorDiagnostics } from "./analyze-kSc2pmy2.js";
|
|
2
|
+
import { t as createShellRuntime } from "./session-DjjZUIPE.js";
|
|
3
|
+
import { createJustBashDriver } from "./providers/just-bash.js";
|
|
4
4
|
import { Buffer } from "node:buffer";
|
|
5
5
|
import { posix } from "node:path";
|
|
6
|
-
import { hasRuntimeType } from "@vite-hub/runtime/internal/runtime-type";
|
|
7
6
|
//#region src/workspace/filesystem.ts
|
|
8
7
|
const workspaceMountPoint = "/workspace";
|
|
9
8
|
function createEscapeError(path) {
|
|
@@ -13,7 +12,7 @@ function createReadonlyError() {
|
|
|
13
12
|
return shellErrorDiagnostics.SHELL_R0014({ message: "[vitehub] Workspace filesystem is read-only." });
|
|
14
13
|
}
|
|
15
14
|
function toShellContent(content, encoding) {
|
|
16
|
-
if (
|
|
15
|
+
if (typeof content !== "string") return content;
|
|
17
16
|
if (encoding === "base64") {
|
|
18
17
|
const decoded = atob(content);
|
|
19
18
|
const bytes = new Uint8Array(decoded.length);
|
|
@@ -93,7 +92,7 @@ var WorkspaceFileSystem = class {
|
|
|
93
92
|
}
|
|
94
93
|
async writeFile(path, content, options) {
|
|
95
94
|
const workspace = this.#requireWritable();
|
|
96
|
-
const encoding =
|
|
95
|
+
const encoding = typeof options === "string" ? options : options?.encoding;
|
|
97
96
|
await workspace.writeFile(this.#toRelativePath(path), encoding ? toShellContent(content, encoding) : content);
|
|
98
97
|
await this.#refreshPaths();
|
|
99
98
|
}
|
|
@@ -104,7 +103,7 @@ var WorkspaceFileSystem = class {
|
|
|
104
103
|
if (await workspace.exists(relativePath)) throw error;
|
|
105
104
|
return /* @__PURE__ */ new Uint8Array();
|
|
106
105
|
}));
|
|
107
|
-
const next = toShellContent(content,
|
|
106
|
+
const next = toShellContent(content, typeof options === "string" ? options : options?.encoding);
|
|
108
107
|
const merged = new Uint8Array(current.byteLength + next.byteLength);
|
|
109
108
|
merged.set(current, 0);
|
|
110
109
|
merged.set(next, current.byteLength);
|
|
@@ -258,16 +257,12 @@ function createWritableWorkspaceFs(workspace) {
|
|
|
258
257
|
}
|
|
259
258
|
//#endregion
|
|
260
259
|
//#region src/workspace/command-analysis.ts
|
|
261
|
-
function analyzeWorkspaceInspectionCommand(command) {
|
|
262
|
-
return splitShellCommandSegments(command).map((segment) => {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
searchRecursive: hasRecursiveGrepFlag(words),
|
|
268
|
-
words
|
|
269
|
-
};
|
|
270
|
-
});
|
|
260
|
+
async function analyzeWorkspaceInspectionCommand(command) {
|
|
261
|
+
return (await splitShellCommandSegments(command)).map((segment) => ({
|
|
262
|
+
...segment,
|
|
263
|
+
paths: shellPathArguments(segment.words),
|
|
264
|
+
searchRecursive: hasRecursiveGrepFlag(segment.words)
|
|
265
|
+
}));
|
|
271
266
|
}
|
|
272
267
|
function shellPathArguments(words) {
|
|
273
268
|
switch (words[0]) {
|
|
@@ -471,30 +466,31 @@ async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
|
471
466
|
throwIfAborted(options.signal);
|
|
472
467
|
const maxOutputLength = options.maxOutputLength === void 0 ? 3e4 : normalizeOutputLength(options.maxOutputLength);
|
|
473
468
|
const timeout = options.timeout ?? 3e4;
|
|
474
|
-
const
|
|
469
|
+
const driver = options.driver && options.networkGrants && await usesWorkspaceNetworkGrant(command) ? void 0 : options.driver;
|
|
475
470
|
const chargeRejected = () => {
|
|
476
471
|
if (!options.budgetCharged) options.budget?.recordToolCall(new TextEncoder().encode(command).byteLength);
|
|
477
472
|
};
|
|
478
|
-
const
|
|
473
|
+
const segments = await analyzeWorkspaceInspectionCommand(command);
|
|
474
|
+
const unsupportedSyntax = preflightUnsupportedWorkspaceSyntax(command, segments, options.commands, Boolean(driver));
|
|
479
475
|
if (unsupportedSyntax) {
|
|
480
476
|
chargeRejected();
|
|
481
477
|
return limitInspectionObservation(unsupportedSyntax, maxOutputLength);
|
|
482
478
|
}
|
|
483
479
|
const preflight = await withInspectionTimeout(timeout, options.signal, async (signal) => {
|
|
484
|
-
const broad = await preflightWorkspaceInspectionCommand(
|
|
480
|
+
const broad = await preflightWorkspaceInspectionCommand(segments, options.fs, options.broadSearchPaths, options.cwd, signal);
|
|
485
481
|
if (broad) return broad;
|
|
486
|
-
return await preflightMissingWorkspacePath(command, options.fs, options.cwd, signal);
|
|
482
|
+
return await preflightMissingWorkspacePath(command, segments, options.fs, options.cwd, signal);
|
|
487
483
|
}, command, options.cwd);
|
|
488
484
|
if (preflight) {
|
|
489
485
|
chargeRejected();
|
|
490
486
|
return limitInspectionObservation(preflight, maxOutputLength);
|
|
491
487
|
}
|
|
492
|
-
const pipedBroadSearch = preflightPipedSearchWithoutPath(
|
|
488
|
+
const pipedBroadSearch = preflightPipedSearchWithoutPath(segments, options.broadSearchPaths);
|
|
493
489
|
if (pipedBroadSearch) {
|
|
494
490
|
chargeRejected();
|
|
495
491
|
return limitInspectionObservation(pipedBroadSearch, maxOutputLength);
|
|
496
492
|
}
|
|
497
|
-
const unsupported = preflightUnsupportedWorkspaceCommand(command, options.commands, Boolean(
|
|
493
|
+
const unsupported = preflightUnsupportedWorkspaceCommand(command, segments, options.commands, Boolean(driver));
|
|
498
494
|
if (unsupported) {
|
|
499
495
|
chargeRejected();
|
|
500
496
|
return limitInspectionObservation(unsupported, maxOutputLength);
|
|
@@ -504,7 +500,7 @@ async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
|
504
500
|
maxOutputLength,
|
|
505
501
|
timeout
|
|
506
502
|
},
|
|
507
|
-
|
|
503
|
+
driver: driver ?? createJustBashDriver({
|
|
508
504
|
commands: options.commands,
|
|
509
505
|
cwd: options.cwd || "/workspace",
|
|
510
506
|
fs: options.fs,
|
|
@@ -518,9 +514,9 @@ async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
|
518
514
|
cwd,
|
|
519
515
|
timeout,
|
|
520
516
|
signal: options.signal,
|
|
521
|
-
workspacePaths: workspaceSessionPaths(
|
|
517
|
+
workspacePaths: workspaceSessionPaths(segments, cwd)
|
|
522
518
|
});
|
|
523
|
-
const noMatchFeedback = searchNoMatchFeedback(
|
|
519
|
+
const noMatchFeedback = searchNoMatchFeedback(segments, result, options.broadSearchPaths, options.cwd);
|
|
524
520
|
if (!noMatchFeedback) return result;
|
|
525
521
|
const bounded = limitInspectionFeedback(noMatchFeedback, maxOutputLength);
|
|
526
522
|
return {
|
|
@@ -595,11 +591,11 @@ async function withInspectionTimeout(timeout, callerSignal, run, command, cwd) {
|
|
|
595
591
|
removeCallerAbortListener?.();
|
|
596
592
|
}
|
|
597
593
|
}
|
|
598
|
-
function workspaceSessionPaths(
|
|
594
|
+
function workspaceSessionPaths(segments, cwd = workspaceMountPoint) {
|
|
599
595
|
try {
|
|
600
596
|
const paths = /* @__PURE__ */ new Set();
|
|
601
597
|
let currentCwd = cwd;
|
|
602
|
-
for (const segment of
|
|
598
|
+
for (const segment of segments) {
|
|
603
599
|
const words = segment.words;
|
|
604
600
|
if (words[0] === "cd") {
|
|
605
601
|
const path = words[1] || "/workspace";
|
|
@@ -616,9 +612,9 @@ function workspaceSessionPaths(command, cwd = workspaceMountPoint) {
|
|
|
616
612
|
return;
|
|
617
613
|
}
|
|
618
614
|
}
|
|
619
|
-
function preflightPipedSearchWithoutPath(
|
|
615
|
+
function preflightPipedSearchWithoutPath(segments, broadSearchPaths = []) {
|
|
620
616
|
try {
|
|
621
|
-
for (const segment of
|
|
617
|
+
for (const segment of segments) {
|
|
622
618
|
if (segment.separatorAfter !== "|" || segment.paths.length) continue;
|
|
623
619
|
const name = segment.words[0];
|
|
624
620
|
if (name === "rg" || name === "grep") return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
@@ -627,16 +623,34 @@ function preflightPipedSearchWithoutPath(command, broadSearchPaths = []) {
|
|
|
627
623
|
return;
|
|
628
624
|
}
|
|
629
625
|
}
|
|
630
|
-
function usesWorkspaceNetworkGrant(command) {
|
|
626
|
+
async function usesWorkspaceNetworkGrant(command) {
|
|
631
627
|
try {
|
|
632
|
-
|
|
628
|
+
const { Bash } = await import("just-bash/browser");
|
|
629
|
+
return containsCurlCommand(new Bash({ commands: [] }).transform(command).ast);
|
|
633
630
|
} catch {
|
|
634
631
|
return false;
|
|
635
632
|
}
|
|
636
633
|
}
|
|
637
|
-
|
|
634
|
+
function containsCurlCommand(node) {
|
|
635
|
+
if ("type" in node && node.type === "SimpleCommand") {
|
|
636
|
+
const { name } = node;
|
|
637
|
+
if (name && staticCommandWord(name.parts) === "curl") return true;
|
|
638
|
+
}
|
|
639
|
+
return Object.values(node).some((value) => value && typeof value === "object" && containsCurlCommand(value));
|
|
640
|
+
}
|
|
641
|
+
function staticCommandWord(parts) {
|
|
642
|
+
let word = "";
|
|
643
|
+
for (const part of parts) if (part.type === "Literal" || part.type === "SingleQuoted" || part.type === "Escaped") word += part.value;
|
|
644
|
+
else if (part.type === "DoubleQuoted") {
|
|
645
|
+
const quoted = staticCommandWord(part.parts);
|
|
646
|
+
if (quoted === void 0) return void 0;
|
|
647
|
+
word += quoted;
|
|
648
|
+
} else return void 0;
|
|
649
|
+
return word;
|
|
650
|
+
}
|
|
651
|
+
async function preflightWorkspaceInspectionCommand(segments, fs, broadSearchPaths = [], cwd = workspaceMountPoint, signal) {
|
|
638
652
|
try {
|
|
639
|
-
for await (const { segment, cwd: segmentCwd } of walkWorkspaceInspectionCommand(
|
|
653
|
+
for await (const { segment, cwd: segmentCwd } of walkWorkspaceInspectionCommand(segments, fs, cwd, signal)) {
|
|
640
654
|
throwIfAborted(signal);
|
|
641
655
|
if (isBroadWorkspaceSearch(segment, broadSearchPaths, segmentCwd)) return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
642
656
|
}
|
|
@@ -645,9 +659,9 @@ async function preflightWorkspaceInspectionCommand(command, fs, broadSearchPaths
|
|
|
645
659
|
return;
|
|
646
660
|
}
|
|
647
661
|
}
|
|
648
|
-
async function preflightMissingWorkspacePath(command, fs, cwd = workspaceMountPoint, signal) {
|
|
662
|
+
async function preflightMissingWorkspacePath(command, segments, fs, cwd = workspaceMountPoint, signal) {
|
|
649
663
|
try {
|
|
650
|
-
for await (const { segment, cwd: segmentCwd } of walkWorkspaceInspectionCommand(
|
|
664
|
+
for await (const { segment, cwd: segmentCwd } of walkWorkspaceInspectionCommand(segments, fs, cwd, signal)) {
|
|
651
665
|
throwIfAborted(signal);
|
|
652
666
|
for (const path of segment.paths) {
|
|
653
667
|
if (!isConcreteWorkspacePath(path)) continue;
|
|
@@ -665,11 +679,11 @@ async function preflightMissingWorkspacePath(command, fs, cwd = workspaceMountPo
|
|
|
665
679
|
return;
|
|
666
680
|
}
|
|
667
681
|
}
|
|
668
|
-
async function* walkWorkspaceInspectionCommand(
|
|
682
|
+
async function* walkWorkspaceInspectionCommand(segments, fs, cwd, signal) {
|
|
669
683
|
let currentCwd = cwd;
|
|
670
684
|
let skipAndChain = false;
|
|
671
685
|
let skipOrChain = false;
|
|
672
|
-
for (const segment of
|
|
686
|
+
for (const segment of segments) {
|
|
673
687
|
throwIfAborted(signal);
|
|
674
688
|
if (skipAndChain) {
|
|
675
689
|
skipAndChain = segment.separatorAfter === "&&";
|
|
@@ -758,11 +772,11 @@ function isBroadWorkspacePath(path, broadSearchPaths = [], cwd = workspaceMountP
|
|
|
758
772
|
const normalized = cleanWorkspaceShellPath(resolvedWorkspaceCwd(cwd, path)) || ".";
|
|
759
773
|
return normalized === "." || broadSearchPaths.includes(normalized);
|
|
760
774
|
}
|
|
761
|
-
function searchNoMatchFeedback(
|
|
775
|
+
function searchNoMatchFeedback(segments, result, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
762
776
|
if (result.exitCode !== 1 || result.stderr || result.stdout) return "";
|
|
763
777
|
try {
|
|
764
778
|
let currentCwd = cwd;
|
|
765
|
-
for (const segment of
|
|
779
|
+
for (const segment of segments) {
|
|
766
780
|
const words = segment.words;
|
|
767
781
|
if (words[0] === "cd") {
|
|
768
782
|
const path = words[1] || "/workspace";
|
|
@@ -787,10 +801,10 @@ function searchNoMatchFeedback(command, result, broadSearchPaths = [], cwd = wor
|
|
|
787
801
|
}
|
|
788
802
|
return "";
|
|
789
803
|
}
|
|
790
|
-
function preflightUnsupportedWorkspaceCommand(command, commands,
|
|
791
|
-
const syntax = preflightUnsupportedWorkspaceSyntax(command, commands,
|
|
804
|
+
function preflightUnsupportedWorkspaceCommand(command, segments, commands, strictDriver = false) {
|
|
805
|
+
const syntax = preflightUnsupportedWorkspaceSyntax(command, segments, commands, strictDriver);
|
|
792
806
|
if (syntax) return syntax;
|
|
793
|
-
if (!
|
|
807
|
+
if (!strictDriver && (usesShellSubstitution(command) || usesCompoundShellSyntax(segments))) return void 0;
|
|
794
808
|
if (!commands) return void 0;
|
|
795
809
|
const allowed = /* @__PURE__ */ new Set([
|
|
796
810
|
...commands,
|
|
@@ -798,19 +812,20 @@ function preflightUnsupportedWorkspaceCommand(command, commands, strictProvider
|
|
|
798
812
|
"false",
|
|
799
813
|
"true"
|
|
800
814
|
]);
|
|
801
|
-
for (const segment of
|
|
815
|
+
for (const segment of segments) {
|
|
802
816
|
const executable = workspaceExecutable(segment.words);
|
|
803
817
|
if (!executable || allowed.has(executable.name)) continue;
|
|
804
818
|
return unsupportedWorkspaceCommandFeedback(command, executable.name, commands);
|
|
805
819
|
}
|
|
806
820
|
}
|
|
807
|
-
function preflightUnsupportedWorkspaceSyntax(command, commands,
|
|
821
|
+
function preflightUnsupportedWorkspaceSyntax(command, segments, commands, strictDriver = false) {
|
|
822
|
+
if (segments.some((segment) => segment.invalid)) return unsupportedWorkspaceCommandFeedback(command, "invalid shell syntax", commands);
|
|
808
823
|
if (usesShellSubstitution(command)) {
|
|
809
|
-
if (
|
|
824
|
+
if (strictDriver) return unsupportedWorkspaceCommandFeedback(command, "shell substitution", commands);
|
|
810
825
|
return;
|
|
811
826
|
}
|
|
812
|
-
if (usesCompoundShellSyntax(
|
|
813
|
-
if (
|
|
827
|
+
if (usesCompoundShellSyntax(segments)) {
|
|
828
|
+
if (strictDriver) return unsupportedWorkspaceCommandFeedback(command, "compound shell syntax", commands);
|
|
814
829
|
return;
|
|
815
830
|
}
|
|
816
831
|
}
|
|
@@ -869,8 +884,8 @@ function workspaceExecutable(words) {
|
|
|
869
884
|
};
|
|
870
885
|
}
|
|
871
886
|
}
|
|
872
|
-
function usesCompoundShellSyntax(
|
|
873
|
-
return
|
|
887
|
+
function usesCompoundShellSyntax(segments) {
|
|
888
|
+
return segments.some((segment) => isCompoundShellSyntaxWord(segment.words[0] || ""));
|
|
874
889
|
}
|
|
875
890
|
function isCompoundShellSyntaxWord(word) {
|
|
876
891
|
return word === "{" || word.startsWith("(") || /^[A-Za-z_][A-Za-z0-9_]*\(\)$/.test(word) || shellSyntaxWords.has(word);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/shell",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Unified shell runtimes for ViteHub.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bash",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"./package.json": "./package.json"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@vite-hub/runtime": "0.1
|
|
48
|
+
"@vite-hub/runtime": "0.3.1",
|
|
49
49
|
"just-bash": "^3.6.0",
|
|
50
|
-
"nostics": "1.
|
|
51
|
-
"sh-syntax": "0.
|
|
50
|
+
"nostics": "1.4.0",
|
|
51
|
+
"sh-syntax": "0.8.3"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/node": "^24.19.1",
|
|
@@ -58,12 +58,12 @@
|
|
|
58
58
|
"vitest": "^4.1.11"
|
|
59
59
|
},
|
|
60
60
|
"engines": {
|
|
61
|
-
"node": ">=
|
|
61
|
+
"node": ">=22.18.0"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "vp pack",
|
|
65
65
|
"dev": "vp pack --watch",
|
|
66
66
|
"typecheck": "vp run --cache -t @vite-hub/shell#build && node ../../test/package-typecheck.mjs --noEmit -p ./tsconfig.json",
|
|
67
|
-
"test": "vp run -t @vite-hub/shell#build && vp test"
|
|
67
|
+
"test": "vp run --cache -t @vite-hub/shell#build && vp test"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/dist/analyze-BXwz20IH.js
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import { defineDiagnostics } from "nostics";
|
|
2
|
-
//#region src/error-diagnostics.ts
|
|
3
|
-
const dynamicError = { why: ({ message }) => message === void 0 ? "" : String(message) };
|
|
4
|
-
const shellErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
5
|
-
docsBase: () => "https://vitehub.dev/docs/reference/diagnostics",
|
|
6
|
-
codes: {
|
|
7
|
-
SHELL_R0001: dynamicError,
|
|
8
|
-
SHELL_R0002: dynamicError,
|
|
9
|
-
SHELL_R0003: dynamicError,
|
|
10
|
-
SHELL_R0004: dynamicError,
|
|
11
|
-
SHELL_R0005: dynamicError,
|
|
12
|
-
SHELL_R0006: dynamicError,
|
|
13
|
-
SHELL_R0007: dynamicError,
|
|
14
|
-
SHELL_R0008: dynamicError,
|
|
15
|
-
SHELL_R0009: dynamicError,
|
|
16
|
-
SHELL_R0010: dynamicError,
|
|
17
|
-
SHELL_R0011: dynamicError,
|
|
18
|
-
SHELL_R0012: dynamicError,
|
|
19
|
-
SHELL_R0013: dynamicError,
|
|
20
|
-
SHELL_R0014: dynamicError,
|
|
21
|
-
SHELL_R0015: dynamicError,
|
|
22
|
-
SHELL_R0016: dynamicError,
|
|
23
|
-
SHELL_R0017: dynamicError,
|
|
24
|
-
SHELL_R0018: dynamicError,
|
|
25
|
-
SHELL_R0019: dynamicError,
|
|
26
|
-
SHELL_R0020: dynamicError,
|
|
27
|
-
SHELL_R0021: dynamicError,
|
|
28
|
-
SHELL_R0022: dynamicError,
|
|
29
|
-
SHELL_R0023: dynamicError
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
//#endregion
|
|
33
|
-
//#region src/command/parse.ts
|
|
34
|
-
function parseShellCommand(command, mode = "execution") {
|
|
35
|
-
const words = [];
|
|
36
|
-
let current = "";
|
|
37
|
-
let wordStarted = false;
|
|
38
|
-
let quote;
|
|
39
|
-
let escaped = false;
|
|
40
|
-
for (let index = 0; index < command.length; index += 1) {
|
|
41
|
-
const char = command[index];
|
|
42
|
-
if (escaped) {
|
|
43
|
-
if (char !== "\n") {
|
|
44
|
-
current += char;
|
|
45
|
-
wordStarted = true;
|
|
46
|
-
}
|
|
47
|
-
escaped = false;
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
if (quote) {
|
|
51
|
-
if (char === quote) quote = void 0;
|
|
52
|
-
else if (char === "\\" && quote === "\"" && /[\\$`"\n]/.test(command[index + 1] ?? "")) escaped = true;
|
|
53
|
-
else current += char;
|
|
54
|
-
wordStarted = true;
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (char === "\\") {
|
|
58
|
-
escaped = true;
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
if (char === "'" || char === "\"") {
|
|
62
|
-
quote = char;
|
|
63
|
-
wordStarted = true;
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
if (/\s/.test(char)) {
|
|
67
|
-
if (wordStarted) {
|
|
68
|
-
words.push(current);
|
|
69
|
-
current = "";
|
|
70
|
-
wordStarted = false;
|
|
71
|
-
}
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
current += char;
|
|
75
|
-
wordStarted = true;
|
|
76
|
-
}
|
|
77
|
-
if (mode === "execution") {
|
|
78
|
-
if (escaped) {
|
|
79
|
-
current += "\\";
|
|
80
|
-
wordStarted = true;
|
|
81
|
-
}
|
|
82
|
-
if (quote) throw shellErrorDiagnostics.SHELL_R0003({ message: "unterminated quote" });
|
|
83
|
-
}
|
|
84
|
-
if (wordStarted) words.push(current);
|
|
85
|
-
return words;
|
|
86
|
-
}
|
|
87
|
-
function splitShellCommandSegments(command) {
|
|
88
|
-
const segments = [];
|
|
89
|
-
let current = "";
|
|
90
|
-
let quote;
|
|
91
|
-
let ansiCQuoted = false;
|
|
92
|
-
let escaped = false;
|
|
93
|
-
let comment = false;
|
|
94
|
-
let wordBoundary = true;
|
|
95
|
-
let followsPipe = false;
|
|
96
|
-
for (let index = 0; index < command.length; index++) {
|
|
97
|
-
const char = command[index];
|
|
98
|
-
if (comment) {
|
|
99
|
-
if (char !== "\n") continue;
|
|
100
|
-
comment = false;
|
|
101
|
-
}
|
|
102
|
-
if (escaped) {
|
|
103
|
-
if (char === "\n" && !ansiCQuoted) {
|
|
104
|
-
current = current.slice(0, -1);
|
|
105
|
-
escaped = false;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
current += char;
|
|
109
|
-
wordBoundary = false;
|
|
110
|
-
escaped = false;
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
if (char === "\\" && (quote !== "'" || ansiCQuoted)) {
|
|
114
|
-
current += char;
|
|
115
|
-
escaped = true;
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
if (quote) {
|
|
119
|
-
if (char === quote) {
|
|
120
|
-
quote = void 0;
|
|
121
|
-
ansiCQuoted = false;
|
|
122
|
-
}
|
|
123
|
-
current += char;
|
|
124
|
-
wordBoundary = false;
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (char === "$" && command[index + 1] === "'") {
|
|
128
|
-
quote = "'";
|
|
129
|
-
ansiCQuoted = true;
|
|
130
|
-
current += "$'";
|
|
131
|
-
wordBoundary = false;
|
|
132
|
-
index += 1;
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
if (char === "'" || char === "\"") {
|
|
136
|
-
quote = char;
|
|
137
|
-
current += char;
|
|
138
|
-
wordBoundary = false;
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (char === "#" && wordBoundary) {
|
|
142
|
-
comment = true;
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
const next = command[index + 1];
|
|
146
|
-
if (char === "&" && next === "&") {
|
|
147
|
-
segments.push({
|
|
148
|
-
command: current,
|
|
149
|
-
followsPipe,
|
|
150
|
-
separatorAfter: "&&"
|
|
151
|
-
});
|
|
152
|
-
current = "";
|
|
153
|
-
followsPipe = false;
|
|
154
|
-
wordBoundary = true;
|
|
155
|
-
index += 1;
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
if (char === "|" && next === "|") {
|
|
159
|
-
segments.push({
|
|
160
|
-
command: current,
|
|
161
|
-
followsPipe,
|
|
162
|
-
separatorAfter: "||"
|
|
163
|
-
});
|
|
164
|
-
current = "";
|
|
165
|
-
followsPipe = false;
|
|
166
|
-
wordBoundary = true;
|
|
167
|
-
index += 1;
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (char === "|" || char === ";" || char === "\n") {
|
|
171
|
-
segments.push({
|
|
172
|
-
command: current,
|
|
173
|
-
followsPipe,
|
|
174
|
-
separatorAfter: char
|
|
175
|
-
});
|
|
176
|
-
current = "";
|
|
177
|
-
followsPipe = char === "|";
|
|
178
|
-
wordBoundary = true;
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
current += char;
|
|
182
|
-
wordBoundary = /[\s()<>]/.test(char) || char === "&";
|
|
183
|
-
}
|
|
184
|
-
segments.push({
|
|
185
|
-
command: current,
|
|
186
|
-
followsPipe
|
|
187
|
-
});
|
|
188
|
-
return segments;
|
|
189
|
-
}
|
|
190
|
-
//#endregion
|
|
191
|
-
//#region src/command/analyze.ts
|
|
192
|
-
const defaultMaxInputBytes = 65536;
|
|
193
|
-
const defaultTimeoutMs = 100;
|
|
194
|
-
async function analyzeShellCommand(command, options = {}) {
|
|
195
|
-
const maxInputBytes = options.maxInputBytes ?? defaultMaxInputBytes;
|
|
196
|
-
if (new TextEncoder().encode(command).byteLength > maxInputBytes) return {
|
|
197
|
-
error: `Shell command exceeds ${maxInputBytes} bytes.`,
|
|
198
|
-
ok: false,
|
|
199
|
-
parser: "sh-syntax"
|
|
200
|
-
};
|
|
201
|
-
const segments = splitShellCommandSegments(command);
|
|
202
|
-
const commands = [...new Set(segments.map((segment) => firstCommandWord(segment.command)).filter((name) => name !== void 0))];
|
|
203
|
-
try {
|
|
204
|
-
await withTimeout(parseWithShSyntax(command), options.timeoutMs ?? defaultTimeoutMs);
|
|
205
|
-
} catch (error) {
|
|
206
|
-
return {
|
|
207
|
-
commands,
|
|
208
|
-
error: error instanceof Error ? error.message : String(error),
|
|
209
|
-
ok: false,
|
|
210
|
-
parser: "sh-syntax"
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
return {
|
|
214
|
-
commands,
|
|
215
|
-
hasCommandSubstitution: /(?:\$\(|`)/.test(command),
|
|
216
|
-
hasHeredocs: /<<-?/.test(command),
|
|
217
|
-
hasPipelines: segments.some((segment) => segment.separatorAfter === "|" || segment.separatorAfter === "||"),
|
|
218
|
-
hasRedirects: /(?:^|[^<])(?:>>?|<)/.test(command),
|
|
219
|
-
ok: true,
|
|
220
|
-
parser: "sh-syntax"
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
async function parseWithShSyntax(command) {
|
|
224
|
-
const { parse, LangVariant } = await import("sh-syntax");
|
|
225
|
-
await parse(command, { variant: LangVariant.LangBash });
|
|
226
|
-
}
|
|
227
|
-
async function withTimeout(promise, timeoutMs) {
|
|
228
|
-
if (timeoutMs <= 0) throw shellErrorDiagnostics.SHELL_R0001({ message: `Shell analysis timed out after ${timeoutMs}ms.` });
|
|
229
|
-
let timeout;
|
|
230
|
-
try {
|
|
231
|
-
return await Promise.race([promise, new Promise((_, reject) => {
|
|
232
|
-
timeout = setTimeout(() => reject(shellErrorDiagnostics.SHELL_R0002({ message: `Shell analysis timed out after ${timeoutMs}ms.` })), timeoutMs);
|
|
233
|
-
})]);
|
|
234
|
-
} finally {
|
|
235
|
-
if (timeout) clearTimeout(timeout);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
function firstCommandWord(segment) {
|
|
239
|
-
const words = segment.trim().match(/[^\s]+/g) || [];
|
|
240
|
-
for (const word of words) {
|
|
241
|
-
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) continue;
|
|
242
|
-
if (/^\d*(?:>>?|<<?-?|<&|>&)/.test(word)) continue;
|
|
243
|
-
return word.replace(/^command$/, "") || void 0;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
//#endregion
|
|
247
|
-
export { shellErrorDiagnostics as i, parseShellCommand as n, splitShellCommandSegments as r, analyzeShellCommand as t };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { o as ShellExecutionProvider } from "./types-2uHfTVHi.js";
|
|
2
|
-
import { IFileSystem } from "just-bash";
|
|
3
|
-
//#region src/providers/just-bash.d.ts
|
|
4
|
-
interface JustBashFileSystem extends IFileSystem {
|
|
5
|
-
readonly writeFs: boolean;
|
|
6
|
-
}
|
|
7
|
-
interface JustBashProviderOptions {
|
|
8
|
-
commands?: string[];
|
|
9
|
-
cwd?: string;
|
|
10
|
-
fs: JustBashFileSystem;
|
|
11
|
-
networkGrants?: ShellNetworkGrantExecutor;
|
|
12
|
-
}
|
|
13
|
-
interface ShellNetworkGrantExecutor {
|
|
14
|
-
executeSourceRequest(input: ShellNetworkRequest): Promise<ShellNetworkRequestResult>;
|
|
15
|
-
}
|
|
16
|
-
interface ShellNetworkRequest {
|
|
17
|
-
body?: unknown;
|
|
18
|
-
method: "GET" | "HEAD" | "POST";
|
|
19
|
-
signal?: AbortSignal;
|
|
20
|
-
url: string;
|
|
21
|
-
}
|
|
22
|
-
interface ShellNetworkRequestResult {
|
|
23
|
-
content: string | Uint8Array;
|
|
24
|
-
}
|
|
25
|
-
declare function createJustBashProvider(options: JustBashProviderOptions): ShellExecutionProvider;
|
|
26
|
-
//#endregion
|
|
27
|
-
export { ShellNetworkRequestResult as a, ShellNetworkRequest as i, JustBashProviderOptions as n, createJustBashProvider as o, ShellNetworkGrantExecutor as r, JustBashFileSystem as t };
|