-
Notifications
You must be signed in to change notification settings - Fork 31.8k
feat(next/image)!: add images.maximumResponseBody config
#88183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* eslint-env jest */ | ||
| import { | ||
| fetchExternalImage, | ||
| ImageError, | ||
| } from 'next/dist/server/image-optimizer' | ||
|
|
||
| describe('fetchExternalImage', () => { | ||
| describe('response size limit', () => { | ||
| it('should throw error when response has no body', async () => { | ||
| global.fetch = jest.fn().mockResolvedValue({ | ||
| ok: true, | ||
| status: 200, | ||
| body: null, | ||
| headers: { | ||
| get: jest.fn(() => null), | ||
| }, | ||
| }) | ||
|
|
||
| const error = await fetchExternalImage( | ||
| 'http://example.com/no-body.jpg', | ||
| false, | ||
| 300_000_000 | ||
| ).catch((e) => e) | ||
|
|
||
| expect(error).toBeInstanceOf(ImageError) | ||
| expect((error as ImageError).statusCode).toBe(400) | ||
| expect((error as ImageError).message).toBe( | ||
| '"url" parameter is valid but upstream response is invalid' | ||
| ) | ||
| }) | ||
|
|
||
| it('should throw error when exceeding maximumResponseBody config on later chunk', async () => { | ||
| const maximumResponseBody = 2_000 // 2KB custom limit | ||
| const chunkSize = 1_000 // 1KB chunks | ||
| const numChunks = 3 // 3KB total, exceeds custom 2KB limit | ||
|
|
||
| global.fetch = jest.fn().mockImplementation(() => { | ||
| let chunksRead = 0 | ||
| const mockReadableStream = new ReadableStream({ | ||
| async pull(controller) { | ||
| if (chunksRead < numChunks) { | ||
| controller.enqueue(new Uint8Array(chunkSize)) | ||
| chunksRead++ | ||
| } else { | ||
| controller.close() | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| return Promise.resolve({ | ||
| ok: true, | ||
| status: 200, | ||
| body: mockReadableStream, | ||
| headers: { | ||
| get: jest.fn((header: string) => { | ||
| if (header === 'Content-Type') return 'image/jpeg' | ||
| return null | ||
| }), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| const error = await fetchExternalImage( | ||
| 'http://example.com/custom-limit.jpg', | ||
| false, | ||
| maximumResponseBody | ||
| ).catch((e) => e) | ||
|
|
||
| expect(error).toBeInstanceOf(ImageError) | ||
| expect((error as ImageError).statusCode).toBe(413) | ||
| expect((error as ImageError).message).toBe( | ||
| '"url" parameter is valid but upstream response is invalid' | ||
| ) | ||
| }) | ||
|
|
||
| it('should throw error when exceeding maximumResponseBody config on first chunk', async () => { | ||
| const maximumResponseBody = 2_000 // 2KB custom limit | ||
|
|
||
| global.fetch = jest.fn().mockImplementation(() => { | ||
| const mockReadableStream = new ReadableStream({ | ||
| async pull(controller) { | ||
| controller.enqueue(new Uint8Array(maximumResponseBody + 1)) | ||
| controller.close() | ||
| }, | ||
| }) | ||
|
|
||
| return Promise.resolve({ | ||
| ok: true, | ||
| status: 200, | ||
| body: mockReadableStream, | ||
| headers: { | ||
| get: jest.fn((header: string) => { | ||
| if (header === 'Content-Type') return 'image/jpeg' | ||
| return null | ||
| }), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| const error = await fetchExternalImage( | ||
| 'http://example.com/custom-limit.jpg', | ||
| false, | ||
| maximumResponseBody | ||
| ).catch((e) => e) | ||
|
|
||
| expect(error).toBeInstanceOf(ImageError) | ||
| expect((error as ImageError).statusCode).toBe(413) | ||
| expect((error as ImageError).message).toBe( | ||
| '"url" parameter is valid but upstream response is invalid' | ||
| ) | ||
| }) | ||
|
|
||
| it('should succeed when exactly matching maximumResponseBody config on first chunk', async () => { | ||
| const maximumResponseBody = 3_000 // 3KB custom limit | ||
|
|
||
| global.fetch = jest.fn().mockImplementation(() => { | ||
| const mockReadableStream = new ReadableStream({ | ||
| async pull(controller) { | ||
| controller.enqueue(new Uint8Array(maximumResponseBody)) | ||
| controller.close() | ||
| }, | ||
| }) | ||
|
|
||
| return Promise.resolve({ | ||
| ok: true, | ||
| status: 200, | ||
| body: mockReadableStream, | ||
| headers: { | ||
| get: jest.fn((header: string) => { | ||
| if (header === 'Content-Type') return 'image/jpeg' | ||
| return null | ||
| }), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| const result = await fetchExternalImage( | ||
| 'http://example.com/custom-limit.jpg', | ||
| false, | ||
| maximumResponseBody | ||
| ) | ||
|
|
||
| expect(result.buffer).toBeInstanceOf(Buffer) | ||
| expect(result.buffer.length).toBe(maximumResponseBody) | ||
| }) | ||
|
|
||
| it('should succeed when exactly matching maximumResponseBody config on later chunk', async () => { | ||
| const maximumResponseBody = 3_000 // 3KB custom limit | ||
| const chunkSize = 1_000 // 1KB chunks | ||
| const numChunks = 3 // 3KB total | ||
|
|
||
| global.fetch = jest.fn().mockImplementation(() => { | ||
| let chunksRead = 0 | ||
| const mockReadableStream = new ReadableStream({ | ||
| async pull(controller) { | ||
| if (chunksRead < numChunks) { | ||
| controller.enqueue(new Uint8Array(chunkSize)) | ||
| chunksRead++ | ||
| } else { | ||
| controller.close() | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| return Promise.resolve({ | ||
| ok: true, | ||
| status: 200, | ||
| body: mockReadableStream, | ||
| headers: { | ||
| get: jest.fn((header: string) => { | ||
| if (header === 'Content-Type') return 'image/jpeg' | ||
| return null | ||
| }), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| const result = await fetchExternalImage( | ||
| 'http://example.com/custom-limit.jpg', | ||
| false, | ||
| maximumResponseBody | ||
| ) | ||
|
|
||
| expect(result.buffer).toBeInstanceOf(Buffer) | ||
| expect(result.buffer.length).toBe(maximumResponseBody) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this and the other errors be more explicit that it's a max size issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are all generic like this and the server log provides more information