Skip to content

Commit

Permalink
chore(screenshots): remove unsupported parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cherfia committed Sep 9, 2024
1 parent cc1483f commit 787b344
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 198 deletions.
5 changes: 4 additions & 1 deletion src/chromium/interfaces/screenshot.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export type ImageProperties = {
clip?: boolean; // Define whether to clip the screenshot according to the device dimensions (default false).
};

export type ScreenshotOptions = ChromiumOptions & {
export type ScreenshotOptions = Omit<
ChromiumOptions,
'assets' | 'header' | 'footer'
> & {
properties?: ImageProperties;
optimizeForSpeed?: boolean; // Define whether to optimize image encoding for speed, not for resulting size.
};
18 changes: 0 additions & 18 deletions src/chromium/screenshots/html.screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export class HtmlScreenshot extends Screenshot {
*
* @param {Object} options - Screenshot options.
* @param {PathLikeOrReadStream} options.html - PathLike or ReadStream of the HTML content to be screenshoted.
* @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
* @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
* @param {ImageProperties} [options.properties] - Image properties for the screenshot.
* @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the screenshot.
* @param {string} [options.waitDelay] - Delay before the screenshot process starts.
Expand All @@ -42,9 +40,6 @@ export class HtmlScreenshot extends Screenshot {
*/
async capture({
html,
assets,
header,
footer,
properties,
emulatedMediaType,
waitDelay,
Expand All @@ -56,9 +51,6 @@ export class HtmlScreenshot extends Screenshot {
optimizeForSpeed
}: {
html: PathLikeOrReadStream;
assets?: { file: PathLikeOrReadStream; name: string }[];
header?: PathLikeOrReadStream;
footer?: PathLikeOrReadStream;
properties?: ImageProperties;
emulatedMediaType?: EmulatedMediaType;
waitDelay?: string;
Expand All @@ -73,17 +65,7 @@ export class HtmlScreenshot extends Screenshot {

await GotenbergUtils.addFile(data, html, 'index.html');

if (assets?.length) {
await Promise.all(
assets.map(({ file, name }) =>
GotenbergUtils.addFile(data, file, name)
)
);
}

await ScreenshotUtils.customize(data, {
header,
footer,
properties,
emulatedMediaType,
waitDelay,
Expand Down
6 changes: 0 additions & 6 deletions src/chromium/screenshots/markdown.screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export class MarkdownScreenshot extends Screenshot {
* @param {Object} options - Screenshot options.
* @param {PathLikeOrReadStream} options.html - PathLike or ReadStream of the HTML content to be screenshoted.
* @param {PathLikeOrReadStream} options.markdown - PathLike or ReadStream of the Markdown content to be screenshoted.
* @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
* @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
* @param {ImageProperties} [options.properties] - Image properties for the screenshot.
* @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the screenshot.
* @param {string} [options.waitDelay] - Delay before the screenshot process starts.
Expand All @@ -44,8 +42,6 @@ export class MarkdownScreenshot extends Screenshot {
async capture({
html,
markdown,
header,
footer,
properties,
emulatedMediaType,
waitDelay,
Expand Down Expand Up @@ -77,8 +73,6 @@ export class MarkdownScreenshot extends Screenshot {
await GotenbergUtils.addFile(data, markdown, 'file.md');

await ScreenshotUtils.customize(data, {
header,
footer,
properties,
emulatedMediaType,
waitDelay,
Expand Down
47 changes: 1 addition & 46 deletions src/chromium/screenshots/tests/html.screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ describe('HtmlScreenshot', () => {
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');

const assets = [
{ file: Buffer.from('asset1'), name: 'asset1' },
{ file: Buffer.from('asset2'), name: 'asset2' }
];

beforeEach(() => {
(createReadStream as jest.Mock) = jest.fn();
});
Expand Down Expand Up @@ -60,43 +55,6 @@ describe('HtmlScreenshot', () => {
});
});

describe('when header parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
html: Buffer.from('data'),
header: Buffer.from('header')
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when footer parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
html: Buffer.from('data'),
footer: Buffer.from('footer')
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when assets parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
html: Buffer.from('data'),
assets
});

expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when emulatedMediaType parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
Expand Down Expand Up @@ -151,17 +109,14 @@ describe('HtmlScreenshot', () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
html: Buffer.from('data'),
assets,
header: Buffer.from('header'),
footer: Buffer.from('footer'),
emulatedMediaType: 'screen',
failOnHttpStatusCodes: [499, 599],
skipNetworkIdleEvent: true,
failOnConsoleExceptions: true,
properties: { format: 'jpeg', quality: 50 },
optimizeForSpeed: true
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(12);
expect(mockFormDataAppend).toHaveBeenCalledTimes(8);
expect(buffer).toEqual(Buffer.from('content'));
});
});
Expand Down
30 changes: 1 addition & 29 deletions src/chromium/screenshots/tests/markdown.screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,6 @@ describe('MarkdownScreenshot', () => {
});
});

describe('when header parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
html: Buffer.from('data'),
markdown: Buffer.from('markdown'),
header: Buffer.from('header')
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when footer parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
html: Buffer.from('data'),
markdown: Buffer.from('markdown'),
footer: Buffer.from('footer')
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when emulatedMediaType parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValue(new Response('content'));
Expand Down Expand Up @@ -129,16 +103,14 @@ describe('MarkdownScreenshot', () => {
const buffer = await screenshot.capture({
html: Buffer.from('data'),
markdown: Buffer.from('markdown'),
header: Buffer.from('header'),
footer: Buffer.from('footer'),
emulatedMediaType: 'screen',
failOnHttpStatusCodes: [499, 599],
skipNetworkIdleEvent: true,
failOnConsoleExceptions: true,
properties: { format: 'jpeg', quality: 50 },
optimizeForSpeed: true
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(11);
expect(mockFormDataAppend).toHaveBeenCalledTimes(9);
expect(buffer).toEqual(Buffer.from('content'));
});
});
Expand Down
28 changes: 1 addition & 27 deletions src/chromium/screenshots/tests/url.screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,6 @@ describe('URLScreenshot', () => {
});
});

describe('when header parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValueOnce(new Response('content'));
const buffer = await screenshot.capture({
url: 'http://www.example.com/',
header: Buffer.from('header')
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when footer parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValueOnce(new Response('content'));
const buffer = await screenshot.capture({
url: 'http://www.example.com/',
footer: Buffer.from('footer')
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from('content'));
});
});

describe('when image properties parameter is passed', () => {
it('should return a buffer', async () => {
mockFetch.mockResolvedValueOnce(new Response('content'));
Expand Down Expand Up @@ -130,8 +106,6 @@ describe('URLScreenshot', () => {
mockFetch.mockResolvedValue(new Response('content'));
const buffer = await screenshot.capture({
url: 'http://www.example.com/',
header: Buffer.from('header'),
footer: Buffer.from('footer'),
emulatedMediaType: 'screen',
failOnHttpStatusCodes: [499, 599],
skipNetworkIdleEvent: true,
Expand All @@ -146,7 +120,7 @@ describe('URLScreenshot', () => {
}
]
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(11);
expect(mockFormDataAppend).toHaveBeenCalledTimes(9);
expect(buffer).toEqual(Buffer.from('content'));
});
});
Expand Down
10 changes: 1 addition & 9 deletions src/chromium/screenshots/url.screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URL } from 'url';
import FormData from 'form-data';
import { GotenbergUtils, PathLikeOrReadStream } from '../../common';
import { GotenbergUtils } from '../../common';
import { ImageProperties } from '../interfaces/screenshot.types';
import { ScreenshotUtils } from '../utils/screenshot.utils';
import { Screenshot } from './screenshot';
Expand All @@ -27,8 +27,6 @@ export class UrlScreenshot extends Screenshot {
*
* @param {Object} options - Screenshot options.
* @param {string} options.url - The URL of the content to be screenshoted
* @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
* @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
* @param {ImageProperties} [options.properties] - Image properties for the screenshot.
* @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the screenshot.
* @param {string} [options.waitDelay] - Delay before the screenshot process starts.
Expand All @@ -43,8 +41,6 @@ export class UrlScreenshot extends Screenshot {
*/
async capture({
url,
header,
footer,
properties,
emulatedMediaType,
waitDelay,
Expand All @@ -57,8 +53,6 @@ export class UrlScreenshot extends Screenshot {
cookies
}: {
url: string;
header?: PathLikeOrReadStream;
footer?: PathLikeOrReadStream;
properties?: ImageProperties;
emulatedMediaType?: EmulatedMediaType;
waitDelay?: string;
Expand All @@ -76,8 +70,6 @@ export class UrlScreenshot extends Screenshot {
data.append('url', _url.href);

await ScreenshotUtils.customize(data, {
header,
footer,
properties,
emulatedMediaType,
waitDelay,
Expand Down
10 changes: 0 additions & 10 deletions src/chromium/utils/screenshot.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ export class ScreenshotUtils {
data: FormData,
options: ScreenshotOptions
): Promise<void> {
if (options.header) {
const { header } = options;
await GotenbergUtils.addFile(data, header, 'header.html');
}

if (options.footer) {
const { footer } = options;
await GotenbergUtils.addFile(data, footer, 'footer.html');
}

if (options.emulatedMediaType) {
data.append('emulatedMediaType', options.emulatedMediaType);
}
Expand Down
Loading

0 comments on commit 787b344

Please sign in to comment.