-
Notifications
You must be signed in to change notification settings - Fork 0
Webp and animated media #68
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
7 commits
Select commit
Hold shift + click to select a range
2b65429
add webp to list of scaled media types
jdub233 111cb81
add the animated flag to all resizes
jdub233 688dd04
remove unused function (not related to webp or animation)
jdub233 68fd453
add additional tests
jdub233 a7dbd99
add fallback for sharp pixel limit and unit tests
jdub233 ec8c0ba
add staging directory to gitignore
jdub233 b24f02f
version bump
jdub233 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| .DS_Store | ||
| dist | ||
| node_modules | ||
| staging | ||
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,160 @@ | ||
| /* eslint-disable import/no-extraneous-dependencies */ | ||
| import { | ||
| describe, it, expect, vi, beforeEach, | ||
| } from 'vitest'; | ||
| import { mockClient } from 'aws-sdk-client-mock'; | ||
| import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
| import { Readable } from 'stream'; | ||
|
|
||
| // Import the function to test. | ||
| // Vitest hoists the vi.hoisted() and vi.mock() calls below above this import, | ||
| // so the bucket name is set and sharp is mocked before resizeAndSave.js loads. | ||
| import { resizeAndSave } from './resizeAndSave.js'; | ||
|
|
||
| // resizeAndSave.js reads the bucket name once, when it loads. | ||
| vi.hoisted(() => { | ||
| process.env.ORIGINAL_BUCKET = 'test-bucket'; | ||
| }); | ||
|
|
||
| // Mock sharp, so that these tests can make the resize succeed or fail on demand. | ||
| // These tests cover our own fallback logic, not sharp's image processing. | ||
| // getOrCreateObject.test.js still runs the real sharp library against real image bytes. | ||
| const { sharpMock, sharpPipeline } = vi.hoisted(() => { | ||
| // sharp's resize() and withMetadata() return the pipeline, so that calls can be chained. | ||
| const pipeline = {}; | ||
| pipeline.resize = vi.fn(() => pipeline); | ||
| pipeline.withMetadata = vi.fn(() => pipeline); | ||
| pipeline.toBuffer = vi.fn(async () => Buffer.from('resized-image-bytes')); | ||
|
|
||
| const sharpFn = vi.fn(() => pipeline); | ||
| // resizeAndSave reads sharp.position when a crop is requested. | ||
| sharpFn.position = { | ||
| top: 1, right: 2, bottom: 3, left: 4, centre: 5, | ||
| }; | ||
|
|
||
| return { sharpMock: sharpFn, sharpPipeline: pipeline }; | ||
| }); | ||
|
|
||
| vi.mock('sharp', () => ({ default: sharpMock })); | ||
|
|
||
| // Mock the s3 client. | ||
| const s3Mock = mockClient(S3Client); | ||
|
|
||
| // A size match for a 150x150 gif, in the shape the regex in getOrCreateObject produces. | ||
| const sizeMatch = ['-150x150.gif', '150', '150', 'gif']; | ||
|
|
||
| // Build a getObject response, with the image bytes as a readable stream. | ||
| const s3Response = (bytes) => ({ | ||
| Body: Readable.from(Buffer.from(bytes)), | ||
| ContentType: 'image/gif', | ||
| }); | ||
|
|
||
| describe('resizeAndSave', () => { | ||
| beforeEach(() => { | ||
| s3Mock.reset(); | ||
| s3Mock.on(PutObjectCommand).resolves({}); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should resize the image, save it to S3, and return the sharp pipeline', async () => { | ||
| const result = await resizeAndSave( | ||
| s3Response('original-image-bytes'), | ||
| '/www.bu.edu/somesite/files/01/example.gif', | ||
| sizeMatch, | ||
| false, | ||
| ); | ||
|
|
||
| // The original bytes and the animated flag go to sharp, | ||
| // and the dimensions parsed from the size match go to resize. | ||
| expect(sharpMock).toHaveBeenCalledWith(Buffer.from('original-image-bytes'), { animated: true }); | ||
| expect(sharpPipeline.resize).toHaveBeenCalledWith({ width: 150, height: 150 }); | ||
|
|
||
| // The resized bytes are saved for future requests, under the size suffixed key. | ||
| const putCalls = s3Mock.commandCalls(PutObjectCommand); | ||
| expect(putCalls).toHaveLength(1); | ||
| expect(putCalls[0].args[0].input).toEqual({ | ||
| Bucket: 'test-bucket', | ||
| Key: 'rendered_media/www.bu.edu/somesite/files/01/example-150x150.gif', | ||
| Body: Buffer.from('resized-image-bytes'), | ||
| ContentType: 'image/gif', | ||
| Metadata: { | ||
| 'original-key': 'original_media/www.bu.edu/somesite/files/01/example.gif', | ||
| }, | ||
| }); | ||
|
|
||
| // The pipeline itself is returned, for the caller to use as the response body. | ||
| expect(result).toBe(sharpPipeline); | ||
| }); | ||
|
|
||
| it('should return the original image and save nothing when it exceeds the pixel limit', async () => { | ||
| // This is the error sharp throws for an image with more pixels than its limit. | ||
| // Animated images can hit it because every frame counts toward the limit. | ||
| sharpPipeline.toBuffer.mockRejectedValueOnce(new Error('Input image exceeds pixel limit')); | ||
|
|
||
| const result = await resizeAndSave( | ||
| s3Response('too-many-pixels'), | ||
| '/www.bu.edu/somesite/files/01/huge.gif', | ||
| sizeMatch, | ||
| false, | ||
| ); | ||
|
|
||
| // The original, unresized bytes are returned. | ||
| expect(Buffer.isBuffer(result)).toBe(true); | ||
| expect(result.toString()).toBe('too-many-pixels'); | ||
|
|
||
| // Nothing is saved to S3, because there is no resized image to save. | ||
| expect(s3Mock.commandCalls(PutObjectCommand)).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should rethrow any other resize failure', async () => { | ||
| sharpPipeline.toBuffer.mockRejectedValueOnce(new Error('Input buffer contains unsupported image format')); | ||
|
|
||
| await expect(resizeAndSave( | ||
| s3Response('not-an-image'), | ||
| '/www.bu.edu/somesite/files/01/broken.gif', | ||
| sizeMatch, | ||
| false, | ||
| )).rejects.toThrow('unsupported image format'); | ||
|
|
||
| expect(s3Mock.commandCalls(PutObjectCommand)).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should resize with the crop position and include the crop in the saved key', async () => { | ||
| await resizeAndSave( | ||
| s3Response('original-image-bytes'), | ||
| '/www.bu.edu/somesite/files/01/example.gif', | ||
| sizeMatch, | ||
| 'top', | ||
| ); | ||
|
|
||
| expect(sharpPipeline.resize).toHaveBeenCalledWith({ | ||
| width: 150, | ||
| height: 150, | ||
| fit: 'cover', | ||
| position: sharpMock.position.top, | ||
| }); | ||
|
|
||
| expect(s3Mock.commandCalls(PutObjectCommand)[0].args[0].input.Key) | ||
| .toBe('rendered_media/www.bu.edu/somesite/files/01/example-150x150*crop-top.gif'); | ||
| }); | ||
| }); | ||
|
|
||
| // This is the guard for the message matched in resizeAndSave. | ||
| // The message comes from sharp's native code, so a sharp upgrade could change it. | ||
| // limitInputPixels provokes the same error from a tiny image, with no large fixture needed. | ||
| describe('the sharp pixel limit error', () => { | ||
| it('should still contain the message that resizeAndSave matches on', async () => { | ||
| const { default: realSharp } = await vi.importActual('sharp'); | ||
|
|
||
| const tinyPng = await realSharp({ | ||
| create: { | ||
| width: 4, height: 4, channels: 3, background: { r: 255, g: 0, b: 0 }, | ||
| }, | ||
| }).png().toBuffer(); | ||
|
|
||
| await expect(realSharp(tinyPng, { animated: true, limitInputPixels: 1 }) | ||
| .resize({ width: 2, height: 2 }) | ||
| .withMetadata() | ||
| .toBuffer()).rejects.toThrow('exceeds pixel limit'); | ||
| }); | ||
| }); |
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
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.
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.
👏 This is a great fallback!