123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.createAnonymizer = createAnonymizer;
- const set_js_1 = __importDefault(require("../utils/lodash/set.cjs"));
- function extractStringNodes(data, options) {
- const parsedOptions = { ...options, maxDepth: options.maxDepth ?? 10 };
- const queue = [
- [data, 0, ""],
- ];
- const result = [];
- while (queue.length > 0) {
- const task = queue.shift();
- if (task == null)
- continue;
- const [value, depth, path] = task;
- if (typeof value === "object" && value != null) {
- if (depth >= parsedOptions.maxDepth)
- continue;
- for (const [key, nestedValue] of Object.entries(value)) {
- queue.push([nestedValue, depth + 1, path ? `${path}.${key}` : key]);
- }
- }
- else if (Array.isArray(value)) {
- if (depth >= parsedOptions.maxDepth)
- continue;
- for (let i = 0; i < value.length; i++) {
- queue.push([value[i], depth + 1, `${path}[${i}]`]);
- }
- }
- else if (typeof value === "string") {
- result.push({ value, path });
- }
- }
- return result;
- }
- function deepClone(data) {
- return JSON.parse(JSON.stringify(data));
- }
- function createAnonymizer(replacer, options) {
- return (data) => {
- let mutateValue = deepClone(data);
- const nodes = extractStringNodes(mutateValue, {
- maxDepth: options?.maxDepth,
- });
- const processor = Array.isArray(replacer)
- ? (() => {
- const replacers = replacer.map(({ pattern, type, replace }) => {
- if (type != null && type !== "pattern")
- throw new Error("Invalid anonymizer type");
- return [
- typeof pattern === "string"
- ? new RegExp(pattern, "g")
- : pattern,
- replace ?? "[redacted]",
- ];
- });
- if (replacers.length === 0)
- throw new Error("No replacers provided");
- return {
- maskNodes: (nodes) => {
- return nodes.reduce((memo, item) => {
- const newValue = replacers.reduce((value, [regex, replace]) => {
- const result = value.replace(regex, replace);
- // make sure we reset the state of regex
- regex.lastIndex = 0;
- return result;
- }, item.value);
- if (newValue !== item.value) {
- memo.push({ value: newValue, path: item.path });
- }
- return memo;
- }, []);
- },
- };
- })()
- : typeof replacer === "function"
- ? {
- maskNodes: (nodes) => nodes.reduce((memo, item) => {
- const newValue = replacer(item.value, item.path);
- if (newValue !== item.value) {
- memo.push({ value: newValue, path: item.path });
- }
- return memo;
- }, []),
- }
- : replacer;
- const toUpdate = processor.maskNodes(nodes);
- for (const node of toUpdate) {
- if (node.path === "") {
- mutateValue = node.value;
- }
- else {
- (0, set_js_1.default)(mutateValue, node.path, node.value);
- }
- }
- return mutateValue;
- };
- }
|