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,5 +1,5 @@
1
1
  export enum PdfFormat {
2
- A_1a = "PDF/A-1a",
3
- A_2b = "PDF/A-2b",
4
- A_3b = "PDF/A-3b",
2
+ A_1a = "PDF/A-1a",
3
+ A_2b = "PDF/A-2b",
4
+ A_3b = "PDF/A-3b",
5
5
  }
@@ -1,25 +1,44 @@
1
1
  import FormData from "form-data";
2
2
  import fetch from "node-fetch";
3
3
 
4
+ /**
5
+ * Utility class for common tasks related to the Gotenberg service.
6
+ */
4
7
  export class GotenbergUtils {
5
- public static assert(condition: boolean, message: string): asserts condition {
6
- if (!condition) {
7
- throw new Error(message);
8
+ /**
9
+ * Asserts that a condition is true; otherwise, throws an error with the specified message.
10
+ *
11
+ * @param {boolean} condition - The condition to assert.
12
+ * @param {string} message - The error message to throw if the condition is false.
13
+ * @throws {Error} Throws an error with the specified message if the condition is false.
14
+ */
15
+ public static assert(condition: boolean, message: string): asserts condition {
16
+ if (!condition) {
17
+ throw new Error(message);
18
+ }
8
19
  }
9
- }
10
20
 
11
- public static async fetch(endpoint: string, data: FormData): Promise<Buffer> {
12
- const response = await fetch(endpoint, {
13
- method: "post",
14
- body: data,
15
- headers: {
16
- ...data.getHeaders(),
17
- },
18
- });
21
+ /**
22
+ * Performs a POST request to the specified Gotenberg endpoint with the provided FormData.
23
+ *
24
+ * @param {string} endpoint - The Gotenberg endpoint URL.
25
+ * @param {FormData} data - The FormData object to be sent in the POST request.
26
+ * @returns {Promise<Buffer>} A Promise that resolves to the response body as a Buffer.
27
+ * @throws {Error} Throws an error if the HTTP response status is not OK.
28
+ */
29
+ public static async fetch(endpoint: string, data: FormData): Promise<Buffer> {
30
+ const response = await fetch(endpoint, {
31
+ method: "post",
32
+ body: data,
33
+ headers: {
34
+ ...data.getHeaders(),
35
+ },
36
+ });
19
37
 
20
- if (!response.ok) {
21
- throw new Error(`${response.status} ${response.statusText}`);
38
+ if (!response.ok) {
39
+ throw new Error(`${response.status} ${response.statusText}`);
40
+ }
41
+
42
+ return response.buffer();
22
43
  }
23
- return response.buffer();
24
- }
25
- }
44
+ }
@@ -1,2 +1,3 @@
1
- export { PdfFormat } from "./constants";
2
- export { GotenbergUtils } from "./gotenberg.utils";
1
+ export {PdfFormat} from "./constants";
2
+ export {GotenbergUtils} from "./gotenberg.utils";
3
+ export {PathLikeOrReadStream} from "./types"
@@ -1,71 +1,71 @@
1
1
  import fetch from "node-fetch";
2
2
  import FormData from "form-data";
3
3
 
4
- import { GotenbergUtils } from "./../gotenberg.utils";
4
+ import {GotenbergUtils} from "./../gotenberg.utils";
5
5
 
6
- const { Response, FetchError } = jest.requireActual("node-fetch");
6
+ const {Response, FetchError} = jest.requireActual("node-fetch");
7
7
 
8
8
  jest.mock("node-fetch", () => jest.fn());
9
9
 
10
10
  describe("GotenbergUtils", () => {
11
- afterEach(() => {
12
- jest.resetAllMocks();
13
- });
14
-
15
- describe("assert", () => {
16
- const errorMessage = "error message";
17
- describe("when condition is true", () => {
18
- it("should pass", () => {
19
- expect(() => GotenbergUtils.assert(true, errorMessage)).not.toThrow();
20
- });
21
- });
22
- describe("when condition is false", () => {
23
- it("should return error message", () => {
24
- expect(() => GotenbergUtils.assert(false, errorMessage)).toThrow(
25
- errorMessage
26
- );
27
- });
11
+ afterEach(() => {
12
+ jest.resetAllMocks();
28
13
  });
29
- });
30
14
 
31
- describe("fetch", () => {
32
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
33
- const data = new FormData();
34
- const endpoint = "http://localhost:3000/forms/chromium/convert/html";
35
-
36
- describe("when fetch request succeeds", () => {
37
- it("should return a buffer", async () => {
38
- mockFetch.mockResolvedValueOnce(new Response("content"));
39
- const response = await GotenbergUtils.fetch(endpoint, data);
40
- await expect(response).toEqual(Buffer.from("content"));
41
- });
15
+ describe("assert", () => {
16
+ const errorMessage = "error message";
17
+ describe("when condition is true", () => {
18
+ it("should pass", () => {
19
+ expect(() => GotenbergUtils.assert(true, errorMessage)).not.toThrow();
20
+ });
21
+ });
22
+ describe("when condition is false", () => {
23
+ it("should return error message", () => {
24
+ expect(() => GotenbergUtils.assert(false, errorMessage)).toThrow(
25
+ errorMessage
26
+ );
27
+ });
28
+ });
42
29
  });
43
30
 
44
- describe("when fetch request fails", () => {
45
- describe("when there is a known error", () => {
46
- it("should throw an error", async () => {
47
- const errorMessage =
48
- "FetchError: request to http://localhost:3000/forms/chromium/convert/html failed";
49
- mockFetch.mockRejectedValueOnce(new FetchError(errorMessage));
50
- await expect(() =>
51
- GotenbergUtils.fetch(endpoint, data)
52
- ).rejects.toThrow(errorMessage);
31
+ describe("fetch", () => {
32
+ const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
33
+ const data = new FormData();
34
+ const endpoint = "http://localhost:3000/forms/chromium/convert/html";
35
+
36
+ describe("when fetch request succeeds", () => {
37
+ it("should return a buffer", async () => {
38
+ mockFetch.mockResolvedValueOnce(new Response("content"));
39
+ const response = await GotenbergUtils.fetch(endpoint, data);
40
+ await expect(response).toEqual(Buffer.from("content"));
41
+ });
53
42
  });
54
- });
55
43
 
56
- describe("when there is an unknown error", () => {
57
- it("should throw an error", async () => {
58
- mockFetch.mockResolvedValueOnce(
59
- new Response(
60
- {},
61
- { status: 500, statusText: "Internal server error" }
62
- )
63
- );
64
- await expect(() =>
65
- GotenbergUtils.fetch(endpoint, data)
66
- ).rejects.toThrow("500 Internal server error");
44
+ describe("when fetch request fails", () => {
45
+ describe("when there is a known error", () => {
46
+ it("should throw an error", async () => {
47
+ const errorMessage =
48
+ "FetchError: request to http://localhost:3000/forms/chromium/convert/html failed";
49
+ mockFetch.mockRejectedValueOnce(new FetchError(errorMessage));
50
+ await expect(() =>
51
+ GotenbergUtils.fetch(endpoint, data)
52
+ ).rejects.toThrow(errorMessage);
53
+ });
54
+ });
55
+
56
+ describe("when there is an unknown error", () => {
57
+ it("should throw an error", async () => {
58
+ mockFetch.mockResolvedValueOnce(
59
+ new Response(
60
+ {},
61
+ {status: 500, statusText: "Internal server error"}
62
+ )
63
+ );
64
+ await expect(() =>
65
+ GotenbergUtils.fetch(endpoint, data)
66
+ ).rejects.toThrow("500 Internal server error");
67
+ });
68
+ });
67
69
  });
68
- });
69
70
  });
70
- });
71
71
  });
@@ -0,0 +1,3 @@
1
+ import {PathLike, ReadStream} from "fs";
2
+
3
+ export type PathLikeOrReadStream = PathLike | ReadStream
package/src/gotenberg.ts CHANGED
@@ -8,14 +8,23 @@ import config from "config";
8
8
  const envFile = `.env.${process.env.NODE_ENV}`;
9
9
  const envFileFallback = ".env";
10
10
 
11
- const dotenvConfig = dotenv.config({ path: path.resolve(envFile) });
11
+ const dotenvConfig = dotenv.config({path: path.resolve(envFile)});
12
12
 
13
13
  // Fallback to loading the default environment file.
14
14
  if (dotenvConfig.error) {
15
- dotenv.config({ path: path.resolve(envFileFallback) });
15
+ dotenv.config({path: path.resolve(envFileFallback)});
16
16
  }
17
17
 
18
+ /**
19
+ * Class representing configuration for interacting with Gotenberg service.
20
+ */
18
21
  export class Gotenberg {
19
- public static endpoint: string =
20
- process.env.GOTENBERG_ENDPOINT || config.get<string>("gotenberg.endpoint");
22
+ /**
23
+ * The Gotenberg service endpoint.
24
+ * Defaults to the value from the environment variable `GOTENBERG_ENDPOINT`, or falls back to the configuration file.
25
+ * @type {string}
26
+ */
27
+ public static endpoint: string =
28
+ process.env.GOTENBERG_ENDPOINT || config.get<string>("gotenberg.endpoint");
21
29
  }
30
+
@@ -1,2 +1,2 @@
1
- export { PageProperties } from "./interfaces/libre-office.types";
2
- export { LibreOfficeUtils } from "./utils/libre-office.utils";
1
+ export {PageProperties} from "./interfaces/libre-office.types";
2
+ export {LibreOfficeUtils} from "./utils/libre-office.utils";
@@ -1,4 +1,4 @@
1
1
  export type PageProperties = {
2
- landscape?: boolean; // Set the paper orientation to landscape (default false)
3
- nativePageRanges?: { from: number; to: number }; // Page ranges to print
2
+ landscape?: boolean; // Set the paper orientation to landscape (default false)
3
+ nativePageRanges?: { from: number; to: number }; // Page ranges to print
4
4
  };
@@ -1,78 +1,78 @@
1
1
  export const LIBRE_OFFICE_EXTENSIONS = [
2
- ".bib",
3
- ".doc",
4
- ".xml",
5
- ".docx",
6
- ".fodt",
7
- ".html",
8
- ".ltx",
9
- ".txt",
10
- ".odt",
11
- ".ott",
12
- ".pdb",
13
- ".pdf",
14
- ".psw",
15
- ".rtf",
16
- ".sdw",
17
- ".stw",
18
- ".sxw",
19
- ".uot",
20
- ".vor",
21
- ".wps",
22
- ".epub",
23
- ".png",
24
- ".bmp",
25
- ".emf",
26
- ".eps",
27
- ".fodg",
28
- ".gif",
29
- ".jpg",
30
- ".met",
31
- ".odd",
32
- ".otg",
33
- ".pbm",
34
- ".pct",
35
- ".pgm",
36
- ".ppm",
37
- ".ras",
38
- ".std",
39
- ".svg",
40
- ".svm",
41
- ".swf",
42
- ".sxd",
43
- ".sxw",
44
- ".tiff",
45
- ".xhtml",
46
- ".xpm",
47
- ".fodp",
48
- ".potm",
49
- ".pot",
50
- ".pptx",
51
- ".pps",
52
- ".ppt",
53
- ".pwp",
54
- ".sda",
55
- ".sdd",
56
- ".sti",
57
- ".sxi",
58
- ".uop",
59
- ".wmf",
60
- ".csv",
61
- ".dbf",
62
- ".dif",
63
- ".fods",
64
- ".ods",
65
- ".ots",
66
- ".pxl",
67
- ".sdc",
68
- ".slk",
69
- ".stc",
70
- ".sxc",
71
- ".uos",
72
- ".xls",
73
- ".xlt",
74
- ".xlsx",
75
- ".tif",
76
- ".jpeg",
77
- ".odp",
2
+ "bib",
3
+ "doc",
4
+ "xml",
5
+ "docx",
6
+ "fodt",
7
+ "html",
8
+ "ltx",
9
+ "txt",
10
+ "odt",
11
+ "ott",
12
+ "pdb",
13
+ "pdf",
14
+ "psw",
15
+ "rtf",
16
+ "sdw",
17
+ "stw",
18
+ "sxw",
19
+ "uot",
20
+ "vor",
21
+ "wps",
22
+ "epub",
23
+ "png",
24
+ "bmp",
25
+ "emf",
26
+ "eps",
27
+ "fodg",
28
+ "gif",
29
+ "jpg",
30
+ "met",
31
+ "odd",
32
+ "otg",
33
+ "pbm",
34
+ "pct",
35
+ "pgm",
36
+ "ppm",
37
+ "ras",
38
+ "std",
39
+ "svg",
40
+ "svm",
41
+ "swf",
42
+ "sxd",
43
+ "sxw",
44
+ "tiff",
45
+ "xhtml",
46
+ "xpm",
47
+ "fodp",
48
+ "potm",
49
+ "pot",
50
+ "pptx",
51
+ "pps",
52
+ "ppt",
53
+ "pwp",
54
+ "sda",
55
+ "sdd",
56
+ "sti",
57
+ "sxi",
58
+ "uop",
59
+ "wmf",
60
+ "csv",
61
+ "dbf",
62
+ "dif",
63
+ "fods",
64
+ "ods",
65
+ "ots",
66
+ "pxl",
67
+ "sdc",
68
+ "slk",
69
+ "stc",
70
+ "sxc",
71
+ "uos",
72
+ "xls",
73
+ "xlt",
74
+ "xlsx",
75
+ "tif",
76
+ "jpeg",
77
+ "odp",
78
78
  ];
@@ -1,47 +1,82 @@
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
+ import {fromStream, fromBuffer} from "file-type";
5
+
4
6
  import FormData from "form-data";
5
7
 
6
- import { GotenbergUtils } from "../../common";
7
- import { LIBRE_OFFICE_EXTENSIONS } from "./constants";
8
- import { PageProperties } from "../interfaces/libre-office.types";
8
+ import {GotenbergUtils, PathLikeOrReadStream} from "../../common";
9
+ import {LIBRE_OFFICE_EXTENSIONS} from "./constants";
10
+ import {PageProperties} from "../interfaces/libre-office.types";
9
11
 
12
+ /**
13
+ * Utility class for handling common tasks related to LibreOffice conversions.
14
+ */
10
15
  export class LibreOfficeUtils {
11
- public static async injectFiles(files: PathLike[], data: FormData) {
12
- for (const file of files) {
13
- await promises.access(file, constants.R_OK);
14
- const filename = path.basename(file.toString());
15
- const extension = path.extname(filename);
16
- if (LIBRE_OFFICE_EXTENSIONS.includes(extension)) {
17
- data.append(filename, createReadStream(file));
18
- } else {
19
- throw new Error(`${extension} is not supported`);
20
- }
21
- }
22
- }
23
-
24
- public static injectPageProperties(
25
- data: FormData,
26
- pageProperties: PageProperties
27
- ): void {
28
- if (pageProperties.landscape) {
29
- data.append("landscape", String(pageProperties.landscape));
16
+ /**
17
+ * Adds files to the FormData object for LibreOffice conversion.
18
+ *
19
+ * @param {PathLikeOrReadStream[]} files - An array of files to be added to the FormData.
20
+ * @param {FormData} data - The FormData object to which files will be added.
21
+ * @throws {Error} Throws an error if the file extension is not supported.
22
+ */
23
+ public static async addFiles(files: PathLikeOrReadStream[], data: FormData) {
24
+ for (const [key, value] of files.entries()) {
25
+ let file = value;
26
+ let fileInfo;
27
+
28
+ if (Buffer.isBuffer(value)) {
29
+ fileInfo = await fromBuffer(value);
30
+ } else if (value instanceof ReadStream) {
31
+ fileInfo = await fromStream(value);
32
+ } else {
33
+ await promises.access(value, constants.R_OK);
34
+ const filename = path.basename(value.toString());
35
+ fileInfo = {ext: path.extname(filename).slice(1)};
36
+ file = createReadStream(value)
37
+ }
38
+
39
+ if (!fileInfo) {
40
+ throw new Error("File type could not be determined");
41
+ }
42
+
43
+ const extension = fileInfo.ext;
44
+
45
+ if (LIBRE_OFFICE_EXTENSIONS.includes(extension)) {
46
+ data.append("files", file, `${key}.${extension}`);
47
+ } else {
48
+ throw new Error(`${extension} is not supported`);
49
+ }
50
+ }
30
51
  }
31
52
 
32
- if (pageProperties.nativePageRanges) {
33
- GotenbergUtils.assert(
34
- pageProperties.nativePageRanges.from > 0 &&
35
- pageProperties.nativePageRanges.to > 0 &&
36
- pageProperties.nativePageRanges.to >=
37
- pageProperties.nativePageRanges.from,
38
- "page ranges syntax error"
39
- );
40
-
41
- data.append(
42
- "nativePageRanges",
43
- `${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`
44
- );
53
+ /**
54
+ * Adds page properties to the FormData object based on the provided PageProperties.
55
+ *
56
+ * @param {FormData} data - The FormData object to which page properties will be added.
57
+ * @param {PageProperties} pageProperties - The page properties to be added to the FormData.
58
+ */
59
+ public static addPageProperties(
60
+ data: FormData,
61
+ pageProperties: PageProperties
62
+ ): void {
63
+ if (pageProperties.landscape) {
64
+ data.append("landscape", String(pageProperties.landscape));
65
+ }
66
+
67
+ if (pageProperties.nativePageRanges) {
68
+ GotenbergUtils.assert(
69
+ pageProperties.nativePageRanges.from > 0 &&
70
+ pageProperties.nativePageRanges.to > 0 &&
71
+ pageProperties.nativePageRanges.to >=
72
+ pageProperties.nativePageRanges.from,
73
+ "page ranges syntax error"
74
+ );
75
+
76
+ data.append(
77
+ "nativePageRanges",
78
+ `${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`
79
+ );
80
+ }
45
81
  }
46
- }
47
82
  }