chromiumly 2.3.1 → 2.5.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/dist/chromium/converters/converter.js.map +1 -1
- package/dist/chromium/converters/html.converter.d.ts +4 -6
- package/dist/chromium/converters/html.converter.js +3 -4
- package/dist/chromium/converters/html.converter.js.map +1 -1
- package/dist/chromium/converters/markdown.converter.d.ts +5 -7
- package/dist/chromium/converters/markdown.converter.js +3 -6
- package/dist/chromium/converters/markdown.converter.js.map +1 -1
- package/dist/chromium/converters/url.converter.d.ts +3 -5
- package/dist/chromium/converters/url.converter.js +1 -1
- package/dist/chromium/converters/url.converter.js.map +1 -1
- package/dist/chromium/index.js.map +1 -1
- package/dist/chromium/interfaces/converter.types.d.ts +3 -5
- package/dist/chromium/utils/converter.utils.d.ts +3 -1
- package/dist/chromium/utils/converter.utils.js +26 -11
- package/dist/chromium/utils/converter.utils.js.map +1 -1
- package/dist/common/constants.js.map +1 -1
- package/dist/common/gotenberg.utils.js.map +1 -1
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js.map +1 -1
- package/dist/common/types.d.ts +3 -0
- package/dist/common/types.js +3 -0
- package/dist/common/types.js.map +1 -0
- package/dist/gotenberg.js.map +1 -1
- package/dist/libre-office/index.js.map +1 -1
- package/dist/libre-office/utils/constants.js.map +1 -1
- package/dist/libre-office/utils/libre-office.utils.d.ts +3 -4
- package/dist/libre-office/utils/libre-office.utils.js +18 -9
- package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
- package/dist/main.config.js.map +1 -1
- package/dist/main.js.map +1 -1
- package/dist/pdf-engines/index.js.map +1 -1
- package/dist/pdf-engines/pdf.engine.js +3 -3
- package/dist/pdf-engines/pdf.engine.js.map +1 -1
- package/dist/pdf-engines/utils/engine.utils.d.ts +2 -3
- package/dist/pdf-engines/utils/engine.utils.js +17 -8
- package/dist/pdf-engines/utils/engine.utils.js.map +1 -1
- package/package.json +7 -7
- package/src/chromium/converters/converter.ts +5 -5
- package/src/chromium/converters/html.converter.ts +54 -56
- package/src/chromium/converters/markdown.converter.ts +57 -60
- package/src/chromium/converters/tests/html.converter.test.ts +121 -126
- package/src/chromium/converters/tests/markdown.converter.test.ts +132 -139
- package/src/chromium/converters/tests/url.converter.test.ts +114 -123
- package/src/chromium/converters/url.converter.ts +55 -57
- package/src/chromium/index.ts +3 -3
- package/src/chromium/interfaces/converter.types.ts +27 -27
- package/src/chromium/utils/converter.utils.ts +141 -129
- package/src/chromium/utils/tests/converter.utils.test.ts +315 -280
- package/src/common/constants.ts +3 -3
- package/src/common/gotenberg.utils.ts +16 -16
- package/src/common/index.ts +3 -2
- package/src/common/tests/gotenberg.utils.test.ts +54 -54
- package/src/common/types.ts +3 -0
- package/src/gotenberg.ts +4 -4
- package/src/libre-office/index.ts +2 -2
- package/src/libre-office/interfaces/libre-office.types.ts +2 -2
- package/src/libre-office/utils/constants.ts +76 -76
- package/src/libre-office/utils/libre-office.utils.ts +42 -35
- package/src/libre-office/utils/tests/libre-office.utils.test.ts +81 -69
- package/src/main.config.ts +22 -21
- package/src/main.ts +3 -3
- package/src/pdf-engines/index.ts +1 -1
- package/src/pdf-engines/pdf.engine.ts +49 -49
- package/src/pdf-engines/tests/pdf.engine.test.ts +94 -94
- package/src/pdf-engines/utils/engine.utils.ts +20 -12
- package/src/pdf-engines/utils/tests/engine.utils.test.ts +60 -48
package/src/main.config.ts
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {Gotenberg} from "./gotenberg";
|
|
2
2
|
|
|
3
3
|
export enum ChromiumRoute {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
URL = "url",
|
|
5
|
+
HTML = "html",
|
|
6
|
+
MARKDOWN = "markdown",
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
enum PdfEngineRoute {
|
|
10
|
-
|
|
10
|
+
MERGE = "merge",
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
enum LibreOfficeRoute {
|
|
14
|
-
|
|
14
|
+
CONVERT = "convert",
|
|
15
15
|
}
|
|
16
|
+
|
|
16
17
|
export class Chromiumly {
|
|
17
|
-
|
|
18
|
+
public static readonly GOTENBERG_ENDPOINT = Gotenberg.endpoint;
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
public static readonly CHROMIUM_PATH = "forms/chromium/convert";
|
|
21
|
+
public static readonly PDF_ENGINES_PATH = "forms/pdfengines";
|
|
22
|
+
public static readonly LIBRE_OFFICE_PATH = "forms/libreoffice";
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
public static readonly CHROMIUM_ROUTES = {
|
|
25
|
+
url: ChromiumRoute.URL,
|
|
26
|
+
html: ChromiumRoute.HTML,
|
|
27
|
+
markdown: ChromiumRoute.MARKDOWN,
|
|
28
|
+
};
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
public static readonly PDF_ENGINE_ROUTES = {
|
|
31
|
+
merge: PdfEngineRoute.MERGE,
|
|
32
|
+
};
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
public static readonly LIBRE_OFFICE_ROUTES = {
|
|
35
|
+
convert: LibreOfficeRoute.CONVERT,
|
|
36
|
+
};
|
|
36
37
|
}
|
package/src/main.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export {PdfFormat} from "./common/constants";
|
|
2
|
+
export {HtmlConverter, MarkdownConverter, UrlConverter} from "./chromium";
|
|
3
|
+
export {PDFEngine} from "./pdf-engines";
|
package/src/pdf-engines/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {PDFEngine} from "./pdf.engine";
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {PathLike, promises} from "fs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
|
|
4
4
|
import FormData from "form-data";
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import {Chromiumly} from "../main.config";
|
|
7
|
+
import {GotenbergUtils, PdfFormat} from "../common";
|
|
8
|
+
import {LibreOfficeUtils, PageProperties} from "../libre-office";
|
|
9
|
+
import {PDFEngineUtils} from "./utils/engine.utils";
|
|
10
10
|
|
|
11
11
|
export class PDFEngine {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
public static async merge({files}: { files: PathLike[] }): Promise<Buffer> {
|
|
13
|
+
const data = new FormData();
|
|
14
|
+
await PDFEngineUtils.addFiles(files, data);
|
|
15
|
+
const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.merge}`;
|
|
16
|
+
return GotenbergUtils.fetch(endpoint, data);
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
19
|
+
public static async convert({
|
|
20
|
+
files,
|
|
21
|
+
properties,
|
|
22
|
+
pdfFormat,
|
|
23
|
+
pdfUA,
|
|
24
|
+
merge,
|
|
25
|
+
}: {
|
|
26
|
+
files: PathLike[];
|
|
27
|
+
properties?: PageProperties;
|
|
28
|
+
pdfFormat?: PdfFormat;
|
|
29
|
+
pdfUA?: boolean;
|
|
30
|
+
merge?: boolean;
|
|
31
|
+
}): Promise<Buffer> {
|
|
32
|
+
const data = new FormData();
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
if (pdfFormat) {
|
|
35
|
+
data.append("pdfa", pdfFormat);
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
if (pdfUA) {
|
|
39
|
+
data.append("pdfUA", String(pdfUA));
|
|
40
|
+
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
if (merge) {
|
|
43
|
+
data.append("merge", String(merge));
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
if (properties) {
|
|
47
|
+
LibreOfficeUtils.addPageProperties(data, properties);
|
|
48
|
+
}
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
await LibreOfficeUtils.addFiles(files, data);
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
return GotenbergUtils.fetch(endpoint, data);
|
|
55
|
+
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
public static async generate(
|
|
58
|
+
filename: string,
|
|
59
|
+
buffer: Buffer
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
const __generated__ = path.resolve(process.cwd(), "__generated__");
|
|
62
|
+
await promises.mkdir(path.resolve(__generated__), {recursive: true});
|
|
63
|
+
await promises.writeFile(path.resolve(__generated__, filename), buffer);
|
|
64
|
+
}
|
|
65
65
|
}
|
|
@@ -1,123 +1,123 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
-
import {
|
|
2
|
+
import {createReadStream, promises} from "fs";
|
|
3
3
|
import FormData from "form-data";
|
|
4
4
|
import fs from "fs/promises";
|
|
5
5
|
import fetch from "node-fetch";
|
|
6
6
|
import path from "path";
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import {PdfFormat} from "../../common";
|
|
9
|
+
import {PDFEngine} from "../pdf.engine";
|
|
10
10
|
|
|
11
|
-
const {
|
|
11
|
+
const {Response} = jest.requireActual("node-fetch");
|
|
12
12
|
jest.mock("node-fetch", () => jest.fn());
|
|
13
13
|
|
|
14
14
|
describe("PDFEngine", () => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe("convert", () => {
|
|
30
|
-
describe("when no properties are passed", () => {
|
|
31
|
-
it("should return a buffer", async () => {
|
|
32
|
-
mockPromisesAccess.mockResolvedValue();
|
|
33
|
-
mockFetch.mockResolvedValue(new Response("content"));
|
|
34
|
-
const buffer = await PDFEngine.convert({
|
|
35
|
-
files: ["path/to/file.docx", "path/to/file.bib"],
|
|
36
|
-
});
|
|
37
|
-
expect(buffer).toEqual(Buffer.from("content"));
|
|
38
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
39
|
-
});
|
|
15
|
+
const mockPromisesAccess = jest.spyOn(promises, "access");
|
|
16
|
+
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
|
|
17
|
+
const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
(createReadStream as jest.Mock) = jest
|
|
21
|
+
.fn()
|
|
22
|
+
.mockImplementation((file) => file);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
jest.resetAllMocks();
|
|
40
27
|
});
|
|
41
28
|
|
|
42
|
-
describe("
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
29
|
+
describe("convert", () => {
|
|
30
|
+
describe("when no properties are passed", () => {
|
|
31
|
+
it("should return a buffer", async () => {
|
|
32
|
+
mockPromisesAccess.mockResolvedValue();
|
|
33
|
+
mockFetch.mockResolvedValue(new Response("content"));
|
|
34
|
+
const buffer = await PDFEngine.convert({
|
|
35
|
+
files: ["path/to/file.docx", "path/to/file.bib"],
|
|
36
|
+
});
|
|
37
|
+
expect(buffer).toEqual(Buffer.from("content"));
|
|
38
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("when properties are passed", () => {
|
|
43
|
+
it("should return a buffer", async () => {
|
|
44
|
+
mockPromisesAccess.mockResolvedValue();
|
|
45
|
+
mockFetch.mockResolvedValue(new Response("content"));
|
|
46
|
+
const buffer = await PDFEngine.convert({
|
|
47
|
+
files: ["path/to/file.docx", "path/to/file.bib"],
|
|
48
|
+
properties: {landscape: true},
|
|
49
|
+
pdfFormat: PdfFormat.A_1a,
|
|
50
|
+
pdfUA: true,
|
|
51
|
+
merge: true,
|
|
52
|
+
});
|
|
53
|
+
expect(buffer).toEqual(Buffer.from("content"));
|
|
54
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
|
|
55
|
+
});
|
|
52
56
|
});
|
|
53
|
-
expect(buffer).toEqual(Buffer.from("content"));
|
|
54
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
|
|
55
|
-
});
|
|
56
57
|
});
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
|
|
59
|
+
describe("merge", () => {
|
|
60
|
+
it("should return a buffer", async () => {
|
|
61
|
+
mockPromisesAccess.mockResolvedValue();
|
|
62
|
+
mockFetch.mockResolvedValue(new Response("content"));
|
|
63
|
+
const buffer = await PDFEngine.merge({
|
|
64
|
+
files: ["path/to/file.pdf", "path/to/another-file.pdf"],
|
|
65
|
+
});
|
|
66
|
+
expect(buffer).toEqual(Buffer.from("content"));
|
|
67
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
68
|
+
});
|
|
68
69
|
});
|
|
69
|
-
});
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
describe("generate", () => {
|
|
72
|
+
const mockFilename = "test.pdf";
|
|
73
|
+
const mockBuffer = Buffer.from("mock pdf content");
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
afterAll(() => {
|
|
76
|
+
jest.restoreAllMocks();
|
|
77
|
+
});
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
it("should generate a PDF file", async () => {
|
|
80
|
+
const mockGeneratedDir = path.join(process.cwd(), "__generated__");
|
|
81
|
+
const mockGeneratedFilePath = path.join(mockGeneratedDir, mockFilename);
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
const mockPromisesMkDir = jest
|
|
84
|
+
.spyOn(fs, "mkdir")
|
|
85
|
+
.mockResolvedValueOnce(mockGeneratedDir);
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
const mockPromisesWriteFile = jest
|
|
88
|
+
.spyOn(fs, "writeFile")
|
|
89
|
+
.mockResolvedValueOnce();
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
await PDFEngine.generate(mockFilename, mockBuffer);
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
expect(mockPromisesMkDir).toHaveBeenCalledWith(mockGeneratedDir, {
|
|
94
|
+
recursive: true,
|
|
95
|
+
});
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
expect(mockPromisesWriteFile).toHaveBeenCalledWith(
|
|
98
|
+
mockGeneratedFilePath,
|
|
99
|
+
mockBuffer
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
it("should handle errors during file generation", async () => {
|
|
104
|
+
jest
|
|
105
|
+
.spyOn(fs, "mkdir")
|
|
106
|
+
.mockRejectedValueOnce(new Error("Cannot create directory"));
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
await expect(
|
|
109
|
+
PDFEngine.generate(mockFilename, mockBuffer)
|
|
110
|
+
).rejects.toThrow("Cannot create directory");
|
|
111
|
+
});
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
it("should handle errors during file writing", async () => {
|
|
114
|
+
jest
|
|
115
|
+
.spyOn(fs, "writeFile")
|
|
116
|
+
.mockRejectedValueOnce(new Error("Failed to write to file"));
|
|
117
117
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
await expect(
|
|
119
|
+
PDFEngine.generate(mockFilename, mockBuffer)
|
|
120
|
+
).rejects.toThrow("Failed to write to file");
|
|
121
|
+
});
|
|
121
122
|
});
|
|
122
|
-
});
|
|
123
123
|
});
|
|
@@ -1,19 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {constants, createReadStream, promises, ReadStream} from "fs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
|
|
4
4
|
import FormData from "form-data";
|
|
5
|
+
import {PathLikeOrReadStream} from "../../common";
|
|
5
6
|
|
|
6
7
|
export class PDFEngineUtils {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
public static async addFiles(files: PathLikeOrReadStream[], data: FormData) {
|
|
9
|
+
for (const [key, file] of files.entries()) {
|
|
10
|
+
const filename = `file${key}`
|
|
11
|
+
if (Buffer.isBuffer(file)) {
|
|
12
|
+
data.append("files", file, filename);
|
|
13
|
+
} else if (file instanceof ReadStream) {
|
|
14
|
+
data.append("files", file, filename);
|
|
15
|
+
} else {
|
|
16
|
+
await promises.access(file, constants.R_OK);
|
|
17
|
+
const filename = path.basename(file.toString());
|
|
18
|
+
const extension = path.extname(filename);
|
|
19
|
+
if (extension === ".pdf") {
|
|
20
|
+
data.append(filename, createReadStream(file));
|
|
21
|
+
} else {
|
|
22
|
+
throw new Error(`${extension} is not supported`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
17
26
|
}
|
|
18
|
-
}
|
|
19
27
|
}
|
|
@@ -1,60 +1,72 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
-
import {
|
|
2
|
+
import {createReadStream, promises} from "fs";
|
|
3
3
|
import FormData from "form-data";
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {PDFEngineUtils} from "../engine.utils";
|
|
6
6
|
|
|
7
7
|
describe("PDFEngineUtils", () => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const mockPromisesAccess = jest.spyOn(promises, "access");
|
|
9
|
+
const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
|
|
10
|
+
const data = new FormData();
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
afterEach(() => {
|
|
19
|
-
jest.resetAllMocks();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe("injectFiles", () => {
|
|
23
|
-
describe("when files exist", () => {
|
|
24
|
-
it("should append each file to data", async () => {
|
|
25
|
-
mockPromisesAccess.mockResolvedValue();
|
|
26
|
-
await PDFEngineUtils.injectFiles(
|
|
27
|
-
["path/to/file.pdf", "path/to/another-file.pdf"],
|
|
28
|
-
data
|
|
29
|
-
);
|
|
30
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
31
|
-
});
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
(createReadStream as jest.Mock) = jest
|
|
14
|
+
.fn()
|
|
15
|
+
.mockImplementation((file) => file);
|
|
32
16
|
});
|
|
33
17
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
mockPromisesAccess.mockResolvedValue();
|
|
37
|
-
await expect(() =>
|
|
38
|
-
PDFEngineUtils.injectFiles(
|
|
39
|
-
["path/to/file.docx", "path/to/file.pdf"],
|
|
40
|
-
data
|
|
41
|
-
)
|
|
42
|
-
).rejects.toThrow(".docx is not supported");
|
|
43
|
-
});
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
jest.resetAllMocks();
|
|
44
20
|
});
|
|
45
21
|
|
|
46
|
-
describe("
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
22
|
+
describe("addFiles", () => {
|
|
23
|
+
describe("when files exist", () => {
|
|
24
|
+
describe("when files parameter contains paths", () => {
|
|
25
|
+
it("should append each file to data", async () => {
|
|
26
|
+
mockPromisesAccess.mockResolvedValue();
|
|
27
|
+
await PDFEngineUtils.addFiles(
|
|
28
|
+
["path/to/file.pdf", "path/to/another-file.pdf"],
|
|
29
|
+
data
|
|
30
|
+
);
|
|
31
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
32
|
+
});
|
|
33
|
+
})
|
|
34
|
+
describe("when files parameter contains a buffer", () => {
|
|
35
|
+
it("should append each file to data", async () => {
|
|
36
|
+
mockPromisesAccess.mockResolvedValue();
|
|
37
|
+
await PDFEngineUtils.addFiles(
|
|
38
|
+
[Buffer.from("data"), "path/to/another-file.pdf"],
|
|
39
|
+
data
|
|
40
|
+
);
|
|
41
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
42
|
+
});
|
|
43
|
+
})
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("when one of the files is not PDF", () => {
|
|
47
|
+
it("should throw an error", async () => {
|
|
48
|
+
mockPromisesAccess.mockResolvedValue();
|
|
49
|
+
await expect(() =>
|
|
50
|
+
PDFEngineUtils.addFiles(
|
|
51
|
+
["path/to/file.docx", "path/to/file.pdf"],
|
|
52
|
+
data
|
|
53
|
+
)
|
|
54
|
+
).rejects.toThrow(".docx is not supported");
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("when one of the files does not exist", () => {
|
|
59
|
+
it("should throw an error", async () => {
|
|
60
|
+
const errorMessage =
|
|
61
|
+
"ENOENT: no such file or directory, access 'path/to/index.html'";
|
|
62
|
+
mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
|
|
63
|
+
await expect(() =>
|
|
64
|
+
PDFEngineUtils.addFiles(
|
|
65
|
+
["path/to/file.pdf", "path/to/another-file.pdf"],
|
|
66
|
+
data
|
|
67
|
+
)
|
|
68
|
+
).rejects.toThrow(errorMessage);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
58
71
|
});
|
|
59
|
-
});
|
|
60
72
|
});
|