0

I have a little builder class that does url manipulation from a URL object, using the legacy node require('url') however now i am trying to migrate it to use the new WHATWG URL class.

In my builder class i had a method called removeProtocol() which does nothing more than removing the protocol so we can have convert to string urls like test.com/path/go/somewhere as opposed to https://test.com/path/go/somewhere

While the new class works much better than the legacy one. It does not accept the protocol to be an empty string, keeping it with previous value.

I tried things like:

delete this.url.protocol => this was ignored
delete this.url.__proto__.protocol => also results in the url being printed with the protocol.

removing the protocol from the href property also causes an error.

TypeError: Invalid URL: test.com/path/go/somewhere

Is there a way to do with this class. or does it mean I will have to convert the object to a string a remove it the protocol from the string?

3
  • 1
    Yes, you have to convert it to a string yourself. Notice that an URL instance represents a resolved, absolute URL. See also github.com/whatwg/url/issues/421 Commented Apr 29, 2020 at 18:02
  • Regarding your attempt, the docs say: "In accordance with browser conventions, all properties of URL objects are implemented as getters and setters on the class prototype, rather than as data properties on the object itself. Thus, unlike legacy urlObjects, using the delete keyword on any properties of URL objects (e.g. delete myURL.protocol, delete myURL.pathname, etc) has no effect but will still return true." Commented Apr 29, 2020 at 18:04
  • @Bergi thank you for the replies. deleting the properties were silly desperate attempts, tbh. I did read the spec as, that having a url in the constructor without a protocol constitutes an invalid url, i was surprised that they made sure that all properties were validating it also (as the previous url object in node didnt care at all. Thats for the issue link though i hadnt seen it in my searches. Commented Apr 29, 2020 at 18:24

1 Answer 1

2

The scheme (a.k.a. protocol) is a necessary part of a valid URL, and the WHATWG URL object represents a valid URL.

So, to answer your question: No, it's not possible to have a WHATWG URL object without a protocol.

Whai is possible is to extend the URL class:

class ProtocollessURL extends URL {
  constructor(plURL, base) {
    super("http://" + plURL, base);
  }
  toString() {
    return this.host + this.pathname + this.search + this.hash;
  }
}

const url = new ProtocollessURL("google.com/search?q=hello+world#something");

console.log("Host: %s", url.host);
console.log('"URL": %s', url);

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for this. i dont get why on earth i didnt think of it. you sir just rocked my day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.