Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.
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: 2 additions & 0 deletions packages/core/src/pipes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { Pipe } from "@ipp/common";
import { ConvertPipe } from "./convert";
import { PassthroughPipe } from "./passthrough";
import { ResizePipe } from "./resize";
import { RotatePipe } from "./rotate";

export const PIPES: { [index: string]: Pipe<any> } = {
convert: ConvertPipe,
passthrough: PassthroughPipe,
resize: ResizePipe,
rotate: RotatePipe,
} as const;
71 changes: 71 additions & 0 deletions packages/core/src/pipes/rotate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Image Processing Pipeline - Copyright (c) Marcus Cemes
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { DataObject, sampleMetadata } from "@ipp/common";

import { randomBytes } from "crypto";
import sharp, { OutputInfo, Sharp } from "sharp";

import { RotateOptions, RotatePipe } from "./rotate";

jest.mock("sharp");

type UnPromise<T> = T extends Promise<infer U> ? U : never;

describe("built-in rotate pipe", () => {
/** The input value */
const data: DataObject = {
buffer: randomBytes(8),
metadata: sampleMetadata(256, "jpeg"),
};

const rotateOptions = <RotateOptions>{ angle: 45 };

/** The return value of the mocked sharp.toBuffer() function */
const toBufferResult: UnPromise<ReturnType<Sharp["toBuffer"]>> = {
data: data.buffer,
info: {
width: 256,
height: 256,
channels: data.metadata.current.channels,
size: data.buffer.length,
format: data.metadata.current.format,
premultiplied: false,
} as OutputInfo,
};

/** The expected value */
const newData: DataObject = {
...data,
metadata: {
...data.metadata,
current: {
...data.metadata.current,
width: toBufferResult.info.width,
height: toBufferResult.info.height,
},
},
};

const toBufferMock = jest.fn(async () => toBufferResult);
const rotateMock = jest.fn(() => ({ toBuffer: toBufferMock }));
const sharpMock = sharp as unknown as jest.Mock<{ rotate: typeof rotateMock }>;
const mocks = [toBufferMock, rotateMock, sharpMock];

beforeAll(() => sharpMock.mockImplementation(() => ({ rotate: rotateMock })));
afterAll(() => sharpMock.mockRestore());
afterEach(() => mocks.forEach((m) => m.mockClear()));

test("rotate image", async () => {
const result = RotatePipe(data, rotateOptions);

await expect(result).resolves.toMatchObject<DataObject>(newData);

expect(rotateMock).toHaveBeenCalledWith(rotateOptions.angle, rotateOptions?.rotateOptions);
expect(toBufferMock).toHaveBeenCalledWith({ resolveWithObject: true });
});
});
43 changes: 43 additions & 0 deletions packages/core/src/pipes/rotate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Image Processing Pipeline - Copyright (c) Marcus Cemes
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { Pipe } from "@ipp/common";

import produce from "immer";
import sharp, { RotateOptions as SharpOptions } from "sharp";

sharp.concurrency(1);

export interface RotateOptions {
angle?: number;
rotateOptions?: SharpOptions;
}

/**
* A built-in pipe that lets you rotate an image
* This can be used to rotate the image properly before the EXIF data is removed from CompressPipe
*/
export const RotatePipe: Pipe<RotateOptions> = async (data, options = {}) => {
const {
data: newBuffer,
info: { width, height, format, channels },
} = await sharp(data.buffer)
.rotate(options.angle, options.rotateOptions)
.toBuffer({ resolveWithObject: true });

const newMetadata = produce(data.metadata, (draft) => {
draft.current.width = width;
draft.current.height = height;
draft.current.channels = channels;
draft.current.format = format;
});

return {
buffer: newBuffer,
metadata: newMetadata,
};
};