Skip to content

Commit f23623e

Browse files
committed
Fix linting and add note to readme
1 parent 92f8c80 commit f23623e

File tree

12 files changed

+134
-103
lines changed

12 files changed

+134
-103
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ jobs:
1111
node-version: [22.x, 24.x]
1212
deno-version: [1.40.x]
1313
runs-on: ${{ matrix.os }}
14+
env:
15+
CI: true
1416
steps:
1517
- uses: actions/checkout@v5
1618
- name: Use Node.js ${{ matrix.node-version }}
@@ -23,13 +25,9 @@ jobs:
2325
deno-version: ${{ matrix.deno-version }}
2426
- name: npm install
2527
run: npm ci
26-
env:
27-
CI: true
2828
- name: prepare
2929
run: npm run prepare
30-
env:
31-
CI: true
30+
- name: lint
31+
run: npm run lint
3232
- name: vitest
3333
run: npm run test
34-
env:
35-
CI: true

README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,52 @@
22

33
[![NPM version](https://img.shields.io/npm/v/deno-http-worker.svg?style=flat)](https://npmjs.org/package/deno-http-worker)
44

5-
Similarly to [deno-vm](https://github.com/casual-simulation/node-deno-vm), deno-http-worker lets you securely spawn Deno http servers.
5+
Similarly to [deno-vm](https://github.com/casual-simulation/node-deno-vm),
6+
deno-http-worker lets you securely spawn Deno http servers.
67

78
## Usage
89

910
```ts
10-
import { newDenoHTTPWorker } from 'deno-http-worker';
11+
import { newDenoHTTPWorker } from "deno-http-worker";
1112

1213
let worker = await newDenoHTTPWorker(
1314
`export default {
1415
async fetch(req: Request): Promise<Response> {
1516
return Response.json({ ok: req.url });
1617
},
1718
}`,
18-
{ printOutput: true, runFlags: ["--allow-net"] }
19+
{ printOutput: true, runFlags: ["--allow-net"] },
1920
);
2021

2122
const req = await worker.request({
2223
url: "https://hello/world?query=param",
23-
method: "GET"
24-
})
25-
const body = await req.body.json()
24+
method: "GET",
25+
});
26+
const body = await req.body.json();
2627

27-
console.log(body) // => {"ok":"https://hello/world?query=param"}
28+
console.log(body); // => {"ok":"https://hello/world?query=param"}
2829

2930
worker.terminate();
3031
```
3132

3233
## Internals
3334

34-
Deno-http-worker connects to the Deno process over a Unix socket via undici to make requests. As a result, the worker does not provide an address or url, but instead returns `undici.ResponseData` that uses `undici.Pool.request` under the hood, but modifies the request attributes to work over the socket, and we expose parts of [request and response interface](./src/types.ts).
35-
36-
You can also connect to the Deno process over a WebSocket connection, which uses the same `undici.Pool`. We modify the inbound Request objects to preserve various headers. Unfortunately Deno doesn't let you copy a request and then modify properties, so we patch `Deno.upgradeWebSocket` when you use the WebSockets functionality to use the original request for the upgrade, which may be slightly different.
37-
38-
If you need more advanced usage here, or run into bugs, please open an issue.
35+
> [!TIP]
36+
> This package globally patches Deno.upgradeWebSocket to enable websocket
37+
> proxying. You can provide your own bootstrap script if different behavior is
38+
> desired.
39+
40+
Deno-http-worker connects to the Deno process over a Unix socket via undici to
41+
make requests. As a result, the worker does not provide an address or url, but
42+
instead returns `undici.ResponseData` that uses `undici.Pool.request` under the
43+
hood, but modifies the request attributes to work over the socket, and we expose
44+
parts of [request and response interface](./src/types.ts).
45+
46+
You can also connect to the Deno process over a WebSocket connection, which uses
47+
the same `undici.Pool`. We modify the inbound Request objects to preserve
48+
various headers. Unfortunately Deno doesn't let you copy a request and then
49+
modify properties, so we patch `Deno.upgradeWebSocket` when you use the
50+
WebSockets functionality to use the original request for the upgrade, which may
51+
be slightly different.
52+
53+
If you need more advanced usage here, or run into bugs, please open an issue.

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"indentStyle": "space"
66
},
77
"files": {
8-
"includes": ["**", "!**/dist"]
8+
"includes": ["**", "!**/dist", "!**/deno-bootstrap", "!**/src/test"]
99
},
1010
"assist": { "actions": { "source": { "organizeImports": "off" } } },
1111
"linter": {

deno-bootstrap/index.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
declare global {
2-
interface Request {
3-
[originalRequestProp]: Request;
4-
}
5-
}
6-
71
const socketFile = Deno.args[0];
82
const scriptType = Deno.args[1];
93
const script = Deno.args[2];
104

11-
const importURL =
12-
scriptType === "import"
13-
? script
14-
: `data:text/tsx,${encodeURIComponent(script)}`;
5+
const importURL = scriptType === "import"
6+
? script
7+
: `data:text/tsx,${encodeURIComponent(script)}`;
158

169
const mod = await import(importURL);
1710
if (!mod.default) {
@@ -21,13 +14,12 @@ if (typeof mod.default.fetch !== "function") {
2114
throw new Error("Default export does not have a fetch function.");
2215
}
2316

24-
const onError =
25-
mod.default.onError ??
17+
const onError = mod.default.onError ??
2618
((error: unknown) => {
2719
console.error(error);
2820
return new Response("Internal Server Error", { status: 500 });
2921
});
30-
const onListen = mod.default.onListen ?? ((_localAddr: Deno.NetAddr) => { });
22+
const onListen = mod.default.onListen ?? ((_localAddr: Deno.NetAddr) => {});
3123

3224
const originalRequestProp = Symbol("originalRequest");
3325

@@ -36,7 +28,11 @@ const originalRequestProp = Symbol("originalRequest");
3628
const originalUpgrade = Deno.upgradeWebSocket;
3729
Object.defineProperty(Deno, "upgradeWebSocket", {
3830
value: (req: Request) => {
39-
return originalUpgrade(req[originalRequestProp]);
31+
return originalUpgrade(
32+
(req as unknown as { [originalRequestProp]: Request })[
33+
originalRequestProp
34+
],
35+
);
4036
},
4137
});
4238

@@ -62,7 +58,9 @@ const server = Deno.serve(
6258
const req = new Request(url.toString(), originalReq);
6359

6460
// Add the original request so that we can use it during Deno.upgradeWebSocket
65-
req[originalRequestProp] = originalReq;
61+
(req as unknown as { [originalRequestProp]: Request })[
62+
originalRequestProp
63+
] = originalReq;
6664

6765
// Restore host and connection headers.
6866
req.headers.delete("host");
@@ -74,7 +72,7 @@ const server = Deno.serve(
7472
if (req.headers.has("X-Deno-Worker-Connection")) {
7573
req.headers.set(
7674
"connection",
77-
req.headers.get("X-Deno-Worker-Connection")!
75+
req.headers.get("X-Deno-Worker-Connection")!,
7876
);
7977
}
8078

@@ -83,7 +81,7 @@ const server = Deno.serve(
8381
req.headers.delete("X-Deno-Worker-Connection");
8482

8583
return mod.default.fetch(req);
86-
}
84+
},
8785
);
8886

8987
addEventListener("error", (e) => {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"scripts": {
88
"test": "vitest run",
99
"test:watch": "vitest",
10-
"lint": "eslint . && npm run lint:deno-bootstrap && npm run lint:deno-test-files",
11-
"lint:deno-bootstrap": "cd deno-bootstrap && deno lint",
12-
"lint:deno-test-files": "cd src/test && deno lint",
10+
"lint": "biome check && eslint . && npm run lint:deno-bootstrap && npm run lint:deno-test-files",
11+
"lint:deno-bootstrap": "cd deno-bootstrap && deno lint && deno fmt --check && deno check",
12+
"lint:deno-test-files": "cd src/test && deno lint && deno fmt --check && deno check",
1313
"build": "tsc --build",
1414
"prepare": "npm run lint && npm run build"
1515
},

src/DenoHTTPWorker.test.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
182182
);
183183
jsonRequest(worker, "https://localhost/hello?isee=you", {
184184
headers: { accept: "application/json" },
185-
}).catch(() => { });
185+
}).catch(() => {});
186186

187-
for (; ;) {
187+
for (;;) {
188188
const stderr = worker.stderr.read();
189189
if (stderr) {
190190
expect(stderr.toString()).toContain("Error: uncaught!");
@@ -210,9 +210,9 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
210210
);
211211
jsonRequest(worker, "https://localhost/hello?isee=you", {
212212
headers: { accept: "application/json" },
213-
}).catch(() => { });
213+
}).catch(() => {});
214214

215-
for (; ;) {
215+
for (;;) {
216216
const stderr = worker.stderr.read();
217217
if (stderr) {
218218
expect(stderr.toString()).toContain("Error: uncaught!");
@@ -246,11 +246,11 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
246246
});
247247
});
248248
const json = await jsonRequest(worker, "https://localhost/hello?isee=you");
249-
console.log(json)
249+
console.log(json);
250250
expect(json).toEqual({
251251
ok: "https://localhost/hello?isee=you",
252252
});
253-
console.log("OKK")
253+
console.log("OKK");
254254
void worker.shutdown();
255255
await exitPromise;
256256
});
@@ -422,12 +422,16 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
422422
});
423423

424424
it("can upgrade websocket", async () => {
425-
const webSocketScriptStr = fs.readFileSync(echoWebsocketFile, { encoding: "utf-8" });
426-
const worker = await newDenoHTTPWorker(webSocketScriptStr, { printOutput: true });
425+
const webSocketScriptStr = fs.readFileSync(echoWebsocketFile, {
426+
encoding: "utf-8",
427+
});
428+
const worker = await newDenoHTTPWorker(webSocketScriptStr, {
429+
printOutput: true,
430+
});
427431

428432
const messages: string[] = [];
429433
const ws = await worker.websocket("ws://localhost/echo");
430-
const event = await new Promise<Event>(res => {
434+
const event = await new Promise<Event>((res) => {
431435
ws.addEventListener("open", (event) => {
432436
console.log("WebSocket connection opened");
433437
ws.send("message1");
@@ -458,8 +462,12 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
458462
});
459463

460464
it("can upgrade many websockets at the same time, and identify them uniquely", async () => {
461-
const webSocketScriptStr = fs.readFileSync(echoWebsocketFile, { encoding: "utf-8" });
462-
const worker = await newDenoHTTPWorker(webSocketScriptStr, { printOutput: true });
465+
const webSocketScriptStr = fs.readFileSync(echoWebsocketFile, {
466+
encoding: "utf-8",
467+
});
468+
const worker = await newDenoHTTPWorker(webSocketScriptStr, {
469+
printOutput: true,
470+
});
463471

464472
const ws1 = await worker.websocket("ws://localhost/echo");
465473
const ws2 = await worker.websocket("ws://localhost/echo");
@@ -497,5 +505,5 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
497505
);
498506

499507
await worker.terminate();
500-
} );
508+
});
501509
});

0 commit comments

Comments
 (0)