-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
138 lines (117 loc) · 3.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// @ts-check
import { fastify } from "fastify";
import { chromium } from "playwright-core";
import { readFileSync } from "node:fs";
// we'd love to do this, but eslint doesn't like import assertions yet
// import PdfRequestBodySchema from "./pdf_request_body.json" assert { type: "json" };
const PdfRequestBodySchema = JSON.parse(
readFileSync("./pdf_request_body.json", { encoding: "utf-8" }),
);
/**
* @typedef {Parameters<import("playwright-core").Page["pdf"]>[0]} PdfOptions
* @typedef {Parameters<import("playwright-core").Page["goto"]>[1]} GotoOptions
* @typedef {import("playwright-core").BrowserContextOptions} BrowserContextOptions
*/
/**
* @typedef {Object} PdfRequestBody
* @property {string} url
* @property {PdfOptions} pdfOptions
* @property {BrowserContextOptions} browserContextOptions
* @property {GotoOptions} gotoOptions
*/
export const defaultBrowserContextOptions = /** @type {const} */ ({
ignoreHTTPSErrors: true,
});
export const defaultGotoOptions = /** @type {const} */ ({
timeout: 60000,
waitUntil: "networkidle",
});
/**
*
* @param {string} url
* @param {PdfOptions} pdfOptions
* @param {BrowserContextOptions} browserContextOptions
* @param {GotoOptions} gotoOptions
* @returns Buffer
*/
const createPDF = async (
url,
pdfOptions = {},
browserContextOptions = {},
gotoOptions = {},
) => {
const browser = await chromium.launch();
try {
const context = await browser.newContext(browserContextOptions);
const page = await context.newPage();
const response = await page.goto(url, gotoOptions);
if (response?.ok()) {
return await page.pdf(pdfOptions);
}
return await Promise.reject({
error: true,
message: await response?.text(),
status: response?.status(),
statusText: response?.statusText(),
});
} finally {
await browser.close();
}
};
/**
*
* @param {import("fastify").FastifyServerOptions} opts
*/
function build(opts = {}) {
const fastifyOptions = {
...opts,
ajv: {
customOptions: {
// we want validation errors if someone passes an unknown option
removeAdditional: false,
// Don't adhere to ajvs strict types, as our schema is auto generated anyway
allowUnionTypes: true,
},
},
};
const app = fastify(fastifyOptions);
app.post(
"/pdf",
{ schema: { body: PdfRequestBodySchema }, attachValidation: true },
/**
*
* @param {import('fastify').FastifyRequest<{Body: PdfRequestBody}>} request
* @param {import('fastify').FastifyReply} response
*/
async (request, response) => {
if (request.validationError) {
response.code(400);
return {
error: true,
message: request.validationError.message,
};
}
const { url, pdfOptions, browserContextOptions, gotoOptions } =
request.body;
const pdfOpts = { ...pdfOptions };
const contextOpts = {
...defaultBrowserContextOptions,
...browserContextOptions,
};
const gotoOpts = {
...defaultGotoOptions,
...gotoOptions,
};
try {
return await createPDF(url, pdfOpts, contextOpts, gotoOpts);
} catch (error) {
// @ts-ignore
response.code(error.status ?? 500);
return error;
}
},
);
app.get("/health", async (_, response) => response.code(200).send());
return app;
}
export default build;