Skip to content

Commit

Permalink
Fix for base fs in headless mode (#259)
Browse files Browse the repository at this point in the history
* Fix for base fs in headless mode

* Remove js-untar dependency from runtime
  • Loading branch information
taybenlor authored Aug 25, 2023
1 parent 34d2512 commit b5d1890
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 74 deletions.
68 changes: 53 additions & 15 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"build:deploy": "npm run bootstrap && npm run build",
"test:client": "cd packages/client && npm run test",
"test:wasi": "cd packages/wasi && npm run test:prepare && npm run test",
"test": "npm run test:client && npm run test:wasi"
"test:runtime": "cd packages/runtime && npm run test",
"test": "npm run test:client && npm run test:wasi && npm run test:runtime"
},
"eslintConfig": {
"extends": "preact",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"test": "npm run test:playwright"
},
"devDependencies": {
"@playwright/test": "^1.35.0",
"@playwright/test": "^1.37.1",
"@types/url-safe-base64": "^1.1.0",
"eslint": "^8.2.0",
"typescript": "^5.1.6",
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ node_modules
.DS_Store
dist
dist-ssr
*.local
*.local
/test-results/
/playwright-report/
/playwright/.cache/
31 changes: 24 additions & 7 deletions packages/runtime/lib/headless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
commandsForRuntime,
getBinaryPathFromCommand,
} from "./commands";
import { fetchWASIFS } from "./helpers";
import { fetchWASIFS, makeRunnoError } from "./helpers";

export async function headlessRunCode(
runtime: Runtime,
Expand Down Expand Up @@ -35,12 +35,32 @@ export async function headlessRunFS(
): Promise<RunResult> {
const commands = commandsForRuntime(runtime, entryPath);

const prepare = await headlessPrepareFS(commands.prepare ?? [], fs);
fs = prepare.fs;
let prepare: CompleteResult;
try {
prepare = await headlessPrepareFS(commands.prepare ?? [], fs);
fs = prepare.fs;
} catch (e) {
return {
resultType: "crash",
error: makeRunnoError(e),
};
}

const { run } = commands;
const binaryPath = getBinaryPathFromCommand(run, fs);

if (run.baseFSURL) {
try {
const baseFS = await fetchWASIFS(run.baseFSURL);
fs = { ...fs, ...baseFS };
} catch (e) {
return {
resultType: "crash",
error: makeRunnoError(e),
};
}
}

const workerHost = new WASIWorkerHost(binaryPath, {
args: [run.binaryName, ...(run.args ?? [])],
env: run.env,
Expand All @@ -61,7 +81,7 @@ export async function headlessRunFS(

const result = await workerHost.start();

prepare.fs = { ...prepare.fs, ...result.fs };
prepare.fs = { ...fs, ...result.fs };
prepare.exitCode = result.exitCode;

return prepare;
Expand Down Expand Up @@ -129,9 +149,6 @@ export async function headlessPrepareFS(
prepare.exitCode = result.exitCode;

if (result.exitCode !== 0) {
// TODO: Remove this
console.error("Prepare failed", prepare);

// If a prepare step fails then we stop.
throw new PrepareError(
"Prepare step returned a non-zero exit code",
Expand Down
19 changes: 1 addition & 18 deletions packages/runtime/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WASIFS } from "@runno/wasi";
import { extractTarGz } from "./tar";

export function stripWhitespace(text: string): string {
Expand Down Expand Up @@ -62,23 +61,7 @@ export function elementCodeContent(element: HTMLElement): string {
export async function fetchWASIFS(fsURL: string) {
const response = await fetch(fsURL);
const buffer = await response.arrayBuffer();
const files = await extractTarGz(new Uint8Array(buffer));

const fs: WASIFS = {};
for (const file of files) {
fs[file.name] = {
path: file.name,
timestamps: {
change: new Date(file.lastModified),
access: new Date(file.lastModified),
modification: new Date(file.lastModified),
},
mode: "binary",
content: new Uint8Array(await file.arrayBuffer()),
};
}

return fs;
return await extractTarGz(new Uint8Array(buffer));
}

export function isErrorObject(
Expand Down
14 changes: 12 additions & 2 deletions packages/runtime/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,18 @@ export class RunnoProvider implements RuntimeMethods {
this.terminal.terminal.clear();

if (run.baseFSURL) {
const baseFS = await fetchWASIFS(run.baseFSURL);
fs = { ...fs, ...baseFS };
try {
const baseFS = await fetchWASIFS(run.baseFSURL);
fs = { ...fs, ...baseFS };
} catch (e) {
console.error(e);
this.terminal.terminal.write(`\nRunno crashed: ${e}\n`);

return {
resultType: "crash",
error: makeRunnoError(e),
};
}
}

return this.terminal.run(
Expand Down
Loading

0 comments on commit b5d1890

Please sign in to comment.