-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
792 lines (654 loc) · 22.4 KB
/
index.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
const getNodeVersion = () => {
try {
return parseFloat(process.version.replace(/v/, ""));
} catch (err) {
return null;
}
};
const nodeVersion = getNodeVersion();
if (nodeVersion < 12.0) {
console.log("prerendercloud requires node >= 12.0");
process.exit(1);
}
const debug = require("debug")("prerendercloud");
const util = require("./lib/util");
const Url = require("./lib/our-url");
const middlewareCacheSingleton = {};
const Options = require("./lib/options");
const options = new Options(middlewareCacheSingleton);
const got = require("got-lite");
require("./lib/got-retries")(got, options, debug);
const vary = require("vary");
// preserve (and send to client) these headers from service.prerender.cloud which originally came from the origin server
const headerWhitelist = [
"vary",
"content-type",
"cache-control",
"strict-transport-security",
"content-security-policy",
"public-key-pins",
"x-frame-options",
"x-xss-protection",
"x-content-type-options",
"location",
];
const botUserAgentMapper = (ua) => ua.toLowerCase();
const botsOnlyList = [
"googlebot",
"yahoo",
"bingbot",
"yandex",
"baiduspider",
"facebookexternalhit",
"twitterbot",
"rogerbot",
"linkedinbot",
"embedly",
"quora link preview",
"showyoubot",
"outbrain",
"pinterest/0.",
"pinterestbot",
"developers.google.com/+/web/snippet",
"slackbot",
"vkShare",
"W3C_Validator",
"redditbot",
"Applebot",
"WhatsApp",
"flipboard",
"tumblr",
"bitlybot",
"Bitrix link preview",
"XING-contenttabreceiver",
"Discordbot",
"TelegramBot",
"Google Search Console",
].map(botUserAgentMapper);
const userAgentIsBot = (headers, requestedPath = "") => {
const reqUserAgent =
(headers["user-agent"] && headers["user-agent"].toLowerCase()) || "";
if (headers["x-bufferbot"]) return true;
if (requestedPath.match(/[?&]_escaped_fragment_/)) return true;
return botsOnlyList.some((enabledUserAgent) =>
reqUserAgent.includes(enabledUserAgent)
);
};
const userAgentIsBotFromList = (botsOnlyList, headers, requestedPath = "") => {
const reqUserAgent =
(headers["user-agent"] && headers["user-agent"].toLowerCase()) || "";
if (headers["x-bufferbot"]) return true;
if (requestedPath.match(/[?&]_escaped_fragment_/)) return true;
return botsOnlyList.some((enabledUserAgent) =>
reqUserAgent.includes(enabledUserAgent)
);
};
const getServiceUrl = (hardcoded) =>
(hardcoded && hardcoded.replace(/\/+$/, "")) ||
process.env.PRERENDER_SERVICE_URL ||
"https://service.prerender.cloud";
const getRenderUrl = (action, url) =>
[getServiceUrl(), action, url].filter((p) => p).join("/");
// from https://stackoverflow.com/a/41072596
// if there are multiple values with different case, it just takes the last
// (which is wrong, it should merge some of them according to the HTTP spec, but it's fine for now)
const objectKeysToLowerCase = function (origObj) {
return Object.keys(origObj).reduce(function (newObj, key) {
const val = origObj[key];
const newVal = typeof val === "object" ? objectKeysToLowerCase(val) : val;
newObj[key.toLowerCase()] = newVal;
return newObj;
}, {});
};
const is5xxError = (statusCode) => parseInt(statusCode / 100) === 5;
const is4xxError = (statusCode) => {
const n = parseInt(statusCode);
const i = parseInt(statusCode / 100);
return i === 4 && n !== 429;
};
const isGotClientTimeout = (err) =>
err.name === "RequestError" && err.code === "ETIMEDOUT";
const zlib = require("zlib");
function compression(req, res, data) {
if (
req.headers["accept-encoding"] &&
req.headers["accept-encoding"].match(/gzip/i)
) {
zlib.gzip(data.body, (err, gzipped) => {
if (err) {
console.error(err);
return res.status(500).send("compression error");
}
res.writeHead(
data.statusCode,
Object.assign({}, data.headers, { "content-encoding": "gzip" })
);
res.end(gzipped);
});
} else {
res.writeHead(data.statusCode, data.headers);
res.end(data.body);
}
}
const handleSkip = (msg, next) => {
debug(msg);
if (process.env.NODE_ENV !== "test")
console.error("prerendercloud middleware SKIPPED:", msg);
return next();
};
const concurrentRequestCache = {};
// response: { body, statusCode, headers }
function createResponse(req, requestedUrl, response) {
const lowerCasedHeaders = objectKeysToLowerCase(response.headers);
const headers = {};
headerWhitelist.forEach((h) => {
if (lowerCasedHeaders[h]) headers[h] = lowerCasedHeaders[h];
});
let body = response.body;
let screenshot;
let meta;
let links;
if (
(options.options.withScreenshot && options.options.withScreenshot(req)) ||
(options.options.withMetadata && options.options.withMetadata(req))
) {
headers["content-type"] = "text/html";
let json;
try {
json = JSON.parse(body);
} catch (err) {
if (err.name && err.name.match(/SyntaxError/)) {
console.log(options.options);
console.log(req.host);
console.log(req.url);
console.log(response.headers);
console.log(body);
console.error(
"withScreenshot expects JSON from server but parsing this failed:",
body && body.toString().slice(0, 140) + "..."
);
}
throw err;
}
screenshot = json.screenshot && Buffer.from(json.screenshot, "base64");
body = json.body && Buffer.from(json.body, "base64").toString();
meta = json.meta && JSON.parse(Buffer.from(json.meta, "base64"));
links = json.links && JSON.parse(Buffer.from(json.links, "base64"));
}
const data = { statusCode: response.statusCode, headers, body };
if (screenshot) data.screenshot = screenshot;
if (meta) data.meta = meta;
if (links) data.links = links;
if (
options.options.enableMiddlewareCache &&
`${response.statusCode}`.startsWith("2") &&
body &&
body.length
)
middlewareCacheSingleton.instance.set(requestedUrl, data);
return data;
}
class Prerender {
constructor(req) {
this.req = req;
this.url = Url.parse(req, options);
this.configureBotsOnlyList();
}
configureBotsOnlyList() {
this.botsOnlyList = botsOnlyList;
if (options.options.botsOnly && Array.isArray(options.options.botsOnly)) {
options.options.botsOnly.forEach((additionalBotUserAgent) => {
const alreadyExistsInList = this.botsOnlyList.find(
(b) => b === additionalBotUserAgent
);
if (!alreadyExistsInList) {
this.botsOnlyList.push(botUserAgentMapper(additionalBotUserAgent));
}
});
}
}
// promise cache wrapper around ._get to prevent concurrent requests to same URL
get() {
if (concurrentRequestCache[this.url.requestedUrl])
return concurrentRequestCache[this.url.requestedUrl];
const promise = this._get();
const deleteCache = () => {
concurrentRequestCache[this.url.requestedUrl] = undefined;
delete concurrentRequestCache[this.url.requestedUrl];
};
return (concurrentRequestCache[this.url.requestedUrl] = promise)
.then((res) => {
deleteCache();
return res;
})
.catch((err) => {
deleteCache();
return Promise.reject(err);
});
}
// fulfills promise when service.prerender.cloud response is: 2xx, 4xx
// rejects promise when request lib errors or service.prerender.cloud response is: 5xx
_get() {
const apiRequestUrl = this._createApiRequestUrl();
const headers = this._createHeaders();
let requestPromise;
if (options.isThrottled(this.url.requestedUrl)) {
requestPromise = Promise.reject(new Error("throttled"));
} else {
debug("prerendering:", apiRequestUrl, headers);
requestPromise = got.get(apiRequestUrl, {
headers,
retries: options.options.retries,
followRedirect: false,
timeout: options.options.timeout || 20000,
});
}
return requestPromise
.then((response) => {
return createResponse(this.req, this.url.requestedUrl, response);
})
.catch((err) => {
const shouldBubble = util.isFunction(options.options.bubbleUp5xxErrors)
? options.options.bubbleUp5xxErrors(err, this.req, err.response)
: options.options.bubbleUp5xxErrors;
const statusCode = err.response && parseInt(err.response.statusCode);
const nonErrorStatusCode =
statusCode === 404 || statusCode === 301 || statusCode === 302;
if (!nonErrorStatusCode) {
options.recordFail(this.url.requestedUrl);
}
if (shouldBubble && !nonErrorStatusCode) {
if (err.response && is5xxError(err.response.statusCode))
return createResponse(
this.req,
this.url.requestedUrl,
err.response
);
if (err.message && err.message.match(/throttle/)) {
return createResponse(this.req, this.url.requestedUrl, {
body: "Error: prerender.cloud client throttled this prerender request due to a recent timeout",
statusCode: 503,
headers: { "content-type": "text/html" },
});
}
if (isGotClientTimeout(err))
return createResponse(this.req, this.url.requestedUrl, {
body: "Error: prerender.cloud client timeout (as opposed to prerender.cloud server timeout)",
statusCode: 500,
headers: { "content-type": "text/html" },
});
return Promise.reject(err);
} else if (err.response && is4xxError(err.response.statusCode)) {
return createResponse(this.req, this.url.requestedUrl, err.response);
}
return Promise.reject(err);
});
}
writeHttpResponse(req, res, next, data) {
const _writeHttpResponse = () =>
this._writeHttpResponse(req, res, next, data);
if (options.options.afterRenderBlocking) {
// preserve original body from origin prerender.cloud so mutations
// from afterRenderBlocking don't affect concurrentRequestCache
if (!data.origBodyBeforeAfterRenderBlocking) {
data.origBodyBeforeAfterRenderBlocking = data.body;
} else {
data.body = data.origBodyBeforeAfterRenderBlocking;
}
return options.options.afterRenderBlocking(
null,
req,
data,
_writeHttpResponse
);
}
_writeHttpResponse();
}
// data looks like { statusCode, headers, body }
_writeHttpResponse(req, res, next, data) {
if (options.options.afterRender)
process.nextTick(() => options.options.afterRender(null, req, data));
try {
if (data.statusCode === 400) {
res.statusCode = 400;
return res.end(
`service.prerender.cloud can't prerender this page due to user error: ${data.body}`
);
} else if (data.statusCode === 429) {
return handleSkip("rate limited due to free tier", next);
} else {
return compression(req, res, data);
}
} catch (error) {
console.error(
"unrecoverable prerendercloud middleware error:",
error && error.message
);
console.error(
"submit steps to reproduce here: https://github.com/sanfrancesco/prerendercloud-nodejs/issues"
);
throw error;
}
}
static middleware(req, res, next) {
const prerender = new Prerender(req);
const objForReqRes = {
url: { requestedPath: prerender.url.requestedPath },
};
// this is for beforeRender(req, done) func so there's visibility into what URL is being used
req.prerender = objForReqRes;
// this is for lambda@edge downstream: https://github.com/sanfrancesco/prerendercloud-lambda-edge
res.prerender = objForReqRes;
if (options.options.botsOnly) {
vary(res, "User-Agent");
}
if (!prerender._shouldPrerender()) {
debug(
"NOT prerendering",
req.originalUrl,
req && req.headers && { "user-agent": req.headers["user-agent"] }
);
return next();
}
if (options.options.enableMiddlewareCache) {
const cached = middlewareCacheSingleton.instance.get(
prerender.url.requestedUrl
);
if (cached) {
debug(
"returning cache",
req.originalUrl,
req && req.headers && { "user-agent": req.headers["user-agent"] }
);
return prerender.writeHttpResponse(req, res, next, cached);
}
}
const remotePrerender = function () {
return prerender
.get()
.then(function (data) {
return prerender.writeHttpResponse(req, res, next, data);
})
.catch(function (error) {
if (process.env.NODE_ENV !== "test") console.error(error);
return handleSkip(`server error: ${error && error.message}`, next);
});
};
if (options.options.beforeRender) {
const donePassedToUserBeforeRender = function (err, stringOrObject) {
if (!stringOrObject) {
return remotePrerender();
} else if (typeof stringOrObject === "string") {
return prerender.writeHttpResponse(req, res, next, {
statusCode: 200,
headers: {
"content-type": "text/html; charset=utf-8",
},
body: stringOrObject,
});
} else if (typeof stringOrObject === "object") {
return prerender.writeHttpResponse(
req,
res,
next,
Object.assign(
{
statusCode: stringOrObject.status,
headers: Object.assign(
{
"content-type": "text/html; charset=utf-8",
},
stringOrObject.headers
),
body: stringOrObject.body,
},
{
screenshot: stringOrObject.screenshot,
meta: stringOrObject.meta,
links: stringOrObject.links,
}
)
);
}
};
return options.options.beforeRender(req, donePassedToUserBeforeRender);
} else {
return remotePrerender();
}
}
_shouldPrerender() {
if (!(this.req && this.req.headers)) return false;
if (this.req.method != "GET" && this.req.method != "HEAD") return false;
if (this._alreadyPrerendered()) return false;
if (!this._prerenderableExtension()) return false;
if (this._isPrerenderCloudUserAgent()) return false;
if (this._isBlacklistedPath()) return false;
if (options.options.whitelistPaths && !this._isWhitelistedPath())
return false;
if (options.options.shouldPrerender) {
return options.options.shouldPrerender(this.req);
} else {
return this._prerenderableUserAgent();
}
}
_createHeaders() {
let h = {
"User-Agent": "prerender-cloud-nodejs-middleware",
"accept-encoding": "gzip",
};
if (this.req.headers["user-agent"])
Object.assign(h, {
"X-Original-User-Agent": this.req.headers["user-agent"],
});
let token = options.options.prerenderToken || process.env.PRERENDER_TOKEN;
if (token) Object.assign(h, { "X-Prerender-Token": token });
if (options.options.removeScriptTags)
Object.assign(h, { "Prerender-Remove-Script-Tags": true });
if (options.options.removeTrailingSlash)
Object.assign(h, { "Prerender-Remove-Trailing-Slash": true });
if (options.options.metaOnly && options.options.metaOnly(this.req))
Object.assign(h, { "Prerender-Meta-Only": true });
if (
options.options.followRedirects &&
options.options.followRedirects(this.req)
)
Object.assign(h, { "Prerender-Follow-Redirects": true });
if (options.options.serverCacheDurationSeconds) {
const duration = options.options.serverCacheDurationSeconds(this.req);
if (duration != null) {
Object.assign(h, { "Prerender-Cache-Duration": duration });
}
}
if (options.options.deviceWidth) {
const deviceWidth = options.options.deviceWidth(this.req);
if (deviceWidth != null && typeof deviceWidth === "number") {
Object.assign(h, { "Prerender-Device-Width": deviceWidth });
}
}
if (options.options.deviceHeight) {
const deviceHeight = options.options.deviceHeight(this.req);
if (deviceHeight != null && typeof deviceHeight === "number") {
Object.assign(h, { "Prerender-Device-Height": deviceHeight });
}
}
if (options.options.waitExtraLong)
Object.assign(h, { "Prerender-Wait-Extra-Long": true });
if (options.options.disableServerCache) Object.assign(h, { noCache: true });
if (options.options.disableAjaxBypass)
Object.assign(h, { "Prerender-Disable-Ajax-Bypass": true });
if (options.options.disableAjaxPreload)
Object.assign(h, { "Prerender-Disable-Ajax-Preload": true });
if (options.options.disableHeadDedupe)
Object.assign(h, { "Prerender-Disable-Head-Dedupe": true });
if (
options.options.withScreenshot &&
options.options.withScreenshot(this.req)
)
Object.assign(h, { "Prerender-With-Screenshot": true });
if (options.options.withMetadata && options.options.withMetadata(this.req))
Object.assign(h, { "Prerender-With-Metadata": true });
if (this._hasOriginHeaderWhitelist()) {
options.options.originHeaderWhitelist.forEach((_h) => {
if (this.req.headers[_h])
Object.assign(h, { [_h]: this.req.headers[_h] });
});
Object.assign(h, {
"Origin-Header-Whitelist":
options.options.originHeaderWhitelist.join(" "),
});
}
return h;
}
_hasOriginHeaderWhitelist() {
return (
options.options.originHeaderWhitelist &&
Array.isArray(options.options.originHeaderWhitelist)
);
}
_createApiRequestUrl() {
return getRenderUrl(null, this.url.requestedUrl);
}
_alreadyPrerendered() {
return !!this.req.headers["x-prerendered"];
}
_prerenderableExtension() {
return this.url.hasHtmlPath;
}
_isPrerenderCloudUserAgent() {
let reqUserAgent = this.req.headers["user-agent"];
if (!reqUserAgent) return false;
reqUserAgent = reqUserAgent.toLowerCase();
return reqUserAgent.match(/prerendercloud/i);
}
_isWhitelistedPath() {
const paths = options.options.whitelistPaths(this.req);
if (paths && Array.isArray(paths)) {
if (paths.length === 0) {
return true;
}
return paths.some((path) => {
if (path instanceof RegExp) {
return path.test(this.req.url);
} else {
return path === this.req.url;
}
return false;
});
}
return true;
}
_isBlacklistedPath() {
if (options.options.blacklistPaths) {
const paths = options.options.blacklistPaths(this.req);
if (paths && Array.isArray(paths)) {
return paths.some((path) => {
if (path === this.req.url) return true;
if (path.endsWith("*")) {
const starIndex = path.indexOf("*");
const pathSlice = path.slice(0, starIndex);
if (this.req.url.startsWith(pathSlice)) return true;
}
return false;
});
}
}
return false;
}
_prerenderableUserAgent() {
const reqUserAgent = this.req.headers["user-agent"];
if (!reqUserAgent) return false;
if (options.options.whitelistUserAgents)
return options.options.whitelistUserAgents.some((enabledUserAgent) =>
reqUserAgent.includes(enabledUserAgent)
);
if (!options.options.botsOnly) return true;
// bots only
return userAgentIsBotFromList(
this.botsOnlyList,
this.req.headers,
this.url.original
);
}
}
Prerender.middleware.set = options.set.bind(options, Prerender.middleware);
// Prerender.middleware.cache =
Object.defineProperty(Prerender.middleware, "cache", {
get: function () {
return middlewareCacheSingleton.instance;
},
});
const screenshotAndPdf = (action, url, params = {}) => {
const headers = {};
const token = options.options.prerenderToken || process.env.PRERENDER_TOKEN;
if (token) Object.assign(headers, { "X-Prerender-Token": token });
if (params.viewportQuerySelector) {
Object.assign(headers, {
"Prerender-Viewport-Query-Selector": params.viewportQuerySelector,
});
if (params.viewportQuerySelectorPadding) {
Object.assign(headers, {
"Prerender-Viewport-Query-Selector-Padding":
params.viewportQuerySelectorPadding,
});
}
}
if (params.deviceWidth)
Object.assign(headers, { "Prerender-Device-Width": params.deviceWidth });
if (params.deviceHeight)
Object.assign(headers, { "Prerender-Device-Height": params.deviceHeight });
if (params.viewportWidth)
Object.assign(headers, {
"Prerender-Viewport-Width": params.viewportWidth,
});
if (params.viewportHeight)
Object.assign(headers, {
"Prerender-Viewport-Height": params.viewportHeight,
});
if (params.viewportX)
Object.assign(headers, { "Prerender-Viewport-X": params.viewportX });
if (params.viewportY)
Object.assign(headers, { "Prerender-Viewport-Y": params.viewportY });
if (
(params.viewportX || params.viewportY) &&
!(params.viewportWidth && params.viewportHeight)
) {
return Promise.reject(
new Error(
"can't set viewportX or viewportY without viewportWidth and viewportHeight"
)
);
}
if (params.noPageBreaks)
Object.assign(headers, { "Prerender-Pdf-No-Page-Breaks": "true" });
if (params.emulatedMedia)
Object.assign(headers, {
"Prerender-Emulated-Media": params.emulatedMedia,
});
return got(getRenderUrl(action, url), {
encoding: null,
headers,
retries: options.options.retries,
}).then((res) => res.body);
};
Prerender.middleware.screenshot = screenshotAndPdf.bind(
undefined,
"screenshot"
);
Prerender.middleware.pdf = screenshotAndPdf.bind(undefined, "pdf");
Prerender.middleware.prerender = function (url, params) {
const headers = {};
const token = options.options.prerenderToken || process.env.PRERENDER_TOKEN;
if (token) Object.assign(headers, { "X-Prerender-Token": token });
return got(getRenderUrl(null, url), {
encoding: null,
headers,
retries: options.options.retries,
}).then((res) => res.body);
};
Prerender.middleware.botsOnlyList = botsOnlyList;
Prerender.middleware.userAgentIsBot = userAgentIsBot;
Prerender.middleware.util = util;
// for testing only
Prerender.middleware.resetOptions = options.reset.bind(options);
Prerender.middleware.Options = Options;
module.exports = Prerender.middleware;