chromiumly 1.0.2 → 2.0.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.
Files changed (91) hide show
  1. package/README.md +50 -20
  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/chromium/converters/url.converter.js +36 -0
  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/chromium/interfaces/converter.interface.d.ts +9 -0
  18. package/dist/chromium/interfaces/converter.interface.js +3 -0
  19. package/dist/chromium/interfaces/converter.interface.js.map +1 -0
  20. package/dist/chromium/interfaces/converter.types.d.ts +23 -0
  21. package/dist/chromium/interfaces/converter.types.js +3 -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/gotenberg.d.ts +4 -0
  36. package/dist/gotenberg.js +12 -0
  37. package/dist/gotenberg.js.map +1 -0
  38. package/dist/libre-office/index.d.ts +2 -0
  39. package/dist/libre-office/index.js +6 -0
  40. package/dist/libre-office/index.js.map +1 -0
  41. package/dist/libre-office/interfaces/libre-office.types.d.ts +7 -0
  42. package/dist/libre-office/interfaces/libre-office.types.js +3 -0
  43. package/dist/libre-office/interfaces/libre-office.types.js.map +1 -0
  44. package/dist/libre-office/utils/constants.d.ts +1 -0
  45. package/dist/libre-office/utils/constants.js +82 -0
  46. package/dist/libre-office/utils/constants.js.map +1 -0
  47. package/dist/libre-office/utils/libre-office.utils.d.ts +8 -0
  48. package/dist/libre-office/utils/libre-office.utils.js +44 -0
  49. package/dist/libre-office/utils/libre-office.utils.js.map +1 -0
  50. package/dist/main.config.d.ts +29 -0
  51. package/dist/main.config.js +37 -0
  52. package/dist/main.config.js.map +1 -0
  53. package/dist/main.d.ts +3 -0
  54. package/dist/main.js +9 -7
  55. package/dist/main.js.map +1 -0
  56. package/dist/pdf-engines/index.d.ts +1 -0
  57. package/dist/pdf-engines/index.js +6 -0
  58. package/dist/pdf-engines/index.js.map +1 -0
  59. package/dist/pdf-engines/pdf.engine.d.ts +16 -0
  60. package/dist/pdf-engines/pdf.engine.js +47 -0
  61. package/dist/pdf-engines/pdf.engine.js.map +1 -0
  62. package/dist/pdf-engines/utils/engine.utils.d.ts +6 -0
  63. package/dist/pdf-engines/utils/engine.utils.js +24 -0
  64. package/dist/pdf-engines/utils/engine.utils.js.map +1 -0
  65. package/package.json +6 -2
  66. package/src/chromium/converters/converter.ts +9 -0
  67. package/src/chromium/converters/html.converter.ts +41 -0
  68. package/src/chromium/converters/markdown.converter.ts +45 -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/common/constants.ts +5 -0
  75. package/src/common/gotenberg.utils.ts +25 -0
  76. package/src/common/index.ts +2 -0
  77. package/src/libre-office/index.ts +2 -0
  78. package/src/libre-office/interfaces/libre-office.types.ts +4 -0
  79. package/src/libre-office/utils/constants.ts +78 -0
  80. package/src/libre-office/utils/libre-office.utils.ts +51 -0
  81. package/src/main.config.ts +23 -10
  82. package/src/main.ts +3 -3
  83. package/src/pdf-engines/index.ts +1 -0
  84. package/src/pdf-engines/pdf.engine.ts +53 -0
  85. package/src/pdf-engines/utils/engine.utils.ts +18 -0
  86. package/.github/workflows/publish.yml +0 -18
  87. package/src/common/converter.ts +0 -18
  88. package/src/common/converter.utils.ts +0 -101
  89. package/src/converters/html.converter.ts +0 -30
  90. package/src/converters/markdown.converter.ts +0 -33
  91. package/tsconfig.json +0 -27
@@ -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,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
+ }
@@ -1,23 +1,36 @@
1
1
  import { Gotenberg } from "./gotenberg";
2
2
 
3
- export enum Route {
3
+ export enum ChromiumRoute {
4
4
  URL = "url",
5
5
  HTML = "html",
6
6
  MARKDOWN = "markdown",
7
7
  }
8
8
 
9
+ enum PdfEngineRoute {
10
+ MERGE = "merge",
11
+ }
12
+
13
+ enum LibreOfficeRoute {
14
+ CONVERT = "convert",
15
+ }
9
16
  export class Chromiumly {
10
- private static readonly GOTENBERG_ENDPOINT = Gotenberg.endpoint;
17
+ public static readonly GOTENBERG_ENDPOINT = Gotenberg.endpoint;
11
18
 
12
- private static readonly PATH = "forms/chromium/convert";
19
+ public static readonly CHROMIUM_PATH = "forms/chromium/convert";
20
+ public static readonly PDF_ENGINES_PATH = "forms/pdfengines";
21
+ public static readonly LIBRE_OFFICE_PATH = "forms/libreoffice";
13
22
 
14
- private static readonly ROUTES = {
15
- url: Route.URL,
16
- html: Route.HTML,
17
- markdown: Route.MARKDOWN,
23
+ public static readonly CHROMIUM_ROUTES = {
24
+ url: ChromiumRoute.URL,
25
+ html: ChromiumRoute.HTML,
26
+ markdown: ChromiumRoute.MARKDOWN,
18
27
  };
19
28
 
20
- public static readonly endpoint = Chromiumly.GOTENBERG_ENDPOINT;
21
- public static readonly path = Chromiumly.PATH;
22
- public static readonly routes = Chromiumly.ROUTES;
29
+ public static readonly PDF_ENGINE_ROUTES = {
30
+ merge: PdfEngineRoute.MERGE,
31
+ };
32
+
33
+ public static readonly LIBRE_OFFICE_ROUTES = {
34
+ convert: LibreOfficeRoute.CONVERT,
35
+ };
23
36
  }
package/src/main.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { HtmlConverter } from "./converters/html.converter";
2
- export { MarkdownConverter } from "./converters/markdown.converter";
3
- export { UrlConverter } from "./converters/url.converter";
1
+ export { PdfFormat } from "./common/constants";
2
+ export { HtmlConverter, MarkdownConverter, UrlConverter } from "./chromium";
3
+ export { PDFEngine } from "./pdf-engines";
@@ -0,0 +1 @@
1
+ export { PDFEngine } from "./pdf.engine";
@@ -0,0 +1,53 @@
1
+ import { PathLike, promises } from "fs";
2
+ import path from "path";
3
+
4
+ import FormData from "form-data";
5
+
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
+
11
+ export class PDFEngine {
12
+ public static async merge({ files }: { files: PathLike[] }): Promise<Buffer> {
13
+ const data = new FormData();
14
+ await PDFEngineUtils.injectFiles(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
+
19
+ public static async convert({
20
+ files,
21
+ properties,
22
+ pdfFormat,
23
+ merge,
24
+ }: {
25
+ files: PathLike[];
26
+ properties?: PageProperties;
27
+ pdfFormat?: PdfFormat;
28
+ merge?: boolean;
29
+ }): Promise<Buffer> {
30
+ const data = new FormData();
31
+ if (pdfFormat) {
32
+ data.append("pdfFormat", pdfFormat);
33
+ }
34
+ if (merge) {
35
+ data.append("merge", String(merge));
36
+ }
37
+ if (properties) {
38
+ LibreOfficeUtils.injectPageProperties(data, properties);
39
+ }
40
+ await LibreOfficeUtils.injectFiles(files, data);
41
+ const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
42
+ return GotenbergUtils.fetch(endpoint, data);
43
+ }
44
+
45
+ public static async generate(
46
+ filename: string,
47
+ buffer: Buffer
48
+ ): Promise<void> {
49
+ const __generated__ = path.resolve(process.cwd(), "__generated__");
50
+ await promises.mkdir(path.resolve(__generated__), { recursive: true });
51
+ await promises.writeFile(path.resolve(__generated__, filename), buffer);
52
+ }
53
+ }
@@ -0,0 +1,18 @@
1
+ import { constants, createReadStream, PathLike, promises } from "fs";
2
+ import path from "path";
3
+
4
+ import FormData from "form-data";
5
+
6
+ export class PDFEngineUtils {
7
+ public static async injectFiles(files: PathLike[], data: FormData) {
8
+ for (const file of files) {
9
+ try {
10
+ await promises.access(file, constants.R_OK);
11
+ const filename = path.basename(file.toString());
12
+ data.append(filename, createReadStream(file));
13
+ } catch (error) {
14
+ throw error;
15
+ }
16
+ }
17
+ }
18
+ }
@@ -1,18 +0,0 @@
1
- name: Publish
2
- on:
3
- release:
4
- types: [published]
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v2
10
- - uses: actions/setup-node@v2
11
- with:
12
- node-version: "14.x"
13
- registry-url: "https://registry.npmjs.org"
14
- - run: yarn
15
- - run: yarn build
16
- - run: yarn publish --access public
17
- env:
18
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -1,18 +0,0 @@
1
- import { promises } from "fs";
2
- import path from "path";
3
-
4
- import { Chromiumly, Route } from "../main.config";
5
-
6
- export abstract class Converter {
7
- readonly endpoint: string;
8
-
9
- constructor(route: Route) {
10
- this.endpoint = `${Chromiumly.endpoint}/${Chromiumly.path}/${Chromiumly.routes[route]}`;
11
- }
12
-
13
- async generate(filename: string, buffer: Buffer): Promise<void> {
14
- const __generated__ = path.resolve(process.cwd(), "__generated__");
15
- await promises.mkdir(path.resolve(__generated__), { recursive: true });
16
- await promises.writeFile(path.resolve(__generated__, filename), buffer);
17
- }
18
- }
@@ -1,101 +0,0 @@
1
- import FormData from "form-data";
2
- import fetch from "node-fetch";
3
-
4
- import { PageProperties } from "./converter.types";
5
-
6
- export class ConverterUtils {
7
- private static assert(
8
- condition: boolean,
9
- message: string
10
- ): asserts condition {
11
- if (!condition) {
12
- throw new Error(message);
13
- }
14
- }
15
-
16
- public static injectPageProperties(
17
- data: FormData,
18
- pageProperties: PageProperties
19
- ): void {
20
- if (pageProperties) {
21
- if (pageProperties.size) {
22
- ConverterUtils.assert(
23
- pageProperties.size.width >= 1.0 && pageProperties.size.height >= 1.5,
24
- "size is smaller than the minimum printing requirements (i.e. 1.0 x 1.5 in)"
25
- );
26
-
27
- data.append("paperWidth", pageProperties.size.width);
28
- data.append("paperHeight", pageProperties.size.height);
29
- }
30
-
31
- if (pageProperties.margins) {
32
- ConverterUtils.assert(
33
- pageProperties.margins.top >= 0 &&
34
- pageProperties.margins.bottom >= 0 &&
35
- pageProperties.margins.left >= 0 &&
36
- pageProperties.margins.left >= 0,
37
- "negative margins are not allowed"
38
- );
39
-
40
- data.append("marginTop", pageProperties.margins.top);
41
- data.append("marginBottom", pageProperties.margins.bottom);
42
- data.append("marginLeft", pageProperties.margins.left);
43
- data.append("marginRight", pageProperties.margins.right);
44
- }
45
-
46
- if (pageProperties.preferCssPageSize) {
47
- data.append(
48
- "preferCssPageSize",
49
- String(pageProperties.preferCssPageSize)
50
- );
51
- }
52
-
53
- if (pageProperties.printBackground) {
54
- data.append("printBackground", String(pageProperties.printBackground));
55
- }
56
-
57
- if (pageProperties.landscape) {
58
- data.append("landscape", String(pageProperties.landscape));
59
- }
60
-
61
- if (pageProperties.scale) {
62
- ConverterUtils.assert(
63
- pageProperties.scale >= 0.1 && pageProperties.scale <= 2.0,
64
- "scale is outside of [0.1 - 2] range"
65
- );
66
-
67
- data.append("scale", pageProperties.scale);
68
- }
69
-
70
- if (pageProperties.nativePageRanges) {
71
- ConverterUtils.assert(
72
- pageProperties.nativePageRanges.from > 0 &&
73
- pageProperties.nativePageRanges.to > 0 &&
74
- pageProperties.nativePageRanges.to >=
75
- pageProperties.nativePageRanges.from,
76
- "page ranges syntax error"
77
- );
78
-
79
- data.append(
80
- "nativePageRanges",
81
- `${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`
82
- );
83
- }
84
- }
85
- }
86
-
87
- static async fetch(endpoint: string, data: FormData): Promise<Buffer> {
88
- const response = await fetch(endpoint, {
89
- method: "post",
90
- body: data,
91
- headers: {
92
- ...data.getHeaders(),
93
- },
94
- });
95
-
96
- if (!response.ok) {
97
- throw new Error(`${response.status} ${response.statusText}`);
98
- }
99
- return response.buffer();
100
- }
101
- }
@@ -1,30 +0,0 @@
1
- import { createReadStream, PathLike } from "fs";
2
-
3
- import FormData from "form-data";
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";
10
-
11
- export class HtmlConverter extends Converter implements IConverter {
12
- constructor() {
13
- super(Route.HTML);
14
- }
15
-
16
- async convert({
17
- html,
18
- properties,
19
- }: {
20
- html: PathLike;
21
- properties?: PageProperties;
22
- }): Promise<Buffer> {
23
- const data = new FormData();
24
- data.append("index.html", createReadStream(html));
25
- if (properties) {
26
- ConverterUtils.injectPageProperties(data, properties);
27
- }
28
- return ConverterUtils.fetch(this.endpoint, data);
29
- }
30
- }
@@ -1,33 +0,0 @@
1
- import { createReadStream, PathLike } from "fs";
2
-
3
- import FormData from "form-data";
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";
10
-
11
- export class MarkdownConverter extends Converter implements IConverter {
12
- constructor() {
13
- super(Route.MARKDOWN);
14
- }
15
-
16
- async convert({
17
- html,
18
- markdown,
19
- properties,
20
- }: {
21
- html: PathLike;
22
- markdown: PathLike;
23
- properties?: PageProperties;
24
- }): Promise<Buffer> {
25
- const data = new FormData();
26
- data.append("index.html", createReadStream(html));
27
- data.append("file.md", createReadStream(markdown));
28
- if (properties) {
29
- ConverterUtils.injectPageProperties(data, properties);
30
- }
31
- return ConverterUtils.fetch(this.endpoint, data);
32
- }
33
- }
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es6",
4
- "module": "commonjs",
5
- "moduleResolution": "node",
6
- "declaration": true,
7
- "strict": true,
8
- "noImplicitAny": true,
9
- "strictNullChecks": true,
10
- "strictFunctionTypes": true,
11
- "noUnusedLocals": true,
12
- "noUnusedParameters": true,
13
- "noImplicitReturns": true,
14
- "noFallthroughCasesInSwitch": true,
15
- "importHelpers": true,
16
- "skipLibCheck": true,
17
- "esModuleInterop": true,
18
- "allowSyntheticDefaultImports": true,
19
- "experimentalDecorators": true,
20
- "sourceMap": true,
21
- "outDir": "./dist",
22
- "types": ["node"],
23
- "lib": ["ES6"]
24
- },
25
- "include": ["src/**/*.ts"],
26
- "exclude": ["node_modules", "**/*.test.ts"]
27
- }