index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. // Dependencies
  3. var parseUrl = require("parse-url"),
  4. isSsh = require("is-ssh");
  5. /**
  6. * gitUp
  7. * Parses the input url.
  8. *
  9. * @name gitUp
  10. * @function
  11. * @param {String} input The input url.
  12. * @return {Object} An object containing the following fields:
  13. *
  14. * - `protocols` (Array): An array with the url protocols (usually it has one element).
  15. * - `port` (null|Number): The domain port.
  16. * - `resource` (String): The url domain (including subdomains).
  17. * - `user` (String): The authentication user (usually for ssh urls).
  18. * - `pathname` (String): The url pathname.
  19. * - `hash` (String): The url hash.
  20. * - `search` (String): The url querystring value.
  21. * - `href` (String): The input url.
  22. * - `protocol` (String): The git url protocol.
  23. * - `token` (String): The oauth token (could appear in the https urls).
  24. */
  25. function gitUp(input) {
  26. var output = parseUrl(input);
  27. output.token = "";
  28. if (output.password === "x-oauth-basic") {
  29. output.token = output.user;
  30. } else if (output.user === "x-token-auth") {
  31. output.token = output.password;
  32. }
  33. if (isSsh(output.protocols) || output.protocols.length === 0 && isSsh(input)) {
  34. output.protocol = "ssh";
  35. } else if (output.protocols.length) {
  36. output.protocol = output.protocols[0];
  37. } else {
  38. output.protocol = "file";
  39. output.protocols = ["file"];
  40. }
  41. output.href = output.href.replace(/\/$/, "");
  42. return output;
  43. }
  44. module.exports = gitUp;