chromiumly 3.0.0 → 3.1.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 (56) hide show
  1. package/README.md +46 -4
  2. package/dist/chromium/converters/html.converter.d.ts +4 -2
  3. package/dist/chromium/converters/html.converter.js +5 -3
  4. package/dist/chromium/converters/html.converter.js.map +1 -1
  5. package/dist/chromium/converters/markdown.converter.d.ts +4 -2
  6. package/dist/chromium/converters/markdown.converter.js +5 -3
  7. package/dist/chromium/converters/markdown.converter.js.map +1 -1
  8. package/dist/chromium/converters/url.converter.d.ts +4 -2
  9. package/dist/chromium/converters/url.converter.js +5 -3
  10. package/dist/chromium/converters/url.converter.js.map +1 -1
  11. package/dist/chromium/interfaces/converter.types.d.ts +10 -0
  12. package/dist/chromium/screenshots/html.screenshot.js +1 -1
  13. package/dist/chromium/screenshots/html.screenshot.js.map +1 -1
  14. package/dist/chromium/screenshots/markdown.screenshot.js +1 -1
  15. package/dist/chromium/screenshots/markdown.screenshot.js.map +1 -1
  16. package/dist/chromium/screenshots/url.screenshot.js +1 -1
  17. package/dist/chromium/screenshots/url.screenshot.js.map +1 -1
  18. package/dist/chromium/utils/converter.utils.js +3 -0
  19. package/dist/chromium/utils/converter.utils.js.map +1 -1
  20. package/dist/common/gotenberg.utils.d.ts +3 -1
  21. package/dist/common/gotenberg.utils.js +10 -2
  22. package/dist/common/gotenberg.utils.js.map +1 -1
  23. package/dist/gotenberg.d.ts +12 -3
  24. package/dist/gotenberg.js +28 -7
  25. package/dist/gotenberg.js.map +1 -1
  26. package/dist/libre-office/interfaces/libre-office.types.d.ts +11 -0
  27. package/dist/libre-office/libre-office.d.ts +2 -2
  28. package/dist/libre-office/libre-office.js +1 -1
  29. package/dist/libre-office/libre-office.js.map +1 -1
  30. package/dist/libre-office/utils/libre-office.utils.d.ts +2 -2
  31. package/dist/libre-office/utils/libre-office.utils.js +24 -26
  32. package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
  33. package/dist/main.config.d.ts +10 -0
  34. package/dist/main.config.js +10 -0
  35. package/dist/main.config.js.map +1 -1
  36. package/dist/pdf-engines/pdf-engines.js +4 -4
  37. package/dist/pdf-engines/pdf-engines.js.map +1 -1
  38. package/package.json +5 -6
  39. package/src/chromium/converters/html.converter.ts +14 -5
  40. package/src/chromium/converters/markdown.converter.ts +14 -5
  41. package/src/chromium/converters/url.converter.ts +14 -5
  42. package/src/chromium/interfaces/converter.types.ts +11 -0
  43. package/src/chromium/screenshots/html.screenshot.ts +7 -2
  44. package/src/chromium/screenshots/markdown.screenshot.ts +7 -2
  45. package/src/chromium/screenshots/url.screenshot.ts +7 -2
  46. package/src/chromium/utils/converter.utils.ts +4 -0
  47. package/src/chromium/utils/tests/converter.utils.test.ts +33 -0
  48. package/src/common/gotenberg.utils.ts +15 -4
  49. package/src/common/tests/gotenberg.utils.test.ts +20 -3
  50. package/src/gotenberg.ts +29 -5
  51. package/src/libre-office/interfaces/libre-office.types.ts +141 -0
  52. package/src/libre-office/libre-office.ts +10 -7
  53. package/src/libre-office/utils/libre-office.utils.ts +33 -32
  54. package/src/libre-office/utils/tests/libre-office.utils.test.ts +24 -10
  55. package/src/main.config.ts +14 -0
  56. package/src/pdf-engines/pdf-engines.ts +24 -4
package/src/gotenberg.ts CHANGED
@@ -20,11 +20,35 @@ if (dotenvConfig.error) {
20
20
  */
21
21
  export class Gotenberg {
22
22
  /**
23
- * The Gotenberg service endpoint.
24
- * Defaults to the value from the environment variable `GOTENBERG_ENDPOINT`, or falls back to the configuration file.
23
+ * The endpoint for the Gotenberg service.
25
24
  * @type {string}
26
25
  */
27
- public static endpoint: string =
28
- process.env.GOTENBERG_ENDPOINT ||
29
- config.get<string>('gotenberg.endpoint');
26
+ static get endpoint(): string {
27
+ return (
28
+ process.env.GOTENBERG_ENDPOINT ||
29
+ config.get<string>('gotenberg.endpoint')
30
+ );
31
+ }
32
+
33
+ /**
34
+ * The username for basic authentication with the Gotenberg service.
35
+ * @type {string | undefined}
36
+ */
37
+ static get username(): string | undefined {
38
+ const hasUsername = config.has('gotenberg.api.basicAuth.username');
39
+ return hasUsername
40
+ ? config.get<string>('gotenberg.api.basicAuth.username')
41
+ : process.env.GOTENBERG_API_BASIC_AUTH_USERNAME;
42
+ }
43
+
44
+ /**
45
+ * The password for basic authentication with the Gotenberg service.
46
+ * @type {string | undefined}
47
+ */
48
+ static get password(): string | undefined {
49
+ const hasPassword = config.has('gotenberg.api.basicAuth.password');
50
+ return hasPassword
51
+ ? config.get<string>('gotenberg.api.basicAuth.password')
52
+ : process.env.GOTENBERG_API_BASIC_AUTH_PASSWORD;
53
+ }
30
54
  }
@@ -1,9 +1,150 @@
1
+ import { ReadStream } from 'fs';
1
2
  import { Metadata, PdfFormat } from '../../common';
2
3
 
4
+ type FileExtension =
5
+ | '123'
6
+ | '602'
7
+ | 'abw'
8
+ | 'bib'
9
+ | 'bmp'
10
+ | 'cdr'
11
+ | 'cgm'
12
+ | 'cmx'
13
+ | 'csv'
14
+ | 'cwk'
15
+ | 'dbf'
16
+ | 'dif'
17
+ | 'doc'
18
+ | 'docm'
19
+ | 'docx'
20
+ | 'dot'
21
+ | 'dotm'
22
+ | 'dotx'
23
+ | 'dxf'
24
+ | 'emf'
25
+ | 'eps'
26
+ | 'epub'
27
+ | 'fodg'
28
+ | 'fodp'
29
+ | 'fods'
30
+ | 'fodt'
31
+ | 'fopd'
32
+ | 'gif'
33
+ | 'htm'
34
+ | 'html'
35
+ | 'hwp'
36
+ | 'jpeg'
37
+ | 'jpg'
38
+ | 'key'
39
+ | 'ltx'
40
+ | 'lwp'
41
+ | 'mcw'
42
+ | 'met'
43
+ | 'mml'
44
+ | 'mw'
45
+ | 'numbers'
46
+ | 'odd'
47
+ | 'odg'
48
+ | 'odm'
49
+ | 'odp'
50
+ | 'ods'
51
+ | 'odt'
52
+ | 'otg'
53
+ | 'oth'
54
+ | 'otp'
55
+ | 'ots'
56
+ | 'ott'
57
+ | 'pages'
58
+ | 'pbm'
59
+ | 'pcd'
60
+ | 'pct'
61
+ | 'pcx'
62
+ | 'pdb'
63
+ | 'pdf'
64
+ | 'pgm'
65
+ | 'png'
66
+ | 'pot'
67
+ | 'potm'
68
+ | 'potx'
69
+ | 'ppm'
70
+ | 'pps'
71
+ | 'ppt'
72
+ | 'pptm'
73
+ | 'pptx'
74
+ | 'psd'
75
+ | 'psw'
76
+ | 'pub'
77
+ | 'pwp'
78
+ | 'pxl'
79
+ | 'ras'
80
+ | 'rtf'
81
+ | 'sda'
82
+ | 'sdc'
83
+ | 'sdd'
84
+ | 'sdp'
85
+ | 'sdw'
86
+ | 'sgl'
87
+ | 'slk'
88
+ | 'smf'
89
+ | 'stc'
90
+ | 'std'
91
+ | 'sti'
92
+ | 'stw'
93
+ | 'svg'
94
+ | 'svm'
95
+ | 'swf'
96
+ | 'sxc'
97
+ | 'sxd'
98
+ | 'sxg'
99
+ | 'sxi'
100
+ | 'sxm'
101
+ | 'sxw'
102
+ | 'tga'
103
+ | 'tif'
104
+ | 'tiff'
105
+ | 'txt'
106
+ | 'uof'
107
+ | 'uop'
108
+ | 'uos'
109
+ | 'uot'
110
+ | 'vdx'
111
+ | 'vor'
112
+ | 'vsd'
113
+ | 'vsdm'
114
+ | 'vsdx'
115
+ | 'wb2'
116
+ | 'wk1'
117
+ | 'wks'
118
+ | 'wmf'
119
+ | 'wpd'
120
+ | 'wpg'
121
+ | 'wps'
122
+ | 'xbm'
123
+ | 'xhtml'
124
+ | 'xls'
125
+ | 'xlsb'
126
+ | 'xlsm'
127
+ | 'xlsx'
128
+ | 'xlt'
129
+ | 'xltm'
130
+ | 'xltx'
131
+ | 'xlw'
132
+ | 'xml'
133
+ | 'xpm'
134
+ | 'zabw';
135
+
136
+ type FileInfo = {
137
+ data: Buffer | ReadStream;
138
+ ext: FileExtension;
139
+ };
140
+
141
+ export type PathLikeOrReadStream = string | FileInfo;
142
+
3
143
  export type PageProperties = {
4
144
  landscape?: boolean; // Set the paper orientation to landscape (default false)
5
145
  nativePageRanges?: { from: number; to: number }; // Page ranges to print
6
146
  exportFormFields?: boolean; // Set whether to export the form fields or to use the inputted/selected content of the fields (default true)
147
+ singlePageSheets?: boolean; // Set whether to render the entire spreadsheet as a single page. (default false)
7
148
  };
8
149
 
9
150
  export type ConversionOptions = {
@@ -1,13 +1,11 @@
1
1
  import FormData from 'form-data';
2
2
 
3
3
  import { Chromiumly } from '../main.config';
4
+ import { GotenbergUtils, PdfFormat, Metadata } from '../common';
4
5
  import {
5
- GotenbergUtils,
6
- PathLikeOrReadStream,
7
- PdfFormat,
8
- Metadata
9
- } from '../common';
10
- import { PageProperties } from './interfaces/libre-office.types';
6
+ PageProperties,
7
+ PathLikeOrReadStream
8
+ } from './interfaces/libre-office.types';
11
9
  import { LibreOfficeUtils } from './utils/libre-office.utils';
12
10
 
13
11
  /**
@@ -53,6 +51,11 @@ export class LibreOffice {
53
51
 
54
52
  const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
55
53
 
56
- return GotenbergUtils.fetch(endpoint, data);
54
+ return GotenbergUtils.fetch(
55
+ endpoint,
56
+ data,
57
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
58
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
59
+ );
57
60
  }
58
61
  }
@@ -1,21 +1,33 @@
1
- import { constants, createReadStream, promises, ReadStream } from 'fs';
1
+ import { constants, createReadStream, promises } from 'fs';
2
2
  import path from 'path';
3
3
 
4
- import { fromStream, fromBuffer } from 'file-type';
5
-
6
4
  import FormData from 'form-data';
7
5
 
8
- import { GotenbergUtils, PathLikeOrReadStream } from '../../common';
6
+ import { GotenbergUtils } from '../../common';
9
7
  import { LIBRE_OFFICE_EXTENSIONS } from './constants';
10
8
  import {
11
9
  ConversionOptions,
12
- PageProperties
10
+ PageProperties,
11
+ PathLikeOrReadStream
13
12
  } from '../interfaces/libre-office.types';
14
13
 
15
14
  /**
16
15
  * Utility class for handling common tasks related to LibreOffice conversions.
17
16
  */
18
17
  export class LibreOfficeUtils {
18
+ private static async getFileInfo(file: PathLikeOrReadStream) {
19
+ if (typeof file === 'string') {
20
+ await promises.access(file, constants.R_OK);
21
+ const filename = path.basename(path.parse(file).base);
22
+ return {
23
+ data: createReadStream(file),
24
+ ext: path.extname(filename).slice(1)
25
+ };
26
+ } else {
27
+ return { data: file.data, ext: file.ext };
28
+ }
29
+ }
30
+
19
31
  /**
20
32
  * Adds files to the FormData object for LibreOffice conversion.
21
33
  *
@@ -27,33 +39,15 @@ export class LibreOfficeUtils {
27
39
  files: PathLikeOrReadStream[],
28
40
  data: FormData
29
41
  ) {
30
- for (const [key, value] of files.entries()) {
31
- let file = value;
32
- let fileInfo;
33
-
34
- if (Buffer.isBuffer(value)) {
35
- fileInfo = await fromBuffer(value);
36
- } else if (value instanceof ReadStream) {
37
- fileInfo = await fromStream(value);
38
- } else {
39
- await promises.access(value, constants.R_OK);
40
- const filename = path.basename(value.toString());
41
- fileInfo = { ext: path.extname(filename).slice(1) };
42
- file = createReadStream(value);
43
- }
44
-
45
- if (!fileInfo) {
46
- throw new Error('File type could not be determined');
47
- }
48
-
49
- const extension = fileInfo.ext;
50
-
51
- if (LIBRE_OFFICE_EXTENSIONS.includes(extension)) {
52
- data.append('files', file, `${key}.${extension}`);
53
- } else {
54
- throw new Error(`${extension} is not supported`);
55
- }
56
- }
42
+ await Promise.all(
43
+ files.map(async (file, key) => {
44
+ const fileInfo = await this.getFileInfo(file);
45
+ if (!LIBRE_OFFICE_EXTENSIONS.includes(fileInfo.ext)) {
46
+ throw new Error(`${fileInfo.ext} is not supported`);
47
+ }
48
+ data.append('files', fileInfo.data, `${key}.${fileInfo.ext}`);
49
+ })
50
+ );
57
51
  }
58
52
 
59
53
  /**
@@ -91,6 +85,13 @@ export class LibreOfficeUtils {
91
85
  String(pageProperties.exportFormFields)
92
86
  );
93
87
  }
88
+
89
+ if (pageProperties.singlePageSheets) {
90
+ data.append(
91
+ 'singlePageSheets',
92
+ String(pageProperties.singlePageSheets)
93
+ );
94
+ }
94
95
  }
95
96
 
96
97
  /**
@@ -3,12 +3,10 @@ import { promises, createReadStream } from 'fs';
3
3
  import { LibreOfficeUtils } from '../libre-office.utils';
4
4
 
5
5
  import FormData from 'form-data';
6
- import FileType from 'file-type';
7
6
 
8
7
  describe('LibreOfficeUtils', () => {
9
8
  const mockPromisesAccess = jest.spyOn(promises, 'access');
10
9
  const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
11
- const mockFromBuffer = jest.spyOn(FileType, 'fromBuffer');
12
10
 
13
11
  const data = new FormData();
14
12
 
@@ -37,12 +35,11 @@ describe('LibreOfficeUtils', () => {
37
35
  describe('when files parameter contains a buffer', () => {
38
36
  it('should append each file to data', async () => {
39
37
  mockPromisesAccess.mockResolvedValueOnce();
40
- mockFromBuffer.mockResolvedValueOnce({
41
- ext: 'docx',
42
- mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
43
- });
44
38
  await LibreOfficeUtils.addFiles(
45
- [Buffer.from('data'), 'path/to/file.bib'],
39
+ [
40
+ { data: Buffer.from('data'), ext: 'csv' },
41
+ 'path/to/file.bib'
42
+ ],
46
43
  data
47
44
  );
48
45
  expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
@@ -53,14 +50,16 @@ describe('LibreOfficeUtils', () => {
53
50
  describe('when one of the files has undetermined format', () => {
54
51
  it('should throw an error', async () => {
55
52
  mockPromisesAccess.mockResolvedValueOnce();
56
- mockFromBuffer.mockResolvedValueOnce(undefined);
57
53
 
58
54
  await expect(
59
55
  LibreOfficeUtils.addFiles(
60
- [Buffer.from('data'), 'path/to/file.bib'],
56
+ [
57
+ { data: Buffer.from('data'), ext: 'docx' },
58
+ 'path/to/file.none'
59
+ ],
61
60
  data
62
61
  )
63
- ).rejects.toThrow('File type could not be determined');
62
+ ).rejects.toThrow('none is not supported');
64
63
  });
65
64
  });
66
65
 
@@ -138,5 +137,20 @@ describe('LibreOfficeUtils', () => {
138
137
  });
139
138
  });
140
139
  });
140
+
141
+ describe('Single page sheets', () => {
142
+ describe('when singlePageSheets parameter is set', () => {
143
+ it('should append singlePageSheets to data', () => {
144
+ LibreOfficeUtils.addPageProperties(data, {
145
+ singlePageSheets: true
146
+ });
147
+ expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
148
+ expect(data.append).toHaveBeenCalledWith(
149
+ 'singlePageSheets',
150
+ 'true'
151
+ );
152
+ });
153
+ });
154
+ });
141
155
  });
142
156
  });
@@ -38,6 +38,20 @@ export class Chromiumly {
38
38
  */
39
39
  public static readonly GOTENBERG_ENDPOINT = Gotenberg.endpoint;
40
40
 
41
+ /**
42
+ * The username for basic authentication with the Gotenberg service.
43
+ * @type {string | undefined}
44
+ */
45
+ public static readonly GOTENBERG_API_BASIC_AUTH_USERNAME =
46
+ Gotenberg.username;
47
+
48
+ /**
49
+ * The password for basic authentication with the Gotenberg service.
50
+ * @type {string | undefined}
51
+ */
52
+ public static readonly GOTENBERG_API_BASIC_AUTH_PASSWORD =
53
+ Gotenberg.password;
54
+
41
55
  /**
42
56
  * The path for Chromium-related conversions.
43
57
  * @type {string}
@@ -42,7 +42,12 @@ export class PDFEngines {
42
42
  metadata
43
43
  });
44
44
  const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.merge}`;
45
- return GotenbergUtils.fetch(endpoint, data);
45
+ return GotenbergUtils.fetch(
46
+ endpoint,
47
+ data,
48
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
49
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
50
+ );
46
51
  }
47
52
 
48
53
  /**
@@ -73,7 +78,12 @@ export class PDFEngines {
73
78
 
74
79
  const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
75
80
 
76
- return GotenbergUtils.fetch(endpoint, data);
81
+ return GotenbergUtils.fetch(
82
+ endpoint,
83
+ data,
84
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
85
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
86
+ );
77
87
  }
78
88
 
79
89
  /**
@@ -91,7 +101,12 @@ export class PDFEngines {
91
101
 
92
102
  const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.readMetadata}`;
93
103
 
94
- return GotenbergUtils.fetch(endpoint, data);
104
+ return GotenbergUtils.fetch(
105
+ endpoint,
106
+ data,
107
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
108
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
109
+ );
95
110
  }
96
111
 
97
112
  /**
@@ -115,7 +130,12 @@ export class PDFEngines {
115
130
 
116
131
  const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.writeMetadata}`;
117
132
 
118
- return GotenbergUtils.fetch(endpoint, data);
133
+ return GotenbergUtils.fetch(
134
+ endpoint,
135
+ data,
136
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
137
+ Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
138
+ );
119
139
  }
120
140
 
121
141
  /**