chromiumly 2.4.0 → 2.5.1
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/README.md +57 -40
- package/dist/chromium/converters/converter.d.ts +13 -0
- package/dist/chromium/converters/converter.js +10 -0
- package/dist/chromium/converters/converter.js.map +1 -1
- package/dist/chromium/converters/html.converter.d.ts +32 -6
- package/dist/chromium/converters/html.converter.js +30 -9
- package/dist/chromium/converters/html.converter.js.map +1 -1
- package/dist/chromium/converters/markdown.converter.d.ts +34 -7
- package/dist/chromium/converters/markdown.converter.js +32 -16
- package/dist/chromium/converters/markdown.converter.js.map +1 -1
- package/dist/chromium/converters/url.converter.d.ts +31 -5
- package/dist/chromium/converters/url.converter.js +29 -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 -6
- package/dist/chromium/utils/converter.utils.d.ts +27 -1
- package/dist/chromium/utils/converter.utils.js +48 -21
- package/dist/chromium/utils/converter.utils.js.map +1 -1
- package/dist/common/constants.js.map +1 -1
- package/dist/common/gotenberg.utils.d.ts +18 -0
- package/dist/common/gotenberg.utils.js +18 -0
- 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.d.ts +8 -0
- package/dist/gotenberg.js +8 -0
- package/dist/gotenberg.js.map +1 -1
- package/dist/libre-office/index.js.map +1 -1
- package/dist/libre-office/utils/constants.js +76 -76
- package/dist/libre-office/utils/constants.js.map +1 -1
- package/dist/libre-office/utils/libre-office.utils.d.ts +19 -4
- package/dist/libre-office/utils/libre-office.utils.js +39 -7
- package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
- package/dist/main.config.d.ts +43 -0
- package/dist/main.config.js +43 -0
- 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.d.ts +31 -5
- package/dist/pdf-engines/pdf.engine.js +31 -3
- package/dist/pdf-engines/pdf.engine.js.map +1 -1
- package/dist/pdf-engines/utils/engine.utils.d.ts +12 -3
- package/dist/pdf-engines/utils/engine.utils.js +27 -8
- package/dist/pdf-engines/utils/engine.utils.js.map +1 -1
- package/package.json +8 -7
- package/src/chromium/converters/converter.ts +18 -5
- package/src/chromium/converters/html.converter.ts +82 -61
- package/src/chromium/converters/markdown.converter.ts +86 -69
- package/src/chromium/converters/tests/html.converter.test.ts +119 -135
- package/src/chromium/converters/tests/markdown.converter.test.ts +131 -150
- package/src/chromium/converters/tests/url.converter.test.ts +114 -123
- package/src/chromium/converters/url.converter.ts +84 -60
- package/src/chromium/index.ts +3 -3
- package/src/chromium/interfaces/converter.types.ts +27 -27
- package/src/chromium/utils/converter.utils.ts +165 -139
- package/src/chromium/utils/tests/converter.utils.test.ts +312 -311
- package/src/common/constants.ts +3 -3
- package/src/common/gotenberg.utils.ts +36 -17
- 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 +13 -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 +72 -37
- package/src/libre-office/utils/tests/libre-office.utils.test.ts +100 -68
- package/src/main.config.ts +68 -22
- package/src/main.ts +3 -3
- package/src/pdf-engines/index.ts +1 -1
- package/src/pdf-engines/pdf.engine.ts +77 -49
- package/src/pdf-engines/tests/pdf.engine.test.ts +94 -94
- package/src/pdf-engines/utils/engine.utils.ts +30 -12
- package/src/pdf-engines/utils/tests/engine.utils.test.ts +60 -48
|
@@ -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,37 @@
|
|
|
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
|
|
|
7
|
+
/**
|
|
8
|
+
* Utility class for handling common tasks related to PDF engine operations.
|
|
9
|
+
*/
|
|
6
10
|
export class PDFEngineUtils {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Adds PDF files to the FormData object.
|
|
13
|
+
*
|
|
14
|
+
* @param {PathLikeOrReadStream[]} files - An array of PDF files to be added to the FormData.
|
|
15
|
+
* @param {FormData} data - The FormData object to which PDF files will be added.
|
|
16
|
+
* @throws {Error} Throws an error if the file extension is not supported.
|
|
17
|
+
*/
|
|
18
|
+
public static async addFiles(files: PathLikeOrReadStream[], data: FormData) {
|
|
19
|
+
for (const [key, file] of files.entries()) {
|
|
20
|
+
const filename = `file${key}.pdf`
|
|
21
|
+
if (Buffer.isBuffer(file)) {
|
|
22
|
+
data.append("files", file, filename);
|
|
23
|
+
} else if (file instanceof ReadStream) {
|
|
24
|
+
data.append("files", file, filename);
|
|
25
|
+
} else {
|
|
26
|
+
await promises.access(file, constants.R_OK);
|
|
27
|
+
const filename = path.basename(file.toString());
|
|
28
|
+
const extension = path.extname(filename);
|
|
29
|
+
if (extension === ".pdf") {
|
|
30
|
+
data.append(filename, createReadStream(file));
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error(`${extension} is not supported`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
17
36
|
}
|
|
18
|
-
}
|
|
19
37
|
}
|
|
@@ -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
|
});
|