chromiumly 1.0.4 → 2.0.2

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 (110) hide show
  1. package/README.md +62 -18
  2. package/dist/chromium/converters/converter.d.ts +5 -0
  3. package/dist/chromium/converters/converter.js +11 -0
  4. package/dist/chromium/converters/converter.js.map +1 -0
  5. package/dist/chromium/converters/html.converter.d.ts +14 -0
  6. package/dist/chromium/converters/html.converter.js +36 -0
  7. package/dist/chromium/converters/html.converter.js.map +1 -0
  8. package/dist/chromium/converters/markdown.converter.d.ts +15 -0
  9. package/dist/chromium/converters/markdown.converter.js +38 -0
  10. package/dist/chromium/converters/markdown.converter.js.map +1 -0
  11. package/dist/chromium/converters/url.converter.d.ts +13 -0
  12. package/dist/{converters → chromium/converters}/url.converter.js +10 -6
  13. package/dist/chromium/converters/url.converter.js.map +1 -0
  14. package/dist/chromium/index.d.ts +3 -0
  15. package/dist/chromium/index.js +10 -0
  16. package/dist/chromium/index.js.map +1 -0
  17. package/dist/{common → chromium/interfaces}/converter.interface.d.ts +2 -1
  18. package/dist/{common → chromium/interfaces}/converter.interface.js +0 -0
  19. package/dist/chromium/interfaces/converter.interface.js.map +1 -0
  20. package/dist/{common → chromium/interfaces}/converter.types.d.ts +0 -0
  21. package/dist/{common → chromium/interfaces}/converter.types.js +0 -0
  22. package/dist/chromium/interfaces/converter.types.js.map +1 -0
  23. package/dist/chromium/utils/converter.utils.d.ts +5 -0
  24. package/dist/chromium/utils/converter.utils.js +45 -0
  25. package/dist/chromium/utils/converter.utils.js.map +1 -0
  26. package/dist/common/constants.d.ts +5 -0
  27. package/dist/common/constants.js +10 -0
  28. package/dist/common/constants.js.map +1 -0
  29. package/dist/common/gotenberg.utils.d.ts +6 -0
  30. package/dist/common/gotenberg.utils.js +27 -0
  31. package/dist/common/gotenberg.utils.js.map +1 -0
  32. package/dist/common/index.d.ts +2 -0
  33. package/dist/common/index.js +8 -0
  34. package/dist/common/index.js.map +1 -0
  35. package/dist/libre-office/index.d.ts +2 -0
  36. package/dist/libre-office/index.js +6 -0
  37. package/dist/libre-office/index.js.map +1 -0
  38. package/dist/libre-office/interfaces/libre-office.types.d.ts +7 -0
  39. package/dist/libre-office/interfaces/libre-office.types.js +3 -0
  40. package/dist/libre-office/interfaces/libre-office.types.js.map +1 -0
  41. package/dist/libre-office/utils/constants.d.ts +1 -0
  42. package/dist/libre-office/utils/constants.js +82 -0
  43. package/dist/libre-office/utils/constants.js.map +1 -0
  44. package/dist/libre-office/utils/libre-office.utils.d.ts +8 -0
  45. package/dist/libre-office/utils/libre-office.utils.js +44 -0
  46. package/dist/libre-office/utils/libre-office.utils.js.map +1 -0
  47. package/dist/main.config.d.ts +22 -10
  48. package/dist/main.config.js +28 -15
  49. package/dist/main.config.js.map +1 -1
  50. package/dist/main.d.ts +3 -3
  51. package/dist/main.js +9 -7
  52. package/dist/main.js.map +1 -1
  53. package/dist/pdf-engines/index.d.ts +1 -0
  54. package/dist/pdf-engines/index.js +6 -0
  55. package/dist/pdf-engines/index.js.map +1 -0
  56. package/dist/pdf-engines/pdf.engine.d.ts +16 -0
  57. package/dist/pdf-engines/pdf.engine.js +47 -0
  58. package/dist/pdf-engines/pdf.engine.js.map +1 -0
  59. package/dist/pdf-engines/utils/engine.utils.d.ts +6 -0
  60. package/dist/pdf-engines/utils/engine.utils.js +30 -0
  61. package/dist/pdf-engines/utils/engine.utils.js.map +1 -0
  62. package/package.json +10 -3
  63. package/src/chromium/converters/converter.ts +9 -0
  64. package/src/chromium/converters/html.converter.ts +41 -0
  65. package/src/chromium/converters/markdown.converter.ts +45 -0
  66. package/src/chromium/converters/tests/html.converter.test.ts +95 -0
  67. package/src/chromium/converters/tests/markdown.converter.test.ts +105 -0
  68. package/src/chromium/converters/tests/url.converter.test.ts +82 -0
  69. package/src/{converters → chromium/converters}/url.converter.ts +13 -7
  70. package/src/chromium/index.ts +3 -0
  71. package/src/{common → chromium/interfaces}/converter.interface.ts +2 -1
  72. package/src/{common → chromium/interfaces}/converter.types.ts +0 -0
  73. package/src/chromium/utils/converter.utils.ts +75 -0
  74. package/src/chromium/utils/tests/converter.utils.test.ts +101 -0
  75. package/src/common/constants.ts +5 -0
  76. package/src/common/gotenberg.utils.ts +25 -0
  77. package/src/common/index.ts +2 -0
  78. package/src/common/tests/gotenberg.utils.test.ts +71 -0
  79. package/src/libre-office/index.ts +2 -0
  80. package/src/libre-office/interfaces/libre-office.types.ts +4 -0
  81. package/src/libre-office/utils/constants.ts +78 -0
  82. package/src/libre-office/utils/libre-office.utils.ts +51 -0
  83. package/src/libre-office/utils/test/libre-office.utils.test.ts +85 -0
  84. package/src/main.config.ts +23 -10
  85. package/src/main.ts +3 -3
  86. package/src/pdf-engines/index.ts +1 -0
  87. package/src/pdf-engines/pdf.engine.ts +53 -0
  88. package/src/pdf-engines/tests/pdf.engine.test.ts +84 -0
  89. package/src/pdf-engines/utils/engine.utils.ts +23 -0
  90. package/src/pdf-engines/utils/tests/engine.utils.test.ts +59 -0
  91. package/dist/common/converter.d.ts +0 -7
  92. package/dist/common/converter.interface.js.map +0 -1
  93. package/dist/common/converter.js +0 -21
  94. package/dist/common/converter.js.map +0 -1
  95. package/dist/common/converter.types.js.map +0 -1
  96. package/dist/common/converter.utils.d.ts +0 -8
  97. package/dist/common/converter.utils.js +0 -66
  98. package/dist/common/converter.utils.js.map +0 -1
  99. package/dist/converters/html.converter.d.ts +0 -12
  100. package/dist/converters/html.converter.js +0 -26
  101. package/dist/converters/html.converter.js.map +0 -1
  102. package/dist/converters/markdown.converter.d.ts +0 -13
  103. package/dist/converters/markdown.converter.js +0 -27
  104. package/dist/converters/markdown.converter.js.map +0 -1
  105. package/dist/converters/url.converter.d.ts +0 -11
  106. package/dist/converters/url.converter.js.map +0 -1
  107. package/src/common/converter.ts +0 -18
  108. package/src/common/converter.utils.ts +0 -101
  109. package/src/converters/html.converter.ts +0 -30
  110. package/src/converters/markdown.converter.ts +0 -33
@@ -0,0 +1,105 @@
1
+ import { createReadStream, promises } from "fs";
2
+
3
+ import fetch from "node-fetch";
4
+ import FormData from "form-data";
5
+
6
+ import { PdfFormat } from "../../../common";
7
+ import { MarkdownConverter } from "../markdown.converter";
8
+
9
+ const { Response } = jest.requireActual("node-fetch");
10
+ jest.mock("node-fetch", () => jest.fn());
11
+
12
+ describe("MarkdownConverter", () => {
13
+ const mockPromisesAccess = jest.spyOn(promises, "access");
14
+ const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
15
+ const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
16
+
17
+ const converter = new MarkdownConverter();
18
+ afterEach(() => {
19
+ jest.resetAllMocks();
20
+ });
21
+
22
+ describe("endpoint", () => {
23
+ it("should route to Chromium Markdown route", () => {
24
+ expect(converter.endpoint).toEqual(
25
+ "http://localhost:3000/forms/chromium/convert/markdown"
26
+ );
27
+ });
28
+ });
29
+
30
+ describe("convert", () => {
31
+ beforeEach(() => {
32
+ (createReadStream as jest.Mock) = jest.fn().mockImplementation(() => {});
33
+ });
34
+
35
+ describe("when file exists", () => {
36
+ it("should return a buffer", async () => {
37
+ mockPromisesAccess.mockResolvedValue();
38
+ mockFetch.mockResolvedValue(new Response("content"));
39
+ const buffer = await converter.convert({
40
+ html: "path/to/index.html",
41
+ markdown: "path/to/file.md",
42
+ });
43
+ expect(buffer).toEqual(Buffer.from("content"));
44
+ });
45
+ });
46
+
47
+ describe("when pdf format parameter is passed", () => {
48
+ it("should return a buffer", async () => {
49
+ mockPromisesAccess.mockResolvedValue();
50
+ mockFetch.mockResolvedValue(new Response("content"));
51
+ const buffer = await converter.convert({
52
+ html: "path/to/index.html",
53
+ markdown: "path/to/file.md",
54
+ pdfFormat: PdfFormat.A_2b,
55
+ });
56
+ expect(mockFormDataAppend).toBeCalledTimes(3);
57
+ expect(buffer).toEqual(Buffer.from("content"));
58
+ });
59
+ });
60
+
61
+ describe("when page properties parameter is passed", () => {
62
+ it("should return a buffer", async () => {
63
+ mockPromisesAccess.mockResolvedValue();
64
+ mockFetch.mockResolvedValue(new Response("content"));
65
+ const buffer = await converter.convert({
66
+ html: "path/to/index.html",
67
+ markdown: "path/to/file.md",
68
+ properties: { size: { width: 8.3, height: 11.7 } },
69
+ });
70
+ expect(mockFormDataAppend).toBeCalledTimes(4);
71
+ expect(buffer).toEqual(Buffer.from("content"));
72
+ });
73
+ });
74
+
75
+ describe("when file does not exist", () => {
76
+ it("should throw an error", async () => {
77
+ const errorMessage =
78
+ "ENOENT: no such file or directory, access 'path/to/index.html'";
79
+ mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
80
+
81
+ await expect(() =>
82
+ converter.convert({
83
+ html: "path/to/index.html",
84
+ markdown: "path/to/file.md",
85
+ })
86
+ ).rejects.toThrow(errorMessage);
87
+ });
88
+ });
89
+
90
+ describe("when fetch request fails", () => {
91
+ it("should throw an error", async () => {
92
+ const errorMessage =
93
+ "FetchError: request to http://localhost:3000/forms/chromium/convert/html failed";
94
+ mockPromisesAccess.mockResolvedValue();
95
+ mockFetch.mockRejectedValue(new Error(errorMessage));
96
+ await expect(() =>
97
+ converter.convert({
98
+ html: "path/to/index.html",
99
+ markdown: "path/to/file.md",
100
+ })
101
+ ).rejects.toThrow(errorMessage);
102
+ });
103
+ });
104
+ });
105
+ });
@@ -0,0 +1,82 @@
1
+ import fetch from "node-fetch";
2
+ import FormData from "form-data";
3
+
4
+ import { PdfFormat } from "../../../common";
5
+ import { UrlConverter } from "../url.converter";
6
+
7
+ const { Response } = jest.requireActual("node-fetch");
8
+ jest.mock("node-fetch", () => jest.fn());
9
+
10
+ describe("HtmlConverter", () => {
11
+ const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
12
+ const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
13
+
14
+ const converter = new UrlConverter();
15
+
16
+ afterEach(() => {
17
+ jest.resetAllMocks();
18
+ });
19
+
20
+ describe("endpoint", () => {
21
+ it("should route to Chromium HTML route", () => {
22
+ expect(converter.endpoint).toEqual(
23
+ "http://localhost:3000/forms/chromium/convert/url"
24
+ );
25
+ });
26
+ });
27
+
28
+ describe("convert", () => {
29
+ describe("when URL is valid", () => {
30
+ it("should return a buffer", async () => {
31
+ mockFetch.mockResolvedValueOnce(new Response("content"));
32
+ const buffer = await converter.convert({
33
+ url: "http://www.example.com/",
34
+ });
35
+ expect(buffer).toEqual(Buffer.from("content"));
36
+ });
37
+ });
38
+
39
+ describe("when pdf format parameter is passed", () => {
40
+ it("should return a buffer", async () => {
41
+ mockFetch.mockResolvedValueOnce(new Response("content"));
42
+ const buffer = await converter.convert({
43
+ url: "http://www.example.com/",
44
+ pdfFormat: PdfFormat.A_3b,
45
+ });
46
+ expect(mockFormDataAppend).toBeCalledTimes(2);
47
+ expect(buffer).toEqual(Buffer.from("content"));
48
+ });
49
+ });
50
+
51
+ describe("when page properties parameter is passed", () => {
52
+ it("should return a buffer", async () => {
53
+ mockFetch.mockResolvedValueOnce(new Response("content"));
54
+ const buffer = await converter.convert({
55
+ url: "http://www.example.com/",
56
+ properties: { size: { width: 8.3, height: 11.7 } },
57
+ });
58
+ expect(mockFormDataAppend).toBeCalledTimes(3);
59
+ expect(buffer).toEqual(Buffer.from("content"));
60
+ });
61
+ });
62
+
63
+ describe("when URL is invalid", () => {
64
+ it("should throw an error", async () => {
65
+ await expect(() =>
66
+ converter.convert({ url: "invalid url" })
67
+ ).rejects.toThrow("Invalid URL: invalid url");
68
+ });
69
+ });
70
+
71
+ describe("when fetch request fails", () => {
72
+ it("should throw an error", async () => {
73
+ const errorMessage =
74
+ "FetchError: request to http://localhost:3000/forms/chromium/convert/html failed";
75
+ mockFetch.mockRejectedValueOnce(new Error(errorMessage));
76
+ await expect(() =>
77
+ converter.convert({ url: "http://www.example.com/" })
78
+ ).rejects.toThrow(errorMessage);
79
+ });
80
+ });
81
+ });
82
+ });
@@ -2,32 +2,38 @@ import { URL } from "url";
2
2
 
3
3
  import FormData from "form-data";
4
4
 
5
- import { IConverter } from "../common/converter.interface";
6
- import { PageProperties } from "../common/converter.types";
7
- import { ConverterUtils } from "../common/converter.utils";
8
- import { Converter } from "../common/converter";
9
- import { Route } from "../main.config";
5
+ import { GotenbergUtils, PdfFormat } from "../../common";
6
+ import { IConverter } from "../interfaces/converter.interface";
7
+ import { PageProperties } from "../interfaces/converter.types";
8
+ import { ConverterUtils } from "../utils/converter.utils";
9
+ import { Converter } from "./converter";
10
+ import { ChromiumRoute } from "../../main.config";
10
11
 
11
12
  export class UrlConverter extends Converter implements IConverter {
12
13
  constructor() {
13
- super(Route.URL);
14
+ super(ChromiumRoute.URL);
14
15
  }
15
16
 
16
17
  async convert({
17
18
  url,
18
19
  properties,
20
+ pdfFormat,
19
21
  }: {
20
22
  url: string;
21
23
  properties?: PageProperties;
24
+ pdfFormat?: PdfFormat;
22
25
  }): Promise<Buffer> {
23
26
  try {
24
27
  const _url = new URL(url);
25
28
  const data = new FormData();
29
+ if (pdfFormat) {
30
+ data.append("pdfFormat", pdfFormat);
31
+ }
26
32
  data.append("url", _url.href);
27
33
  if (properties) {
28
34
  ConverterUtils.injectPageProperties(data, properties);
29
35
  }
30
- return ConverterUtils.fetch(this.endpoint, data);
36
+ return GotenbergUtils.fetch(this.endpoint, data);
31
37
  } catch (error) {
32
38
  throw error;
33
39
  }
@@ -0,0 +1,3 @@
1
+ export { HtmlConverter } from "./converters/html.converter";
2
+ export { MarkdownConverter } from "./converters/markdown.converter";
3
+ export { UrlConverter } from "./converters/url.converter";
@@ -1,11 +1,12 @@
1
1
  import { PathLike } from "fs";
2
2
 
3
+ import { PdfFormat } from "../../common";
3
4
  import { PageProperties } from "./converter.types";
4
5
 
5
6
  export interface IConverter {
6
7
  convert({
7
8
  ...args
8
9
  }: {
9
- [x: string]: string | PathLike | PageProperties;
10
+ [x: string]: string | PathLike | PageProperties | PdfFormat;
10
11
  }): Promise<Buffer>;
11
12
  }
@@ -0,0 +1,75 @@
1
+ import FormData from "form-data";
2
+
3
+ import { GotenbergUtils } from "../../common/gotenberg.utils";
4
+ import { PageProperties } from "../interfaces/converter.types";
5
+
6
+ export class ConverterUtils {
7
+ public static injectPageProperties(
8
+ data: FormData,
9
+ pageProperties: PageProperties
10
+ ): void {
11
+ if (pageProperties.size) {
12
+ GotenbergUtils.assert(
13
+ pageProperties.size.width >= 1.0 && pageProperties.size.height >= 1.5,
14
+ "size is smaller than the minimum printing requirements (i.e. 1.0 x 1.5 in)"
15
+ );
16
+
17
+ data.append("paperWidth", pageProperties.size.width);
18
+ data.append("paperHeight", pageProperties.size.height);
19
+ }
20
+
21
+ if (pageProperties.margins) {
22
+ GotenbergUtils.assert(
23
+ pageProperties.margins.top >= 0 &&
24
+ pageProperties.margins.bottom >= 0 &&
25
+ pageProperties.margins.left >= 0 &&
26
+ pageProperties.margins.left >= 0,
27
+ "negative margins are not allowed"
28
+ );
29
+
30
+ data.append("marginTop", pageProperties.margins.top);
31
+ data.append("marginBottom", pageProperties.margins.bottom);
32
+ data.append("marginLeft", pageProperties.margins.left);
33
+ data.append("marginRight", pageProperties.margins.right);
34
+ }
35
+
36
+ if (pageProperties.preferCssPageSize) {
37
+ data.append(
38
+ "preferCssPageSize",
39
+ String(pageProperties.preferCssPageSize)
40
+ );
41
+ }
42
+
43
+ if (pageProperties.printBackground) {
44
+ data.append("printBackground", String(pageProperties.printBackground));
45
+ }
46
+
47
+ if (pageProperties.landscape) {
48
+ data.append("landscape", String(pageProperties.landscape));
49
+ }
50
+
51
+ if (pageProperties.scale) {
52
+ GotenbergUtils.assert(
53
+ pageProperties.scale >= 0.1 && pageProperties.scale <= 2.0,
54
+ "scale is outside of [0.1 - 2] range"
55
+ );
56
+
57
+ data.append("scale", pageProperties.scale);
58
+ }
59
+
60
+ if (pageProperties.nativePageRanges) {
61
+ GotenbergUtils.assert(
62
+ pageProperties.nativePageRanges.from > 0 &&
63
+ pageProperties.nativePageRanges.to > 0 &&
64
+ pageProperties.nativePageRanges.to >=
65
+ pageProperties.nativePageRanges.from,
66
+ "page ranges syntax error"
67
+ );
68
+
69
+ data.append(
70
+ "nativePageRanges",
71
+ `${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`
72
+ );
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,101 @@
1
+ import FormData from "form-data";
2
+ import { ConverterUtils } from "../converter.utils";
3
+
4
+ describe("ConverterUtils", () => {
5
+ const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
6
+ const data = new FormData();
7
+
8
+ afterEach(() => {
9
+ jest.resetAllMocks();
10
+ });
11
+
12
+ describe("injectPageProperties", () => {
13
+ describe("Page size", () => {
14
+ describe("when page size is valid", () => {
15
+ it("should append page size to data", () => {
16
+ ConverterUtils.injectPageProperties(data, {
17
+ size: { width: 8.3, height: 11.7 },
18
+ });
19
+ expect(mockFormDataAppend).toBeCalledTimes(2);
20
+ expect(data.append).toHaveBeenCalledWith("paperWidth", 8.3);
21
+ expect(data.append).toHaveBeenNthCalledWith(2, "paperHeight", 11.7);
22
+ });
23
+ });
24
+ });
25
+
26
+ describe("Page margins", () => {
27
+ describe("when page margins are valid", () => {
28
+ it("should append page margins to data", () => {
29
+ ConverterUtils.injectPageProperties(data, {
30
+ margins: { top: 0.5, bottom: 0.5, left: 1, right: 1 },
31
+ });
32
+ expect(mockFormDataAppend).toBeCalledTimes(4);
33
+ expect(data.append).toHaveBeenCalledWith("marginTop", 0.5);
34
+ expect(data.append).toHaveBeenNthCalledWith(2, "marginBottom", 0.5);
35
+ expect(data.append).toHaveBeenNthCalledWith(3, "marginLeft", 1);
36
+ expect(data.append).toHaveBeenNthCalledWith(4, "marginRight", 1);
37
+ });
38
+ });
39
+ });
40
+
41
+ describe("Page css size", () => {
42
+ describe("when preferCssPageSize parameter is set", () => {
43
+ it("should append preferCssPageSize to data", () => {
44
+ ConverterUtils.injectPageProperties(data, {
45
+ preferCssPageSize: true,
46
+ });
47
+ expect(mockFormDataAppend).toBeCalledTimes(1);
48
+ expect(data.append).toHaveBeenCalledWith("preferCssPageSize", "true");
49
+ });
50
+ });
51
+ });
52
+
53
+ describe("Page background", () => {
54
+ describe("when printBackground parameter is set", () => {
55
+ it("should append printBackground to data", () => {
56
+ ConverterUtils.injectPageProperties(data, {
57
+ printBackground: true,
58
+ });
59
+ expect(mockFormDataAppend).toBeCalledTimes(1);
60
+ expect(data.append).toHaveBeenCalledWith("printBackground", "true");
61
+ });
62
+ });
63
+ });
64
+
65
+ describe("Page landscape", () => {
66
+ describe("when landscape parameter is set", () => {
67
+ it("should append landscape to data", () => {
68
+ ConverterUtils.injectPageProperties(data, {
69
+ landscape: true,
70
+ });
71
+ expect(mockFormDataAppend).toBeCalledTimes(1);
72
+ expect(data.append).toHaveBeenCalledWith("landscape", "true");
73
+ });
74
+ });
75
+ });
76
+
77
+ describe("Page scale", () => {
78
+ describe("when page scale is valid", () => {
79
+ it("should append scale to data", () => {
80
+ ConverterUtils.injectPageProperties(data, {
81
+ scale: 1.5,
82
+ });
83
+ expect(mockFormDataAppend).toBeCalledTimes(1);
84
+ expect(data.append).toHaveBeenCalledWith("scale", 1.5);
85
+ });
86
+ });
87
+ });
88
+
89
+ describe("Page ranges", () => {
90
+ describe("when nativePageRanges is valid", () => {
91
+ it("should append nativePageRanges to data", () => {
92
+ ConverterUtils.injectPageProperties(data, {
93
+ nativePageRanges: { from: 1, to: 6 },
94
+ });
95
+ expect(mockFormDataAppend).toBeCalledTimes(1);
96
+ expect(data.append).toHaveBeenCalledWith("nativePageRanges", "1-6");
97
+ });
98
+ });
99
+ });
100
+ });
101
+ });
@@ -0,0 +1,5 @@
1
+ export enum PdfFormat {
2
+ A_1a = "PDF/A-1a",
3
+ A_2b = "PDF/A-2b",
4
+ A_3b = "PDF/A-3b",
5
+ }
@@ -0,0 +1,25 @@
1
+ import FormData from "form-data";
2
+ import fetch from "node-fetch";
3
+
4
+ export class GotenbergUtils {
5
+ public static assert(condition: boolean, message: string): asserts condition {
6
+ if (!condition) {
7
+ throw new Error(message);
8
+ }
9
+ }
10
+
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
+ });
19
+
20
+ if (!response.ok) {
21
+ throw new Error(`${response.status} ${response.statusText}`);
22
+ }
23
+ return response.buffer();
24
+ }
25
+ }
@@ -0,0 +1,2 @@
1
+ export { PdfFormat } from "./constants";
2
+ export { GotenbergUtils } from "./gotenberg.utils";
@@ -0,0 +1,71 @@
1
+ import fetch from "node-fetch";
2
+ import FormData from "form-data";
3
+
4
+ import { GotenbergUtils } from "./../gotenberg.utils";
5
+
6
+ const { Response, FetchError } = jest.requireActual("node-fetch");
7
+
8
+ jest.mock("node-fetch", () => jest.fn());
9
+
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
+ });
28
+ });
29
+ });
30
+
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
+ });
42
+ });
43
+
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
+ });
69
+ });
70
+ });
71
+ });
@@ -0,0 +1,2 @@
1
+ export { PageProperties } from "./interfaces/libre-office.types";
2
+ export { LibreOfficeUtils } from "./utils/libre-office.utils";
@@ -0,0 +1,4 @@
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
4
+ };
@@ -0,0 +1,78 @@
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",
78
+ ];
@@ -0,0 +1,51 @@
1
+ import { constants, createReadStream, PathLike, promises } from "fs";
2
+ import path from "path";
3
+
4
+ import FormData from "form-data";
5
+
6
+ import { GotenbergUtils } from "../../common";
7
+ import { LIBRE_OFFICE_EXTENSIONS } from "./constants";
8
+ import { PageProperties } from "../interfaces/libre-office.types";
9
+
10
+ export class LibreOfficeUtils {
11
+ public static async injectFiles(files: PathLike[], data: FormData) {
12
+ for (const file of files) {
13
+ try {
14
+ await promises.access(file, constants.R_OK);
15
+ const filename = path.basename(file.toString());
16
+ const extension = path.extname(filename);
17
+ if (LIBRE_OFFICE_EXTENSIONS.includes(extension)) {
18
+ data.append(filename, createReadStream(file));
19
+ } else {
20
+ throw new Error(`${extension} is not supported`);
21
+ }
22
+ } catch (error) {
23
+ throw error;
24
+ }
25
+ }
26
+ }
27
+
28
+ public static injectPageProperties(
29
+ data: FormData,
30
+ pageProperties: PageProperties
31
+ ): void {
32
+ if (pageProperties.landscape) {
33
+ data.append("landscape", String(pageProperties.landscape));
34
+ }
35
+
36
+ if (pageProperties.nativePageRanges) {
37
+ GotenbergUtils.assert(
38
+ pageProperties.nativePageRanges.from > 0 &&
39
+ pageProperties.nativePageRanges.to > 0 &&
40
+ pageProperties.nativePageRanges.to >=
41
+ pageProperties.nativePageRanges.from,
42
+ "page ranges syntax error"
43
+ );
44
+
45
+ data.append(
46
+ "nativePageRanges",
47
+ `${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`
48
+ );
49
+ }
50
+ }
51
+ }