@@ -10,8 +10,8 @@ function importPunycode() {
1010
1111const { toASCII } = importPunycode ( ) ;
1212
13+ const { StorageObject, hexTable } = require ( 'internal/querystring' ) ;
1314const internalUrl = require ( 'internal/url' ) ;
14- const encodeAuth = internalUrl . encodeAuth ;
1515exports . parse = urlParse ;
1616exports . resolve = urlResolve ;
1717exports . resolveObject = urlResolveObject ;
@@ -76,12 +76,6 @@ const slashedProtocol = {
7676} ;
7777const querystring = require ( 'querystring' ) ;
7878
79- // This constructor is used to store parsed query string values. Instantiating
80- // this is faster than explicitly calling `Object.create(null)` to get a
81- // "clean" empty object (tested with v8 v4.9).
82- function ParsedQueryString ( ) { }
83- ParsedQueryString . prototype = Object . create ( null ) ;
84-
8579function urlParse ( url , parseQueryString , slashesDenoteHost ) {
8680 if ( url instanceof Url ) return url ;
8781
@@ -190,7 +184,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
190184 }
191185 } else if ( parseQueryString ) {
192186 this . search = '' ;
193- this . query = new ParsedQueryString ( ) ;
187+ this . query = new StorageObject ( ) ;
194188 }
195189 return this ;
196190 }
@@ -380,7 +374,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
380374 } else if ( parseQueryString ) {
381375 // no query string, but parseQueryString still requested
382376 this . search = '' ;
383- this . query = new ParsedQueryString ( ) ;
377+ this . query = new StorageObject ( ) ;
384378 }
385379
386380 var firstIdx = ( questionIdx !== - 1 &&
@@ -959,3 +953,75 @@ function spliceOne(list, index) {
959953 list [ i ] = list [ k ] ;
960954 list . pop ( ) ;
961955}
956+
957+ // These characters do not need escaping:
958+ // ! - . _ ~
959+ // ' ( ) * :
960+ // digits
961+ // alpha (uppercase)
962+ // alpha (lowercase)
963+ const noEscapeAuth = [
964+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 0x00 - 0x0F
965+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 0x10 - 0x1F
966+ 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , // 0x20 - 0x2F
967+ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , // 0x30 - 0x3F
968+ 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // 0x40 - 0x4F
969+ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , // 0x50 - 0x5F
970+ 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // 0x60 - 0x6F
971+ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 // 0x70 - 0x7F
972+ ] ;
973+
974+ function encodeAuth ( str ) {
975+ // faster encodeURIComponent alternative for encoding auth uri components
976+ var out = '' ;
977+ var lastPos = 0 ;
978+ for ( var i = 0 ; i < str . length ; ++ i ) {
979+ var c = str . charCodeAt ( i ) ;
980+
981+ // ASCII
982+ if ( c < 0x80 ) {
983+ if ( noEscapeAuth [ c ] === 1 )
984+ continue ;
985+ if ( lastPos < i )
986+ out += str . slice ( lastPos , i ) ;
987+ lastPos = i + 1 ;
988+ out += hexTable [ c ] ;
989+ continue ;
990+ }
991+
992+ if ( lastPos < i )
993+ out += str . slice ( lastPos , i ) ;
994+
995+ // Multi-byte characters ...
996+ if ( c < 0x800 ) {
997+ lastPos = i + 1 ;
998+ out += hexTable [ 0xC0 | ( c >> 6 ) ] + hexTable [ 0x80 | ( c & 0x3F ) ] ;
999+ continue ;
1000+ }
1001+ if ( c < 0xD800 || c >= 0xE000 ) {
1002+ lastPos = i + 1 ;
1003+ out += hexTable [ 0xE0 | ( c >> 12 ) ] +
1004+ hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ] +
1005+ hexTable [ 0x80 | ( c & 0x3F ) ] ;
1006+ continue ;
1007+ }
1008+ // Surrogate pair
1009+ ++ i ;
1010+ var c2 ;
1011+ if ( i < str . length )
1012+ c2 = str . charCodeAt ( i ) & 0x3FF ;
1013+ else
1014+ c2 = 0 ;
1015+ lastPos = i + 1 ;
1016+ c = 0x10000 + ( ( ( c & 0x3FF ) << 10 ) | c2 ) ;
1017+ out += hexTable [ 0xF0 | ( c >> 18 ) ] +
1018+ hexTable [ 0x80 | ( ( c >> 12 ) & 0x3F ) ] +
1019+ hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ] +
1020+ hexTable [ 0x80 | ( c & 0x3F ) ] ;
1021+ }
1022+ if ( lastPos === 0 )
1023+ return str ;
1024+ if ( lastPos < str . length )
1025+ return out + str . slice ( lastPos ) ;
1026+ return out ;
1027+ }
0 commit comments