index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. var splits = output.user.split(":");
  29. if (splits.length === 2) {
  30. if (splits[1] === "x-oauth-basic") {
  31. output.token = splits[0];
  32. } else if (splits[0] === "x-token-auth") {
  33. output.token = splits[1];
  34. }
  35. }
  36. if (isSsh(output.protocols) || isSsh(input)) {
  37. output.protocol = "ssh";
  38. } else if (output.protocols.length) {
  39. output.protocol = output.protocols[0];
  40. } else {
  41. output.protocol = "file";
  42. }
  43. output.href = output.href.replace(/\/$/, "");
  44. return output;
  45. }
  46. module.exports = gitUp;