Skip to content

Commit 54c456b

Browse files
authored
feat: Add MongoOptions interface (#2616)
Add MongoOptions representing the internal combined view of options. NODE-2698
1 parent 5e04fff commit 54c456b

File tree

5 files changed

+277
-60
lines changed

5 files changed

+277
-60
lines changed

package-lock.json

+112-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
},
3333
"devDependencies": {
3434
"@istanbuljs/nyc-config-typescript": "^1.0.1",
35-
"@microsoft/api-extractor": "^7.9.10",
36-
"@microsoft/tsdoc-config": "^0.13.5",
35+
"@microsoft/api-extractor": "^7.12.0",
36+
"@microsoft/tsdoc-config": "^0.13.6",
3737
"@types/aws4": "^1.5.1",
3838
"@types/bl": "^2.1.0",
3939
"@types/bson": "^4.0.2",
@@ -66,14 +66,14 @@
6666
"sinon": "^4.3.0",
6767
"sinon-chai": "^3.2.0",
6868
"snappy": "^6.3.0",
69-
"spec-xunit-file": "0.0.1-3",
7069
"source-map-support": "^0.5.19",
70+
"spec-xunit-file": "0.0.1-3",
7171
"standard-version": "^7.1.0",
7272
"through2": "^3.0.1",
7373
"ts-node": "^9.0.0",
74-
"typedoc": "^0.18.0",
74+
"typedoc": "^0.19.2",
7575
"typedoc-plugin-pages": "^1.0.1",
76-
"typescript": "^4.0.2",
76+
"typescript": "^4.0.5",
7777
"typescript-cached-transpile": "^0.0.6",
7878
"worker-farm": "^1.5.0",
7979
"wtfnode": "^0.8.2",

src/connection_string.ts

+48
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as url from 'url';
22
import * as qs from 'querystring';
33
import * as dns from 'dns';
4+
import { URL } from 'url';
45
import { ReadPreference } from './read_preference';
56
import { MongoParseError } from './error';
67
import type { AnyOptions, Callback } from './utils';
78
import type { ConnectionOptions } from './cmap/connection';
89
import type { Document } from './bson';
910
import type { CompressorName } from './cmap/wire_protocol/compression';
11+
import type { MongoClientOptions, MongoOptions } from './mongo_client';
1012

1113
/**
1214
* The following regular expression validates a connection string and breaks the
@@ -760,3 +762,49 @@ export function parseConnectionString(
760762

761763
callback(undefined, result);
762764
}
765+
766+
// NEW PARSER WORK...
767+
768+
const HOSTS_REGEX = new RegExp(
769+
'(?<protocol>mongodb(?:\\+srv|)):\\/\\/(?:(?<username>[^:]*)(?::(?<password>[^@]*))?@)?(?<hosts>[^\\/?]*)(?<rest>.*)'
770+
);
771+
772+
function parseURI(uri: string): { srv: boolean; url: URL; hosts: string[] } {
773+
const match = uri.match(HOSTS_REGEX);
774+
if (!match) {
775+
throw new MongoParseError(`Invalid connection string ${uri}`);
776+
}
777+
778+
const protocol = match.groups?.protocol;
779+
const username = match.groups?.username;
780+
const password = match.groups?.password;
781+
const hosts = match.groups?.hosts;
782+
const rest = match.groups?.rest;
783+
784+
if (!protocol || !hosts) {
785+
throw new MongoParseError('Invalid connection string, protocol and host(s) required');
786+
}
787+
788+
const authString = username ? (password ? `${username}:${password}` : `${username}`) : '';
789+
return {
790+
srv: protocol.includes('srv'),
791+
url: new URL(`${protocol.toLowerCase()}://${authString}@dummyHostname${rest}`),
792+
hosts: hosts.split(',')
793+
};
794+
}
795+
796+
export function parseOptions(
797+
uri: string,
798+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
799+
options: MongoClientOptions = {}
800+
): Readonly<MongoOptions> {
801+
try {
802+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
803+
const { srv, url, hosts } = parseURI(uri);
804+
const mongoOptions: MongoOptions = ({ srv, hosts } as unknown) as MongoOptions;
805+
// TODO(NODE-2699): option parse logic
806+
return Object.freeze(mongoOptions);
807+
} catch {
808+
return Object.freeze({} as MongoOptions);
809+
}
810+
}

0 commit comments

Comments
 (0)