cc1f3b8a38
Add support for AVIF to `next/image` - Fixes #27882 - Closes #27432 ## Feature - [x] Implements an existing feature request - [x] Related issues linked - [x] Integration tests added - [x] Documentation added - [x] Update manifest output - [x] Warn when `sharp` is outdated - [x] Errors & Warnings have helpful link attached - [ ] Remove `image-size` in favor of `squoosh`/`sharp` (optional, need to benchmark)
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
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 {
|
|
if (extWithoutDot === 'avif') {
|
|
// TODO: update "mime" package
|
|
return 'image/avif'
|
|
}
|
|
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 {
|
|
if (contentType === 'image/avif') {
|
|
// TODO: update "mime" package
|
|
return 'avif'
|
|
}
|
|
const { mime } = send
|
|
if ('getExtension' in mime) {
|
|
// 2.0
|
|
return mime.getExtension(contentType)
|
|
}
|
|
// 1.0
|
|
return (mime as any).extension(contentType)
|
|
}
|