I try create url from object and new URL from package url
const URL = require('url').URL
const mongoURLObj = {
protocol: 'mongodb',
password: MONGODB_APPLICATION_PASS,
username: MONGODB_APPLICATION_USER,
hostname: MONGODB_HOST,
port: MONGODB_EXPOSED_PORT,
pathname: MONGODB_APPLICATION_DATABASE,
searchParams: {},
}
const mongoURL = Objest.assign(new URL('http://test.com'), mongoURLObj) // mongoURL.href === 'http://test.com'
and anyway get http://test.com.
Image from node repl

I try some case in browser and i got what i expected.

url.prototcol = 'mongodb'; doesnβt work, so itβs probably not related to Object.assign. (I guess the updated issue title means that @dimensi reached the same conclusion.)
@nodejs/url
I think it's behaving correctly from a spec perspective. http is considered a "special scheme" and can only be replaced with another special scheme:



@targos Yea, i read this spec and now understand why not working like i expected. This bug in ff and chrome then?
@targos Yeah, that's what I told the author in a chat ;-).
Interestingly though, I don't think that section 4.1 (URL representation) is being followed correctly.
scheme host
domain IPv4 IPv6 opaque empty null
non-"file" special β
β
β
β β β
"file" β
β
β
β β
β
non-special β β β
β
β
β
I.e. in the case of non-special URLs, new URL('foobar:127.0.0.1') and new URL('foobar:[::1]') (or new URL('foobar://127.0.0.1') / new URL('foobar://[::1]') β not sure whiich exact form is correct there) don't seem to work consistently in browsers and Node, and to my understanding, each of them does something incorrectly according to the spec.
Any relations to generic URI syntax ?
Re: my comment above.
That is, according the the spec, IPv4 and IPv6 should work differently for non-special URLs β there shouldn't be IPv4 hosts there, but IPv6 is allowed.
But both Node.js and Chrome seem to treat IPv4 and IPv6 equally there, but the behaviour differs between Node.js and Chrome.
Node.js seems to allow both IPv4 and IPv6 as host part, and Chrome seems to disallow both of them and puts them into pathname instead.
@ChALkeR Node.js' implementation is to the spec. The IPv4 cases for non-special are grouped into "opaque hosts"; see host parser. The note is just that β an informative note β and the actual normative requirements are embedded in the algorithms in host parser.
So yes, it is a bug in Firefox and Chrome that they are not implementing the current URL Standard.
Most helpful comment
I think it's behaving correctly from a spec perspective.
httpis considered a "special scheme" and can only be replaced with another special scheme: