Skip to content

Commit 8639883

Browse files
committed
recurse arrays; fix ID increment
1 parent a148e61 commit 8639883

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

utils/event-proxy-server/src/event-proxy-server.ts

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,83 @@ function addCommaAfterEachLine(data: string): string {
4949
return jsonDataWithCommas.join('\n');
5050
}
5151

52-
function recursivelyReplaceData(obj: any, idCounter: number, idMap: Map<string, string>) {
53-
for (let key in obj) {
54-
if (typeof obj[key] === 'string' && isDateLikeString(obj[key])) {
55-
obj[key] = `[[ISODateString]]`;
56-
} else if (key.includes('timestamp')) {
57-
obj[key] = `[[timestamp]]`;
58-
} else if (typeof obj[key] === 'number' && obj[key] > 1000) {
59-
obj[key] = `[[highNumber]]`;
60-
} else if (key === 'if-none-match') {
61-
if (obj[key].startsWith('W/')) {
62-
obj[key] = `[[W/entityTagValue]]`;
63-
} else {
64-
obj[key] = `[[entityTagValue]]`;
52+
// has to be an object to be able to pass by reference. Otherwise, the counter would not increase
53+
type Counter = {
54+
value: number;
55+
};
56+
57+
function recursivelyReplaceData(
58+
obj: any,
59+
idCounter: Counter,
60+
idMap: Map<string, string>,
61+
filenameCounter: Counter,
62+
filenameMap: Map<string, string>,
63+
) {
64+
if (Array.isArray(obj)) {
65+
// some values are arrays with objects in it
66+
obj.forEach((item, index) => {
67+
if (typeof item === 'object' && item !== null) {
68+
recursivelyReplaceData(item, idCounter, idMap, filenameCounter, filenameMap);
6569
}
66-
} else if (key.includes('_id')) {
67-
if (idMap.has(obj[key])) {
68-
// give the same ID replacement to the same value
69-
obj[key] = idMap.get(obj[key]);
70-
} else {
71-
const newId = `[[ID${idCounter++}]]`;
72-
idMap.set(obj[key], newId);
73-
obj[key] = newId;
70+
});
71+
} else {
72+
for (let key in obj) {
73+
if (typeof obj[key] === 'string' && isDateLikeString(obj[key])) {
74+
obj[key] = `[[ISODateString]]`;
75+
} else if (key.includes('timestamp')) {
76+
obj[key] = `[[timestamp]]`;
77+
} else if (key.includes('timestamp')) {
78+
obj[key] = `[[timestamp]]`;
79+
} else if (typeof obj[key] === 'number' && obj[key] > 1000) {
80+
obj[key] = `[[highNumber]]`;
81+
} else if (key === 'if-none-match') {
82+
if (obj[key].startsWith('W/')) {
83+
obj[key] = `[[W/entityTagValue]]`;
84+
} else {
85+
obj[key] = `[[entityTagValue]]`;
86+
}
87+
} else if (key === 'user-agent') {
88+
obj[key] = `[[user-agent]]`;
89+
} else if (key.includes('_id')) {
90+
if (idMap.has(obj[key])) {
91+
// give the same ID replacement to the same value
92+
obj[key] = idMap.get(obj[key]);
93+
} else {
94+
const newId = `[[ID${idCounter.value++}]]`;
95+
idMap.set(obj[key], newId);
96+
obj[key] = newId;
97+
}
98+
} else if (key === 'filename') {
99+
if (filenameMap.has(obj[key])) {
100+
// give the same ID replacement to the same value
101+
obj[key] = filenameMap.get(obj[key]);
102+
} else {
103+
const newId = `[[FILENAME${filenameCounter.value++}]]`;
104+
filenameMap.set(obj[key], newId);
105+
obj[key] = newId;
106+
}
107+
}
108+
// recurse into object or array
109+
else if (typeof obj[key] === 'object' && obj[key] !== null) {
110+
recursivelyReplaceData(obj[key], idCounter, idMap, filenameCounter, filenameMap);
74111
}
75-
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
76-
recursivelyReplaceData(obj[key], idCounter, idMap);
77112
}
78113
}
79114
}
80115

81116
function replaceDynamicValues(data: string): string[] {
82117
const jsonData = JSON.parse(data);
83118

84-
recursivelyReplaceData(jsonData, 1, new Map());
119+
recursivelyReplaceData(jsonData, { value: 1 }, new Map(), { value: 1 }, new Map());
85120

86121
// change remaining dynamic values
87122
jsonData.forEach((item: any) => {
88123
if (item.trace?.public_key) {
89124
item.trace.public_key = '[[publicKey]]';
90125
}
126+
if (item.dsn) {
127+
item.dsn = '[[dsn]]';
128+
}
91129
});
92130

93131
return jsonData;
@@ -103,13 +141,15 @@ async function transformSavedJSON() {
103141

104142
const jsonData = addCommaAfterEachLine(data);
105143
const transformedJSON = replaceDynamicValues(jsonData);
144+
106145
const objWithReq = transformedJSON[2] as unknown as { request: { url: string } };
146+
const type = (transformedJSON[1] as unknown as { type: string }).type;
107147

108148
if ('request' in objWithReq) {
109149
const url = objWithReq.request.url;
110150
const replaceForwardSlashes = (str: string) => str.split('/').join('_');
111151

112-
const filepath = `payload-files/${APP}/${replaceForwardSlashes(extractPathFromUrl(url))}.json`;
152+
const filepath = `payload-files/${APP}/${replaceForwardSlashes(extractPathFromUrl(url))}--${type}.json`;
113153

114154
writeFile(filepath, JSON.stringify(transformedJSON, null, 2)).then(() => {
115155
console.log(`Successfully replaced data and saved file in ${filepath}`);

0 commit comments

Comments
 (0)