mindee 2.1.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +11 -9
- package/package.json +3 -2
- package/src/api/endpoint.d.ts +19 -17
- package/src/api/endpoint.js +27 -39
- package/src/api/index.d.ts +2 -2
- package/src/api/index.js +2 -11
- package/src/api/response.d.ts +8 -41
- package/src/api/response.js +18 -89
- package/src/cli.js +46 -12
- package/src/client.d.ts +47 -13
- package/src/client.js +73 -27
- package/src/documents/cropper/cropperV1.d.ts +7 -0
- package/src/documents/{cropper.js → cropper/cropperV1.js} +12 -8
- package/src/documents/custom.d.ts +2 -6
- package/src/documents/custom.js +12 -7
- package/src/documents/document.d.ts +8 -3
- package/src/documents/document.js +11 -5
- package/src/documents/documentConfig.d.ts +20 -34
- package/src/documents/documentConfig.js +33 -57
- package/src/documents/{financialDocument.d.ts → financialDocument/financialDocumentV1.d.ts} +4 -4
- package/src/documents/{financialDocument.js → financialDocument/financialDocumentV1.js} +28 -22
- package/src/documents/fr/idCard/idCardV1.d.ts +3 -0
- package/src/documents/fr/idCard/idCardV1.js +7 -0
- package/src/documents/index.d.ts +15 -13
- package/src/documents/index.js +21 -18
- package/src/documents/{invoice.d.ts → invoice/invoiceV3.d.ts} +4 -4
- package/src/documents/{invoice.js → invoice/invoiceV3.js} +30 -24
- package/src/documents/{passport.d.ts → passport/passportV1.d.ts} +4 -4
- package/src/documents/{passport.js → passport/passportV1.js} +19 -14
- package/src/documents/receipt/receiptV3.d.ts +17 -0
- package/src/documents/{receipt.js → receipt/receiptV3.js} +28 -22
- package/src/documents/receipt/receiptV4.d.ts +18 -0
- package/src/documents/receipt/receiptV4.js +95 -0
- package/src/documents/receipt/tax.d.ts +25 -0
- package/src/documents/receipt/tax.js +40 -0
- package/src/documents/us/bankCheck/bankCheckV1.d.ts +3 -0
- package/src/documents/us/bankCheck/bankCheckV1.js +7 -0
- package/src/errors/MindeeError.d.ts +3 -0
- package/src/errors/MindeeError.js +10 -0
- package/src/errors/index.d.ts +1 -0
- package/src/errors/index.js +5 -0
- package/src/fields/{customDocs.d.ts → apiBuilder.d.ts} +5 -1
- package/src/fields/{customDocs.js → apiBuilder.js} +0 -0
- package/src/fields/index.d.ts +1 -1
- package/src/fields/index.js +4 -4
- package/src/index.d.ts +2 -1
- package/src/index.js +11 -9
- package/src/inputs/base.d.ts +33 -0
- package/src/inputs/base.js +116 -0
- package/src/inputs/index.d.ts +3 -0
- package/src/inputs/index.js +17 -0
- package/src/inputs/pageOptions.d.ts +20 -0
- package/src/inputs/pageOptions.js +8 -0
- package/src/inputs/sources.d.ts +55 -0
- package/src/inputs/sources.js +121 -0
- package/src/pdf/index.d.ts +1 -0
- package/src/pdf/index.js +6 -0
- package/src/pdf/pdfOperation.d.ts +14 -0
- package/src/pdf/pdfOperation.js +69 -0
- package/src/constants.d.ts +0 -13
- package/src/constants.js +0 -60
- package/src/documents/cropper.d.ts +0 -7
- package/src/documents/receipt.d.ts +0 -17
- package/src/inputs.d.ts +0 -69
- package/src/inputs.js +0 -196
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TaxField } from "../../fields";
|
|
2
|
+
interface ReceiptTaxConstructor {
|
|
3
|
+
prediction: any;
|
|
4
|
+
valueKey?: string;
|
|
5
|
+
rateKey?: string;
|
|
6
|
+
codeKey?: string;
|
|
7
|
+
basisKey?: string;
|
|
8
|
+
reconstructed?: boolean;
|
|
9
|
+
pageId?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class ReceiptTaxField extends TaxField {
|
|
12
|
+
/** The tax base */
|
|
13
|
+
basis?: number;
|
|
14
|
+
/**
|
|
15
|
+
* @param {Object} prediction - Prediction object from HTTP response
|
|
16
|
+
* @param {String} valueKey - Key to use in the prediction dict to get the tax value
|
|
17
|
+
* @param {String} rateKey - Key to use to get the tax rate in the prediction dict
|
|
18
|
+
* @param {String} codeKey - Key to use to get the tax code in the prediction dict
|
|
19
|
+
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
20
|
+
* @param {Integer} pageNumber - Page ID for multi-page document
|
|
21
|
+
*/
|
|
22
|
+
constructor({ prediction, valueKey, rateKey, codeKey, basisKey, reconstructed, pageId, }: ReceiptTaxConstructor);
|
|
23
|
+
toString(): string;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReceiptTaxField = void 0;
|
|
4
|
+
const fields_1 = require("../../fields");
|
|
5
|
+
const amount_1 = require("../../fields/amount");
|
|
6
|
+
class ReceiptTaxField extends fields_1.TaxField {
|
|
7
|
+
/**
|
|
8
|
+
* @param {Object} prediction - Prediction object from HTTP response
|
|
9
|
+
* @param {String} valueKey - Key to use in the prediction dict to get the tax value
|
|
10
|
+
* @param {String} rateKey - Key to use to get the tax rate in the prediction dict
|
|
11
|
+
* @param {String} codeKey - Key to use to get the tax code in the prediction dict
|
|
12
|
+
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
13
|
+
* @param {Integer} pageNumber - Page ID for multi-page document
|
|
14
|
+
*/
|
|
15
|
+
constructor({ prediction, valueKey = "value", rateKey = "rate", codeKey = "code", basisKey = "basis", reconstructed = false, pageId = undefined, }) {
|
|
16
|
+
super({ prediction, valueKey, rateKey, codeKey, reconstructed, pageId });
|
|
17
|
+
/** The tax base */
|
|
18
|
+
this.basis = undefined;
|
|
19
|
+
this.basis = parseFloat(prediction[basisKey]);
|
|
20
|
+
if (isNaN(this.basis))
|
|
21
|
+
this.basis = undefined;
|
|
22
|
+
}
|
|
23
|
+
toString() {
|
|
24
|
+
let outStr = "";
|
|
25
|
+
if (this.value !== undefined) {
|
|
26
|
+
outStr += `${(0, amount_1.floatToString)(this.value)}`;
|
|
27
|
+
}
|
|
28
|
+
if (this.rate !== undefined) {
|
|
29
|
+
outStr += ` ${(0, amount_1.floatToString)(this.rate)}%`;
|
|
30
|
+
}
|
|
31
|
+
if (this.code !== undefined) {
|
|
32
|
+
outStr += ` ${this.code}`;
|
|
33
|
+
}
|
|
34
|
+
if (this.basis !== undefined) {
|
|
35
|
+
outStr += ` ${this.basis}`;
|
|
36
|
+
}
|
|
37
|
+
return outStr.trim();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ReceiptTaxField = ReceiptTaxField;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MindeeError = void 0;
|
|
4
|
+
class MindeeError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "MindeeError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.MindeeError = MindeeError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MindeeError } from "./MindeeError";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MindeeError = void 0;
|
|
4
|
+
var MindeeError_1 = require("./MindeeError");
|
|
5
|
+
Object.defineProperty(exports, "MindeeError", { enumerable: true, get: function () { return MindeeError_1.MindeeError; } });
|
|
@@ -2,6 +2,10 @@ import { FieldConstructor, stringDict } from "./field";
|
|
|
2
2
|
import { Polygon } from "../geometry";
|
|
3
3
|
export declare class ClassificationField {
|
|
4
4
|
value: string;
|
|
5
|
+
/**
|
|
6
|
+
* The confidence score of the prediction.
|
|
7
|
+
* Note: Score is calculated on **word selection**, not its textual content (OCR).
|
|
8
|
+
*/
|
|
5
9
|
confidence: number;
|
|
6
10
|
constructor({ prediction }: {
|
|
7
11
|
prediction: stringDict;
|
|
@@ -34,7 +38,7 @@ export declare class ListField {
|
|
|
34
38
|
/** True if the field was reconstructed or computed using other fields. */
|
|
35
39
|
reconstructed: boolean;
|
|
36
40
|
/** The document page on which the information was found. */
|
|
37
|
-
pageId
|
|
41
|
+
pageId: number;
|
|
38
42
|
constructor({ prediction, reconstructed, pageId }: FieldConstructor);
|
|
39
43
|
contentsList(): Array<any>;
|
|
40
44
|
contentsString(separator?: string): string;
|
|
File without changes
|
package/src/fields/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { Locale } from "./locale";
|
|
|
5
5
|
export { Amount } from "./amount";
|
|
6
6
|
export { DateField } from "./date";
|
|
7
7
|
export { BaseField, stringDict, Field } from "./field";
|
|
8
|
-
export { ListField, ListFieldValue, ClassificationField } from "./
|
|
8
|
+
export { ListField, ListFieldValue, ClassificationField } from "./apiBuilder";
|
|
9
9
|
export { FullText } from "./fullText";
|
|
10
10
|
export { CompanyRegistration } from "./companyRegistration";
|
|
11
11
|
export { CropperField } from "./cropper";
|
package/src/fields/index.js
CHANGED
|
@@ -16,10 +16,10 @@ Object.defineProperty(exports, "DateField", { enumerable: true, get: function ()
|
|
|
16
16
|
var field_1 = require("./field");
|
|
17
17
|
Object.defineProperty(exports, "BaseField", { enumerable: true, get: function () { return field_1.BaseField; } });
|
|
18
18
|
Object.defineProperty(exports, "Field", { enumerable: true, get: function () { return field_1.Field; } });
|
|
19
|
-
var
|
|
20
|
-
Object.defineProperty(exports, "ListField", { enumerable: true, get: function () { return
|
|
21
|
-
Object.defineProperty(exports, "ListFieldValue", { enumerable: true, get: function () { return
|
|
22
|
-
Object.defineProperty(exports, "ClassificationField", { enumerable: true, get: function () { return
|
|
19
|
+
var apiBuilder_1 = require("./apiBuilder");
|
|
20
|
+
Object.defineProperty(exports, "ListField", { enumerable: true, get: function () { return apiBuilder_1.ListField; } });
|
|
21
|
+
Object.defineProperty(exports, "ListFieldValue", { enumerable: true, get: function () { return apiBuilder_1.ListFieldValue; } });
|
|
22
|
+
Object.defineProperty(exports, "ClassificationField", { enumerable: true, get: function () { return apiBuilder_1.ClassificationField; } });
|
|
23
23
|
var fullText_1 = require("./fullText");
|
|
24
24
|
Object.defineProperty(exports, "FullText", { enumerable: true, get: function () { return fullText_1.FullText; } });
|
|
25
25
|
var companyRegistration_1 = require("./companyRegistration");
|
package/src/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { CustomV1, FinancialDocumentV1, InvoiceV3, PassportV1, ReceiptV3, ReceiptV4, CropperV1, } from "./documents";
|
|
2
2
|
export { Client } from "./client";
|
|
3
|
+
export { PageOptionsOperation } from "./inputs";
|
package/src/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Client = exports.
|
|
4
|
-
var
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
Object.defineProperty(exports, "
|
|
7
|
-
Object.defineProperty(exports, "
|
|
8
|
-
Object.defineProperty(exports, "
|
|
9
|
-
Object.defineProperty(exports, "
|
|
10
|
-
Object.defineProperty(exports, "
|
|
11
|
-
Object.defineProperty(exports, "
|
|
3
|
+
exports.PageOptionsOperation = exports.Client = exports.CropperV1 = exports.ReceiptV4 = exports.ReceiptV3 = exports.PassportV1 = exports.InvoiceV3 = exports.FinancialDocumentV1 = exports.CustomV1 = void 0;
|
|
4
|
+
var documents_1 = require("./documents");
|
|
5
|
+
Object.defineProperty(exports, "CustomV1", { enumerable: true, get: function () { return documents_1.CustomV1; } });
|
|
6
|
+
Object.defineProperty(exports, "FinancialDocumentV1", { enumerable: true, get: function () { return documents_1.FinancialDocumentV1; } });
|
|
7
|
+
Object.defineProperty(exports, "InvoiceV3", { enumerable: true, get: function () { return documents_1.InvoiceV3; } });
|
|
8
|
+
Object.defineProperty(exports, "PassportV1", { enumerable: true, get: function () { return documents_1.PassportV1; } });
|
|
9
|
+
Object.defineProperty(exports, "ReceiptV3", { enumerable: true, get: function () { return documents_1.ReceiptV3; } });
|
|
10
|
+
Object.defineProperty(exports, "ReceiptV4", { enumerable: true, get: function () { return documents_1.ReceiptV4; } });
|
|
11
|
+
Object.defineProperty(exports, "CropperV1", { enumerable: true, get: function () { return documents_1.CropperV1; } });
|
|
12
12
|
var client_1 = require("./client");
|
|
13
13
|
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
|
|
14
|
+
var inputs_1 = require("./inputs");
|
|
15
|
+
Object.defineProperty(exports, "PageOptionsOperation", { enumerable: true, get: function () { return inputs_1.PageOptionsOperation; } });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { PageOptions } from "./pageOptions";
|
|
3
|
+
interface InputProps {
|
|
4
|
+
inputType: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const INPUT_TYPE_STREAM = "stream";
|
|
7
|
+
export declare const INPUT_TYPE_BASE64 = "base64";
|
|
8
|
+
export declare const INPUT_TYPE_BYTES = "bytes";
|
|
9
|
+
export declare const INPUT_TYPE_PATH = "path";
|
|
10
|
+
export declare const INPUT_TYPE_URL = "URL";
|
|
11
|
+
export declare class InputSource {
|
|
12
|
+
inputType: string;
|
|
13
|
+
filename: string;
|
|
14
|
+
filepath?: string;
|
|
15
|
+
mimeType: string;
|
|
16
|
+
fileObject: Buffer | string;
|
|
17
|
+
/**
|
|
18
|
+
* @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
|
|
19
|
+
* NB: dummy is only used for tests purposes
|
|
20
|
+
* @param {Boolean} cutPages
|
|
21
|
+
* NB: Because of async calls, init() should be called after creating the object
|
|
22
|
+
*/
|
|
23
|
+
constructor({ inputType }: InputProps);
|
|
24
|
+
init(): Promise<void>;
|
|
25
|
+
isPdf(): boolean;
|
|
26
|
+
checkMimetype(): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Merge PDF pages.
|
|
29
|
+
* @param pageOptions
|
|
30
|
+
*/
|
|
31
|
+
cutPdf(pageOptions: PageOptions): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.InputSource = exports.INPUT_TYPE_URL = exports.INPUT_TYPE_PATH = exports.INPUT_TYPE_BYTES = exports.INPUT_TYPE_BASE64 = exports.INPUT_TYPE_STREAM = void 0;
|
|
27
|
+
const fileType = __importStar(require("file-type"));
|
|
28
|
+
const path = __importStar(require("path"));
|
|
29
|
+
const pdf_1 = require("../pdf");
|
|
30
|
+
const logger_1 = require("../logger");
|
|
31
|
+
const handler_1 = require("../errors/handler");
|
|
32
|
+
exports.INPUT_TYPE_STREAM = "stream";
|
|
33
|
+
exports.INPUT_TYPE_BASE64 = "base64";
|
|
34
|
+
exports.INPUT_TYPE_BYTES = "bytes";
|
|
35
|
+
exports.INPUT_TYPE_PATH = "path";
|
|
36
|
+
exports.INPUT_TYPE_URL = "URL";
|
|
37
|
+
const MIMETYPES = new Map([
|
|
38
|
+
[".pdf", "application/pdf"],
|
|
39
|
+
[".heic", "image/heic"],
|
|
40
|
+
[".jpg", "image/jpeg"],
|
|
41
|
+
[".jpeg", "image/jpeg"],
|
|
42
|
+
[".png", "image/png"],
|
|
43
|
+
[".tif", "image/tiff"],
|
|
44
|
+
[".tiff", "image/tiff"],
|
|
45
|
+
[".webp", "image/webp"],
|
|
46
|
+
]);
|
|
47
|
+
const ALLOWED_INPUT_TYPES = [
|
|
48
|
+
exports.INPUT_TYPE_STREAM,
|
|
49
|
+
exports.INPUT_TYPE_BASE64,
|
|
50
|
+
exports.INPUT_TYPE_BYTES,
|
|
51
|
+
exports.INPUT_TYPE_PATH,
|
|
52
|
+
exports.INPUT_TYPE_URL,
|
|
53
|
+
];
|
|
54
|
+
class InputSource {
|
|
55
|
+
/**
|
|
56
|
+
* @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
|
|
57
|
+
* NB: dummy is only used for tests purposes
|
|
58
|
+
* @param {Boolean} cutPages
|
|
59
|
+
* NB: Because of async calls, init() should be called after creating the object
|
|
60
|
+
*/
|
|
61
|
+
constructor({ inputType }) {
|
|
62
|
+
this.filename = "";
|
|
63
|
+
this.mimeType = "";
|
|
64
|
+
// Check if inputType is valid
|
|
65
|
+
if (!ALLOWED_INPUT_TYPES.includes(inputType)) {
|
|
66
|
+
const allowed = Array.from(ALLOWED_INPUT_TYPES.keys()).join(", ");
|
|
67
|
+
handler_1.errorHandler.throw(new Error(`Invalid input type, must be one of ${allowed}.`));
|
|
68
|
+
}
|
|
69
|
+
this.inputType = inputType;
|
|
70
|
+
logger_1.logger.debug(`Loading file from: ${inputType}`);
|
|
71
|
+
}
|
|
72
|
+
async init() {
|
|
73
|
+
throw new Error("not Implemented");
|
|
74
|
+
}
|
|
75
|
+
isPdf() {
|
|
76
|
+
return this.mimeType === "application/pdf";
|
|
77
|
+
}
|
|
78
|
+
async checkMimetype() {
|
|
79
|
+
if (!(this.fileObject instanceof Buffer)) {
|
|
80
|
+
throw new Error(`MIME type cannot be verified on input source of type ${this.inputType}.`);
|
|
81
|
+
}
|
|
82
|
+
let mimeType;
|
|
83
|
+
const fileExt = path.extname(this.filename);
|
|
84
|
+
if (fileExt) {
|
|
85
|
+
mimeType = MIMETYPES.get(fileExt.toLowerCase()) || "";
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const guess = await fileType.fromBuffer(this.fileObject);
|
|
89
|
+
if (guess !== undefined) {
|
|
90
|
+
mimeType = guess.mime;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw "Could not determine the MIME type of the file";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!mimeType) {
|
|
97
|
+
const allowed = Array.from(MIMETYPES.keys()).join(", ");
|
|
98
|
+
const err = new Error(`Invalid file type, must be one of ${allowed}.`);
|
|
99
|
+
handler_1.errorHandler.throw(err);
|
|
100
|
+
}
|
|
101
|
+
logger_1.logger.debug(`File is of type: ${mimeType}`);
|
|
102
|
+
return mimeType;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Merge PDF pages.
|
|
106
|
+
* @param pageOptions
|
|
107
|
+
*/
|
|
108
|
+
async cutPdf(pageOptions) {
|
|
109
|
+
if (!(this.fileObject instanceof Buffer)) {
|
|
110
|
+
throw new Error(`Cannot modify an input source of type ${this.inputType}.`);
|
|
111
|
+
}
|
|
112
|
+
const processedPdf = await (0, pdf_1.extractPages)(this.fileObject, pageOptions);
|
|
113
|
+
this.fileObject = processedPdf.file;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.InputSource = InputSource;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INPUT_TYPE_STREAM = exports.INPUT_TYPE_PATH = exports.INPUT_TYPE_BYTES = exports.INPUT_TYPE_BASE64 = exports.InputSource = exports.UrlInput = exports.StreamInput = exports.PathInput = exports.BytesInput = exports.Base64Input = exports.PageOptionsOperation = void 0;
|
|
4
|
+
var pageOptions_1 = require("./pageOptions");
|
|
5
|
+
Object.defineProperty(exports, "PageOptionsOperation", { enumerable: true, get: function () { return pageOptions_1.PageOptionsOperation; } });
|
|
6
|
+
var sources_1 = require("./sources");
|
|
7
|
+
Object.defineProperty(exports, "Base64Input", { enumerable: true, get: function () { return sources_1.Base64Input; } });
|
|
8
|
+
Object.defineProperty(exports, "BytesInput", { enumerable: true, get: function () { return sources_1.BytesInput; } });
|
|
9
|
+
Object.defineProperty(exports, "PathInput", { enumerable: true, get: function () { return sources_1.PathInput; } });
|
|
10
|
+
Object.defineProperty(exports, "StreamInput", { enumerable: true, get: function () { return sources_1.StreamInput; } });
|
|
11
|
+
Object.defineProperty(exports, "UrlInput", { enumerable: true, get: function () { return sources_1.UrlInput; } });
|
|
12
|
+
var base_1 = require("./base");
|
|
13
|
+
Object.defineProperty(exports, "InputSource", { enumerable: true, get: function () { return base_1.InputSource; } });
|
|
14
|
+
Object.defineProperty(exports, "INPUT_TYPE_BASE64", { enumerable: true, get: function () { return base_1.INPUT_TYPE_BASE64; } });
|
|
15
|
+
Object.defineProperty(exports, "INPUT_TYPE_BYTES", { enumerable: true, get: function () { return base_1.INPUT_TYPE_BYTES; } });
|
|
16
|
+
Object.defineProperty(exports, "INPUT_TYPE_PATH", { enumerable: true, get: function () { return base_1.INPUT_TYPE_PATH; } });
|
|
17
|
+
Object.defineProperty(exports, "INPUT_TYPE_STREAM", { enumerable: true, get: function () { return base_1.INPUT_TYPE_STREAM; } });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface PageOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Zero-based list of page indexes.
|
|
4
|
+
*
|
|
5
|
+
* A negative index can be used, indicating an offset from the end of the document.
|
|
6
|
+
*
|
|
7
|
+
* [0, -1] represents the fist and last pages of the document.
|
|
8
|
+
*/
|
|
9
|
+
pageIndexes: number[];
|
|
10
|
+
/** Operation to apply on the document, given the `pageIndexes` specified. */
|
|
11
|
+
operation: PageOptionsOperation;
|
|
12
|
+
/**
|
|
13
|
+
* Apply the operation only if document has at least this many pages.
|
|
14
|
+
*/
|
|
15
|
+
onMinPages: number;
|
|
16
|
+
}
|
|
17
|
+
export declare enum PageOptionsOperation {
|
|
18
|
+
KeepOnly = "KEEP_ONLY",
|
|
19
|
+
Remove = "REMOVE"
|
|
20
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PageOptionsOperation = void 0;
|
|
4
|
+
var PageOptionsOperation;
|
|
5
|
+
(function (PageOptionsOperation) {
|
|
6
|
+
PageOptionsOperation["KeepOnly"] = "KEEP_ONLY";
|
|
7
|
+
PageOptionsOperation["Remove"] = "REMOVE";
|
|
8
|
+
})(PageOptionsOperation = exports.PageOptionsOperation || (exports.PageOptionsOperation = {}));
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { ReadStream } from "fs";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { InputSource } from "./base";
|
|
6
|
+
interface PathInputProps {
|
|
7
|
+
inputPath: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class PathInput extends InputSource {
|
|
10
|
+
readonly inputPath: string;
|
|
11
|
+
fileObject: Buffer;
|
|
12
|
+
constructor({ inputPath }: PathInputProps);
|
|
13
|
+
init(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
interface Base64InputProps {
|
|
16
|
+
inputString: string;
|
|
17
|
+
filename: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class Base64Input extends InputSource {
|
|
20
|
+
private inputString;
|
|
21
|
+
fileObject: Buffer;
|
|
22
|
+
constructor({ inputString, filename }: Base64InputProps);
|
|
23
|
+
init(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
interface StreamInputProps {
|
|
26
|
+
inputStream: ReadStream;
|
|
27
|
+
filename: string;
|
|
28
|
+
}
|
|
29
|
+
export declare class StreamInput extends InputSource {
|
|
30
|
+
private readonly inputStream;
|
|
31
|
+
fileObject: Buffer;
|
|
32
|
+
constructor({ inputStream, filename }: StreamInputProps);
|
|
33
|
+
init(): Promise<void>;
|
|
34
|
+
stream2buffer(stream: ReadStream): Promise<Buffer>;
|
|
35
|
+
}
|
|
36
|
+
interface BytesInputProps {
|
|
37
|
+
inputBytes: string;
|
|
38
|
+
filename: string;
|
|
39
|
+
}
|
|
40
|
+
export declare class BytesInput extends InputSource {
|
|
41
|
+
private inputBytes;
|
|
42
|
+
fileObject: Buffer;
|
|
43
|
+
constructor({ inputBytes, filename }: BytesInputProps);
|
|
44
|
+
init(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
interface UrlInputProps {
|
|
47
|
+
url: string;
|
|
48
|
+
}
|
|
49
|
+
export declare class UrlInput extends InputSource {
|
|
50
|
+
private readonly url;
|
|
51
|
+
fileObject: string;
|
|
52
|
+
constructor({ url }: UrlInputProps);
|
|
53
|
+
init(): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.UrlInput = exports.BytesInput = exports.StreamInput = exports.Base64Input = exports.PathInput = void 0;
|
|
27
|
+
const fs_1 = require("fs");
|
|
28
|
+
const path = __importStar(require("path"));
|
|
29
|
+
const handler_1 = require("../errors/handler");
|
|
30
|
+
const node_buffer_1 = require("node:buffer");
|
|
31
|
+
const logger_1 = require("../logger");
|
|
32
|
+
const base_1 = require("./base");
|
|
33
|
+
class PathInput extends base_1.InputSource {
|
|
34
|
+
constructor({ inputPath }) {
|
|
35
|
+
super({
|
|
36
|
+
inputType: base_1.INPUT_TYPE_PATH,
|
|
37
|
+
});
|
|
38
|
+
this.fileObject = node_buffer_1.Buffer.alloc(0);
|
|
39
|
+
this.inputPath = inputPath;
|
|
40
|
+
this.filename = path.basename(this.inputPath);
|
|
41
|
+
}
|
|
42
|
+
async init() {
|
|
43
|
+
logger_1.logger.debug(`Loading from: ${this.inputPath}`);
|
|
44
|
+
this.fileObject = node_buffer_1.Buffer.from(await fs_1.promises.readFile(this.inputPath));
|
|
45
|
+
this.mimeType = await this.checkMimetype();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.PathInput = PathInput;
|
|
49
|
+
class Base64Input extends base_1.InputSource {
|
|
50
|
+
constructor({ inputString, filename }) {
|
|
51
|
+
super({
|
|
52
|
+
inputType: base_1.INPUT_TYPE_BASE64,
|
|
53
|
+
});
|
|
54
|
+
this.fileObject = node_buffer_1.Buffer.alloc(0);
|
|
55
|
+
this.filename = filename;
|
|
56
|
+
this.inputString = inputString;
|
|
57
|
+
}
|
|
58
|
+
async init() {
|
|
59
|
+
this.fileObject = node_buffer_1.Buffer.from(this.inputString, "base64");
|
|
60
|
+
this.mimeType = await this.checkMimetype();
|
|
61
|
+
// clear out the string
|
|
62
|
+
this.inputString = "";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.Base64Input = Base64Input;
|
|
66
|
+
class StreamInput extends base_1.InputSource {
|
|
67
|
+
constructor({ inputStream, filename }) {
|
|
68
|
+
super({
|
|
69
|
+
inputType: base_1.INPUT_TYPE_STREAM,
|
|
70
|
+
});
|
|
71
|
+
this.fileObject = node_buffer_1.Buffer.alloc(0);
|
|
72
|
+
this.filename = filename;
|
|
73
|
+
this.inputStream = inputStream;
|
|
74
|
+
}
|
|
75
|
+
async init() {
|
|
76
|
+
this.fileObject = await this.stream2buffer(this.inputStream);
|
|
77
|
+
this.mimeType = await this.checkMimetype();
|
|
78
|
+
}
|
|
79
|
+
async stream2buffer(stream) {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
const _buf = Array();
|
|
82
|
+
stream.on("data", (chunk) => _buf.push(chunk));
|
|
83
|
+
stream.on("end", () => resolve(node_buffer_1.Buffer.concat(_buf)));
|
|
84
|
+
stream.on("error", (err) => reject(`Error converting stream - ${err}`));
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.StreamInput = StreamInput;
|
|
89
|
+
class BytesInput extends base_1.InputSource {
|
|
90
|
+
constructor({ inputBytes, filename }) {
|
|
91
|
+
super({
|
|
92
|
+
inputType: base_1.INPUT_TYPE_BYTES,
|
|
93
|
+
});
|
|
94
|
+
this.fileObject = node_buffer_1.Buffer.alloc(0);
|
|
95
|
+
this.filename = filename;
|
|
96
|
+
this.inputBytes = inputBytes;
|
|
97
|
+
}
|
|
98
|
+
async init() {
|
|
99
|
+
this.fileObject = node_buffer_1.Buffer.from(this.inputBytes, "hex");
|
|
100
|
+
this.mimeType = await this.checkMimetype();
|
|
101
|
+
// clear out the string
|
|
102
|
+
this.inputBytes = "";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.BytesInput = BytesInput;
|
|
106
|
+
class UrlInput extends base_1.InputSource {
|
|
107
|
+
constructor({ url }) {
|
|
108
|
+
super({
|
|
109
|
+
inputType: base_1.INPUT_TYPE_URL,
|
|
110
|
+
});
|
|
111
|
+
this.url = url;
|
|
112
|
+
}
|
|
113
|
+
async init() {
|
|
114
|
+
if (!this.url.toLowerCase().startsWith("https")) {
|
|
115
|
+
handler_1.errorHandler.throw(new Error("URL must be HTTPS"));
|
|
116
|
+
}
|
|
117
|
+
this.fileObject = this.url;
|
|
118
|
+
this.filename = "hello.jpg";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.UrlInput = UrlInput;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extractPages, countPages, SplitPdf } from "./pdfOperation";
|
package/src/pdf/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.countPages = exports.extractPages = void 0;
|
|
4
|
+
var pdfOperation_1 = require("./pdfOperation");
|
|
5
|
+
Object.defineProperty(exports, "extractPages", { enumerable: true, get: function () { return pdfOperation_1.extractPages; } });
|
|
6
|
+
Object.defineProperty(exports, "countPages", { enumerable: true, get: function () { return pdfOperation_1.countPages; } });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { PageOptions } from "../inputs";
|
|
3
|
+
export interface SplitPdf {
|
|
4
|
+
file: Buffer;
|
|
5
|
+
totalPagesRemoved: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Cut pages from a pdf file. If pages index are out of bound, it will throw an error.
|
|
9
|
+
* @param file
|
|
10
|
+
* @param pageOptions
|
|
11
|
+
* @returns the new cutted pdf file.
|
|
12
|
+
*/
|
|
13
|
+
export declare function extractPages(file: Buffer, pageOptions: PageOptions): Promise<SplitPdf>;
|
|
14
|
+
export declare function countPages(file: Buffer): Promise<number>;
|