-
Notifications
You must be signed in to change notification settings - Fork 568
/
Copy pathdeno-deploy.ts
48 lines (40 loc) · 1.21 KB
/
deno-deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import "#internal/nitro/virtual/polyfill";
// @ts-ignore
import { nitroApp } from "../app";
// https://deno.land/api?s=Deno.ServeHandlerInfo
type ServeHandlerInfo = {
remoteAddr: {
transport: "tcp" | "udp";
hostname: string;
port: number;
};
};
// @ts-expect-error unknown global Deno
Deno.serve((request, info) => {
return handleRequest(request, info);
});
async function handleRequest(request: Request, info: ServeHandlerInfo) {
const url = new URL(request.url);
const headers = new Headers(request.headers);
// Add client IP address to headers
// (rightmost is most trustable)
headers.append("x-forwarded-for", info.remoteAddr.hostname);
// There is currently no way to know if the request was made over HTTP or HTTPS
// Deno deploy force redirects to HTTPS so we assume HTTPS by default
if (!headers.has("x-forwarded-proto")) {
headers.set("x-forwarded-proto", "https");
}
// https://deno.land/api?s=Body
let body;
if (request.body) {
body = await request.arrayBuffer();
}
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers,
method: request.method,
redirect: request.redirect,
body,
});
}