* Materializes sandbox input files before user code runs. `content` entries are written inline; * `url` entries are fetched from inside the sandbox via `curl` — their bytes never pass through the * web process, so the mount size is bounded by sandbox disk, not web heap. The URL and paths are * pa
(
sandbox: E2BSandbox,
files: SandboxFile[] | undefined,
opts: { sandboxId?: string; rootUser?: boolean }
)
| 60 | * the shell sandbox's root execution context. |
| 61 | */ |
| 62 | async function writeSandboxInputs( |
| 63 | sandbox: E2BSandbox, |
| 64 | files: SandboxFile[] | undefined, |
| 65 | opts: { sandboxId?: string; rootUser?: boolean } |
| 66 | ): Promise<void> { |
| 67 | if (!files?.length) return |
| 68 | const fetchedByUrl: string[] = [] |
| 69 | const writtenInline: string[] = [] |
| 70 | for (const file of files) { |
| 71 | if (file.type === 'url') { |
| 72 | const dir = file.path.slice(0, file.path.lastIndexOf('/')) |
| 73 | try { |
| 74 | await sandbox.commands.run( |
| 75 | 'set -e; [ -n "$DIR" ] && mkdir -p "$DIR"; curl -fsS --retry 3 --retry-connrefused --max-time 300 "$URL" -o "$DST"', |
| 76 | { |
| 77 | envs: { URL: file.url, DST: file.path, DIR: dir }, |
| 78 | ...(opts.rootUser ? { user: 'root' } : {}), |
| 79 | } |
| 80 | ) |
| 81 | fetchedByUrl.push(file.path) |
| 82 | } catch (error) { |
| 83 | throw new Error( |
| 84 | `Failed to fetch mounted file into sandbox at ${file.path}: ${getErrorMessage(error)}` |
| 85 | ) |
| 86 | } |
| 87 | } else if (file.encoding === 'base64') { |
| 88 | const buf = Buffer.from(file.content, 'base64') |
| 89 | await sandbox.files.write( |
| 90 | file.path, |
| 91 | buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) |
| 92 | ) |
| 93 | writtenInline.push(file.path) |
| 94 | } else { |
| 95 | await sandbox.files.write(file.path, file.content) |
| 96 | writtenInline.push(file.path) |
| 97 | } |
| 98 | } |
| 99 | // Split counts so it's visible whether a mount was fetched in-sandbox (by presigned URL, no bytes |
| 100 | // through the web process) or written inline. |
| 101 | logger.info('Materialized sandbox inputs', { |
| 102 | sandboxId: opts.sandboxId, |
| 103 | fetchedByUrlCount: fetchedByUrl.length, |
| 104 | writtenInlineCount: writtenInline.length, |
| 105 | fetchedByUrl, |
| 106 | writtenInline, |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | async function createE2BSandbox(kind: 'code' | 'shell' | 'doc' | 'pi'): Promise<E2BSandbox> { |
| 111 | const apiKey = env.E2B_API_KEY |
no test coverage detected