rsnext/packages/next/server/serve-static.ts
Tim Neutkens 5b9ad8da90
Move next-server directory files to server directory (#26756)
* Move next-server directory files to server directory

* Update tests

* Update paths in other places
2021-06-30 13:44:40 +02:00

41 lines
1,020 B
TypeScript

import { IncomingMessage, ServerResponse } from 'http'
import send from 'next/dist/compiled/send'
export function serveStatic(
req: IncomingMessage,
res: ServerResponse,
path: string
): Promise<void> {
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)
})
}
export function getContentType(extWithoutDot: string): string | null {
const { mime } = send
if ('getType' in mime) {
// 2.0
return mime.getType(extWithoutDot)
}
// 1.0
return (mime as any).lookup(extWithoutDot)
}
export function getExtension(contentType: string): string | null {
const { mime } = send
if ('getExtension' in mime) {
// 2.0
return mime.getExtension(contentType)
}
// 1.0
return (mime as any).extension(contentType)
}