@larkup/sandbox 0.1.10 → 0.1.11

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @larkup/sandbox
2
2
 
3
+ ## 0.1.11
4
+
5
+ ### Patch Changes
6
+
7
+ - 3bf4b48: Stage E2B input files through its filesystem API so large analysis datasets are not embedded in process command arguments.
8
+
3
9
  ## 0.1.10
4
10
 
5
11
  ### Patch Changes
@@ -76,7 +76,9 @@ async function execute(request, credentials) {
76
76
  }
77
77
  throw err;
78
78
  }
79
- }, request);
79
+ }, request, async (files) => {
80
+ await withTimeout(activeSandbox.files.write(files), timeoutMs, 'E2B file staging');
81
+ });
80
82
  }
81
83
  catch (err) {
82
84
  return failedResult(err instanceof Error ? err.message : 'E2B execution failed.', startTime);
@@ -7,10 +7,10 @@
7
7
  * with a different SDK shape (commands.run, runCommand, exec, ...). Rather
8
8
  * than re-implement file staging + artifact capture per provider, adapters
9
9
  * plug their SDK's exec call in as a `RemoteExecFn` and hand it to
10
- * `runScriptedExecution`, which builds one POSIX shell script that writes
11
- * any input files, runs the code, and reports back any files written to an
12
- * output directory (mirroring what the Docker adapter gets for free via a
13
- * bind mount).
10
+ * `runScriptedExecution`, which stages inputs, runs the code, and reports
11
+ * back files written to an output directory (mirroring what the Docker
12
+ * adapter gets for free via a bind mount). Providers with a native filesystem
13
+ * API can stage large files without embedding them in a process command.
14
14
  */
15
15
  import type { ExecutionRequest, ExecutionResult } from './types.js';
16
16
  /** Default per-execution timeout for remote adapters when a request doesn't specify one. */
@@ -22,14 +22,23 @@ export interface RemoteCommandResult {
22
22
  }
23
23
  /** Runs a single shell command/script inside a provider's sandbox instance. */
24
24
  export type RemoteExecFn = (script: string) => Promise<RemoteCommandResult>;
25
+ export interface RemoteExecutionFile {
26
+ path: string;
27
+ data: string | ArrayBuffer;
28
+ }
29
+ /** Optional native filesystem staging used by providers that support it. */
30
+ export type RemoteStageFilesFn = (files: RemoteExecutionFile[]) => Promise<void>;
25
31
  /** Builds the POSIX shell script a remote adapter hands to its provider's exec call. */
26
- export declare function buildExecutionScript(request: ExecutionRequest): string;
32
+ export declare function buildExecutionScript(request: ExecutionRequest, filesAlreadyStaged?: boolean): string;
27
33
  /**
28
- * Runs `request` via `execFn` and reassembles an `ExecutionResult`,
29
- * including a best-effort fetch of any files the code wrote to the output
34
+ * Runs `request` via `execFn` and reassembles an `ExecutionResult`. When
35
+ * `stageFiles` is supplied, inputs are written through the provider filesystem;
36
+ * otherwise the portable shell-script staging path is used.
37
+ *
38
+ * It includes a best-effort fetch of any files the code wrote to the output
30
39
  * directory (one follow-up `base64 <file>` call per artifact).
31
40
  */
32
- export declare function runScriptedExecution(execFn: RemoteExecFn, request: ExecutionRequest): Promise<ExecutionResult>;
41
+ export declare function runScriptedExecution(execFn: RemoteExecFn, request: ExecutionRequest, stageFiles?: RemoteStageFilesFn): Promise<ExecutionResult>;
33
42
  /** Uniform safety net for providers whose SDK doesn't take its own per-call timeout. */
34
43
  export declare function withTimeout<T>(promise: Promise<T>, timeoutMs: number, label: string): Promise<T>;
35
44
  /**
@@ -7,10 +7,10 @@
7
7
  * with a different SDK shape (commands.run, runCommand, exec, ...). Rather
8
8
  * than re-implement file staging + artifact capture per provider, adapters
9
9
  * plug their SDK's exec call in as a `RemoteExecFn` and hand it to
10
- * `runScriptedExecution`, which builds one POSIX shell script that writes
11
- * any input files, runs the code, and reports back any files written to an
12
- * output directory (mirroring what the Docker adapter gets for free via a
13
- * bind mount).
10
+ * `runScriptedExecution`, which stages inputs, runs the code, and reports
11
+ * back files written to an output directory (mirroring what the Docker
12
+ * adapter gets for free via a bind mount). Providers with a native filesystem
13
+ * API can stage large files without embedding them in a process command.
14
14
  */
15
15
  import { getMimeType } from './mime.js';
16
16
  /** Default per-execution timeout for remote adapters when a request doesn't specify one. */
@@ -37,7 +37,7 @@ function writeBase64FileScript(name, base64) {
37
37
  return `base64 -d > ${shellSingleQuote(name)} <<'${HEREDOC_DELIMITER}'\n${base64}\n${HEREDOC_DELIMITER}`;
38
38
  }
39
39
  /** Builds the POSIX shell script a remote adapter hands to its provider's exec call. */
40
- export function buildExecutionScript(request) {
40
+ export function buildExecutionScript(request, filesAlreadyStaged = false) {
41
41
  const lines = [
42
42
  'set -e',
43
43
  `mkdir -p ${shellSingleQuote(OUTPUT_DIR)}`,
@@ -45,6 +45,8 @@ export function buildExecutionScript(request) {
45
45
  ];
46
46
  for (const file of request.files ?? []) {
47
47
  assertSafeFileName(file.name);
48
+ if (filesAlreadyStaged)
49
+ continue;
48
50
  const base64 = file.isBase64
49
51
  ? file.content
50
52
  : Buffer.from(file.content, 'utf8').toString('base64');
@@ -52,12 +54,27 @@ export function buildExecutionScript(request) {
52
54
  }
53
55
  const scriptName = request.language === 'python' ? 'run.py' : 'run.js';
54
56
  const runner = request.language === 'python' ? 'python3' : 'node';
55
- const codeBase64 = Buffer.from(request.code, 'utf8').toString('base64');
56
- lines.push(writeBase64FileScript(scriptName, codeBase64));
57
+ if (!filesAlreadyStaged) {
58
+ const codeBase64 = Buffer.from(request.code, 'utf8').toString('base64');
59
+ lines.push(writeBase64FileScript(scriptName, codeBase64));
60
+ }
57
61
  lines.push(`${runner} ${shellSingleQuote(scriptName)}`);
58
62
  lines.push(`if [ -d ${shellSingleQuote(OUTPUT_DIR)} ]; then echo "${ARTIFACT_MARKER}$(ls -1 ${shellSingleQuote(OUTPUT_DIR)} 2>/dev/null | tr '\\n' ',')"; fi`);
59
63
  return lines.join('\n');
60
64
  }
65
+ function executionFiles(request) {
66
+ const files = (request.files ?? []).map((file) => {
67
+ assertSafeFileName(file.name);
68
+ if (!file.isBase64)
69
+ return { path: `${WORKDIR}/${file.name}`, data: file.content };
70
+ const bytes = Buffer.from(file.content, 'base64');
71
+ const data = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
72
+ return { path: `${WORKDIR}/${file.name}`, data };
73
+ });
74
+ const scriptName = request.language === 'python' ? 'run.py' : 'run.js';
75
+ files.push({ path: `${WORKDIR}/${scriptName}`, data: request.code });
76
+ return files;
77
+ }
61
78
  function extractArtifactMarker(stdout) {
62
79
  const markerIndex = stdout.lastIndexOf(ARTIFACT_MARKER);
63
80
  if (markerIndex === -1)
@@ -74,13 +91,18 @@ function extractArtifactMarker(stdout) {
74
91
  return { stdout: (before + rest).replace(/\n$/, ''), artifactNames };
75
92
  }
76
93
  /**
77
- * Runs `request` via `execFn` and reassembles an `ExecutionResult`,
78
- * including a best-effort fetch of any files the code wrote to the output
94
+ * Runs `request` via `execFn` and reassembles an `ExecutionResult`. When
95
+ * `stageFiles` is supplied, inputs are written through the provider filesystem;
96
+ * otherwise the portable shell-script staging path is used.
97
+ *
98
+ * It includes a best-effort fetch of any files the code wrote to the output
79
99
  * directory (one follow-up `base64 <file>` call per artifact).
80
100
  */
81
- export async function runScriptedExecution(execFn, request) {
101
+ export async function runScriptedExecution(execFn, request, stageFiles) {
82
102
  const startTime = Date.now();
83
- const script = buildExecutionScript(request);
103
+ if (stageFiles)
104
+ await stageFiles(executionFiles(request));
105
+ const script = buildExecutionScript(request, Boolean(stageFiles));
84
106
  const main = await execFn(script);
85
107
  const { stdout, artifactNames } = extractArtifactMarker(main.stdout);
86
108
  const artifacts = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@larkup/sandbox",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -46,7 +46,7 @@
46
46
  "e2b": "^2.39.0",
47
47
  "@vercel/sandbox": "^3.0.0",
48
48
  "modal": "^0.11.0",
49
- "@daytona/sdk": "^0.224.1",
49
+ "@daytona/sdk": "^0.225.0",
50
50
  "@browserbasehq/sdk": "^2.17.0",
51
51
  "playwright-core": "^1.62.1",
52
52
  "@fly/sprites": "^0.2.0",