urlstring.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";
  2. var _parseUrl = require("url").parse;
  3. /*
  4. Customize the URL object that Node generates
  5. because:
  6. * necessary data for later
  7. * urlObj.host is useless
  8. * urlObj.hostname is too long
  9. * urlObj.path is useless
  10. * urlObj.pathname is too long
  11. * urlObj.protocol is inaccurate; should be called "scheme"
  12. * urlObj.search is mostly useless
  13. */
  14. function clean(urlObj)
  15. {
  16. var scheme = urlObj.protocol;
  17. if (scheme)
  18. {
  19. // Remove ":" suffix
  20. if (scheme.indexOf(":") === scheme.length-1)
  21. {
  22. scheme = scheme.substr(0, scheme.length-1);
  23. }
  24. }
  25. urlObj.host =
  26. {
  27. // TODO :: unescape(encodeURIComponent(s)) ? ... http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html
  28. full: urlObj.hostname,
  29. stripped: null
  30. };
  31. urlObj.path =
  32. {
  33. absolute:
  34. {
  35. array: null,
  36. string: urlObj.pathname
  37. },
  38. relative:
  39. {
  40. array: null,
  41. string: null
  42. }
  43. };
  44. urlObj.query =
  45. {
  46. object: urlObj.query,
  47. string:
  48. {
  49. full: null,
  50. stripped: null
  51. }
  52. };
  53. urlObj.extra =
  54. {
  55. hrefInfo:
  56. {
  57. minimumPathOnly: null,
  58. minimumResourceOnly: null,
  59. minimumQueryOnly: null,
  60. minimumHashOnly: null,
  61. empty: null,
  62. separatorOnlyQuery: urlObj.search==="?"
  63. },
  64. portIsDefault: null,
  65. relation:
  66. {
  67. maximumScheme: null,
  68. maximumAuth: null,
  69. maximumHost: null,
  70. maximumPort: null,
  71. maximumPath: null,
  72. maximumResource: null,
  73. maximumQuery: null,
  74. maximumHash: null,
  75. minimumScheme: null,
  76. minimumAuth: null,
  77. minimumHost: null,
  78. minimumPort: null,
  79. minimumPath: null,
  80. minimumResource: null,
  81. minimumQuery: null,
  82. minimumHash: null,
  83. overridesQuery: null
  84. },
  85. resourceIsIndex: null,
  86. slashes: urlObj.slashes
  87. };
  88. urlObj.resource = null;
  89. urlObj.scheme = scheme;
  90. delete urlObj.hostname;
  91. delete urlObj.pathname;
  92. delete urlObj.protocol;
  93. delete urlObj.search;
  94. delete urlObj.slashes;
  95. return urlObj;
  96. }
  97. function validScheme(url, options)
  98. {
  99. var valid = true;
  100. options.rejectedSchemes.every( function(rejectedScheme)
  101. {
  102. valid = !(url.indexOf(rejectedScheme+":") === 0);
  103. // Break loop
  104. return valid;
  105. });
  106. return valid;
  107. }
  108. function parseUrlString(url, options)
  109. {
  110. if ( validScheme(url,options) )
  111. {
  112. return clean( _parseUrl(url, true, options.slashesDenoteHost) );
  113. }
  114. else
  115. {
  116. return {href:url, valid:false};
  117. }
  118. }
  119. module.exports = parseUrlString;