• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const { URL } = require('url')
2
3const replace = '***'
4const tokenRegex = /\bnpm_[a-zA-Z0-9]{36}\b/g
5const guidRegex = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/g
6
7const cleanUrl = (str) => {
8  if (typeof str !== 'string' || !str) {
9    return str
10  }
11
12  try {
13    const url = new URL(str)
14    if (url.password) {
15      url.password = replace
16      str = url.toString()
17    }
18  } catch {
19    // ignore errors
20  }
21
22  return str
23    .replace(tokenRegex, `npm_${replace}`)
24    .replace(guidRegex, `npm_${replace}`)
25}
26
27module.exports = cleanUrl
28