Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmcd committed May 8, 2024
1 parent 0d0ae1a commit 7e3775a
Show file tree
Hide file tree
Showing 12 changed files with 2,562 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: ci

on: [push]

jobs:
test:
name: Test
strategy:
matrix:
os: [ubuntu-latest]
node-version: [20.x]
deno-version: [1.40.x]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Use Deno Version ${{ matrix.deno-version }}
uses: denolib/setup-deno@master
with:
deno-version: ${{ matrix.deno-version }}
- name: npm install
run: npm ci
env:
CI: true
- name: build
run: npm run build
env:
CI: true
- name: vitest
run: npm run test
env:
CI: true
- name: deno lint
run: |
cd deno-bootstrap
deno lint
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.DS_Store
.DS_Store
dist
1 change: 1 addition & 0 deletions deno-bootstrap/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"lock": false}
57 changes: 57 additions & 0 deletions deno-bootstrap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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();
}
})();

// serveHttp is deprecated, but we don't have many other options if we'd like to
// keep this pattern of rejecting future connections at the TCP level.
// https://discord.com/channels/684898665143206084/1232398264947445810/1234614780111880303
//
// deno-lint-ignore no-deprecated-deno-api
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));
})();
}
Loading

0 comments on commit 7e3775a

Please sign in to comment.