Skip to content

Commit

Permalink
Move around code
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Nov 18, 2020
1 parent e42cdb3 commit e61706c
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 203 deletions.
142 changes: 89 additions & 53 deletions package-lock.json

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

8 changes: 4 additions & 4 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.11.2",
"@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 @@ -71,9 +71,9 @@
"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);
}
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,3 @@ export type {
ClientMetadataOptions
} from './utils';
export type { WriteConcern, W, WriteConcernOptions } from './write_concern';
export type { MongoOptions } from './mongo_client_options';
Loading

0 comments on commit e61706c

Please sign in to comment.