-
Notifications
You must be signed in to change notification settings - Fork 945
Expand file tree
/
Copy pathutil.js
More file actions
123 lines (98 loc) · 3.04 KB
/
util.js
File metadata and controls
123 lines (98 loc) · 3.04 KB
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
const url = require('url');
const util = {};
// Normalizes unimportant differences in URLs - e.g. ensures
// http://google.com/ and http://google.com normalize to the same string
util.normalizeUrl = function (u) {
return url.format(url.parse(u, true));
};
util.getOptions = function (req) {
var requestedUrl = req.url;
//new API starts with render so we'll parse the URL differently if found
if (requestedUrl.indexOf('/render') === 0) {
let optionsObj = {};
if (req.method === 'GET') {
optionsObj = url.parse(requestedUrl, true).query;
} else if (req.method === 'POST') {
optionsObj = req.body;
}
return {
url: util.getUrl(optionsObj.url),
renderType: optionsObj.renderType || 'html',
userAgent: optionsObj.userAgent,
fullpage: optionsObj.fullpage || false,
width: optionsObj.width,
height: optionsObj.height,
followRedirects: optionsObj.followRedirects,
javascript: optionsObj.javascript,
};
} else {
return {
url: util.getUrl(requestedUrl),
renderType: 'html',
};
}
};
// Gets the URL to prerender from a request, stripping out unnecessary parts
util.getUrl = function (requestedUrl) {
var decodedUrl,
realUrl = requestedUrl,
parts;
if (!requestedUrl) {
return '';
}
realUrl = realUrl.replace(/^\//, '');
try {
decodedUrl = decodeURIComponent(realUrl);
} catch (e) {
decodedUrl = realUrl;
}
//encode a # for a non #! URL so that we access it correctly
decodedUrl = this.encodeHash(decodedUrl);
//if decoded url has two query params from a decoded escaped fragment for hashbang URLs
if (decodedUrl.indexOf('?') !== decodedUrl.lastIndexOf('?')) {
decodedUrl =
decodedUrl.substr(0, decodedUrl.lastIndexOf('?')) +
'&' +
decodedUrl.substr(decodedUrl.lastIndexOf('?') + 1);
}
parts = url.parse(decodedUrl, true);
// Remove the _escaped_fragment_ query parameter
if (parts.query && parts.query['_escaped_fragment_'] !== undefined) {
if (
parts.query['_escaped_fragment_'] &&
!Array.isArray(parts.query['_escaped_fragment_'])
) {
parts.hash = '#!' + parts.query['_escaped_fragment_'];
}
delete parts.query['_escaped_fragment_'];
delete parts.search;
}
// Bing was seen accessing a URL like /?&_escaped_fragment_=
delete parts.query[''];
var newUrl = url.format(parts);
//url.format encodes spaces but not arabic characters. decode it here so we can encode it all correctly later
try {
newUrl = decodeURIComponent(newUrl);
} catch (e) {}
newUrl = this.encodeHash(newUrl);
return newUrl;
};
util.encodeHash = function (url) {
if (url.indexOf('#!') === -1 && url.indexOf('#') >= 0) {
url = url.replace(/#/g, '%23');
}
return url;
};
util.log = function () {
console.log.apply(
console.log,
[new Date().toISOString()].concat(Array.prototype.slice.call(arguments, 0)),
);
};
util.debug = function (...args) {
if (!process.env.PRERENDER_DEBUG_LOG) {
return;
}
util.log.apply(util.log, args);
}
module.exports = util;