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

fix(NODE-3275): Fix enum type export naming and serverApi validation #2809

Merged
merged 4 commits into from
May 18, 2021
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
12 changes: 6 additions & 6 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import type { CommandOperationOptions, CollationOptions } from '../operations/co
import type { Hint } from '../operations/operation';

/** @public */
export const BatchType = {
export const BatchType = Object.freeze({
INSERT: 1,
UPDATE: 2,
DELETE: 3
} as const;
} as const);

/** @public */
export type BatchTypeId = typeof BatchType[keyof typeof BatchType];
export type BatchType = typeof BatchType[keyof typeof BatchType];

/** @public */
export interface InsertOneModel {
Expand Down Expand Up @@ -136,12 +136,12 @@ export class Batch<T = Document> {
originalZeroIndex: number;
currentIndex: number;
originalIndexes: number[];
batchType: BatchTypeId;
batchType: BatchType;
operations: T[];
size: number;
sizeBytes: number;

constructor(batchType: BatchTypeId, originalZeroIndex: number) {
constructor(batchType: BatchType, originalZeroIndex: number) {
this.originalZeroIndex = originalZeroIndex;
this.currentIndex = 0;
this.originalIndexes = [];
Expand Down Expand Up @@ -1220,7 +1220,7 @@ export abstract class BulkOperationBase {
}

abstract addToOperationsList(
batchType: BatchTypeId,
batchType: BatchType,
document: Document | UpdateStatement | DeleteStatement
): this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/bulk/ordered.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as BSON from '../bson';
import { BulkOperationBase, Batch, BatchType, BulkWriteOptions, BatchTypeId } from './common';
import { BulkOperationBase, Batch, BatchType, BulkWriteOptions } from './common';
import type { Document } from '../bson';
import type { Collection } from '../collection';
import type { UpdateStatement } from '../operations/update';
Expand All @@ -12,7 +12,7 @@ export class OrderedBulkOperation extends BulkOperationBase {
}

addToOperationsList(
batchType: BatchTypeId,
batchType: BatchType,
document: Document | UpdateStatement | DeleteStatement
): this {
// Get the bsonSize
Expand Down
11 changes: 2 additions & 9 deletions src/bulk/unordered.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import * as BSON from '../bson';
import {
BulkOperationBase,
Batch,
BatchType,
BulkWriteOptions,
BulkWriteResult,
BatchTypeId
} from './common';
import { BulkOperationBase, Batch, BatchType, BulkWriteOptions, BulkWriteResult } from './common';
import type { Callback } from '../utils';
import type { Document } from '../bson';
import type { Collection } from '../collection';
Expand All @@ -28,7 +21,7 @@ export class UnorderedBulkOperation extends BulkOperationBase {
}

addToOperationsList(
batchType: BatchTypeId,
batchType: BatchType,
document: Document | UpdateStatement | DeleteStatement
): this {
// Get the bsonSize
Expand Down
8 changes: 4 additions & 4 deletions src/cmap/auth/defaultAuthProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MongoDBAWS } from './mongodb_aws';
import type { AuthProvider } from './auth_provider';

/** @public */
export const AuthMechanism = {
export const AuthMechanism = Object.freeze({
MONGODB_AWS: 'MONGODB-AWS',
MONGODB_CR: 'MONGODB-CR',
MONGODB_DEFAULT: 'DEFAULT',
Expand All @@ -16,12 +16,12 @@ export const AuthMechanism = {
MONGODB_SCRAM_SHA1: 'SCRAM-SHA-1',
MONGODB_SCRAM_SHA256: 'SCRAM-SHA-256',
MONGODB_X509: 'MONGODB-X509'
} as const;
} as const);

/** @public */
export type AuthMechanismId = typeof AuthMechanism[keyof typeof AuthMechanism];
export type AuthMechanism = typeof AuthMechanism[keyof typeof AuthMechanism];

export const AUTH_PROVIDERS = new Map<AuthMechanismId | string, AuthProvider>([
export const AUTH_PROVIDERS = new Map<AuthMechanism | string, AuthProvider>([
[AuthMechanism.MONGODB_AWS, new MongoDBAWS()],
[AuthMechanism.MONGODB_CR, new MongoCR()],
[AuthMechanism.MONGODB_GSSAPI, new GSSAPI()],
Expand Down
8 changes: 4 additions & 4 deletions src/cmap/auth/mongo_credentials.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Resolves the default auth mechanism according to

import type { Document } from '../../bson';
import { AuthMechanismId, AuthMechanism } from './defaultAuthProviders';
import { AuthMechanism } from './defaultAuthProviders';

// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
function getDefaultAuthMechanism(ismaster?: Document): AuthMechanismId {
function getDefaultAuthMechanism(ismaster?: Document): AuthMechanism {
if (ismaster) {
// If ismaster contains saslSupportedMechs, use scram-sha-256
// if it is available, else scram-sha-1
Expand All @@ -30,7 +30,7 @@ export interface MongoCredentialsOptions {
password: string;
source: string;
db?: string;
mechanism?: AuthMechanismId;
mechanism?: AuthMechanism;
mechanismProperties: Document;
}

Expand All @@ -46,7 +46,7 @@ export class MongoCredentials {
/** The database that the user should authenticate against */
readonly source: string;
/** The method used to authenticate */
readonly mechanism: AuthMechanismId;
readonly mechanism: AuthMechanism;
/** Special properties used by some types of auth mechanisms */
readonly mechanismProperties: Document;

Expand Down
5 changes: 2 additions & 3 deletions src/cmap/message_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
decompress,
uncompressibleCommands,
Compressor,
CompressorName,
CompressorId
CompressorName
} from './wire_protocol/compression';
import type { Document, BSONSerializeOptions } from '../bson';
import { BufferPool, Callback } from '../utils';
Expand Down Expand Up @@ -178,7 +177,7 @@ function processIncomingData(stream: MessageStream, callback: Callback<Buffer>)
messageHeader.fromCompressed = true;
messageHeader.opCode = message.readInt32LE(MESSAGE_HEADER_SIZE);
messageHeader.length = message.readInt32LE(MESSAGE_HEADER_SIZE + 4);
const compressorID: CompressorId = message[MESSAGE_HEADER_SIZE + 8] as CompressorId;
const compressorID: Compressor = message[MESSAGE_HEADER_SIZE + 8] as Compressor;
const compressedBuffer = message.slice(MESSAGE_HEADER_SIZE + 9);

// recalculate based on wrapped opcode
Expand Down
5 changes: 3 additions & 2 deletions src/cmap/wire_protocol/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const Compressor = Object.freeze({
zlib: 2
} as const);

export type CompressorId = typeof Compressor[keyof typeof Compressor];
/** @public */
export type Compressor = typeof Compressor[CompressorName];

/** @public */
export type CompressorName = keyof typeof Compressor;
Expand Down Expand Up @@ -61,7 +62,7 @@ export function compress(

// Decompress a message using the given compressor
export function decompress(
compressorID: CompressorId,
compressorID: Compressor,
compressedData: Buffer,
callback: Callback<Buffer>
): void {
Expand Down
35 changes: 25 additions & 10 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as dns from 'dns';
import * as fs from 'fs';
import { URL, URLSearchParams } from 'url';
import { AuthMechanism } from './cmap/auth/defaultAuthProviders';
import { ReadPreference, ReadPreferenceModeId } from './read_preference';
import { ReadConcern, ReadConcernLevelId } from './read_concern';
import { ReadPreference, ReadPreferenceMode } from './read_preference';
import { ReadConcern, ReadConcernLevel } from './read_concern';
import { W, WriteConcern } from './write_concern';
import { MongoParseError } from './error';
import {
Expand All @@ -23,11 +23,12 @@ import {
MongoClientOptions,
MongoOptions,
PkFactory,
ServerApi
ServerApi,
ServerApiVersion
} from './mongo_client';
import { MongoCredentials } from './cmap/auth/mongo_credentials';
import type { TagSet } from './sdam/server_description';
import { Logger, LoggerLevelId } from './logger';
import { Logger, LoggerLevel } from './logger';
import { PromiseProvider } from './promise_provider';
import { Encrypter } from './encrypter';

Expand Down Expand Up @@ -590,10 +591,24 @@ export const OPTIONS = {
serverApi: {
target: 'serverApi',
transform({ values: [version] }): ServerApi {
if (typeof version === 'string') {
return { version };
const serverApiToValidate =
typeof version === 'string' ? ({ version } as ServerApi) : (version as ServerApi);
const versionToValidate = serverApiToValidate && serverApiToValidate.version;
if (!versionToValidate) {
throw new MongoParseError(
`Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values(
ServerApiVersion
).join('", "')}"]`
);
}
if (!Object.values(ServerApiVersion).some(v => v === versionToValidate)) {
throw new MongoParseError(
`Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values(
ServerApiVersion
).join('", "')}"]`
);
}
return version as ServerApi;
return serverApiToValidate;
}
},
checkKeys: {
Expand Down Expand Up @@ -728,7 +743,7 @@ export const OPTIONS = {
loggerLevel: {
target: 'logger',
transform({ values: [value] }) {
return new Logger('MongoClient', { loggerLevel: value as LoggerLevelId });
return new Logger('MongoClient', { loggerLevel: value as LoggerLevel });
}
},
maxIdleTimeMS: {
Expand Down Expand Up @@ -818,7 +833,7 @@ export const OPTIONS = {
transform({ values: [level], options }) {
return ReadConcern.fromOptions({
...options.readConcern,
level: level as ReadConcernLevelId
level: level as ReadConcernLevel
});
}
},
Expand All @@ -845,7 +860,7 @@ export const OPTIONS = {
maxStalenessSeconds: options.readPreference?.maxStalenessSeconds
};
return new ReadPreference(
value as ReadPreferenceModeId,
value as ReadPreferenceMode,
options.readPreference?.tags,
rpOpts
);
Expand Down
22 changes: 11 additions & 11 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { RenameOperation, RenameOptions } from './operations/rename';
import {
SetProfilingLevelOperation,
SetProfilingLevelOptions,
ProfilingLevelId
ProfilingLevel
} from './operations/set_profiling_level';
import { executeOperation } from './operations/execute_operation';
import type { IndexInformationOptions } from './operations/common_functions';
Expand Down Expand Up @@ -635,22 +635,22 @@ export class Db {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
setProfilingLevel(level: ProfilingLevelId): Promise<ProfilingLevelId>;
setProfilingLevel(level: ProfilingLevelId, callback: Callback<ProfilingLevelId>): void;
setProfilingLevel(level: ProfilingLevel): Promise<ProfilingLevel>;
setProfilingLevel(level: ProfilingLevel, callback: Callback<ProfilingLevel>): void;
setProfilingLevel(
level: ProfilingLevelId,
level: ProfilingLevel,
options: SetProfilingLevelOptions
): Promise<ProfilingLevelId>;
): Promise<ProfilingLevel>;
setProfilingLevel(
level: ProfilingLevelId,
level: ProfilingLevel,
options: SetProfilingLevelOptions,
callback: Callback<ProfilingLevelId>
callback: Callback<ProfilingLevel>
): void;
setProfilingLevel(
level: ProfilingLevelId,
options?: SetProfilingLevelOptions | Callback<ProfilingLevelId>,
callback?: Callback<ProfilingLevelId>
): Promise<ProfilingLevelId> | void {
level: ProfilingLevel,
options?: SetProfilingLevelOptions | Callback<ProfilingLevel>,
callback?: Callback<ProfilingLevel>
): Promise<ProfilingLevel> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand Down
4 changes: 2 additions & 2 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const AutoEncryptionLoggerLevel = Object.freeze({
} as const);

/** @public */
export type AutoEncryptionLoggerLevelId = typeof AutoEncryptionLoggerLevel[keyof typeof AutoEncryptionLoggerLevel];
export type AutoEncryptionLoggerLevel = typeof AutoEncryptionLoggerLevel[keyof typeof AutoEncryptionLoggerLevel];

/** @public */
export interface AutoEncryptionOptions {
Expand Down Expand Up @@ -153,7 +153,7 @@ export interface AutoEncryptionOptions {
bypassAutoEncryption?: boolean;
options?: {
/** An optional hook to catch logging messages from the underlying encryption engine */
logger?: (level: AutoEncryptionLoggerLevelId, message: string) => void;
logger?: (level: AutoEncryptionLoggerLevel, message: string) => void;
};
extraOptions?: {
/**
Expand Down
11 changes: 7 additions & 4 deletions src/explain.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { MongoError } from './error';

/** @public */
export const ExplainVerbosity = {
export const ExplainVerbosity = Object.freeze({
queryPlanner: 'queryPlanner',
queryPlannerExtended: 'queryPlannerExtended',
executionStats: 'executionStats',
allPlansExecution: 'allPlansExecution'
} as const;
} as const);

/** @public */
export type ExplainVerbosity = typeof ExplainVerbosity[keyof typeof ExplainVerbosity];

/**
* For backwards compatibility, true is interpreted as "allPlansExecution"
* and false as "queryPlanner". Prior to server version 3.6, aggregate()
* ignores the verbosity parameter and executes in "queryPlanner".
* @public
*/
export type ExplainVerbosityLike = keyof typeof ExplainVerbosity | boolean;
export type ExplainVerbosityLike = ExplainVerbosity | boolean;

/** @public */
export interface ExplainOptions {
Expand All @@ -24,7 +27,7 @@ export interface ExplainOptions {

/** @internal */
export class Explain {
verbosity: keyof typeof ExplainVerbosity;
verbosity: ExplainVerbosity;

constructor(verbosity: ExplainVerbosityLike) {
if (typeof verbosity === 'boolean') {
Expand Down
Loading