From a50a22ef16b243ab4c7c5c30c0d703fb10172d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Thu, 15 Sep 2022 18:06:49 +0200 Subject: [PATCH 1/3] chore: remove wire version checks for unsupported servers --- src/cmap/connection.ts | 2 +- src/operations/aggregate.ts | 6 ++---- src/operations/command.ts | 34 ++++++++----------------------- src/operations/delete.ts | 16 +-------------- src/operations/find.ts | 29 ++------------------------ src/operations/find_and_modify.ts | 9 -------- src/operations/update.ts | 31 +--------------------------- src/sdam/server.ts | 8 -------- src/utils.ts | 11 ---------- 9 files changed, 15 insertions(+), 131 deletions(-) diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 416b0ee855..0bfc0b2663 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -645,7 +645,7 @@ function supportsOpMsg(conn: Connection) { return false; } - return maxWireVersion(conn) >= 6 && !description.__nodejs_mock_server__; + return !description.__nodejs_mock_server__; } function streamIdentifier(stream: Stream, options: ConnectionOptions): string { diff --git a/src/operations/aggregate.ts b/src/operations/aggregate.ts index 1acc6ac628..a48a89ca5b 100644 --- a/src/operations/aggregate.ts +++ b/src/operations/aggregate.ts @@ -99,10 +99,8 @@ export class AggregateOperation extends CommandOperation { this.readConcern = undefined; } - if (serverWireVersion >= 5) { - if (this.hasWriteStage && this.writeConcern) { - Object.assign(command, { writeConcern: this.writeConcern }); - } + if (this.hasWriteStage && this.writeConcern) { + Object.assign(command, { writeConcern: this.writeConcern }); } if (options.bypassDocumentValidation === true) { diff --git a/src/operations/command.ts b/src/operations/command.ts index 57186ab42c..1d0c042eec 100644 --- a/src/operations/command.ts +++ b/src/operations/command.ts @@ -1,5 +1,5 @@ import type { BSONSerializeOptions, Document } from '../bson'; -import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error'; +import { MongoInvalidArgumentError } from '../error'; import { Explain, ExplainOptions } from '../explain'; import type { Logger } from '../logger'; import { ReadConcern } from '../read_concern'; @@ -18,8 +18,6 @@ import { WriteConcern, WriteConcernOptions } from '../write_concern'; import type { ReadConcernLike } from './../read_concern'; import { AbstractOperation, Aspect, OperationOptions } from './operation'; -const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5; - /** @public */ export interface CollationOptions { locale: string; @@ -152,27 +150,16 @@ export abstract class CommandOperation extends AbstractOperation { options.omitReadPreference = true; } - if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) { - callback( - new MongoCompatibilityError( - `Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation` - ) - ); - return; - } - if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION) && !inTransaction) { Object.assign(cmd, { writeConcern: this.writeConcern }); } - if (serverWireVersion >= SUPPORTS_WRITE_CONCERN_AND_COLLATION) { - if ( - options.collation && - typeof options.collation === 'object' && - !this.hasAspect(Aspect.SKIP_COLLATION) - ) { - Object.assign(cmd, { collation: options.collation }); - } + if ( + options.collation && + typeof options.collation === 'object' && + !this.hasAspect(Aspect.SKIP_COLLATION) + ) { + Object.assign(cmd, { collation: options.collation }); } if (typeof options.maxTimeMS === 'number') { @@ -180,12 +167,7 @@ export abstract class CommandOperation extends AbstractOperation { } if (this.hasAspect(Aspect.EXPLAINABLE) && this.explain) { - if (serverWireVersion < 6 && cmd.aggregate) { - // Prior to 3.6, with aggregate, verbosity is ignored, and we must pass in "explain: true" - cmd.explain = true; - } else { - cmd = decorateWithExplain(cmd, this.explain); - } + cmd = decorateWithExplain(cmd, this.explain); } server.command(this.ns, cmd, options, callback); diff --git a/src/operations/delete.ts b/src/operations/delete.ts index ccdfd1c80b..319d14f208 100644 --- a/src/operations/delete.ts +++ b/src/operations/delete.ts @@ -3,7 +3,7 @@ import type { Collection } from '../collection'; import { MongoCompatibilityError, MongoServerError } from '../error'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; -import { Callback, collationNotSupported, maxWireVersion, MongoDBNamespace } from '../utils'; +import { Callback, maxWireVersion, MongoDBNamespace } from '../utils'; import type { WriteConcernOptions } from '../write_concern'; import { CollationOptions, CommandOperation, CommandOperationOptions } from './command'; import { Aspect, defineAspects, Hint } from './operation'; @@ -82,14 +82,6 @@ export class DeleteOperation extends CommandOperation { command.comment = options.comment; } - if (options.explain != null && maxWireVersion(server) < 3) { - return callback - ? callback( - new MongoCompatibilityError(`Server ${server.name} does not support explain on delete`) - ) - : undefined; - } - const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0; if (unacknowledgedWrite || maxWireVersion(server) < 5) { if (this.statements.find((o: Document) => o.hint)) { @@ -98,12 +90,6 @@ export class DeleteOperation extends CommandOperation { } } - const statementWithCollation = this.statements.find(statement => !!statement.collation); - if (statementWithCollation && collationNotSupported(server, statementWithCollation)) { - callback(new MongoCompatibilityError(`Server ${server.name} does not support collation`)); - return; - } - super.executeCommand(server, session, command, callback); } } diff --git a/src/operations/find.ts b/src/operations/find.ts index ba2e8cf741..33aedd6b5f 100644 --- a/src/operations/find.ts +++ b/src/operations/find.ts @@ -1,17 +1,11 @@ import type { Document } from '../bson'; import type { Collection } from '../collection'; -import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error'; +import { MongoInvalidArgumentError } from '../error'; import { ReadConcern } from '../read_concern'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; import { formatSort, Sort } from '../sort'; -import { - Callback, - decorateWithExplain, - maxWireVersion, - MongoDBNamespace, - normalizeHintField -} from '../utils'; +import { Callback, decorateWithExplain, MongoDBNamespace, normalizeHintField } from '../utils'; import { CollationOptions, CommandOperation, CommandOperationOptions } from './command'; import { Aspect, defineAspects, Hint } from './operation'; @@ -70,8 +64,6 @@ export interface FindOptions extends Comman oplogReplay?: boolean; } -const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5; - /** @internal */ export class FindOperation extends CommandOperation { override options: FindOptions; @@ -113,24 +105,7 @@ export class FindOperation extends CommandOperation { ): void { this.server = server; - const serverWireVersion = maxWireVersion(server); const options = this.options; - if (options.allowDiskUse != null && serverWireVersion < 4) { - callback( - new MongoCompatibilityError('Option "allowDiskUse" is not supported on MongoDB < 3.2') - ); - return; - } - - if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) { - callback( - new MongoCompatibilityError( - `Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation` - ) - ); - - return; - } let findCommand = makeFindCommand(this.ns, this.filter, options); if (this.explain) { diff --git a/src/operations/find_and_modify.ts b/src/operations/find_and_modify.ts index 12d7ba5044..d292c423c2 100644 --- a/src/operations/find_and_modify.ts +++ b/src/operations/find_and_modify.ts @@ -202,15 +202,6 @@ class FindAndModifyOperation extends CommandOperation { cmd.hint = options.hint; } - if (this.explain && maxWireVersion(server) < 4) { - callback( - new MongoCompatibilityError( - `Server ${server.name} does not support explain on findAndModify` - ) - ); - return; - } - // Execute the command super.executeCommand(server, session, cmd, (err, result) => { if (err) return callback(err); diff --git a/src/operations/update.ts b/src/operations/update.ts index 43760c1431..510ff73543 100644 --- a/src/operations/update.ts +++ b/src/operations/update.ts @@ -3,13 +3,7 @@ import type { Collection } from '../collection'; import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; -import { - Callback, - collationNotSupported, - hasAtomicOperators, - maxWireVersion, - MongoDBNamespace -} from '../utils'; +import { Callback, hasAtomicOperators, maxWireVersion, MongoDBNamespace } from '../utils'; import { CollationOptions, CommandOperation, CommandOperationOptions } from './command'; import { Aspect, defineAspects, Hint } from './operation'; @@ -113,15 +107,6 @@ export class UpdateOperation extends CommandOperation { command.comment = options.comment; } - const statementWithCollation = this.statements.find(statement => !!statement.collation); - if ( - collationNotSupported(server, options) || - (statementWithCollation && collationNotSupported(server, statementWithCollation)) - ) { - callback(new MongoCompatibilityError(`Server ${server.name} does not support collation`)); - return; - } - const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0; if (unacknowledgedWrite || maxWireVersion(server) < 5) { if (this.statements.find((o: Document) => o.hint)) { @@ -130,20 +115,6 @@ export class UpdateOperation extends CommandOperation { } } - if (this.explain && maxWireVersion(server) < 3) { - callback( - new MongoCompatibilityError(`Server ${server.name} does not support explain on update`) - ); - return; - } - - if (this.statements.some(statement => !!statement.arrayFilters) && maxWireVersion(server) < 6) { - callback( - new MongoCompatibilityError('Option "arrayFilters" is only supported on MongoDB 3.6+') - ); - return; - } - super.executeCommand(server, session, command, callback); } } diff --git a/src/sdam/server.ts b/src/sdam/server.ts index 3560ef9cfe..fbff5ba902 100644 --- a/src/sdam/server.ts +++ b/src/sdam/server.ts @@ -23,7 +23,6 @@ import { isNetworkErrorBeforeHandshake, isNodeShuttingDownError, isSDAMUnrecoverableError, - MongoCompatibilityError, MongoError, MongoErrorLabel, MongoInvalidArgumentError, @@ -41,7 +40,6 @@ import type { ClientSession } from '../sessions'; import { isTransactionCommand } from '../transactions'; import { Callback, - collationNotSupported, EventEmitterWithState, makeStateMachine, maxWireVersion, @@ -314,12 +312,6 @@ export class Server extends TypedEventEmitter { delete finalOptions.readPreference; } - // error if collation not supported - if (collationNotSupported(this, cmd)) { - callback(new MongoCompatibilityError(`Server ${this.name} does not support collation`)); - return; - } - const session = finalOptions.session; const conn = session?.pinnedConnection; diff --git a/src/utils.ts b/src/utils.ts index b2321d3a8c..f3349b6eca 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -563,17 +563,6 @@ export function maxWireVersion(topologyOrServer?: Connection | Topology | Server return 0; } -/** - * Checks that collation is supported by server. - * @internal - * - * @param server - to check against - * @param cmd - object where collation may be specified - */ -export function collationNotSupported(server: Server, cmd: Document): boolean { - return cmd && cmd.collation && maxWireVersion(server) < 5; -} - /** * Applies the function `eachFn` to each item in `arr`, in parallel. * @internal From a36535d49d5e7ce5693b5dea77afffce288417ac Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 26 Sep 2022 13:59:52 -0400 Subject: [PATCH 2/3] chore: remove unnecessary wire version checks in update and delete --- src/operations/delete.ts | 4 ++-- src/operations/update.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/operations/delete.ts b/src/operations/delete.ts index 319d14f208..0ce9d871de 100644 --- a/src/operations/delete.ts +++ b/src/operations/delete.ts @@ -3,7 +3,7 @@ import type { Collection } from '../collection'; import { MongoCompatibilityError, MongoServerError } from '../error'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; -import { Callback, maxWireVersion, MongoDBNamespace } from '../utils'; +import type { Callback, MongoDBNamespace } from '../utils'; import type { WriteConcernOptions } from '../write_concern'; import { CollationOptions, CommandOperation, CommandOperationOptions } from './command'; import { Aspect, defineAspects, Hint } from './operation'; @@ -83,7 +83,7 @@ export class DeleteOperation extends CommandOperation { } const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0; - if (unacknowledgedWrite || maxWireVersion(server) < 5) { + if (unacknowledgedWrite) { if (this.statements.find((o: Document) => o.hint)) { callback(new MongoCompatibilityError(`Servers < 3.4 do not support hint on delete`)); return; diff --git a/src/operations/update.ts b/src/operations/update.ts index 510ff73543..8dd1de3bb7 100644 --- a/src/operations/update.ts +++ b/src/operations/update.ts @@ -3,7 +3,7 @@ import type { Collection } from '../collection'; import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; -import { Callback, hasAtomicOperators, maxWireVersion, MongoDBNamespace } from '../utils'; +import { Callback, hasAtomicOperators, MongoDBNamespace } from '../utils'; import { CollationOptions, CommandOperation, CommandOperationOptions } from './command'; import { Aspect, defineAspects, Hint } from './operation'; @@ -108,7 +108,7 @@ export class UpdateOperation extends CommandOperation { } const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0; - if (unacknowledgedWrite || maxWireVersion(server) < 5) { + if (unacknowledgedWrite) { if (this.statements.find((o: Document) => o.hint)) { callback(new MongoCompatibilityError(`Servers < 3.4 do not support hint on update`)); return; From cd4acfe7fdeaabde8f4540957f7d937dc3f9f442 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 26 Sep 2022 14:00:12 -0400 Subject: [PATCH 3/3] chore: add back wire version check to connection --- src/cmap/connection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 0bfc0b2663..416b0ee855 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -645,7 +645,7 @@ function supportsOpMsg(conn: Connection) { return false; } - return !description.__nodejs_mock_server__; + return maxWireVersion(conn) >= 6 && !description.__nodejs_mock_server__; } function streamIdentifier(stream: Stream, options: ConnectionOptions): string {