MCPcopy Index your code
hub / github.com/garrytan/gstack / checkPortAvailable

Function checkPortAvailable

browse/src/server.ts:853–872  ·  view source on GitHub ↗
(port: number, hostname: string = '127.0.0.1')

Source from the content-addressed store, hash-verified

851// in the Node.js polyfill where listen/close are async but the caller
852// expects synchronous bind semantics. See: #486
853function checkPortAvailable(port: number, hostname: string = '127.0.0.1'): Promise<PortCheckResult> {
854 return new Promise((resolve) => {
855 const srv = net.createServer();
856 let settled = false;
857 const finish = (result: PortCheckResult) => {
858 if (settled) return;
859 settled = true;
860 resolve(result);
861 };
862
863 srv.once('error', (err) => finish(normalizePortError(err)));
864 try {
865 srv.listen(port, hostname, () => {
866 srv.close(() => finish({ available: true }));
867 });
868 } catch (err) {
869 finish(normalizePortError(err));
870 }
871 });
872}
873
874function isPortAvailable(port: number, hostname: string = '127.0.0.1'): Promise<boolean> {
875 return checkPortAvailable(port, hostname).then((result) => result.available);

Callers 2

isPortAvailableFunction · 0.85
findPortFunction · 0.85

Calls 3

normalizePortErrorFunction · 0.85
finishFunction · 0.70
closeMethod · 0.65

Tested by

no test coverage detected