Unverified Commit bbdebd4f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure animated assets aren't de-animated by optimizer (#17974)

This makes sure the image optimizer doesn't de-animate images by transforming them with sharp since sharp doesn't currently handle outputting animated images

x-ref: https://github.com/vercel/next.js/pull/17749
parent c9eb3dca
......@@ -82,6 +82,7 @@
"get-port": "5.1.1",
"glob": "7.1.6",
"gzip-size": "5.1.1",
"is-animated": "2.0.0",
"isomorphic-unfetch": "3.0.0",
"jest-circus": "26.0.1",
"jest-cli": "24.9.0",
......
module.exports=function(e,r){"use strict";var t={};function __webpack_require__(r){if(t[r]){return t[r].exports}var a=t[r]={i:r,l:false,exports:{}};e[r].call(a.exports,a,a.exports,__webpack_require__);a.l=true;return a.exports}__webpack_require__.ab=__dirname+"/";function startup(){return __webpack_require__(441)}return startup()}({401:function(e,r){r.isWebp=function(e){var r=[87,69,66,80];for(var t=0;t<r.length;t++){if(e[t+8]!==r[t]){return false}}return true};r.isAnimated=function(e){var r=[65,78,73,77];for(var t=0;t<e.length;t++){for(var a=0;a<r.length;a++){if(e[t+a]!==r[a]){break}}if(a===r.length){return true}}return false}},441:function(e,r,t){"use strict";var a=t(552);var n=t(549);var i=t(401);function isAnimated(e){if(a.isGIF(e)){return a.isAnimated(e)}if(n.isPNG(e)){return n.isAnimated(e)}if(i.isWebp(e)){return i.isAnimated(e)}return false}e.exports=isAnimated},549:function(e,r){r.isPNG=function(e){var r=e.slice(0,8).toString("hex");return r==="89504e470d0a1a0a"};r.isAnimated=function(e){var r=false;var t=false;var a=false;var n=null;var i=8;while(i<e.length){var s=e.readUInt32BE(i);var u=e.slice(i+4,i+8).toString("ascii");switch(u){case"acTL":r=true;break;case"IDAT":if(!r){return false}if(n!=="fcTL"){return false}t=true;break;case"fdAT":if(!t){return false}if(n!=="fcTL"){return false}a=true;break}n=u;i+=4+4+s+4}return r&&t&&a}},552:function(e,r){"use strict";function getDataBlocksLength(e,r){var t=0;while(e[r+t]){t+=e[r+t]+1}return t+1}r.isGIF=function(e){var r=e.slice(0,3).toString("ascii");return r==="GIF"};r.isAnimated=function(e){var r,t,a;var n=0;var i=0;a=e.slice(0,3).toString("ascii");if(a!=="GIF"){return false}r=e[10]&128;t=e[10]&7;n+=6;n+=7;n+=r?3*Math.pow(2,t+1):0;while(i<2&&n<e.length){switch(e[n]){case 44:i+=1;r=e[n+9]&128;t=e[n+9]&7;n+=10;n+=r?3*Math.pow(2,t+1):0;n+=getDataBlocksLength(e,n+1)+1;break;case 33:n+=2;n+=getDataBlocksLength(e,n);break;case 59:n=e.length;break;default:n=e.length;break}}return i>1}}});
\ No newline at end of file
{"name":"is-animated","main":"index.js","author":"Józef Sokołowski <j.k.sokolowski@gmail.com>","license":"MIT"}
......@@ -7,14 +7,18 @@ import { createHash } from 'crypto'
import Server from './next-server'
import { getContentType, getExtension } from './serve-static'
import { fileExists } from '../../lib/file-exists'
// @ts-ignore no types for is-animated
import isAnimated from 'next/dist/compiled/is-animated'
let sharp: typeof import('sharp')
//const AVIF = 'image/avif'
const WEBP = 'image/webp'
const PNG = 'image/png'
const JPEG = 'image/jpeg'
const GIF = 'image/gif'
const MIME_TYPES = [/* AVIF, */ WEBP, PNG, JPEG]
const CACHE_VERSION = 1
const ANIMATABLE_TYPES = [WEBP, PNG, GIF]
export async function imageOptimizer(
server: Server,
......@@ -146,6 +150,18 @@ export async function imageOptimizer(
const expireAt = maxAge * 1000 + now
let contentType: string
if (
upstreamType &&
ANIMATABLE_TYPES.includes(upstreamType) &&
isAnimated(upstreamBuffer)
) {
if (upstreamType) {
res.setHeader('Content-Type', upstreamType)
}
res.end(upstreamBuffer)
return { finished: true }
}
if (mimeType) {
contentType = mimeType
} else if (upstreamType?.startsWith('image/') && getExtension(upstreamType)) {
......
......@@ -241,6 +241,14 @@ export async function ncc_ignore_loader(task, opts) {
.target('compiled/ignore-loader')
}
// eslint-disable-next-line camelcase
externals['is-animated'] = 'next/dist/compiled/is-animated'
export async function ncc_is_animated(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('is-animated')))
.ncc({ packageName: 'is-animated', externals })
.target('compiled/is-animated')
}
// eslint-disable-next-line camelcase
externals['is-docker'] = 'next/dist/compiled/is-docker'
export async function ncc_is_docker(task, opts) {
await task
......@@ -504,6 +512,7 @@ export async function ncc(task) {
'ncc_gzip_size',
'ncc_http_proxy',
'ncc_ignore_loader',
'ncc_is_animated',
'ncc_is_docker',
'ncc_is_wsl',
'ncc_json5',
......
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import isAnimated from 'next/dist/compiled/is-animated'
import {
killApp,
findPort,
......@@ -40,6 +41,30 @@ function runTests({ w, isDev }) {
expect(await res.text()).toMatch(/Image Optimizer Home/m)
})
it('should maintain animated gif', async () => {
const query = { w, q: 90, url: '/animated.gif' }
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/gif')
expect(isAnimated(await res.buffer())).toBe(true)
})
it('should maintain animated png', async () => {
const query = { w, q: 90, url: '/animated.png' }
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/png')
expect(isAnimated(await res.buffer())).toBe(true)
})
it('should maintain animated webp', async () => {
const query = { w, q: 90, url: '/animated.webp' }
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/webp')
expect(isAnimated(await res.buffer())).toBe(true)
})
it('should fail when url is missing', async () => {
const query = { w, q: 100 }
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
......
......@@ -8878,6 +8878,11 @@ is-alphanumerical@^1.0.0:
is-alphabetical "^1.0.0"
is-decimal "^1.0.0"
is-animated@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-animated/-/is-animated-2.0.0.tgz#ffb1f180ff10782b8442dcb0955b5fc915e1a111"
integrity sha512-ZsfhGnSRUjto9owW5WpaDL7z/03H77Ny9JgP/BSyOQ/mAPAKNbdX30WNYbg2R5NhvT5VqKs2KL38znj5OG8tDg==
is-arguments@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/is-arguments/-/is-arguments-1.0.4.tgz#3faf966c7cba0ff437fb31f6250082fcf0448cf3"
......
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