Skip to content

Commit

Permalink
Add tgz support to sandbox (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
taybenlor authored Nov 17, 2024
1 parent b252dba commit c6cbcea
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 52 deletions.
47 changes: 31 additions & 16 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/sandbox/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": {
"@runno/wasi": "npm:@runno/wasi@^0.7.0",
"@std/tar": "jsr:@std/tar@^0.1.3"
}
}
3 changes: 1 addition & 2 deletions packages/sandbox/lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export function commandsForRuntime(
binaryName: "python",
args: [entryPath],
env: {},
// TODO: This is eventually needed for extended python support
// baseFSURL: `${baseURL}/python-3.11.3.tar.gz`,
baseFSURL: `${baseURL}/python-3.11.3.tar.gz`,
},
};

Expand Down
28 changes: 15 additions & 13 deletions packages/sandbox/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { extractTarGz } from "./tar.ts";

export function stripWhitespace(text: string): string {
const lines = text.split(/\n/);
let commonWhitespace = null;
Expand Down Expand Up @@ -34,19 +36,19 @@ export function stripWhitespace(text: string): string {
return outputText.replace(/^\s*/, "").replace(/\s*$/, "");
}

// /**
// * Fetches and deflates a .tar.gz file representing a base filesystem.
// * This is for languages that require specific files to already exist.
// *
// * Prefers .tar.gz files in ustar format.
// *
// * @param fsURL The URL of the filesystem to fetch
// */
// export async function fetchWASIFS(fsURL: string) {
// const response = await fetch(fsURL);
// const buffer = await response.arrayBuffer();
// return await extractTarGz(new Uint8Array(buffer));
// }
/**
* Fetches and deflates a .tar.gz file representing a base filesystem.
* This is for languages that require specific files to already exist.
*
* Prefers .tar.gz files in ustar format.
*
* @param fsURL The URL of the filesystem to fetch
*/
export async function fetchWASIFS(fsURL: string) {
const response = await fetch(fsURL);
const buffer = await response.arrayBuffer();
return await extractTarGz(new Uint8Array(buffer));
}

export function isErrorObject(
e: unknown
Expand Down
37 changes: 19 additions & 18 deletions packages/sandbox/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
commandsForRuntime,
getBinaryPathFromCommand,
} from "./commands.ts";
import { makeRunnoError } from "./helpers.ts";
import { fetchWASIFS, makeRunnoError } from "./helpers.ts";
import { CompleteResult, RunResult, Runtime } from "./types.ts";

export async function runCode(
Expand Down Expand Up @@ -50,24 +50,28 @@ export async function runFS(
const binaryPath = getBinaryPathFromCommand(run, fs);

if (run.baseFSURL) {
// TODO: add support for untarring baseFS
// try {
// const baseFS = await fetchWASIFS(run.baseFSURL);
// fs = { ...fs, ...baseFS };
// } catch (e) {
// return {
// resultType: "crash",
// error: makeRunnoError(e),
// };
// }
throw new Error("Unsupported baseFSURL");
try {
const baseFS = await fetchWASIFS(run.baseFSURL);
fs = { ...fs, ...baseFS };
} catch (e) {
return {
resultType: "crash",
error: makeRunnoError(e),
};
}
}

// TODO: Support STDIN
let stdinBytes = new TextEncoder().encode(stdin ?? "");

const result = await WASI.start(fetch(binaryPath), {
args: [run.binaryName, ...(run.args ?? [])],
env: run.env,
fs,
stdin: (maxByteLength) => {
const chunk = stdinBytes.slice(0, maxByteLength);
stdinBytes = stdinBytes.slice(maxByteLength);
return new TextDecoder().decode(chunk);
},
stdout: (out) => {
prepare.stdout += out;
prepare.tty += out;
Expand Down Expand Up @@ -119,11 +123,8 @@ export async function headlessPrepareFS(
const binaryPath = getBinaryPathFromCommand(command, prepare.fs);

if (command.baseFSURL) {
// TODO: Support baseFSURL
// const baseFS = await fetchWASIFS(command.baseFSURL);
// prepare.fs = { ...prepare.fs, ...baseFS };

throw new Error("Unsupported baseFSURL");
const baseFS = await fetchWASIFS(command.baseFSURL);
prepare.fs = { ...prepare.fs, ...baseFS };
}

const result = await WASI.start(fetch(binaryPath), {
Expand Down
Loading

0 comments on commit c6cbcea

Please sign in to comment.