Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please un-deprecate url.resolve #37492

Closed
evert opened this issue Feb 23, 2021 · 4 comments · Fixed by #37501
Closed

Please un-deprecate url.resolve #37492

evert opened this issue Feb 23, 2021 · 4 comments · Fixed by #37501

Comments

@evert
Copy link

evert commented Feb 23, 2021

What steps will reproduce the bug?

We use url.resolve a LOT. It's a great function that also inspired me to write a PHP version with 8 million downloads =)

I recently discovered that url.resolve() was marked as deprecated, and that users should use URL instead.

Aside from the fact that WhatWG URL is not a real RFC3986 uri, there's also a a very simple feature missing. We use url.resolve all the time to do things like this:

const newUrl = url.resolve(
  '/base'
  '?foo=bar'
);

The URL object does not let us work with relative urls:

> new URL('?foo=bar', '/bar');
Uncaught TypeError [ERR_INVALID_URL]: Invalid URL: /bar
    at onParseError (internal/url.js:259:9)
    at new URL (internal/url.js:335:5)
    at new URL (internal/url.js:332:22)
    at REPL11:1:1
    at Script.runInThisContext (vm.js:133:18)
    at REPLServer.defaultEval (repl.js:484:29)
    at bound (domain.js:413:15)
    at REPLServer.runBound [as eval] (domain.js:424:12)
    at REPLServer.onLine (repl.js:817:10)
    at REPLServer.emit (events.js:327:22) {
  input: '/bar',
  code: 'ERR_INVALID_URL'
@jasnell
Copy link
Member

jasnell commented Feb 23, 2021

The challenge is not that URL does not support relative URLs, it's that it is designed to work for more than just HTTP URLs and needs to know the protocol to properly resolve the URL (since different URL protocol schemes use different resolve and serialization algorithms). You can achieve what you're looking for by appending a prefix to the /bar, then ignoring that prefix in the result:

const { pathname, search } = new URL('?foo=bar', 'http://example/bar');
console.log(pathname + search);  // Outputs: /bar?foo=bar

@evert
Copy link
Author

evert commented Feb 23, 2021

Interesting, that makes sense! It's just tricky to handle. I work a lot on HATEOAS-style systems, where people just generally want to use relative urls.

What I need is some way for the origin to be preserved if it was supplied in any of the 2 components, but removed if both were relative. I could solve this by supplying a domain that will never be registered, but it's a bit clumsy.

I do appreciate the reasoning here though, so feel free to close,

@jasnell
Copy link
Member

jasnell commented Feb 23, 2021

The example.org/example.com domains are safe to use. They are officially registered by IANA/IETF and used for examples in IETF standards.

What I need is some way for the origin to be preserved if it was supplied in any of the 2 components, but removed if both were relative.

The way to handle this is to use two resolves:

function resolve(path, base) {
  const baseUrl = new URL(base, 'http://example.com');
  const { pathname, search } = new URL(path, baseUrl);
  return pathname + search;
}

resolve('?foo=bar', '/bar');
resolve('?foo=bar', 'https://example.org/bar');

aduh95 added a commit to aduh95/node that referenced this issue Feb 24, 2021
aduh95 added a commit to aduh95/node that referenced this issue Feb 24, 2021
aduh95 added a commit to aduh95/node that referenced this issue Feb 24, 2021
@aduh95
Copy link
Contributor

aduh95 commented Feb 24, 2021

PR to add the suggested replacement function: #37501

@aduh95 aduh95 closed this as completed in 5d240c5 Feb 26, 2021
targos pushed a commit that referenced this issue Feb 28, 2021
Fixes: #37492

PR-URL: #37501
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
targos pushed a commit that referenced this issue May 1, 2021
Fixes: #37492

PR-URL: #37501
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants