Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 9 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@
"ts-node": "^10.9.1",
"typescript": "^5.9.2"
},
"dependencies": {
"@types/whatwg-url": "^13.0.0",
"whatwg-url": "^14.1.0"
},
"dependencies": {},
"engines": {
"node": ">=20.19.0"
}
Expand Down
24 changes: 10 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { URL, URLSearchParams } from 'whatwg-url';
import {
redactValidConnectionString,
redactConnectionString,
Expand Down Expand Up @@ -95,24 +94,21 @@ function caseInsenstiveURLSearchParams<K extends string = string>(Ctor: typeof U
};
}

// Abstract middle class to appease TypeScript, see https://github.com/microsoft/TypeScript/pull/37894
abstract class URLWithoutHost extends URL {
abstract get host(): never;
abstract set host(value: never);
abstract get hostname(): never;
abstract set hostname(value: never);
abstract get port(): never;
abstract set port(value: never);
abstract get href(): string;
abstract set href(value: string);
}

class MongoParseError extends Error {
get name(): string {
return 'MongoParseError';
}
}

// The native URL class declares host, hostname, port, and href as properties
// in its TypeScript type definitions. ConnectionString needs to override these
// with accessors, which TypeScript does not allow (TS2611). We work around this
// by re-typing the URL constructor to omit the conflicting property declarations.
const URLBase = URL as unknown as {
new (url: string | URL, base?: string | URL): Omit<URL, 'host' | 'hostname' | 'port' | 'href' | 'toString'>;
prototype: Omit<URL, 'host' | 'hostname' | 'port' | 'href' | 'toString'>;
};

export interface ConnectionStringParsingOptions {
looseValidation?: boolean;
}
Expand All @@ -121,7 +117,7 @@ export interface ConnectionStringParsingOptions {
* Represents a mongodb:// or mongodb+srv:// connection string.
* See: https://github.com/mongodb/specifications/blob/master/source/connection-string/connection-string-spec.rst#reference-implementation
*/
export class ConnectionString extends URLWithoutHost {
export class ConnectionString extends URLBase {
_hosts: string[];

// eslint-disable-next-line complexity
Expand Down