1+ /* eslint-disable @typescript-eslint/no-explicit-any */
12import { URL , URLSearchParams } from 'whatwg-url' ;
23import {
34 redactValidConnectionString ,
@@ -9,10 +10,7 @@ export { redactConnectionString, ConnectionStringRedactionOptions };
910const DUMMY_HOSTNAME = '__this_is_a_placeholder__' ;
1011
1112function connectionStringHasValidScheme ( connectionString : string ) {
12- return (
13- connectionString . startsWith ( 'mongodb://' ) ||
14- connectionString . startsWith ( 'mongodb+srv://' )
15- ) ;
13+ return connectionString . startsWith ( 'mongodb://' ) || connectionString . startsWith ( 'mongodb+srv://' ) ;
1614}
1715
1816// Adapted from the Node.js driver code:
@@ -130,7 +128,9 @@ export class ConnectionString extends URLWithoutHost {
130128 constructor ( uri : string , options : ConnectionStringParsingOptions = { } ) {
131129 const { looseValidation } = options ;
132130 if ( ! looseValidation && ! connectionStringHasValidScheme ( uri ) ) {
133- throw new MongoParseError ( 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"' ) ;
131+ throw new MongoParseError (
132+ 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'
133+ ) ;
134134 }
135135
136136 const match = uri . match ( HOSTS_REGEX ) ;
@@ -205,20 +205,39 @@ export class ConnectionString extends URLWithoutHost {
205205 if ( ! this . pathname ) {
206206 this . pathname = '/' ;
207207 }
208- Object . setPrototypeOf ( this . searchParams , caseInsenstiveURLSearchParams ( this . searchParams . constructor as any ) . prototype ) ;
208+ Object . setPrototypeOf (
209+ this . searchParams ,
210+ caseInsenstiveURLSearchParams ( this . searchParams . constructor as any ) . prototype
211+ ) ;
209212 }
210213
211214 // The getters here should throw, but that would break .toString() because of
212215 // https://github.com/nodejs/node/issues/36887. Using 'never' as the type
213216 // should be enough to stop anybody from using them in TypeScript, though.
214- get host ( ) : never { return DUMMY_HOSTNAME as never ; }
215- set host ( _ignored : never ) { throw new Error ( 'No single host for connection string' ) ; }
216- get hostname ( ) : never { return DUMMY_HOSTNAME as never ; }
217- set hostname ( _ignored : never ) { throw new Error ( 'No single host for connection string' ) ; }
218- get port ( ) : never { return '' as never ; }
219- set port ( _ignored : never ) { throw new Error ( 'No single host for connection string' ) ; }
220- get href ( ) : string { return this . toString ( ) ; }
221- set href ( _ignored : string ) { throw new Error ( 'Cannot set href for connection strings' ) ; }
217+ get host ( ) : never {
218+ return DUMMY_HOSTNAME as never ;
219+ }
220+ set host ( _ignored : never ) {
221+ throw new Error ( 'No single host for connection string' ) ;
222+ }
223+ get hostname ( ) : never {
224+ return DUMMY_HOSTNAME as never ;
225+ }
226+ set hostname ( _ignored : never ) {
227+ throw new Error ( 'No single host for connection string' ) ;
228+ }
229+ get port ( ) : never {
230+ return '' as never ;
231+ }
232+ set port ( _ignored : never ) {
233+ throw new Error ( 'No single host for connection string' ) ;
234+ }
235+ get href ( ) : string {
236+ return this . toString ( ) ;
237+ }
238+ set href ( _ignored : string ) {
239+ throw new Error ( 'Cannot set href for connection strings' ) ;
240+ }
222241
223242 get isSRV ( ) : boolean {
224243 return this . protocol . includes ( 'srv' ) ;
@@ -246,34 +265,57 @@ export class ConnectionString extends URLWithoutHost {
246265 return redactValidConnectionString ( this , options ) ;
247266 }
248267
249- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/ban-types
250- typedSearchParams < T extends { } > ( ) {
251- const sametype = ( false as true ) && new ( caseInsenstiveURLSearchParams < keyof T & string > ( URLSearchParams ) ) ( ) ;
252- return this . searchParams as unknown as typeof sametype ;
268+ typedSearchParams < T extends Record < string , any > > ( ) {
269+ const _sametype =
270+ ( false as true ) && new ( caseInsenstiveURLSearchParams < keyof T & string > ( URLSearchParams ) ) ( ) ;
271+ return this . searchParams as unknown as typeof _sametype ;
253272 }
254273
255274 [ Symbol . for ( 'nodejs.util.inspect.custom' ) ] ( ) : any {
256- const { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash } = this ;
257- return { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash } ;
275+ const {
276+ href,
277+ origin,
278+ protocol,
279+ username,
280+ password,
281+ hosts,
282+ pathname,
283+ search,
284+ searchParams,
285+ hash
286+ } = this ;
287+ return {
288+ href,
289+ origin,
290+ protocol,
291+ username,
292+ password,
293+ hosts,
294+ pathname,
295+ search,
296+ searchParams,
297+ hash
298+ } ;
258299 }
259300}
260301
261302/**
262303 * Parses and serializes the format of the authMechanismProperties or
263304 * readPreferenceTags connection string parameters.
264305 */
265- // eslint-disable-next-line @typescript-eslint/ban-types
266- export class CommaAndColonSeparatedRecord < K extends { } = Record < string , unknown > > extends CaseInsensitiveMap < keyof K & string > {
306+ export class CommaAndColonSeparatedRecord <
307+ K extends Record < string , unknown > = Record < string , unknown >
308+ > extends CaseInsensitiveMap < keyof K & string > {
267309 constructor ( from ?: string | null ) {
268310 super ( ) ;
269311 for ( const entry of ( from ?? '' ) . split ( ',' ) ) {
270312 if ( ! entry ) continue ;
271313 const colonIndex = entry . indexOf ( ':' ) ;
272314 // Use .set() to properly account for case insensitivity
273315 if ( colonIndex === - 1 ) {
274- this . set ( entry as ( keyof K & string ) , '' ) ;
316+ this . set ( entry as keyof K & string , '' ) ;
275317 } else {
276- this . set ( entry . slice ( 0 , colonIndex ) as ( keyof K & string ) , entry . slice ( colonIndex + 1 ) ) ;
318+ this . set ( entry . slice ( 0 , colonIndex ) as keyof K & string , entry . slice ( colonIndex + 1 ) ) ;
277319 }
278320 }
279321 }
0 commit comments