replace-info.js 556 B

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