UNPKG

2.86 kBJavaScriptView Raw
1const url = require('url');
2
3const util = exports = module.exports = {};
4
5// Normalizes unimportant differences in URLs - e.g. ensures
6// http://google.com/ and http://google.com normalize to the same string
7util.normalizeUrl = function(u) {
8 return url.format(url.parse(u, true));
9};
10
11util.getOptions = function(req) {
12
13 var requestedUrl = req.url;
14
15 //new API starts with render so we'll parse the URL differently if found
16 if(requestedUrl.indexOf('/render') === 0) {
17
18 let optionsObj = {};
19 if(req.method === 'GET') {
20 optionsObj = url.parse(requestedUrl, true).query;
21 } else if (req.method === 'POST') {
22 optionsObj = req.body;
23 }
24
25 return {
26 url: util.getUrl(optionsObj.url),
27 renderType: optionsObj.renderType || 'html',
28 userAgent: optionsObj.userAgent,
29 fullpage: optionsObj.fullpage || false,
30 width: optionsObj.width,
31 height: optionsObj.height,
32 followRedirects: optionsObj.followRedirects,
33 javascript: optionsObj.javascript
34 }
35
36 } else {
37
38 return {
39 url: util.getUrl(requestedUrl),
40 renderType: 'html'
41 }
42 }
43}
44
45// Gets the URL to prerender from a request, stripping out unnecessary parts
46util.getUrl = function(requestedUrl) {
47 var decodedUrl, realUrl = requestedUrl,
48 parts;
49
50 if (!requestedUrl) {
51 return '';
52 }
53
54 realUrl = realUrl.replace(/^\//, '');
55
56 try {
57 decodedUrl = decodeURIComponent(realUrl);
58 } catch (e) {
59 decodedUrl = realUrl;
60 }
61
62 //encode a # for a non #! URL so that we access it correctly
63 decodedUrl = this.encodeHash(decodedUrl);
64
65 //if decoded url has two query params from a decoded escaped fragment for hashbang URLs
66 if (decodedUrl.indexOf('?') !== decodedUrl.lastIndexOf('?')) {
67 decodedUrl = decodedUrl.substr(0, decodedUrl.lastIndexOf('?')) + '&' + decodedUrl.substr(decodedUrl.lastIndexOf('?') + 1);
68 }
69
70 parts = url.parse(decodedUrl, true);
71
72 // Remove the _escaped_fragment_ query parameter
73 if (parts.query && parts.query['_escaped_fragment_'] !== undefined) {
74
75 if (parts.query['_escaped_fragment_'] && !Array.isArray(parts.query['_escaped_fragment_'])) {
76 parts.hash = '#!' + parts.query['_escaped_fragment_'];
77 }
78
79 delete parts.query['_escaped_fragment_'];
80 delete parts.search;
81 }
82
83 // Bing was seen accessing a URL like /?&_escaped_fragment_=
84 delete parts.query[''];
85
86 var newUrl = url.format(parts);
87
88 //url.format encodes spaces but not arabic characters. decode it here so we can encode it all correctly later
89 try {
90 newUrl = decodeURIComponent(newUrl);
91 } catch (e) {}
92
93 newUrl = this.encodeHash(newUrl);
94
95 return newUrl;
96};
97
98util.encodeHash = function(url) {
99 if (url.indexOf('#!') === -1 && url.indexOf('#') >= 0) {
100 url = url.replace(/#/g, '%23');
101 }
102
103 return url;
104}
105
106util.log = function() {
107 if (process.env.DISABLE_LOGGING) {
108 return;
109 }
110
111 console.log.apply(console.log, [new Date().toISOString()].concat(Array.prototype.slice.call(arguments, 0)));
112};
\No newline at end of file