1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { URL , URLSearchParams } from " whatwg-url" ;
2
+ import { URL , URLSearchParams } from ' whatwg-url' ;
3
3
import {
4
4
redactValidConnectionString ,
5
5
redactConnectionString ,
6
- ConnectionStringRedactionOptions ,
7
- } from " ./redact" ;
6
+ ConnectionStringRedactionOptions
7
+ } from ' ./redact' ;
8
8
export { redactConnectionString , ConnectionStringRedactionOptions } ;
9
9
10
- const DUMMY_HOSTNAME = " __this_is_a_placeholder__" ;
10
+ const DUMMY_HOSTNAME = ' __this_is_a_placeholder__' ;
11
11
12
12
function connectionStringHasValidScheme ( connectionString : string ) {
13
- return (
14
- connectionString . startsWith ( "mongodb://" ) ||
15
- connectionString . startsWith ( "mongodb+srv://" )
16
- ) ;
13
+ return connectionString . startsWith ( 'mongodb://' ) || connectionString . startsWith ( 'mongodb+srv://' ) ;
17
14
}
18
15
19
16
// Adapted from the Node.js driver code:
@@ -50,9 +47,7 @@ class CaseInsensitiveMap<K extends string = string> extends Map<K, string> {
50
47
}
51
48
}
52
49
53
- function caseInsenstiveURLSearchParams < K extends string = string > (
54
- Ctor : typeof URLSearchParams ,
55
- ) {
50
+ function caseInsenstiveURLSearchParams < K extends string = string > ( Ctor : typeof URLSearchParams ) {
56
51
return class CaseInsenstiveURLSearchParams extends Ctor {
57
52
append ( name : K , value : any ) : void {
58
53
return super . append ( this . _normalizeKey ( name ) , value ) ;
@@ -114,7 +109,7 @@ abstract class URLWithoutHost extends URL {
114
109
115
110
class MongoParseError extends Error {
116
111
get name ( ) : string {
117
- return " MongoParseError" ;
112
+ return ' MongoParseError' ;
118
113
}
119
114
}
120
115
@@ -134,7 +129,7 @@ export class ConnectionString extends URLWithoutHost {
134
129
const { looseValidation } = options ;
135
130
if ( ! looseValidation && ! connectionStringHasValidScheme ( uri ) ) {
136
131
throw new MongoParseError (
137
- 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"' ,
132
+ 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'
138
133
) ;
139
134
}
140
135
@@ -147,84 +142,72 @@ export class ConnectionString extends URLWithoutHost {
147
142
148
143
if ( ! looseValidation ) {
149
144
if ( ! protocol || ! hosts ) {
150
- throw new MongoParseError (
151
- `Protocol and host list are required in "${ uri } "` ,
152
- ) ;
145
+ throw new MongoParseError ( `Protocol and host list are required in "${ uri } "` ) ;
153
146
}
154
147
155
148
try {
156
- decodeURIComponent ( username ?? "" ) ;
157
- decodeURIComponent ( password ?? "" ) ;
149
+ decodeURIComponent ( username ?? '' ) ;
150
+ decodeURIComponent ( password ?? '' ) ;
158
151
} catch ( err ) {
159
152
throw new MongoParseError ( ( err as Error ) . message ) ;
160
153
}
161
154
162
155
// characters not permitted in username nor password Set([':', '/', '?', '#', '[', ']', '@'])
163
156
const illegalCharacters = / [: / ? # [ \] @ ] / gi;
164
157
if ( username ?. match ( illegalCharacters ) ) {
165
- throw new MongoParseError (
166
- `Username contains unescaped characters ${ username } ` ,
167
- ) ;
158
+ throw new MongoParseError ( `Username contains unescaped characters ${ username } ` ) ;
168
159
}
169
160
if ( ! username || ! password ) {
170
- const uriWithoutProtocol = uri . replace ( `${ protocol } ://` , "" ) ;
171
- if (
172
- uriWithoutProtocol . startsWith ( "@" ) ||
173
- uriWithoutProtocol . startsWith ( ":" )
174
- ) {
175
- throw new MongoParseError ( "URI contained empty userinfo section" ) ;
161
+ const uriWithoutProtocol = uri . replace ( `${ protocol } ://` , '' ) ;
162
+ if ( uriWithoutProtocol . startsWith ( '@' ) || uriWithoutProtocol . startsWith ( ':' ) ) {
163
+ throw new MongoParseError ( 'URI contained empty userinfo section' ) ;
176
164
}
177
165
}
178
166
179
167
if ( password ?. match ( illegalCharacters ) ) {
180
- throw new MongoParseError ( " Password contains unescaped characters" ) ;
168
+ throw new MongoParseError ( ' Password contains unescaped characters' ) ;
181
169
}
182
170
}
183
171
184
- let authString = "" ;
185
- if ( typeof username === " string" ) authString += username ;
186
- if ( typeof password === " string" ) authString += `:${ password } ` ;
187
- if ( authString ) authString += "@" ;
172
+ let authString = '' ;
173
+ if ( typeof username === ' string' ) authString += username ;
174
+ if ( typeof password === ' string' ) authString += `:${ password } ` ;
175
+ if ( authString ) authString += '@' ;
188
176
189
177
try {
190
- super (
191
- `${ protocol . toLowerCase ( ) } ://${ authString } ${ DUMMY_HOSTNAME } ${ rest } ` ,
192
- ) ;
178
+ super ( `${ protocol . toLowerCase ( ) } ://${ authString } ${ DUMMY_HOSTNAME } ${ rest } ` ) ;
193
179
} catch ( err : any ) {
194
180
if ( looseValidation ) {
195
181
// Call the constructor again, this time with loose validation off,
196
182
// for a better error message
197
183
// eslint-disable-next-line no-new
198
184
new ConnectionString ( uri , {
199
185
...options ,
200
- looseValidation : false ,
186
+ looseValidation : false
201
187
} ) ;
202
188
}
203
- if ( typeof err . message === " string" ) {
189
+ if ( typeof err . message === ' string' ) {
204
190
err . message = err . message . replace ( DUMMY_HOSTNAME , hosts ) ;
205
191
}
206
192
throw err ;
207
193
}
208
- this . _hosts = hosts . split ( "," ) ;
194
+ this . _hosts = hosts . split ( ',' ) ;
209
195
210
196
if ( ! looseValidation ) {
211
197
if ( this . isSRV && this . hosts . length !== 1 ) {
212
- throw new MongoParseError (
213
- "mongodb+srv URI cannot have multiple service names" ,
214
- ) ;
198
+ throw new MongoParseError ( 'mongodb+srv URI cannot have multiple service names' ) ;
215
199
}
216
- if ( this . isSRV && this . hosts . some ( ( host ) => host . includes ( ":" ) ) ) {
217
- throw new MongoParseError ( " mongodb+srv URI cannot have port number" ) ;
200
+ if ( this . isSRV && this . hosts . some ( host => host . includes ( ':' ) ) ) {
201
+ throw new MongoParseError ( ' mongodb+srv URI cannot have port number' ) ;
218
202
}
219
203
}
220
204
221
205
if ( ! this . pathname ) {
222
- this . pathname = "/" ;
206
+ this . pathname = '/' ;
223
207
}
224
208
Object . setPrototypeOf (
225
209
this . searchParams ,
226
- caseInsenstiveURLSearchParams ( this . searchParams . constructor as any )
227
- . prototype ,
210
+ caseInsenstiveURLSearchParams ( this . searchParams . constructor as any ) . prototype
228
211
) ;
229
212
}
230
213
@@ -235,29 +218,29 @@ export class ConnectionString extends URLWithoutHost {
235
218
return DUMMY_HOSTNAME as never ;
236
219
}
237
220
set host ( _ignored : never ) {
238
- throw new Error ( " No single host for connection string" ) ;
221
+ throw new Error ( ' No single host for connection string' ) ;
239
222
}
240
223
get hostname ( ) : never {
241
224
return DUMMY_HOSTNAME as never ;
242
225
}
243
226
set hostname ( _ignored : never ) {
244
- throw new Error ( " No single host for connection string" ) ;
227
+ throw new Error ( ' No single host for connection string' ) ;
245
228
}
246
229
get port ( ) : never {
247
- return "" as never ;
230
+ return '' as never ;
248
231
}
249
232
set port ( _ignored : never ) {
250
- throw new Error ( " No single host for connection string" ) ;
233
+ throw new Error ( ' No single host for connection string' ) ;
251
234
}
252
235
get href ( ) : string {
253
236
return this . toString ( ) ;
254
237
}
255
238
set href ( _ignored : string ) {
256
- throw new Error ( " Cannot set href for connection strings" ) ;
239
+ throw new Error ( ' Cannot set href for connection strings' ) ;
257
240
}
258
241
259
242
get isSRV ( ) : boolean {
260
- return this . protocol . includes ( " srv" ) ;
243
+ return this . protocol . includes ( ' srv' ) ;
261
244
}
262
245
263
246
get hosts ( ) : string [ ] {
@@ -269,12 +252,12 @@ export class ConnectionString extends URLWithoutHost {
269
252
}
270
253
271
254
toString ( ) : string {
272
- return super . toString ( ) . replace ( DUMMY_HOSTNAME , this . hosts . join ( "," ) ) ;
255
+ return super . toString ( ) . replace ( DUMMY_HOSTNAME , this . hosts . join ( ',' ) ) ;
273
256
}
274
257
275
258
clone ( ) : ConnectionString {
276
259
return new ConnectionString ( this . toString ( ) , {
277
- looseValidation : true ,
260
+ looseValidation : true
278
261
} ) ;
279
262
}
280
263
@@ -284,12 +267,11 @@ export class ConnectionString extends URLWithoutHost {
284
267
285
268
typedSearchParams < T extends Record < string , any > > ( ) {
286
269
const _sametype =
287
- ( false as true ) &&
288
- new ( caseInsenstiveURLSearchParams < keyof T & string > ( URLSearchParams ) ) ( ) ;
270
+ ( false as true ) && new ( caseInsenstiveURLSearchParams < keyof T & string > ( URLSearchParams ) ) ( ) ;
289
271
return this . searchParams as unknown as typeof _sametype ;
290
272
}
291
273
292
- [ Symbol . for ( " nodejs.util.inspect.custom" ) ] ( ) : any {
274
+ [ Symbol . for ( ' nodejs.util.inspect.custom' ) ] ( ) : any {
293
275
const {
294
276
href,
295
277
origin,
@@ -300,7 +282,7 @@ export class ConnectionString extends URLWithoutHost {
300
282
pathname,
301
283
search,
302
284
searchParams,
303
- hash,
285
+ hash
304
286
} = this ;
305
287
return {
306
288
href,
@@ -312,7 +294,7 @@ export class ConnectionString extends URLWithoutHost {
312
294
pathname,
313
295
search,
314
296
searchParams,
315
- hash,
297
+ hash
316
298
} ;
317
299
}
318
300
}
@@ -322,27 +304,24 @@ export class ConnectionString extends URLWithoutHost {
322
304
* readPreferenceTags connection string parameters.
323
305
*/
324
306
export class CommaAndColonSeparatedRecord <
325
- K extends Record < string , unknown > = Record < string , unknown > ,
307
+ K extends Record < string , unknown > = Record < string , unknown >
326
308
> extends CaseInsensitiveMap < keyof K & string > {
327
309
constructor ( from ?: string | null ) {
328
310
super ( ) ;
329
- for ( const entry of ( from ?? "" ) . split ( "," ) ) {
311
+ for ( const entry of ( from ?? '' ) . split ( ',' ) ) {
330
312
if ( ! entry ) continue ;
331
- const colonIndex = entry . indexOf ( ":" ) ;
313
+ const colonIndex = entry . indexOf ( ':' ) ;
332
314
// Use .set() to properly account for case insensitivity
333
315
if ( colonIndex === - 1 ) {
334
- this . set ( entry as keyof K & string , "" ) ;
316
+ this . set ( entry as keyof K & string , '' ) ;
335
317
} else {
336
- this . set (
337
- entry . slice ( 0 , colonIndex ) as keyof K & string ,
338
- entry . slice ( colonIndex + 1 ) ,
339
- ) ;
318
+ this . set ( entry . slice ( 0 , colonIndex ) as keyof K & string , entry . slice ( colonIndex + 1 ) ) ;
340
319
}
341
320
}
342
321
}
343
322
344
323
toString ( ) : string {
345
- return [ ...this ] . map ( ( entry ) => entry . join ( ":" ) ) . join ( "," ) ;
324
+ return [ ...this ] . map ( entry => entry . join ( ':' ) ) . join ( ',' ) ;
346
325
}
347
326
}
348
327
0 commit comments