Node: Can't set mongodb protocol to URL

Created on 20 Apr 2018  Β·  7Comments  Β·  Source: nodejs/node

  • Version: v9.11.1
  • Platform: Linux 4.14.34-1-MANJARO #1 SMP PREEMPT Thu Apr 12 17:26:43 UTC 2018 x86_64 GNU/Linux
  • Subsystem:

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
image

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

whatwg-url

Most helpful comment

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:

image

image

image

All 7 comments

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:

image

image

image

@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.

Was this page helpful?
0 / 5 - 0 ratings