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:
Will Chen
2025-05-16 23:28:26 -07:00
committed by GitHub
parent 63e41454c7
commit 5966dd7f4b
15 changed files with 563 additions and 158 deletions

View 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();
});
}