diff --git a/packages/compass-shell/config/webpack.base.config.js b/packages/compass-shell/config/webpack.base.config.js index fb6484f8c5..62152c7fe0 100644 --- a/packages/compass-shell/config/webpack.base.config.js +++ b/packages/compass-shell/config/webpack.base.config.js @@ -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 }; diff --git a/packages/compass-shell/src/modules/runtime.js b/packages/compass-shell/src/modules/runtime.js index 8fb1244cb2..63df9652b7 100644 --- a/packages/compass-shell/src/modules/runtime.js +++ b/packages/compass-shell/src/modules/runtime.js @@ -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'; /** diff --git a/packages/compass-shell/src/modules/worker-runtime.js b/packages/compass-shell/src/modules/worker-runtime.js new file mode 100644 index 0000000000..bae1bc06b2 --- /dev/null +++ b/packages/compass-shell/src/modules/worker-runtime.js @@ -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 }; diff --git a/packages/node-runtime-worker-thread/src/child-process-proxy.ts b/packages/node-runtime-worker-thread/src/child-process-proxy.ts index 642c1dd56e..e81d7af987 100644 --- a/packages/node-runtime-worker-thread/src/child-process-proxy.ts +++ b/packages/node-runtime-worker-thread/src/child-process-proxy.ts @@ -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