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
3 changes: 2 additions & 1 deletion packages/compass-shell/config/webpack.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,6 @@ module.exports = {
// main import and for that reason it needs to stay external to the
// compass-shell
'@mongosh/node-runtime-worker-thread': 'commonjs2 @mongosh/node-runtime-worker-thread',
}
},
node: false
};
2 changes: 1 addition & 1 deletion packages/compass-shell/src/modules/runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { CompassServiceProvider } from '@mongosh/service-provider-server';
import { WorkerRuntime } from '@mongosh/node-runtime-worker-thread';
import { WorkerRuntime } from './worker-runtime';
import { adaptDriverV36ConnectionParams } from './adapt-driver-v36-connection-params';

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/compass-shell/src/modules/worker-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createRequire } from 'module';

const { WorkerRuntime } = (() => {
// Workaround for webpack require that overrides global require
const req = createRequire(__filename);
const realModulePath = req.resolve('@mongosh/node-runtime-worker-thread');
// Runtime needs to be outside the asar bundle to function properly, so if we
// resolved it inside of one, we will try to import it from outside (and hard
// fail if this didn't work)
if (/\.asar(?!\.unpacked)/.test(realModulePath)) {
try {
return req(realModulePath.replace('.asar', '.asar.unpacked'));
} catch (e) {
e.message +=
'\n\n@mongosh/node-runtime-worker-thread module and all its dependencies needs to be unpacked before it can be used';
throw e;
}
}

return req(realModulePath);
})();

export { WorkerRuntime };
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,13 @@
*/
import { once } from 'events';
import { SHARE_ENV, Worker } from 'worker_threads';
import fs from 'fs';
import path from 'path';
import { exposeAll, createCaller } from './rpc';
import { InterruptHandle, interrupt as nativeInterrupt } from 'interruptor';

const workerRuntimeSrcPath = path.resolve(__dirname, 'worker-runtime.js');

const workerProcess = new Worker(
// It's fine in this use-case: this process is spawned so we are not blocking
// anything in the main process
// eslint-disable-next-line no-sync
fs.readFileSync(workerRuntimeSrcPath, 'utf8'),
{ env: SHARE_ENV, eval: true }
);
const workerProcess = new Worker(workerRuntimeSrcPath, { env: SHARE_ENV });

// We expect the amount of listeners to be more than the default value of 10 but
// probably not more than ~25 (all exposed methods on
Expand Down