Skip to content

Commit 7d6b572

Browse files
authored
fix(service-provider-core): re-fix connection string casing (#781)
#760 broke the compass-shell tests, likely because there are multiple conflicting implementations of `URLSearchParams` that come into play. We didn't notice because the compass-shell tests don't run in CI. Instead of using a fixed prototype, make the case-insensitive URLSearchParams implementation a mixin.
1 parent 173c5a4 commit 7d6b572

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

packages/service-provider-core/src/connection-string.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,43 @@ const HOSTS_REGEX = new RegExp(
1010
String.raw`(?<protocol>mongodb(?:\+srv|)):\/\/(?:(?<username>[^:]*)(?::(?<password>[^@]*))?@)?(?<hosts>(?!:)[^\/?@]+)(?<rest>.*)`
1111
);
1212

13-
class CaseInsenstiveURLSearchParams extends URLSearchParams {
14-
append(name: any, value: any): void {
15-
return super.append(this._normalizeKey(name), value);
16-
}
13+
const caseInsenstiveURLSearchParams = (Ctor: typeof URLSearchParams) =>
14+
class CaseInsenstiveURLSearchParams extends Ctor {
15+
append(name: any, value: any): void {
16+
return super.append(this._normalizeKey(name), value);
17+
}
1718

18-
delete(name: any): void {
19-
return super.delete(this._normalizeKey(name));
20-
}
19+
delete(name: any): void {
20+
return super.delete(this._normalizeKey(name));
21+
}
2122

22-
get(name: any): string | null {
23-
return super.get(this._normalizeKey(name));
24-
}
23+
get(name: any): string | null {
24+
return super.get(this._normalizeKey(name));
25+
}
2526

26-
getAll(name: any): string[] {
27-
return super.getAll(this._normalizeKey(name));
28-
}
27+
getAll(name: any): string[] {
28+
return super.getAll(this._normalizeKey(name));
29+
}
2930

30-
has(name: any): boolean {
31-
return super.has(this._normalizeKey(name));
32-
}
31+
has(name: any): boolean {
32+
return super.has(this._normalizeKey(name));
33+
}
3334

34-
set(name: any, value: any): void {
35-
return super.set(this._normalizeKey(name), value);
36-
}
35+
set(name: any, value: any): void {
36+
return super.set(this._normalizeKey(name), value);
37+
}
3738

38-
_normalizeKey(name: any): string {
39-
name = `${name}`;
40-
for (const key of this.keys()) {
41-
if (key.toLowerCase() === name.toLowerCase()) {
42-
name = key;
43-
break;
39+
_normalizeKey(name: any): string {
40+
name = `${name}`;
41+
for (const key of this.keys()) {
42+
if (key.toLowerCase() === name.toLowerCase()) {
43+
name = key;
44+
break;
45+
}
4446
}
47+
return name;
4548
}
46-
return name;
47-
}
48-
}
49+
};
4950

5051
// Abstract middle class to appease TypeScript, see https://github.com/microsoft/TypeScript/pull/37894
5152
abstract class URLWithoutHost extends URL {
@@ -114,7 +115,7 @@ export class ConnectionString extends URLWithoutHost {
114115
if (!this.pathname) {
115116
this.pathname = '/';
116117
}
117-
Object.setPrototypeOf(this.searchParams, CaseInsenstiveURLSearchParams.prototype);
118+
Object.setPrototypeOf(this.searchParams, caseInsenstiveURLSearchParams(this.searchParams.constructor as any).prototype);
118119
}
119120

120121
// The getters here should throw, but that would break .toString() because of

0 commit comments

Comments
 (0)