index.js 883 B

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. // Dependencies
  3. var protocols = require("protocols");
  4. /**
  5. * isSsh
  6. * Checks if an input value is a ssh url or not.
  7. *
  8. * @name isSsh
  9. * @function
  10. * @param {String|Array} input The input url or an array of protocols.
  11. * @return {Boolean} `true` if the input is a ssh url, `false` otherwise.
  12. */
  13. function isSsh(input) {
  14. if (Array.isArray(input)) {
  15. return input.indexOf("ssh") !== -1 || input.indexOf("rsync") !== -1;
  16. }
  17. if (typeof input !== "string") {
  18. return false;
  19. }
  20. var prots = protocols(input);
  21. input = input.substring(input.indexOf("://") + 3);
  22. if (isSsh(prots)) {
  23. return true;
  24. }
  25. // TODO This probably could be improved :)
  26. var urlPortPattern = new RegExp('\.([a-zA-Z\\d]+):(\\d+)\/');
  27. return !input.match(urlPortPattern) && input.indexOf("@") < input.indexOf(":");
  28. }
  29. module.exports = isSsh;