@@ -90,6 +90,112 @@ class URLContext {
9090 }
9191}
9292
93+ class URLSearchParams {
94+ // URL Standard says the default value is '', but as undefined and '' have
95+ // the same result, undefined is used to prevent unnecessary parsing.
96+ // Default parameter is necessary to keep URLSearchParams.length === 0 in
97+ // accordance with Web IDL spec.
98+ constructor ( init = undefined ) {
99+ if ( init === null || init === undefined ) {
100+ this [ searchParams ] = [ ] ;
101+ } else if ( ( typeof init === 'object' && init !== null ) ||
102+ typeof init === 'function' ) {
103+ const method = init [ Symbol . iterator ] ;
104+ if ( method === this [ Symbol . iterator ] ) {
105+ // While the spec does not have this branch, we can use it as a
106+ // shortcut to avoid having to go through the costly generic iterator.
107+ const childParams = init [ searchParams ] ;
108+ this [ searchParams ] = childParams . slice ( ) ;
109+ } else if ( method !== null && method !== undefined ) {
110+ if ( typeof method !== 'function' ) {
111+ throw new errors . TypeError ( 'ERR_ARG_NOT_ITERABLE' , 'Query pairs' ) ;
112+ }
113+
114+ // sequence<sequence<USVString>>
115+ // Note: per spec we have to first exhaust the lists then process them
116+ const pairs = [ ] ;
117+ for ( const pair of init ) {
118+ if ( ( typeof pair !== 'object' && typeof pair !== 'function' ) ||
119+ pair === null ||
120+ typeof pair [ Symbol . iterator ] !== 'function' ) {
121+ throw new errors . TypeError ( 'ERR_INVALID_TUPLE' , 'Each query pair' ,
122+ '[name, value]' ) ;
123+ }
124+ const convertedPair = [ ] ;
125+ for ( const element of pair )
126+ convertedPair . push ( toUSVString ( element ) ) ;
127+ pairs . push ( convertedPair ) ;
128+ }
129+
130+ this [ searchParams ] = [ ] ;
131+ for ( const pair of pairs ) {
132+ if ( pair . length !== 2 ) {
133+ throw new errors . TypeError ( 'ERR_INVALID_TUPLE' , 'Each query pair' ,
134+ '[name, value]' ) ;
135+ }
136+ this [ searchParams ] . push ( pair [ 0 ] , pair [ 1 ] ) ;
137+ }
138+ } else {
139+ // record<USVString, USVString>
140+ // Need to use reflection APIs for full spec compliance.
141+ this [ searchParams ] = [ ] ;
142+ const keys = Reflect . ownKeys ( init ) ;
143+ for ( var i = 0 ; i < keys . length ; i ++ ) {
144+ const key = keys [ i ] ;
145+ const desc = Reflect . getOwnPropertyDescriptor ( init , key ) ;
146+ if ( desc !== undefined && desc . enumerable ) {
147+ const typedKey = toUSVString ( key ) ;
148+ const typedValue = toUSVString ( init [ key ] ) ;
149+ this [ searchParams ] . push ( typedKey , typedValue ) ;
150+ }
151+ }
152+ }
153+ } else {
154+ // USVString
155+ init = toUSVString ( init ) ;
156+ if ( init [ 0 ] === '?' ) init = init . slice ( 1 ) ;
157+ initSearchParams ( this , init ) ;
158+ }
159+
160+ // "associated url object"
161+ this [ context ] = null ;
162+ }
163+
164+ [ util . inspect . custom ] ( recurseTimes , ctx ) {
165+ if ( ! this || ! this [ searchParams ] || this [ searchParams ] [ searchParams ] ) {
166+ throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'URLSearchParams' ) ;
167+ }
168+
169+ if ( typeof recurseTimes === 'number' && recurseTimes < 0 )
170+ return ctx . stylize ( '[Object]' , 'special' ) ;
171+
172+ var separator = ', ' ;
173+ var innerOpts = Object . assign ( { } , ctx ) ;
174+ if ( recurseTimes !== null ) {
175+ innerOpts . depth = recurseTimes - 1 ;
176+ }
177+ var innerInspect = ( v ) => util . inspect ( v , innerOpts ) ;
178+
179+ var list = this [ searchParams ] ;
180+ var output = [ ] ;
181+ for ( var i = 0 ; i < list . length ; i += 2 )
182+ output . push ( `${ innerInspect ( list [ i ] ) } => ${ innerInspect ( list [ i + 1 ] ) } ` ) ;
183+
184+ var colorRe = / \u001b \[ \d \d ? m / g;
185+ var length = output . reduce (
186+ ( prev , cur ) => prev + cur . replace ( colorRe , '' ) . length + separator . length ,
187+ - separator . length
188+ ) ;
189+ if ( length > ctx . breakLength ) {
190+ return `${ this . constructor . name } {\n ${ output . join ( ',\n ' ) } }` ;
191+ } else if ( output . length ) {
192+ return `${ this . constructor . name } { ${ output . join ( separator ) } }` ;
193+ } else {
194+ return `${ this . constructor . name } {}` ;
195+ }
196+ }
197+ }
198+
93199function onParseComplete ( flags , protocol , username , password ,
94200 host , port , path , query , fragment ) {
95201 var ctx = this [ context ] ;
@@ -806,112 +912,6 @@ function defineIDLClass(proto, classStr, obj) {
806912 }
807913}
808914
809- class URLSearchParams {
810- // URL Standard says the default value is '', but as undefined and '' have
811- // the same result, undefined is used to prevent unnecessary parsing.
812- // Default parameter is necessary to keep URLSearchParams.length === 0 in
813- // accordance with Web IDL spec.
814- constructor ( init = undefined ) {
815- if ( init === null || init === undefined ) {
816- this [ searchParams ] = [ ] ;
817- } else if ( ( typeof init === 'object' && init !== null ) ||
818- typeof init === 'function' ) {
819- const method = init [ Symbol . iterator ] ;
820- if ( method === this [ Symbol . iterator ] ) {
821- // While the spec does not have this branch, we can use it as a
822- // shortcut to avoid having to go through the costly generic iterator.
823- const childParams = init [ searchParams ] ;
824- this [ searchParams ] = childParams . slice ( ) ;
825- } else if ( method !== null && method !== undefined ) {
826- if ( typeof method !== 'function' ) {
827- throw new errors . TypeError ( 'ERR_ARG_NOT_ITERABLE' , 'Query pairs' ) ;
828- }
829-
830- // sequence<sequence<USVString>>
831- // Note: per spec we have to first exhaust the lists then process them
832- const pairs = [ ] ;
833- for ( const pair of init ) {
834- if ( ( typeof pair !== 'object' && typeof pair !== 'function' ) ||
835- pair === null ||
836- typeof pair [ Symbol . iterator ] !== 'function' ) {
837- throw new errors . TypeError ( 'ERR_INVALID_TUPLE' , 'Each query pair' ,
838- '[name, value]' ) ;
839- }
840- const convertedPair = [ ] ;
841- for ( const element of pair )
842- convertedPair . push ( toUSVString ( element ) ) ;
843- pairs . push ( convertedPair ) ;
844- }
845-
846- this [ searchParams ] = [ ] ;
847- for ( const pair of pairs ) {
848- if ( pair . length !== 2 ) {
849- throw new errors . TypeError ( 'ERR_INVALID_TUPLE' , 'Each query pair' ,
850- '[name, value]' ) ;
851- }
852- this [ searchParams ] . push ( pair [ 0 ] , pair [ 1 ] ) ;
853- }
854- } else {
855- // record<USVString, USVString>
856- // Need to use reflection APIs for full spec compliance.
857- this [ searchParams ] = [ ] ;
858- const keys = Reflect . ownKeys ( init ) ;
859- for ( var i = 0 ; i < keys . length ; i ++ ) {
860- const key = keys [ i ] ;
861- const desc = Reflect . getOwnPropertyDescriptor ( init , key ) ;
862- if ( desc !== undefined && desc . enumerable ) {
863- const typedKey = toUSVString ( key ) ;
864- const typedValue = toUSVString ( init [ key ] ) ;
865- this [ searchParams ] . push ( typedKey , typedValue ) ;
866- }
867- }
868- }
869- } else {
870- // USVString
871- init = toUSVString ( init ) ;
872- if ( init [ 0 ] === '?' ) init = init . slice ( 1 ) ;
873- initSearchParams ( this , init ) ;
874- }
875-
876- // "associated url object"
877- this [ context ] = null ;
878- }
879-
880- [ util . inspect . custom ] ( recurseTimes , ctx ) {
881- if ( ! this || ! this [ searchParams ] || this [ searchParams ] [ searchParams ] ) {
882- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'URLSearchParams' ) ;
883- }
884-
885- if ( typeof recurseTimes === 'number' && recurseTimes < 0 )
886- return ctx . stylize ( '[Object]' , 'special' ) ;
887-
888- var separator = ', ' ;
889- var innerOpts = Object . assign ( { } , ctx ) ;
890- if ( recurseTimes !== null ) {
891- innerOpts . depth = recurseTimes - 1 ;
892- }
893- var innerInspect = ( v ) => util . inspect ( v , innerOpts ) ;
894-
895- var list = this [ searchParams ] ;
896- var output = [ ] ;
897- for ( var i = 0 ; i < list . length ; i += 2 )
898- output . push ( `${ innerInspect ( list [ i ] ) } => ${ innerInspect ( list [ i + 1 ] ) } ` ) ;
899-
900- var colorRe = / \u001b \[ \d \d ? m / g;
901- var length = output . reduce (
902- ( prev , cur ) => prev + cur . replace ( colorRe , '' ) . length + separator . length ,
903- - separator . length
904- ) ;
905- if ( length > ctx . breakLength ) {
906- return `${ this . constructor . name } {\n ${ output . join ( ',\n ' ) } }` ;
907- } else if ( output . length ) {
908- return `${ this . constructor . name } { ${ output . join ( separator ) } }` ;
909- } else {
910- return `${ this . constructor . name } {}` ;
911- }
912- }
913- }
914-
915915// for merge sort
916916function merge ( out , start , mid , end , lBuffer , rBuffer ) {
917917 const sizeLeft = mid - start ;
0 commit comments