parseuri.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parse = void 0;
  4. // imported from https://github.com/galkn/parseuri
  5. /**
  6. * Parses a URI
  7. *
  8. * Note: we could also have used the built-in URL object, but it isn't supported on all platforms.
  9. *
  10. * See:
  11. * - https://developer.mozilla.org/en-US/docs/Web/API/URL
  12. * - https://caniuse.com/url
  13. * - https://www.rfc-editor.org/rfc/rfc3986#appendix-B
  14. *
  15. * History of the parse() method:
  16. * - first commit: https://github.com/socketio/socket.io-client/commit/4ee1d5d94b3906a9c052b459f1a818b15f38f91c
  17. * - export into its own module: https://github.com/socketio/engine.io-client/commit/de2c561e4564efeb78f1bdb1ba39ef81b2822cb3
  18. * - reimport: https://github.com/socketio/engine.io-client/commit/df32277c3f6d622eec5ed09f493cae3f3391d242
  19. *
  20. * @author Steven Levithan <stevenlevithan.com> (MIT license)
  21. * @api private
  22. */
  23. const re = /^(?:(?![^:@\/?#]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@\/?#]*)(?::([^:@\/?#]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  24. const parts = [
  25. 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
  26. ];
  27. function parse(str) {
  28. const src = str, b = str.indexOf('['), e = str.indexOf(']');
  29. if (b != -1 && e != -1) {
  30. str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
  31. }
  32. let m = re.exec(str || ''), uri = {}, i = 14;
  33. while (i--) {
  34. uri[parts[i]] = m[i] || '';
  35. }
  36. if (b != -1 && e != -1) {
  37. uri.source = src;
  38. uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
  39. uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
  40. uri.ipv6uri = true;
  41. }
  42. uri.pathNames = pathNames(uri, uri['path']);
  43. uri.queryKey = queryKey(uri, uri['query']);
  44. return uri;
  45. }
  46. exports.parse = parse;
  47. function pathNames(obj, path) {
  48. const regx = /\/{2,9}/g, names = path.replace(regx, "/").split("/");
  49. if (path.slice(0, 1) == '/' || path.length === 0) {
  50. names.splice(0, 1);
  51. }
  52. if (path.slice(-1) == '/') {
  53. names.splice(names.length - 1, 1);
  54. }
  55. return names;
  56. }
  57. function queryKey(uri, query) {
  58. const data = {};
  59. query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) {
  60. if ($1) {
  61. data[$1] = $2;
  62. }
  63. });
  64. return data;
  65. }