chromiumly 2.5.1 → 2.7.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.
- package/README.md +55 -35
- package/dist/chromium/converters/converter.d.ts +1 -1
- package/dist/chromium/converters/converter.js +1 -1
- package/dist/chromium/converters/converter.js.map +1 -1
- package/dist/chromium/converters/html.converter.d.ts +13 -4
- package/dist/chromium/converters/html.converter.js +9 -2
- package/dist/chromium/converters/html.converter.js.map +1 -1
- package/dist/chromium/converters/markdown.converter.d.ts +9 -4
- package/dist/chromium/converters/markdown.converter.js +7 -3
- package/dist/chromium/converters/markdown.converter.js.map +1 -1
- package/dist/chromium/converters/url.converter.d.ts +9 -4
- package/dist/chromium/converters/url.converter.js +6 -2
- package/dist/chromium/converters/url.converter.js.map +1 -1
- package/dist/chromium/index.d.ts +3 -3
- package/dist/chromium/index.js.map +1 -1
- package/dist/chromium/interfaces/common.types.d.ts +13 -0
- package/dist/chromium/interfaces/common.types.js +3 -0
- package/dist/chromium/interfaces/common.types.js.map +1 -0
- package/dist/chromium/interfaces/converter.types.d.ts +4 -10
- package/dist/chromium/utils/converter.utils.d.ts +2 -12
- package/dist/chromium/utils/converter.utils.js +36 -50
- package/dist/chromium/utils/converter.utils.js.map +1 -1
- package/dist/common/gotenberg.utils.d.ts +11 -1
- package/dist/common/gotenberg.utils.js +25 -2
- package/dist/common/gotenberg.utils.js.map +1 -1
- package/dist/common/index.d.ts +3 -3
- package/dist/common/index.js.map +1 -1
- package/dist/common/types.d.ts +1 -1
- package/dist/gotenberg.js +4 -3
- package/dist/gotenberg.js.map +1 -1
- package/dist/libre-office/index.d.ts +2 -2
- package/dist/libre-office/index.js.map +1 -1
- package/dist/libre-office/utils/constants.js +80 -76
- package/dist/libre-office/utils/constants.js.map +1 -1
- package/dist/libre-office/utils/libre-office.utils.d.ts +3 -3
- package/dist/libre-office/utils/libre-office.utils.js +5 -5
- package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
- package/dist/main.config.d.ts +1 -1
- package/dist/main.config.js +6 -6
- package/dist/main.config.js.map +1 -1
- package/dist/main.d.ts +3 -3
- package/dist/main.js.map +1 -1
- package/dist/pdf-engines/index.d.ts +1 -1
- package/dist/pdf-engines/index.js.map +1 -1
- package/dist/pdf-engines/pdf.engine.d.ts +3 -3
- package/dist/pdf-engines/pdf.engine.js +5 -5
- package/dist/pdf-engines/pdf.engine.js.map +1 -1
- package/dist/pdf-engines/utils/engine.utils.d.ts +2 -2
- package/dist/pdf-engines/utils/engine.utils.js +3 -3
- package/dist/pdf-engines/utils/engine.utils.js.map +1 -1
- package/package.json +25 -17
- package/src/.prettierrc.yml +4 -0
- package/src/chromium/converters/converter.ts +2 -2
- package/src/chromium/converters/html.converter.ts +40 -24
- package/src/chromium/converters/markdown.converter.ts +32 -26
- package/src/chromium/converters/tests/html.converter.test.ts +111 -68
- package/src/chromium/converters/tests/markdown.converter.test.ts +102 -77
- package/src/chromium/converters/tests/url.converter.test.ts +90 -66
- package/src/chromium/converters/url.converter.ts +31 -25
- package/src/chromium/index.ts +3 -3
- package/src/chromium/interfaces/common.types.ts +15 -0
- package/src/chromium/interfaces/converter.types.ts +4 -12
- package/src/chromium/utils/converter.utils.ts +67 -60
- package/src/chromium/utils/tests/converter.utils.test.ts +237 -167
- package/src/common/constants.ts +3 -3
- package/src/common/gotenberg.utils.ts +39 -8
- package/src/common/index.ts +3 -3
- package/src/common/tests/gotenberg.utils.test.ts +38 -31
- package/src/common/types.ts +2 -2
- package/src/gotenberg.ts +9 -9
- package/src/libre-office/index.ts +2 -2
- package/src/libre-office/utils/constants.ts +80 -76
- package/src/libre-office/utils/libre-office.utils.ts +21 -18
- package/src/libre-office/utils/tests/libre-office.utils.test.ts +58 -49
- package/src/main.config.ts +13 -13
- package/src/main.ts +3 -3
- package/src/pdf-engines/index.ts +1 -1
- package/src/pdf-engines/pdf.engine.ts +23 -19
- package/src/pdf-engines/tests/pdf.engine.test.ts +52 -49
- package/src/pdf-engines/utils/engine.utils.ts +12 -9
- package/src/pdf-engines/utils/tests/engine.utils.test.ts +23 -23
package/src/main.config.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {Gotenberg} from
|
|
1
|
+
import { Gotenberg } from './gotenberg';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Enum representing the available Chromium routes for conversion.
|
|
5
5
|
* @enum {string}
|
|
6
6
|
*/
|
|
7
7
|
export enum ChromiumRoute {
|
|
8
|
-
URL =
|
|
9
|
-
HTML =
|
|
10
|
-
MARKDOWN =
|
|
8
|
+
URL = 'url',
|
|
9
|
+
HTML = 'html',
|
|
10
|
+
MARKDOWN = 'markdown'
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -15,7 +15,7 @@ export enum ChromiumRoute {
|
|
|
15
15
|
* @enum {string}
|
|
16
16
|
*/
|
|
17
17
|
enum PdfEngineRoute {
|
|
18
|
-
MERGE =
|
|
18
|
+
MERGE = 'merge'
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -23,7 +23,7 @@ enum PdfEngineRoute {
|
|
|
23
23
|
* @enum {string}
|
|
24
24
|
*/
|
|
25
25
|
enum LibreOfficeRoute {
|
|
26
|
-
CONVERT =
|
|
26
|
+
CONVERT = 'convert'
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
@@ -40,19 +40,19 @@ export class Chromiumly {
|
|
|
40
40
|
* The path for Chromium-related conversions.
|
|
41
41
|
* @type {string}
|
|
42
42
|
*/
|
|
43
|
-
public static readonly
|
|
43
|
+
public static readonly CHROMIUM_CONVERT_PATH = 'forms/chromium/convert';
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* The path for PDF engine-related operations.
|
|
47
47
|
* @type {string}
|
|
48
48
|
*/
|
|
49
|
-
public static readonly PDF_ENGINES_PATH =
|
|
49
|
+
public static readonly PDF_ENGINES_PATH = 'forms/pdfengines';
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* The path for LibreOffice-related conversions.
|
|
53
53
|
* @type {string}
|
|
54
54
|
*/
|
|
55
|
-
public static readonly LIBRE_OFFICE_PATH =
|
|
55
|
+
public static readonly LIBRE_OFFICE_PATH = 'forms/libreoffice';
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Routes for Chromium conversions.
|
|
@@ -61,7 +61,7 @@ export class Chromiumly {
|
|
|
61
61
|
public static readonly CHROMIUM_ROUTES = {
|
|
62
62
|
url: ChromiumRoute.URL,
|
|
63
63
|
html: ChromiumRoute.HTML,
|
|
64
|
-
markdown: ChromiumRoute.MARKDOWN
|
|
64
|
+
markdown: ChromiumRoute.MARKDOWN
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
/**
|
|
@@ -69,7 +69,7 @@ export class Chromiumly {
|
|
|
69
69
|
* @type {Object}
|
|
70
70
|
*/
|
|
71
71
|
public static readonly PDF_ENGINE_ROUTES = {
|
|
72
|
-
merge: PdfEngineRoute.MERGE
|
|
72
|
+
merge: PdfEngineRoute.MERGE
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -77,6 +77,6 @@ export class Chromiumly {
|
|
|
77
77
|
* @type {Object}
|
|
78
78
|
*/
|
|
79
79
|
public static readonly LIBRE_OFFICE_ROUTES = {
|
|
80
|
-
convert: LibreOfficeRoute.CONVERT
|
|
80
|
+
convert: LibreOfficeRoute.CONVERT
|
|
81
81
|
};
|
|
82
|
-
}
|
|
82
|
+
}
|
package/src/main.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {PdfFormat} from
|
|
2
|
-
export {HtmlConverter, MarkdownConverter, UrlConverter} from
|
|
3
|
-
export {PDFEngine} from
|
|
1
|
+
export { PdfFormat } from './common/constants';
|
|
2
|
+
export { HtmlConverter, MarkdownConverter, UrlConverter } from './chromium';
|
|
3
|
+
export { PDFEngine } from './pdf-engines';
|
package/src/pdf-engines/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {PDFEngine} from
|
|
1
|
+
export { PDFEngine } from './pdf.engine';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {promises} from
|
|
2
|
-
import path from
|
|
1
|
+
import { promises } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
3
|
|
|
4
|
-
import FormData from
|
|
4
|
+
import FormData from 'form-data';
|
|
5
5
|
|
|
6
|
-
import {Chromiumly} from
|
|
7
|
-
import {GotenbergUtils, PathLikeOrReadStream, PdfFormat} from
|
|
8
|
-
import {LibreOfficeUtils, PageProperties} from
|
|
9
|
-
import {PDFEngineUtils} from
|
|
6
|
+
import { Chromiumly } from '../main.config';
|
|
7
|
+
import { GotenbergUtils, PathLikeOrReadStream, PdfFormat } from '../common';
|
|
8
|
+
import { LibreOfficeUtils, PageProperties } from '../libre-office';
|
|
9
|
+
import { PDFEngineUtils } from './utils/engine.utils';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Class representing a PDF engine for various operations such as merging and conversion.
|
|
@@ -19,7 +19,11 @@ export class PDFEngine {
|
|
|
19
19
|
* @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the PDF files to be merged.
|
|
20
20
|
* @returns {Promise<Buffer>} A Promise resolving to the merged PDF content as a Buffer.
|
|
21
21
|
*/
|
|
22
|
-
public static async merge({
|
|
22
|
+
public static async merge({
|
|
23
|
+
files
|
|
24
|
+
}: {
|
|
25
|
+
files: PathLikeOrReadStream[];
|
|
26
|
+
}): Promise<Buffer> {
|
|
23
27
|
const data = new FormData();
|
|
24
28
|
await PDFEngineUtils.addFiles(files, data);
|
|
25
29
|
const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.merge}`;
|
|
@@ -38,12 +42,12 @@ export class PDFEngine {
|
|
|
38
42
|
* @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a Buffer.
|
|
39
43
|
*/
|
|
40
44
|
public static async convert({
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
files,
|
|
46
|
+
properties,
|
|
47
|
+
pdfFormat,
|
|
48
|
+
pdfUA,
|
|
49
|
+
merge
|
|
50
|
+
}: {
|
|
47
51
|
files: PathLikeOrReadStream[];
|
|
48
52
|
properties?: PageProperties;
|
|
49
53
|
pdfFormat?: PdfFormat;
|
|
@@ -53,15 +57,15 @@ export class PDFEngine {
|
|
|
53
57
|
const data = new FormData();
|
|
54
58
|
|
|
55
59
|
if (pdfFormat) {
|
|
56
|
-
data.append(
|
|
60
|
+
data.append('pdfa', pdfFormat);
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
if (pdfUA) {
|
|
60
|
-
data.append(
|
|
64
|
+
data.append('pdfUA', String(pdfUA));
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
if (merge) {
|
|
64
|
-
data.append(
|
|
68
|
+
data.append('merge', String(merge));
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
if (properties) {
|
|
@@ -86,8 +90,8 @@ export class PDFEngine {
|
|
|
86
90
|
filename: string,
|
|
87
91
|
buffer: Buffer
|
|
88
92
|
): Promise<void> {
|
|
89
|
-
const __generated__ = path.resolve(process.cwd(),
|
|
90
|
-
await promises.mkdir(path.resolve(__generated__), {recursive: true});
|
|
93
|
+
const __generated__ = path.resolve(process.cwd(), '__generated__');
|
|
94
|
+
await promises.mkdir(path.resolve(__generated__), { recursive: true });
|
|
91
95
|
await promises.writeFile(path.resolve(__generated__, filename), buffer);
|
|
92
96
|
}
|
|
93
97
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
-
import {createReadStream, promises} from
|
|
3
|
-
import FormData from
|
|
4
|
-
import fs from
|
|
5
|
-
import fetch from
|
|
6
|
-
import path from
|
|
2
|
+
import { createReadStream, promises } from 'fs';
|
|
3
|
+
import FormData from 'form-data';
|
|
4
|
+
import fs from 'fs/promises';
|
|
5
|
+
import fetch from 'node-fetch';
|
|
6
|
+
import path from 'path';
|
|
7
7
|
|
|
8
|
-
import {PdfFormat} from
|
|
9
|
-
import {PDFEngine} from
|
|
8
|
+
import { PdfFormat } from '../../common';
|
|
9
|
+
import { PDFEngine } from '../pdf.engine';
|
|
10
10
|
|
|
11
|
-
const {Response} = jest.requireActual(
|
|
12
|
-
jest.mock(
|
|
11
|
+
const { Response } = jest.requireActual('node-fetch');
|
|
12
|
+
jest.mock('node-fetch', () => jest.fn());
|
|
13
13
|
|
|
14
|
-
describe(
|
|
15
|
-
const mockPromisesAccess = jest.spyOn(promises,
|
|
14
|
+
describe('PDFEngine', () => {
|
|
15
|
+
const mockPromisesAccess = jest.spyOn(promises, 'access');
|
|
16
16
|
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
|
|
17
|
-
const mockFormDataAppend = jest.spyOn(FormData.prototype,
|
|
17
|
+
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
|
|
18
18
|
|
|
19
19
|
beforeEach(() => {
|
|
20
20
|
(createReadStream as jest.Mock) = jest
|
|
@@ -26,72 +26,75 @@ describe("PDFEngine", () => {
|
|
|
26
26
|
jest.resetAllMocks();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
describe(
|
|
30
|
-
describe(
|
|
31
|
-
it(
|
|
29
|
+
describe('convert', () => {
|
|
30
|
+
describe('when no properties are passed', () => {
|
|
31
|
+
it('should return a buffer', async () => {
|
|
32
32
|
mockPromisesAccess.mockResolvedValue();
|
|
33
|
-
mockFetch.mockResolvedValue(new Response(
|
|
33
|
+
mockFetch.mockResolvedValue(new Response('content'));
|
|
34
34
|
const buffer = await PDFEngine.convert({
|
|
35
|
-
files: [
|
|
35
|
+
files: ['path/to/file.docx', 'path/to/file.bib']
|
|
36
36
|
});
|
|
37
|
-
expect(buffer).toEqual(Buffer.from(
|
|
37
|
+
expect(buffer).toEqual(Buffer.from('content'));
|
|
38
38
|
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
39
39
|
});
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
describe(
|
|
43
|
-
it(
|
|
42
|
+
describe('when properties are passed', () => {
|
|
43
|
+
it('should return a buffer', async () => {
|
|
44
44
|
mockPromisesAccess.mockResolvedValue();
|
|
45
|
-
mockFetch.mockResolvedValue(new Response(
|
|
45
|
+
mockFetch.mockResolvedValue(new Response('content'));
|
|
46
46
|
const buffer = await PDFEngine.convert({
|
|
47
|
-
files: [
|
|
48
|
-
properties: {landscape: true},
|
|
47
|
+
files: ['path/to/file.docx', 'path/to/file.bib'],
|
|
48
|
+
properties: { landscape: true },
|
|
49
49
|
pdfFormat: PdfFormat.A_1a,
|
|
50
50
|
pdfUA: true,
|
|
51
|
-
merge: true
|
|
51
|
+
merge: true
|
|
52
52
|
});
|
|
53
|
-
expect(buffer).toEqual(Buffer.from(
|
|
53
|
+
expect(buffer).toEqual(Buffer.from('content'));
|
|
54
54
|
expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
|
|
55
55
|
});
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
describe(
|
|
60
|
-
it(
|
|
59
|
+
describe('merge', () => {
|
|
60
|
+
it('should return a buffer', async () => {
|
|
61
61
|
mockPromisesAccess.mockResolvedValue();
|
|
62
|
-
mockFetch.mockResolvedValue(new Response(
|
|
62
|
+
mockFetch.mockResolvedValue(new Response('content'));
|
|
63
63
|
const buffer = await PDFEngine.merge({
|
|
64
|
-
files: [
|
|
64
|
+
files: ['path/to/file.pdf', 'path/to/another-file.pdf']
|
|
65
65
|
});
|
|
66
|
-
expect(buffer).toEqual(Buffer.from(
|
|
66
|
+
expect(buffer).toEqual(Buffer.from('content'));
|
|
67
67
|
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
68
68
|
});
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
-
describe(
|
|
72
|
-
const mockFilename =
|
|
73
|
-
const mockBuffer = Buffer.from(
|
|
71
|
+
describe('generate', () => {
|
|
72
|
+
const mockFilename = 'test.pdf';
|
|
73
|
+
const mockBuffer = Buffer.from('mock pdf content');
|
|
74
74
|
|
|
75
75
|
afterAll(() => {
|
|
76
76
|
jest.restoreAllMocks();
|
|
77
77
|
});
|
|
78
78
|
|
|
79
|
-
it(
|
|
80
|
-
const mockGeneratedDir = path.join(process.cwd(),
|
|
81
|
-
const mockGeneratedFilePath = path.join(
|
|
79
|
+
it('should generate a PDF file', async () => {
|
|
80
|
+
const mockGeneratedDir = path.join(process.cwd(), '__generated__');
|
|
81
|
+
const mockGeneratedFilePath = path.join(
|
|
82
|
+
mockGeneratedDir,
|
|
83
|
+
mockFilename
|
|
84
|
+
);
|
|
82
85
|
|
|
83
86
|
const mockPromisesMkDir = jest
|
|
84
|
-
.spyOn(fs,
|
|
87
|
+
.spyOn(fs, 'mkdir')
|
|
85
88
|
.mockResolvedValueOnce(mockGeneratedDir);
|
|
86
89
|
|
|
87
90
|
const mockPromisesWriteFile = jest
|
|
88
|
-
.spyOn(fs,
|
|
91
|
+
.spyOn(fs, 'writeFile')
|
|
89
92
|
.mockResolvedValueOnce();
|
|
90
93
|
|
|
91
94
|
await PDFEngine.generate(mockFilename, mockBuffer);
|
|
92
95
|
|
|
93
96
|
expect(mockPromisesMkDir).toHaveBeenCalledWith(mockGeneratedDir, {
|
|
94
|
-
recursive: true
|
|
97
|
+
recursive: true
|
|
95
98
|
});
|
|
96
99
|
|
|
97
100
|
expect(mockPromisesWriteFile).toHaveBeenCalledWith(
|
|
@@ -100,24 +103,24 @@ describe("PDFEngine", () => {
|
|
|
100
103
|
);
|
|
101
104
|
});
|
|
102
105
|
|
|
103
|
-
it(
|
|
104
|
-
jest
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
it('should handle errors during file generation', async () => {
|
|
107
|
+
jest.spyOn(fs, 'mkdir').mockRejectedValueOnce(
|
|
108
|
+
new Error('Cannot create directory')
|
|
109
|
+
);
|
|
107
110
|
|
|
108
111
|
await expect(
|
|
109
112
|
PDFEngine.generate(mockFilename, mockBuffer)
|
|
110
|
-
).rejects.toThrow(
|
|
113
|
+
).rejects.toThrow('Cannot create directory');
|
|
111
114
|
});
|
|
112
115
|
|
|
113
|
-
it(
|
|
114
|
-
jest
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
it('should handle errors during file writing', async () => {
|
|
117
|
+
jest.spyOn(fs, 'writeFile').mockRejectedValueOnce(
|
|
118
|
+
new Error('Failed to write to file')
|
|
119
|
+
);
|
|
117
120
|
|
|
118
121
|
await expect(
|
|
119
122
|
PDFEngine.generate(mockFilename, mockBuffer)
|
|
120
|
-
).rejects.toThrow(
|
|
123
|
+
).rejects.toThrow('Failed to write to file');
|
|
121
124
|
});
|
|
122
125
|
});
|
|
123
126
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {constants, createReadStream, promises, ReadStream} from
|
|
2
|
-
import path from
|
|
1
|
+
import { constants, createReadStream, promises, ReadStream } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
3
|
|
|
4
|
-
import FormData from
|
|
5
|
-
import {PathLikeOrReadStream} from
|
|
4
|
+
import FormData from 'form-data';
|
|
5
|
+
import { PathLikeOrReadStream } from '../../common';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Utility class for handling common tasks related to PDF engine operations.
|
|
@@ -15,18 +15,21 @@ export class PDFEngineUtils {
|
|
|
15
15
|
* @param {FormData} data - The FormData object to which PDF files will be added.
|
|
16
16
|
* @throws {Error} Throws an error if the file extension is not supported.
|
|
17
17
|
*/
|
|
18
|
-
public static async addFiles(
|
|
18
|
+
public static async addFiles(
|
|
19
|
+
files: PathLikeOrReadStream[],
|
|
20
|
+
data: FormData
|
|
21
|
+
) {
|
|
19
22
|
for (const [key, file] of files.entries()) {
|
|
20
|
-
const filename = `file${key}.pdf
|
|
23
|
+
const filename = `file${key}.pdf`;
|
|
21
24
|
if (Buffer.isBuffer(file)) {
|
|
22
|
-
data.append(
|
|
25
|
+
data.append('files', file, filename);
|
|
23
26
|
} else if (file instanceof ReadStream) {
|
|
24
|
-
data.append(
|
|
27
|
+
data.append('files', file, filename);
|
|
25
28
|
} else {
|
|
26
29
|
await promises.access(file, constants.R_OK);
|
|
27
30
|
const filename = path.basename(file.toString());
|
|
28
31
|
const extension = path.extname(filename);
|
|
29
|
-
if (extension ===
|
|
32
|
+
if (extension === '.pdf') {
|
|
30
33
|
data.append(filename, createReadStream(file));
|
|
31
34
|
} else {
|
|
32
35
|
throw new Error(`${extension} is not supported`);
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
-
import {createReadStream, promises} from
|
|
3
|
-
import FormData from
|
|
2
|
+
import { createReadStream, promises } from 'fs';
|
|
3
|
+
import FormData from 'form-data';
|
|
4
4
|
|
|
5
|
-
import {PDFEngineUtils} from
|
|
5
|
+
import { PDFEngineUtils } from '../engine.utils';
|
|
6
6
|
|
|
7
|
-
describe(
|
|
8
|
-
const mockPromisesAccess = jest.spyOn(promises,
|
|
9
|
-
const mockFormDataAppend = jest.spyOn(FormData.prototype,
|
|
7
|
+
describe('PDFEngineUtils', () => {
|
|
8
|
+
const mockPromisesAccess = jest.spyOn(promises, 'access');
|
|
9
|
+
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
|
|
10
10
|
const data = new FormData();
|
|
11
11
|
|
|
12
12
|
beforeEach(() => {
|
|
@@ -19,50 +19,50 @@ describe("PDFEngineUtils", () => {
|
|
|
19
19
|
jest.resetAllMocks();
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
describe(
|
|
23
|
-
describe(
|
|
24
|
-
describe(
|
|
25
|
-
it(
|
|
22
|
+
describe('addFiles', () => {
|
|
23
|
+
describe('when files exist', () => {
|
|
24
|
+
describe('when files parameter contains paths', () => {
|
|
25
|
+
it('should append each file to data', async () => {
|
|
26
26
|
mockPromisesAccess.mockResolvedValue();
|
|
27
27
|
await PDFEngineUtils.addFiles(
|
|
28
|
-
[
|
|
28
|
+
['path/to/file.pdf', 'path/to/another-file.pdf'],
|
|
29
29
|
data
|
|
30
30
|
);
|
|
31
31
|
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
32
32
|
});
|
|
33
|
-
})
|
|
34
|
-
describe(
|
|
35
|
-
it(
|
|
33
|
+
});
|
|
34
|
+
describe('when files parameter contains a buffer', () => {
|
|
35
|
+
it('should append each file to data', async () => {
|
|
36
36
|
mockPromisesAccess.mockResolvedValue();
|
|
37
37
|
await PDFEngineUtils.addFiles(
|
|
38
|
-
[Buffer.from(
|
|
38
|
+
[Buffer.from('data'), 'path/to/another-file.pdf'],
|
|
39
39
|
data
|
|
40
40
|
);
|
|
41
41
|
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
42
42
|
});
|
|
43
|
-
})
|
|
43
|
+
});
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
describe(
|
|
47
|
-
it(
|
|
46
|
+
describe('when one of the files is not PDF', () => {
|
|
47
|
+
it('should throw an error', async () => {
|
|
48
48
|
mockPromisesAccess.mockResolvedValue();
|
|
49
49
|
await expect(() =>
|
|
50
50
|
PDFEngineUtils.addFiles(
|
|
51
|
-
[
|
|
51
|
+
['path/to/file.docx', 'path/to/file.pdf'],
|
|
52
52
|
data
|
|
53
53
|
)
|
|
54
|
-
).rejects.toThrow(
|
|
54
|
+
).rejects.toThrow('.docx is not supported');
|
|
55
55
|
});
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
-
describe(
|
|
59
|
-
it(
|
|
58
|
+
describe('when one of the files does not exist', () => {
|
|
59
|
+
it('should throw an error', async () => {
|
|
60
60
|
const errorMessage =
|
|
61
61
|
"ENOENT: no such file or directory, access 'path/to/index.html'";
|
|
62
62
|
mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
|
|
63
63
|
await expect(() =>
|
|
64
64
|
PDFEngineUtils.addFiles(
|
|
65
|
-
[
|
|
65
|
+
['path/to/file.pdf', 'path/to/another-file.pdf'],
|
|
66
66
|
data
|
|
67
67
|
)
|
|
68
68
|
).rejects.toThrow(errorMessage);
|