Skip to content

Commit 661511d

Browse files
authored
fix(NODE-3275): Fix enum type export naming and serverApi validation (#2809)
1 parent 3a2ea28 commit 661511d

24 files changed

+219
-155
lines changed

src/bulk/common.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import type { Hint } from '../operations/operation';
2222
import type { Filter, OptionalId, UpdateQuery } from '../mongo_types';
2323

2424
/** @public */
25-
export const BatchType = {
25+
export const BatchType = Object.freeze({
2626
INSERT: 1,
2727
UPDATE: 2,
2828
DELETE: 3
29-
} as const;
29+
} as const);
3030

3131
/** @public */
32-
export type BatchTypeId = typeof BatchType[keyof typeof BatchType];
32+
export type BatchType = typeof BatchType[keyof typeof BatchType];
3333

3434
/** @public */
3535
export interface InsertOneModel<TSchema extends Document = Document> {
@@ -137,12 +137,12 @@ export class Batch<T = Document> {
137137
originalZeroIndex: number;
138138
currentIndex: number;
139139
originalIndexes: number[];
140-
batchType: BatchTypeId;
140+
batchType: BatchType;
141141
operations: T[];
142142
size: number;
143143
sizeBytes: number;
144144

145-
constructor(batchType: BatchTypeId, originalZeroIndex: number) {
145+
constructor(batchType: BatchType, originalZeroIndex: number) {
146146
this.originalZeroIndex = originalZeroIndex;
147147
this.currentIndex = 0;
148148
this.originalIndexes = [];
@@ -1221,7 +1221,7 @@ export abstract class BulkOperationBase {
12211221
}
12221222

12231223
abstract addToOperationsList(
1224-
batchType: BatchTypeId,
1224+
batchType: BatchType,
12251225
document: Document | UpdateStatement | DeleteStatement
12261226
): this;
12271227
}

src/bulk/ordered.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as BSON from '../bson';
2-
import { BulkOperationBase, Batch, BatchType, BulkWriteOptions, BatchTypeId } from './common';
2+
import { BulkOperationBase, Batch, BatchType, BulkWriteOptions } from './common';
33
import type { Document } from '../bson';
44
import type { Collection } from '../collection';
55
import type { UpdateStatement } from '../operations/update';
@@ -12,7 +12,7 @@ export class OrderedBulkOperation extends BulkOperationBase {
1212
}
1313

1414
addToOperationsList(
15-
batchType: BatchTypeId,
15+
batchType: BatchType,
1616
document: Document | UpdateStatement | DeleteStatement
1717
): this {
1818
// Get the bsonSize

src/bulk/unordered.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import * as BSON from '../bson';
2-
import {
3-
BulkOperationBase,
4-
Batch,
5-
BatchType,
6-
BulkWriteOptions,
7-
BulkWriteResult,
8-
BatchTypeId
9-
} from './common';
2+
import { BulkOperationBase, Batch, BatchType, BulkWriteOptions, BulkWriteResult } from './common';
103
import type { Callback } from '../utils';
114
import type { Document } from '../bson';
125
import type { Collection } from '../collection';
@@ -28,7 +21,7 @@ export class UnorderedBulkOperation extends BulkOperationBase {
2821
}
2922

3023
addToOperationsList(
31-
batchType: BatchTypeId,
24+
batchType: BatchType,
3225
document: Document | UpdateStatement | DeleteStatement
3326
): this {
3427
// Get the bsonSize

src/cmap/auth/defaultAuthProviders.ts

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

99
/** @public */
10-
export const AuthMechanism = {
10+
export const AuthMechanism = Object.freeze({
1111
MONGODB_AWS: 'MONGODB-AWS',
1212
MONGODB_CR: 'MONGODB-CR',
1313
MONGODB_DEFAULT: 'DEFAULT',
@@ -16,12 +16,12 @@ export const AuthMechanism = {
1616
MONGODB_SCRAM_SHA1: 'SCRAM-SHA-1',
1717
MONGODB_SCRAM_SHA256: 'SCRAM-SHA-256',
1818
MONGODB_X509: 'MONGODB-X509'
19-
} as const;
19+
} as const);
2020

2121
/** @public */
22-
export type AuthMechanismId = typeof AuthMechanism[keyof typeof AuthMechanism];
22+
export type AuthMechanism = typeof AuthMechanism[keyof typeof AuthMechanism];
2323

24-
export const AUTH_PROVIDERS = new Map<AuthMechanismId | string, AuthProvider>([
24+
export const AUTH_PROVIDERS = new Map<AuthMechanism | string, AuthProvider>([
2525
[AuthMechanism.MONGODB_AWS, new MongoDBAWS()],
2626
[AuthMechanism.MONGODB_CR, new MongoCR()],
2727
[AuthMechanism.MONGODB_GSSAPI, new GSSAPI()],

src/cmap/auth/mongo_credentials.ts

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

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

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

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

src/cmap/message_stream.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
decompress,
88
uncompressibleCommands,
99
Compressor,
10-
CompressorName,
11-
CompressorId
10+
CompressorName
1211
} from './wire_protocol/compression';
1312
import type { Document, BSONSerializeOptions } from '../bson';
1413
import { BufferPool, Callback } from '../utils';
@@ -178,7 +177,7 @@ function processIncomingData(stream: MessageStream, callback: Callback<Buffer>)
178177
messageHeader.fromCompressed = true;
179178
messageHeader.opCode = message.readInt32LE(MESSAGE_HEADER_SIZE);
180179
messageHeader.length = message.readInt32LE(MESSAGE_HEADER_SIZE + 4);
181-
const compressorID: CompressorId = message[MESSAGE_HEADER_SIZE + 8] as CompressorId;
180+
const compressorID: Compressor = message[MESSAGE_HEADER_SIZE + 8] as Compressor;
182181
const compressedBuffer = message.slice(MESSAGE_HEADER_SIZE + 9);
183182

184183
// recalculate based on wrapped opcode

src/cmap/wire_protocol/compression.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export const Compressor = Object.freeze({
1111
zlib: 2
1212
} as const);
1313

14-
export type CompressorId = typeof Compressor[keyof typeof Compressor];
14+
/** @public */
15+
export type Compressor = typeof Compressor[CompressorName];
1516

1617
/** @public */
1718
export type CompressorName = keyof typeof Compressor;
@@ -61,7 +62,7 @@ export function compress(
6162

6263
// Decompress a message using the given compressor
6364
export function decompress(
64-
compressorID: CompressorId,
65+
compressorID: Compressor,
6566
compressedData: Buffer,
6667
callback: Callback<Buffer>
6768
): void {

src/connection_string.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as dns from 'dns';
22
import * as fs from 'fs';
33
import { URL, URLSearchParams } from 'url';
44
import { AuthMechanism } from './cmap/auth/defaultAuthProviders';
5-
import { ReadPreference, ReadPreferenceModeId } from './read_preference';
6-
import { ReadConcern, ReadConcernLevelId } from './read_concern';
5+
import { ReadPreference, ReadPreferenceMode } from './read_preference';
6+
import { ReadConcern, ReadConcernLevel } from './read_concern';
77
import { W, WriteConcern } from './write_concern';
88
import { MongoParseError } from './error';
99
import {
@@ -23,11 +23,12 @@ import {
2323
MongoClientOptions,
2424
MongoOptions,
2525
PkFactory,
26-
ServerApi
26+
ServerApi,
27+
ServerApiVersion
2728
} from './mongo_client';
2829
import { MongoCredentials } from './cmap/auth/mongo_credentials';
2930
import type { TagSet } from './sdam/server_description';
30-
import { Logger, LoggerLevelId } from './logger';
31+
import { Logger, LoggerLevel } from './logger';
3132
import { PromiseProvider } from './promise_provider';
3233
import { Encrypter } from './encrypter';
3334

@@ -590,10 +591,24 @@ export const OPTIONS = {
590591
serverApi: {
591592
target: 'serverApi',
592593
transform({ values: [version] }): ServerApi {
593-
if (typeof version === 'string') {
594-
return { version };
594+
const serverApiToValidate =
595+
typeof version === 'string' ? ({ version } as ServerApi) : (version as ServerApi);
596+
const versionToValidate = serverApiToValidate && serverApiToValidate.version;
597+
if (!versionToValidate) {
598+
throw new MongoParseError(
599+
`Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values(
600+
ServerApiVersion
601+
).join('", "')}"]`
602+
);
603+
}
604+
if (!Object.values(ServerApiVersion).some(v => v === versionToValidate)) {
605+
throw new MongoParseError(
606+
`Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values(
607+
ServerApiVersion
608+
).join('", "')}"]`
609+
);
595610
}
596-
return version as ServerApi;
611+
return serverApiToValidate;
597612
}
598613
},
599614
checkKeys: {
@@ -728,7 +743,7 @@ export const OPTIONS = {
728743
loggerLevel: {
729744
target: 'logger',
730745
transform({ values: [value] }) {
731-
return new Logger('MongoClient', { loggerLevel: value as LoggerLevelId });
746+
return new Logger('MongoClient', { loggerLevel: value as LoggerLevel });
732747
}
733748
},
734749
maxIdleTimeMS: {
@@ -818,7 +833,7 @@ export const OPTIONS = {
818833
transform({ values: [level], options }) {
819834
return ReadConcern.fromOptions({
820835
...options.readConcern,
821-
level: level as ReadConcernLevelId
836+
level: level as ReadConcernLevel
822837
});
823838
}
824839
},
@@ -845,7 +860,7 @@ export const OPTIONS = {
845860
maxStalenessSeconds: options.readPreference?.maxStalenessSeconds
846861
};
847862
return new ReadPreference(
848-
value as ReadPreferenceModeId,
863+
value as ReadPreferenceMode,
849864
options.readPreference?.tags,
850865
rpOpts
851866
);

src/db.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { RenameOperation, RenameOptions } from './operations/rename';
4141
import {
4242
SetProfilingLevelOperation,
4343
SetProfilingLevelOptions,
44-
ProfilingLevelId
44+
ProfilingLevel
4545
} from './operations/set_profiling_level';
4646
import { executeOperation } from './operations/execute_operation';
4747
import type { IndexInformationOptions } from './operations/common_functions';
@@ -657,22 +657,22 @@ export class Db {
657657
* @param options - Optional settings for the command
658658
* @param callback - An optional callback, a Promise will be returned if none is provided
659659
*/
660-
setProfilingLevel(level: ProfilingLevelId): Promise<ProfilingLevelId>;
661-
setProfilingLevel(level: ProfilingLevelId, callback: Callback<ProfilingLevelId>): void;
660+
setProfilingLevel(level: ProfilingLevel): Promise<ProfilingLevel>;
661+
setProfilingLevel(level: ProfilingLevel, callback: Callback<ProfilingLevel>): void;
662662
setProfilingLevel(
663-
level: ProfilingLevelId,
663+
level: ProfilingLevel,
664664
options: SetProfilingLevelOptions
665-
): Promise<ProfilingLevelId>;
665+
): Promise<ProfilingLevel>;
666666
setProfilingLevel(
667-
level: ProfilingLevelId,
667+
level: ProfilingLevel,
668668
options: SetProfilingLevelOptions,
669-
callback: Callback<ProfilingLevelId>
669+
callback: Callback<ProfilingLevel>
670670
): void;
671671
setProfilingLevel(
672-
level: ProfilingLevelId,
673-
options?: SetProfilingLevelOptions | Callback<ProfilingLevelId>,
674-
callback?: Callback<ProfilingLevelId>
675-
): Promise<ProfilingLevelId> | void {
672+
level: ProfilingLevel,
673+
options?: SetProfilingLevelOptions | Callback<ProfilingLevel>,
674+
callback?: Callback<ProfilingLevel>
675+
): Promise<ProfilingLevel> | void {
676676
if (typeof options === 'function') (callback = options), (options = {});
677677

678678
return executeOperation(

src/deps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const AutoEncryptionLoggerLevel = Object.freeze({
7777
} as const);
7878

7979
/** @public */
80-
export type AutoEncryptionLoggerLevelId = typeof AutoEncryptionLoggerLevel[keyof typeof AutoEncryptionLoggerLevel];
80+
export type AutoEncryptionLoggerLevel = typeof AutoEncryptionLoggerLevel[keyof typeof AutoEncryptionLoggerLevel];
8181

8282
/** @public */
8383
export interface AutoEncryptionOptions {
@@ -153,7 +153,7 @@ export interface AutoEncryptionOptions {
153153
bypassAutoEncryption?: boolean;
154154
options?: {
155155
/** An optional hook to catch logging messages from the underlying encryption engine */
156-
logger?: (level: AutoEncryptionLoggerLevelId, message: string) => void;
156+
logger?: (level: AutoEncryptionLoggerLevel, message: string) => void;
157157
};
158158
extraOptions?: {
159159
/**

0 commit comments

Comments
 (0)