Skip to content

Commit 49d296f

Browse files
authored
feat(compass-shell): Always use new worker runtime (#711)
1 parent c6eacd4 commit 49d296f

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

packages/compass-shell/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@
5757
"@leafygreen-ui/icon": "^5.1.0",
5858
"@leafygreen-ui/icon-button": "^5.0.2",
5959
"@mongosh/browser-repl": "0.0.0-dev.0",
60-
"@mongosh/browser-runtime-electron": "0.0.0-dev.0",
6160
"@mongosh/node-runtime-worker-thread": "0.0.0-dev.0",
6261
"@mongosh/service-provider-core": "0.0.0-dev.0",
63-
"@mongosh/service-provider-server": "0.0.0-dev.0",
6462
"hadron-react-buttons": "^4.0.4",
6563
"mongodb-redux-common": "0.0.2"
6664
},

packages/compass-shell/src/modules/runtime.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
2-
import { CompassServiceProvider } from '@mongosh/service-provider-server';
31
import { WorkerRuntime } from './worker-runtime';
42
import { adaptDriverV36ConnectionParams } from './adapt-driver-v36-connection-params';
53

@@ -47,18 +45,7 @@ function reduceSetupRuntime(state, action) {
4745
return state;
4846
}
4947

50-
const shouldUseNewRuntime = !!process.env
51-
.COMPASS_SHELL_EXPERIMENTAL_WORKER_RUNTIME;
52-
53-
const runtime = shouldUseNewRuntime ?
54-
createWorkerRuntime(
55-
action.dataService,
56-
action.appRegistry
57-
) :
58-
new ElectronRuntime(
59-
CompassServiceProvider.fromDataService(action.dataService),
60-
action.appRegistry
61-
);
48+
const runtime = createWorkerRuntime(action.dataService, action.appRegistry);
6249

6350
return {
6451
error: action.error,
@@ -86,10 +73,13 @@ export const setupRuntime = (error, dataService, appRegistry) => ({
8673
function createWorkerRuntime(dataService, appRegistry) {
8774
const {
8875
url: driverV36Url,
89-
options: driverV36Options
76+
options: driverV36Options,
77+
// Not really provided by dataService, used only for testing purposes
78+
cliOptions,
9079
} = dataService.getConnectionOptions();
9180

92-
const connectionModelDriverOptions = dataService?.client?.model?.driverOptions || {};
81+
const connectionModelDriverOptions =
82+
dataService?.client?.model?.driverOptions ?? {};
9383

9484
const [ driverUrl, driverOptions ] = adaptDriverV36ConnectionParams(
9585
driverV36Url,
@@ -100,7 +90,7 @@ function createWorkerRuntime(dataService, appRegistry) {
10090
return new WorkerRuntime(
10191
driverUrl,
10292
driverOptions,
103-
{},
93+
cliOptions ?? {},
10494
{
10595
env: { ...process.env, ELECTRON_RUN_AS_NODE: 1 },
10696
serialization: 'advanced',

packages/compass-shell/src/modules/worker-runtime.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createRequire } from 'module';
22

3+
/**
4+
* @type {{ WorkerRuntime: typeof import('@mongosh/node-runtime-worker-thread').WorkerRuntime }}
5+
*/
36
const { WorkerRuntime } = (() => {
47
// Workaround for webpack require that overrides global require
58
const req = createRequire(__filename);

packages/compass-shell/src/stores/store.spec.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
import CompassShellStore from 'stores';
22
import { EventEmitter } from 'events';
3-
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
3+
import { WorkerRuntime } from '../modules/worker-runtime';
4+
5+
function createMockDataService() {
6+
return {
7+
getConnectionOptions() {
8+
return {
9+
url: 'mongodb://nodb/',
10+
options: {},
11+
cliOptions: { nodb: true },
12+
};
13+
},
14+
client: {
15+
client: {},
16+
},
17+
};
18+
}
419

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

24+
const getRuntimeState = () => store.reduxStore.getState().runtime;
25+
926
beforeEach(() => {
1027
store = new CompassShellStore();
1128
appRegistry = new EventEmitter();
1229
store.onActivated(appRegistry);
1330
});
1431

32+
afterEach(async() => {
33+
const { runtime } = getRuntimeState();
34+
35+
if (runtime && runtime.terminate) {
36+
await runtime.terminate();
37+
}
38+
});
39+
1540
describe('appRegistry', () => {
1641
it('sets the global appRegistry', () => {
1742
expect(store.reduxStore.getState().appRegistry).to.not.equal(null);
@@ -20,8 +45,6 @@ describe('CompassShellStore [Store]', () => {
2045
});
2146

2247
describe('runtime', () => {
23-
const getRuntimeState = () => store.reduxStore.getState().runtime;
24-
2548
it('has initialized runtime state', () => {
2649
const runtimeState = getRuntimeState();
2750

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

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

3558
const runtimeState = getRuntimeState();
3659

3760
expect(runtimeState.error).to.equal(null);
38-
expect(runtimeState.runtime).to.be.instanceOf(ElectronRuntime);
61+
expect(runtimeState.runtime).to.be.instanceOf(WorkerRuntime);
3962
});
4063

41-
it('emits mongosh events to the appRegistry', () => {
42-
appRegistry.emit('data-service-connected', null, {client: {client: {
43-
db: () => ({
44-
admin: () => ({
45-
listDatabases: () => Promise.resolve({
46-
databases: [
47-
{ name: 'db1', sizeOnDisk: 10000, empty: false }
48-
],
49-
totalSize: 50000,
50-
ok: 1
51-
})
52-
})
53-
})
54-
}}});
64+
it('emits mongosh events to the appRegistry', async() => {
65+
appRegistry.emit('data-service-connected', null, createMockDataService());
5566
let eventRecieved = false;
56-
appRegistry.on('mongosh:show', () => {
67+
appRegistry.on('mongosh:setCtx', () => {
5768
eventRecieved = true;
5869
});
5970

6071
const runtimeState = getRuntimeState();
6172

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

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

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

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

0 commit comments

Comments
 (0)