Unverified Commit 55f705f1 authored by Steven's avatar Steven Committed by GitHub

fix(next/image): handle empty buffer and experimental flag for `skipMetadata` (#82569)

As a follow up to PR https://github.com/vercel/next.js/pull/82538 this
PR adds an experimental flag to optionally skip `sharp.metadata()` and
rely only on the JS implementation for format detection.
parent 2bca1fde
...@@ -385,6 +385,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -385,6 +385,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
imgOptTimeoutInSeconds: z.number().int().optional(), imgOptTimeoutInSeconds: z.number().int().optional(),
imgOptMaxInputPixels: z.number().int().optional(), imgOptMaxInputPixels: z.number().int().optional(),
imgOptSequentialRead: z.boolean().optional().nullable(), imgOptSequentialRead: z.boolean().optional().nullable(),
imgOptSkipMetadata: z.boolean().optional().nullable(),
isrFlushToDisk: z.boolean().optional(), isrFlushToDisk: z.boolean().optional(),
largePageDataBytes: z.number().optional(), largePageDataBytes: z.number().optional(),
linkNoTouchStart: z.boolean().optional(), linkNoTouchStart: z.boolean().optional(),
......
...@@ -473,6 +473,7 @@ export interface ExperimentalConfig { ...@@ -473,6 +473,7 @@ export interface ExperimentalConfig {
imgOptTimeoutInSeconds?: number imgOptTimeoutInSeconds?: number
imgOptMaxInputPixels?: number imgOptMaxInputPixels?: number
imgOptSequentialRead?: boolean | null imgOptSequentialRead?: boolean | null
imgOptSkipMetadata?: boolean | null
optimisticClientCache?: boolean optimisticClientCache?: boolean
/** /**
* @deprecated use config.expireTime instead * @deprecated use config.expireTime instead
...@@ -1526,6 +1527,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1526,6 +1527,7 @@ export const defaultConfig = Object.freeze({
imgOptTimeoutInSeconds: 7, imgOptTimeoutInSeconds: 7,
imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D
imgOptSequentialRead: null, imgOptSequentialRead: null,
imgOptSkipMetadata: null,
isrFlushToDisk: true, isrFlushToDisk: true,
workerThreads: false, workerThreads: false,
proxyTimeout: undefined, proxyTimeout: undefined,
......
...@@ -158,8 +158,13 @@ async function writeToCacheDir( ...@@ -158,8 +158,13 @@ async function writeToCacheDir(
* https://en.wikipedia.org/wiki/List_of_file_signatures * https://en.wikipedia.org/wiki/List_of_file_signatures
*/ */
export async function detectContentType( export async function detectContentType(
buffer: Buffer buffer: Buffer,
skipMetadata: boolean | null | undefined,
concurrency?: number | null | undefined
): Promise<string | null> { ): Promise<string | null> {
if (buffer.byteLength === 0) {
return null
}
if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) { if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) {
return JPEG return JPEG
} }
...@@ -239,8 +244,8 @@ export async function detectContentType( ...@@ -239,8 +244,8 @@ export async function detectContentType(
| undefined | undefined
format = detector(buffer) format = detector(buffer)
if (!format) { if (!format && !skipMetadata) {
const sharp = getSharp(null) const sharp = getSharp(concurrency)
const meta = await sharp(buffer) const meta = await sharp(buffer)
.metadata() .metadata()
.catch((_) => null) .catch((_) => null)
...@@ -776,6 +781,7 @@ export async function imageOptimizer( ...@@ -776,6 +781,7 @@ export async function imageOptimizer(
| 'imgOptConcurrency' | 'imgOptConcurrency'
| 'imgOptMaxInputPixels' | 'imgOptMaxInputPixels'
| 'imgOptSequentialRead' | 'imgOptSequentialRead'
| 'imgOptSkipMetadata'
| 'imgOptTimeoutInSeconds' | 'imgOptTimeoutInSeconds'
> >
images: Pick< images: Pick<
...@@ -803,7 +809,11 @@ export async function imageOptimizer( ...@@ -803,7 +809,11 @@ export async function imageOptimizer(
getMaxAge(imageUpstream.cacheControl) getMaxAge(imageUpstream.cacheControl)
) )
const upstreamType = await detectContentType(upstreamBuffer) const upstreamType = await detectContentType(
upstreamBuffer,
nextConfig.experimental.imgOptSkipMetadata,
nextConfig.experimental.imgOptConcurrency
)
if ( if (
!upstreamType || !upstreamType ||
......
...@@ -5,79 +5,129 @@ import { join } from 'path' ...@@ -5,79 +5,129 @@ import { join } from 'path'
const getImage = (filepath) => readFile(join(__dirname, filepath)) const getImage = (filepath) => readFile(join(__dirname, filepath))
describe('detectContentType', () => { describe.each([false, true])(
it('should return jpg', async () => { 'detectContentType with imgOptSkipMetadata: %s',
const buffer = await getImage('./images/test.jpg') (imgOptSkipMetadata) => {
expect(await detectContentType(buffer)).toBe('image/jpeg') it('should return null for empty buffer', async () => {
}) expect(await detectContentType(Buffer.alloc(0), imgOptSkipMetadata)).toBe(
it('should return png', async () => { null
const buffer = await getImage('./images/test.png') )
expect(await detectContentType(buffer)).toBe('image/png') })
}) it('should return null for unrecognized buffer', async () => {
it('should return webp', async () => { expect(
const buffer = await getImage('./images/animated.webp') await detectContentType(
expect(await detectContentType(buffer)).toBe('image/webp') Buffer.from([0xa, 0xb, 0xc]),
}) imgOptSkipMetadata
it('should return svg', async () => { )
const buffer = await getImage('./images/test.svg') ).toBe(null)
expect(await detectContentType(buffer)).toBe('image/svg+xml') })
}) it('should return jpg', async () => {
it('should return svg for inline svg', async () => { const buffer = await getImage('./images/test.jpg')
const buffer = await getImage('./images/test-inline.svg') expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
expect(await detectContentType(buffer)).toBe('image/svg+xml') 'image/jpeg'
}) )
it('should return svg when starts with space', async () => { })
const buffer = Buffer.from( it('should return png', async () => {
' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>' const buffer = await getImage('./images/test.png')
) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
expect(await detectContentType(buffer)).toBe('image/svg+xml') 'image/png'
}) )
it('should return svg when starts with newline', async () => { })
const buffer = Buffer.from( it('should return webp', async () => {
'\n<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>' const buffer = await getImage('./images/animated.webp')
) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
expect(await detectContentType(buffer)).toBe('image/svg+xml') 'image/webp'
}) )
it('should return svg when starts with tab', async () => { })
const buffer = Buffer.from( it('should return svg', async () => {
'\t<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>' const buffer = await getImage('./images/test.svg')
) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
expect(await detectContentType(buffer)).toBe('image/svg+xml') 'image/svg+xml'
}) )
it('should return avif', async () => { })
const buffer = await getImage('./images/test.avif') it('should return svg for inline svg', async () => {
expect(await detectContentType(buffer)).toBe('image/avif') const buffer = await getImage('./images/test-inline.svg')
}) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
it('should return icon', async () => { 'image/svg+xml'
const buffer = await getImage('./images/test.ico') )
expect(await detectContentType(buffer)).toBe('image/x-icon') })
}) it('should return svg when starts with space', async () => {
it('should return icns', async () => { const buffer = Buffer.from(
const buffer = await getImage('./images/test.icns') ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>'
expect(await detectContentType(buffer)).toBe('image/x-icns') )
}) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
it('should return jxl', async () => { 'image/svg+xml'
const buffer = await getImage('./images/test.jxl') )
expect(await detectContentType(buffer)).toBe('image/jxl') })
}) it('should return svg when starts with newline', async () => {
it('should return jp2', async () => { const buffer = Buffer.from(
const buffer = await getImage('./images/test.jp2') '\n<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>'
expect(await detectContentType(buffer)).toBe('image/jp2') )
}) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
it('should return heic', async () => { 'image/svg+xml'
const buffer = await getImage('./images/test.heic') )
expect(await detectContentType(buffer)).toBe('image/heic') })
}) it('should return svg when starts with tab', async () => {
it('should return pdf', async () => { const buffer = Buffer.from(
const buffer = await getImage('./images/test.pdf') '\t<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>'
expect(await detectContentType(buffer)).toBe('application/pdf') )
}) expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
it('should return tiff', async () => { 'image/svg+xml'
const buffer = await getImage('./images/test.tiff') )
expect(await detectContentType(buffer)).toBe('image/tiff') })
}) it('should return avif', async () => {
it('should return bmp', async () => { const buffer = await getImage('./images/test.avif')
const buffer = await getImage('./images/test.bmp') expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
expect(await detectContentType(buffer)).toBe('image/bmp') 'image/avif'
}) )
}) })
it('should return icon', async () => {
const buffer = await getImage('./images/test.ico')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/x-icon'
)
})
it('should return icns', async () => {
const buffer = await getImage('./images/test.icns')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/x-icns'
)
})
it('should return jxl', async () => {
const buffer = await getImage('./images/test.jxl')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/jxl'
)
})
it('should return jp2', async () => {
const buffer = await getImage('./images/test.jp2')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/jp2'
)
})
it('should return heic', async () => {
const buffer = await getImage('./images/test.heic')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/heic'
)
})
it('should return pdf', async () => {
const buffer = await getImage('./images/test.pdf')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'application/pdf'
)
})
it('should return tiff', async () => {
const buffer = await getImage('./images/test.tiff')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/tiff'
)
})
it('should return bmp', async () => {
const buffer = await getImage('./images/test.bmp')
expect(await detectContentType(buffer, imgOptSkipMetadata)).toBe(
'image/bmp'
)
})
}
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment