uri.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. // Load Modules
  3. const RFC3986 = require('./rfc3986');
  4. // Declare internals
  5. const internals = {
  6. Uri: {
  7. createUriRegex: function (optionalScheme, allowRelative) {
  8. let scheme = RFC3986.scheme;
  9. // If we were passed a scheme, use it instead of the generic one
  10. if (optionalScheme) {
  11. // Have to put this in a non-capturing group to handle the OR statements
  12. scheme = '(?:' + optionalScheme + ')';
  13. }
  14. const withScheme = '(?:' + scheme + ':' + RFC3986.hierPart + ')';
  15. const prefix = allowRelative ? '(?:' + withScheme + '|' + RFC3986.relativeRef + ')' : withScheme;
  16. /**
  17. * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  18. *
  19. * OR
  20. *
  21. * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  22. */
  23. return new RegExp('^' + prefix + '(?:\\?' + RFC3986.query + ')?' + '(?:#' + RFC3986.fragment + ')?$');
  24. }
  25. }
  26. };
  27. module.exports = internals.Uri;