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.
Files changed (77) hide show
  1. package/README.md +57 -40
  2. package/dist/chromium/converters/converter.d.ts +13 -0
  3. package/dist/chromium/converters/converter.js +10 -0
  4. package/dist/chromium/converters/converter.js.map +1 -1
  5. package/dist/chromium/converters/html.converter.d.ts +32 -6
  6. package/dist/chromium/converters/html.converter.js +30 -9
  7. package/dist/chromium/converters/html.converter.js.map +1 -1
  8. package/dist/chromium/converters/markdown.converter.d.ts +34 -7
  9. package/dist/chromium/converters/markdown.converter.js +32 -16
  10. package/dist/chromium/converters/markdown.converter.js.map +1 -1
  11. package/dist/chromium/converters/url.converter.d.ts +31 -5
  12. package/dist/chromium/converters/url.converter.js +29 -1
  13. package/dist/chromium/converters/url.converter.js.map +1 -1
  14. package/dist/chromium/index.js.map +1 -1
  15. package/dist/chromium/interfaces/converter.types.d.ts +3 -6
  16. package/dist/chromium/utils/converter.utils.d.ts +27 -1
  17. package/dist/chromium/utils/converter.utils.js +48 -21
  18. package/dist/chromium/utils/converter.utils.js.map +1 -1
  19. package/dist/common/constants.js.map +1 -1
  20. package/dist/common/gotenberg.utils.d.ts +18 -0
  21. package/dist/common/gotenberg.utils.js +18 -0
  22. package/dist/common/gotenberg.utils.js.map +1 -1
  23. package/dist/common/index.d.ts +1 -0
  24. package/dist/common/index.js.map +1 -1
  25. package/dist/common/types.d.ts +3 -0
  26. package/dist/common/types.js +3 -0
  27. package/dist/common/types.js.map +1 -0
  28. package/dist/gotenberg.d.ts +8 -0
  29. package/dist/gotenberg.js +8 -0
  30. package/dist/gotenberg.js.map +1 -1
  31. package/dist/libre-office/index.js.map +1 -1
  32. package/dist/libre-office/utils/constants.js +76 -76
  33. package/dist/libre-office/utils/constants.js.map +1 -1
  34. package/dist/libre-office/utils/libre-office.utils.d.ts +19 -4
  35. package/dist/libre-office/utils/libre-office.utils.js +39 -7
  36. package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
  37. package/dist/main.config.d.ts +43 -0
  38. package/dist/main.config.js +43 -0
  39. package/dist/main.config.js.map +1 -1
  40. package/dist/main.js.map +1 -1
  41. package/dist/pdf-engines/index.js.map +1 -1
  42. package/dist/pdf-engines/pdf.engine.d.ts +31 -5
  43. package/dist/pdf-engines/pdf.engine.js +31 -3
  44. package/dist/pdf-engines/pdf.engine.js.map +1 -1
  45. package/dist/pdf-engines/utils/engine.utils.d.ts +12 -3
  46. package/dist/pdf-engines/utils/engine.utils.js +27 -8
  47. package/dist/pdf-engines/utils/engine.utils.js.map +1 -1
  48. package/package.json +8 -7
  49. package/src/chromium/converters/converter.ts +18 -5
  50. package/src/chromium/converters/html.converter.ts +82 -61
  51. package/src/chromium/converters/markdown.converter.ts +86 -69
  52. package/src/chromium/converters/tests/html.converter.test.ts +119 -135
  53. package/src/chromium/converters/tests/markdown.converter.test.ts +131 -150
  54. package/src/chromium/converters/tests/url.converter.test.ts +114 -123
  55. package/src/chromium/converters/url.converter.ts +84 -60
  56. package/src/chromium/index.ts +3 -3
  57. package/src/chromium/interfaces/converter.types.ts +27 -27
  58. package/src/chromium/utils/converter.utils.ts +165 -139
  59. package/src/chromium/utils/tests/converter.utils.test.ts +312 -311
  60. package/src/common/constants.ts +3 -3
  61. package/src/common/gotenberg.utils.ts +36 -17
  62. package/src/common/index.ts +3 -2
  63. package/src/common/tests/gotenberg.utils.test.ts +54 -54
  64. package/src/common/types.ts +3 -0
  65. package/src/gotenberg.ts +13 -4
  66. package/src/libre-office/index.ts +2 -2
  67. package/src/libre-office/interfaces/libre-office.types.ts +2 -2
  68. package/src/libre-office/utils/constants.ts +76 -76
  69. package/src/libre-office/utils/libre-office.utils.ts +72 -37
  70. package/src/libre-office/utils/tests/libre-office.utils.test.ts +100 -68
  71. package/src/main.config.ts +68 -22
  72. package/src/main.ts +3 -3
  73. package/src/pdf-engines/index.ts +1 -1
  74. package/src/pdf-engines/pdf.engine.ts +77 -49
  75. package/src/pdf-engines/tests/pdf.engine.test.ts +94 -94
  76. package/src/pdf-engines/utils/engine.utils.ts +30 -12
  77. 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 { createReadStream, promises } from "fs";
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 { PdfFormat } from "../../common";
9
- import { PDFEngine } from "../pdf.engine";
8
+ import {PdfFormat} from "../../common";
9
+ import {PDFEngine} from "../pdf.engine";
10
10
 
11
- const { Response } = jest.requireActual("node-fetch");
11
+ const {Response} = jest.requireActual("node-fetch");
12
12
  jest.mock("node-fetch", () => jest.fn());
13
13
 
14
14
  describe("PDFEngine", () => {
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();
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("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,
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
- 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);
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
- describe("generate", () => {
72
- const mockFilename = "test.pdf";
73
- const mockBuffer = Buffer.from("mock pdf content");
71
+ describe("generate", () => {
72
+ const mockFilename = "test.pdf";
73
+ const mockBuffer = Buffer.from("mock pdf content");
74
74
 
75
- afterAll(() => {
76
- jest.restoreAllMocks();
77
- });
75
+ afterAll(() => {
76
+ jest.restoreAllMocks();
77
+ });
78
78
 
79
- it("should generate a PDF file", async () => {
80
- const mockGeneratedDir = path.join(process.cwd(), "__generated__");
81
- const mockGeneratedFilePath = path.join(mockGeneratedDir, mockFilename);
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
- const mockPromisesMkDir = jest
84
- .spyOn(fs, "mkdir")
85
- .mockResolvedValueOnce(mockGeneratedDir);
83
+ const mockPromisesMkDir = jest
84
+ .spyOn(fs, "mkdir")
85
+ .mockResolvedValueOnce(mockGeneratedDir);
86
86
 
87
- const mockPromisesWriteFile = jest
88
- .spyOn(fs, "writeFile")
89
- .mockResolvedValueOnce();
87
+ const mockPromisesWriteFile = jest
88
+ .spyOn(fs, "writeFile")
89
+ .mockResolvedValueOnce();
90
90
 
91
- await PDFEngine.generate(mockFilename, mockBuffer);
91
+ await PDFEngine.generate(mockFilename, mockBuffer);
92
92
 
93
- expect(mockPromisesMkDir).toHaveBeenCalledWith(mockGeneratedDir, {
94
- recursive: true,
95
- });
93
+ expect(mockPromisesMkDir).toHaveBeenCalledWith(mockGeneratedDir, {
94
+ recursive: true,
95
+ });
96
96
 
97
- expect(mockPromisesWriteFile).toHaveBeenCalledWith(
98
- mockGeneratedFilePath,
99
- mockBuffer
100
- );
101
- });
97
+ expect(mockPromisesWriteFile).toHaveBeenCalledWith(
98
+ mockGeneratedFilePath,
99
+ mockBuffer
100
+ );
101
+ });
102
102
 
103
- it("should handle errors during file generation", async () => {
104
- jest
105
- .spyOn(fs, "mkdir")
106
- .mockRejectedValueOnce(new Error("Cannot create directory"));
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
- await expect(
109
- PDFEngine.generate(mockFilename, mockBuffer)
110
- ).rejects.toThrow("Cannot create directory");
111
- });
108
+ await expect(
109
+ PDFEngine.generate(mockFilename, mockBuffer)
110
+ ).rejects.toThrow("Cannot create directory");
111
+ });
112
112
 
113
- it("should handle errors during file writing", async () => {
114
- jest
115
- .spyOn(fs, "writeFile")
116
- .mockRejectedValueOnce(new Error("Failed to write to file"));
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
- await expect(
119
- PDFEngine.generate(mockFilename, mockBuffer)
120
- ).rejects.toThrow("Failed to write to file");
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 { constants, createReadStream, PathLike, promises } from "fs";
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
- public static async injectFiles(files: PathLike[], data: FormData) {
8
- for (const file of files) {
9
- await promises.access(file, constants.R_OK);
10
- const filename = path.basename(file.toString());
11
- const extension = path.extname(filename);
12
- if (extension === ".pdf") {
13
- data.append(filename, createReadStream(file));
14
- } else {
15
- throw new Error(`${extension} is not supported`);
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 { createReadStream, promises } from "fs";
2
+ import {createReadStream, promises} from "fs";
3
3
  import FormData from "form-data";
4
4
 
5
- import { PDFEngineUtils } from "./../engine.utils";
5
+ import {PDFEngineUtils} from "../engine.utils";
6
6
 
7
7
  describe("PDFEngineUtils", () => {
8
- const mockPromisesAccess = jest.spyOn(promises, "access");
9
- const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
10
- const data = new FormData();
8
+ const mockPromisesAccess = jest.spyOn(promises, "access");
9
+ const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
10
+ const data = new FormData();
11
11
 
12
- beforeEach(() => {
13
- (createReadStream as jest.Mock) = jest
14
- .fn()
15
- .mockImplementation((file) => file);
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
- describe("when one of the files is not PDF", () => {
35
- it("should throw an error", async () => {
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("when one of the files does not exist", () => {
47
- it("should throw an error", async () => {
48
- const errorMessage =
49
- "ENOENT: no such file or directory, access 'path/to/index.html'";
50
- mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
51
- await expect(() =>
52
- PDFEngineUtils.injectFiles(
53
- ["path/to/file.pdf", "path/to/another-file.pdf"],
54
- data
55
- )
56
- ).rejects.toThrow(errorMessage);
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
  });