Skip to content

Commit 9a5d5d8

Browse files
author
Victoria Hall
committed
cleaner implementation
1 parent e035023 commit 9a5d5d8

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/http/httpProxy.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,26 @@ export async function setupHttpProxy(): Promise<string> {
113113
workerSystemLog('error', `Http proxy error: ${err.stack || err.message}`);
114114
});
115115

116-
server.listen(0, () => {
117-
workerSystemLog('debug', `VICTORIA: auto-assigned port: ${server.address().port}`);
118-
119-
// If port is still 0, find and bind to an open port
120-
if (server.address().port === 0) {
116+
server.listen(() => {
117+
const address = server.address();
118+
if (address !== null && address.port === 0) {
119+
// Auto-assigned port is 0, find and bind to an open port
121120
workerSystemLog('debug', `VICTORIA: Port 0 assigned. Finding open port.`);
122121
findOpenPort(51929, (openPort) => {
123122
workerSystemLog('debug', `VICTORIA: found open port: ${openPort}`);
124-
server.close(); // Close the server
123+
// Close the server and re-listen on the found open port
124+
server.close();
125125
server.listen(openPort, () => {
126126
workerSystemLog('debug', `VICTORIA: server is now listening on found open port: ${openPort}`);
127127
});
128128
resolve(`http://localhost:${openPort}/`);
129129
});
130+
} else if (address !== null && typeof address === 'object') {
131+
// Auto-assigned port is not 0
132+
workerSystemLog('debug', `VICTORIA: auto-assigned port is valid. Port: ${address.port}`);
133+
resolve(`http://localhost:${address.port}/`);
130134
} else {
131-
resolve(`http://localhost:${server.address().port}/`);
135+
reject(new AzFuncSystemError('Unexpected server address during http proxy setup'));
132136
}
133137
});
134138

@@ -138,27 +142,27 @@ export async function setupHttpProxy(): Promise<string> {
138142
});
139143
}
140144

141-
142145
// Function to find an open port starting from a specified port
143146
function findOpenPort(startingPort, callback) {
144147
const server = net.createServer();
145148

146149
function tryPort(port) {
147-
server.once('error', () => {
148-
// If the port is unavailable, increment and try the next one
149-
tryPort(port + 1);
150-
});
151-
152-
server.once('listening', () => {
153-
const port = server.address().port;
154-
server.close();
155-
callback(port);
156-
});
157-
158-
// Try binding to the given port
159-
server.listen(port);
150+
server.once('error', () => {
151+
// If the port is unavailable, increment and try the next one
152+
tryPort(port + 1);
153+
});
154+
155+
// If the port is available, return it
156+
server.once('listening', () => {
157+
const port = server.address().port;
158+
server.close();
159+
callback(port);
160+
});
161+
162+
// Try binding to the given port
163+
server.listen(port);
160164
}
161165

162166
// Start trying from the specified starting port
163167
tryPort(startingPort);
164-
}
168+
}

0 commit comments

Comments
 (0)