Unverified Commit 39afdc5f authored by Steven's avatar Steven Committed by GitHub

chore: refactor image optimization to separate external/internal urls (#61172)

This PR is refactoring only and doesn't change the logic, therefore no
tests were added.

The improvement here is readability:
- separate functions to fetch external vs internal image (in the future,
we could improve perf of internal image routing)
- types of the arguments are more specific (rather than passing in
complete NextConfig)

> Note: Its easier to review with whitespace hidden:
https://github.com/vercel/next.js/pull/61172/files?w=1
parent 53b1924b
...@@ -72,6 +72,12 @@ export interface ImageParamsResult { ...@@ -72,6 +72,12 @@ export interface ImageParamsResult {
minimumCacheTTL: number minimumCacheTTL: number
} }
interface ImageUpstream {
buffer: Buffer
contentType: string | null | undefined
cacheControl: string | null | undefined
}
function getSupportedMimeType(options: string[], accept = ''): string { function getSupportedMimeType(options: string[], accept = ''): string {
const mimeType = mediaType(accept, options) const mimeType = mediaType(accept, options)
return accept.includes(mimeType) ? mimeType : '' return accept.includes(mimeType) ? mimeType : ''
...@@ -373,7 +379,9 @@ export class ImageError extends Error { ...@@ -373,7 +379,9 @@ export class ImageError extends Error {
} }
} }
function parseCacheControl(str: string | null): Map<string, string> { function parseCacheControl(
str: string | null | undefined
): Map<string, string> {
const map = new Map<string, string>() const map = new Map<string, string>()
if (!str) { if (!str) {
return map return map
...@@ -389,7 +397,7 @@ function parseCacheControl(str: string | null): Map<string, string> { ...@@ -389,7 +397,7 @@ function parseCacheControl(str: string | null): Map<string, string> {
return map return map
} }
export function getMaxAge(str: string | null): number { export function getMaxAge(str: string | null | undefined): number {
const map = parseCacheControl(str) const map = parseCacheControl(str)
if (map) { if (map) {
let age = map.get('s-maxage') || map.get('max-age') || '' let age = map.get('s-maxage') || map.get('max-age') || ''
...@@ -516,40 +524,34 @@ export async function optimizeImage({ ...@@ -516,40 +524,34 @@ export async function optimizeImage({
return optimizedBuffer return optimizedBuffer
} }
export async function imageOptimizer( export async function fetchExternalImage(href: string): Promise<ImageUpstream> {
const res = await fetch(href)
if (!res.ok) {
Log.error('upstream image response failed for', href, res.status)
throw new ImageError(
res.status,
'"url" parameter is valid but upstream response is invalid'
)
}
const buffer = Buffer.from(await res.arrayBuffer())
const contentType = res.headers.get('Content-Type')
const cacheControl = res.headers.get('Cache-Control')
return { buffer, contentType, cacheControl }
}
export async function fetchInternalImage(
href: string,
_req: IncomingMessage, _req: IncomingMessage,
_res: ServerResponse, _res: ServerResponse,
paramsResult: ImageParamsResult,
nextConfig: NextConfigComplete,
isDev: boolean | undefined,
handleRequest: ( handleRequest: (
newReq: IncomingMessage, newReq: IncomingMessage,
newRes: ServerResponse, newRes: ServerResponse,
newParsedUrl?: NextUrlWithParsedQuery newParsedUrl?: NextUrlWithParsedQuery
) => Promise<void> ) => Promise<void>
): Promise<{ buffer: Buffer; contentType: string; maxAge: number }> { ): Promise<ImageUpstream> {
let upstreamBuffer: Buffer
let upstreamType: string | null | undefined
let maxAge: number
const { isAbsolute, href, width, mimeType, quality } = paramsResult
if (isAbsolute) {
const upstreamRes = await fetch(href)
if (!upstreamRes.ok) {
Log.error('upstream image response failed for', href, upstreamRes.status)
throw new ImageError(
upstreamRes.status,
'"url" parameter is valid but upstream response is invalid'
)
}
upstreamBuffer = Buffer.from(await upstreamRes.arrayBuffer())
upstreamType =
detectContentType(upstreamBuffer) ||
upstreamRes.headers.get('Content-Type')
maxAge = getMaxAge(upstreamRes.headers.get('Cache-Control'))
} else {
try { try {
const mocked = createRequestResponseMocks({ const mocked = createRequestResponseMocks({
url: href, url: href,
...@@ -569,12 +571,10 @@ export async function imageOptimizer( ...@@ -569,12 +571,10 @@ export async function imageOptimizer(
) )
} }
upstreamBuffer = Buffer.concat(mocked.res.buffers) const buffer = Buffer.concat(mocked.res.buffers)
upstreamType = const contentType = mocked.res.getHeader('Content-Type')
detectContentType(upstreamBuffer) ||
mocked.res.getHeader('Content-Type')
const cacheControl = mocked.res.getHeader('Cache-Control') const cacheControl = mocked.res.getHeader('Cache-Control')
maxAge = cacheControl ? getMaxAge(cacheControl) : 0 return { buffer, contentType, cacheControl }
} catch (err) { } catch (err) {
Log.error('upstream image response failed for', href, err) Log.error('upstream image response failed for', href, err)
throw new ImageError( throw new ImageError(
...@@ -582,11 +582,31 @@ export async function imageOptimizer( ...@@ -582,11 +582,31 @@ export async function imageOptimizer(
'"url" parameter is valid but upstream response is invalid' '"url" parameter is valid but upstream response is invalid'
) )
} }
} }
if (upstreamType) { export async function imageOptimizer(
upstreamType = upstreamType.toLowerCase().trim() imageUpstream: ImageUpstream,
paramsResult: Pick<
ImageParamsResult,
'href' | 'width' | 'quality' | 'mimeType'
>,
nextConfig: {
output: NextConfigComplete['output']
images: Pick<
NextConfigComplete['images'],
'dangerouslyAllowSVG' | 'minimumCacheTTL'
>
},
isDev: boolean | undefined
): Promise<{ buffer: Buffer; contentType: string; maxAge: number }> {
const { href, quality, width, mimeType } = paramsResult
const upstreamBuffer = imageUpstream.buffer
const maxAge = getMaxAge(imageUpstream.cacheControl)
const upstreamType =
detectContentType(upstreamBuffer) ||
imageUpstream.contentType?.toLowerCase().trim()
if (upstreamType) {
if ( if (
upstreamType.startsWith('image/svg') && upstreamType.startsWith('image/svg') &&
!nextConfig.images.dangerouslyAllowSVG !nextConfig.images.dangerouslyAllowSVG
......
...@@ -544,20 +544,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -544,20 +544,15 @@ export default class NextNodeServer extends BaseServer {
'invariant: imageOptimizer should not be called in minimal mode' 'invariant: imageOptimizer should not be called in minimal mode'
) )
} else { } else {
const { imageOptimizer } = const { imageOptimizer, fetchExternalImage, fetchInternalImage } =
require('./image-optimizer') as typeof import('./image-optimizer') require('./image-optimizer') as typeof import('./image-optimizer')
return imageOptimizer( const handleInternalReq = async (
req.originalRequest, newReq: IncomingMessage,
res.originalResponse, newRes: ServerResponse
paramsResult, ) => {
this.nextConfig,
this.renderOpts.dev,
async (newReq, newRes) => {
if (newReq.url === req.url) { if (newReq.url === req.url) {
throw new Error( throw new Error(`Invariant attempted to optimize _next/image itself`)
`Invariant attempted to optimize _next/image itself`
)
} }
const protocol = this.serverOptions.experimentalHttpsServer const protocol = this.serverOptions.experimentalHttpsServer
...@@ -591,6 +586,23 @@ export default class NextNodeServer extends BaseServer { ...@@ -591,6 +586,23 @@ export default class NextNodeServer extends BaseServer {
} }
return return
} }
const { isAbsolute, href } = paramsResult
const imageUpstream = isAbsolute
? await fetchExternalImage(href)
: await fetchInternalImage(
href,
req.originalRequest,
res.originalResponse,
handleInternalReq
)
return imageOptimizer(
imageUpstream,
paramsResult,
this.nextConfig,
this.renderOpts.dev
) )
} }
} }
......
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