Proxy server to inject shim (#178)
things to test: - [x] allow real URL to open in new window - [x] packaging in electron? - [ ] does it work on windows? - [x] make sure it works with older apps - [x] what about cache / reuse? - maybe use a bigger range of ports??
This commit is contained in:
48
src/ipc/utils/port_utils.ts
Normal file
48
src/ipc/utils/port_utils.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import net from "net";
|
||||
|
||||
export function findAvailablePort(
|
||||
minPort: number,
|
||||
maxPort: number,
|
||||
): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let attempts = 0;
|
||||
const maxAttempts = 3;
|
||||
|
||||
function tryPort() {
|
||||
if (attempts >= maxAttempts) {
|
||||
reject(
|
||||
new Error(
|
||||
`Failed to find an available port after ${maxAttempts} attempts.`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
attempts++;
|
||||
const port =
|
||||
Math.floor(Math.random() * (maxPort - minPort + 1)) + minPort;
|
||||
const server = net.createServer();
|
||||
|
||||
server.once("error", (err: any) => {
|
||||
if (err.code === "EADDRINUSE") {
|
||||
// Port is in use, try another one
|
||||
console.log(`Port ${port} is in use, trying another...`);
|
||||
server.close(() => tryPort());
|
||||
} else {
|
||||
// Other error
|
||||
server.close(() => reject(err));
|
||||
}
|
||||
});
|
||||
|
||||
server.once("listening", () => {
|
||||
server.close(() => {
|
||||
resolve(port);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, "localhost");
|
||||
}
|
||||
|
||||
tryPort();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user