codekingpro/portable-devtools
114k
1const matchers = require('./matchers')
2const { redactUrlPassword } = require('./utils')
3
4const REPLACE = '***'
5
6const redact = (value) => {
7 if (typeof value !== 'string' || !value) {
8 return value
9 }
10 return redactUrlPassword(value, REPLACE)
11 .replace(matchers.NPM_SECRET.pattern, `npm_${REPLACE}`)
12 .replace(matchers.UUID.pattern, REPLACE)
13}
14
15// split on \s|= similar to how nopt parses options
16const splitAndRedact = (str) => {
17 // stateful regex, don't move out of this scope
18 const splitChars = /[\s=]/g
19
20 let match = null
21 let result = ''
22 let index = 0
23 while (match = splitChars.exec(str)) {
24 result += redact(str.slice(index, match.index)) + match[0]
25 index = splitChars.lastIndex
26 }
27
28 return result + redact(str.slice(index))
29}
30
31// replaces auth info in an array of arguments or in a strings
32const redactLog = (arg) => {
33 if (typeof arg === 'string') {
34 return splitAndRedact(arg)
35 } else if (Array.isArray(arg)) {
36 return arg.map((a) => typeof a === 'string' ? splitAndRedact(a) : a)
37 }
38 return arg
39}
40
41module.exports = {
42 redact,
43 redactLog,
44}
45 