diff --git a/packages/compass-shell/package.json b/packages/compass-shell/package.json index 27e3090942..5f718205ee 100644 --- a/packages/compass-shell/package.json +++ b/packages/compass-shell/package.json @@ -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" }, diff --git a/packages/compass-shell/src/modules/runtime.js b/packages/compass-shell/src/modules/runtime.js index 63df9652b7..c47ae736c3 100644 --- a/packages/compass-shell/src/modules/runtime.js +++ b/packages/compass-shell/src/modules/runtime.js @@ -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'; @@ -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, @@ -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, @@ -100,7 +90,7 @@ function createWorkerRuntime(dataService, appRegistry) { return new WorkerRuntime( driverUrl, driverOptions, - {}, + cliOptions ?? {}, { env: { ...process.env, ELECTRON_RUN_AS_NODE: 1 }, serialization: 'advanced', diff --git a/packages/compass-shell/src/modules/worker-runtime.js b/packages/compass-shell/src/modules/worker-runtime.js index bae1bc06b2..254b05d66c 100644 --- a/packages/compass-shell/src/modules/worker-runtime.js +++ b/packages/compass-shell/src/modules/worker-runtime.js @@ -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); diff --git a/packages/compass-shell/src/stores/store.spec.js b/packages/compass-shell/src/stores/store.spec.js index 28141a881c..ee0b37beb9 100644 --- a/packages/compass-shell/src/stores/store.spec.js +++ b/packages/compass-shell/src/stores/store.spec.js @@ -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); @@ -20,8 +45,6 @@ describe('CompassShellStore [Store]', () => { }); describe('runtime', () => { - const getRuntimeState = () => store.reduxStore.getState().runtime; - it('has initialized runtime state', () => { const runtimeState = getRuntimeState(); @@ -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); }); @@ -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();