@vite-hub/shell 0.3.1 → 0.5.0

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 CHANGED
@@ -1,9 +1,9 @@
1
1
  # @vite-hub/shell
2
2
 
3
3
  <p>
4
- <a href="https://vitehub.dev"><img alt="ViteHub" src="https://comark-ui.onmax.me/badge.svg?label=ViteHub&amp;value=vitehub.dev"></a>
5
- <img alt="TypeScript" src="https://comark-ui.onmax.me/badge.svg?label=TypeScript&amp;value=ready">
6
- <img alt="Shell" src="https://comark-ui.onmax.me/badge.svg?label=Shell&amp;value=controlled+runtime">
4
+ <a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/static/v1?label=ViteHub&amp;message=vitehub.dev&amp;color=blue"></a>
5
+ <img alt="TypeScript" src="https://img.shields.io/static/v1?label=TypeScript&amp;message=ready&amp;color=blue">
6
+ <img alt="Shell" src="https://img.shields.io/static/v1?label=Shell&amp;message=controlled+runtime&amp;color=blue">
7
7
  </p>
8
8
 
9
9
  `@vite-hub/shell` gives server code structured command analysis and execution with explicit filesystem, network, process, and timeout boundaries.
@@ -1,4 +1,4 @@
1
- import { BinCmdOperator, LangVariant, RedirOperator, walk } from "sh-syntax";
1
+ import { BinCmdOperator, LangVariant, RedirOperator, createSourceIndex, walk } from "sh-syntax";
2
2
  import { defineDiagnostics } from "nostics";
3
3
  //#region src/error-diagnostics.ts
4
4
  const dynamicError = { why: ({ message }) => message === void 0 ? "" : String(message) };
@@ -93,6 +93,10 @@ const shellErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
93
93
  ...dynamicError,
94
94
  fix: "Make the driver return its process handle within the Shell timeout, or raise the Shell policy timeout if longer startup is expected."
95
95
  },
96
+ SHELL_PATH_NOT_FOUND: {
97
+ ...dynamicError,
98
+ fix: "Create the path first, or correct the path. The command found no file or directory there."
99
+ },
96
100
  SHELL_SH_SYNTAX_WASM_UNAVAILABLE: {
97
101
  why: "[vitehub] sh-syntax cannot load its WebAssembly parser on this host.",
98
102
  fix: "Build the server with `vitehub()`, which bundles the sh-syntax WebAssembly parser, or run it on Node, where sh-syntax reads `main.wasm` from its own package."
@@ -111,22 +115,23 @@ async function parseShell(text, options) {
111
115
  }
112
116
  async function loadParser() {
113
117
  const shSyntax = await import("sh-syntax");
114
- const wasm = await bundledWasmModule();
118
+ const { importError, module: wasm } = await bundledWasmModule();
115
119
  if (!wasm) {
116
- if (!shSyntax.parse) throw shellErrorDiagnostics.SHELL_SH_SYNTAX_WASM_UNAVAILABLE();
120
+ if (!shSyntax.parse) throw shellErrorDiagnostics.SHELL_SH_SYNTAX_WASM_UNAVAILABLE({ cause: importError });
117
121
  return shSyntax.parse;
118
122
  }
119
123
  const processor = shSyntax.getProcessor((imports) => WebAssembly.instantiate(wasm, imports));
120
124
  return (text, options) => processor(text, options);
121
125
  }
122
126
  async function bundledWasmModule() {
127
+ let wasm;
123
128
  try {
124
- const wasm = await (await import("sh-syntax/main.wasm?module")).default;
125
- if (wasm instanceof Uint8Array) return await WebAssembly.compile(wasm);
126
- return wasm instanceof WebAssembly.Module ? wasm : void 0;
127
- } catch {
128
- return;
129
+ wasm = await (await import("sh-syntax/main.wasm?module")).default;
130
+ } catch (importError) {
131
+ return { importError };
129
132
  }
133
+ if (wasm instanceof Uint8Array) return { module: await WebAssembly.compile(wasm) };
134
+ return { module: wasm instanceof WebAssembly.Module ? wasm : void 0 };
130
135
  }
131
136
  //#endregion
132
137
  //#region src/command/parse.ts
@@ -191,8 +196,7 @@ async function splitShellCommandSegments(command) {
191
196
  }];
192
197
  }
193
198
  const toIndex = createSourceIndex(command);
194
- const scanned = withoutHeredocBodies(file, command, toIndex);
195
- if (scanned === void 0 || stripLineContinuations(scanned).commentContinuation) return [{
199
+ if (hasUnterminatedHeredoc(file)) return [{
196
200
  command,
197
201
  followsPipe: false,
198
202
  invalid: true,
@@ -208,7 +212,7 @@ async function splitShellCommandSegments(command) {
208
212
  for (const { negated, stmt, separator } of leaves) {
209
213
  const trailingComment = stmt.comments.find((comment) => comment.start >= stmt.end);
210
214
  const rawEnd = separator?.offset ?? (trailingComment ? toIndex(trailingComment.start) : toIndex(stmt.end));
211
- const raw = stripLineContinuations(command.slice(cursor, Math.max(cursor, rawEnd))).text;
215
+ const raw = stripLineContinuations(command.slice(cursor, Math.max(cursor, rawEnd)));
212
216
  segments.push({
213
217
  command: raw,
214
218
  followsPipe,
@@ -220,31 +224,13 @@ async function splitShellCommandSegments(command) {
220
224
  }
221
225
  return segments;
222
226
  }
223
- function withoutHeredocBodies(file, command, toIndex) {
224
- let result = command;
227
+ function hasUnterminatedHeredoc(file) {
228
+ let unterminated = false;
225
229
  walk(file, (node) => {
226
- if (result === void 0) return false;
227
- if (node.type === "Redirect" && !node.hdoc && (node.op === RedirOperator.Hdoc || node.op === RedirOperator.DashHdoc)) {
228
- result = withoutTerminatorLine(result, node.word ? wordText(node.word, command, toIndex) : "", toIndex(node.end), node.op === RedirOperator.DashHdoc);
229
- return result !== void 0;
230
- }
231
- if (node.type === "Redirect" && node.hdoc) {
232
- const start = toIndex(node.hdoc.start);
233
- const end = toIndex(node.hdoc.end);
234
- result = result.slice(0, start) + " ".repeat(end - start) + result.slice(end);
235
- }
236
- return true;
230
+ if (node.type === "Redirect" && (node.op === RedirOperator.Hdoc || node.op === RedirOperator.DashHdoc) && node.closePos.offset === 0) unterminated = true;
231
+ return !unterminated;
237
232
  });
238
- return result;
239
- }
240
- function withoutTerminatorLine(source, delimiter, from, stripTabs) {
241
- let start = source.indexOf("\n", from) + 1;
242
- while (start > 0 && start < source.length) {
243
- const end = source.indexOf("\n", start) === -1 ? source.length : source.indexOf("\n", start);
244
- const line = source.slice(start, end);
245
- if ((stripTabs ? line.replace(/^\t+/, "") : line) === delimiter) return source.slice(0, start) + " ".repeat(end - start) + source.slice(end);
246
- start = end + 1;
247
- }
233
+ return unterminated;
248
234
  }
249
235
  function flattenStatement(stmt, outer, toIndex, leaves, negated = false) {
250
236
  if (stmt.cmd?.type === "BinaryCmd") {
@@ -299,26 +285,8 @@ function binaryOperator(op, offset) {
299
285
  default: return;
300
286
  }
301
287
  }
302
- function createSourceIndex(source) {
303
- const indexes = [0];
304
- let byteOffset = 0;
305
- for (let index = 0; index < source.length;) {
306
- const codePoint = source.codePointAt(index);
307
- byteOffset += utf8Length(codePoint);
308
- index += codePoint > 65535 ? 2 : 1;
309
- indexes[byteOffset] = index;
310
- }
311
- return (offset) => indexes[offset] ?? source.length;
312
- }
313
- function utf8Length(codePoint) {
314
- if (codePoint <= 127) return 1;
315
- if (codePoint <= 2047) return 2;
316
- if (codePoint <= 65535) return 3;
317
- return 4;
318
- }
319
288
  function stripLineContinuations(command) {
320
289
  let result = "";
321
- let commentContinuation = false;
322
290
  let quote;
323
291
  let ansiCQuoted = false;
324
292
  let escaped = false;
@@ -327,10 +295,7 @@ function stripLineContinuations(command) {
327
295
  const char = command[index];
328
296
  if (comment) {
329
297
  result += char;
330
- if (char === "\n") {
331
- comment = false;
332
- if (command[index - 1] === "\\") commentContinuation = true;
333
- }
298
+ if (char === "\n") comment = false;
334
299
  continue;
335
300
  }
336
301
  if (escaped) {
@@ -371,10 +336,7 @@ function stripLineContinuations(command) {
371
336
  if (char === "'" || char === "\"") quote = char;
372
337
  result += char;
373
338
  }
374
- return {
375
- commentContinuation,
376
- text: result
377
- };
339
+ return result;
378
340
  }
379
341
  //#endregion
380
342
  //#region src/command/analyze.ts
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { t as analyzeShellCommand } from "./analyze-kSc2pmy2.js";
2
- import { t as createShellRuntime } from "./session-DjjZUIPE.js";
1
+ import { t as analyzeShellCommand } from "./analyze-psY4ydry.js";
2
+ import { t as createShellRuntime } from "./session-DOtQ5C_n.js";
3
3
  export { analyzeShellCommand, createShellRuntime };
@@ -1,4 +1,4 @@
1
- import { r as shellErrorDiagnostics, t as analyzeShellCommand } from "../analyze-kSc2pmy2.js";
1
+ import { r as shellErrorDiagnostics, t as analyzeShellCommand } from "../analyze-psY4ydry.js";
2
2
  //#region src/providers/just-bash.ts
3
3
  function createJustBashDriver(options) {
4
4
  const commands = options.commands?.slice();
@@ -1,4 +1,4 @@
1
- import { r as shellErrorDiagnostics, t as analyzeShellCommand } from "./analyze-kSc2pmy2.js";
1
+ import { r as shellErrorDiagnostics, t as analyzeShellCommand } from "./analyze-psY4ydry.js";
2
2
  //#region src/runtime/session.ts
3
3
  function applyOutputLimit(result, maxLength) {
4
4
  const limit = normalizeLimit(maxLength);
@@ -35,7 +35,6 @@ function normalizeLimit(value) {
35
35
  if (value === Number.POSITIVE_INFINITY) return void 0;
36
36
  return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
37
37
  }
38
- /** A per-call limit can only lower the policy limit. */
39
38
  function lowerLimit(requested, policy) {
40
39
  const call = normalizeLimit(requested);
41
40
  const bound = normalizeLimit(policy);
@@ -78,14 +77,12 @@ function createTimeoutObservation(command, cwd, timeout) {
78
77
  timedOut: true
79
78
  };
80
79
  }
81
- /** Race the driver against the timeout, then abort the driver work. */
82
80
  async function execWithRuntimeTimeout(driver, command, options, timeout) {
83
81
  const controller = new AbortController();
84
82
  const running = driver.exec(command, {
85
83
  ...options,
86
84
  signal: options.signal ? AbortSignal.any([options.signal, controller.signal]) : controller.signal
87
85
  });
88
- running.catch(() => {});
89
86
  let timer;
90
87
  const timedOut = new Promise((resolve) => {
91
88
  timer = setTimeout(() => {
package/dist/workspace.js CHANGED
@@ -1,5 +1,5 @@
1
- import { n as splitShellCommandSegments, r as shellErrorDiagnostics } from "./analyze-kSc2pmy2.js";
2
- import { t as createShellRuntime } from "./session-DjjZUIPE.js";
1
+ import { n as splitShellCommandSegments, r as shellErrorDiagnostics } from "./analyze-psY4ydry.js";
2
+ import { t as createShellRuntime } from "./session-DOtQ5C_n.js";
3
3
  import { createJustBashDriver } from "./providers/just-bash.js";
4
4
  import { Buffer } from "node:buffer";
5
5
  import { posix } from "node:path";
@@ -55,8 +55,12 @@ function statFromEntry(entry) {
55
55
  size: entry.type === "file" ? entry.size || 0 : 0
56
56
  };
57
57
  }
58
- async function copyWorkspacePath(workspace, from, to) {
58
+ function isMissingWorkspacePath(error) {
59
+ return error instanceof Error && /Workspace (?:path|file) does not exist:/.test(error.message);
60
+ }
61
+ async function copyWorkspacePath(workspace, from, to, missingDirectory) {
59
62
  if ((await workspace.stat(from)).type === "file") {
63
+ if (missingDirectory) throw missingDirectory();
60
64
  await workspace.writeFile(to, await workspace.readFile(from, { encoding: "binary" }));
61
65
  return;
62
66
  }
@@ -125,7 +129,13 @@ var WorkspaceFileSystem = class {
125
129
  path: "",
126
130
  type: "directory"
127
131
  });
128
- return statFromEntry(await this.workspace.stat(relativePath));
132
+ return statFromEntry(await this.workspace.stat(relativePath).catch((error) => {
133
+ if (!isMissingWorkspacePath(error)) throw error;
134
+ throw shellErrorDiagnostics.SHELL_PATH_NOT_FOUND({
135
+ cause: error,
136
+ message: `[vitehub] ENOENT: no such file or directory, stat '${this.#resolveFromRoot(path)}'`
137
+ });
138
+ }));
129
139
  }
130
140
  async mkdir(path, options) {
131
141
  const workspace = this.#requireWritable();
@@ -161,7 +171,7 @@ var WorkspaceFileSystem = class {
161
171
  await this.#refreshPaths();
162
172
  }
163
173
  async cp(src, dest, _options) {
164
- await copyWorkspacePath(this.#requireWritable(), this.#toRelativePath(src), this.#toRelativePath(dest));
174
+ await copyWorkspacePath(this.#requireWritable(), this.#toRelativePath(src), this.#toRelativePath(dest), this.#missingDirectory(dest));
165
175
  await this.#refreshPaths();
166
176
  }
167
177
  async mv(src, dest) {
@@ -173,7 +183,7 @@ var WorkspaceFileSystem = class {
173
183
  return;
174
184
  }
175
185
  if (!from || to.startsWith(`${from}/`)) throw shellErrorDiagnostics.SHELL_R0022({ message: `[vitehub] Cannot move Workspace path "${src}" into itself, "${dest}".` });
176
- await copyWorkspacePath(workspace, from, to);
186
+ await copyWorkspacePath(workspace, from, to, this.#missingDirectory(dest));
177
187
  await workspace.rm(from, {
178
188
  recursive: true,
179
189
  force: true
@@ -228,6 +238,10 @@ var WorkspaceFileSystem = class {
228
238
  #resolveFromRoot(path) {
229
239
  return this.resolvePath(workspaceMountPoint, path);
230
240
  }
241
+ #missingDirectory(path) {
242
+ if (!path.endsWith("/")) return void 0;
243
+ return () => shellErrorDiagnostics.SHELL_PATH_NOT_FOUND({ message: `[vitehub] ENOTDIR: not a directory, '${this.#resolveFromRoot(path)}'` });
244
+ }
231
245
  #toRelativePath(path) {
232
246
  const absolutePath = this.#resolveFromRoot(path);
233
247
  if (absolutePath === "/workspace") return "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vite-hub/shell",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
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.3.1",
48
+ "@vite-hub/runtime": "0.5.0",
49
49
  "just-bash": "^3.6.0",
50
50
  "nostics": "1.4.0",
51
- "sh-syntax": "0.8.3"
51
+ "sh-syntax": "0.8.7"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@types/node": "^24.19.1",
@@ -58,7 +58,7 @@
58
58
  "vitest": "^4.1.11"
59
59
  },
60
60
  "engines": {
61
- "node": ">=22.18.0"
61
+ "node": ">=24.15.0"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "vp pack",