nxapi/src/util/net.ts
2022-05-30 13:16:06 +01:00

12 lines
410 B
TypeScript

import * as net from 'node:net';
export function parseListenAddress(address: string | number) {
const match = ('' + address).match(/^((?:((?:\d+\.){3}\d+)|\[(.*)\]):)?(\d+)$/);
if (!match || (match[1] && !net.isIP(match[2] || match[3]))) throw new Error('Invalid address/port');
const host = match[2] || match[3] || null;
const port = parseInt(match[4]);
return [host, port] as const;
}