Skip to content

Commit cb5898e

Browse files
committed
feat: Add MongoOptions builder
Parse options from uri and object creating frozen MongoOptions interface object. NODE-2699
1 parent e61706c commit cb5898e

14 files changed

+994
-129
lines changed

src/cmap/auth/defaultAuthProviders.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@ import { MongoDBAWS } from './mongodb_aws';
77
import type { AuthProvider } from './auth_provider';
88

99
/** @public */
10-
export enum AuthMechanism {
11-
MONGODB_AWS = 'MONGODB-AWS',
12-
MONGODB_CR = 'MONGODB-CR',
13-
MONGODB_DEFAULT = 'DEFAULT',
14-
MONGODB_GSSAPI = 'GSSAPI',
15-
MONGODB_PLAIN = 'PLAIN',
16-
MONGODB_SCRAM_SHA1 = 'SCRAM-SHA-1',
17-
MONGODB_SCRAM_SHA256 = 'SCRAM-SHA-256',
18-
MONGODB_X509 = 'MONGODB-X509'
19-
}
10+
export const AuthMechanismEnum = {
11+
MONGODB_AWS: 'MONGODB-AWS',
12+
MONGODB_CR: 'MONGODB-CR',
13+
MONGODB_DEFAULT: 'DEFAULT',
14+
MONGODB_GSSAPI: 'GSSAPI',
15+
MONGODB_PLAIN: 'PLAIN',
16+
MONGODB_SCRAM_SHA1: 'SCRAM-SHA-1',
17+
MONGODB_SCRAM_SHA256: 'SCRAM-SHA-256',
18+
MONGODB_X509: 'MONGODB-X509'
19+
} as const;
20+
21+
/** @public */
22+
export type AuthMechanism = typeof AuthMechanismEnum[keyof typeof AuthMechanismEnum];
2023

2124
export const AUTH_PROVIDERS = {
22-
[AuthMechanism.MONGODB_AWS]: new MongoDBAWS(),
23-
[AuthMechanism.MONGODB_CR]: new MongoCR(),
24-
[AuthMechanism.MONGODB_GSSAPI]: new GSSAPI(),
25-
[AuthMechanism.MONGODB_PLAIN]: new Plain(),
26-
[AuthMechanism.MONGODB_SCRAM_SHA1]: new ScramSHA1(),
27-
[AuthMechanism.MONGODB_SCRAM_SHA256]: new ScramSHA256(),
28-
[AuthMechanism.MONGODB_X509]: new X509()
25+
[AuthMechanismEnum.MONGODB_AWS]: new MongoDBAWS(),
26+
[AuthMechanismEnum.MONGODB_CR]: new MongoCR(),
27+
[AuthMechanismEnum.MONGODB_GSSAPI]: new GSSAPI(),
28+
[AuthMechanismEnum.MONGODB_PLAIN]: new Plain(),
29+
[AuthMechanismEnum.MONGODB_SCRAM_SHA1]: new ScramSHA1(),
30+
[AuthMechanismEnum.MONGODB_SCRAM_SHA256]: new ScramSHA256(),
31+
[AuthMechanismEnum.MONGODB_X509]: new X509()
2932
};
3033

3134
// TODO: We can make auth mechanism more functional since we pass around a context object

src/cmap/auth/mongo_credentials.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Resolves the default auth mechanism according to
22

33
import type { Document } from '../../bson';
4-
import { AuthMechanism } from './defaultAuthProviders';
4+
import { AuthMechanism, AuthMechanismEnum } from './defaultAuthProviders';
55

66
// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
77
function getDefaultAuthMechanism(ismaster?: Document): AuthMechanism {
@@ -10,18 +10,18 @@ function getDefaultAuthMechanism(ismaster?: Document): AuthMechanism {
1010
// if it is available, else scram-sha-1
1111
if (Array.isArray(ismaster.saslSupportedMechs)) {
1212
return ismaster.saslSupportedMechs.indexOf('SCRAM-SHA-256') >= 0
13-
? AuthMechanism.MONGODB_SCRAM_SHA256
14-
: AuthMechanism.MONGODB_SCRAM_SHA1;
13+
? AuthMechanismEnum.MONGODB_SCRAM_SHA256
14+
: AuthMechanismEnum.MONGODB_SCRAM_SHA1;
1515
}
1616

1717
// Fallback to legacy selection method. If wire version >= 3, use scram-sha-1
1818
if (ismaster.maxWireVersion >= 3) {
19-
return AuthMechanism.MONGODB_SCRAM_SHA1;
19+
return AuthMechanismEnum.MONGODB_SCRAM_SHA1;
2020
}
2121
}
2222

2323
// Default for wireprotocol < 3
24-
return AuthMechanism.MONGODB_CR;
24+
return AuthMechanismEnum.MONGODB_CR;
2525
}
2626

2727
/** @public */
@@ -57,7 +57,7 @@ export class MongoCredentials {
5757
if (!this.source && options.db) {
5858
this.source = options.db;
5959
}
60-
this.mechanism = options.mechanism || AuthMechanism.MONGODB_DEFAULT;
60+
this.mechanism = options.mechanism || AuthMechanismEnum.MONGODB_DEFAULT;
6161
this.mechanismProperties = options.mechanismProperties || {};
6262

6363
if (this.mechanism.match(/MONGODB-AWS/i)) {

src/cmap/auth/mongodb_aws.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { maxWireVersion, Callback } from '../../utils';
99
import type { BSONSerializeOptions } from '../../bson';
1010

1111
import { aws4 } from '../../deps';
12-
import { AuthMechanism } from './defaultAuthProviders';
12+
import { AuthMechanismEnum } from './defaultAuthProviders';
1313

1414
const ASCII_N = 110;
1515
const AWS_RELATIVE_URI = 'http://169.254.170.2';
@@ -155,7 +155,7 @@ function makeTempCredentials(credentials: MongoCredentials, callback: Callback<M
155155
username: creds.AccessKeyId,
156156
password: creds.SecretAccessKey,
157157
source: credentials.source,
158-
mechanism: AuthMechanism.MONGODB_AWS,
158+
mechanism: AuthMechanismEnum.MONGODB_AWS,
159159
mechanismProperties: {
160160
AWS_SESSION_TOKEN: creds.Token
161161
}

src/cmap/connect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as net from 'net';
22
import * as tls from 'tls';
33
import { Connection, ConnectionOptions } from './connection';
44
import { MongoError, MongoNetworkError, MongoNetworkTimeoutError, AnyError } from '../error';
5-
import { defaultAuthProviders, AuthMechanism } from './auth/defaultAuthProviders';
5+
import { defaultAuthProviders, AuthMechanismEnum } from './auth/defaultAuthProviders';
66
import { AuthContext } from './auth/auth_provider';
77
import { makeClientMetadata, ClientMetadata, Callback, CallbackWithType } from '../utils';
88
import {
@@ -179,7 +179,7 @@ function prepareHandshakeDocument(authContext: AuthContext, callback: Callback<H
179179
});
180180

181181
let provider;
182-
if ((provider = AUTH_PROVIDERS[AuthMechanism.MONGODB_SCRAM_SHA256])) {
182+
if ((provider = AUTH_PROVIDERS[AuthMechanismEnum.MONGODB_SCRAM_SHA256])) {
183183
// This auth mechanism is always present.
184184
provider.prepare(handshakeDoc, authContext, callback);
185185
return;

src/cmap/connection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import type { Server } from '../sdam/server';
2020
import type { MongoCredentials } from './auth/mongo_credentials';
2121
import type { CommandOptions } from './wire_protocol/command';
2222
import type { GetMoreOptions } from './wire_protocol/get_more';
23-
import type { InsertOptions, UpdateOptions, RemoveOptions } from './wire_protocol/index';
2423
import type { Stream } from './connect';
2524
import type { LoggerOptions } from '../logger';
2625
import type { FindOptions } from '../operations/find';
26+
import type { WriteCommandOptions } from './wire_protocol/write_command';
2727

2828
const kStream = Symbol('stream');
2929
const kQueue = Symbol('queue');
@@ -258,15 +258,15 @@ export class Connection extends EventEmitter {
258258
wp.killCursors(makeServerTrampoline(this), ns, cursorIds, options, callback);
259259
}
260260

261-
insert(ns: string, ops: Document[], options: InsertOptions, callback: Callback): void {
261+
insert(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
262262
wp.insert(makeServerTrampoline(this), ns, ops, options, callback);
263263
}
264264

265-
update(ns: string, ops: Document[], options: UpdateOptions, callback: Callback): void {
265+
update(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
266266
wp.update(makeServerTrampoline(this), ns, ops, options, callback);
267267
}
268268

269-
remove(ns: string, ops: Document[], options: RemoveOptions, callback: Callback): void {
269+
remove(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
270270
wp.remove(makeServerTrampoline(this), ns, ops, options, callback);
271271
}
272272
}

src/cmap/wire_protocol/index.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,31 @@ import type { Callback } from '../../utils';
1111

1212
export { writeCommand };
1313

14-
/** @internal */
15-
export type InsertOptions = WriteCommandOptions;
16-
1714
export function insert(
1815
server: Server,
1916
ns: string,
2017
ops: Document[],
21-
options: InsertOptions,
18+
options: WriteCommandOptions,
2219
callback: Callback
2320
): void {
2421
writeCommand(server, 'insert', 'documents', ns, ops, options, callback);
2522
}
2623

27-
/** @internal */
28-
export type UpdateOptions = WriteCommandOptions;
29-
3024
export function update(
3125
server: Server,
3226
ns: string,
3327
ops: Document[],
34-
options: UpdateOptions,
28+
options: WriteCommandOptions,
3529
callback: Callback
3630
): void {
3731
writeCommand(server, 'update', 'updates', ns, ops, options, callback);
3832
}
3933

40-
/** @internal */
41-
export type RemoveOptions = WriteCommandOptions;
42-
4334
export function remove(
4435
server: Server,
4536
ns: string,
4637
ops: Document[],
47-
options: RemoveOptions,
38+
options: WriteCommandOptions,
4839
callback: Callback
4940
): void {
5041
writeCommand(server, 'delete', 'deletes', ns, ops, options, callback);

0 commit comments

Comments
 (0)