From 2912b5f44c142422161125974f3df1ad62c70e82 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Mon, 14 Sep 2020 12:23:04 +0200 Subject: [PATCH] chore: remove mime dependency Bug: #5026, #6125 --- package.json | 1 - src/common/Page.ts | 26 +++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 7358a4c9f30a0..7eee4284ea33e 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ "devtools-protocol": "0.0.781568", "extract-zip": "^2.0.0", "https-proxy-agent": "^4.0.0", - "mime": "^2.0.3", "pkg-dir": "^4.2.0", "progress": "^2.0.1", "proxy-from-env": "^1.0.0", diff --git a/src/common/Page.ts b/src/common/Page.ts index 211b1bcba8475..c7c59a15445d1 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -17,7 +17,6 @@ import * as fs from 'fs'; import { promisify } from 'util'; import { EventEmitter } from './EventEmitter.js'; -import * as mime from 'mime'; import { Connection, CDPSession, @@ -61,6 +60,26 @@ import { PDFOptions, paperFormats } from './PDFOptions.js'; const writeFileAsync = promisify(fs.writeFile); +/** + * @param filePath - The desired screenshot file path. + * @returns The screenshot type as a string, either 'jpeg' or 'png'. + * Throws an exception for unknown JPEG/PNG file extensions. + */ +const getScreenshotType = (filePath: string) => { + const extension = filePath.slice(filePath.lastIndexOf('.') + 1).toLowerCase(); + switch (extension) { + case 'png': + return 'png'; + case 'jpg': + case 'jpeg': + return 'jpeg'; + default: + throw new Error( + `Unsupported screenshot type for extension: \`.${extension}\`` + ); + } +}; + /** * @public */ @@ -1589,10 +1608,7 @@ export class Page extends EventEmitter { ); screenshotType = options.type; } else if (options.path) { - const mimeType = mime.getType(options.path); - if (mimeType === 'image/png') screenshotType = 'png'; - else if (mimeType === 'image/jpeg') screenshotType = 'jpeg'; - assert(screenshotType, 'Unsupported screenshot mime type: ' + mimeType); + screenshotType = getScreenshotType(options.path); } if (!screenshotType) screenshotType = 'png';