7
7
ArrayPrototypePush,
8
8
ArrayPrototypeReduce,
9
9
ArrayPrototypeSlice,
10
+ Error,
10
11
FunctionPrototypeBind,
11
12
Int8Array,
12
13
Number,
@@ -29,6 +30,7 @@ const {
29
30
Symbol,
30
31
SymbolIterator,
31
32
SymbolToStringTag,
33
+ TypeError,
32
34
decodeURIComponent,
33
35
} = primordials ;
34
36
@@ -118,6 +120,8 @@ const cannotHaveUsernamePasswordPort =
118
120
const special = Symbol ( 'special' ) ;
119
121
const searchParams = Symbol ( 'query' ) ;
120
122
const kFormat = Symbol ( 'format' ) ;
123
+ const initURLSearchParamsFromRecord =
124
+ Symbol ( 'init-url-search-params-from-record' ) ;
121
125
122
126
let blob ;
123
127
let cryptoRandom ;
@@ -174,6 +178,20 @@ function isURLSearchParams(self) {
174
178
return self && self [ searchParams ] && ! self [ searchParams ] [ searchParams ] ;
175
179
}
176
180
181
+ // WPT needs Error in URLSearchParams' constructor exactly be an instance of
182
+ // TypeError.
183
+ function throwTypeError ( message ) {
184
+ if ( message instanceof Error ) {
185
+ const err = new TypeError ( message . message ) ; // eslint-disable-line
186
+ err . stack = message . stack ;
187
+ if ( message . code ) err . code = message . code ;
188
+ if ( message . name ) err . name = message . name ;
189
+ throw err ;
190
+ }
191
+
192
+ throw new TypeError ( message ) ; // eslint-disable-line
193
+ }
194
+
177
195
class URLSearchParams {
178
196
// URL Standard says the default value is '', but as undefined and '' have
179
197
// the same result, undefined is used to prevent unnecessary parsing.
@@ -191,7 +209,7 @@ class URLSearchParams {
191
209
this [ searchParams ] = childParams . slice ( ) ;
192
210
} else if ( method !== null && method !== undefined ) {
193
211
if ( typeof method !== 'function' ) {
194
- throw new ERR_ARG_NOT_ITERABLE ( 'Query pairs' ) ;
212
+ throwTypeError ( new ERR_ARG_NOT_ITERABLE ( 'Query pairs' ) ) ;
195
213
}
196
214
197
215
// Sequence<sequence<USVString>>
@@ -201,7 +219,8 @@ class URLSearchParams {
201
219
if ( ( typeof pair !== 'object' && typeof pair !== 'function' ) ||
202
220
pair === null ||
203
221
typeof pair [ SymbolIterator ] !== 'function' ) {
204
- throw new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ;
222
+ throwTypeError (
223
+ new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ) ;
205
224
}
206
225
const convertedPair = [ ] ;
207
226
for ( const element of pair )
@@ -217,18 +236,10 @@ class URLSearchParams {
217
236
ArrayPrototypePush ( this [ searchParams ] , pair [ 0 ] , pair [ 1 ] ) ;
218
237
}
219
238
} else {
220
- // Record<USVString, USVString>
221
- // Need to use reflection APIs for full spec compliance.
222
- this [ searchParams ] = [ ] ;
223
- const keys = ReflectOwnKeys ( init ) ;
224
- for ( let i = 0 ; i < keys . length ; i ++ ) {
225
- const key = keys [ i ] ;
226
- const desc = ReflectGetOwnPropertyDescriptor ( init , key ) ;
227
- if ( desc !== undefined && desc . enumerable ) {
228
- const typedKey = toUSVString ( key ) ;
229
- const typedValue = toUSVString ( init [ key ] ) ;
230
- this [ searchParams ] . push ( typedKey , typedValue ) ;
231
- }
239
+ try {
240
+ this [ initURLSearchParamsFromRecord ] ( init ) ;
241
+ } catch ( e ) {
242
+ throwTypeError ( e ) ;
232
243
}
233
244
}
234
245
} else {
@@ -242,6 +253,22 @@ class URLSearchParams {
242
253
this [ context ] = null ;
243
254
}
244
255
256
+ [ initURLSearchParamsFromRecord ] ( init ) {
257
+ // Record<USVString, USVString>
258
+ // Need to use reflection APIs for full spec compliance.
259
+ this [ searchParams ] = [ ] ;
260
+ const keys = ReflectOwnKeys ( init ) ;
261
+ for ( let i = 0 ; i < keys . length ; i ++ ) {
262
+ const key = keys [ i ] ;
263
+ const desc = ReflectGetOwnPropertyDescriptor ( init , key ) ;
264
+ if ( desc !== undefined && desc . enumerable ) {
265
+ const typedKey = toUSVString ( key ) ;
266
+ const typedValue = toUSVString ( init [ key ] ) ;
267
+ this [ searchParams ] . push ( typedKey , typedValue ) ;
268
+ }
269
+ }
270
+ }
271
+
245
272
[ inspect . custom ] ( recurseTimes , ctx ) {
246
273
if ( ! isURLSearchParams ( this ) )
247
274
throw new ERR_INVALID_THIS ( 'URLSearchParams' ) ;
0 commit comments