Skip to content

Commit e362638

Browse files
committed
fix(exit-node): support Deno fetch export
1 parent a64f97c commit e362638

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

assets/exit_node/exit_node.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function sanitizeHeaders(h: unknown): Record<string, string> {
8181
return out;
8282
}
8383

84-
export default async function (req: Request): Promise<Response> {
84+
export async function handleExitNodeRequest(req: Request): Promise<Response> {
8585
// Fail closed on the placeholder PSK so a fresh deploy without setup
8686
// can't accidentally serve as an open relay.
8787
if (PSK === "CHANGE_ME_TO_A_STRONG_SECRET") {
@@ -173,3 +173,7 @@ export default async function (req: Request): Promise<Response> {
173173
return Response.json({ e: message }, { status: 500 });
174174
}
175175
}
176+
177+
export default {
178+
fetch: handleExitNodeRequest,
179+
};

assets/exit_node/wrapper.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@
3636
// secret. The wrapper imports the handler from exit_node.ts directly,
3737
// so changing the constant in exit_node.ts is all you need.
3838

39-
import handler from "./exit_node.ts";
39+
import exitNode from "./exit_node.ts";
40+
41+
const handler =
42+
typeof exitNode === "function" ? exitNode : exitNode.fetch.bind(exitNode);
43+
44+
function concatChunks(chunks: Uint8Array[]): Blob | undefined {
45+
if (!chunks.length) return undefined;
46+
const len = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
47+
const out = new Uint8Array(len);
48+
let offset = 0;
49+
for (const chunk of chunks) {
50+
out.set(chunk, offset);
51+
offset += chunk.length;
52+
}
53+
return new Blob([out.buffer as ArrayBuffer]);
54+
}
4055

4156
// Deno (preferred)
4257
if (typeof (globalThis as any).Deno !== "undefined") {
@@ -94,7 +109,7 @@ else if (typeof (globalThis as any).process !== "undefined") {
94109
// Build a web-standard Request from Node's IncomingMessage.
95110
const chunks: Uint8Array[] = [];
96111
for await (const c of req) chunks.push(c as Uint8Array);
97-
const body = chunks.length ? Buffer.concat(chunks) : undefined;
112+
const body = concatChunks(chunks);
98113

99114
const url = `http://${req.headers.host ?? hostname}${req.url ?? "/"}`;
100115
const webReq = new Request(url, {

0 commit comments

Comments
 (0)