@@ -5,20 +5,44 @@ const {
55 hexTable,
66 isHexTable
77} = require ( 'internal/querystring' ) ;
8+
89const { getConstructorOf } = require ( 'internal/util' ) ;
910const errors = require ( 'internal/errors' ) ;
10- const binding = process . binding ( 'url' ) ;
11+ const querystring = require ( 'querystring' ) ;
12+
13+ const { platform } = process ;
14+ const isWindows = platform === 'win32' ;
15+
16+ const {
17+ domainToASCII : _domainToASCII ,
18+ domainToUnicode : _domainToUnicode ,
19+ encodeAuth,
20+ toUSVString : _toUSVString ,
21+ parse : _parse ,
22+ setURLConstructor,
23+ URL_FLAGS_CANNOT_BE_BASE ,
24+ URL_FLAGS_HAS_FRAGMENT ,
25+ URL_FLAGS_HAS_HOST ,
26+ URL_FLAGS_HAS_PASSWORD ,
27+ URL_FLAGS_HAS_PATH ,
28+ URL_FLAGS_HAS_QUERY ,
29+ URL_FLAGS_HAS_USERNAME ,
30+ URL_FLAGS_SPECIAL ,
31+ kFragment,
32+ kHost,
33+ kHostname,
34+ kPathStart,
35+ kPort,
36+ kQuery,
37+ kSchemeStart
38+ } = process . binding ( 'url' ) ;
39+
1140const context = Symbol ( 'context' ) ;
1241const cannotBeBase = Symbol ( 'cannot-be-base' ) ;
1342const cannotHaveUsernamePasswordPort =
1443 Symbol ( 'cannot-have-username-password-port' ) ;
1544const special = Symbol ( 'special' ) ;
1645const searchParams = Symbol ( 'query' ) ;
17- const querystring = require ( 'querystring' ) ;
18-
19- const { platform } = process ;
20- const isWindows = platform === 'win32' ;
21-
2246const kFormat = Symbol ( 'format' ) ;
2347
2448// https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object
@@ -35,7 +59,7 @@ function toUSVString(val) {
3559 const match = unpairedSurrogateRe . exec ( str ) ;
3660 if ( ! match )
3761 return str ;
38- return binding . toUSVString ( str , match . index ) ;
62+ return _toUSVString ( str , match . index ) ;
3963}
4064
4165// Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque
@@ -74,10 +98,10 @@ function onParseComplete(flags, protocol, username, password,
7498 var ctx = this [ context ] ;
7599 ctx . flags = flags ;
76100 ctx . scheme = protocol ;
77- ctx . username = ( flags & binding . URL_FLAGS_HAS_USERNAME ) !== 0 ? username : '' ;
78- ctx . password = ( flags & binding . URL_FLAGS_HAS_PASSWORD ) !== 0 ? password : '' ;
101+ ctx . username = ( flags & URL_FLAGS_HAS_USERNAME ) !== 0 ? username : '' ;
102+ ctx . password = ( flags & URL_FLAGS_HAS_PASSWORD ) !== 0 ? password : '' ;
79103 ctx . port = port ;
80- ctx . path = ( flags & binding . URL_FLAGS_HAS_PATH ) !== 0 ? path : [ ] ;
104+ ctx . path = ( flags & URL_FLAGS_HAS_PATH ) !== 0 ? path : [ ] ;
81105 ctx . query = query ;
82106 ctx . fragment = fragment ;
83107 ctx . host = host ;
@@ -98,31 +122,30 @@ function onParseError(flags, input) {
98122function parse ( url , input , base ) {
99123 const base_context = base ? base [ context ] : undefined ;
100124 url [ context ] = new URLContext ( ) ;
101- binding . parse ( input . trim ( ) , - 1 ,
102- base_context , undefined ,
103- onParseComplete . bind ( url ) , onParseError ) ;
125+ _parse ( input . trim ( ) , - 1 , base_context , undefined ,
126+ onParseComplete . bind ( url ) , onParseError ) ;
104127}
105128
106129function onParseProtocolComplete ( flags , protocol , username , password ,
107130 host , port , path , query , fragment ) {
108131 const ctx = this [ context ] ;
109- if ( ( flags & binding . URL_FLAGS_SPECIAL ) !== 0 ) {
110- ctx . flags |= binding . URL_FLAGS_SPECIAL ;
132+ if ( ( flags & URL_FLAGS_SPECIAL ) !== 0 ) {
133+ ctx . flags |= URL_FLAGS_SPECIAL ;
111134 } else {
112- ctx . flags &= ~ binding . URL_FLAGS_SPECIAL ;
135+ ctx . flags &= ~ URL_FLAGS_SPECIAL ;
113136 }
114137 ctx . scheme = protocol ;
115138}
116139
117140function onParseHostComplete ( flags , protocol , username , password ,
118141 host , port , path , query , fragment ) {
119142 const ctx = this [ context ] ;
120- if ( ( flags & binding . URL_FLAGS_HAS_HOST ) !== 0 ) {
143+ if ( ( flags & URL_FLAGS_HAS_HOST ) !== 0 ) {
121144 ctx . host = host ;
122- ctx . flags |= binding . URL_FLAGS_HAS_HOST ;
145+ ctx . flags |= URL_FLAGS_HAS_HOST ;
123146 } else {
124147 ctx . host = null ;
125- ctx . flags &= ~ binding . URL_FLAGS_HAS_HOST ;
148+ ctx . flags &= ~ URL_FLAGS_HAS_HOST ;
126149 }
127150 if ( port !== null )
128151 ctx . port = port ;
@@ -131,12 +154,12 @@ function onParseHostComplete(flags, protocol, username, password,
131154function onParseHostnameComplete ( flags , protocol , username , password ,
132155 host , port , path , query , fragment ) {
133156 const ctx = this [ context ] ;
134- if ( ( flags & binding . URL_FLAGS_HAS_HOST ) !== 0 ) {
157+ if ( ( flags & URL_FLAGS_HAS_HOST ) !== 0 ) {
135158 ctx . host = host ;
136- ctx . flags |= binding . URL_FLAGS_HAS_HOST ;
159+ ctx . flags |= URL_FLAGS_HAS_HOST ;
137160 } else {
138161 ctx . host = null ;
139- ctx . flags &= ~ binding . URL_FLAGS_HAS_HOST ;
162+ ctx . flags &= ~ URL_FLAGS_HAS_HOST ;
140163 }
141164}
142165
@@ -148,18 +171,18 @@ function onParsePortComplete(flags, protocol, username, password,
148171function onParsePathComplete ( flags , protocol , username , password ,
149172 host , port , path , query , fragment ) {
150173 const ctx = this [ context ] ;
151- if ( ( flags & binding . URL_FLAGS_HAS_PATH ) !== 0 ) {
174+ if ( ( flags & URL_FLAGS_HAS_PATH ) !== 0 ) {
152175 ctx . path = path ;
153- ctx . flags |= binding . URL_FLAGS_HAS_PATH ;
176+ ctx . flags |= URL_FLAGS_HAS_PATH ;
154177 } else {
155178 ctx . path = [ ] ;
156- ctx . flags &= ~ binding . URL_FLAGS_HAS_PATH ;
179+ ctx . flags &= ~ URL_FLAGS_HAS_PATH ;
157180 }
158181
159182 // The C++ binding may set host to empty string.
160- if ( ( flags & binding . URL_FLAGS_HAS_HOST ) !== 0 ) {
183+ if ( ( flags & URL_FLAGS_HAS_HOST ) !== 0 ) {
161184 ctx . host = host ;
162- ctx . flags |= binding . URL_FLAGS_HAS_HOST ;
185+ ctx . flags |= URL_FLAGS_HAS_HOST ;
163186 }
164187}
165188
@@ -185,11 +208,11 @@ class URL {
185208 }
186209
187210 get [ special ] ( ) {
188- return ( this [ context ] . flags & binding . URL_FLAGS_SPECIAL ) !== 0 ;
211+ return ( this [ context ] . flags & URL_FLAGS_SPECIAL ) !== 0 ;
189212 }
190213
191214 get [ cannotBeBase ] ( ) {
192- return ( this [ context ] . flags & binding . URL_FLAGS_CANNOT_BE_BASE ) !== 0 ;
215+ return ( this [ context ] . flags & URL_FLAGS_CANNOT_BE_BASE ) !== 0 ;
193216 }
194217
195218 // https://url.spec.whatwg.org/#cannot-have-a-username-password-port
@@ -348,8 +371,8 @@ Object.defineProperties(URL.prototype, {
348371 ( ctx . host === '' || ctx . host === null ) ) {
349372 return ;
350373 }
351- binding . parse ( scheme , binding . kSchemeStart , null , ctx ,
352- onParseProtocolComplete . bind ( this ) ) ;
374+ _parse ( scheme , kSchemeStart , null , ctx ,
375+ onParseProtocolComplete . bind ( this ) ) ;
353376 }
354377 } ,
355378 username : {
@@ -366,11 +389,11 @@ Object.defineProperties(URL.prototype, {
366389 const ctx = this [ context ] ;
367390 if ( username === '' ) {
368391 ctx . username = '' ;
369- ctx . flags &= ~ binding . URL_FLAGS_HAS_USERNAME ;
392+ ctx . flags &= ~ URL_FLAGS_HAS_USERNAME ;
370393 return ;
371394 }
372- ctx . username = binding . encodeAuth ( username ) ;
373- ctx . flags |= binding . URL_FLAGS_HAS_USERNAME ;
395+ ctx . username = encodeAuth ( username ) ;
396+ ctx . flags |= URL_FLAGS_HAS_USERNAME ;
374397 }
375398 } ,
376399 password : {
@@ -387,11 +410,11 @@ Object.defineProperties(URL.prototype, {
387410 const ctx = this [ context ] ;
388411 if ( password === '' ) {
389412 ctx . password = '' ;
390- ctx . flags &= ~ binding . URL_FLAGS_HAS_PASSWORD ;
413+ ctx . flags &= ~ URL_FLAGS_HAS_PASSWORD ;
391414 return ;
392415 }
393- ctx . password = binding . encodeAuth ( password ) ;
394- ctx . flags |= binding . URL_FLAGS_HAS_PASSWORD ;
416+ ctx . password = encodeAuth ( password ) ;
417+ ctx . flags |= URL_FLAGS_HAS_PASSWORD ;
395418 }
396419 } ,
397420 host : {
@@ -412,8 +435,7 @@ Object.defineProperties(URL.prototype, {
412435 // Cannot set the host if cannot-be-base is set
413436 return ;
414437 }
415- binding . parse ( host , binding . kHost , null , ctx ,
416- onParseHostComplete . bind ( this ) ) ;
438+ _parse ( host , kHost , null , ctx , onParseHostComplete . bind ( this ) ) ;
417439 }
418440 } ,
419441 hostname : {
@@ -430,8 +452,7 @@ Object.defineProperties(URL.prototype, {
430452 // Cannot set the host if cannot-be-base is set
431453 return ;
432454 }
433- binding . parse ( host , binding . kHostname , null , ctx ,
434- onParseHostnameComplete . bind ( this ) ) ;
455+ _parse ( host , kHostname , null , ctx , onParseHostnameComplete . bind ( this ) ) ;
435456 }
436457 } ,
437458 port : {
@@ -451,8 +472,7 @@ Object.defineProperties(URL.prototype, {
451472 ctx . port = null ;
452473 return ;
453474 }
454- binding . parse ( port , binding . kPort , null , ctx ,
455- onParsePortComplete . bind ( this ) ) ;
475+ _parse ( port , kPort , null , ctx , onParsePortComplete . bind ( this ) ) ;
456476 }
457477 } ,
458478 pathname : {
@@ -471,8 +491,8 @@ Object.defineProperties(URL.prototype, {
471491 path = `${ path } ` ;
472492 if ( this [ cannotBeBase ] )
473493 return ;
474- binding . parse ( path , binding . kPathStart , null , this [ context ] ,
475- onParsePathComplete . bind ( this ) ) ;
494+ _parse ( path , kPathStart , null , this [ context ] ,
495+ onParsePathComplete . bind ( this ) ) ;
476496 }
477497 } ,
478498 search : {
@@ -489,14 +509,13 @@ Object.defineProperties(URL.prototype, {
489509 search = toUSVString ( search ) ;
490510 if ( search === '' ) {
491511 ctx . query = null ;
492- ctx . flags &= ~ binding . URL_FLAGS_HAS_QUERY ;
512+ ctx . flags &= ~ URL_FLAGS_HAS_QUERY ;
493513 } else {
494514 if ( search [ 0 ] === '?' ) search = search . slice ( 1 ) ;
495515 ctx . query = '' ;
496- ctx . flags |= binding . URL_FLAGS_HAS_QUERY ;
516+ ctx . flags |= URL_FLAGS_HAS_QUERY ;
497517 if ( search ) {
498- binding . parse ( search , binding . kQuery , null , ctx ,
499- onParseSearchComplete . bind ( this ) ) ;
518+ _parse ( search , kQuery , null , ctx , onParseSearchComplete . bind ( this ) ) ;
500519 }
501520 }
502521 initSearchParams ( this [ searchParams ] , search ) ;
@@ -524,14 +543,13 @@ Object.defineProperties(URL.prototype, {
524543 hash = `${ hash } ` ;
525544 if ( ! hash ) {
526545 ctx . fragment = null ;
527- ctx . flags &= ~ binding . URL_FLAGS_HAS_FRAGMENT ;
546+ ctx . flags &= ~ URL_FLAGS_HAS_FRAGMENT ;
528547 return ;
529548 }
530549 if ( hash [ 0 ] === '#' ) hash = hash . slice ( 1 ) ;
531550 ctx . fragment = '' ;
532- ctx . flags |= binding . URL_FLAGS_HAS_FRAGMENT ;
533- binding . parse ( hash , binding . kFragment , null , ctx ,
534- onParseHashComplete . bind ( this ) ) ;
551+ ctx . flags |= URL_FLAGS_HAS_FRAGMENT ;
552+ _parse ( hash , kFragment , null , ctx , onParseHashComplete . bind ( this ) ) ;
535553 }
536554 } ,
537555 toJSON : {
@@ -553,10 +571,10 @@ function update(url, params) {
553571 const serializedParams = params . toString ( ) ;
554572 if ( serializedParams ) {
555573 ctx . query = serializedParams ;
556- ctx . flags |= binding . URL_FLAGS_HAS_QUERY ;
574+ ctx . flags |= URL_FLAGS_HAS_QUERY ;
557575 } else {
558576 ctx . query = null ;
559- ctx . flags &= ~ binding . URL_FLAGS_HAS_QUERY ;
577+ ctx . flags &= ~ URL_FLAGS_HAS_QUERY ;
560578 }
561579}
562580
@@ -1257,15 +1275,15 @@ function domainToASCII(domain) {
12571275 throw new errors . TypeError ( 'ERR_MISSING_ARGS' , 'domain' ) ;
12581276
12591277 // toUSVString is not needed.
1260- return binding . domainToASCII ( `${ domain } ` ) ;
1278+ return _domainToASCII ( `${ domain } ` ) ;
12611279}
12621280
12631281function domainToUnicode ( domain ) {
12641282 if ( arguments . length < 1 )
12651283 throw new errors . TypeError ( 'ERR_MISSING_ARGS' , 'domain' ) ;
12661284
12671285 // toUSVString is not needed.
1268- return binding . domainToUnicode ( `${ domain } ` ) ;
1286+ return _domainToUnicode ( `${ domain } ` ) ;
12691287}
12701288
12711289// Utility function that converts a URL object into an ordinary
@@ -1365,10 +1383,10 @@ function constructUrl(flags, protocol, username, password,
13651383 var ctx = new URLContext ( ) ;
13661384 ctx . flags = flags ;
13671385 ctx . scheme = protocol ;
1368- ctx . username = ( flags & binding . URL_FLAGS_HAS_USERNAME ) !== 0 ? username : '' ;
1369- ctx . password = ( flags & binding . URL_FLAGS_HAS_PASSWORD ) !== 0 ? password : '' ;
1386+ ctx . username = ( flags & URL_FLAGS_HAS_USERNAME ) !== 0 ? username : '' ;
1387+ ctx . password = ( flags & URL_FLAGS_HAS_PASSWORD ) !== 0 ? password : '' ;
13701388 ctx . port = port ;
1371- ctx . path = ( flags & binding . URL_FLAGS_HAS_PATH ) !== 0 ? path : [ ] ;
1389+ ctx . path = ( flags & URL_FLAGS_HAS_PATH ) !== 0 ? path : [ ] ;
13721390 ctx . query = query ;
13731391 ctx . fragment = fragment ;
13741392 ctx . host = host ;
@@ -1378,7 +1396,7 @@ function constructUrl(flags, protocol, username, password,
13781396 initSearchParams ( url [ searchParams ] , query ) ;
13791397 return url ;
13801398}
1381- binding . setURLConstructor ( constructUrl ) ;
1399+ setURLConstructor ( constructUrl ) ;
13821400
13831401module . exports = {
13841402 toUSVString,
0 commit comments