@vite-hub/shell 0.0.4 → 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.
@@ -1,61 +1,92 @@
1
- import { t as analyzeShellCommand } from "../analyze-Bu5Ueqph.js";
2
- import { t as parseShellCommand } from "../parse-D9VT4P6N.js";
1
+ import { r as shellErrorDiagnostics, t as analyzeShellCommand } from "../analyze-kSc2pmy2.js";
3
2
  //#region src/providers/just-bash.ts
4
- function createJustBashProvider(options) {
3
+ function createJustBashDriver(options) {
4
+ const commands = options.commands?.slice();
5
+ const networkGrants = options.networkGrants;
6
+ const executionLimits = options.executionLimits && { ...options.executionLimits };
7
+ const executionLimitProfile = options.executionLimitProfile;
8
+ const boundary = {
9
+ cwd: true,
10
+ env: true,
11
+ filesystem: {
12
+ mountPoint: "/workspace",
13
+ writable: options.fs.writeFs
14
+ },
15
+ network: Boolean(networkGrants) && (!commands || commands.includes("curl")),
16
+ processes: {
17
+ background: false,
18
+ interactive: false
19
+ },
20
+ streaming: false,
21
+ timeout: {
22
+ enforcedBy: "provider",
23
+ supported: true
24
+ },
25
+ execution: {
26
+ isolation: "in_process",
27
+ model: "virtual"
28
+ },
29
+ resources: { output: {
30
+ enforcedBy: "unsupported",
31
+ unit: "characters"
32
+ } }
33
+ };
5
34
  return {
6
35
  analyze: analyzeShellCommand,
7
- boundary: {
8
- cwd: true,
9
- env: true,
10
- filesystem: {
11
- mountPoint: "/workspace",
12
- writable: options.fs.writeFs
13
- },
14
- network: Boolean(options.networkGrants),
15
- processes: {
16
- background: false,
17
- interactive: false
18
- },
19
- streaming: false,
20
- timeout: {
21
- enforcedBy: "provider",
22
- supported: true
23
- }
24
- },
36
+ boundary,
25
37
  async exec(command, execOptions = {}) {
26
- const result = await withProviderTimeout(command, execOptions, async () => {
27
- const curlResult = await runControlledCurlCommand(command, {
28
- cwd: execOptions.cwd || options.cwd,
29
- networkGrants: options.networkGrants
38
+ const cwd = execOptions.cwd ?? options.cwd ?? "/workspace";
39
+ const result = await withDriverTimeout(command, {
40
+ ...execOptions,
41
+ cwd
42
+ }, async (runOptions) => {
43
+ const { Bash, defineCommand } = await import("just-bash/browser");
44
+ let policyDenied = false;
45
+ const curl = defineCommand("curl", async (args, ctx) => {
46
+ const curlResult = await runControlledCurl(args, {
47
+ commands,
48
+ networkGrants,
49
+ signal: ctx.signal ?? runOptions.signal
50
+ });
51
+ policyDenied ||= curlResult.policyDenied;
52
+ return curlResult;
30
53
  });
31
- if (curlResult) return curlResult;
32
- const { Bash } = await import("just-bash/browser");
33
- const bash = new Bash({
34
- commands: options.commands,
35
- cwd: options.cwd,
54
+ return await new Bash({
55
+ commands,
56
+ customCommands: [curl],
57
+ cwd,
58
+ executionLimitProfile,
59
+ executionLimits,
36
60
  fs: options.fs
37
- });
38
- const signal = typeof execOptions.timeout === "number" ? AbortSignal.timeout(execOptions.timeout) : void 0;
39
- return await bash.exec(command, {
40
- cwd: execOptions.cwd,
61
+ }).exec(command, {
62
+ cwd,
41
63
  env: execOptions.env,
42
- signal
43
- }).then((result) => ({
44
- command,
45
- cwd: execOptions.cwd,
46
- event: "command_finished",
47
- exitCode: result.exitCode,
48
- stderr: result.stderr,
49
- stdout: result.stdout
50
- }));
64
+ signal: runOptions.signal,
65
+ stdin: execOptions.stdin
66
+ }).then((result) => {
67
+ const stdout = boundOutput(result.stdout, runOptions.maxOutputLength);
68
+ const stderr = boundOutput(result.stderr, runOptions.maxOutputLength);
69
+ return {
70
+ command,
71
+ cwd,
72
+ event: policyDenied ? "policy_denied" : "command_finished",
73
+ exitCode: result.exitCode,
74
+ maxOutputLength: runOptions.maxOutputLength,
75
+ outputTruncated: stdout.truncated || stderr.truncated,
76
+ stderr: stderr.content,
77
+ stdout: stdout.content
78
+ };
79
+ });
51
80
  });
52
81
  execOptions.onStdout?.(result.stdout);
53
82
  execOptions.onStderr?.(result.stderr);
54
83
  return {
55
84
  command: result.command ?? command,
56
- cwd: result.cwd ?? execOptions.cwd,
85
+ cwd: result.cwd ?? cwd,
57
86
  event: result.timedOut ? "command_timed_out" : result.event,
58
87
  exitCode: result.exitCode,
88
+ maxOutputLength: result.maxOutputLength,
89
+ outputTruncated: result.outputTruncated,
59
90
  stderr: result.stderr,
60
91
  stdout: result.stdout,
61
92
  timedOut: result.timedOut
@@ -63,167 +94,126 @@ function createJustBashProvider(options) {
63
94
  }
64
95
  };
65
96
  }
66
- async function runControlledCurlCommand(command, options) {
67
- if (!mentionsCurlCommand(command)) return void 0;
68
- const [parseError, parsed] = parseControlledCurlCommand(command);
69
- if (parseError) return policyDeniedCurl(command, options.cwd, parseError.message);
70
- if (!options.networkGrants) return policyDeniedCurl(command, options.cwd, "No API-backed Source request descriptors are visible in this workspace.");
97
+ async function runControlledCurl(args, options) {
98
+ options.signal?.throwIfAborted();
99
+ if (options.commands && !options.commands.includes("curl")) return policyDeniedCurl("curl is not in the permitted commands for this shell.");
100
+ const [parseError, parsed] = parseControlledCurlArgs(args);
101
+ if (parseError) return policyDeniedCurl(parseError.message);
102
+ if (!options.networkGrants) return policyDeniedCurl("No API-backed Source request descriptors are visible in this workspace.");
71
103
  try {
72
- const result = await options.networkGrants.executeSourceRequest({
104
+ const request = {
73
105
  body: parsed.body,
74
106
  method: parsed.method,
107
+ signal: options.signal,
75
108
  url: parsed.url
109
+ };
110
+ if (options.signal) Object.defineProperty(request, "signal", {
111
+ configurable: true,
112
+ enumerable: false,
113
+ value: options.signal
76
114
  });
115
+ const result = await options.networkGrants.executeSourceRequest(request);
77
116
  return {
78
- command,
79
- cwd: options.cwd,
80
- event: "command_finished",
81
117
  exitCode: 0,
118
+ policyDenied: false,
82
119
  stderr: "",
83
120
  stdout: typeof result.content === "string" ? result.content : new TextDecoder().decode(result.content)
84
121
  };
85
122
  } catch (error) {
123
+ if (options.signal?.aborted) throw options.signal.reason;
86
124
  const message = error instanceof Error ? error.message : String(error);
87
125
  const httpFailure = message.includes("HTTP request failed");
88
126
  return {
89
- command,
90
- cwd: options.cwd,
91
- event: httpFailure ? "command_finished" : "policy_denied",
92
127
  exitCode: httpFailure ? 22 : 126,
128
+ policyDenied: !httpFailure,
93
129
  stderr: `${message}\n`,
94
130
  stdout: ""
95
131
  };
96
132
  }
97
133
  }
98
134
  function parseFailure(message) {
99
- return [new Error(message), void 0];
135
+ return [shellErrorDiagnostics.SHELL_R0004({ message }), void 0];
100
136
  }
101
- function parseControlledCurlCommand(command) {
102
- if (hasUnsupportedCurlShellSyntax(command)) return parseFailure("Controlled curl must be a single command without pipes, redirects, chaining, command substitution, or heredocs.");
103
- let words;
104
- try {
105
- words = parseShellCommand(command);
106
- } catch (error) {
107
- return [error instanceof Error ? error : new Error(String(error)), void 0];
108
- }
109
- if (words[0] !== "curl") return parseFailure("Controlled curl command must start with curl.");
137
+ function parseControlledCurlArgs(words) {
110
138
  let body;
111
139
  let method;
112
140
  let url;
113
- try {
114
- for (let index = 1; index < words.length; index += 1) {
115
- const word = words[index];
116
- if (word === "-s" || word === "-S" || word === "-sS" || word === "--silent" || word === "--show-error" || word === "-L" || word === "--location") continue;
117
- if (word === "-X" || word === "--request") {
118
- const value = words[++index];
119
- if (!value) return parseFailure(`${word} requires a method.`);
120
- method = parseCurlMethod(value);
121
- if (!method) return parseFailure(`Unsupported curl request method: ${value}.`);
122
- continue;
123
- }
124
- if (word.startsWith("--request=")) {
125
- method = parseCurlMethod(word.slice(10));
126
- if (!method) return parseFailure(`Unsupported curl request method: ${word.slice(10)}.`);
127
- continue;
128
- }
129
- if (word === "--url") {
130
- const value = words[++index];
131
- if (!value) return parseFailure("--url requires a URL.");
132
- url = setCurlUrl(url, value);
133
- continue;
134
- }
135
- if (word.startsWith("--url=")) {
136
- url = setCurlUrl(url, word.slice(6));
137
- continue;
138
- }
139
- if (word === "--json") {
140
- const value = words[++index];
141
- if (!value) return parseFailure("--json requires a JSON body.");
142
- const [error, parsedBody] = parseJsonCurlBody(value);
143
- if (error) return [error, void 0];
144
- body = parsedBody;
145
- method ??= "POST";
146
- continue;
147
- }
148
- if (word.startsWith("--json=")) {
149
- const [error, parsedBody] = parseJsonCurlBody(word.slice(7));
150
- if (error) return [error, void 0];
151
- body = parsedBody;
152
- method ??= "POST";
153
- continue;
154
- }
155
- if (word === "-d" || word === "--data" || word === "--data-raw" || word === "--data-binary") {
156
- const value = words[++index];
157
- if (!value) return parseFailure(`${word} requires a body.`);
158
- body = parseCurlDataBody(value);
159
- method ??= "POST";
160
- continue;
161
- }
162
- if (word.startsWith("-d") && word !== "-d") {
163
- body = parseCurlDataBody(word.slice(2));
164
- method ??= "POST";
165
- continue;
166
- }
167
- if (word.startsWith("--data=") || word.startsWith("--data-raw=") || word.startsWith("--data-binary=")) {
168
- body = parseCurlDataBody(word.slice(word.indexOf("=") + 1));
169
- method ??= "POST";
170
- continue;
171
- }
172
- if (word === "-H" || word === "--header" || word === "-b" || word === "--cookie" || word.startsWith("--header=") || word.startsWith("--cookie=")) return parseFailure("Controlled curl injects Source credentials itself; do not pass headers or cookies in the command.");
173
- if (word.startsWith("-")) return parseFailure(`Unsupported curl flag: ${word}.`);
174
- url = setCurlUrl(url, word);
141
+ for (let index = 0; index < words.length; index += 1) {
142
+ const word = words[index];
143
+ if (word === "-s" || word === "-S" || word === "-sS" || word === "--silent" || word === "--show-error" || word === "-L" || word === "--location") continue;
144
+ if (word === "-X" || word === "--request") {
145
+ const value = words[++index];
146
+ if (!value) return parseFailure(`${word} requires a method.`);
147
+ method = parseCurlMethod(value);
148
+ if (!method) return parseFailure(`Unsupported curl request method: ${value}.`);
149
+ continue;
175
150
  }
176
- } catch (error) {
177
- return [error instanceof Error ? error : new Error(String(error)), void 0];
178
- }
179
- if (!url) return parseFailure("Controlled curl requires a URL.");
180
- try {
181
- url = new URL(url).toString();
182
- } catch {
183
- return parseFailure(`Controlled curl URL is invalid: ${url}.`);
184
- }
185
- return [null, {
186
- body,
187
- method: method ?? "GET",
188
- url
189
- }];
190
- }
191
- function mentionsCurlCommand(command) {
192
- return parseShellCommandLenient(command)[0] === "curl";
193
- }
194
- function hasUnsupportedCurlShellSyntax(command) {
195
- let quote;
196
- let escaped = false;
197
- for (let index = 0; index < command.length; index += 1) {
198
- const char = command[index];
199
- const next = command[index + 1];
200
- if (escaped) {
201
- escaped = false;
151
+ if (word.startsWith("--request=")) {
152
+ method = parseCurlMethod(word.slice(10));
153
+ if (!method) return parseFailure(`Unsupported curl request method: ${word.slice(10)}.`);
154
+ continue;
155
+ }
156
+ if (word === "--url") {
157
+ const value = words[++index];
158
+ if (!value) return parseFailure("--url requires a URL.");
159
+ if (url) return multipleCurlUrls();
160
+ url = value;
161
+ continue;
162
+ }
163
+ if (word.startsWith("--url=")) {
164
+ if (url) return multipleCurlUrls();
165
+ url = word.slice(6);
202
166
  continue;
203
167
  }
204
- if (char === "\\") {
205
- escaped = true;
168
+ if (word === "--json") {
169
+ const value = words[++index];
170
+ if (!value) return parseFailure("--json requires a JSON body.");
171
+ const [error, parsedBody] = parseJsonCurlBody(value);
172
+ if (error) return [error, void 0];
173
+ body = parsedBody;
174
+ method ??= "POST";
206
175
  continue;
207
176
  }
208
- if (quote) {
209
- if (char === quote) quote = void 0;
177
+ if (word.startsWith("--json=")) {
178
+ const [error, parsedBody] = parseJsonCurlBody(word.slice(7));
179
+ if (error) return [error, void 0];
180
+ body = parsedBody;
181
+ method ??= "POST";
210
182
  continue;
211
183
  }
212
- if (char === "'" || char === "\"") {
213
- quote = char;
184
+ if (word === "-d" || word === "--data" || word === "--data-raw" || word === "--data-binary") {
185
+ const value = words[++index];
186
+ if (value === void 0) return parseFailure(`${word} requires a body.`);
187
+ body = parseCurlDataBody(value);
188
+ method ??= "POST";
214
189
  continue;
215
190
  }
216
- if (char === "|" || char === ";" || char === "<" || char === ">" || char === "`" || char === "$" && next === "(") return true;
217
- if ((char === "&" || char === "|") && next === char) return true;
191
+ if (word.startsWith("-d") && word !== "-d") {
192
+ body = parseCurlDataBody(word.slice(2));
193
+ method ??= "POST";
194
+ continue;
195
+ }
196
+ if (word.startsWith("--data=") || word.startsWith("--data-raw=") || word.startsWith("--data-binary=")) {
197
+ body = parseCurlDataBody(word.slice(word.indexOf("=") + 1));
198
+ method ??= "POST";
199
+ continue;
200
+ }
201
+ if (word === "-H" || word === "--header" || word === "-b" || word === "--cookie" || word.startsWith("--header=") || word.startsWith("--cookie=")) return parseFailure("Controlled curl injects Source credentials itself; do not pass headers or cookies in the command.");
202
+ if (word.startsWith("-")) return parseFailure(`Unsupported curl flag: ${word}.`);
203
+ if (url) return multipleCurlUrls();
204
+ url = word;
218
205
  }
219
- return /<<-?/.test(command);
220
- }
221
- function parseShellCommandLenient(command) {
206
+ if (!url) return parseFailure("Controlled curl requires a URL.");
222
207
  try {
223
- return parseShellCommand(command);
208
+ url = new URL(url).toString();
224
209
  } catch {
225
- return command.trim().split(/\s+/).filter(Boolean);
210
+ return parseFailure(`Controlled curl URL is invalid: ${url}.`);
226
211
  }
212
+ return [null, {
213
+ body,
214
+ method: method ?? "GET",
215
+ url
216
+ }];
227
217
  }
228
218
  function parseCurlDataBody(value) {
229
219
  const [error, body] = parseJsonCurlBody(value);
@@ -240,38 +230,72 @@ function parseJsonCurlBody(value) {
240
230
  return parseFailure("--json body must be valid JSON.");
241
231
  }
242
232
  }
243
- function setCurlUrl(current, next) {
244
- if (current) throw new Error("Controlled curl accepts exactly one URL.");
245
- return next;
233
+ function multipleCurlUrls() {
234
+ return [shellErrorDiagnostics.SHELL_R0007({ message: "Controlled curl accepts exactly one URL." }), void 0];
246
235
  }
247
- function policyDeniedCurl(command, cwd, message) {
236
+ function policyDeniedCurl(message) {
248
237
  return {
249
- command,
250
- cwd,
251
- event: "policy_denied",
252
238
  exitCode: 126,
239
+ policyDenied: true,
253
240
  stderr: `[vitehub] ${message}\n`,
254
- stdout: ["[vitehub] Controlled curl request was not run.", "Expected a single curl command matching a visible Source Request Descriptor at `.vitehub/sources/<sourceKey>.json`."].join("\n") + "\n"
241
+ stdout: ["[vitehub] Controlled curl request was not run.", "Expected a curl command matching a visible Source Request Descriptor at `.vitehub/sources/<sourceKey>.json`."].join("\n") + "\n"
255
242
  };
256
243
  }
257
- async function withProviderTimeout(command, options, run) {
258
- if (typeof options.timeout !== "number") return await run();
244
+ async function withDriverTimeout(command, options, run) {
245
+ options.signal?.throwIfAborted();
246
+ const controller = new AbortController();
247
+ const abortListener = options.signal ? () => controller.abort(options.signal?.reason) : void 0;
248
+ if (abortListener) options.signal?.addEventListener("abort", abortListener, { once: true });
249
+ const runOptions = {
250
+ ...options,
251
+ signal: controller.signal
252
+ };
259
253
  let timeout;
254
+ let onAbort;
255
+ const aborted = options.signal ? new Promise((_resolve, reject) => {
256
+ const handleAbort = () => reject(options.signal?.reason);
257
+ onAbort = handleAbort;
258
+ options.signal?.addEventListener("abort", handleAbort, { once: true });
259
+ if (options.signal?.aborted) handleAbort();
260
+ }) : void 0;
260
261
  try {
261
- return await Promise.race([run(), new Promise((resolve) => {
262
- timeout = setTimeout(() => resolve({
263
- command,
264
- cwd: options.cwd,
265
- event: "command_timed_out",
266
- exitCode: null,
267
- stderr: `[vitehub] Workspace shell command timed out after ${options.timeout}ms.`,
268
- stdout: "",
269
- timedOut: true
270
- }), options.timeout);
271
- })]);
262
+ const running = run(runOptions);
263
+ if (typeof options.timeout !== "number" && !aborted) return await running;
264
+ const races = [running];
265
+ if (aborted) races.push(aborted);
266
+ if (typeof options.timeout === "number") races.push(new Promise((resolve) => {
267
+ timeout = setTimeout(() => {
268
+ resolve({
269
+ command,
270
+ cwd: options.cwd,
271
+ event: "command_timed_out",
272
+ exitCode: null,
273
+ maxOutputLength: options.maxOutputLength,
274
+ outputTruncated: false,
275
+ stderr: `[vitehub] Workspace shell command timed out after ${options.timeout}ms.`,
276
+ stdout: "",
277
+ timedOut: true
278
+ });
279
+ controller.abort(/* @__PURE__ */ new Error("Shell command timed out."));
280
+ }, options.timeout);
281
+ }));
282
+ return await Promise.race(races);
272
283
  } finally {
273
284
  if (timeout) clearTimeout(timeout);
285
+ if (onAbort) options.signal?.removeEventListener("abort", onAbort);
286
+ if (abortListener) options.signal?.removeEventListener("abort", abortListener);
274
287
  }
275
288
  }
289
+ function boundOutput(content, maxLength) {
290
+ const limit = maxLength === void 0 ? void 0 : maxLength === Number.POSITIVE_INFINITY ? void 0 : Number.isFinite(maxLength) ? Math.max(0, Math.floor(maxLength)) : 0;
291
+ if (limit === void 0 || content.length <= limit) return {
292
+ content,
293
+ truncated: false
294
+ };
295
+ return {
296
+ content: `${content.slice(0, limit)}\n[output truncated to ${limit} characters]\n`,
297
+ truncated: true
298
+ };
299
+ }
276
300
  //#endregion
277
- export { createJustBashProvider };
301
+ export { createJustBashDriver };