codekingpro/portable-devtools
115k
1const {
2 AUTH_HEADER,
3 JSON_WEB_TOKEN,
4 NPM_SECRET,
5 DEEP_HEADER_AUTHORIZATION,
6 DEEP_HEADER_SET_COOKIE,
7 REWRITE_REQUEST,
8 REWRITE_RESPONSE,
9 DEEP_HEADER_COOKIE,
10} = require('./matchers')
11
12const {
13 redactUrlMatcher,
14 redactUrlPasswordMatcher,
15 redactMatchers,
16} = require('./utils')
17
18const { serializeError } = require('./error')
19
20const { deepMap } = require('./deep-map')
21
22const _redact = redactMatchers(
23 NPM_SECRET,
24 AUTH_HEADER,
25 JSON_WEB_TOKEN,
26 DEEP_HEADER_AUTHORIZATION,
27 DEEP_HEADER_SET_COOKIE,
28 DEEP_HEADER_COOKIE,
29 REWRITE_REQUEST,
30 REWRITE_RESPONSE,
31 redactUrlMatcher(
32 redactUrlPasswordMatcher()
33 )
34)
35
36const redact = (input) => deepMap(input, (value, path) => _redact(value, { path }))
37
38/** takes an error returns new error keeping some custom properties */
39function redactError (input) {
40 const { message, ...data } = serializeError(input)
41 const output = new Error(redact(message))
42 return Object.assign(output, redact(data))
43}
44
45/** runs a function within try / catch and throws error wrapped in redactError */
46function redactThrow (func) {
47 if (typeof func !== 'function') {
48 throw new Error('redactThrow expects a function')
49 }
50 return async (...args) => {
51 try {
52 return await func(...args)
53 } catch (error) {
54 throw redactError(error)
55 }
56 }
57}
58
59module.exports = { redact, redactError, redactThrow }
60 