BVB Source Codes

prerender Show util.js Source code

Return Download prerender: download util.js Source code - Download prerender Source code - Type:.js
  1. /*jshint sub: true */
  2. var url = require('url');
  3.  
  4. var util = exports = module.exports = {};
  5.  
  6. // Normalizes unimportant differences in URLs - e.g. ensures
  7. // http://google.com/ and http://google.com normalize to the same string
  8. util.normalizeUrl = function (u) {
  9.     return url.format(url.parse(u, true));
  10. };
  11.  
  12. // Gets the URL to prerender from a request, stripping out unnecessary parts
  13. util.getUrl = function (req) {
  14.     var decodedUrl
  15.       , realUrl = req.url
  16.       , parts;
  17.  
  18.     realUrl = realUrl.replace(/^\//, '');
  19.  
  20.     try {
  21.         decodedUrl = decodeURIComponent(realUrl);
  22.     } catch (e) {
  23.         decodedUrl = realUrl;
  24.     }
  25.  
  26.     //encode a # for a non #! URL so that we access it correctly
  27.     decodedUrl = this.encodeHash(decodedUrl);
  28.  
  29.     //if decoded url has two query params from a decoded escaped fragment for hashbang URLs
  30.     if(decodedUrl.indexOf('?') !== decodedUrl.lastIndexOf('?')) {
  31.         decodedUrl = decodedUrl.substr(0, decodedUrl.lastIndexOf('?')) + '&' + decodedUrl.substr(decodedUrl.lastIndexOf('?')+1);
  32.     }
  33.  
  34.     parts = url.parse(decodedUrl, true);
  35.  
  36.     // Remove the _escaped_fragment_ query parameter
  37.     if (parts.query && parts.query['_escaped_fragment_'] !== undefined) {
  38.        
  39.         if (parts.query['_escaped_fragment_'] && !Array.isArray(parts.query['_escaped_fragment_'])) {
  40.             parts.hash = '#!' + parts.query['_escaped_fragment_'];
  41.         }
  42.  
  43.         delete parts.query['_escaped_fragment_'];
  44.         delete parts.search;
  45.     }
  46.  
  47.     // Bing was seen accessing a URL like /?&_escaped_fragment_=
  48.     delete parts.query[''];
  49.  
  50.     var newUrl = url.format(parts);
  51.  
  52.     //url.format encodes spaces but not arabic characters. decode it here so we can encode it all correctly later
  53.     try {
  54.         newUrl = decodeURIComponent(newUrl);
  55.     } catch (e) {}
  56.  
  57.     newUrl = this.encodeHash(newUrl);
  58.  
  59.     return newUrl;
  60. };
  61.  
  62. util.encodeHash = function(url) {
  63.     if(url.indexOf('#!') === -1 && url.indexOf('#') >= 0) {
  64.         url = url.replace(/#/g, '%23');
  65.     }
  66.  
  67.     return url;
  68. }
  69.  
  70. util.log = function() {
  71.   if (process.env.DISABLE_LOGGING) {
  72.     return;
  73.   }
  74.  
  75.   console.log.apply(console.log, [new Date().toISOString()].concat(Array.prototype.slice.call(arguments, 0)));
  76. };
  77.  
downloadutil.js Source code - Download prerender Source code
Related Source Codes/Software:
libsodium - A modern and easy-to-use crypto library. 2017-01-09
Side-Menu.Android - Side menu with some categories to choose. ... 2017-01-09
docker-gitlab - Dockerized GitLab http://www.da... 2017-01-09
sinon - Test spies, stubs and mocks for JavaScript. ... 2017-01-09
in-view - Get notified when a DOM element enters or exits th... 2017-01-09
falcon - Falcon is a low-level, high-performance Python fra... 2017-01-09
ImageOptim - GUI image optimizer for Mac htt... 2017-01-09
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
CLNDR - 2017-02-19
OptiKey - OptiKey - Full computer control and speech with yo... 2017-02-19
MRProgress - Collection of iOS drop-in components to visualize ... 2017-02-19
BGARefreshLayout-Android - On a variety of drop-down refresh effect, loading ... 2017-02-19
angular-http-auth - 2017-02-19
pydata-book - Materials and IPython notebooks for "Python for Da... 2017-02-19
xenia - Xbox 360 Emulator Research Project ... 2017-02-18
flux-comparison - 2017-02-18
luvit - Lua + libUV + jIT = pure awesomesauce ... 2017-02-18
orleans - Orleans - Distributed Virtual Actor Model ... 2017-02-18

 Back to top