Skip to content

Commit

Permalink
fix: add locking around transpilation
Browse files Browse the repository at this point in the history
  • Loading branch information
sprutton1 committed Jan 17, 2025
1 parent 57e0ce7 commit a2c450e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions bin/lang-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"dependencies": {
"@si/ts-lib-deno": "workspace:*",
"@typescript/vfs": "^1.4.0",
"async-mutex": "^0.5.0",
"execa": "^5.1.1",
"fast-json-patch": "^3.1.1",
"joi": "^17.11.0",
Expand Down
43 changes: 24 additions & 19 deletions bin/lang-js/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { rawStorage } from "./sandbox/requestStorage.ts";
import { Debugger } from "./debug.ts";
import { transpile } from "jsr:@deno/emit";
import { Debug } from "./debug.ts";
import { Mutex } from "npm:async-mutex";
import * as _worker from "./worker.js";

const debug = Debug("langJs:function");
Expand Down Expand Up @@ -265,27 +266,31 @@ export async function runCode(
});
}

async function bundleCode(code: string): Promise<string> {
debug({ "code before bundle": code });
const tempDir = await Deno.makeTempDir();
const tempFile = `${tempDir}/script.ts`;
const transpileMutex = new Mutex();

await Deno.writeTextFile(tempFile, code);
const fileUrl = new URL(tempFile, import.meta.url);
function bundleCode(code: string): Promise<string> {
return transpileMutex.runExclusive(async () => {
debug({ "code before bundle": code });
const tempDir = await Deno.makeTempDir();
const tempFile = `${tempDir}/script.ts`;

try {
const result = await transpile(fileUrl);
await Deno.writeTextFile(tempFile, code);
const fileUrl = new URL(tempFile, import.meta.url);

const bundled = result.get(fileUrl.href) as string;
if (!bundled) {
throw new Error("Transpilation resulted in empty output");
}
try {
const result = await transpile(fileUrl);

debug({ "code after bundle": code });
return bundled;
} catch (error) {
throw error;
} finally {
await Deno.remove(tempDir, { recursive: true });
}
const bundled = result.get(fileUrl.href) as string;
if (!bundled) {
throw new Error("Transpilation resulted in empty output");
}

debug({ "code after bundle": code });
return bundled;
} catch (error) {
throw error;
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});
}
2 changes: 1 addition & 1 deletion bin/lang-js/src/function_kinds/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface ManagementCreate {
}

export interface ManagementOperations {
create?: ManagementCreate,
create?: ManagementCreate;
update?: {
[key: string]: {
properties?: object;
Expand Down
11 changes: 11 additions & 0 deletions deno.lock

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

0 comments on commit a2c450e

Please sign in to comment.