You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
2.3 KiB

4 years ago
  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;