Skip to content

Fix TypeError when processing SVG images with undefined edits #605

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/image-handler/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class ImageRequest {
*/
if (
imageRequestInfo.contentType !== ContentTypes.SVG ||
imageRequestInfo.edits.toFormat ||
imageRequestInfo.edits?.toFormat ||
imageRequestInfo.outputFormat
) {
this.determineOutputFormat(imageRequestInfo, event);
Expand Down
67 changes: 67 additions & 0 deletions source/image-handler/test/image-request/setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,73 @@ describe("setup", () => {
});
expect(imageRequestInfo).toEqual(expectedResult);
});

it("Should handle SVG image with undefined edits", async () => {
// Arrange
const event = {
path: "/image.svg",
};

// Mock - create an imageRequest that will have undefined edits
mockS3Commands.getObject.mockResolvedValue({
ContentType: "image/svg+xml",
Body: mockImageBody,
});

// Act
const imageRequest = new ImageRequest(s3Client, secretProvider);
const imageRequestInfo = await imageRequest.setup(event);
const expectedResult = {
requestType: "Thumbor",
bucket: "validBucket",
key: "image.svg",
edits: {},
originalImage: mockImage,
cacheControl: "max-age=31536000,public",
contentType: "image/svg+xml",
};

// Assert
expect(mockS3Commands.getObject).toHaveBeenCalledWith({
Bucket: "validBucket",
Key: "image.svg",
});
expect(imageRequestInfo).toEqual(expectedResult);
});

it("Should handle SVG image with edits object but no toFormat property", async () => {
// Arrange - create request with edits that has other properties but no toFormat
const event = {
path: "/filters:grayscale()/image.svg",
};

// Mock
mockS3Commands.getObject.mockResolvedValue({
ContentType: "image/svg+xml",
Body: mockImageBody,
});

// Act
const imageRequest = new ImageRequest(s3Client, secretProvider);
const imageRequestInfo = await imageRequest.setup(event);
const expectedResult = {
requestType: "Thumbor",
bucket: "validBucket",
key: "image.svg",
edits: { grayscale: true },
outputFormat: "png",
originalImage: mockImage,
cacheControl: "max-age=31536000,public",
contentType: "image/png",
};

// Assert
expect(mockS3Commands.getObject).toHaveBeenCalledWith({
Bucket: "validBucket",
Key: "image.svg",
});
expect(imageRequestInfo).toEqual(expectedResult);
});
});

it("Should pass and return the customer headers if custom headers are provided", async () => {
Expand Down