Socket
Socket
Sign inDemoInstall

@mongosh/service-provider-server

Package Overview
Dependencies
Maintainers
8
Versions
120
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.2.2 to 0.3.0

9

lib/cli-service-provider.d.ts
import { MongoClient } from 'mongodb';
import { ServiceProvider, Document, Cursor, Result, BulkWriteResult, DatabaseOptions, WriteConcern, CommandOptions, ReplPlatform, ServiceProviderCore, AuthOptions } from '@mongosh/service-provider-core';
import { ServiceProvider, Document, Cursor, Result, BulkWriteResult, DatabaseOptions, WriteConcern, CommandOptions, ReplPlatform, ServiceProviderCore, AuthOptions, ReadConcern, ReadPreference } from '@mongosh/service-provider-core';
import NodeOptions from './node/node-options';

@@ -12,3 +12,3 @@ declare type DropDatabaseResult = {

static connect(uri: string, options?: NodeOptions, cliOptions?: any): Promise<CliServiceProvider>;
private mongoClient;
mongoClient: MongoClient;
private readonly uri?;

@@ -44,2 +44,3 @@ private initialOptions;

runCommand(database: string, spec?: Document, options?: CommandOptions, dbOptions?: DatabaseOptions): Promise<Result>;
runCommandWithCheck(database: string, spec?: Document, options?: CommandOptions, dbOptions?: DatabaseOptions): Promise<Result>;
listDatabases(database: string): Promise<Result>;

@@ -62,3 +63,7 @@ updateMany(database: string, collection: string, filter?: Document, update?: Document, options?: Document, dbOptions?: DatabaseOptions): Promise<Result>;

initializeBulkOp(dbName: string, collName: string, ordered: boolean, options?: {}, dbOptions?: any): Promise<any>;
getReadPreference(): ReadPreference;
getReadConcern(): ReadConcern | undefined;
getWriteConcern(): WriteConcern | undefined;
resetConnectionOptions(options: Document): Promise<void>;
}
export default CliServiceProvider;

@@ -26,2 +26,21 @@ "use strict";

};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

@@ -63,12 +82,6 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var mongodb_1 = __importStar(require("mongodb"));
var service_provider_core_1 = require("@mongosh/service-provider-core");
var errors_1 = require("@mongosh/errors");
var DEFAULT_DRIVER_OPTIONS = Object.freeze({

@@ -209,3 +222,3 @@ useNewUrlParser: true,

options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options);
return [4, this.runCommand(database, {
return [4, this.runCommandWithCheck(database, {
convertToCapped: collection,

@@ -398,2 +411,23 @@ size: size

};
CliServiceProvider.prototype.runCommandWithCheck = function (database, spec, options, dbOptions) {
if (spec === void 0) { spec = {}; }
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var db, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options);
db = this.db(database, dbOptions);
return [4, db.command(spec, options)];
case 1:
result = _a.sent();
if (result.ok === 0) {
throw new errors_1.MongoshCommandFailed(JSON.stringify(spec));
}
return [2, result];
}
});
});
};
CliServiceProvider.prototype.listDatabases = function (database) {

@@ -436,3 +470,3 @@ return this.db(database).admin().listDatabases();

switch (_a.label) {
case 0: return [4, this.runCommand('admin', {
case 0: return [4, this.runCommandWithCheck('admin', {
buildInfo: 1

@@ -454,3 +488,3 @@ }, {})];

switch (_a.label) {
case 0: return [4, this.runCommand('admin', { getCmdLineOpts: 1 }, {})];
case 0: return [4, this.runCommandWithCheck('admin', { getCmdLineOpts: 1 }, {})];
case 1:

@@ -510,3 +544,3 @@ result = _a.sent();

options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options);
return [4, this.runCommand(database, {
return [4, this.runCommandWithCheck(database, {
dropIndexes: collection,

@@ -556,3 +590,3 @@ index: indexes,

options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options);
return [4, this.runCommand(database, {
return [4, this.runCommandWithCheck(database, {
reIndex: collection

@@ -628,2 +662,41 @@ }, options, dbOptions)];

};
CliServiceProvider.prototype.getReadPreference = function () {
return this.mongoClient.readPreference;
};
CliServiceProvider.prototype.getReadConcern = function () {
return this.mongoClient.s.options.readConcern;
};
CliServiceProvider.prototype.getWriteConcern = function () {
return this.mongoClient.writeConcern;
};
CliServiceProvider.prototype.resetConnectionOptions = function (options) {
return __awaiter(this, void 0, void 0, function () {
var pr, clientOptions, _a, mc;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if ('readPreference' in options) {
pr = new mongodb_1.ReadPreference(options.readPreference.mode, options.readPreference.tagSet, options.hedgeOptions);
options.readPreference = pr;
}
clientOptions = __assign(__assign(__assign({}, DEFAULT_DRIVER_OPTIONS), this.initialOptions), options);
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4, this.mongoClient.close()];
case 2:
_b.sent();
return [3, 4];
case 3:
_a = _b.sent();
return [3, 4];
case 4: return [4, mongodb_1.MongoClient.connect(this.uri, clientOptions)];
case 5:
mc = _b.sent();
this.mongoClient = mc;
return [2];
}
});
});
};
return CliServiceProvider;

@@ -630,0 +703,0 @@ }(service_provider_core_1.ServiceProviderCore));

@@ -6,2 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.generateUri = exports.Scheme = exports.DEFAULT_DB = exports.CompassServiceProvider = exports.CliServiceProvider = void 0;
var cli_service_provider_1 = __importDefault(require("./cli-service-provider"));

@@ -12,5 +13,5 @@ exports.CliServiceProvider = cli_service_provider_1.default;

var service_provider_core_1 = require("@mongosh/service-provider-core");
exports.DEFAULT_DB = service_provider_core_1.DEFAULT_DB;
exports.Scheme = service_provider_core_1.Scheme;
exports.generateUri = service_provider_core_1.generateUri;
Object.defineProperty(exports, "DEFAULT_DB", { enumerable: true, get: function () { return service_provider_core_1.DEFAULT_DB; } });
Object.defineProperty(exports, "Scheme", { enumerable: true, get: function () { return service_provider_core_1.Scheme; } });
Object.defineProperty(exports, "generateUri", { enumerable: true, get: function () { return service_provider_core_1.generateUri; } });
//# sourceMappingURL=index.js.map
{
"name": "@mongosh/service-provider-server",
"version": "0.2.2",
"version": "0.3.0",
"description": "MongoDB Shell Server Service Provider Package",

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

"dependencies": {
"@mongosh/errors": "^0.2.2",
"@mongosh/service-provider-core": "^0.2.2",
"@mongosh/errors": "^0.3.0",
"@mongosh/service-provider-core": "^0.3.0",
"@types/sinon": "^7.5.1",
"@types/sinon-chai": "^3.2.3",
"mongodb": "3.5.3 || ^3.5.5"
"mongodb": "^3.6.1"
},
"gitHead": "4e188a4336bb5f57bc2431811e54b90bf83b1cdf"
"gitHead": "decab9f8f132403125d7084922a0b7a84afc8098"
}

@@ -399,2 +399,36 @@ import mongodb, { MongoClient, Db } from 'mongodb';

describe('#runCommandWithCheck', () => {
let clientStub;
let dbStub;
const commandResult = { ok: 0 };
const commandMock = sinon.mock().withArgs({ ismaster: 1 }).resolves(commandResult);
beforeEach(() => {
dbStub = sinon.createStubInstance(Db, {
command: commandMock
});
clientStub = sinon.createStubInstance(MongoClient, {
db: sinon.stub().returns(dbStub)
});
serviceProvider = new CliServiceProvider(clientStub);
});
afterEach(() => {
dbStub = null;
clientStub = null;
serviceProvider = null;
});
it('executes the command against the database and throws if ok: 0', async() => {
try {
await serviceProvider.runCommandWithCheck('admin', { ismaster: 1 });
} catch (e) {
expect(e.message).to.include(JSON.stringify({ ismaster: 1 }));
expect(e.name).to.equal('MongoshCommandFailed');
return;
}
expect.fail('Error not thrown');
});
});
describe('#updateOne', () => {

@@ -908,2 +942,38 @@ const filter = { name: 'Aphex Twin' };

});
describe('Throw error on ok: 0', () => {
let clientStub;
let dbStub;
let commandMock;
beforeEach(() => {
commandMock = sinon.mock().resolves({ ok: 0 });
dbStub = sinon.createStubInstance(Db, {
command: commandMock
});
clientStub = sinon.createStubInstance(MongoClient, {
db: sinon.stub().returns(dbStub)
});
serviceProvider = new CliServiceProvider(clientStub);
});
afterEach(() => {
dbStub = null;
clientStub = null;
serviceProvider = null;
});
['convertToCapped', 'buildInfo', 'getCmdLineOpts', 'dropIndexes', 'reIndex'].forEach((cmd) => {
it(cmd, async() => {
try {
await serviceProvider[cmd]('db', 'coll', 1);
} catch (e) {
expect(e.message).to.include(cmd);
expect(e.name).to.equal('MongoshCommandFailed');
return;
}
expect.fail(`Error not thrown for ok:0 on cmd ${cmd}`);
});
});
});
});
import mongodb, {
MongoClient,
Db
Db,
ReadPreference as DriverReadPreference
} from 'mongodb';

@@ -19,3 +20,5 @@

ServiceProviderCore,
AuthOptions
AuthOptions,
ReadConcern,
ReadPreference
} from '@mongosh/service-provider-core';

@@ -25,2 +28,4 @@

import { MongoshCommandFailed } from '@mongosh/errors';
type DropDatabaseResult = {

@@ -81,3 +86,3 @@ ok: 0 | 1;

private mongoClient: MongoClient;
public mongoClient: MongoClient; // public for testing
private readonly uri?: string;

@@ -189,3 +194,3 @@ private initialOptions: any;

options = { ...DEFAULT_BASE_OPTIONS, ...options };
const result: any = await this.runCommand(
const result: any = await this.runCommandWithCheck(
database,

@@ -724,2 +729,3 @@ {

* @param {Object} options - The database options.
* @param {Object} dbOptions - The connection-wide database options.
*

@@ -743,2 +749,30 @@ * @returns {Promise} The promise of command results.

/**
* Run a command against the database and check the results for ok: 0.
*
* @param {String} database - The database name.
* @param {Object} spec - The command specification.
* @param {Object} options - The database options.
* @param {Object} dbOptions - The connection-wide database options.
*
* @returns {Promise} The promise of command results.
*/
async runCommandWithCheck(
database: string,
spec: Document = {},
options: CommandOptions = {},
dbOptions?: DatabaseOptions
): Promise<Result> {
options = { ...DEFAULT_BASE_OPTIONS, ...options };
const db: any = this.db(database, dbOptions);
const result = await db.command(
spec,
options
);
if (result.ok === 0) {
throw new MongoshCommandFailed(JSON.stringify(spec));
}
return result;
}
/**
* list databases.

@@ -819,3 +853,3 @@ *

async buildInfo(): Promise<Result> {
const result: any = await this.runCommand(
const result: any = await this.runCommandWithCheck(
'admin',

@@ -839,3 +873,3 @@ {

async getCmdLineOpts(): Promise<Result> {
const result: any = await this.runCommand(
const result: any = await this.runCommandWithCheck(
'admin', { getCmdLineOpts: 1 }, {}

@@ -933,3 +967,3 @@ );

options = { ...DEFAULT_BASE_OPTIONS, ...options };
return await this.runCommand(database, {
return await this.runCommandWithCheck(database, {
dropIndexes: collection,

@@ -998,3 +1032,3 @@ index: indexes,

options = { ...DEFAULT_BASE_OPTIONS, ...options };
return await this.runCommand(database, {
return await this.runCommandWithCheck(database, {
reIndex: collection

@@ -1078,4 +1112,48 @@ }, options, dbOptions);

}
getReadPreference(): ReadPreference {
return this.mongoClient.readPreference; // TODO: node driver bug always returns primary NODE-2806
}
getReadConcern(): ReadConcern | undefined {
// return this.mongoClient.readConcern; TODO: this is only on latest driver, for now use workaround
return this.mongoClient.s.options.readConcern;
}
getWriteConcern(): WriteConcern | undefined {
return this.mongoClient.writeConcern;
}
/**
* For instances where a user wants to set a option that requires a new MongoClient.
*
* @param options
*/
async resetConnectionOptions(options: Document): Promise<void> {
// NOTE: we keep all the original options and just overwrite the passed.
if ('readPreference' in options) {
const pr = new DriverReadPreference(
options.readPreference.mode,
options.readPreference.tagSet,
options.hedgeOptions
);
options.readPreference = pr;
}
const clientOptions: any = {
...DEFAULT_DRIVER_OPTIONS,
...this.initialOptions,
...options
};
try {
await this.mongoClient.close();
// eslint-disable-next-line no-empty
} catch {}
const mc = await MongoClient.connect(
this.uri,
clientOptions
);
this.mongoClient = mc;
}
}
export default CliServiceProvider;

Sorry, the diff of this file is not supported yet

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