56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
|
'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'
|
|||
|
|
|||
|
if (paths.length === 0) return serialize || noop
|
|||
|
|
|||
|
validate({ paths, serialize, censor })
|
|||
|
|
|||
|
const { wildcards, wcLen, secret } = parse({ paths, censor })
|
|||
|
|
|||
|
const compileRestore = restorer({ secret, wcLen })
|
|||
|
const strict = 'strict' in opts ? opts.strict : true
|
|||
|
|
|||
|
return redactor({ secret, wcLen, serialize, strict, isCensorFct }, state({
|
|||
|
secret,
|
|||
|
censor,
|
|||
|
compileRestore,
|
|||
|
serialize,
|
|||
|
groupRedact,
|
|||
|
nestedRedact,
|
|||
|
wildcards,
|
|||
|
wcLen
|
|||
|
}))
|
|||
|
}
|