UNPKG

http-proxy-middleware

Version:

The one-liner node.js proxy middleware for connect, express, next.js and more

18 lines (17 loc) 670 B
import { URL } from 'url'; export function createUrl({ protocol, host, port, path }) { // wrap IPv6 host in brackets const ipv6Host = host?.includes(':') ? `[${host}]` : host; // use fallback values to create a valid URL (protocol: 'undefined:', host: '[::]') // nock v13 issue: protocol and host are undefined (https://github.com/chimurai/http-proxy-middleware/issues/1035) // nock v14+ seems to return protocol and host correctly const base = `${protocol || 'undefined:'}//${ipv6Host || '[::]'}`; const url = new URL(base); if (port) { url.port = port; } if (path) { url.pathname = path; } return url; }