Skip to content

Commit

Permalink
Remove all the deno flags options and replace with simpler implementa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
maxmcd committed Apr 30, 2024
1 parent 95f52c2 commit 9c045b5
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 315 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ jobs:
CI: true
# - name: deno test
# run: |
# cd deno-guest
# cd deno-bootstrap
# deno test
File renamed without changes.
51 changes: 51 additions & 0 deletions deno-bootstrap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const scriptType = Deno.args[0];
const script = Deno.args[1];

const importURL =
scriptType == "import"
? script
: "data:text/tsx," + encodeURIComponent(script);

const server = Deno.listen({
hostname: "0.0.0.0",
port: 0,
});

const addr = server.addr as Deno.NetAddr;

console.log(`deno-listening-port ${addr.port.toString().padStart(5, " ")} `);

// Now that we're listening, start executing user-provided code. We could
// import while starting the server for a small performance improvement,
// but it would complicate reading the port from the Deno logs.
const handler = await import(importURL);
if (!handler.default) {
throw new Error("No default export found in script.");
}
if (typeof handler.default !== "function") {
throw new Error("Default export is not a function.");
}

const conn = await server.accept();
(async () => {
// Reject all additional connections.
for await (const conn of server) {
conn.close();
}
})();
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {
(async () => {
let req = requestEvent.request;
const url = new URL(req.url);
url.host = req.headers.get("X-Deno-Worker-Host") || url.host;
url.protocol = req.headers.get("X-Deno-Worker-Protocol") + ":";
url.port = req.headers.get("X-Deno-Worker-Port") || url.port;
req = new Request(url.toString(), req);
req.headers.delete("X-Deno-Worker-Host");
req.headers.delete("X-Deno-Worker-Protocol");
req.headers.delete("X-Deno-Worker-Port");

await requestEvent.respondWith(handler.default(req));
})();
}
60 changes: 0 additions & 60 deletions deno-guest/index.ts

This file was deleted.

25 changes: 20 additions & 5 deletions src/DenoHTTPWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { it as _it, describe, expect } from "vitest";
import { newDenoHTTPWorker } from "./index.js";
import fs from "fs";
import path from "path";

import readline from "readline";
// Uncomment this if you want to debug serial test execution
const it = _it.concurrent;
// const it = _it
Expand Down Expand Up @@ -42,12 +42,12 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
const file = path.resolve(__dirname, "./test/echo-request.ts");
const url = new URL(`file://${file}`);
let worker = await newDenoHTTPWorker(url, {
permissions: {
allowRead: [file],
denoFlags: {
"--allow-read": [file],
},
});
process.stderr.on("data", (data) => {
console.log(data.toString());
console.error(data.toString());
});
process.stdout.on("data", (data) => {
console.log(data.toString());
Expand Down Expand Up @@ -128,13 +128,28 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
expect(allStdout).toEqual("Hi, I am here\n");
});

it("cannot make outside connection to deno server", async () => {
let worker = await newDenoHTTPWorker(
`export default async function (req: Request): Promise<Response> {
let body = await req.text();
return Response.json({ length: body.length })
}`
);

await expect(
fetch("http://localhost:" + worker.denoListeningPort)
).rejects.toThrowError("fetch failed");

worker.terminate();
});

it("can implement val town", async () => {
let worker = await newDenoHTTPWorker(vtScript);
worker.stdout.on("data", (data) => {
console.log(data.toString());
});
worker.stderr.on("data", (data) => {
console.log(data.toString());
console.error(data.toString());
});
let first = worker.client.post("https://localhost:8080/", {
body:
Expand Down
Loading

0 comments on commit 9c045b5

Please sign in to comment.