diff --git a/CHANGELOG.md b/CHANGELOG.md index 9403cb323..6bb585c27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## v4.32.0 - 2025-10-13 +### Changes +* :sparkles: add getting page count of a local source + + ## v4.31.1 - 2025-10-08 ### Fixes * :bug: make sure input sources are only initialized once diff --git a/README.md b/README.md index 875b9a6d3..baf37a5d7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![License: MIT](https://img.shields.io/github/license/mindee/mindee-api-nodejs)](https://opensource.org/licenses/MIT) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mindee/mindee-api-nodejs/test.yml)](https://github.com/mindee/mindee-api-nodejs) [![NPM Version](https://img.shields.io/npm/v/mindee)](https://www.npmjs.com/package/mindee) [![Downloads](https://img.shields.io/npm/dm/mindee)](https://www.npmjs.com/package/mindee) +[![License: MIT](https://img.shields.io/github/license/mindee/mindee-api-nodejs)](https://opensource.org/licenses/MIT) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mindee/mindee-api-nodejs/push-main-branch.yml)](https://github.com/mindee/mindee-api-nodejs) [![NPM Version](https://img.shields.io/npm/v/mindee)](https://www.npmjs.com/package/mindee) [![Downloads](https://img.shields.io/npm/dm/mindee)](https://www.npmjs.com/package/mindee) # Mindee API Helper Library for Node.js Quickly and easily connect to Mindee's API services using Node.js. diff --git a/package-lock.json b/package-lock.json index 641115250..84e1d1375 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mindee", - "version": "4.31.1", + "version": "4.32.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mindee", - "version": "4.31.1", + "version": "4.32.0", "license": "MIT", "dependencies": { "@cantoo/pdf-lib": "^2.3.2", @@ -27,16 +27,16 @@ "@types/mocha": "^10.0.10", "@types/node": "^18.15.13", "@types/tmp": "^0.2.6", - "@typescript-eslint/eslint-plugin": "^8.40.0", - "@typescript-eslint/parser": "^8.40.0", - "chai": "^4.3.10", - "eslint": "^9.15.0", - "eslint-plugin-jsdoc": "^50.5.0", - "mocha": "^11.7.1", + "@typescript-eslint/eslint-plugin": "^8.42.1", + "@typescript-eslint/parser": "^8.42.1", + "chai": "^4.4.1", + "eslint": "^9.20.1", + "eslint-plugin-jsdoc": "^50.6.17", + "mocha": "^11.7.4", "nock": "^13.5.6", "ts-node": "^10.9.2", - "typedoc": "~0.28.7", - "typescript": "^5.6.3" + "typedoc": "~0.28.14", + "typescript": "^5.7.3" }, "engines": { "node": ">= 16" @@ -969,17 +969,17 @@ "license": "MIT" }, "node_modules/@typescript-eslint/eslint-plugin": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.42.0.tgz", - "integrity": "sha512-Aq2dPqsQkxHOLfb2OPv43RnIvfj05nw8v/6n3B2NABIPpHnjQnaLo9QGMTvml+tv4korl/Cjfrb/BYhoL8UUTQ==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.46.0.tgz", + "integrity": "sha512-hA8gxBq4ukonVXPy0OKhiaUh/68D0E88GSmtC1iAEnGaieuDi38LhS7jdCHRLi6ErJBNDGCzvh5EnzdPwUc0DA==", "dev": true, "license": "MIT", "dependencies": { "@eslint-community/regexpp": "^4.10.0", - "@typescript-eslint/scope-manager": "8.42.0", - "@typescript-eslint/type-utils": "8.42.0", - "@typescript-eslint/utils": "8.42.0", - "@typescript-eslint/visitor-keys": "8.42.0", + "@typescript-eslint/scope-manager": "8.46.0", + "@typescript-eslint/type-utils": "8.46.0", + "@typescript-eslint/utils": "8.46.0", + "@typescript-eslint/visitor-keys": "8.46.0", "graphemer": "^1.4.0", "ignore": "^7.0.0", "natural-compare": "^1.4.0", @@ -993,22 +993,22 @@ "url": "https://opencollective.com/typescript-eslint" }, "peerDependencies": { - "@typescript-eslint/parser": "^8.42.0", + "@typescript-eslint/parser": "^8.46.0", "eslint": "^8.57.0 || ^9.0.0", "typescript": ">=4.8.4 <6.0.0" } }, "node_modules/@typescript-eslint/parser": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-8.42.0.tgz", - "integrity": "sha512-r1XG74QgShUgXph1BYseJ+KZd17bKQib/yF3SR+demvytiRXrwd12Blnz5eYGm8tXaeRdd4x88MlfwldHoudGg==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-8.46.0.tgz", + "integrity": "sha512-n1H6IcDhmmUEG7TNVSspGmiHHutt7iVKtZwRppD7e04wha5MrkV1h3pti9xQLcCMt6YWsncpoT0HMjkH1FNwWQ==", "dev": true, "license": "MIT", "dependencies": { - "@typescript-eslint/scope-manager": "8.42.0", - "@typescript-eslint/types": "8.42.0", - "@typescript-eslint/typescript-estree": "8.42.0", - "@typescript-eslint/visitor-keys": "8.42.0", + "@typescript-eslint/scope-manager": "8.46.0", + "@typescript-eslint/types": "8.46.0", + "@typescript-eslint/typescript-estree": "8.46.0", + "@typescript-eslint/visitor-keys": "8.46.0", "debug": "^4.3.4" }, "engines": { @@ -1024,14 +1024,14 @@ } }, "node_modules/@typescript-eslint/project-service": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/project-service/-/project-service-8.42.0.tgz", - "integrity": "sha512-vfVpLHAhbPjilrabtOSNcUDmBboQNrJUiNAGoImkZKnMjs2TIcWG33s4Ds0wY3/50aZmTMqJa6PiwkwezaAklg==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/project-service/-/project-service-8.46.0.tgz", + "integrity": "sha512-OEhec0mH+U5Je2NZOeK1AbVCdm0ChyapAyTeXVIYTPXDJ3F07+cu87PPXcGoYqZ7M9YJVvFnfpGg1UmCIqM+QQ==", "dev": true, "license": "MIT", "dependencies": { - "@typescript-eslint/tsconfig-utils": "^8.42.0", - "@typescript-eslint/types": "^8.42.0", + "@typescript-eslint/tsconfig-utils": "^8.46.0", + "@typescript-eslint/types": "^8.46.0", "debug": "^4.3.4" }, "engines": { @@ -1046,14 +1046,14 @@ } }, "node_modules/@typescript-eslint/scope-manager": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-8.42.0.tgz", - "integrity": "sha512-51+x9o78NBAVgQzOPd17DkNTnIzJ8T/O2dmMBLoK9qbY0Gm52XJcdJcCl18ExBMiHo6jPMErUQWUv5RLE51zJw==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-8.46.0.tgz", + "integrity": "sha512-lWETPa9XGcBes4jqAMYD9fW0j4n6hrPtTJwWDmtqgFO/4HF4jmdH/Q6wggTw5qIT5TXjKzbt7GsZUBnWoO3dqw==", "dev": true, "license": "MIT", "dependencies": { - "@typescript-eslint/types": "8.42.0", - "@typescript-eslint/visitor-keys": "8.42.0" + "@typescript-eslint/types": "8.46.0", + "@typescript-eslint/visitor-keys": "8.46.0" }, "engines": { "node": "^18.18.0 || ^20.9.0 || >=21.1.0" @@ -1064,9 +1064,9 @@ } }, "node_modules/@typescript-eslint/tsconfig-utils": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/tsconfig-utils/-/tsconfig-utils-8.42.0.tgz", - "integrity": "sha512-kHeFUOdwAJfUmYKjR3CLgZSglGHjbNTi1H8sTYRYV2xX6eNz4RyJ2LIgsDLKf8Yi0/GL1WZAC/DgZBeBft8QAQ==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/tsconfig-utils/-/tsconfig-utils-8.46.0.tgz", + "integrity": "sha512-WrYXKGAHY836/N7zoK/kzi6p8tXFhasHh8ocFL9VZSAkvH956gfeRfcnhs3xzRy8qQ/dq3q44v1jvQieMFg2cw==", "dev": true, "license": "MIT", "engines": { @@ -1081,15 +1081,15 @@ } }, "node_modules/@typescript-eslint/type-utils": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-8.42.0.tgz", - "integrity": "sha512-9KChw92sbPTYVFw3JLRH1ockhyR3zqqn9lQXol3/YbI6jVxzWoGcT3AsAW0mu1MY0gYtsXnUGV/AKpkAj5tVlQ==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-8.46.0.tgz", + "integrity": "sha512-hy+lvYV1lZpVs2jRaEYvgCblZxUoJiPyCemwbQZ+NGulWkQRy0HRPYAoef/CNSzaLt+MLvMptZsHXHlkEilaeg==", "dev": true, "license": "MIT", "dependencies": { - "@typescript-eslint/types": "8.42.0", - "@typescript-eslint/typescript-estree": "8.42.0", - "@typescript-eslint/utils": "8.42.0", + "@typescript-eslint/types": "8.46.0", + "@typescript-eslint/typescript-estree": "8.46.0", + "@typescript-eslint/utils": "8.46.0", "debug": "^4.3.4", "ts-api-utils": "^2.1.0" }, @@ -1106,9 +1106,9 @@ } }, "node_modules/@typescript-eslint/types": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.42.0.tgz", - "integrity": "sha512-LdtAWMiFmbRLNP7JNeY0SqEtJvGMYSzfiWBSmx+VSZ1CH+1zyl8Mmw1TT39OrtsRvIYShjJWzTDMPWZJCpwBlw==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.46.0.tgz", + "integrity": "sha512-bHGGJyVjSE4dJJIO5yyEWt/cHyNwga/zXGJbJJ8TiO01aVREK6gCTu3L+5wrkb1FbDkQ+TKjMNe9R/QQQP9+rA==", "dev": true, "license": "MIT", "engines": { @@ -1120,16 +1120,16 @@ } }, "node_modules/@typescript-eslint/typescript-estree": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-8.42.0.tgz", - "integrity": "sha512-ku/uYtT4QXY8sl9EDJETD27o3Ewdi72hcXg1ah/kkUgBvAYHLwj2ofswFFNXS+FL5G+AGkxBtvGt8pFBHKlHsQ==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-8.46.0.tgz", + "integrity": "sha512-ekDCUfVpAKWJbRfm8T1YRrCot1KFxZn21oV76v5Fj4tr7ELyk84OS+ouvYdcDAwZL89WpEkEj2DKQ+qg//+ucg==", "dev": true, "license": "MIT", "dependencies": { - "@typescript-eslint/project-service": "8.42.0", - "@typescript-eslint/tsconfig-utils": "8.42.0", - "@typescript-eslint/types": "8.42.0", - "@typescript-eslint/visitor-keys": "8.42.0", + "@typescript-eslint/project-service": "8.46.0", + "@typescript-eslint/tsconfig-utils": "8.46.0", + "@typescript-eslint/types": "8.46.0", + "@typescript-eslint/visitor-keys": "8.46.0", "debug": "^4.3.4", "fast-glob": "^3.3.2", "is-glob": "^4.0.3", @@ -1149,16 +1149,16 @@ } }, "node_modules/@typescript-eslint/utils": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.42.0.tgz", - "integrity": "sha512-JnIzu7H3RH5BrKC4NoZqRfmjqCIS1u3hGZltDYJgkVdqAezl4L9d1ZLw+36huCujtSBSAirGINF/S4UxOcR+/g==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.46.0.tgz", + "integrity": "sha512-nD6yGWPj1xiOm4Gk0k6hLSZz2XkNXhuYmyIrOWcHoPuAhjT9i5bAG+xbWPgFeNR8HPHHtpNKdYUXJl/D3x7f5g==", "dev": true, "license": "MIT", "dependencies": { "@eslint-community/eslint-utils": "^4.7.0", - "@typescript-eslint/scope-manager": "8.42.0", - "@typescript-eslint/types": "8.42.0", - "@typescript-eslint/typescript-estree": "8.42.0" + "@typescript-eslint/scope-manager": "8.46.0", + "@typescript-eslint/types": "8.46.0", + "@typescript-eslint/typescript-estree": "8.46.0" }, "engines": { "node": "^18.18.0 || ^20.9.0 || >=21.1.0" @@ -1173,13 +1173,13 @@ } }, "node_modules/@typescript-eslint/visitor-keys": { - "version": "8.42.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-8.42.0.tgz", - "integrity": "sha512-3WbiuzoEowaEn8RSnhJBrxSwX8ULYE9CXaPepS2C2W3NSA5NNIvBaslpBSBElPq0UGr0xVJlXFWOAKIkyylydQ==", + "version": "8.46.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-8.46.0.tgz", + "integrity": "sha512-FrvMpAK+hTbFy7vH5j1+tMYHMSKLE6RzluFJlkFNKD0p9YsUT75JlBSmr5so3QRzvMwU5/bIEdeNrxm8du8l3Q==", "dev": true, "license": "MIT", "dependencies": { - "@typescript-eslint/types": "8.42.0", + "@typescript-eslint/types": "8.46.0", "eslint-visitor-keys": "^4.2.1" }, "engines": { @@ -2653,6 +2653,16 @@ "node": ">=0.12.0" } }, + "node_modules/is-path-inside": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz", + "integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/is-plain-obj": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", @@ -2961,9 +2971,9 @@ } }, "node_modules/mocha": { - "version": "11.7.2", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-11.7.2.tgz", - "integrity": "sha512-lkqVJPmqqG/w5jmmFtiRvtA2jkDyNVUcefFJKb2uyX4dekk8Okgqop3cgbFiaIvj8uCRJVTP5x9dfxGyXm2jvQ==", + "version": "11.7.4", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-11.7.4.tgz", + "integrity": "sha512-1jYAaY8x0kAZ0XszLWu14pzsf4KV740Gld4HXkhNTXwcHx4AUEDkPzgEHg9CM5dVcW+zv036tjpsEbLraPJj4w==", "dev": true, "license": "MIT", "dependencies": { @@ -2975,6 +2985,7 @@ "find-up": "^5.0.0", "glob": "^10.4.5", "he": "^1.2.0", + "is-path-inside": "^3.0.3", "js-yaml": "^4.1.0", "log-symbols": "^4.1.0", "minimatch": "^9.0.5", @@ -3876,9 +3887,9 @@ } }, "node_modules/typedoc": { - "version": "0.28.12", - "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.28.12.tgz", - "integrity": "sha512-H5ODu4f7N+myG4MfuSp2Vh6wV+WLoZaEYxKPt2y8hmmqNEMVrH69DAjjdmYivF4tP/C2jrIZCZhPalZlTU/ipA==", + "version": "0.28.14", + "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.28.14.tgz", + "integrity": "sha512-ftJYPvpVfQvFzpkoSfHLkJybdA/geDJ8BGQt/ZnkkhnBYoYW6lBgPQXu6vqLxO4X75dA55hX8Af847H5KXlEFA==", "dev": true, "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index dc9d91362..1fda6b991 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mindee", - "version": "4.31.1", + "version": "4.32.0", "description": "Mindee Client Library for Node.js", "main": "src/index.js", "bin": "bin/mindee.js", @@ -44,16 +44,16 @@ "@types/mocha": "^10.0.10", "@types/node": "^18.15.13", "@types/tmp": "^0.2.6", - "@typescript-eslint/eslint-plugin": "^8.40.0", - "@typescript-eslint/parser": "^8.40.0", - "chai": "^4.3.10", - "eslint": "^9.15.0", - "eslint-plugin-jsdoc": "^50.5.0", - "mocha": "^11.7.1", + "@typescript-eslint/eslint-plugin": "^8.42.1", + "@typescript-eslint/parser": "^8.42.1", + "chai": "^4.4.1", + "eslint": "^9.20.1", + "eslint-plugin-jsdoc": "^50.6.17", + "mocha": "^11.7.4", "nock": "^13.5.6", "ts-node": "^10.9.2", - "typedoc": "~0.28.7", - "typescript": "^5.6.3" + "typedoc": "~0.28.14", + "typescript": "^5.7.3" }, "dependencies": { "@cantoo/pdf-lib": "^2.3.2", diff --git a/src/input/sources/localInputSource.ts b/src/input/sources/localInputSource.ts index 36cf07c14..40c8f00fd 100644 --- a/src/input/sources/localInputSource.ts +++ b/src/input/sources/localInputSource.ts @@ -1,7 +1,7 @@ import { errorHandler } from "../../errors/handler"; import { logger } from "../../logger"; import { compressImage } from "../../imageOperations"; -import { compressPdf } from "../../pdf"; +import { compressPdf, countPages } from "../../pdf"; import path from "path"; import * as fileType from "file-type"; import { PageOptions } from "../pageOptions"; @@ -83,6 +83,22 @@ export abstract class LocalInputSource extends InputSource { return mimeType; } + /** + * Returns the file object as a Buffer. + * @returns Buffer representation of the file object + * @protected + */ + protected getBuffer(): Buffer { + if (typeof this.fileObject === "string") { + return Buffer.from(this.fileObject); + } + return this.fileObject; + } + + /** + * Determines whether the current file is a PDF. + * @returns {boolean} Returns true if the file is a PDF; otherwise, returns false. + */ isPdf(): boolean { if (!this.initialized) { throw new Error( @@ -97,15 +113,9 @@ export abstract class LocalInputSource extends InputSource { * @param pageOptions */ public async applyPageOptions(pageOptions: PageOptions) { - if (!this.initialized) { - await this.init(); - } - if (!(this.fileObject instanceof Buffer)) { - throw new Error( - `Cannot modify an input source of type ${this.inputType}.` - ); - } - const processedPdf = await extractPages(this.fileObject, pageOptions); + await this.init(); + const buffer = this.getBuffer(); + const processedPdf = await extractPages(buffer, pageOptions); this.fileObject = processedPdf.file; } @@ -137,15 +147,8 @@ export abstract class LocalInputSource extends InputSource { forceSourceText: boolean = false, disableSourceText: boolean = true ) { - if (!this.initialized) { - await this.init(); - } - let buffer: Buffer; - if (typeof this.fileObject === "string") { - buffer = Buffer.from(this.fileObject); - } else { - buffer = this.fileObject; - } + await this.init(); + const buffer = this.getBuffer(); if (this.isPdf()){ this.fileObject = await compressPdf(buffer, quality, forceSourceText, disableSourceText); } else { @@ -158,13 +161,25 @@ export abstract class LocalInputSource extends InputSource { * @return boolean */ public async hasSourceText() { - if (!this.initialized) { - await this.init(); - } + await this.init(); if (!this.isPdf()){ return false; } - const buffer = typeof this.fileObject === "string" ? Buffer.from(this.fileObject) : this.fileObject; + const buffer = this.getBuffer(); return hasSourceText(buffer); } + + /** + * Returns the number of pages in the input source. + * For PDFs, returns the actual page count. For images, returns 1. + * @return Promise The number of pages + */ + public async getPageCount(): Promise { + await this.init(); + if (!this.isPdf()) { + return 1; + } + const buffer = this.getBuffer(); + return countPages(buffer); + } } diff --git a/src/pdf/pdfOperation.ts b/src/pdf/pdfOperation.ts index b1831cf15..50999812e 100644 --- a/src/pdf/pdfOperation.ts +++ b/src/pdf/pdfOperation.ts @@ -84,6 +84,11 @@ export async function extractPages( return { file: fileBuffer, totalPagesRemoved: sumRemovedPages }; } +/** + * Count the number of pages in a pdf file. + * @param file + * @returns the number of pages in the file. + */ export async function countPages(file: Buffer): Promise { const currentPdf = await PDFDocument.load(file, { ignoreEncryption: true, diff --git a/src/pdf/pdfUtils.ts b/src/pdf/pdfUtils.ts index bf7e7d279..3618d9f9a 100644 --- a/src/pdf/pdfUtils.ts +++ b/src/pdf/pdfUtils.ts @@ -64,10 +64,6 @@ export async function extractTextFromPdf(pdfBuffer: Buffer): Promise { invoiceResult.document.toString(), testStringRstInvoice.toString() ) - ).to.be.greaterThanOrEqual(0.97); + ).to.be.greaterThanOrEqual(0.90); }).timeout(60000); }); diff --git a/tests/inputs/sources.spec.ts b/tests/inputs/sources.spec.ts index a9b57e970..c98743698 100644 --- a/tests/inputs/sources.spec.ts +++ b/tests/inputs/sources.spec.ts @@ -35,79 +35,89 @@ describe("Test different types of input", () => { // don't provide an extension to see if we can detect MIME // type based on contents const filename = "receipt"; - const input = new Base64Input({ + const inputSource = new Base64Input({ inputString: b64String, filename: filename, }); - await input.init(); - expect(input.inputType).to.equals(INPUT_TYPE_BASE64); - expect(input.filename).to.equals(filename); - expect(input.mimeType).to.equals("image/jpeg"); + await inputSource.init(); + expect(inputSource.inputType).to.equals(INPUT_TYPE_BASE64); + expect(inputSource.filename).to.equals(filename); + expect(inputSource.mimeType).to.equals("image/jpeg"); + expect(inputSource.isPdf()).to.false; + expect(await inputSource.getPageCount()).to.equals(1); // we need to insert a newline very 76 chars to match the format // of the input file. - const expectedString = input.fileObject + const expectedString = inputSource.fileObject .toString("base64") .replace(/(.{76})/gm, "$1\n"); expect(expectedString).to.eqls(b64String); }); it("should accept JPEG files from a path", async () => { - const input = new PathInput({ + const inputSource = new PathInput({ inputPath: path.join(__dirname, "../data/products/expense_receipts/default_sample.jpg"), }); - await input.init(); + await inputSource.init(); const expectedResult = await fs.promises.readFile( path.join(__dirname, "../data/products/expense_receipts/default_sample.jpg") ); - expect(input.inputType).to.equals(INPUT_TYPE_PATH); - expect(input.filename).to.equals("default_sample.jpg"); - expect(input.mimeType).to.equals("image/jpeg"); - expect(input.fileObject).to.eqls(expectedResult); + expect(inputSource.inputType).to.equals(INPUT_TYPE_PATH); + expect(inputSource.filename).to.equals("default_sample.jpg"); + expect(inputSource.mimeType).to.equals("image/jpeg"); + expect(inputSource.isPdf()).to.false; + expect(await inputSource.getPageCount()).to.equals(1); + expect(inputSource.fileObject).to.eqls(expectedResult); }); it("should accept TIFF from a path", async () => { - const input = new PathInput({ + const inputSource = new PathInput({ inputPath: path.join(__dirname, "../data/file_types/receipt.tif"), }); - await input.init(); + await inputSource.init(); const expectedResult = await fs.promises.readFile( path.join(__dirname, "../data/file_types/receipt.tif") ); - expect(input.inputType).to.equals(INPUT_TYPE_PATH); - expect(input.filename).to.equals("receipt.tif"); - expect(input.mimeType).to.equals("image/tiff"); - expect(input.fileObject).to.eqls(expectedResult); + expect(inputSource.inputType).to.equals(INPUT_TYPE_PATH); + expect(inputSource.filename).to.equals("receipt.tif"); + expect(inputSource.mimeType).to.equals("image/tiff"); + expect(inputSource.isPdf()).to.false; + expect(await inputSource.getPageCount()).to.equals(1); + expect(inputSource.fileObject).to.eqls(expectedResult); }); it("should accept HEIC from a path", async () => { - const input = new PathInput({ + const inputSource = new PathInput({ inputPath: path.join(__dirname, "../data/file_types/receipt.heic"), }); - await input.init(); + await inputSource.init(); const expectedResult = await fs.promises.readFile( path.join(__dirname, "../data/file_types/receipt.heic") ); - expect(input.inputType).to.equals(INPUT_TYPE_PATH); - expect(input.filename).to.equals("receipt.heic"); - expect(input.mimeType).to.equals("image/heic"); - expect(input.fileObject).to.eqls(expectedResult); + expect(inputSource.inputType).to.equals(INPUT_TYPE_PATH); + expect(inputSource.filename).to.equals("receipt.heic"); + expect(inputSource.mimeType).to.equals("image/heic"); + expect(inputSource.isPdf()).to.false; + expect(await inputSource.getPageCount()).to.equals(1); + expect(inputSource.fileObject).to.eqls(expectedResult); }); it("should accept read streams", async () => { const filePath = path.join(__dirname, "../data/products/expense_receipts/default_sample.jpg"); const stream = fs.createReadStream(filePath); const filename = "default_sample.jpg"; - const input = new StreamInput({ + const inputSource = new StreamInput({ inputStream: stream, filename: filename, }); - await input.init(); - expect(input.inputType).to.equals(INPUT_TYPE_STREAM); - expect(input.filename).to.equals(filename); - expect(input.mimeType).to.equals("image/jpeg"); + await inputSource.init(); + expect(inputSource.inputType).to.equals(INPUT_TYPE_STREAM); + expect(inputSource.filename).to.equals(filename); + expect(inputSource.mimeType).to.equals("image/jpeg"); + expect(inputSource.isPdf()).to.false; + expect(await inputSource.getPageCount()).to.equals(1); const expectedResult = await fs.promises.readFile(filePath); - expect(input.fileObject.toString()).to.eqls(expectedResult.toString()); + expect(inputSource.fileObject.toString()).to.eqls(expectedResult.toString()); }); it("should accept raw bytes", async () => { @@ -116,16 +126,18 @@ describe("Test different types of input", () => { // don't provide an extension to see if we can detect MIME // type based on contents const filename = "receipt"; - const input = new BytesInput({ + const inputSource = new BytesInput({ inputBytes: inputBytes, filename: filename, }); - await input.init(); - expect(input.inputType).to.equal(INPUT_TYPE_BYTES); - expect(input.filename).to.equal(filename); - expect(input.mimeType).to.equal("image/jpeg"); + await inputSource.init(); + expect(inputSource.inputType).to.equal(INPUT_TYPE_BYTES); + expect(inputSource.filename).to.equal(filename); + expect(inputSource.mimeType).to.equal("image/jpeg"); + expect(inputSource.isPdf()).to.false; + expect(await inputSource.getPageCount()).to.equals(1); const expectedResult = await fs.promises.readFile(filePath); - expect(Buffer.compare(input.fileObject, expectedResult)).to.equal(0); + expect(Buffer.compare(inputSource.fileObject, expectedResult)).to.equal(0); }); it("should accept a Buffer", async () => { @@ -135,15 +147,16 @@ describe("Test different types of input", () => { path.join(__dirname, "../data/products/invoices/invoice_10p.pdf") ) ); - const input = new BufferInput({ + const inputSource = new BufferInput({ buffer: buffer, filename: filename, }); - await input.init(); - expect(input.inputType).to.equals(INPUT_TYPE_BUFFER); - expect(input.filename).to.equals(filename); - expect(input.isPdf()).to.be.true; - expect(input.fileObject).to.be.instanceOf(Buffer); + await inputSource.init(); + expect(inputSource.inputType).to.equals(INPUT_TYPE_BUFFER); + expect(inputSource.filename).to.equals(filename); + expect(inputSource.isPdf()).to.be.true; + expect(await inputSource.getPageCount()).to.equals(10); + expect(inputSource.fileObject).to.be.instanceOf(Buffer); });