Skip to content

Commit

Permalink
fix: bulk returning a promise of array of promise
Browse files Browse the repository at this point in the history
  • Loading branch information
yakovmeister committed Sep 13, 2020
1 parent ea30d48 commit eb2fcef
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdf2pic",
"version": "2.0.0",
"version": "2.1.0",
"description": "A utility for converting pdf to image and base64 format.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class Graphics {

return resolve({
name: path.basename(output),
size: fs.statSync(output).size / 1000.0,
size: `${this.width}x${this.height}`,
fileSize: fs.statSync(output).size / 1000.0,
path: output,
page: page + 1
});
Expand Down
2 changes: 1 addition & 1 deletion src/types/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ToBase64Response } from "@module/types/toBase64Response";

export type Convert = {

bulk?: (pages?: number | number[], toBase64?: boolean) => Promise<(Promise<WriteImageResponse> | Promise<ToBase64Response>)[]>;
bulk?: (pages?: number | number[], toBase64?: boolean) => Promise<WriteImageResponse[] | ToBase64Response[]>;

setOptions?: () => void;

Expand Down
3 changes: 2 additions & 1 deletion src/types/writeImageResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type WriteImageResponse = {
name?: string;
size?: number;
size?: string;
fileSize?: number;
path?: string;
page?: number;
}
4 changes: 2 additions & 2 deletions src/utils/bulk/bulkConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { convertToStream } from "@module/utils/converters/convertToStream";
import { WriteImageResponse } from "@module/types/writeImageResponse";
import { ToBase64Response } from "@module/types/toBase64Response";

export async function bulkConvert(gm: Graphics, source: string, filePath: string | Buffer, pageNumber: number | number[] = 1, toBase64 = false): Promise<(Promise<WriteImageResponse> | Promise<ToBase64Response>)[]> {
export async function bulkConvert(gm: Graphics, source: string, filePath: string | Buffer, pageNumber: number | number[] = 1, toBase64 = false): Promise<WriteImageResponse[] | ToBase64Response[]> {
if (pageNumber !== -1 && pageNumber < 1) {
throw new Error("Page number should be more than or equal 1");
}
Expand All @@ -27,5 +27,5 @@ export async function bulkConvert(gm: Graphics, source: string, filePath: string
return gm.writeImage(stream, (page - 1));
});

return pages;
return Promise.all(pages);
}
2 changes: 1 addition & 1 deletion src/utils/pdf2picCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function pdf2picCore(source: string, filePath: string | Buffer, options =
return gm.writeImage(stream, (page - 1));
};

convert.bulk = (pages: number | number[], toBase64 = false): Promise<(Promise<WriteImageResponse> | Promise<ToBase64Response>)[]> => {
convert.bulk = (pages: number | number[], toBase64 = false): Promise<WriteImageResponse[] | ToBase64Response[]> => {
return bulkConvert(gm, source, filePath, pages, toBase64);
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("PDF2Pic Core", () => {
expect(info.size.height).to.be.equal(512);
});

it.skip("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,
Expand Down

0 comments on commit eb2fcef

Please sign in to comment.