Skip to content

Commit 9090f79

Browse files
author
Luca Forstner
committed
Extract server port registration into helper function
1 parent aadeb29 commit 9090f79

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/e2e-tests/test-utils/event-proxy-server.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import * as https from 'https';
66
import type { AddressInfo } from 'net';
77
import * as os from 'os';
88
import * as path from 'path';
9+
import * as util from 'util';
10+
11+
const readFile = util.promisify(fs.readFile);
12+
const writeFile = util.promisify(fs.writeFile);
913

1014
interface EventProxyServerOptions {
1115
/** Port to start the event proxy server at. */
@@ -123,9 +127,7 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
123127
const eventCallbackServerStartupPromise = new Promise<void>(resolve => {
124128
const listener = eventCallbackServer.listen(0, () => {
125129
const port = String((listener.address() as AddressInfo).port);
126-
const tmpFileWithPort = path.join(os.tmpdir(), `event-proxy-server-${options.proxyServerName}`);
127-
fs.writeFileSync(tmpFileWithPort, port, { encoding: 'utf8' });
128-
resolve();
130+
void registerCallbackServerPort(options.proxyServerName, port).then(resolve);
129131
});
130132
});
131133

@@ -134,12 +136,11 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
134136
return;
135137
}
136138

137-
export function waitForRequest(
139+
export async function waitForRequest(
138140
proxyServerName: string,
139141
callback: (eventData: SentryRequestCallbackData) => boolean,
140142
): Promise<SentryRequestCallbackData> {
141-
const tmpFileWithPort = path.join(os.tmpdir(), `event-proxy-server-${proxyServerName}`);
142-
const eventCallbackServerPort = fs.readFileSync(tmpFileWithPort, 'utf8');
143+
const eventCallbackServerPort = await retrieveCallbackServerPort(proxyServerName);
143144

144145
return new Promise<SentryRequestCallbackData>((resolve, reject) => {
145146
const request = http.request(`http://localhost:${eventCallbackServerPort}/`, {}, response => {
@@ -218,3 +219,15 @@ export function waitForTransaction(
218219
}).catch(reject);
219220
});
220221
}
222+
223+
const TEMP_FILE_PREFIX = 'event-proxy-server-';
224+
225+
async function registerCallbackServerPort(serverName: string, port: string): Promise<void> {
226+
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
227+
await writeFile(tmpFilePath, port, { encoding: 'utf8' });
228+
}
229+
230+
async function retrieveCallbackServerPort(serverName: string): Promise<string> {
231+
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
232+
return await readFile(tmpFilePath, 'utf8');
233+
}

0 commit comments

Comments
 (0)