Skip to content

Commit e721038

Browse files
committed
await file writes
1 parent d53f17c commit e721038

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import * as zlib from 'zlib';
99
import type { Envelope, EnvelopeItem } from '@sentry/types';
1010
import { parseEnvelope } from '@sentry/utils';
1111

12+
const readFile = util.promisify(fs.readFile);
1213
const writeFile = util.promisify(fs.writeFile);
14+
const unlink = util.promisify(fs.unlink);
1315

1416
interface EventProxyServerOptions {
1517
/** Port to start the event proxy server at. */
@@ -83,12 +85,9 @@ function replaceDynamicValues(data: string): string[] {
8385
* The new content is saved into a new file with the url as the filename.
8486
* The temporary file is deleted in the end.
8587
*/
86-
function transformSavedJSON() {
87-
fs.readFile(TEMPORARY_FILE_PATH, 'utf8', (err, data) => {
88-
if (err) {
89-
console.error('Error reading file', err);
90-
return;
91-
}
88+
async function transformSavedJSON() {
89+
try {
90+
const data = await readFile(TEMPORARY_FILE_PATH, 'utf8');
9291

9392
const jsonData = addCommaAfterEachLine(data);
9493
const transformedJSON = replaceDynamicValues(jsonData);
@@ -98,23 +97,15 @@ function transformSavedJSON() {
9897
const url = objWithReq.request.url;
9998
const filepath = `payload-files/${extractPathFromUrl(url)}.json`;
10099

101-
fs.writeFile(filepath, JSON.stringify(transformedJSON, null, 2), err => {
102-
if (err) {
103-
console.error('Error writing file', err);
104-
} else {
105-
console.log(`Successfully modified timestamp in ${filepath}`);
106-
}
107-
});
100+
await writeFile(filepath, JSON.stringify(transformedJSON, null, 2));
101+
console.log(`Successfully modified timestamp in ${filepath}`);
108102
}
109-
});
110103

111-
fs.unlink(TEMPORARY_FILE_PATH, err => {
112-
if (err) {
113-
console.error('Error deleting file', err);
114-
} else {
115-
console.log(`Successfully deleted ${TEMPORARY_FILE_PATH}`);
116-
}
117-
});
104+
await unlink(TEMPORARY_FILE_PATH);
105+
console.log(`Successfully deleted ${TEMPORARY_FILE_PATH}`);
106+
} catch (err) {
107+
console.error('Error', err);
108+
}
118109
}
119110

120111
/**

0 commit comments

Comments
 (0)