Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add MongoOptions interface #2616

Merged
merged 6 commits into from
Nov 19, 2020
Merged
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
162 changes: 112 additions & 50 deletions package-lock.json

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

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@microsoft/api-extractor": "^7.9.10",
"@microsoft/tsdoc-config": "^0.13.5",
"@microsoft/api-extractor": "^7.12.0",
"@microsoft/tsdoc-config": "^0.13.6",
"@types/aws4": "^1.5.1",
"@types/bl": "^2.1.0",
"@types/bson": "^4.0.2",
Expand Down Expand Up @@ -66,14 +66,14 @@
"sinon": "^4.3.0",
"sinon-chai": "^3.2.0",
"snappy": "^6.3.0",
"spec-xunit-file": "0.0.1-3",
"source-map-support": "^0.5.19",
"spec-xunit-file": "0.0.1-3",
"standard-version": "^7.1.0",
"through2": "^3.0.1",
"ts-node": "^9.0.0",
"typedoc": "^0.18.0",
"typedoc": "^0.19.2",
"typedoc-plugin-pages": "^1.0.1",
"typescript": "^4.0.2",
"typescript": "^4.0.5",
"typescript-cached-transpile": "^0.0.6",
"worker-farm": "^1.5.0",
"wtfnode": "^0.8.2",
Expand Down
48 changes: 48 additions & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as url from 'url';
import * as qs from 'querystring';
import * as dns from 'dns';
import { URL } from 'url';
import { ReadPreference } from './read_preference';
import { MongoParseError } from './error';
import type { AnyOptions, Callback } from './utils';
import type { ConnectionOptions } from './cmap/connection';
import type { Document } from './bson';
import type { CompressorName } from './cmap/wire_protocol/compression';
import type { MongoClientOptions, MongoOptions } from './mongo_client';

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

callback(undefined, result);
}

// NEW PARSER WORK...

const HOSTS_REGEX = new RegExp(
'(?<protocol>mongodb(?:\\+srv|)):\\/\\/(?:(?<username>[^:]*)(?::(?<password>[^@]*))?@)?(?<hosts>[^\\/?]*)(?<rest>.*)'
);

function parseURI(uri: string): { srv: boolean; url: URL; hosts: string[] } {
const match = uri.match(HOSTS_REGEX);
if (!match) {
throw new MongoParseError(`Invalid connection string ${uri}`);
}

const protocol = match.groups?.protocol;
const username = match.groups?.username;
const password = match.groups?.password;
const hosts = match.groups?.hosts;
const rest = match.groups?.rest;

if (!protocol || !hosts) {
throw new MongoParseError('Invalid connection string, protocol and host(s) required');
}

const authString = username ? (password ? `${username}:${password}` : `${username}`) : '';
return {
srv: protocol.includes('srv'),
url: new URL(`${protocol.toLowerCase()}://${authString}@dummyHostname${rest}`),
hosts: hosts.split(',')
};
}

export function parseOptions(
uri: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options: MongoClientOptions = {}
): Readonly<MongoOptions> {
try {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { srv, url, hosts } = parseURI(uri);
const mongoOptions: MongoOptions = ({ srv, hosts } as unknown) as MongoOptions;
// TODO(NODE-2699): option parse logic
return Object.freeze(mongoOptions);
} catch {
return Object.freeze({} as MongoOptions);
}
}
Loading