2021-04-09 15:52:45 +00:00
|
|
|
import type { RequestListener } from 'http'
|
2021-07-15 09:47:15 +00:00
|
|
|
import { loading } from '@nuxt/design'
|
2021-04-09 15:52:45 +00:00
|
|
|
|
|
|
|
export function createServer () {
|
2021-07-15 09:47:15 +00:00
|
|
|
const listener = createDynamicFunction(createLoadingHandler('Loading...'))
|
2021-04-09 15:52:45 +00:00
|
|
|
|
|
|
|
async function listen (opts) {
|
|
|
|
const { listen } = await import('listhen')
|
|
|
|
return listen(listener.call, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
setApp: (app: RequestListener) => listener.set(app),
|
|
|
|
listen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 09:47:15 +00:00
|
|
|
export function createLoadingHandler (message: string): RequestListener {
|
2021-04-15 19:17:44 +00:00
|
|
|
return (_req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
|
|
|
|
res.statusCode = 503 /* Service Unavailable */
|
2021-07-15 09:47:15 +00:00
|
|
|
res.end(loading({ loading: message }))
|
2021-04-15 19:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:52:45 +00:00
|
|
|
function createDynamicFunction<T extends (...args) => any>(initialValue: T) {
|
|
|
|
let fn: T = initialValue
|
|
|
|
return {
|
|
|
|
set: (newFn: T) => { fn = newFn },
|
|
|
|
call: ((...args) => fn(...args)) as T
|
|
|
|
}
|
|
|
|
}
|