Skip to content

Commit

Permalink
Cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmcd committed May 2, 2024
1 parent b197045 commit fcd0d5d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/DenoHTTPWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import fs from "fs";
import path from "path";

// Uncomment this if you want to debug serial test execution
// const it = _it.concurrent;
const it = _it;
const it = _it.concurrent;
// const it = _it;

describe("DenoHTTPWorker", { timeout: 1000 }, () => {
const echoFile = path.resolve(__dirname, "./test/echo-request.ts");
Expand Down Expand Up @@ -47,6 +47,8 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
"--allow-write",
"--allow-read=/dev/null",
"--allow-write=/dev/null",
"--allow-read=foo,/dev/null",
"--allow-write=bar,/dev/null",
])("should handle %s", async (flag) => {
let worker = await newDenoHTTPWorker(echoScript, {
printOutput: true,
Expand Down Expand Up @@ -129,13 +131,14 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
body:
"data:text/tsx," +
encodeURIComponent(`export default async function (req: Request): Promise<Response> {
return Response.json({ ok: true })
} ${"///".repeat(8000)}`),
return Response.json({ ok: true })
} ${"///".repeat(8000)}`),
});
// We send a request to initialize and when the first request is in flight
// we send another request
let second = worker.client("https://foo.web.val.run");

expect((await first).statusCode).toEqual(200);
await first.text();
expect(await second.text()).toEqual('{"ok":true}');

Expand Down
4 changes: 2 additions & 2 deletions src/test/echo-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default async function (req: Request): Promise<Response> {
let headers: { [key: string]: string } = {};
for (let [key, value] of req.headers.entries()) {
const headers: { [key: string]: string } = {};
for (const [key, value] of req.headers.entries()) {
headers[key] = value;
}
return Response.json({
Expand Down
17 changes: 11 additions & 6 deletions src/test/val-town.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ let initialized = false;
let initializing = false;
let handler: (req: Request) => Promise<Response> | Response;

let pendingRequests: any[] = [];
const pendingRequests: {
req: Request;
resolve: (value: Response | Promise<Response>) => void;
reject: (reason?: unknown) => void;
}[] = [];
export default async function (req: Request): Promise<Response> {
if (initializing) {
return new Promise((resolve, reject) => {
Expand All @@ -12,17 +16,18 @@ export default async function (req: Request): Promise<Response> {
if (!initialized) {
initializing = true;
try {
let source = await req.text();
if (!source) {
return new Response("No source provided", { status: 400 });
const importValue = await req.text();
if (!importValue) {
// This request will error and future requests will hang.
return new Response("No source or import value found", { status: 400 });
}
handler = (await import(source)).default;
handler = (await import(importValue)).default;
initialized = true;
initializing = false;
for (const { req, resolve } of pendingRequests) {
resolve(handler(req));
}
} catch (e: any) {
} catch (e) {
return new Response(e, { status: 500 });
}
return new Response("");
Expand Down

0 comments on commit fcd0d5d

Please sign in to comment.