szmarczak / http2-wrapper

Use HTTP/2 the same way like HTTP/1
MIT License
239 stars 18 forks source link

URL's urlToHttpOptions is only available in Node v15+ #73

Closed KyleAMathews closed 3 years ago

KyleAMathews commented 3 years ago

https://nodejs.org/api/url.html#url_url_urltohttpoptions_url

Not sure if that was intentional. Engines in the package.json says you support Node 10 on? Gatsby supports Node 12+ so otherwise we can't use this https://github.com/gatsbyjs/gatsby/issues/32043

Is there an NPM package that provides the same util function? Or maybe just copy it into the src here.

szmarczak commented 3 years ago

That's correct. It says Node 10 just not to break packages running Got v11. I highly do not recommend running HTTP/2 on Node.js < 15. It's buggy AF.

szmarczak commented 3 years ago

Generally you can check process.versions.node to check the Node.js version and pass http2wrapper only when Node.js >= 15

KyleAMathews commented 3 years ago

haha, very fair. Cool, I'll try that 👍

szmarczak commented 3 years ago

Alternatively you can do this and it will work in Node.js < 15: https://runkit.com/szmarczak/60ec8a90668942001af59348

require('url').urlToHttpOptions = function urlToHttpOptions(url) {
  const options = {
    protocol: url.protocol,
    hostname: typeof url.hostname === 'string' &&
              String.prototype.startsWith.call(url.hostname, '[') ?
      String.prototype.slice.call(url.hostname, 1, -1) :
      url.hostname,
    hash: url.hash,
    search: url.search,
    pathname: url.pathname,
    path: `${url.pathname || ''}${url.search || ''}`,
    href: url.href
  };
  if (url.port !== '') {
    options.port = Number(url.port);
  }
  if (url.username || url.password) {
    options.auth = `${url.username}:${url.password}`;
  }
  return options;
};