index.cjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.createAnonymizer = createAnonymizer;
  7. const set_js_1 = __importDefault(require("../utils/lodash/set.cjs"));
  8. function extractStringNodes(data, options) {
  9. const parsedOptions = { ...options, maxDepth: options.maxDepth ?? 10 };
  10. const queue = [
  11. [data, 0, ""],
  12. ];
  13. const result = [];
  14. while (queue.length > 0) {
  15. const task = queue.shift();
  16. if (task == null)
  17. continue;
  18. const [value, depth, path] = task;
  19. if (typeof value === "object" && value != null) {
  20. if (depth >= parsedOptions.maxDepth)
  21. continue;
  22. for (const [key, nestedValue] of Object.entries(value)) {
  23. queue.push([nestedValue, depth + 1, path ? `${path}.${key}` : key]);
  24. }
  25. }
  26. else if (Array.isArray(value)) {
  27. if (depth >= parsedOptions.maxDepth)
  28. continue;
  29. for (let i = 0; i < value.length; i++) {
  30. queue.push([value[i], depth + 1, `${path}[${i}]`]);
  31. }
  32. }
  33. else if (typeof value === "string") {
  34. result.push({ value, path });
  35. }
  36. }
  37. return result;
  38. }
  39. function deepClone(data) {
  40. return JSON.parse(JSON.stringify(data));
  41. }
  42. function createAnonymizer(replacer, options) {
  43. return (data) => {
  44. let mutateValue = deepClone(data);
  45. const nodes = extractStringNodes(mutateValue, {
  46. maxDepth: options?.maxDepth,
  47. });
  48. const processor = Array.isArray(replacer)
  49. ? (() => {
  50. const replacers = replacer.map(({ pattern, type, replace }) => {
  51. if (type != null && type !== "pattern")
  52. throw new Error("Invalid anonymizer type");
  53. return [
  54. typeof pattern === "string"
  55. ? new RegExp(pattern, "g")
  56. : pattern,
  57. replace ?? "[redacted]",
  58. ];
  59. });
  60. if (replacers.length === 0)
  61. throw new Error("No replacers provided");
  62. return {
  63. maskNodes: (nodes) => {
  64. return nodes.reduce((memo, item) => {
  65. const newValue = replacers.reduce((value, [regex, replace]) => {
  66. const result = value.replace(regex, replace);
  67. // make sure we reset the state of regex
  68. regex.lastIndex = 0;
  69. return result;
  70. }, item.value);
  71. if (newValue !== item.value) {
  72. memo.push({ value: newValue, path: item.path });
  73. }
  74. return memo;
  75. }, []);
  76. },
  77. };
  78. })()
  79. : typeof replacer === "function"
  80. ? {
  81. maskNodes: (nodes) => nodes.reduce((memo, item) => {
  82. const newValue = replacer(item.value, item.path);
  83. if (newValue !== item.value) {
  84. memo.push({ value: newValue, path: item.path });
  85. }
  86. return memo;
  87. }, []),
  88. }
  89. : replacer;
  90. const toUpdate = processor.maskNodes(nodes);
  91. for (const node of toUpdate) {
  92. if (node.path === "") {
  93. mutateValue = node.value;
  94. }
  95. else {
  96. (0, set_js_1.default)(mutateValue, node.path, node.value);
  97. }
  98. }
  99. return mutateValue;
  100. };
  101. }