2018-12-06 15:54:33 +00:00
|
|
|
import {IncomingMessage, ServerResponse} from 'http'
|
|
|
|
import send from 'send'
|
|
|
|
|
|
|
|
// since send doesn't support wasm yet
|
|
|
|
send.mime.define({ 'application/wasm': ['wasm'] })
|
|
|
|
|
2018-12-31 13:44:27 +00:00
|
|
|
export function serveStatic(req: IncomingMessage, res: ServerResponse, path: string): Promise<void> {
|
2018-12-06 15:54:33 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
send(req, path)
|
|
|
|
.on('directory', () => {
|
|
|
|
// We don't allow directories to be read.
|
|
|
|
const err: any = new Error('No directory access')
|
|
|
|
err.code = 'ENOENT'
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(res)
|
|
|
|
.on('finish', resolve)
|
|
|
|
})
|
|
|
|
}
|