From 3b8f51fb105c6d165cead1a5b02b0acee483c6b0 Mon Sep 17 00:00:00 2001 From: Martin Skec Date: Wed, 20 Dec 2023 16:10:58 +0100 Subject: [PATCH] Dev tools (#195) * style: added prettier * chore: added husky and lint-staged to run prettier with pre-commit hook --- .husky/pre-commit | 4 + .prettierrc.json | 7 + package.json | 11 +- src/graphics.ts | 73 +++-- src/index.ts | 12 +- src/pdf2picCore.ts | 47 ++- src/types/convert.ts | 8 +- src/types/convertResponse.ts | 2 +- src/types/options.ts | 2 +- src/utils/converters/base64ToStream.ts | 6 +- src/utils/converters/bufferToStream.ts | 6 +- src/utils/converters/convertToStream.ts | 14 +- src/utils/defaultOptions.ts | 10 +- src/utils/getPages.ts | 8 +- src/utils/resolveResponseType.ts | 6 +- test/graphics.test.ts | 180 ++++++------ test/index.test.ts | 375 ++++++++++++------------ test/resolveResponseType.test.ts | 18 +- 18 files changed, 410 insertions(+), 379 deletions(-) create mode 100755 .husky/pre-commit create mode 100644 .prettierrc.json diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..d24fdfc --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +npx lint-staged diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..92688f0 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,7 @@ +{ + "trailingComma": "all", + "tabWidth": 2, + "semi": true, + "printWidth": 120, + "singleQuote": true +} diff --git a/package.json b/package.json index 832a639..320641c 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,11 @@ "@typescript-eslint/parser": "^6.2.1", "chai": "^4.3.7", "eslint": "^8.46.0", + "husky": "^8.0.3", + "lint-staged": "^15.2.0", "mocha": "^10.2.0", "nyc": "^15.1.0", + "prettier": "^3.1.1", "rimraf": "^5.0.1", "rollup": "^3.27.2", "ts-node": "^10.9.1", @@ -37,7 +40,8 @@ "rollup": "rollup -c", "clean": "rimraf ./dist", "lint": "eslint --ext .ts .", - "build": "npm run test && npm run clean && npm run rollup" + "build": "npm run test && npm run clean && npm run rollup", + "prepare": "husky install" }, "engines": { "node": ">=14" @@ -72,5 +76,8 @@ "bugs": { "url": "https://github.com/yakovmeister/pdf2image/issues" }, - "homepage": "https://github.com/yakovmeister/pdf2image#readme" + "homepage": "https://github.com/yakovmeister/pdf2image#readme", + "lint-staged": { + "*.{js,ts}": ["prettier --write", "eslint --fix"] + } } diff --git a/src/graphics.ts b/src/graphics.ts index b7c7d68..62ea9c9 100644 --- a/src/graphics.ts +++ b/src/graphics.ts @@ -1,13 +1,13 @@ -import gm from "gm"; -import path from "path"; -import fs from "fs"; +import gm from 'gm'; +import path from 'path'; +import fs from 'fs'; import { BufferResponse, ToBase64Response, WriteImageResponse } from './types/convertResponse'; -import { Options } from "./types/options"; +import { Options } from './types/options'; export class Graphics { private quality = 0; - private format = "png"; + private format = 'png'; private width = 768; @@ -17,21 +17,21 @@ export class Graphics { private density = 72; - private savePath = "./"; + private savePath = './'; - private saveFilename = "untitled"; + private saveFilename = 'untitled'; - private compression = "jpeg"; + private compression = 'jpeg'; private gm: gm.SubClass = gm.subClass({ imageMagick: false }); public generateValidFilename(page?: number): string { let filePath = path.join(this.savePath, this.saveFilename); if (this.savePath.startsWith('./')) { - filePath = `./${filePath}` + filePath = `./${filePath}`; } - if (typeof page === "number") { + if (typeof page === 'number') { filePath = `${filePath}.${page + 1}`; } @@ -43,13 +43,13 @@ export class Graphics { .density(this.density, this.density) .resize(this.width, this.height, this.preserveAspectRatio ? '^' : '!') .quality(this.quality) - .compress(this.compression) + .compress(this.compression); } public async toBase64(stream: fs.ReadStream, page?: number): Promise { const { buffer, size, page: pageResponse } = await this.toBuffer(stream, page); - return { base64: buffer.toString("base64"), size, page: pageResponse } + return { base64: buffer.toString('base64'), size, page: pageResponse }; } public toBuffer(stream: fs.ReadStream, page?: number): Promise { @@ -67,11 +67,11 @@ export class Graphics { .on('data', (data) => { buffers.push(data); }) - .on("end", () => { + .on('end', () => { return resolve({ buffer: Buffer.concat(buffers), size: `${this.width}x${this.height}`, - page: page + 1 + page: page + 1, }); }); }); @@ -83,20 +83,19 @@ export class Graphics { const pageSetup = `${stream.path}[${page}]`; return new Promise((resolve, reject) => { - this.gmBaseCommand(stream, pageSetup) - .write(output, (error) => { - if (error) { - return reject(error); - } + this.gmBaseCommand(stream, pageSetup).write(output, (error) => { + if (error) { + return reject(error); + } - return resolve({ - name: path.basename(output), - size: `${this.width}x${this.height}`, - fileSize: fs.statSync(output).size / 1000.0, - path: output, - page: page + 1 - }); + return resolve({ + name: path.basename(output), + size: `${this.width}x${this.height}`, + fileSize: fs.statSync(output).size / 1000.0, + path: output, + page: page + 1, }); + }); }); } @@ -110,7 +109,7 @@ export class Graphics { return reject(error); } - return resolve(data.replace(/^[\w\W]*?1/, "1")); + return resolve(data.replace(/^[\w\W]*?1/, '1')); }); } else { image.identify((error, data) => { @@ -119,7 +118,7 @@ export class Graphics { } return resolve(data); - }) + }); } }); } @@ -174,13 +173,13 @@ export class Graphics { } public setGMClass(gmClass: string | boolean): Graphics { - if (typeof gmClass === "boolean") { + if (typeof gmClass === 'boolean') { this.gm = gm.subClass({ imageMagick: gmClass }); return this; } - if (gmClass.toLocaleLowerCase() === "imagemagick") { + if (gmClass.toLocaleLowerCase() === 'imagemagick') { this.gm = gm.subClass({ imageMagick: true }); return this; @@ -193,15 +192,15 @@ export class Graphics { public getOptions(): Options { return { - quality: this.quality, - format: this.format, - width: this.width, - height: this.height, + quality: this.quality, + format: this.format, + width: this.width, + height: this.height, preserveAspectRatio: this.preserveAspectRatio, - density: this.density, - savePath: this.savePath, + density: this.density, + savePath: this.savePath, saveFilename: this.saveFilename, - compression: this.compression + compression: this.compression, }; } } diff --git a/src/index.ts b/src/index.ts index c939b90..594dba4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,15 @@ -import { Convert } from "./types/convert"; -import { defaultOptions } from "./utils/defaultOptions"; -import { pdf2picCore } from "./pdf2picCore"; +import { Convert } from './types/convert'; +import { defaultOptions } from './utils/defaultOptions'; +import { pdf2picCore } from './pdf2picCore'; export function fromPath(filePath: string, options = defaultOptions): Convert { - return pdf2picCore("path", filePath, options); + return pdf2picCore('path', filePath, options); } export function fromBuffer(buffer: Buffer, options = defaultOptions): Convert { - return pdf2picCore("buffer", buffer, options); + return pdf2picCore('buffer', buffer, options); } export function fromBase64(b64string: string, options = defaultOptions): Convert { - return pdf2picCore("base64", b64string, options); + return pdf2picCore('base64', b64string, options); } diff --git a/src/pdf2picCore.ts b/src/pdf2picCore.ts index 45ce00b..540c0bf 100644 --- a/src/pdf2picCore.ts +++ b/src/pdf2picCore.ts @@ -1,12 +1,12 @@ import fs from 'fs'; -import { Graphics } from "./graphics"; -import type { Convert, ConvertOptions } from "./types/convert"; +import { Graphics } from './graphics'; +import type { Convert, ConvertOptions } from './types/convert'; import type { ConvertResponse } from './types/convertResponse'; -import type { Options } from "./types/options"; +import type { Options } from './types/options'; import { bufferToStream } from './utils/converters/bufferToStream'; -import { convertToBuffer } from "./utils/converters/convertToBuffer"; +import { convertToBuffer } from './utils/converters/convertToBuffer'; import { convertToStream } from './utils/converters/convertToStream'; -import { defaultOptions } from "./utils/defaultOptions"; +import { defaultOptions } from './utils/defaultOptions'; import { getPages } from './utils/getPages'; import { resolveResponseType } from './utils/resolveResponseType'; @@ -17,45 +17,44 @@ export function pdf2picCore(source: string, data: string | Buffer, options = def const _convert = (stream: fs.ReadStream, page: number, convertOptions: ConvertOptions): Promise => { if (page < 1) { - throw new Error("Page number should be more than or equal 1"); + throw new Error('Page number should be more than or equal 1'); } - const responseType = resolveResponseType(convertOptions) + const responseType = resolveResponseType(convertOptions); switch (responseType) { case 'base64': - return gm.toBase64(stream, (page - 1)) + return gm.toBase64(stream, page - 1); case 'image': - return gm.writeImage(stream, (page - 1)) + return gm.writeImage(stream, page - 1); case 'buffer': - return gm.toBuffer(stream, (page - 1)) + return gm.toBuffer(stream, page - 1); default: - throw new Error(`Invalid responseType: ${responseType}`) + throw new Error(`Invalid responseType: ${responseType}`); } - } + }; const _bulk = (stream, pages, convertOptions) => { - return Promise.all(pages.map(page => _convert(stream, page, convertOptions))); - } + return Promise.all(pages.map((page) => _convert(stream, page, convertOptions))); + }; const convert = (page = 1, convertOptions) => { const stream = convertToStream(source, data); - return _convert(stream, page, convertOptions) + return _convert(stream, page, convertOptions); }; convert.bulk = async (pages, convertOptions) => { const buffer = await convertToBuffer(source, data); - const pagesToConvert = pages === -1 - ? await getPages(gm, bufferToStream(buffer)) - : Array.isArray(pages) ? pages : [pages]; + const pagesToConvert = + pages === -1 ? await getPages(gm, bufferToStream(buffer)) : Array.isArray(pages) ? pages : [pages]; - const results = [] - const batchSize = 10 + const results = []; + const batchSize = 10; for (let i = 0; i < pagesToConvert.length; i += batchSize) { - results.push(...await _bulk(bufferToStream(buffer), pagesToConvert.slice(i, i + batchSize), convertOptions)) + results.push(...(await _bulk(bufferToStream(buffer), pagesToConvert.slice(i, i + batchSize), convertOptions))); } - return results - } + return results; + }; convert.setOptions = (): void => setGMOptions(gm, options); @@ -78,7 +77,7 @@ function setGMOptions(gm: Graphics, options: Options): void { .setDensity(options.density) .setSavePath(options.savePath) .setSaveFilename(options.saveFilename) - .setCompression(options.compression) + .setCompression(options.compression); return; } diff --git a/src/types/convert.ts b/src/types/convert.ts index 532672c..e7571c4 100644 --- a/src/types/convert.ts +++ b/src/types/convert.ts @@ -1,9 +1,9 @@ import type { BufferResponse, ToBase64Response, WriteImageResponse } from './convertResponse'; -export type ResponseType = 'image' | 'base64' | 'buffer' +export type ResponseType = 'image' | 'base64' | 'buffer'; export type ConvertOptions = { - responseType: ResponseType -} + responseType: ResponseType; +}; export type Convert = { (pages?: number, options?: undefined): Promise; @@ -23,4 +23,4 @@ export type Convert = { setOptions: () => void; setGMClass: (gmClass: string | boolean) => void; -} +}; diff --git a/src/types/convertResponse.ts b/src/types/convertResponse.ts index a72ba0f..ce8e7fc 100644 --- a/src/types/convertResponse.ts +++ b/src/types/convertResponse.ts @@ -17,4 +17,4 @@ export interface BufferResponse extends BaseResponse { buffer?: Buffer; } -export type ConvertResponse = WriteImageResponse | ToBase64Response | BufferResponse +export type ConvertResponse = WriteImageResponse | ToBase64Response | BufferResponse; diff --git a/src/types/options.ts b/src/types/options.ts index 28fada4..7ede55c 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -8,4 +8,4 @@ export type Options = { savePath?: string; saveFilename?: string; compression?: string; -} +}; diff --git a/src/utils/converters/base64ToStream.ts b/src/utils/converters/base64ToStream.ts index 6815bc0..e7823f0 100644 --- a/src/utils/converters/base64ToStream.ts +++ b/src/utils/converters/base64ToStream.ts @@ -1,8 +1,8 @@ -import { ReadStream } from "fs"; -import { bufferToStream } from "../../utils/converters/bufferToStream"; +import { ReadStream } from 'fs'; +import { bufferToStream } from '../../utils/converters/bufferToStream'; export function base64ToStream(base64: string): ReadStream { - const buffer = Buffer.from(base64, "base64"); + const buffer = Buffer.from(base64, 'base64'); return bufferToStream(buffer); } diff --git a/src/utils/converters/bufferToStream.ts b/src/utils/converters/bufferToStream.ts index 0ec40f7..4c55cbd 100644 --- a/src/utils/converters/bufferToStream.ts +++ b/src/utils/converters/bufferToStream.ts @@ -1,12 +1,12 @@ -import { ReadStream } from "fs"; -import { Readable } from "stream"; +import { ReadStream } from 'fs'; +import { Readable } from 'stream'; export function bufferToStream(buffer: Buffer): ReadStream { const readableInstanceStream = new Readable({ read() { this.push(buffer); this.push(null); - } + }, }); return readableInstanceStream as ReadStream; diff --git a/src/utils/converters/convertToStream.ts b/src/utils/converters/convertToStream.ts index 2c8bfb2..b16a628 100644 --- a/src/utils/converters/convertToStream.ts +++ b/src/utils/converters/convertToStream.ts @@ -1,19 +1,19 @@ -import { createReadStream, ReadStream } from "fs"; -import { base64ToStream } from "../../utils/converters/base64ToStream"; -import { bufferToStream } from "../../utils/converters/bufferToStream"; +import { createReadStream, ReadStream } from 'fs'; +import { base64ToStream } from '../../utils/converters/base64ToStream'; +import { bufferToStream } from '../../utils/converters/bufferToStream'; export function convertToStream(source: string, file: string | Buffer): ReadStream { - if (source === "buffer") { + if (source === 'buffer') { return bufferToStream(file as Buffer); } - if (source === "path") { + if (source === 'path') { return createReadStream(file as string); } - if (source === "base64") { + if (source === 'base64') { return base64ToStream(file as string); } - throw new Error("Cannot recognize specified source"); + throw new Error('Cannot recognize specified source'); } diff --git a/src/utils/defaultOptions.ts b/src/utils/defaultOptions.ts index e17417d..c662662 100644 --- a/src/utils/defaultOptions.ts +++ b/src/utils/defaultOptions.ts @@ -1,13 +1,13 @@ -import { Options } from "../types/options"; +import { Options } from '../types/options'; export const defaultOptions: Options = { quality: 0, - format: "png", + format: 'png', width: 768, height: 512, density: 72, preserveAspectRatio: false, - savePath: "./", - saveFilename: "untitled", - compression: "jpeg" + savePath: './', + saveFilename: 'untitled', + compression: 'jpeg', }; diff --git a/src/utils/getPages.ts b/src/utils/getPages.ts index a476352..621d3b0 100644 --- a/src/utils/getPages.ts +++ b/src/utils/getPages.ts @@ -1,8 +1,8 @@ -import { ReadStream } from "fs"; -import { Graphics } from "../graphics"; +import { ReadStream } from 'fs'; +import { Graphics } from '../graphics'; export async function getPages(gm: Graphics, pdf_path: ReadStream): Promise { - const page = (await gm.identify(pdf_path, "%p ") as string) + const page = (await gm.identify(pdf_path, '%p ')) as string; - return page.split(" ").map((pageNumber) => parseInt(pageNumber, 10)); + return page.split(' ').map((pageNumber) => parseInt(pageNumber, 10)); } diff --git a/src/utils/resolveResponseType.ts b/src/utils/resolveResponseType.ts index 2a759ce..4f9b03a 100644 --- a/src/utils/resolveResponseType.ts +++ b/src/utils/resolveResponseType.ts @@ -2,7 +2,7 @@ import { ConvertOptions, ResponseType } from '@module/types/convert'; export const resolveResponseType = (convertOptions?: ConvertOptions): ResponseType => { if (convertOptions && typeof convertOptions !== 'object') { - throw new Error(`Invalid convertOptions type: ${convertOptions}`) + throw new Error(`Invalid convertOptions type: ${convertOptions}`); } - return convertOptions?.responseType ?? 'image' -} + return convertOptions?.responseType ?? 'image'; +}; diff --git a/test/graphics.test.ts b/test/graphics.test.ts index e7455ed..8805dbf 100644 --- a/test/graphics.test.ts +++ b/test/graphics.test.ts @@ -1,129 +1,129 @@ -import chai, { expect } from "chai"; -import { mkdirSync, createReadStream, writeFileSync } from "fs"; -import { rimrafSync } from "rimraf"; -import gm from "gm"; -import { Graphics } from "../src/graphics"; +import chai, { expect } from 'chai'; +import { mkdirSync, createReadStream, writeFileSync } from 'fs'; +import { rimrafSync } from 'rimraf'; +import gm from 'gm'; +import { Graphics } from '../src/graphics'; -describe("graphics", () => { +describe('graphics', () => { before(() => { - rimrafSync("./dump/savefiletest"); + rimrafSync('./dump/savefiletest'); - mkdirSync("./dump/savefiletest", { recursive: true }); + mkdirSync('./dump/savefiletest', { recursive: true }); }); - it("should return page numbers", async () => { + it('should return page numbers', async () => { const gm = new Graphics(); - const pageNumbers = await gm.identify("./test/data/pdf1.pdf", "%p "); + const pageNumbers = await gm.identify('./test/data/pdf1.pdf', '%p '); - expect(pageNumbers).to.be.equal("1 2 3 4 5 6 7 8 9"); + expect(pageNumbers).to.be.equal('1 2 3 4 5 6 7 8 9'); }); - it("should return page numbers (from stream)", async () => { + it('should return page numbers (from stream)', async () => { const gm = new Graphics(); - const file = createReadStream("./test/data/pdf1.pdf"); + const file = createReadStream('./test/data/pdf1.pdf'); - const pageNumbers = await gm.identify(file, "%p "); + const pageNumbers = await gm.identify(file, '%p '); - expect(pageNumbers).to.be.equal("1 2 3 4 5 6 7 8 9"); + expect(pageNumbers).to.be.equal('1 2 3 4 5 6 7 8 9'); }); - it("should return file information", async () => { + it('should return file information', async () => { const gm = new Graphics(); - const result = await gm.identify("./test/data/pdf2.pdf"); + const result = await gm.identify('./test/data/pdf2.pdf'); - expect(result).to.have.haveOwnProperty("Format"); - expect((result as gm.ImageInfo).format).to.be.equal("PDF"); + expect(result).to.have.haveOwnProperty('Format'); + expect((result as gm.ImageInfo).format).to.be.equal('PDF'); }); - it("should return all of the options (default options)", () => { + it('should return all of the options (default options)', () => { const gm = new Graphics(); const options = gm.getOptions(); - expect(options).to.haveOwnProperty("quality"); + expect(options).to.haveOwnProperty('quality'); expect(options.quality).to.be.equal(0); - expect(options).to.haveOwnProperty("format"); - expect(options.format).to.be.equal("png"); + expect(options).to.haveOwnProperty('format'); + expect(options.format).to.be.equal('png'); - expect(options).to.haveOwnProperty("width"); + expect(options).to.haveOwnProperty('width'); expect(options.width).to.be.equal(768); - expect(options).to.haveOwnProperty("height"); + expect(options).to.haveOwnProperty('height'); expect(options.height).to.be.equal(512); - expect(options).to.haveOwnProperty("density"); + expect(options).to.haveOwnProperty('density'); expect(options.density).to.be.equal(72); - expect(options).to.haveOwnProperty("savePath"); - expect(options.savePath).to.be.equal("./"); + expect(options).to.haveOwnProperty('savePath'); + expect(options.savePath).to.be.equal('./'); - expect(options).to.haveOwnProperty("saveFilename"); - expect(options.saveFilename).to.be.equal("untitled"); + expect(options).to.haveOwnProperty('saveFilename'); + expect(options.saveFilename).to.be.equal('untitled'); - expect(options).to.haveOwnProperty("compression"); - expect(options.compression).to.be.equal("jpeg"); + expect(options).to.haveOwnProperty('compression'); + expect(options.compression).to.be.equal('jpeg'); }); - it("should set and return all of the options (custom options)", () => { + it('should set and return all of the options (custom options)', () => { const gm = new Graphics(); gm.setQuality(100); - gm.setFormat("jpg"); + gm.setFormat('jpg'); gm.setPreserveAspectRatio(true); gm.setSize(100, 100); gm.setDensity(100); - gm.setSavePath("./test/data"); - gm.setSaveFilename("specimen"); - gm.setCompression("Lossless"); + gm.setSavePath('./test/data'); + gm.setSaveFilename('specimen'); + gm.setCompression('Lossless'); const options = gm.getOptions(); - expect(options).to.haveOwnProperty("quality"); + expect(options).to.haveOwnProperty('quality'); expect(options.quality).to.be.equal(100); - expect(options).to.haveOwnProperty("format"); - expect(options.format).to.be.equal("jpg"); + expect(options).to.haveOwnProperty('format'); + expect(options.format).to.be.equal('jpg'); - expect(options).to.haveOwnProperty("width"); + expect(options).to.haveOwnProperty('width'); expect(options.width).to.be.equal(100); - expect(options).to.haveOwnProperty("height"); + expect(options).to.haveOwnProperty('height'); expect(options.height).to.be.equal(100); - expect(options).to.haveOwnProperty("preserveAspectRatio"); + expect(options).to.haveOwnProperty('preserveAspectRatio'); expect(options.preserveAspectRatio).to.be.equal(true); - expect(options).to.haveOwnProperty("density"); + expect(options).to.haveOwnProperty('density'); expect(options.density).to.be.equal(100); - expect(options).to.haveOwnProperty("savePath"); - expect(options.savePath).to.be.equal("./test/data"); + expect(options).to.haveOwnProperty('savePath'); + expect(options.savePath).to.be.equal('./test/data'); - expect(options).to.haveOwnProperty("saveFilename"); - expect(options.saveFilename).to.be.equal("specimen"); + expect(options).to.haveOwnProperty('saveFilename'); + expect(options.saveFilename).to.be.equal('specimen'); - expect(options).to.haveOwnProperty("compression"); - expect(options.compression).to.be.equal("Lossless"); + expect(options).to.haveOwnProperty('compression'); + expect(options.compression).to.be.equal('Lossless'); }); - it("should by default use width as height if no height is given", () => { + it('should by default use width as height if no height is given', () => { const gm = new Graphics(); gm.setSize(200); const options = gm.getOptions(); - expect(options).to.haveOwnProperty("width"); + expect(options).to.haveOwnProperty('width'); expect(options.width).to.be.equal(200); - expect(options).to.haveOwnProperty("height"); + expect(options).to.haveOwnProperty('height'); expect(options.height).to.be.equal(200); }); - it("should by not set height if preserveAspectRatio is `true`", () => { + it('should by not set height if preserveAspectRatio is `true`', () => { const gm = new Graphics(); gm.setPreserveAspectRatio(true); @@ -131,95 +131,95 @@ describe("graphics", () => { const options = gm.getOptions(); - expect(options).to.haveOwnProperty("width"); + expect(options).to.haveOwnProperty('width'); expect(options.width).to.be.equal(200); - expect(options).to.haveOwnProperty("height"); + expect(options).to.haveOwnProperty('height'); expect(options.height).to.be.equal(undefined); }); - it("should save first page as image file", async () => { + it('should save first page as image file', async () => { const gm = new Graphics(); gm.setSize(1684, 2384); gm.setSavePath(`./dump/savefiletest`); - const file = createReadStream("./test/data/pdf2.pdf"); + const file = createReadStream('./test/data/pdf2.pdf'); await gm.writeImage(file, 0); - const info = await gm.identify("./dump/savefiletest/untitled.1.png") as gm.ImageInfo; + const info = (await gm.identify('./dump/savefiletest/untitled.1.png')) as gm.ImageInfo; - expect(info).to.haveOwnProperty("format"); - expect(info.format).to.be.equal("PNG"); - expect(info).to.haveOwnProperty("size"); - expect(info.size).to.haveOwnProperty("width"); + expect(info).to.haveOwnProperty('format'); + expect(info.format).to.be.equal('PNG'); + expect(info).to.haveOwnProperty('size'); + expect(info.size).to.haveOwnProperty('width'); expect(info.size.width).to.be.equal(1684); - expect(info.size).to.haveOwnProperty("height"); + expect(info.size).to.haveOwnProperty('height'); expect(info.size.height).to.be.equal(2384); }); - it("should save second page as image file", async () => { + it('should save second page as image file', async () => { const gm = new Graphics(); gm.setSize(1684, 2384); gm.setSavePath(`./dump/savefiletest`); - const file = createReadStream("./test/data/pdf2.pdf"); + const file = createReadStream('./test/data/pdf2.pdf'); await gm.writeImage(file, 1); - const info = await gm.identify("./dump/savefiletest/untitled.2.png") as gm.ImageInfo; + const info = (await gm.identify('./dump/savefiletest/untitled.2.png')) as gm.ImageInfo; - expect(info).to.haveOwnProperty("format"); - expect(info.format).to.be.equal("PNG"); - expect(info).to.haveOwnProperty("size"); - expect(info.size).to.haveOwnProperty("width"); + expect(info).to.haveOwnProperty('format'); + expect(info.format).to.be.equal('PNG'); + expect(info).to.haveOwnProperty('size'); + expect(info.size).to.haveOwnProperty('width'); expect(info.size.width).to.be.equal(1684); - expect(info.size).to.haveOwnProperty("height"); + expect(info.size).to.haveOwnProperty('height'); expect(info.size.height).to.be.equal(2384); }); - it("should save second page as base64 string", async () => { + it('should save second page as base64 string', async () => { const gm = new Graphics(); gm.setSize(1684, 2384); gm.setSavePath(`./dump/savefiletest`); - const file = createReadStream("./test/data/pdf2.pdf"); + const file = createReadStream('./test/data/pdf2.pdf'); const base64string = await gm.toBase64(file, 1); - writeFileSync("./dump/savefiletest/frombase64.png", Buffer.from(base64string.base64, "base64")); + writeFileSync('./dump/savefiletest/frombase64.png', Buffer.from(base64string.base64, 'base64')); - const info = await gm.identify("./dump/savefiletest/frombase64.png") as gm.ImageInfo; + const info = (await gm.identify('./dump/savefiletest/frombase64.png')) as gm.ImageInfo; - expect(info).to.haveOwnProperty("format"); - expect(info.format).to.be.equal("PNG"); - expect(info).to.haveOwnProperty("size"); - expect(info.size).to.haveOwnProperty("width"); + expect(info).to.haveOwnProperty('format'); + expect(info.format).to.be.equal('PNG'); + expect(info).to.haveOwnProperty('size'); + expect(info.size).to.haveOwnProperty('width'); expect(info.size.width).to.be.equal(1684); - expect(info.size).to.haveOwnProperty("height"); + expect(info.size).to.haveOwnProperty('height'); expect(info.size.height).to.be.equal(2384); }); - it("should save second page as buffer", async () => { + it('should save second page as buffer', async () => { const gm = new Graphics(); gm.setSize(1684, 2384); gm.setSavePath(`./dump/savefiletest`); - const file = createReadStream("./test/data/pdf2.pdf"); + const file = createReadStream('./test/data/pdf2.pdf'); const { buffer } = await gm.toBuffer(file, 1); expect(Buffer.isBuffer(buffer)).to.be.true; - writeFileSync("./dump/savefiletest/tobuffer.png", buffer); + writeFileSync('./dump/savefiletest/tobuffer.png', buffer); - const info = await gm.identify("./dump/savefiletest/tobuffer.png") as gm.ImageInfo; + const info = (await gm.identify('./dump/savefiletest/tobuffer.png')) as gm.ImageInfo; - expect(info).to.haveOwnProperty("format"); - expect(info.format).to.be.equal("PNG"); - expect(info).to.haveOwnProperty("size"); - expect(info.size).to.haveOwnProperty("width"); + expect(info).to.haveOwnProperty('format'); + expect(info.format).to.be.equal('PNG'); + expect(info).to.haveOwnProperty('size'); + expect(info.size).to.haveOwnProperty('width'); expect(info.size.width).to.be.equal(1684); - expect(info.size).to.haveOwnProperty("height"); + expect(info.size).to.haveOwnProperty('height'); expect(info.size.height).to.be.equal(2384); }); }); diff --git a/test/index.test.ts b/test/index.test.ts index f8c173d..85a6965 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,21 +1,21 @@ -import chai, { expect } from "chai"; -import { mkdirSync, readFileSync, writeFileSync } from "fs"; -import gm from "gm"; +import chai, { expect } from 'chai'; +import { mkdirSync, readFileSync, writeFileSync } from 'fs'; +import gm from 'gm'; import path from 'path'; -import { rimrafSync } from "rimraf"; -import { fromBase64, fromBuffer, fromPath } from "../src/index"; -import { Graphics } from "../src/graphics"; -import { BufferResponse, ToBase64Response, WriteImageResponse } from "../src/types/convertResponse"; -import { Options } from "../src/types/options"; +import { rimrafSync } from 'rimraf'; +import { fromBase64, fromBuffer, fromPath } from '../src/index'; +import { Graphics } from '../src/graphics'; +import { BufferResponse, ToBase64Response, WriteImageResponse } from '../src/types/convertResponse'; +import { Options } from '../src/types/options'; import { defaultOptions } from '../src/utils/defaultOptions'; -describe("PDF2Pic Core", () => { +describe('PDF2Pic Core', () => { const baseOptions = { quality: 100, - format: "jpg", + format: 'jpg', width: 768, height: 512, - savePath: "./dump/fromfiletest" + savePath: './dump/fromfiletest', }; const expectInfoToBeValid = (info: gm.ImageInfo, options) => { @@ -34,7 +34,7 @@ describe("PDF2Pic Core", () => { if (options.height) { expect(info.size.height).to.be.equal(options.height); } - } + }; const expectImageResponseToBeValid = (response: WriteImageResponse, options: Options) => { expect(response).to.haveOwnProperty('name'); @@ -48,7 +48,7 @@ describe("PDF2Pic Core", () => { expect(response.path).to.equal(savePath); expect(response).to.haveOwnProperty('page'); expect(response.page).to.be.a('number'); - } + }; const expectBase64ResponseToBeValid = (response: ToBase64Response) => { expect(response).to.haveOwnProperty('base64'); @@ -57,14 +57,14 @@ describe("PDF2Pic Core", () => { expect(response.size).to.be.a('string'); expect(response).to.haveOwnProperty('page'); expect(response.page).to.be.a('number'); - } + }; before(() => { - rimrafSync("./dump/fromfiletest"); - rimrafSync("./dump/frombuffertest"); - rimrafSync("./dump/frombase64test"); + rimrafSync('./dump/fromfiletest'); + rimrafSync('./dump/frombuffertest'); + rimrafSync('./dump/frombase64test'); - mkdirSync("./dump/fromfiletest", { recursive: true }); + mkdirSync('./dump/fromfiletest', { recursive: true }); }); it('should use default options', async () => { @@ -75,205 +75,209 @@ describe("PDF2Pic Core", () => { const defaultFilePath = `${defaultOptions.savePath}${defaultOptions.saveFilename}.1.${defaultOptions.format}`; const gm = new Graphics(); - const info = await gm.identify(defaultFilePath) as gm.ImageInfo; - expect(info.format).to.equal(defaultOptions.format.toUpperCase()) - expect(info.size.width).to.equal(defaultOptions.width) - expect(info.size.height).to.equal(defaultOptions.height) - rimrafSync(defaultFilePath) + const info = (await gm.identify(defaultFilePath)) as gm.ImageInfo; + expect(info.format).to.equal(defaultOptions.format.toUpperCase()); + expect(info.size.width).to.equal(defaultOptions.width); + expect(info.size.height).to.equal(defaultOptions.height); + rimrafSync(defaultFilePath); }); - it("should convert pdf to pic (file input, first page)", async () => { + it('should convert pdf to pic (file input, first page)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-1" - } + format: 'png', + saveFilename: 'test-1', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); + const convert = fromPath('./test/data/pdf1.pdf', options); const imageResponse = await convert(); expectImageResponseToBeValid(imageResponse, options); - const info = await gm.identify("./dump/fromfiletest/test-1.1.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + const info = (await gm.identify('./dump/fromfiletest/test-1.1.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (buffer input, first page)", async () => { + it('should convert pdf to pic (buffer input, first page)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-1" - } - const buffer = readFileSync("./test/data/pdf1.pdf"); + format: 'png', + saveFilename: 'test-1', + }; + const buffer = readFileSync('./test/data/pdf1.pdf'); const convert = fromBuffer(buffer, options); const imageResponse = await convert(); expectImageResponseToBeValid(imageResponse, options); - const info = await gm.identify("./dump/fromfiletest/test-1.1.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + const info = (await gm.identify('./dump/fromfiletest/test-1.1.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (base64 input, first page)", async () => { + it('should convert pdf to pic (base64 input, first page)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-1" - } - const b64 = readFileSync("./test/data/pdf1.pdf", "base64"); + format: 'png', + saveFilename: 'test-1', + }; + const b64 = readFileSync('./test/data/pdf1.pdf', 'base64'); const convert = fromBase64(b64, options); const imageResponse = await convert(); expectImageResponseToBeValid(imageResponse, options); - const info = await gm.identify("./dump/fromfiletest/test-1.1.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + const info = (await gm.identify('./dump/fromfiletest/test-1.1.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (file input, second page, base64 output)", async () => { + it('should convert pdf to pic (file input, second page, base64 output)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-2" - } + format: 'png', + saveFilename: 'test-2', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); + const convert = fromPath('./test/data/pdf1.pdf', options); const base64Response = await convert(2, { responseType: 'base64' }); - expectBase64ResponseToBeValid(base64Response) - writeFileSync("./dump/fromfiletest/frombase64.png", Buffer.from(base64Response.base64, "base64")); - const info = await gm.identify("./dump/fromfiletest/frombase64.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBase64ResponseToBeValid(base64Response); + writeFileSync('./dump/fromfiletest/frombase64.png', Buffer.from(base64Response.base64, 'base64')); + const info = (await gm.identify('./dump/fromfiletest/frombase64.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (buffer input, second page, base64 output)", async () => { + it('should convert pdf to pic (buffer input, second page, base64 output)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-2" - } + format: 'png', + saveFilename: 'test-2', + }; - const buffer = readFileSync("./test/data/pdf1.pdf"); + const buffer = readFileSync('./test/data/pdf1.pdf'); const convert = fromBuffer(buffer, options); const base64Response = await convert(2, { responseType: 'base64' }); - expectBase64ResponseToBeValid(base64Response) - writeFileSync("./dump/fromfiletest/frombase64.png", Buffer.from(base64Response.base64, "base64")); - const info = await gm.identify("./dump/fromfiletest/frombase64.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBase64ResponseToBeValid(base64Response); + writeFileSync('./dump/fromfiletest/frombase64.png', Buffer.from(base64Response.base64, 'base64')); + const info = (await gm.identify('./dump/fromfiletest/frombase64.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (base64 input, second page, base64 output)", async () => { + it('should convert pdf to pic (base64 input, second page, base64 output)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-2" - } + format: 'png', + saveFilename: 'test-2', + }; - const b64 = readFileSync("./test/data/pdf1.pdf", "base64"); + const b64 = readFileSync('./test/data/pdf1.pdf', 'base64'); const convert = fromBase64(b64, options); const base64Response = await convert(2, { responseType: 'base64' }); - expectBase64ResponseToBeValid(base64Response) - writeFileSync("./dump/fromfiletest/frombase64.png", Buffer.from(base64Response.base64, "base64")); - const info = await gm.identify("./dump/fromfiletest/frombase64.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBase64ResponseToBeValid(base64Response); + writeFileSync('./dump/fromfiletest/frombase64.png', Buffer.from(base64Response.base64, 'base64')); + const info = (await gm.identify('./dump/fromfiletest/frombase64.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (file input, bulk 2 pages)", async () => { + it('should convert pdf to pic (file input, bulk 2 pages)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: 768, height: 512, - saveFilename: "test-3" - } + saveFilename: 'test-3', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); - const imageResponse = await convert.bulk([1, 2], { responseType: 'image' }); + const convert = fromPath('./test/data/pdf1.pdf', options); + const imageResponse = await convert.bulk([1, 2], { + responseType: 'image', + }); - expect(imageResponse).to.be.an('array').that.has.lengthOf(2) + expect(imageResponse).to.be.an('array').that.has.lengthOf(2); for (let i = 0; i < imageResponse.length; i++) { - expectImageResponseToBeValid(imageResponse[i], options) - const info = await gm.identify(`./dump/fromfiletest/test-3.${i + 1}.png`) as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectImageResponseToBeValid(imageResponse[i], options); + const info = (await gm.identify(`./dump/fromfiletest/test-3.${i + 1}.png`)) as gm.ImageInfo; + expectInfoToBeValid(info, options); } }).timeout(7000); - it("should convert pdf to pic (file input, bulk all pages)", async () => { + it('should convert pdf to pic (file input, bulk all pages)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: 768, height: 512, - saveFilename: "test-3" - } + saveFilename: 'test-3', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); + const convert = fromPath('./test/data/pdf1.pdf', options); const imageResponse = await convert.bulk(-1, { responseType: 'image' }); - expect(imageResponse).to.be.an('array').that.has.lengthOf(9) + expect(imageResponse).to.be.an('array').that.has.lengthOf(9); for (let i = 0; i < imageResponse.length; i++) { - expectImageResponseToBeValid(imageResponse[i], options) - const info = await gm.identify(`./dump/fromfiletest/test-3.${i + 1}.png`) as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectImageResponseToBeValid(imageResponse[i], options); + const info = (await gm.identify(`./dump/fromfiletest/test-3.${i + 1}.png`)) as gm.ImageInfo; + expectInfoToBeValid(info, options); } }).timeout(7000); - it("should convert pdf to pic (buffer input, bulk all pages)", async () => { + it('should convert pdf to pic (buffer input, bulk all pages)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: 768, height: 512, - saveFilename: "test-3" - } + saveFilename: 'test-3', + }; - const buffer = readFileSync("./test/data/pdf1.pdf"); + const buffer = readFileSync('./test/data/pdf1.pdf'); const convert = fromBuffer(buffer, options); const imageResponse = await convert.bulk(-1); - expect(imageResponse).to.be.an('array').that.has.lengthOf(9) + expect(imageResponse).to.be.an('array').that.has.lengthOf(9); for (let i = 0; i < imageResponse.length; i++) { - expectImageResponseToBeValid(imageResponse[i], options) - const info = await gm.identify(`./dump/fromfiletest/test-3.${i + 1}.png`) as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectImageResponseToBeValid(imageResponse[i], options); + const info = (await gm.identify(`./dump/fromfiletest/test-3.${i + 1}.png`)) as gm.ImageInfo; + expectInfoToBeValid(info, options); } }).timeout(7000); - it("should convert pdf to pic (base64 input, bulk all pages)", async () => { + it('should convert pdf to pic (base64 input, bulk all pages)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: 768, height: 512, - saveFilename: "test-3" - } + saveFilename: 'test-3', + }; - const b64 = readFileSync("./test/data/pdf1.pdf", "base64"); + const b64 = readFileSync('./test/data/pdf1.pdf', 'base64'); const convert = fromBase64(b64, options); - const base64Responses = await convert.bulk(-1, { responseType: 'base64' }); + const base64Responses = await convert.bulk(-1, { + responseType: 'base64', + }); - expect(base64Responses).to.be.an('array').that.has.lengthOf(9) + expect(base64Responses).to.be.an('array').that.has.lengthOf(9); for (let i = 0; i < base64Responses.length; i++) { - expectBase64ResponseToBeValid(base64Responses[i]) - const filename = `./dump/fromfiletest/test-3.b64.${i + 1}.png` - writeFileSync(filename, Buffer.from(base64Responses[i].base64, 'base64')) - const info = await gm.identify(filename) as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBase64ResponseToBeValid(base64Responses[i]); + const filename = `./dump/fromfiletest/test-3.b64.${i + 1}.png`; + writeFileSync(filename, Buffer.from(base64Responses[i].base64, 'base64')); + const info = (await gm.identify(filename)) as gm.ImageInfo; + expectInfoToBeValid(info, options); } }).timeout(7000); @@ -285,82 +289,84 @@ describe("PDF2Pic Core", () => { expect(response.size).to.be.a('string'); expect(response).to.haveOwnProperty('page'); expect(response.page).to.be.a('number'); - } + }; - it("should convert pdf to pic (file input, first page)", async () => { + it('should convert pdf to pic (file input, first page)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - } + format: 'png', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); + const convert = fromPath('./test/data/pdf1.pdf', options); const bufferResponse = await convert(2, { responseType: 'buffer' }); - expectBufferResponseToBeValid(bufferResponse) - writeFileSync("./dump/fromfiletest/out-buffer-1.png", bufferResponse.buffer); - const info = await gm.identify("./dump/fromfiletest/out-buffer-1.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBufferResponseToBeValid(bufferResponse); + writeFileSync('./dump/fromfiletest/out-buffer-1.png', bufferResponse.buffer); + const info = (await gm.identify('./dump/fromfiletest/out-buffer-1.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (buffer input, second page)", async () => { + it('should convert pdf to pic (buffer input, second page)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - } + format: 'png', + }; - const buffer = readFileSync("./test/data/pdf1.pdf"); + const buffer = readFileSync('./test/data/pdf1.pdf'); const convert = fromBuffer(buffer, options); const bufferResponse = await convert(2, { responseType: 'buffer' }); - expectBufferResponseToBeValid(bufferResponse) - writeFileSync("./dump/fromfiletest/out-buffer-2.png", bufferResponse.buffer); - const info = await gm.identify("./dump/fromfiletest/out-buffer-2.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBufferResponseToBeValid(bufferResponse); + writeFileSync('./dump/fromfiletest/out-buffer-2.png', bufferResponse.buffer); + const info = (await gm.identify('./dump/fromfiletest/out-buffer-2.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (base64 input, second page)", async () => { + it('should convert pdf to pic (base64 input, second page)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", - saveFilename: "test-2" - } + format: 'png', + saveFilename: 'test-2', + }; - const b64 = readFileSync("./test/data/pdf1.pdf", "base64"); + const b64 = readFileSync('./test/data/pdf1.pdf', 'base64'); const convert = fromBase64(b64, options); const bufferResponse = await convert(2, { responseType: 'buffer' }); - expectBufferResponseToBeValid(bufferResponse) - writeFileSync("./dump/fromfiletest/out-buffer-3.png", bufferResponse.buffer); - const info = await gm.identify("./dump/fromfiletest/out-buffer-3.png") as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBufferResponseToBeValid(bufferResponse); + writeFileSync('./dump/fromfiletest/out-buffer-3.png', bufferResponse.buffer); + const info = (await gm.identify('./dump/fromfiletest/out-buffer-3.png')) as gm.ImageInfo; + expectInfoToBeValid(info, options); }); - it("should convert pdf to pic (base64 input, bulk all pages)", async () => { + it('should convert pdf to pic (base64 input, bulk all pages)', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: 768, height: 512, - } + }; - const b64 = readFileSync("./test/data/pdf1.pdf", "base64"); + const b64 = readFileSync('./test/data/pdf1.pdf', 'base64'); const convert = fromBase64(b64, options); - const bufferResponses = await convert.bulk(-1, { responseType: 'buffer' }); + const bufferResponses = await convert.bulk(-1, { + responseType: 'buffer', + }); - expect(bufferResponses).to.be.an('array').that.has.lengthOf(9) + expect(bufferResponses).to.be.an('array').that.has.lengthOf(9); for (let i = 0; i < bufferResponses.length; i++) { - expectBufferResponseToBeValid(bufferResponses[i]) - const filename = `./dump/fromfiletest/test-bulk.buffer.${i + 1}.png` - writeFileSync(filename, bufferResponses[i].buffer) - const info = await gm.identify(filename) as gm.ImageInfo; - expectInfoToBeValid(info, options) + expectBufferResponseToBeValid(bufferResponses[i]); + const filename = `./dump/fromfiletest/test-bulk.buffer.${i + 1}.png`; + writeFileSync(filename, bufferResponses[i].buffer); + const info = (await gm.identify(filename)) as gm.ImageInfo; + expectInfoToBeValid(info, options); } }).timeout(7000); }); @@ -370,64 +376,73 @@ describe("PDF2Pic Core", () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: undefined, height: undefined, preserveAspectRatio: true, - saveFilename: "test-aspect-ratio-1" - } - - const convert = fromPath("./test/data/pdf1.pdf", options); - const imageResponse = await convert.bulk([1, 2], { responseType: 'image' }); - - expect(imageResponse).to.be.an('array').that.has.lengthOf(2) - - - expectImageResponseToBeValid(imageResponse[0], options) - const page1Info = await gm.identify(`./dump/fromfiletest/${options.saveFilename}.1.png`) as gm.ImageInfo; - expectInfoToBeValid(page1Info, { ...options, width: 842, height: 595 }) - - expectImageResponseToBeValid(imageResponse[1], options) - const page2Info = await gm.identify(`./dump/fromfiletest/${options.saveFilename}.2.png`) as gm.ImageInfo; - expectInfoToBeValid(page2Info, { ...options, width: 1684, height: 595 }) + saveFilename: 'test-aspect-ratio-1', + }; + + const convert = fromPath('./test/data/pdf1.pdf', options); + const imageResponse = await convert.bulk([1, 2], { + responseType: 'image', + }); + + expect(imageResponse).to.be.an('array').that.has.lengthOf(2); + + expectImageResponseToBeValid(imageResponse[0], options); + const page1Info = (await gm.identify(`./dump/fromfiletest/${options.saveFilename}.1.png`)) as gm.ImageInfo; + expectInfoToBeValid(page1Info, { + ...options, + width: 842, + height: 595, + }); + + expectImageResponseToBeValid(imageResponse[1], options); + const page2Info = (await gm.identify(`./dump/fromfiletest/${options.saveFilename}.2.png`)) as gm.ImageInfo; + expectInfoToBeValid(page2Info, { + ...options, + width: 1684, + height: 595, + }); }); it('should set height automatically', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: 600, height: undefined, preserveAspectRatio: true, - saveFilename: "test-aspect-ratio-2" - } + saveFilename: 'test-aspect-ratio-2', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); + const convert = fromPath('./test/data/pdf1.pdf', options); const imageResponse = await convert(1, { responseType: 'image' }); - expectImageResponseToBeValid(imageResponse, options) - const page1Info = await gm.identify(`./dump/fromfiletest/${options.saveFilename}.1.png`) as gm.ImageInfo; - expectInfoToBeValid(page1Info, { ...options, height: 424 }) + expectImageResponseToBeValid(imageResponse, options); + const page1Info = (await gm.identify(`./dump/fromfiletest/${options.saveFilename}.1.png`)) as gm.ImageInfo; + expectInfoToBeValid(page1Info, { ...options, height: 424 }); }); it('should set width automatically', async () => { const gm = new Graphics(); const options = { ...baseOptions, - format: "png", + format: 'png', width: undefined, height: 600, preserveAspectRatio: true, - saveFilename: "test-aspect-ratio-3" - } + saveFilename: 'test-aspect-ratio-3', + }; - const convert = fromPath("./test/data/pdf1.pdf", options); + const convert = fromPath('./test/data/pdf1.pdf', options); const imageResponse = await convert(1, { responseType: 'image' }); - expectImageResponseToBeValid(imageResponse, options) - const page1Info = await gm.identify(`./dump/fromfiletest/${options.saveFilename}.1.png`) as gm.ImageInfo; - expectInfoToBeValid(page1Info, { ...options, width: 849 }) + expectImageResponseToBeValid(imageResponse, options); + const page1Info = (await gm.identify(`./dump/fromfiletest/${options.saveFilename}.1.png`)) as gm.ImageInfo; + expectInfoToBeValid(page1Info, { ...options, width: 849 }); }); - }) + }); }); diff --git a/test/resolveResponseType.test.ts b/test/resolveResponseType.test.ts index a6feff1..9b46dfc 100644 --- a/test/resolveResponseType.test.ts +++ b/test/resolveResponseType.test.ts @@ -1,30 +1,30 @@ import chai, { expect } from 'chai'; -import { resolveResponseType } from '@module/utils/resolveResponseType' +import { resolveResponseType } from '@module/utils/resolveResponseType'; describe('resolveResponseType', () => { it('should resolve to image if convertOptions is undefined', () => { - expect(resolveResponseType()).to.equal('image') + expect(resolveResponseType()).to.equal('image'); }); it('should resolve to image if responseType is image', () => { - expect(resolveResponseType({ responseType: 'image' })).to.equal('image') - }) + expect(resolveResponseType({ responseType: 'image' })).to.equal('image'); + }); it('should resolve to base64 if responseType is base64', () => { - expect(resolveResponseType({ responseType: 'base64' })).to.equal('base64') + expect(resolveResponseType({ responseType: 'base64' })).to.equal('base64'); }); it('should resolve to buffer if responseType is buffer', () => { - expect(resolveResponseType({ responseType: 'buffer' })).to.equal('buffer') + expect(resolveResponseType({ responseType: 'buffer' })).to.equal('buffer'); }); it('should throw an error if convertOptions is invalid type', async () => { // @ts-ignore - expect(() => resolveResponseType(1)).to.throw() + expect(() => resolveResponseType(1)).to.throw(); }); it('should throw an error if convertOptions is invalid responseType', async () => { // @ts-ignore - expect(() => resolveResponseType('invalid-type')).to.throw() + expect(() => resolveResponseType('invalid-type')).to.throw(); }); -}) +});