forked from honojs/node-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
29 lines (27 loc) · 1.16 KB
/
server.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
import { createServer as createServerHTTP } from 'node:http'
import type { AddressInfo } from 'node:net'
import { getRequestListener } from './listener'
import type { Options, ServerType } from './types'
export const createAdaptorServer = (options: Options): ServerType => {
const fetchCallback = options.fetch
const requestListener = getRequestListener(fetchCallback, {
hostname: options.hostname,
overrideGlobalObjects: options.overrideGlobalObjects,
})
// ts will complain about createServerHTTP and createServerHTTP2 not being callable, which works just fine
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createServer: any = options.createServer || createServerHTTP
const server: ServerType = createServer(options.serverOptions || {}, requestListener)
return server
}
export const serve = (
options: Options,
listeningListener?: (info: AddressInfo) => void
): ServerType => {
const server = createAdaptorServer(options)
server.listen(options?.port ?? 3000, options.hostname ?? '0.0.0.0', () => {
const serverInfo = server.address() as AddressInfo
listeningListener && listeningListener(serverInfo)
})
return server
}