Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mongosh/service-provider-server

Package Overview
Dependencies
Maintainers
11
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mongosh/service-provider-server - npm Package Compare versions

Comparing version 0.7.7 to 0.8.0

13

lib/cli-service-provider.d.ts

@@ -1,3 +0,3 @@

import { MongoClient, ReadPreference, ClientMetadata, Topology, ReadPreferenceFromOptions, ReadPreferenceLike } from 'mongodb';
import { ServiceProvider, getConnectInfo, ReplPlatform, ServiceProviderCore, ShellAuthOptions, AggregateOptions, AggregationCursor, AnyBulkWriteOperation, BulkWriteOptions, BulkWriteResult, ClientSessionOptions, CollStatsOptions, Collection, CountDocumentsOptions, CountOptions, CreateCollectionOptions, CreateIndexesOptions, FindCursor, Db, DbOptions, DeleteOptions, DeleteResult, DistinctOptions, Document, DropCollectionOptions, DropDatabaseOptions, EstimatedDocumentCountOptions, FindAndModifyOptions, FindOptions, IndexDescription, InsertManyResult, InsertOneOptions, InsertOneResult, ListCollectionsOptions, ListDatabasesOptions, ListIndexesOptions, MongoClientOptions, ReadConcern, RenameOptions, ReplaceOptions, RunCommandOptions, ClientSession, UpdateOptions, UpdateResult, WriteConcern, ChangeStreamOptions, ChangeStream, ConnectionString } from '@mongosh/service-provider-core';
import { MongoClient, ReadPreference, ClientMetadata, Topology, ReadPreferenceFromOptions, ReadPreferenceLike, OperationOptions } from 'mongodb';
import { ServiceProvider, getConnectInfo, ReplPlatform, ServiceProviderCore, ShellAuthOptions, AggregateOptions, AggregationCursor, AnyBulkWriteOperation, BulkWriteOptions, BulkWriteResult, ClientSessionOptions, CollStatsOptions, Collection, CountDocumentsOptions, CountOptions, CreateCollectionOptions, CreateIndexesOptions, FindCursor, Db, DbOptions, DeleteOptions, DeleteResult, DistinctOptions, Document, DropCollectionOptions, DropDatabaseOptions, EstimatedDocumentCountOptions, FindAndModifyOptions, FindOptions, IndexDescription, InsertManyResult, InsertOneOptions, InsertOneResult, ListCollectionsOptions, ListDatabasesOptions, ListIndexesOptions, MongoClientOptions, ReadConcern, RenameOptions, ReplaceOptions, RunCommandOptions, ClientSession, UpdateOptions, UpdateResult, WriteConcern, ChangeStreamOptions, ChangeStream, ConnectionString, FLE, AutoEncryptionOptions } from '@mongosh/service-provider-core';
declare type DropDatabaseResult = {

@@ -22,6 +22,6 @@ ok: 0 | 1;

private readonly uri?;
private initialOptions;
private currentClientOptions;
private dbcache;
baseCmdOptions: any;
fle: any;
baseCmdOptions: OperationOptions;
fle: FLE | undefined;
constructor(mongoClient: MongoClient, clientOptions?: MongoClientOptions, uri?: ConnectionString);

@@ -67,3 +67,3 @@ getNewConnection(uri: string, options?: MongoClientOptions): Promise<CliServiceProvider>;

authenticate(authDoc: ShellAuthOptions): Promise<{
ok: number;
ok: 1;
}>;

@@ -84,3 +84,4 @@ createCollection(dbName: string, collName: string, options?: CreateCollectionOptions, dbOptions?: DbOptions): Promise<{

getURI(): string | undefined;
getFleOptions(): AutoEncryptionOptions | undefined;
}
export default CliServiceProvider;

@@ -64,3 +64,3 @@ "use strict";

}
this.initialOptions = clientOptions;
this.currentClientOptions = clientOptions;
this.baseCmdOptions = { ...DEFAULT_BASE_OPTIONS };

@@ -89,5 +89,21 @@ this.dbcache = new WeakMap();

var _a, _b;
const buildInfo = await this.runCommandWithCheck('admin', {
buildInfo: 1
}, this.baseCmdOptions);
let buildInfo;
try {
buildInfo = await this.runCommandWithCheck('admin', {
buildInfo: 1
}, this.baseCmdOptions);
}
catch (e) {
if (e.message.includes('not supported for auto encryption')) {
const options = { ...this.currentClientOptions };
delete options.autoEncryption;
const unencrypted = await this.getNewConnection(this.uri.toString(), options);
try {
return await unencrypted.getConnectionInfo();
}
finally {
await unencrypted.close(true);
}
}
}
const topology = this.getTopology();

@@ -321,21 +337,11 @@ const { version } = require('../package.json');

const auth = { username: authDoc.user, password: authDoc.pwd };
const clientOptions = processDriverOptions({
...this.initialOptions,
auth
await this.resetConnectionOptions({
auth,
...(authDoc.mechanism ? { authMechanism: authDoc.mechanism } : {}),
...(authDoc.authDb ? { authSource: authDoc.authDb } : {})
});
if (authDoc.mechanism)
clientOptions.authMechanism = authDoc.mechanism;
if (authDoc.authDb)
clientOptions.authSource = authDoc.authDb;
const mc = await connectMongoClient(Object.assign(this.uri.clone(), {
username: '', password: ''
}).toString(), clientOptions);
try {
await this.mongoClient.close();
}
catch (_a) { }
this.mongoClient = mc;
return { ok: 1 };
}
async createCollection(dbName, collName, options = {}, dbOptions) {
options = { ...this.baseCmdOptions, ...options };
await this.db(dbName, dbOptions).createCollection(collName, options);

@@ -363,6 +369,7 @@ return { ok: 1 };

async resetConnectionOptions(options) {
const clientOptions = processDriverOptions({
...this.initialOptions,
this.currentClientOptions = {
...this.currentClientOptions,
...options
});
};
const clientOptions = processDriverOptions(this.currentClientOptions);
const mc = await connectMongoClient(this.uri.toString(), clientOptions);

@@ -401,4 +408,7 @@ try {

}
getFleOptions() {
return this.currentClientOptions.autoEncryption;
}
}
exports.default = CliServiceProvider;
//# sourceMappingURL=cli-service-provider.js.map
{
"name": "@mongosh/service-provider-server",
"version": "0.7.7",
"version": "0.8.0",
"description": "MongoDB Shell Server Service Provider Package",

@@ -41,4 +41,4 @@ "main": "lib/index.js",

"dependencies": {
"@mongosh/errors": "0.7.7",
"@mongosh/service-provider-core": "0.7.7",
"@mongosh/errors": "0.8.0",
"@mongosh/service-provider-core": "0.8.0",
"@types/sinon": "^7.5.1",

@@ -53,5 +53,5 @@ "@types/sinon-chai": "^3.2.3",

"optionalDependencies": {
"mongodb-client-encryption": "^1.2.0"
"mongodb-client-encryption": "^1.2.1"
},
"gitHead": "9d8be6957c3ef53f4cdaa23547225806652f8ef9"
"gitHead": "9bd4a43c19a598679ff2945fa40787097074167d"
}

@@ -7,2 +7,3 @@ import { CommonErrors } from '@mongosh/errors';

import CliServiceProvider, { connectMongoClient } from './cli-service-provider';
import { ConnectionString } from '@mongosh/service-provider-core';

@@ -729,3 +730,3 @@ chai.use(sinonChai);

expect(result).to.deep.equal({ ok: 1 });
expect(dbStub.createCollection).to.have.been.calledOnceWith('newcoll', {});
expect(dbStub.createCollection).to.have.been.calledOnceWith('newcoll', DEFAULT_BASE_OPTS);
expect(clientStub.db).to.have.been.calledOnceWith('db1');

@@ -806,2 +807,37 @@ });

});
describe('#getConnectionInfo', () => {
let clientStub: any;
let dbStub: any;
let firstCall = true;
beforeEach(() => {
dbStub = stubInterface<Db>();
clientStub = stubInterface<MongoClient>();
dbStub.command.callsFake(() => {
if (firstCall) {
firstCall = false;
throw new Error('some command not supported for auto encryption');
}
return { ok: 1 };
});
clientStub.db.returns(dbStub);
clientStub.topology = { s: {} };
serviceProvider = new CliServiceProvider(clientStub, {}, new ConnectionString('mongodb://localhost/'));
serviceProvider.getNewConnection = async() => serviceProvider;
});
afterEach(() => {
dbStub = null;
clientStub = null;
serviceProvider = null;
});
it('returns some connection info data', async() => {
const info = await serviceProvider.getConnectionInfo();
expect(info.extraInfo.is_atlas).to.equal(false);
expect(info.extraInfo.is_localhost).to.equal(true);
expect(dbStub.command).to.have.callCount(3);
});
});
});

@@ -21,3 +21,4 @@ import {

ReadPreferenceFromOptions,
ReadPreferenceLike
ReadPreferenceLike,
OperationOptions
} from 'mongodb';

@@ -76,3 +77,5 @@

bson as BSON,
ConnectionString
ConnectionString,
FLE,
AutoEncryptionOptions
} from '@mongosh/service-provider-core';

@@ -114,3 +117,3 @@

*/
const DEFAULT_DRIVER_OPTIONS = Object.freeze({
const DEFAULT_DRIVER_OPTIONS: MongoClientOptions = Object.freeze({
});

@@ -129,3 +132,3 @@

*/
const DEFAULT_BASE_OPTIONS = Object.freeze({
const DEFAULT_BASE_OPTIONS: OperationOptions = Object.freeze({
serializeFunctions: true

@@ -198,6 +201,6 @@ });

private readonly uri?: ConnectionString;
private initialOptions: MongoClientOptions;
private currentClientOptions: MongoClientOptions;
private dbcache: WeakMap<MongoClient, Map<string, Db>>;
public baseCmdOptions: any; // public for testing
public fle: any;
public baseCmdOptions: OperationOptions; // public for testing
public fle: FLE | undefined;

@@ -222,3 +225,3 @@ /**

}
this.initialOptions = clientOptions;
this.currentClientOptions = clientOptions;
this.baseCmdOptions = { ... DEFAULT_BASE_OPTIONS }; // currently do not have any user-specified connection-wide command options, but I imagine we will eventually

@@ -242,5 +245,23 @@ this.dbcache = new WeakMap();

async getConnectionInfo(): Promise<ConnectionInfo> {
const buildInfo = await this.runCommandWithCheck('admin', {
buildInfo: 1
}, this.baseCmdOptions);
// buildInfo try/catch can be removed after MONGOCRYPT-308
let buildInfo;
try {
buildInfo = await this.runCommandWithCheck('admin', {
buildInfo: 1
}, this.baseCmdOptions);
} catch (e) {
if (e.message.includes('not supported for auto encryption')) {
const options = { ...this.currentClientOptions };
delete options.autoEncryption;
const unencrypted =
await this.getNewConnection(
(this.uri as ConnectionString).toString(),
options);
try {
return await unencrypted.getConnectionInfo();
} finally {
await unencrypted.close(true);
}
}
}
const topology = this.getTopology() as Topology;

@@ -1057,22 +1078,10 @@ const { version } = require('../package.json');

authDoc: ShellAuthOptions
): Promise<{ ok: number }> {
): Promise<{ ok: 1 }> {
// NOTE: we keep all the original options and just overwrite the auth ones.
const auth: Auth = { username: authDoc.user, password: authDoc.pwd };
// NOTE: we keep all the original options and just overwrite the auth ones.
const clientOptions = processDriverOptions({
...this.initialOptions,
auth
await this.resetConnectionOptions({
auth,
...(authDoc.mechanism ? { authMechanism: authDoc.mechanism as AuthMechanismId } : {}),
...(authDoc.authDb ? { authSource: authDoc.authDb } : {})
});
if (authDoc.mechanism) clientOptions.authMechanism = authDoc.mechanism as AuthMechanismId;
if (authDoc.authDb) clientOptions.authSource = authDoc.authDb;
const mc = await connectMongoClient(
Object.assign((this.uri as ConnectionString).clone(), {
username: '', password: ''
}).toString(),
clientOptions
);
try {
await this.mongoClient.close();
// eslint-disable-next-line no-empty
} catch {}
this.mongoClient = mc;
return { ok: 1 };

@@ -1087,2 +1096,3 @@ }

): Promise<{ ok: number }> {
options = { ...this.baseCmdOptions, ...options };
await this.db(dbName, dbOptions).createCollection(

@@ -1129,8 +1139,8 @@ collName, options

async resetConnectionOptions(options: MongoClientOptions): Promise<void> {
const clientOptions = processDriverOptions({
...this.initialOptions,
this.currentClientOptions = {
...this.currentClientOptions,
...options
});
};
const clientOptions = processDriverOptions(this.currentClientOptions);
const mc = await connectMongoClient(
// TODO This seems to potentially undo a previous db.auth(), MONGOSH-529
(this.uri as ConnectionString).toString(),

@@ -1172,4 +1182,8 @@ clientOptions

}
getFleOptions(): AutoEncryptionOptions | undefined {
return this.currentClientOptions.autoEncryption;
}
}
export default CliServiceProvider;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc