Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/compass-shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@
"@leafygreen-ui/icon": "^5.1.0",
"@leafygreen-ui/icon-button": "^5.0.2",
"@mongosh/browser-repl": "0.0.0-dev.0",
"@mongosh/browser-runtime-electron": "0.0.0-dev.0",
"@mongosh/node-runtime-worker-thread": "0.0.0-dev.0",
"@mongosh/service-provider-core": "0.0.0-dev.0",
"@mongosh/service-provider-server": "0.0.0-dev.0",
"hadron-react-buttons": "^4.0.4",
"mongodb-redux-common": "0.0.2"
},
Expand Down
24 changes: 7 additions & 17 deletions packages/compass-shell/src/modules/runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { CompassServiceProvider } from '@mongosh/service-provider-server';
import { WorkerRuntime } from './worker-runtime';
import { adaptDriverV36ConnectionParams } from './adapt-driver-v36-connection-params';

Expand Down Expand Up @@ -47,18 +45,7 @@ function reduceSetupRuntime(state, action) {
return state;
}

const shouldUseNewRuntime = !!process.env
.COMPASS_SHELL_EXPERIMENTAL_WORKER_RUNTIME;

const runtime = shouldUseNewRuntime ?
createWorkerRuntime(
action.dataService,
action.appRegistry
) :
new ElectronRuntime(
CompassServiceProvider.fromDataService(action.dataService),
action.appRegistry
);
const runtime = createWorkerRuntime(action.dataService, action.appRegistry);

return {
error: action.error,
Expand Down Expand Up @@ -86,10 +73,13 @@ export const setupRuntime = (error, dataService, appRegistry) => ({
function createWorkerRuntime(dataService, appRegistry) {
const {
url: driverV36Url,
options: driverV36Options
options: driverV36Options,
// Not really provided by dataService, used only for testing purposes
cliOptions,
} = dataService.getConnectionOptions();

const connectionModelDriverOptions = dataService?.client?.model?.driverOptions || {};
const connectionModelDriverOptions =
dataService?.client?.model?.driverOptions ?? {};

const [ driverUrl, driverOptions ] = adaptDriverV36ConnectionParams(
driverV36Url,
Expand All @@ -100,7 +90,7 @@ function createWorkerRuntime(dataService, appRegistry) {
return new WorkerRuntime(
driverUrl,
driverOptions,
{},
cliOptions ?? {},
{
env: { ...process.env, ELECTRON_RUN_AS_NODE: 1 },
serialization: 'advanced',
Expand Down
3 changes: 3 additions & 0 deletions packages/compass-shell/src/modules/worker-runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createRequire } from 'module';

/**
* @type {{ WorkerRuntime: typeof import('@mongosh/node-runtime-worker-thread').WorkerRuntime }}
*/
const { WorkerRuntime } = (() => {
// Workaround for webpack require that overrides global require
const req = createRequire(__filename);
Expand Down
57 changes: 35 additions & 22 deletions packages/compass-shell/src/stores/store.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import CompassShellStore from 'stores';
import { EventEmitter } from 'events';
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { WorkerRuntime } from '../modules/worker-runtime';

function createMockDataService() {
return {
getConnectionOptions() {
return {
url: 'mongodb://nodb/',
options: {},
cliOptions: { nodb: true },
};
},
client: {
client: {},
},
};
}

describe('CompassShellStore [Store]', () => {
let store;
let appRegistry;

const getRuntimeState = () => store.reduxStore.getState().runtime;

beforeEach(() => {
store = new CompassShellStore();
appRegistry = new EventEmitter();
store.onActivated(appRegistry);
});

afterEach(async() => {
const { runtime } = getRuntimeState();

if (runtime && runtime.terminate) {
await runtime.terminate();
}
});

describe('appRegistry', () => {
it('sets the global appRegistry', () => {
expect(store.reduxStore.getState().appRegistry).to.not.equal(null);
Expand All @@ -20,8 +45,6 @@ describe('CompassShellStore [Store]', () => {
});

describe('runtime', () => {
const getRuntimeState = () => store.reduxStore.getState().runtime;

it('has initialized runtime state', () => {
const runtimeState = getRuntimeState();

Expand All @@ -30,36 +53,26 @@ describe('CompassShellStore [Store]', () => {
});

it('sets runtime on data-service-connected', () => {
appRegistry.emit('data-service-connected', null, {client: {client: {}}});
appRegistry.emit('data-service-connected', null, createMockDataService());

const runtimeState = getRuntimeState();

expect(runtimeState.error).to.equal(null);
expect(runtimeState.runtime).to.be.instanceOf(ElectronRuntime);
expect(runtimeState.runtime).to.be.instanceOf(WorkerRuntime);
});

it('emits mongosh events to the appRegistry', () => {
appRegistry.emit('data-service-connected', null, {client: {client: {
db: () => ({
admin: () => ({
listDatabases: () => Promise.resolve({
databases: [
{ name: 'db1', sizeOnDisk: 10000, empty: false }
],
totalSize: 50000,
ok: 1
})
})
})
}}});
it('emits mongosh events to the appRegistry', async() => {
appRegistry.emit('data-service-connected', null, createMockDataService());
let eventRecieved = false;
appRegistry.on('mongosh:show', () => {
appRegistry.on('mongosh:setCtx', () => {
eventRecieved = true;
});

const runtimeState = getRuntimeState();

runtimeState.runtime.evaluate('show dbs;');
// Any command will do, just making sure we waited for the runtime to
// become available
await runtimeState.runtime.evaluate('help');

expect(eventRecieved).to.equal(true);
});
Expand All @@ -75,7 +88,7 @@ describe('CompassShellStore [Store]', () => {
});

it('does not change state if dataService is the same', () => {
const fakeDataService = {client: {client: {}}};
const fakeDataService = createMockDataService();

appRegistry.emit('data-service-connected', null, fakeDataService);
const runtimeState1 = getRuntimeState();
Expand Down