chromiumly 3.2.1 → 3.3.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 +21 -2
  2. package/dist/chromium/converters/converter.js +1 -1
  3. package/dist/chromium/converters/converter.js.map +1 -1
  4. package/dist/chromium/converters/html.converter.js +1 -1
  5. package/dist/chromium/converters/html.converter.js.map +1 -1
  6. package/dist/chromium/converters/markdown.converter.js +1 -1
  7. package/dist/chromium/converters/markdown.converter.js.map +1 -1
  8. package/dist/chromium/converters/url.converter.js +1 -1
  9. package/dist/chromium/converters/url.converter.js.map +1 -1
  10. package/dist/chromium/screenshots/html.screenshot.js +1 -1
  11. package/dist/chromium/screenshots/html.screenshot.js.map +1 -1
  12. package/dist/chromium/screenshots/markdown.screenshot.js +1 -1
  13. package/dist/chromium/screenshots/markdown.screenshot.js.map +1 -1
  14. package/dist/chromium/screenshots/screenshot.js +1 -1
  15. package/dist/chromium/screenshots/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/gotenberg.d.ts +1 -1
  19. package/dist/gotenberg.js +4 -2
  20. package/dist/gotenberg.js.map +1 -1
  21. package/dist/libre-office/libre-office.js +2 -2
  22. package/dist/libre-office/libre-office.js.map +1 -1
  23. package/dist/main.config.d.ts +30 -3
  24. package/dist/main.config.js +42 -3
  25. package/dist/main.config.js.map +1 -1
  26. package/dist/main.d.ts +1 -0
  27. package/dist/main.js +3 -1
  28. package/dist/main.js.map +1 -1
  29. package/dist/pdf-engines/pdf-engines.js +8 -8
  30. package/dist/pdf-engines/pdf-engines.js.map +1 -1
  31. package/dist/pdf-engines/utils/pdf-engines.utils.js +5 -5
  32. package/dist/pdf-engines/utils/pdf-engines.utils.js.map +1 -1
  33. package/package.json +16 -11
  34. package/src/.prettierrc.yml +0 -4
  35. package/src/chromium/converters/converter.ts +0 -22
  36. package/src/chromium/converters/html.converter.ts +0 -134
  37. package/src/chromium/converters/markdown.converter.ts +0 -129
  38. package/src/chromium/converters/tests/html.converter.test.ts +0 -190
  39. package/src/chromium/converters/tests/markdown.converter.test.ts +0 -187
  40. package/src/chromium/converters/tests/url.converter.test.ts +0 -164
  41. package/src/chromium/converters/url.converter.ts +0 -125
  42. package/src/chromium/index.ts +0 -6
  43. package/src/chromium/interfaces/common.types.ts +0 -15
  44. package/src/chromium/interfaces/converter.types.ts +0 -45
  45. package/src/chromium/interfaces/screenshot.types.ts +0 -15
  46. package/src/chromium/screenshots/html.screenshot.ts +0 -105
  47. package/src/chromium/screenshots/markdown.screenshot.ts +0 -100
  48. package/src/chromium/screenshots/screenshot.ts +0 -22
  49. package/src/chromium/screenshots/tests/html.screenshot.test.ts +0 -192
  50. package/src/chromium/screenshots/tests/markdown.screenshot.test.ts +0 -176
  51. package/src/chromium/screenshots/tests/url.screenshot.test.ts +0 -166
  52. package/src/chromium/screenshots/url.screenshot.ts +0 -96
  53. package/src/chromium/utils/converter.utils.ts +0 -187
  54. package/src/chromium/utils/screenshot.utils.ts +0 -127
  55. package/src/chromium/utils/tests/converter.utils.test.ts +0 -496
  56. package/src/chromium/utils/tests/screenshot.utils.test.ts +0 -338
  57. package/src/common/constants.ts +0 -9
  58. package/src/common/gotenberg.utils.ts +0 -86
  59. package/src/common/index.ts +0 -3
  60. package/src/common/tests/gotenberg.utils.test.ts +0 -141
  61. package/src/common/types.ts +0 -7
  62. package/src/gotenberg.ts +0 -54
  63. package/src/libre-office/index.ts +0 -1
  64. package/src/libre-office/interfaces/libre-office.types.ts +0 -156
  65. package/src/libre-office/libre-office.ts +0 -61
  66. package/src/libre-office/tests/libre-office.test.ts +0 -56
  67. package/src/libre-office/utils/constants.ts +0 -132
  68. package/src/libre-office/utils/libre-office.utils.ts +0 -128
  69. package/src/libre-office/utils/tests/libre-office.utils.test.ts +0 -156
  70. package/src/main.config.ts +0 -106
  71. package/src/main.ts +0 -11
  72. package/src/pdf-engines/index.ts +0 -1
  73. package/src/pdf-engines/interfaces/pdf-engines.types.ts +0 -10
  74. package/src/pdf-engines/pdf-engines.ts +0 -156
  75. package/src/pdf-engines/tests/pdf.engine.test.ts +0 -163
  76. package/src/pdf-engines/utils/pdf-engines.utils.ts +0 -68
  77. package/src/pdf-engines/utils/tests/pdf-engines.utils.test.ts +0 -71
@@ -1,134 +0,0 @@
1
- import FormData from 'form-data';
2
-
3
- import {
4
- GotenbergUtils,
5
- Metadata,
6
- PathLikeOrReadStream,
7
- PdfFormat
8
- } from '../../common';
9
- import { Cookie, PageProperties } from '../interfaces/converter.types';
10
- import { ConverterUtils } from '../utils/converter.utils';
11
- import { Converter } from './converter';
12
- import { ChromiumRoute, Chromiumly } from '../../main.config';
13
- import { EmulatedMediaType } from '../interfaces/common.types';
14
-
15
- /**
16
- * Class representing an HTML converter that extends the base Converter class.
17
- * This class is used to convert HTML content to PDF using Gotenberg service.
18
- *
19
- * @extends Converter
20
- */
21
- export class HtmlConverter extends Converter {
22
- /**
23
- * Creates an instance of HtmlConverter.
24
- * Initializes the converter with the HTML conversion route.
25
- */
26
- constructor() {
27
- super(ChromiumRoute.HTML);
28
- }
29
-
30
- /**
31
- * Converts HTML content to PDF.
32
- *
33
- * @param {Object} options - Conversion options.
34
- * @param {PathLikeOrReadStream} options.html - PathLike or ReadStream of the HTML content to be converted.
35
- * @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
36
- * @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
37
- * @param {PageProperties} [options.properties] - Page properties for the conversion.
38
- * @param {PdfFormat} [options.pdfFormat] - PDF format options.
39
- * @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output.
40
- * @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the conversion.
41
- * @param {string} [options.waitDelay] - Delay before the conversion process starts.
42
- * @param {string} [options.waitForExpression] - JavaScript expression to wait for before completing the conversion.
43
- * @param {string} [options.userAgent] - User agent string to use during the conversion.
44
- * @param {Record<string, string>} [options.extraHttpHeaders] - Additional HTTP headers for the conversion.
45
- * @param {number[]} [options.failOnHttpStatusCodes] - Whether to fail on HTTP status code.
46
- * @param {boolean} [options.failOnConsoleExceptions] - Whether to fail on console exceptions during conversion.
47
- * @param {boolean} [options.skipNetworkIdleEvent] - Whether to skip network idle event.
48
- * @param {Metadata} options.metadata - Metadata to be written.
49
- * @param {Cookie[]} options.cookies - Cookies to be written.
50
- * @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a Buffer.
51
- */
52
- async convert({
53
- html,
54
- assets,
55
- header,
56
- footer,
57
- properties,
58
- pdfFormat,
59
- pdfUA,
60
- emulatedMediaType,
61
- waitDelay,
62
- waitForExpression,
63
- userAgent,
64
- extraHttpHeaders,
65
- failOnHttpStatusCodes,
66
- failOnConsoleExceptions,
67
- skipNetworkIdleEvent,
68
- metadata,
69
- cookies
70
- }: {
71
- html: PathLikeOrReadStream;
72
- assets?: { file: PathLikeOrReadStream; name: string }[];
73
- header?: PathLikeOrReadStream;
74
- footer?: PathLikeOrReadStream;
75
- properties?: PageProperties;
76
- /**
77
- * @deprecated Starting from Gotenberg version 8.0.0, Chromium no longer provides support for pdfFormat.
78
- * @see {@link https://github.com/gotenberg/gotenberg/releases/tag/v8.0.0}
79
- */
80
- pdfFormat?: PdfFormat;
81
- pdfUA?: boolean;
82
- emulatedMediaType?: EmulatedMediaType;
83
- waitDelay?: string;
84
- waitForExpression?: string;
85
- /**
86
- * @deprecated Starting from Gotenberg version 8.0.0, Chromium no longer provides support for userAgent.
87
- * @see {@link https://github.com/gotenberg/gotenberg/releases/tag/v8.0.0}
88
- */
89
- userAgent?: string;
90
- extraHttpHeaders?: Record<string, string>;
91
- failOnHttpStatusCodes?: number[];
92
- failOnConsoleExceptions?: boolean;
93
- skipNetworkIdleEvent?: boolean;
94
- metadata?: Metadata;
95
- cookies?: Cookie[];
96
- }): Promise<Buffer> {
97
- const data = new FormData();
98
-
99
- await GotenbergUtils.addFile(data, html, 'index.html');
100
-
101
- if (assets?.length) {
102
- await Promise.all(
103
- assets.map(({ file, name }) =>
104
- GotenbergUtils.addFile(data, file, name)
105
- )
106
- );
107
- }
108
-
109
- await ConverterUtils.customize(data, {
110
- header,
111
- footer,
112
- properties,
113
- pdfFormat,
114
- pdfUA,
115
- emulatedMediaType,
116
- waitDelay,
117
- waitForExpression,
118
- userAgent,
119
- extraHttpHeaders,
120
- failOnHttpStatusCodes,
121
- failOnConsoleExceptions,
122
- skipNetworkIdleEvent,
123
- metadata,
124
- cookies
125
- });
126
-
127
- return GotenbergUtils.fetch(
128
- this.endpoint,
129
- data,
130
- Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
131
- Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
132
- );
133
- }
134
- }
@@ -1,129 +0,0 @@
1
- import FormData from 'form-data';
2
-
3
- import {
4
- GotenbergUtils,
5
- PdfFormat,
6
- PathLikeOrReadStream,
7
- Metadata
8
- } from '../../common';
9
- import { Cookie, PageProperties } from '../interfaces/converter.types';
10
- import { ConverterUtils } from '../utils/converter.utils';
11
- import { Converter } from './converter';
12
- import { ChromiumRoute, Chromiumly } from '../../main.config';
13
- import { EmulatedMediaType } from '../interfaces/common.types';
14
-
15
- /**
16
- * Class representing a Markdown converter that extends the base Converter class.
17
- * This class is used to convert HTML with markdown content to PDF using Gotenberg service.
18
- *
19
- * @extends Converter
20
- */
21
- export class MarkdownConverter extends Converter {
22
- /**
23
- * Creates an instance of MarkdownConverter.
24
- * Initializes the converter with the Markdown conversion route.
25
- */
26
- constructor() {
27
- super(ChromiumRoute.MARKDOWN);
28
- }
29
-
30
- /**
31
- * Converts HTML with markdown content to PDF.
32
- *
33
- * @param {Object} options - Conversion options.
34
- * @param {PathLikeOrReadStream} options.html - PathLike or ReadStream of the HTML content to be converted.
35
- * @param {PathLikeOrReadStream} options.markdown - PathLike or ReadStream of the Markdown content to be converted.
36
- * @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
37
- * @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
38
- * @param {PageProperties} [options.properties] - Page properties for the conversion.
39
- * @param {PdfFormat} [options.pdfFormat] - PDF format options.
40
- * @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output.
41
- * @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the conversion.
42
- * @param {string} [options.waitDelay] - Delay before the conversion process starts.
43
- * @param {string} [options.waitForExpression] - JavaScript expression to wait for before completing the conversion.
44
- * @param {string} [options.userAgent] - User agent string to use during the conversion.
45
- * @param {Record<string, string>} [options.extraHttpHeaders] - Additional HTTP headers for the conversion.
46
- * @param {number []} [options.failOnHttpStatusCodes] - Whether to fail on HTTP status code.
47
- * @param {boolean} [options.failOnConsoleExceptions] - Whether to fail on console exceptions during conversion.
48
- * @param {boolean} [options.skipNetworkIdleEvent] - Whether to skip network idle event.
49
- * @param {Metadata} options.metadata - Metadata to be written.
50
- * @param {Cookie[]} options.cookies - Cookies to be written.
51
- * @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a Buffer.
52
- */
53
- async convert({
54
- html,
55
- markdown,
56
- header,
57
- footer,
58
- properties,
59
- pdfFormat,
60
- pdfUA,
61
- emulatedMediaType,
62
- waitDelay,
63
- waitForExpression,
64
- userAgent,
65
- extraHttpHeaders,
66
- failOnHttpStatusCodes,
67
- failOnConsoleExceptions,
68
- skipNetworkIdleEvent,
69
- metadata,
70
- cookies
71
- }: {
72
- html: PathLikeOrReadStream;
73
- markdown: PathLikeOrReadStream;
74
- header?: PathLikeOrReadStream;
75
- footer?: PathLikeOrReadStream;
76
- properties?: PageProperties;
77
- /**
78
- * @deprecated Starting from Gotenberg version 8.0.0, Chromium no longer provides support for pdfFormat.
79
- * @see {@link https://github.com/gotenberg/gotenberg/releases/tag/v8.0.0}
80
- */
81
- pdfFormat?: PdfFormat;
82
- pdfUA?: boolean;
83
- emulatedMediaType?: EmulatedMediaType;
84
- waitDelay?: string;
85
- waitForExpression?: string;
86
- /**
87
- * @deprecated Starting from Gotenberg version 8.0.0, Chromium no longer provides support for userAgent.
88
- * @see {@link https://github.com/gotenberg/gotenberg/releases/tag/v8.0.0}
89
- */
90
- userAgent?: string;
91
- extraHttpHeaders?: Record<string, string>;
92
- failOnHttpStatusCodes?: number[];
93
- failOnConsoleExceptions?: boolean;
94
- skipNetworkIdleEvent?: boolean;
95
- metadata?: Metadata;
96
- cookies?: Cookie[];
97
- }): Promise<Buffer> {
98
- const data = new FormData();
99
-
100
- await GotenbergUtils.addFile(data, html, 'index.html');
101
-
102
- await GotenbergUtils.addFile(data, markdown, 'file.md');
103
-
104
- await ConverterUtils.customize(data, {
105
- header,
106
- footer,
107
- properties,
108
- pdfFormat,
109
- pdfUA,
110
- emulatedMediaType,
111
- waitDelay,
112
- waitForExpression,
113
- userAgent,
114
- extraHttpHeaders,
115
- failOnHttpStatusCodes,
116
- failOnConsoleExceptions,
117
- skipNetworkIdleEvent,
118
- metadata,
119
- cookies
120
- });
121
-
122
- return GotenbergUtils.fetch(
123
- this.endpoint,
124
- data,
125
- Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
126
- Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
127
- );
128
- }
129
- }
@@ -1,190 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream, promises } from 'fs';
3
-
4
- import fetch from 'node-fetch';
5
- import FormData from 'form-data';
6
-
7
- import { PdfFormat } from '../../../common';
8
- import { HtmlConverter } from '../html.converter';
9
-
10
- const { Response } = jest.requireActual('node-fetch');
11
- jest.mock('node-fetch', () => jest.fn());
12
-
13
- describe('HtmlConverter', () => {
14
- const converter = new HtmlConverter();
15
-
16
- describe('endpoint', () => {
17
- it('should route to Chromium HTML route', () => {
18
- expect(converter.endpoint).toEqual(
19
- 'http://localhost:3000/forms/chromium/convert/html'
20
- );
21
- });
22
- });
23
-
24
- describe('convert', () => {
25
- const mockPromisesAccess = jest.spyOn(promises, 'access');
26
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
27
- const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
28
-
29
- const assets = [
30
- { file: Buffer.from('asset1'), name: 'asset1' },
31
- { file: Buffer.from('asset2'), name: 'asset2' }
32
- ];
33
-
34
- beforeEach(() => {
35
- (createReadStream as jest.Mock) = jest.fn();
36
- });
37
-
38
- afterEach(() => {
39
- jest.resetAllMocks();
40
- });
41
-
42
- describe('when html parameter is passed', () => {
43
- it('should return a buffer', async () => {
44
- mockFetch.mockResolvedValue(new Response('content'));
45
- const buffer = await converter.convert({
46
- html: Buffer.from('data')
47
- });
48
- expect(buffer).toEqual(Buffer.from('content'));
49
- });
50
- });
51
-
52
- describe('when pdf format parameter is passed', () => {
53
- it('should return a buffer', async () => {
54
- mockFetch.mockResolvedValue(new Response('content'));
55
- const buffer = await converter.convert({
56
- html: Buffer.from('data'),
57
- pdfFormat: PdfFormat.A_1a
58
- });
59
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
60
- expect(buffer).toEqual(Buffer.from('content'));
61
- });
62
- });
63
-
64
- describe('when page properties parameter is passed', () => {
65
- it('should return a buffer', async () => {
66
- mockFetch.mockResolvedValue(new Response('content'));
67
- const buffer = await converter.convert({
68
- html: Buffer.from('data'),
69
- properties: { size: { width: 8.3, height: 11.7 } }
70
- });
71
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
72
- expect(buffer).toEqual(Buffer.from('content'));
73
- });
74
- });
75
-
76
- describe('when header parameter is passed', () => {
77
- it('should return a buffer', async () => {
78
- mockFetch.mockResolvedValue(new Response('content'));
79
- const buffer = await converter.convert({
80
- html: Buffer.from('data'),
81
- header: Buffer.from('header')
82
- });
83
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
84
- expect(buffer).toEqual(Buffer.from('content'));
85
- });
86
- });
87
-
88
- describe('when footer parameter is passed', () => {
89
- it('should return a buffer', async () => {
90
- mockFetch.mockResolvedValue(new Response('content'));
91
- const buffer = await converter.convert({
92
- html: Buffer.from('data'),
93
- footer: Buffer.from('footer')
94
- });
95
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
96
- expect(buffer).toEqual(Buffer.from('content'));
97
- });
98
- });
99
-
100
- describe('when assets parameter is passed', () => {
101
- it('should return a buffer', async () => {
102
- mockFetch.mockResolvedValue(new Response('content'));
103
- const buffer = await converter.convert({
104
- html: Buffer.from('data'),
105
- assets
106
- });
107
-
108
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
109
- expect(buffer).toEqual(Buffer.from('content'));
110
- });
111
- });
112
-
113
- describe('when emulatedMediaType parameter is passed', () => {
114
- it('should return a buffer', async () => {
115
- mockFetch.mockResolvedValue(new Response('content'));
116
- const buffer = await converter.convert({
117
- html: Buffer.from('data'),
118
- emulatedMediaType: 'screen'
119
- });
120
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
121
- expect(buffer).toEqual(Buffer.from('content'));
122
- });
123
- });
124
-
125
- describe('when failOnHttpStatusCodes parameter is passed', () => {
126
- it('should return a buffer', async () => {
127
- mockFetch.mockResolvedValue(new Response('content'));
128
- const buffer = await converter.convert({
129
- html: Buffer.from('data'),
130
- failOnHttpStatusCodes: [499, 599]
131
- });
132
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
133
- expect(buffer).toEqual(Buffer.from('content'));
134
- });
135
- });
136
-
137
- describe('when skipNetworkIdleEvent parameter is passed', () => {
138
- it('should return a buffer', async () => {
139
- mockFetch.mockResolvedValue(new Response('content'));
140
- const buffer = await converter.convert({
141
- html: Buffer.from('data'),
142
- skipNetworkIdleEvent: true
143
- });
144
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
145
- expect(buffer).toEqual(Buffer.from('content'));
146
- });
147
- });
148
-
149
- describe('when all parameters are passed', () => {
150
- it('should return a buffer', async () => {
151
- mockPromisesAccess.mockResolvedValue();
152
- mockFetch.mockResolvedValue(new Response('content'));
153
- const buffer = await converter.convert({
154
- html: Buffer.from('data'),
155
- assets,
156
- header: Buffer.from('header'),
157
- footer: Buffer.from('footer'),
158
- pdfFormat: PdfFormat.A_1a,
159
- emulatedMediaType: 'screen',
160
- properties: { size: { width: 8.3, height: 11.7 } }
161
- });
162
- expect(mockFormDataAppend).toHaveBeenCalledTimes(9);
163
- expect(buffer).toEqual(Buffer.from('content'));
164
- });
165
- });
166
-
167
- describe('when file does not exist', () => {
168
- it('should throw an error', async () => {
169
- const errorMessage =
170
- "ENOENT: no such file or directory, access 'path/to/index.html'";
171
- mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
172
- await expect(() =>
173
- converter.convert({ html: 'path/to/index.html' })
174
- ).rejects.toThrow(errorMessage);
175
- });
176
- });
177
-
178
- describe('when fetch request fails', () => {
179
- it('should throw an error', async () => {
180
- const errorMessage =
181
- 'FetchError: request to http://localhost:3000/forms/chromium/convert/html failed';
182
- mockPromisesAccess.mockResolvedValue();
183
- mockFetch.mockRejectedValue(new Error(errorMessage));
184
- await expect(() =>
185
- converter.convert({ html: 'path/to/index.html' })
186
- ).rejects.toThrow(errorMessage);
187
- });
188
- });
189
- });
190
- });
@@ -1,187 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream, promises } from 'fs';
3
-
4
- import FormData from 'form-data';
5
- import fetch from 'node-fetch';
6
-
7
- import { PdfFormat } from '../../../common';
8
- import { MarkdownConverter } from '../markdown.converter';
9
-
10
- const { Response } = jest.requireActual('node-fetch');
11
- jest.mock('node-fetch', () => jest.fn());
12
-
13
- describe('MarkdownConverter', () => {
14
- const converter = new MarkdownConverter();
15
-
16
- describe('endpoint', () => {
17
- it('should route to Chromium Markdown route', () => {
18
- expect(converter.endpoint).toEqual(
19
- 'http://localhost:3000/forms/chromium/convert/markdown'
20
- );
21
- });
22
- });
23
-
24
- describe('convert', () => {
25
- const mockPromisesAccess = jest.spyOn(promises, 'access');
26
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
27
- const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
28
-
29
- beforeEach(() => {
30
- (createReadStream as jest.Mock) = jest.fn();
31
- });
32
-
33
- afterEach(() => {
34
- jest.resetAllMocks();
35
- });
36
-
37
- describe('when html and markdown parameters are passed', () => {
38
- it('should return a buffer', async () => {
39
- mockFetch.mockResolvedValue(new Response('content'));
40
- const buffer = await converter.convert({
41
- html: Buffer.from('data'),
42
- markdown: Buffer.from('markdown')
43
- });
44
- expect(buffer).toEqual(Buffer.from('content'));
45
- });
46
- });
47
-
48
- describe('when pdf format parameter is passed', () => {
49
- it('should return a buffer', async () => {
50
- mockFetch.mockResolvedValue(new Response('content'));
51
- const buffer = await converter.convert({
52
- html: Buffer.from('data'),
53
- markdown: Buffer.from('markdown'),
54
- pdfFormat: PdfFormat.A_2b
55
- });
56
- expect(mockFormDataAppend).toHaveBeenCalledTimes(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
- mockFetch.mockResolvedValue(new Response('content'));
64
- const buffer = await converter.convert({
65
- html: Buffer.from('data'),
66
- markdown: Buffer.from('markdown'),
67
- properties: { size: { width: 8.3, height: 11.7 } }
68
- });
69
- expect(mockFormDataAppend).toHaveBeenCalledTimes(4);
70
- expect(buffer).toEqual(Buffer.from('content'));
71
- });
72
- });
73
-
74
- describe('when header parameter is passed', () => {
75
- it('should return a buffer', async () => {
76
- mockFetch.mockResolvedValue(new Response('content'));
77
- const buffer = await converter.convert({
78
- html: Buffer.from('data'),
79
- markdown: Buffer.from('markdown'),
80
- header: Buffer.from('header')
81
- });
82
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
83
- expect(buffer).toEqual(Buffer.from('content'));
84
- });
85
- });
86
-
87
- describe('when footer parameter is passed', () => {
88
- it('should return a buffer', async () => {
89
- mockFetch.mockResolvedValue(new Response('content'));
90
- const buffer = await converter.convert({
91
- html: Buffer.from('data'),
92
- markdown: Buffer.from('markdown'),
93
- footer: Buffer.from('footer')
94
- });
95
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
96
- expect(buffer).toEqual(Buffer.from('content'));
97
- });
98
- });
99
-
100
- describe('when emulatedMediaType parameter is passed', () => {
101
- it('should return a buffer', async () => {
102
- mockFetch.mockResolvedValue(new Response('content'));
103
- const buffer = await converter.convert({
104
- html: Buffer.from('data'),
105
- markdown: Buffer.from('markdown'),
106
- emulatedMediaType: 'screen'
107
- });
108
-
109
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
110
- expect(buffer).toEqual(Buffer.from('content'));
111
- });
112
- });
113
-
114
- describe('when failOnHttpStatusCodes parameter is passed', () => {
115
- it('should return a buffer', async () => {
116
- mockFetch.mockResolvedValue(new Response('content'));
117
- const buffer = await converter.convert({
118
- html: Buffer.from('data'),
119
- markdown: Buffer.from('markdown'),
120
- failOnHttpStatusCodes: [499, 599]
121
- });
122
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
123
- expect(buffer).toEqual(Buffer.from('content'));
124
- });
125
- });
126
-
127
- describe('when skipNetworkIdleEvent parameter is passed', () => {
128
- it('should return a buffer', async () => {
129
- mockFetch.mockResolvedValue(new Response('content'));
130
- const buffer = await converter.convert({
131
- html: Buffer.from('data'),
132
- markdown: Buffer.from('markdown'),
133
- skipNetworkIdleEvent: true
134
- });
135
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
136
- expect(buffer).toEqual(Buffer.from('content'));
137
- });
138
- });
139
-
140
- describe('when all parameters are passed', () => {
141
- it('should return a buffer', async () => {
142
- mockFetch.mockResolvedValue(new Response('content'));
143
- const buffer = await converter.convert({
144
- html: Buffer.from('data'),
145
- markdown: Buffer.from('markdown'),
146
- header: Buffer.from('header'),
147
- footer: Buffer.from('footer'),
148
- pdfFormat: PdfFormat.A_1a,
149
- emulatedMediaType: 'screen',
150
- properties: { size: { width: 8.3, height: 11.7 } }
151
- });
152
- expect(mockFormDataAppend).toHaveBeenCalledTimes(8);
153
- expect(buffer).toEqual(Buffer.from('content'));
154
- });
155
- });
156
-
157
- describe('when file does not exist', () => {
158
- it('should throw an error', async () => {
159
- const errorMessage =
160
- "ENOENT: no such file or directory, access 'path/to/index.html'";
161
- mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
162
-
163
- await expect(() =>
164
- converter.convert({
165
- html: 'path/to/index.html',
166
- markdown: 'path/to/file.md'
167
- })
168
- ).rejects.toThrow(errorMessage);
169
- });
170
- });
171
-
172
- describe('when fetch request fails', () => {
173
- it('should throw an error', async () => {
174
- const errorMessage =
175
- 'FetchError: request to http://localhost:3000/forms/chromium/convert/html failed';
176
- mockPromisesAccess.mockResolvedValue();
177
- mockFetch.mockRejectedValue(new Error(errorMessage));
178
- await expect(() =>
179
- converter.convert({
180
- html: 'path/to/index.html',
181
- markdown: 'path/to/file.md'
182
- })
183
- ).rejects.toThrow(errorMessage);
184
- });
185
- });
186
- });
187
- });