Skip to content

Commit ecd1ebf

Browse files
committed
same ID replacements for same IDs
1 parent e721038 commit ecd1ebf

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,26 @@ function addCommaAfterEachLine(data: string): string {
5050
return jsonDataWithCommas.join('\n');
5151
}
5252

53+
let idCounter = 1;
54+
const idMap = new Map();
55+
5356
function recursivelyReplaceData(obj: any) {
5457
for (let key in obj) {
5558
if (typeof obj[key] === 'string' && isDateLikeString(obj[key])) {
5659
obj[key] = `[[ISODateString]]`;
5760
} else if (key.includes('timestamp')) {
5861
obj[key] = `[[timestamp]]`;
59-
} else if (key.includes('_id')) {
60-
obj[key] = `[[ID]]`;
6162
} else if (typeof obj[key] === 'number' && obj[key] > 1000) {
6263
obj[key] = `[[highNumber]]`;
64+
} else if (key.includes('_id')) {
65+
if (idMap.has(obj[key])) {
66+
// give the same ID replacement to the same value
67+
obj[key] = idMap.get(obj[key]);
68+
} else {
69+
const newId = `[[ID${idCounter++}]]`;
70+
idMap.set(obj[key], newId);
71+
obj[key] = newId;
72+
}
6373
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
6474
recursivelyReplaceData(obj[key]);
6575
}
@@ -97,12 +107,14 @@ async function transformSavedJSON() {
97107
const url = objWithReq.request.url;
98108
const filepath = `payload-files/${extractPathFromUrl(url)}.json`;
99109

100-
await writeFile(filepath, JSON.stringify(transformedJSON, null, 2));
101-
console.log(`Successfully modified timestamp in ${filepath}`);
102-
}
110+
writeFile(filepath, JSON.stringify(transformedJSON, null, 2)).then(() => {
111+
console.log(`Successfully replaced data and saved file in ${filepath}`);
103112

104-
await unlink(TEMPORARY_FILE_PATH);
105-
console.log(`Successfully deleted ${TEMPORARY_FILE_PATH}`);
113+
unlink(TEMPORARY_FILE_PATH).then(() =>
114+
console.log(`Successfully deleted ${TEMPORARY_FILE_PATH}`),
115+
);
116+
});
117+
}
106118
} catch (err) {
107119
console.error('Error', err);
108120
}
@@ -135,15 +147,14 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
135147
? zlib.gunzipSync(Buffer.concat(proxyRequestChunks)).toString()
136148
: Buffer.concat(proxyRequestChunks).toString();
137149

138-
fs.writeFile(TEMPORARY_FILE_PATH, `[${proxyRequestBody}]`, err => {
139-
if (err) {
140-
console.error(`Error writing file ${TEMPORARY_FILE_PATH}`, err);
141-
} else {
142-
console.log(`Successfully wrote to ${TEMPORARY_FILE_PATH}`);
143-
}
144-
});
145-
146-
transformSavedJSON();
150+
// save the JSON payload into a file
151+
try {
152+
writeFile(TEMPORARY_FILE_PATH, `[${proxyRequestBody}]`).then(() => {
153+
transformSavedJSON();
154+
});
155+
} catch (err) {
156+
console.error(`Error writing file ${TEMPORARY_FILE_PATH}`, err);
157+
}
147158

148159
const envelopeHeader: EnvelopeItem[0] = JSON.parse(proxyRequestBody.split('\n')[0]);
149160

0 commit comments

Comments
 (0)