Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
0562cc77
Unverified
Commit
0562cc77
authored
Jul 01, 2021
by
Steven
Committed by
GitHub
Jul 01, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add additional tests for image type detection (#26832)
Adding additional tests. Follow up to #26705
parent
748b005c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletion
+26
-1
image-optimizer.ts
packages/next/server/image-optimizer.ts
+1
-1
detect-content-type.test.js
...egration/image-optimizer/test/detect-content-type.test.js
+25
-0
No files found.
packages/next/server/image-optimizer.ts
View file @
0562cc77
...
...
@@ -498,7 +498,7 @@ function parseCacheControl(str: string | null): Map<string, string> {
* it matches the "magic number" of known file signatures.
* https://en.wikipedia.org/wiki/List_of_file_signatures
*/
function
detectContentType
(
buffer
:
Buffer
)
{
export
function
detectContentType
(
buffer
:
Buffer
)
{
if
([
0xff
,
0xd8
,
0xff
].
every
((
b
,
i
)
=>
buffer
[
i
]
===
b
))
{
return
JPEG
}
...
...
test/integration/image-optimizer/test/detect-content-type.test.js
0 → 100644
View file @
0562cc77
/* eslint-env jest */
import
{
detectContentType
}
from
'../../../../packages/next/dist/server/image-optimizer.js'
import
{
readFile
}
from
'fs/promises'
import
{
join
}
from
'path'
const
getImage
=
(
filepath
)
=>
readFile
(
join
(
__dirname
,
filepath
))
describe
(
'detectContentType'
,
()
=>
{
it
(
'should return jpg'
,
async
()
=>
{
const
buffer
=
await
getImage
(
'../public/test.jpg'
)
expect
(
detectContentType
(
buffer
)).
toBe
(
'image/jpeg'
)
})
it
(
'should return png'
,
async
()
=>
{
const
buffer
=
await
getImage
(
'../public/test.png'
)
expect
(
detectContentType
(
buffer
)).
toBe
(
'image/png'
)
})
it
(
'should return webp'
,
async
()
=>
{
const
buffer
=
await
getImage
(
'../public/animated.webp'
)
expect
(
detectContentType
(
buffer
)).
toBe
(
'image/webp'
)
})
it
(
'should return svg'
,
async
()
=>
{
const
buffer
=
await
getImage
(
'../public/test.svg'
)
expect
(
detectContentType
(
buffer
)).
toBe
(
'image/svg+xml'
)
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment