create-url.js 670 B

1234567891011121314151617
  1. import { URL } from 'url';
  2. export function createUrl({ protocol, host, port, path }) {
  3. // wrap IPv6 host in brackets
  4. const ipv6Host = host?.includes(':') ? `[${host}]` : host;
  5. // use fallback values to create a valid URL (protocol: 'undefined:', host: '[::]')
  6. // nock v13 issue: protocol and host are undefined (https://github.com/chimurai/http-proxy-middleware/issues/1035)
  7. // nock v14+ seems to return protocol and host correctly
  8. const base = `${protocol || 'undefined:'}//${ipv6Host || '[::]'}`;
  9. const url = new URL(base);
  10. if (port) {
  11. url.port = port;
  12. }
  13. if (path) {
  14. url.pathname = path;
  15. }
  16. return url;
  17. }