-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
56 lines (46 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const validator = require('./lib/validator')
const parse = require('./lib/parse')
const redactor = require('./lib/redactor')
const restorer = require('./lib/restorer')
const { groupRedact, nestedRedact } = require('./lib/modifiers')
const state = require('./lib/state')
const rx = require('./lib/rx')
const validate = validator()
const noop = (o) => o
noop.restore = noop
const DEFAULT_CENSOR = '[REDACTED]'
fastRedact.rx = rx
fastRedact.validator = validator
module.exports = fastRedact
function fastRedact (opts = {}) {
const paths = Array.from(new Set(opts.paths || []))
const serialize = 'serialize' in opts ? (
opts.serialize === false ? opts.serialize
: (typeof opts.serialize === 'function' ? opts.serialize : JSON.stringify)
) : JSON.stringify
const remove = opts.remove
if (remove === true && serialize !== JSON.stringify) {
throw Error('fast-redact – remove option may only be set when serializer is JSON.stringify')
}
const censor = remove === true
? undefined
: 'censor' in opts ? opts.censor : DEFAULT_CENSOR
const isCensorFct = typeof censor === 'function'
const censorFctTakesPath = isCensorFct && censor.length > 1
if (paths.length === 0) return serialize || noop
validate({ paths, serialize, censor })
const { wildcards, wcLen, secret } = parse({ paths, censor })
const compileRestore = restorer()
const strict = 'strict' in opts ? opts.strict : true
return redactor({ secret, wcLen, serialize, strict, isCensorFct, censorFctTakesPath }, state({
secret,
censor,
compileRestore,
serialize,
groupRedact,
nestedRedact,
wildcards,
wcLen
}))
}