This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 810
/
renderer.ts
338 lines (293 loc) · 10.1 KB
/
renderer.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import puppeteer, { ScreenshotOptions } from 'puppeteer';
import url from 'url';
import { dirname } from 'path';
import { Config } from './config';
type SerializedResponse = {
status: number;
customHeaders: Map<string, string>;
content: string;
};
type ViewportDimensions = {
width: number;
height: number;
};
const MOBILE_USERAGENT =
'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Mobile Safari/537.36';
/**
* Wraps Puppeteer's interface to Headless Chrome to expose high level rendering
* APIs that are able to handle web components and PWAs.
*/
export class Renderer {
private browser: puppeteer.Browser;
private config: Config;
constructor(browser: puppeteer.Browser, config: Config) {
this.browser = browser;
this.config = config;
}
private restrictRequest(requestUrl: string): boolean {
const parsedUrl = url.parse(requestUrl);
if (parsedUrl.hostname && parsedUrl.hostname.match(/\.internal$/)) {
return true;
}
if (this.config.restrictedUrlPattern && requestUrl.match(new RegExp(this.config.restrictedUrlPattern))) {
return true;
}
return false;
}
async serialize(
requestUrl: string,
isMobile: boolean,
timezoneId?: string
): Promise<SerializedResponse> {
/**
* Executed on the page after the page has loaded. Strips script and
* import tags to prevent further loading of resources.
*/
function stripPage() {
// Strip only script tags that contain JavaScript (either no type attribute or one that contains "javascript")
const elements = document.querySelectorAll(
'script:not([type]), script[type*="javascript"], script[type="module"], link[rel=import]'
);
for (const e of Array.from(elements)) {
e.remove();
}
}
/**
* Injects a <base> tag which allows other resources to load. This
* has no effect on serialised output, but allows it to verify render
* quality.
*/
function injectBaseHref(origin: string, directory: string) {
const bases = document.head.querySelectorAll('base');
if (bases.length) {
// Patch existing <base> if it is relative.
const existingBase = bases[0].getAttribute('href') || '';
if (existingBase.startsWith('/')) {
// check if is only "/" if so add the origin only
if (existingBase === '/') {
bases[0].setAttribute('href', origin);
} else {
bases[0].setAttribute('href', origin + existingBase);
}
}
} else {
// Only inject <base> if it doesn't already exist.
const base = document.createElement('base');
// Base url is the current directory
base.setAttribute('href', origin + directory);
document.head.insertAdjacentElement('afterbegin', base);
}
}
const page = await this.browser.newPage();
// Page may reload when setting isMobile
// https://github.com/GoogleChrome/puppeteer/blob/v1.10.0/docs/api.md#pagesetviewportviewport
await page.setViewport({
width: this.config.width,
height: this.config.height,
isMobile,
});
if (isMobile) {
page.setUserAgent(MOBILE_USERAGENT);
}
if (timezoneId) {
try {
await page.emulateTimezone(timezoneId);
} catch (e) {
if (e.message.includes('Invalid timezone')) {
return {
status: 400,
customHeaders: new Map(),
content: 'Invalid timezone id',
};
}
}
}
await page.setExtraHTTPHeaders(this.config.reqHeaders);
page.evaluateOnNewDocument('customElements.forcePolyfill = true');
page.evaluateOnNewDocument('ShadyDOM = {force: true}');
page.evaluateOnNewDocument('ShadyCSS = {shimcssproperties: true}');
await page.setRequestInterception(true);
page.on('request', (interceptedRequest: puppeteer.HTTPRequest) => {
if (this.restrictRequest(interceptedRequest.url())) {
interceptedRequest.abort();
} else {
interceptedRequest.continue();
}
});
let response: puppeteer.HTTPResponse | null = null;
// Capture main frame response. This is used in the case that rendering
// times out, which results in puppeteer throwing an error. This allows us
// to return a partial response for what was able to be rendered in that
// time frame.
page.on('response', (r: puppeteer.HTTPResponse) => {
if (!response) {
response = r;
}
});
try {
// Navigate to page. Wait until there are no oustanding network requests.
response = await page.goto(requestUrl, {
timeout: this.config.timeout,
waitUntil: 'networkidle0',
});
} catch (e) {
console.error(e);
}
if (!response) {
console.error('response does not exist');
// This should only occur when the page is about:blank. See
// https://github.com/GoogleChrome/puppeteer/blob/v1.5.0/docs/api.md#pagegotourl-options.
await page.close();
if (this.config.closeBrowser) {
await this.browser.close();
}
return { status: 400, customHeaders: new Map(), content: '' };
}
// Disable access to compute metadata. See
// https://cloud.google.com/compute/docs/storing-retrieving-metadata.
if (response.headers()['metadata-flavor'] === 'Google') {
await page.close();
if (this.config.closeBrowser) {
await this.browser.close();
}
return { status: 403, customHeaders: new Map(), content: '' };
}
// Set status to the initial server's response code. Check for a <meta
// name="render:status_code" content="4xx" /> tag which overrides the status
// code.
let statusCode = response.status();
const newStatusCode = await page
.$eval('meta[name="render:status_code"]', (element) =>
parseInt(element.getAttribute('content') || '')
)
.catch(() => undefined);
// On a repeat visit to the same origin, browser cache is enabled, so we may
// encounter a 304 Not Modified. Instead we'll treat this as a 200 OK.
if (statusCode === 304) {
statusCode = 200;
}
// Original status codes which aren't 200 always return with that status
// code, regardless of meta tags.
if (statusCode === 200 && newStatusCode) {
statusCode = newStatusCode;
}
// Check for <meta name="render:header" content="key:value" /> tag to allow a custom header in the response
// to the crawlers.
const customHeaders = await page
.$eval('meta[name="render:header"]', (element) => {
const result = new Map<string, string>();
const header = element.getAttribute('content');
if (header) {
const i = header.indexOf(':');
if (i !== -1) {
result.set(
header.substr(0, i).trim(),
header.substring(i + 1).trim()
);
}
}
return JSON.stringify([...result]);
})
.catch(() => undefined);
// Remove script & import tags.
await page.evaluate(stripPage);
// Inject <base> tag with the origin of the request (ie. no path).
const parsedUrl = url.parse(requestUrl);
await page.evaluate(
injectBaseHref,
`${parsedUrl.protocol}//${parsedUrl.host}`,
`${dirname(parsedUrl.pathname || '')}`
);
// Serialize page.
const result = (await page.content()) as string;
await page.close();
if (this.config.closeBrowser) {
await this.browser.close();
}
return {
status: statusCode,
customHeaders: customHeaders
? new Map(JSON.parse(customHeaders))
: new Map(),
content: result,
};
}
async screenshot(
url: string,
isMobile: boolean,
dimensions: ViewportDimensions,
options?: ScreenshotOptions,
timezoneId?: string
): Promise<Buffer> {
const page = await this.browser.newPage();
// Page may reload when setting isMobile
// https://github.com/GoogleChrome/puppeteer/blob/v1.10.0/docs/api.md#pagesetviewportviewport
await page.setViewport({
width: dimensions.width,
height: dimensions.height,
isMobile,
});
if (isMobile) {
page.setUserAgent(MOBILE_USERAGENT);
}
await page.setRequestInterception(true);
page.addListener('request', (interceptedRequest: puppeteer.HTTPRequest) => {
if (this.restrictRequest(interceptedRequest.url())) {
interceptedRequest.abort();
} else {
interceptedRequest.continue();
}
});
if (timezoneId) {
await page.emulateTimezone(timezoneId);
}
let response: puppeteer.HTTPResponse | null = null;
try {
// Navigate to page. Wait until there are no oustanding network requests.
response = await page.goto(url, {
timeout: this.config.timeout,
waitUntil: 'networkidle0',
});
} catch (e) {
console.error(e);
}
if (!response) {
await page.close();
if (this.config.closeBrowser) {
await this.browser.close();
}
throw new ScreenshotError('NoResponse');
}
// Disable access to compute metadata. See
// https://cloud.google.com/compute/docs/storing-retrieving-metadata.
if (response.headers()['metadata-flavor'] === 'Google') {
await page.close();
if (this.config.closeBrowser) {
await this.browser.close();
}
throw new ScreenshotError('Forbidden');
}
// Must be jpeg & binary format.
const screenshotOptions: ScreenshotOptions = {
type: options?.type || 'jpeg',
encoding: options?.encoding || 'binary',
};
// Screenshot returns a buffer based on specified encoding above.
// https://github.com/GoogleChrome/puppeteer/blob/v1.8.0/docs/api.md#pagescreenshotoptions
const buffer = (await page.screenshot(screenshotOptions)) as Buffer;
await page.close();
if (this.config.closeBrowser) {
await this.browser.close();
}
return buffer;
}
}
type ErrorType = 'Forbidden' | 'NoResponse';
export class ScreenshotError extends Error {
type: ErrorType;
constructor(type: ErrorType) {
super(type);
this.name = this.constructor.name;
this.type = type;
}
}