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
6 changes: 6 additions & 0 deletions packages/compass-shell/config/webpack.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,11 @@ module.exports = {
exclude: /(node_modules)/
}
]
},
externals: {
// Runtime implementation depends on worker file existing near the library
// 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',
}
};
16 changes: 16 additions & 0 deletions packages/compass-shell/config/webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { spawn } = require('child_process');
const baseWebpackConfig = require('./webpack.base.config');
const project = require('./project');

/** @type import('webpack').Configuration */
const config = {
mode: 'development',
target: 'electron-renderer',
Expand Down Expand Up @@ -76,6 +77,21 @@ const config = {
.on('close', () => process.exit(0))
.on('error', spawnError => console.error(spawnError)); // eslint-disable-line no-console
}
},
resolve: {
// Without this alias, in dev mode symlinked browser-repl breaks the code
// by having two reacts loadeded on the page
alias: {
'react': require.resolve('react'),
'react-dom': require.resolve('@hot-loader/react-dom'),
}
},
externals: {
// "Optional" mongodb dependencies that should stay out of the build in dev
// mode
'mongodb-client-encryption': 'commonjs2 mongodb-client-encryption',
kerberos: 'commonjs2 kerberos',
snappy: 'commonjs2 snappy'
}
};

Expand Down
119 changes: 0 additions & 119 deletions packages/node-runtime-worker-thread/loaders/inline-entry-loader.js

This file was deleted.

16 changes: 10 additions & 6 deletions packages/node-runtime-worker-thread/src/child-process-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
*
* @see {@link https://github.com/nodejs/node/pull/36344}
*/

import { once } from 'events';
import { SHARE_ENV, Worker } from 'worker_threads';
import fs from 'fs';
import path from 'path';
import { exposeAll, createCaller } from './rpc';

/**
* The source has to be inlined to allow the runtime to be bundled in a single
* file when used by compass shell
*/
import workerRuntimeSrc from 'inline-entry-loader!./worker-runtime';
// eslint-disable-next-line no-sync
const workerRuntimeSrc = fs.readFileSync(
path.resolve(
process.env.NODE_RUNTIME_WORKER_THREAD_PARENT_DIRNAME || __dirname,
'worker-runtime.js'
),
'utf-8'
);

const workerProcess = new Worker(workerRuntimeSrc, {
eval: true,
Expand Down
20 changes: 16 additions & 4 deletions packages/node-runtime-worker-thread/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import { ChildProcess, SpawnOptionsWithoutStdio } from 'child_process';
import { MongoClientOptions } from '@mongosh/service-provider-core';
import { Runtime, RuntimeEvaluationListener, RuntimeEvaluationResult } from '@mongosh/browser-runtime-core';
import { promises as fs } from 'fs';
import path from 'path';
import spawnChildFromSource, { kill } from './spawn-child-from-source';
import { Caller, createCaller } from './rpc';
import { ChildProcessEvaluationListener } from './child-process-evaluation-listener';
import type { WorkerRuntime as WorkerThreadWorkerRuntime } from './worker-runtime';
import childProcessProxySrc from 'inline-entry-loader!./child-process-proxy';
import { deserializeEvaluationResult } from './serializer';

type ChildProcessRuntime = Caller<WorkerThreadWorkerRuntime>;
Expand Down Expand Up @@ -43,11 +44,22 @@ class WorkerRuntime implements Runtime {
private async initWorker() {
const { uri, driverOptions, cliOptions, spawnOptions } = this.initOptions;

this.childProcess = await spawnChildFromSource(
childProcessProxySrc,
spawnOptions
const childProcessProxySrc = await fs.readFile(
path.resolve(__dirname, 'child-process-proxy.js'),
'utf-8'
);

this.childProcess = await spawnChildFromSource(childProcessProxySrc, {
...spawnOptions,
env: {
// Proxy child process and worker_threads worker are inlined and as such
// they are not aware of the dirname (which child process will need to
// read worker source)
NODE_RUNTIME_WORKER_THREAD_PARENT_DIRNAME: __dirname,
...spawnOptions.env
}
});

this.childProcessRuntime = createCaller(
['init', 'evaluate', 'getCompletions', 'setEvaluationListener'],
this.childProcess
Expand Down
14 changes: 8 additions & 6 deletions packages/node-runtime-worker-thread/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const TerserPlugin = require('terser-webpack-plugin');
const config = {
target: 'node',

devtool: false,

output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
Expand All @@ -27,10 +25,6 @@ const config = {
extensions: ['.ts', '.js']
},

resolveLoader: {
modules: ['node_modules', path.resolve(__dirname, 'loaders')]
},

optimization: {
minimizer: [
new TerserPlugin({
Expand All @@ -40,6 +34,14 @@ const config = {
}
})
]
},

node: false,

externals: {
'mongodb-client-encryption': 'commonjs2 mongodb-client-encryption',
kerberos: 'commonjs2 kerberos',
snappy: 'commonjs2 snappy'
}
};

Expand Down