replace-info.js 764 B

12345678910111213141516171819202122232425262728293031
  1. const URL = require('url').URL
  2. // replaces auth info in an array of arguments or in a strings
  3. function replaceInfo (arg) {
  4. const isArray = Array.isArray(arg)
  5. const isString = str => typeof str === 'string'
  6. if (!isArray && !isString(arg))
  7. return arg
  8. const testUrlAndReplace = str => {
  9. try {
  10. const url = new URL(str)
  11. return url.password === '' ? str : str.replace(url.password, '***')
  12. } catch (e) {
  13. return str
  14. }
  15. }
  16. const args = isString(arg) ? arg.split(' ') : arg
  17. const info = args.map(a => {
  18. if (isString(a) && a.indexOf(' ') > -1)
  19. return a.split(' ').map(testUrlAndReplace).join(' ')
  20. return testUrlAndReplace(a)
  21. })
  22. return isString(arg) ? info.join(' ') : info
  23. }
  24. module.exports = replaceInfo