This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno-entry.mjs
72 lines (62 loc) · 2.08 KB
/
deno-entry.mjs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import process from 'https://deno.land/std/node/process.ts'
import { serve } from 'https://deno.land/std/http/server.ts'
import { serveTls } from 'https://deno.land/std/http/server.ts'
import '#internal/nitro/virtual/polyfill'
import destr from 'destr'
import { useRuntimeConfig } from '#internal/nitro'
import { nitroApp } from '#internal/nitro/app'
import { requestHasBody, useRequestBody } from '#internal/nitro/utils'
const cert = process.env.NITRO_SSL_CERT
const key = process.env.NITRO_SSL_KEY
const port = destr(process.env.NITRO_PORT || process.env.PORT) || 3e3
const hostname = process.env.NITRO_HOST || process.env.HOST
if (cert && key) {
serveTls(handler, { key, cert, port, hostname, onListen })
} else {
serve(handler, { port, hostname, onListen })
}
function onListen({ port, hostname }) {
const baseURL = (useRuntimeConfig().app.baseURL || '').replace(/\/$/, '')
const url = `${hostname}:${port}${baseURL}`
console.log(`Listening ${url}`)
}
// async function handler (request: Request, _connInfo: ConnInfo) {
async function handler(request, _connInfo) {
const url = new URL(request.url)
let body
if (requestHasBody(request)) {
body = await useRequestBody(request)
}
const r = await nitroApp.localCall({
url: url.pathname + url.search,
host: url.hostname,
protocol: url.protocol,
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
})
// TODO: fix in nitro
const responseBody = r.status !== 304 ? r.body : null
return new Response(responseBody, {
headers: r.headers,
status: r.status || 200,
statusText: r.statusText,
})
}
if (process.env.DEBUG) {
process.on('unhandledRejection', err =>
console.error('[nitro] [dev] [unhandledRejection]', err)
)
process.on('uncaughtException', err =>
console.error('[nitro] [dev] [uncaughtException]', err)
)
} else {
process.on('unhandledRejection', err =>
console.error('[nitro] [dev] [unhandledRejection] ' + err)
)
process.on('uncaughtException', err =>
console.error('[nitro] [dev] [uncaughtException] ' + err)
)
}
export default {}