Unverified Commit feb27ad6 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

Revert "feat(next/image)!: remove `squoosh` in favor of `sharp` as optional dep" (#61810)

Reverts vercel/next.js#61696

Closes NEXT-2401
parent b3878423
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
/**/*image*/** @timneutkens @ijjk @shuding @styfle @huozhi @ztanner @vercel/devex /**/*image*/** @timneutkens @ijjk @shuding @styfle @huozhi @ztanner @vercel/devex
/**/*img* @timneutkens @ijjk @shuding @styfle @huozhi @ztanner @vercel/devex /**/*img* @timneutkens @ijjk @shuding @styfle @huozhi @ztanner @vercel/devex
/packages/next/client/use-intersection.tsx @timneutkens @ijjk @shuding @styfle /packages/next/client/use-intersection.tsx @timneutkens @ijjk @shuding @styfle
/packages/next/server/lib/squoosh/ @timneutkens @ijjk @shuding @styfle
/packages/next/server/serve-static.ts @timneutkens @ijjk @shuding @styfle @huozhi @ztanner /packages/next/server/serve-static.ts @timneutkens @ijjk @shuding @styfle @huozhi @ztanner
/packages/next/server/config.ts @timneutkens @ijjk @shuding @styfle @huozhi @ztanner /packages/next/server/config.ts @timneutkens @ijjk @shuding @styfle @huozhi @ztanner
......
...@@ -443,6 +443,8 @@ If you need a different provider, you can use the [`loader`](#loader) prop with ...@@ -443,6 +443,8 @@ If you need a different provider, you can use the [`loader`](#loader) prop with
> Images can not be optimized at build time using [`output: 'export'`](/docs/pages/building-your-application/deploying/static-exports), only on-demand. To use `next/legacy/image` with `output: 'export'`, you will need to use a different loader than the default. [Read more in the discussion.](https://github.com/vercel/next.js/discussions/19065) > Images can not be optimized at build time using [`output: 'export'`](/docs/pages/building-your-application/deploying/static-exports), only on-demand. To use `next/legacy/image` with `output: 'export'`, you will need to use a different loader than the default. [Read more in the discussion.](https://github.com/vercel/next.js/discussions/19065)
> The `next/legacy/image` component's default loader uses [`squoosh`](https://www.npmjs.com/package/@squoosh/lib) because it is quick to install and suitable for a development environment. When using `next start` in your production environment, it is strongly recommended that you install [`sharp`](https://www.npmjs.com/package/sharp) by running `npm i sharp` in your project directory. This is not necessary for Vercel deployments, as `sharp` is installed automatically.
## Advanced ## Advanced
The following configuration is for advanced use cases and is usually not necessary. If you choose to configure the properties below, you will override any changes to the Next.js defaults in future updates. The following configuration is for advanced use cases and is usually not necessary. If you choose to configure the properties below, you will override any changes to the Next.js defaults in future updates.
......
...@@ -107,6 +107,7 @@ ...@@ -107,6 +107,7 @@
"@types/react-dom": "18.2.15", "@types/react-dom": "18.2.15",
"@types/relay-runtime": "14.1.13", "@types/relay-runtime": "14.1.13",
"@types/selenium-webdriver": "4.0.15", "@types/selenium-webdriver": "4.0.15",
"@types/sharp": "0.29.3",
"@types/string-hash": "1.1.1", "@types/string-hash": "1.1.1",
"@types/trusted-types": "2.0.3", "@types/trusted-types": "2.0.3",
"@typescript-eslint/eslint-plugin": "6.14.0", "@typescript-eslint/eslint-plugin": "6.14.0",
...@@ -157,7 +158,7 @@ ...@@ -157,7 +158,7 @@
"html-validator": "5.1.18", "html-validator": "5.1.18",
"http-proxy": "1.18.1", "http-proxy": "1.18.1",
"husky": "8.0.0", "husky": "8.0.0",
"image-size": "1.1.1", "image-size": "0.9.3",
"is-animated": "2.0.2", "is-animated": "2.0.2",
"isomorphic-unfetch": "3.0.0", "isomorphic-unfetch": "3.0.0",
"jest": "29.7.0", "jest": "29.7.0",
......
...@@ -114,9 +114,6 @@ ...@@ -114,9 +114,6 @@
"optional": true "optional": true
} }
}, },
"optionalDependencies": {
"sharp": "^0.33.2"
},
"devDependencies": { "devDependencies": {
"@ampproject/toolbox-optimizer": "2.8.3", "@ampproject/toolbox-optimizer": "2.8.3",
"@babel/code-frame": "7.22.5", "@babel/code-frame": "7.22.5",
...@@ -232,6 +229,7 @@ ...@@ -232,6 +229,7 @@
"events": "3.3.0", "events": "3.3.0",
"find-up": "4.1.0", "find-up": "4.1.0",
"fresh": "0.5.2", "fresh": "0.5.2",
"get-orientation": "1.1.2",
"glob": "7.1.7", "glob": "7.1.7",
"gzip-size": "5.1.1", "gzip-size": "5.1.1",
"http-proxy": "1.18.1", "http-proxy": "1.18.1",
...@@ -240,7 +238,7 @@ ...@@ -240,7 +238,7 @@
"https-proxy-agent": "5.0.1", "https-proxy-agent": "5.0.1",
"icss-utils": "5.1.0", "icss-utils": "5.1.0",
"ignore-loader": "0.1.2", "ignore-loader": "0.1.2",
"image-size": "1.1.1", "image-size": "1.0.0",
"is-docker": "2.0.0", "is-docker": "2.0.0",
"is-wsl": "2.2.0", "is-wsl": "2.2.0",
"jest-worker": "27.5.1", "jest-worker": "27.5.1",
......
...@@ -310,6 +310,7 @@ export async function collectBuildTraces({ ...@@ -310,6 +310,7 @@ export async function collectBuildTraces({
// only ignore image-optimizer code when // only ignore image-optimizer code when
// this is being handled outside of next-server // this is being handled outside of next-server
'**/next/dist/server/image-optimizer.js', '**/next/dist/server/image-optimizer.js',
'**/next/dist/server/lib/squoosh/**/*.wasm',
] ]
: []), : []),
......
...@@ -33,7 +33,7 @@ function nextImageLoader(this: any, content: Buffer) { ...@@ -33,7 +33,7 @@ function nextImageLoader(this: any, content: Buffer) {
const imageSizeSpan = imageLoaderSpan.traceChild('image-size-calculation') const imageSizeSpan = imageLoaderSpan.traceChild('image-size-calculation')
const imageSize = await imageSizeSpan.traceAsyncFn(() => const imageSize = await imageSizeSpan.traceAsyncFn(() =>
getImageSize(content).catch((err) => err) getImageSize(content, extension).catch((err) => err)
) )
if (imageSize instanceof Error) { if (imageSize instanceof Error) {
......
...@@ -160,7 +160,8 @@ async function nextMetadataImageLoader( ...@@ -160,7 +160,8 @@ async function nextMetadataImageLoader(
} }
const imageSize: { width?: number; height?: number } = await getImageSize( const imageSize: { width?: number; height?: number } = await getImageSize(
content content,
extension as 'avif' | 'webp' | 'png' | 'jpeg'
).catch((err) => err) ).catch((err) => err)
if (imageSize instanceof Error) { if (imageSize instanceof Error) {
......
The MIT License (MIT)
Copyright © 2019 MooYeol Prescott Lee, http://debug.so <mooyoul@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
(()=>{var e={780:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:true});var r;(function(e){e[e["TOP_LEFT"]=1]="TOP_LEFT";e[e["TOP_RIGHT"]=2]="TOP_RIGHT";e[e["BOTTOM_RIGHT"]=3]="BOTTOM_RIGHT";e[e["BOTTOM_LEFT"]=4]="BOTTOM_LEFT";e[e["LEFT_TOP"]=5]="LEFT_TOP";e[e["RIGHT_TOP"]=6]="RIGHT_TOP";e[e["RIGHT_BOTTOM"]=7]="RIGHT_BOTTOM";e[e["LEFT_BOTTOM"]=8]="LEFT_BOTTOM"})(r=t.Orientation||(t.Orientation={}))},330:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:true});const s=r(781);const i=r(300);class StreamParserWritableClass extends s.Writable{constructor(){super();i(this)}}t.StreamParserWritable=StreamParserWritableClass},300:(e,t,r)=>{var s=r(491);var i=r(937)("stream-parser");e.exports=Parser;var n=-1;var a=0;var o=1;var f=2;function Parser(e){var t=e&&"function"==typeof e._transform;var r=e&&"function"==typeof e._write;if(!t&&!r)throw new Error("must pass a Writable or Transform stream in");i("extending Parser into stream");e._bytes=_bytes;e._skipBytes=_skipBytes;if(t)e._passthrough=_passthrough;if(t){e._transform=transform}else{e._write=write}}function init(e){i("initializing parser stream");e._parserBytesLeft=0;e._parserBuffers=[];e._parserBuffered=0;e._parserState=n;e._parserCallback=null;if("function"==typeof e.push){e._parserOutput=e.push.bind(e)}e._parserInit=true}function _bytes(e,t){s(!this._parserCallback,'there is already a "callback" set!');s(isFinite(e)&&e>0,'can only buffer a finite number of bytes > 0, got "'+e+'"');if(!this._parserInit)init(this);i("buffering %o bytes",e);this._parserBytesLeft=e;this._parserCallback=t;this._parserState=a}function _skipBytes(e,t){s(!this._parserCallback,'there is already a "callback" set!');s(e>0,'can only skip > 0 bytes, got "'+e+'"');if(!this._parserInit)init(this);i("skipping %o bytes",e);this._parserBytesLeft=e;this._parserCallback=t;this._parserState=o}function _passthrough(e,t){s(!this._parserCallback,'There is already a "callback" set!');s(e>0,'can only pass through > 0 bytes, got "'+e+'"');if(!this._parserInit)init(this);i("passing through %o bytes",e);this._parserBytesLeft=e;this._parserCallback=t;this._parserState=f}function write(e,t,r){if(!this._parserInit)init(this);i("write(%o bytes)",e.length);if("function"==typeof t)r=t;_(this,e,null,r)}function transform(e,t,r){if(!this._parserInit)init(this);i("transform(%o bytes)",e.length);if("function"!=typeof t){t=this._parserOutput}_(this,e,t,r)}function _data(e,t,r,s){if(e._parserBytesLeft<=0){return s(new Error("got data but not currently parsing anything"))}if(t.length<=e._parserBytesLeft){return function(){return process(e,t,r,s)}}else{return function(){var i=t.slice(0,e._parserBytesLeft);return process(e,i,r,(function(n){if(n)return s(n);if(t.length>i.length){return function(){return _data(e,t.slice(i.length),r,s)}}}))}}}function process(e,t,r,s){e._parserBytesLeft-=t.length;i("%o bytes left for stream piece",e._parserBytesLeft);if(e._parserState===a){e._parserBuffers.push(t);e._parserBuffered+=t.length}else if(e._parserState===f){r(t)}if(0===e._parserBytesLeft){var o=e._parserCallback;if(o&&e._parserState===a&&e._parserBuffers.length>1){t=Buffer.concat(e._parserBuffers,e._parserBuffered)}if(e._parserState!==a){t=null}e._parserCallback=null;e._parserBuffered=0;e._parserState=n;e._parserBuffers.splice(0);if(o){var _=[];if(t){_.push(t)}else{}if(r){_.push(r)}var p=o.length>_.length;if(p){_.push(trampoline(s))}var u=o.apply(e,_);if(!p||s===u)return s}}else{return s}}var _=trampoline(_data);function trampoline(e){return function(){var t=e.apply(this,arguments);while("function"==typeof t){t=t()}return t}}},491:e=>{"use strict";e.exports=require("assert")},937:e=>{"use strict";e.exports=require("next/dist/compiled/debug")},781:e=>{"use strict";e.exports=require("stream")}};var t={};function __nccwpck_require__(r){var s=t[r];if(s!==undefined){return s.exports}var i=t[r]={exports:{}};var n=true;try{e[r](i,i.exports,__nccwpck_require__);n=false}finally{if(n)delete t[r]}return i.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r={};(()=>{"use strict";var e=r;Object.defineProperty(e,"__esModule",{value:true});const t=__nccwpck_require__(781);const s=__nccwpck_require__(780);e.Orientation=s.Orientation;const i=__nccwpck_require__(330);const noop=()=>{};class EXIFOrientationParser extends i.StreamParserWritable{constructor(){super();this._bytes(4,this.onSignature.bind(this))}onSignature(e){const t=e.readUInt16BE(0);const r=e.readUInt16BE(2);if(t===65496){this.onJPEGMarker(e.slice(2))}else if(t===18761&&r===10752||t===19789&&r===42){this._bytes(4,(t=>{this.onTIFFHeader(Buffer.concat([e,t]))}))}else{this._skipBytes(Infinity,noop)}}onJPEGMarker(e){const t=e.readUInt16BE(0);if(t===65505){this._bytes(8,(e=>{const t=e.readUInt16BE(2)===17784&&e.readUInt16BE(4)===26982&&e.readUInt16BE(6)===0;if(t){this._bytes(8,this.onTIFFHeader.bind(this))}else{const t=e.readUInt16BE(0);const r=t-6;this._skipBytes(r,(()=>{this._bytes(2,this.onJPEGMarker.bind(this))}))}}))}else if(65504<=t&&t<=65519){this._bytes(2,(t=>{const r=t.readUInt16BE(0);const s=r-e.length;this._skipBytes(s,(()=>{this._bytes(2,this.onJPEGMarker.bind(this))}))}))}else{this._skipBytes(Infinity,noop)}}onTIFFHeader(e){const t=e.readUInt16BE(0)===18761;const readUInt16=(e,r)=>t?e.readUInt16LE(r):e.readUInt16BE(r);const readUInt32=(e,r)=>t?e.readUInt32LE(r):e.readUInt32BE(r);const r=readUInt32(e,4);const s=r-e.length;const consumeIDFBlock=()=>{this._bytes(2,(e=>{let t=readUInt16(e,0);const consumeIFDFields=()=>{if(t-- >0){this._bytes(12,(e=>{const t=readUInt16(e,0);if(t===274){const t=e.slice(8,12);const r=readUInt16(t,0);if(1<=r&&r<=8){this.emit("orientation",r)}else{this.emit("error",new Error("Unexpected Orientation value"))}this._skipBytes(Infinity,noop)}else{consumeIFDFields()}}))}else{this._skipBytes(Infinity,noop)}};consumeIFDFields()}))};if(s>0){this._skipBytes(s,consumeIDFBlock)}else{consumeIDFBlock()}}}e.EXIFOrientationParser=EXIFOrientationParser;function getOrientation(e){return new Promise(((r,i)=>{const n=(new EXIFOrientationParser).once("error",onError).once("finish",onFinish).once("orientation",onOrientation);let a=false;function onError(e){n.removeListener("finish",onFinish);n.removeListener("orientation",onOrientation);if(!a){a=true;i(e)}}function onFinish(){n.removeListener("error",onError);n.removeListener("orientation",onOrientation);if(!a){a=true;r(s.Orientation.TOP_LEFT)}}function onOrientation(e){n.removeListener("error",onError);n.removeListener("finish",onFinish);if(!a){a=true;r(e)}}if(Buffer.isBuffer(e)){n.end(e)}else if(e instanceof t.Readable){e.pipe(n)}else{throw new TypeError("Unexpected input type")}}))}e.getOrientation=getOrientation})();module.exports=r})();
\ No newline at end of file
{"name":"get-orientation","main":"index.js","author":"MooYeol Prescott Lee <mooyoul@gmail.com>","license":"MIT"}
The MIT License (MIT) The MIT License (MIT)
Copyright © 2013-Present Aditya Yadav, http://netroy.in Copyright © 2017 Aditya Yadav, http://netroy.in
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
......
...@@ -4,6 +4,7 @@ import { cpus } from 'os' ...@@ -4,6 +4,7 @@ import { cpus } from 'os'
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import { mediaType } from 'next/dist/compiled/@hapi/accept' import { mediaType } from 'next/dist/compiled/@hapi/accept'
import contentDisposition from 'next/dist/compiled/content-disposition' import contentDisposition from 'next/dist/compiled/content-disposition'
import { getOrientation, Orientation } from 'next/dist/compiled/get-orientation'
import imageSizeOf from 'next/dist/compiled/image-size' import imageSizeOf from 'next/dist/compiled/image-size'
import isAnimated from 'next/dist/compiled/is-animated' import isAnimated from 'next/dist/compiled/is-animated'
import { join } from 'path' import { join } from 'path'
...@@ -14,6 +15,11 @@ import type { ImageConfigComplete } from '../shared/lib/image-config' ...@@ -14,6 +15,11 @@ import type { ImageConfigComplete } from '../shared/lib/image-config'
import { hasMatch } from '../shared/lib/match-remote-pattern' import { hasMatch } from '../shared/lib/match-remote-pattern'
import type { NextConfigComplete } from './config-shared' import type { NextConfigComplete } from './config-shared'
import { createRequestResponseMocks } from './lib/mock-request' import { createRequestResponseMocks } from './lib/mock-request'
// Do not import anything other than types from this module
// because it will throw an error when using `outputFileTracing`
// as `jest-worker` is ignored in file tracing. Use `await import`
// or `require` instead.
import type { Operation } from './lib/squoosh/main'
import type { NextUrlWithParsedQuery } from './request-meta' import type { NextUrlWithParsedQuery } from './request-meta'
import type { import type {
IncrementalCacheEntry, IncrementalCacheEntry,
...@@ -22,7 +28,6 @@ import type { ...@@ -22,7 +28,6 @@ import type {
import { sendEtagResponse } from './send-payload' import { sendEtagResponse } from './send-payload'
import { getContentType, getExtension } from './serve-static' import { getContentType, getExtension } from './serve-static'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import isError from '../lib/is-error'
type XCacheHeader = 'MISS' | 'HIT' | 'STALE' type XCacheHeader = 'MISS' | 'HIT' | 'STALE'
...@@ -39,10 +44,10 @@ const VECTOR_TYPES = [SVG] ...@@ -39,10 +44,10 @@ const VECTOR_TYPES = [SVG]
const BLUR_IMG_SIZE = 8 // should match `next-image-loader` const BLUR_IMG_SIZE = 8 // should match `next-image-loader`
const BLUR_QUALITY = 70 // should match `next-image-loader` const BLUR_QUALITY = 70 // should match `next-image-loader`
let sharp: typeof import('sharp') let sharp: typeof import('sharp') | undefined
try { try {
sharp = require('sharp') sharp = require(process.env.NEXT_SHARP_PATH || 'sharp')
if (sharp && sharp.concurrency() > 1) { if (sharp && sharp.concurrency() > 1) {
// Reducing concurrency should reduce the memory usage too. // Reducing concurrency should reduce the memory usage too.
// We more aggressively reduce in dev but also reduce in prod. // We more aggressively reduce in dev but also reduce in prod.
...@@ -50,15 +55,12 @@ try { ...@@ -50,15 +55,12 @@ try {
const divisor = process.env.NODE_ENV === 'development' ? 4 : 2 const divisor = process.env.NODE_ENV === 'development' ? 4 : 2
sharp.concurrency(Math.floor(Math.max(cpus().length / divisor, 1))) sharp.concurrency(Math.floor(Math.max(cpus().length / divisor, 1)))
} }
} catch (e: unknown) { } catch (e) {
if (isError(e) && e.code === 'MODULE_NOT_FOUND') { // Sharp not present on the server, Squoosh fallback will be used
throw new Error(
'Module `sharp` not found. Please run `npm install --cpu=wasm32 sharp` to install it.'
)
}
throw e
} }
let showSharpMissingWarning = process.env.NODE_ENV === 'production'
export interface ImageParamsResult { export interface ImageParamsResult {
href: string href: string
isAbsolute: boolean isAbsolute: boolean
...@@ -416,6 +418,7 @@ export async function optimizeImage({ ...@@ -416,6 +418,7 @@ export async function optimizeImage({
quality, quality,
width, width,
height, height,
nextConfigOutput,
}: { }: {
buffer: Buffer buffer: Buffer
contentType: string contentType: string
...@@ -425,37 +428,98 @@ export async function optimizeImage({ ...@@ -425,37 +428,98 @@ export async function optimizeImage({
nextConfigOutput?: 'standalone' | 'export' nextConfigOutput?: 'standalone' | 'export'
}): Promise<Buffer> { }): Promise<Buffer> {
let optimizedBuffer = buffer let optimizedBuffer = buffer
if (sharp) {
// Begin sharp transformation logic
const transformer = sharp(buffer, {
sequentialRead: true,
})
// Begin sharp transformation logic transformer.rotate()
const transformer = sharp(buffer, {
sequentialRead: true,
})
transformer.rotate() if (height) {
transformer.resize(width, height)
} else {
transformer.resize(width, undefined, {
withoutEnlargement: true,
})
}
if (contentType === AVIF) {
if (transformer.avif) {
const avifQuality = quality - 15
transformer.avif({
quality: Math.max(avifQuality, 0),
chromaSubsampling: '4:2:0', // same as webp
})
} else {
Log.warnOnce(
`Your installed version of the 'sharp' package does not support AVIF images. Run 'npm i sharp@latest' to upgrade to the latest version.\n` +
'Read more: https://nextjs.org/docs/messages/sharp-version-avif'
)
transformer.webp({ quality })
}
} else if (contentType === WEBP) {
transformer.webp({ quality })
} else if (contentType === PNG) {
transformer.png({ quality })
} else if (contentType === JPEG) {
transformer.jpeg({ quality, progressive: true })
}
if (height) { optimizedBuffer = await transformer.toBuffer()
transformer.resize(width, height) // End sharp transformation logic
} else { } else {
transformer.resize(width, undefined, { if (showSharpMissingWarning && nextConfigOutput === 'standalone') {
withoutEnlargement: true, Log.error(
}) `Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly. Read more at: https://nextjs.org/docs/messages/sharp-missing-in-production`
} )
throw new ImageError(500, 'Internal Server Error')
}
// Show sharp warning in production once
if (showSharpMissingWarning) {
Log.warnOnce(
`For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended. Run 'npm i sharp', and Next.js will use it automatically for Image Optimization.\n` +
'Read more: https://nextjs.org/docs/messages/sharp-missing-in-production'
)
showSharpMissingWarning = false
}
if (contentType === AVIF) { // Begin Squoosh transformation logic
const avifQuality = quality - 15 const orientation = await getOrientation(buffer)
transformer.avif({
quality: Math.max(avifQuality, 0), const operations: Operation[] = []
chromaSubsampling: '4:2:0', // same as webp
}) if (orientation === Orientation.RIGHT_TOP) {
} else if (contentType === WEBP) { operations.push({ type: 'rotate', numRotations: 1 })
transformer.webp({ quality }) } else if (orientation === Orientation.BOTTOM_RIGHT) {
} else if (contentType === PNG) { operations.push({ type: 'rotate', numRotations: 2 })
transformer.png({ quality }) } else if (orientation === Orientation.LEFT_BOTTOM) {
} else if (contentType === JPEG) { operations.push({ type: 'rotate', numRotations: 3 })
transformer.jpeg({ quality, progressive: true }) } else {
} // TODO: support more orientations
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// const _: never = orientation
}
if (height) {
operations.push({ type: 'resize', width, height })
} else {
operations.push({ type: 'resize', width })
}
optimizedBuffer = await transformer.toBuffer() const { processBuffer } =
require('./lib/squoosh/main') as typeof import('./lib/squoosh/main')
if (contentType === AVIF) {
optimizedBuffer = await processBuffer(buffer, operations, 'avif', quality)
} else if (contentType === WEBP) {
optimizedBuffer = await processBuffer(buffer, operations, 'webp', quality)
} else if (contentType === PNG) {
optimizedBuffer = await processBuffer(buffer, operations, 'png', quality)
} else if (contentType === JPEG) {
optimizedBuffer = await processBuffer(buffer, operations, 'jpeg', quality)
}
}
return optimizedBuffer return optimizedBuffer
} }
...@@ -603,10 +667,12 @@ export async function imageOptimizer( ...@@ -603,10 +667,12 @@ export async function imageOptimizer(
}) })
if (optimizedBuffer) { if (optimizedBuffer) {
if (isDev && width <= BLUR_IMG_SIZE && quality === BLUR_QUALITY) { if (isDev && width <= BLUR_IMG_SIZE && quality === BLUR_QUALITY) {
const { getMetadata } =
require('./lib/squoosh/main') as typeof import('./lib/squoosh/main')
// During `next dev`, we don't want to generate blur placeholders with webpack // During `next dev`, we don't want to generate blur placeholders with webpack
// because it can delay starting the dev server. Instead, `next-image-loader.js` // because it can delay starting the dev server. Instead, `next-image-loader.js`
// will inline a special url to lazily generate the blur placeholder at request time. // will inline a special url to lazily generate the blur placeholder at request time.
const meta = await getImageSize(optimizedBuffer) const meta = await getMetadata(optimizedBuffer)
const opts = { const opts = {
blurWidth: meta.width, blurWidth: meta.width,
blurHeight: meta.height, blurHeight: meta.height,
...@@ -728,10 +794,29 @@ export function sendResponse( ...@@ -728,10 +794,29 @@ export function sendResponse(
} }
} }
export async function getImageSize(buffer: Buffer): Promise<{ export async function getImageSize(
buffer: Buffer,
// Should match VALID_BLUR_EXT
extension: 'avif' | 'webp' | 'png' | 'jpeg'
): Promise<{
width?: number width?: number
height?: number height?: number
}> { }> {
// TODO: upgrade "image-size" package to support AVIF
// See https://github.com/image-size/image-size/issues/348
if (extension === 'avif') {
if (sharp) {
const transformer = sharp(buffer)
const { width, height } = await transformer.metadata()
return { width, height }
} else {
const { decodeBuffer } =
require('./lib/squoosh/main') as typeof import('./lib/squoosh/main')
const { width, height } = await decodeBuffer(buffer)
return { width, height }
}
}
const { width, height } = imageSizeOf(buffer) const { width, height } = imageSizeOf(buffer)
return { width, height } return { width, height }
} }
@timneutkens:notify
@ijjk:notify
@shuding:notify
This diff is collapsed.
// eslint-disable-next-line no-shadow
export const enum AVIFTune {
auto,
psnr,
ssim,
}
export interface EncodeOptions {
cqLevel: number
denoiseLevel: number
cqAlphaLevel: number
tileRowsLog2: number
tileColsLog2: number
speed: number
subsample: number
chromaDeltaQ: boolean
sharpness: number
tune: AVIFTune
}
export interface AVIFModule extends EmscriptenWasm.Module {
encode(
data: BufferSource,
width: number,
height: number,
options: EncodeOptions
): Uint8Array
}
declare var moduleFactory: EmscriptenWasm.ModuleFactory<AVIFModule>
export default moduleFactory
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// These types roughly model the object that the JS files generated by Emscripten define. Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/emscripten/index.d.ts and turned into a type definition rather than a global to support our way of using Emscripten.
declare namespace EmscriptenWasm {
type ModuleFactory<T extends Module = Module> = (
moduleOverrides?: ModuleOpts
) => Promise<T>
type EnvironmentType = 'WEB' | 'NODE' | 'SHELL' | 'WORKER'
// Options object for modularized Emscripten files. Shoe-horned by @surma.
// FIXME: This an incomplete definition!
interface ModuleOpts {
mainScriptUrlOrBlob?: string
noInitialRun?: boolean
locateFile?: (url: string) => string
onRuntimeInitialized?: () => void
}
interface Module {
print(str: string): void
printErr(str: string): void
arguments: string[]
environment: EnvironmentType
preInit: { (): void }[]
preRun: { (): void }[]
postRun: { (): void }[]
preinitializedWebGLContext: WebGLRenderingContext
noInitialRun: boolean
noExitRuntime: boolean
logReadFiles: boolean
filePackagePrefixURL: string
wasmBinary: ArrayBuffer
destroy(object: object): void
getPreloadedPackage(
remotePackageName: string,
remotePackageSize: number
): ArrayBuffer
instantiateWasm(
imports: WebAssembly.Imports,
successCallback: (module: WebAssembly.Module) => void
): WebAssembly.Exports
locateFile(url: string): string
onCustomMessage(event: MessageEvent): void
Runtime: any
ccall(
ident: string,
returnType: string | null,
argTypes: string[],
args: any[]
): any
cwrap(ident: string, returnType: string | null, argTypes: string[]): any
setValue(ptr: number, value: any, type: string, noSafe?: boolean): void
getValue(ptr: number, type: string, noSafe?: boolean): number
ALLOC_NORMAL: number
ALLOC_STACK: number
ALLOC_STATIC: number
ALLOC_DYNAMIC: number
ALLOC_NONE: number
allocate(slab: any, types: string, allocator: number, ptr: number): number
allocate(slab: any, types: string[], allocator: number, ptr: number): number
Pointer_stringify(ptr: number, length?: number): string
UTF16ToString(ptr: number): string
stringToUTF16(str: string, outPtr: number): void
UTF32ToString(ptr: number): string
stringToUTF32(str: string, outPtr: number): void
// USE_TYPED_ARRAYS == 1
HEAP: Int32Array
IHEAP: Int32Array
FHEAP: Float64Array
// USE_TYPED_ARRAYS == 2
HEAP8: Int8Array
HEAP16: Int16Array
HEAP32: Int32Array
HEAPU8: Uint8Array
HEAPU16: Uint16Array
HEAPU32: Uint32Array
HEAPF32: Float32Array
HEAPF64: Float64Array
TOTAL_STACK: number
TOTAL_MEMORY: number
FAST_MEMORY: number
addOnPreRun(cb: () => any): void
addOnInit(cb: () => any): void
addOnPreMain(cb: () => any): void
addOnExit(cb: () => any): void
addOnPostRun(cb: () => any): void
// Tools
intArrayFromString(
stringy: string,
dontAddNull?: boolean,
length?: number
): number[]
intArrayToString(array: number[]): string
writeStringToMemory(str: string, buffer: number, dontAddNull: boolean): void
writeArrayToMemory(array: number[], buffer: number): void
writeAsciiToMemory(str: string, buffer: number, dontAddNull: boolean): void
addRunDependency(id: any): void
removeRunDependency(id: any): void
preloadedImages: any
preloadedAudios: any
_malloc(size: number): number
_free(ptr: number): void
// Augmentations below by @surma.
onRuntimeInitialized: () => void | null
}
}
import { fileURLToPath } from 'url'
export function pathify(path: string): string {
if (path.startsWith('file://')) {
path = fileURLToPath(path)
}
return path
}
export function instantiateEmscriptenWasm<T extends EmscriptenWasm.Module>(
factory: EmscriptenWasm.ModuleFactory<T>,
path: string,
workerJS: string = ''
): Promise<T> {
return factory({
locateFile(requestPath) {
// The glue code generated by emscripten uses the original
// file names of the worker file and the wasm binary.
// These will have changed in the bundling process and
// we need to inject them here.
if (requestPath.endsWith('.wasm')) return pathify(path)
if (requestPath.endsWith('.worker.js')) return pathify(workerJS)
return requestPath
},
})
}
export default class ImageData {
static from(input: ImageData): ImageData {
return new ImageData(input.data || input._data, input.width, input.height)
}
private _data: Buffer | Uint8Array | Uint8ClampedArray
width: number
height: number
get data(): Buffer {
if (Object.prototype.toString.call(this._data) === '[object Object]') {
return Buffer.from(Object.values(this._data))
}
if (
this._data instanceof Buffer ||
this._data instanceof Uint8Array ||
this._data instanceof Uint8ClampedArray
) {
return Buffer.from(this._data)
}
throw new Error('invariant')
}
constructor(
data: Buffer | Uint8Array | Uint8ClampedArray,
width: number,
height: number
) {
this._data = data
this.width = width
this.height = height
}
}
This diff is collapsed.
This diff is collapsed.
// eslint-disable-next-line no-shadow
export const enum MozJpegColorSpace {
GRAYSCALE = 1,
RGB,
YCbCr,
}
export interface EncodeOptions {
quality: number
baseline: boolean
arithmetic: boolean
progressive: boolean
optimize_coding: boolean
smoothing: number
color_space: MozJpegColorSpace
quant_table: number
trellis_multipass: boolean
trellis_opt_zero: boolean
trellis_opt_table: boolean
trellis_loops: number
auto_subsample: boolean
chroma_subsample: number
separate_chroma_quality: boolean
chroma_quality: number
}
export interface MozJPEGModule extends EmscriptenWasm.Module {
encode(
data: BufferSource,
width: number,
height: number,
options: EncodeOptions
): Uint8Array
}
declare var moduleFactory: EmscriptenWasm.ModuleFactory<MozJPEGModule>
export default moduleFactory
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -740,6 +740,15 @@ export async function ncc_image_size(task, opts) { ...@@ -740,6 +740,15 @@ export async function ncc_image_size(task, opts) {
.target('src/compiled/image-size') .target('src/compiled/image-size')
} }
// eslint-disable-next-line camelcase
externals['get-orientation'] = 'next/dist/compiled/get-orientation'
export async function ncc_get_orientation(task, opts) {
await task
.source(relative(__dirname, require.resolve('get-orientation')))
.ncc({ packageName: 'get-orientation', externals })
.target('src/compiled/get-orientation')
}
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
externals['@hapi/accept'] = 'next/dist/compiled/@hapi/accept' externals['@hapi/accept'] = 'next/dist/compiled/@hapi/accept'
export async function ncc_hapi_accept(task, opts) { export async function ncc_hapi_accept(task, opts) {
...@@ -2244,6 +2253,7 @@ export async function ncc(task, opts) { ...@@ -2244,6 +2253,7 @@ export async function ncc(task, opts) {
'ncc_p_limit', 'ncc_p_limit',
'ncc_raw_body', 'ncc_raw_body',
'ncc_image_size', 'ncc_image_size',
'ncc_get_orientation',
'ncc_hapi_accept', 'ncc_hapi_accept',
'ncc_node_fetch', 'ncc_node_fetch',
'ncc_node_anser', 'ncc_node_anser',
......
This diff is collapsed.
This diff is collapsed.
...@@ -82,6 +82,7 @@ async function main() { ...@@ -82,6 +82,7 @@ async function main() {
'node_modules/next/dist/pages/**/*', 'node_modules/next/dist/pages/**/*',
'node_modules/next/dist/server/image-optimizer.js', 'node_modules/next/dist/server/image-optimizer.js',
'node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*', 'node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
'node_modules/next/dist/server/lib/squoosh/**/*.wasm',
'node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js', 'node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js',
'node_modules/react/**/*.development.js', 'node_modules/react/**/*.development.js',
'node_modules/react-dom/**/*.development.js', 'node_modules/react-dom/**/*.development.js',
......
This diff is collapsed.
This diff is collapsed.
node_modules
!package-lock.json
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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