got
Version:
Human-friendly and powerful HTTP request library for Node.js
25 lines (21 loc) • 580 B
JavaScript
export default function isUnixSocketUrl(url) {
return url.protocol === 'unix:' || url.hostname === 'unix';
}
/**
Extract the socket path from a UNIX socket URL.
@example
```
getUnixSocketPath(new URL('http://unix/foo:/path'));
//=> '/foo'
getUnixSocketPath(new URL('unix:/foo:/path'));
//=> '/foo'
getUnixSocketPath(new URL('http://example.com'));
//=> undefined
```
*/
export function getUnixSocketPath(url) {
if (!isUnixSocketUrl(url)) {
return undefined;
}
return /^(?<socketPath>[^:]+):/v.exec(`${url.pathname}${url.search}`)?.groups?.socketPath;
}