Skip to content

Commit

Permalink
fix: versioned api low node compat fix (#2970)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariakp authored Aug 31, 2021
1 parent 7602f68 commit 1a76618
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ try {
const ServerApiVersion = Object.freeze({
v1: '1'
});
const ValidServerApiVersions = Object.keys(ServerApiVersion).map(key => ServerApiVersion[key]);

module.exports = {
// Versioned API
ServerApiVersion,
ValidServerApiVersions,
// Errors
MongoError: require('./error').MongoError,
MongoNetworkError: require('./error').MongoNetworkError,
Expand Down
16 changes: 8 additions & 8 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Db = require('./db');
const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;
const MongoError = require('./core').MongoError;
const ServerApiVersion = require('./core').ServerApiVersion;
const ValidServerApiVersions = require('./core').ValidServerApiVersions;
const deprecate = require('util').deprecate;
const WriteConcern = require('./write_concern');
const MongoDBNamespace = require('./utils').MongoDBNamespace;
Expand Down Expand Up @@ -206,16 +206,16 @@ function MongoClient(url, options) {
const versionToValidate = serverApiToValidate && serverApiToValidate.version;
if (!versionToValidate) {
throw new MongoError(
`Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values(
ServerApiVersion
).join('", "')}"]`
`Invalid \`serverApi\` property; must specify a version from the following enum: ["${ValidServerApiVersions.join(
'", "'
)}"]`
);
}
if (!Object.values(ServerApiVersion).some(v => v === versionToValidate)) {
if (!ValidServerApiVersions.some(v => v === versionToValidate)) {
throw new MongoError(
`Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values(
ServerApiVersion
).join('", "')}"]`
`Invalid server API version=${versionToValidate}; must be in the following enum: ["${ValidServerApiVersions.join(
'", "'
)}"]`
);
}
options.serverApi = serverApiToValidate;
Expand Down
7 changes: 5 additions & 2 deletions test/functional/unified-spec-runner/unified-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { expect } from 'chai';
import type { CollectionOrDatabaseOptions, RunOnRequirement, Document } from './schema';
import { gte as semverGte, lte as semverLte } from 'semver';
import { MongoClient } from '../../../index';
import { isDeepStrictEqual } from 'util';
import { TestConfiguration } from './runner';

export async function topologySatisfies(
Expand Down Expand Up @@ -41,7 +40,11 @@ export async function topologySatisfies(
if (!config.parameters) throw new Error('Configuration does not have server parameters');
for (const [name, value] of Object.entries(r.serverParameters)) {
if (name in config.parameters) {
ok &&= isDeepStrictEqual(config.parameters[name], value);
try {
expect(config.parameters[name]).to.deep.equal(value);
} catch (_err) {
ok = false;
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions test/functional/versioned-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
const expect = require('chai').expect;
const loadSpecTests = require('../spec/index').loadSpecTests;
const runUnifiedTest = require('./unified-spec-runner/runner').runUnifiedTest;
const ServerApiVersion = require('../../lib/core').ServerApiVersion;
const validVersions = require('../../lib/core').ValidServerApiVersions;

describe('Versioned API', function() {
describe('client option validation', function() {
it('is supported as a client option when it is a valid ServerApiVersion string', function() {
const validVersions = Object.values(ServerApiVersion);
expect(validVersions.length).to.be.at.least(1);
for (const version of validVersions) {
const client = this.configuration.newClient('mongodb://localhost/', {
Expand All @@ -21,7 +20,6 @@ describe('Versioned API', function() {
});

it('is supported as a client option when it is an object with a valid version property', function() {
const validVersions = Object.values(ServerApiVersion);
expect(validVersions.length).to.be.at.least(1);
for (const version of validVersions) {
const client = this.configuration.newClient('mongodb://localhost/', {
Expand Down

0 comments on commit 1a76618

Please sign in to comment.