swagger-js icon indicating copy to clipboard operation
swagger-js copied to clipboard

replace node:url with native url?

Open jimmywarting opened this issue 4 years ago • 3 comments

https://github.com/swagger-api/swagger-js/blob/ba0db610cfbd6b716206e2eefe8b46760606b30c/src/index.js#L86-L102

jimmywarting avatar Oct 14 '21 10:10 jimmywarting

Yeah, don't see a reason why not.

char0n avatar Oct 14 '21 12:10 char0n

I was triaging this issue a bit and came into conclusion that replacing the url Node.js module with WHATWG API will not be that easy.

url.resolve can be replaced with following function, that's no brainer.

// WHATWG URL API replacement for url.resolve function
export function urlResolve(from, to) {
  const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
  if (resolvedUrl.protocol === 'resolve:') {
    // `from` is a relative URL.
    const { pathname, search, hash } = resolvedUrl;
    return pathname + search + hash;
  }
  return resolvedUrl.toString();
}

But url.parse(str) cannot be easily replaced with new URL(str) as the URL constructor expects valid URL as the first parameter and throws error when provided argument is not valid URL.

char0n avatar Dec 14 '21 15:12 char0n

Found an inconsistency in WHATWG URL parsing. Before this inconsistency is addressed, let's put this issue on hold.

Inconsistency described here: https://github.com/whatwg/url/issues/677

char0n avatar Dec 14 '21 16:12 char0n

url package have been removed in https://github.com/swagger-api/swagger-js/pull/3137

Codebase now contains helper function for parsing URI References that looks like this:

/**
 * `parseURIReference` function simulates the behavior of `node:url` parse function.
 * New WHATWG URL API is not capable of parsing relative references natively,
 * but can be adapter by utilizing the `base` parameter.
 */
const parseURIReference = (uriReference) => {
  try {
    return new URL(uriReference);
  } catch {
    const parsedURL = new URL(uriReference, 'https://swagger.io');
    const pathname = String(uriReference).startsWith('/')
      ? parsedURL.pathname
      : parsedURL.pathname.substring(1);

    return {
      hash: parsedURL.hash,
      host: '',
      hostname: '',
      href: '',
      origin: '',
      password: '',
      pathname,
      port: '',
      protocol: '',
      search: parsedURL.search,
      searchParams: parsedURL.searchParams,
    };
  }
};

char0n avatar Sep 12 '23 09:09 char0n

:tada: This issue has been resolved in version 3.21.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

swagger-bot avatar Sep 12 '23 10:09 swagger-bot