Skip to content

feat(node-runtime-worker-thread): Add new runtime that works in the worker thread COMPASS-4555 #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3c3241d
chore(node-runtime-worker-thread): Initial commit with library setup
gribnoysup Jan 4, 2021
4c93c00
feat(node-runtime-worker-thread): Add spawn from source helper
gribnoysup Jan 4, 2021
9097a11
feat(node-runtime-worker-thread): Add rpc helper
gribnoysup Jan 4, 2021
c0a0add
refactor(node-runtime-worker-thread): Better types for rpc helper
gribnoysup Jan 5, 2021
ba56986
test(node-runtime-worker-thread): Use less strict ts config when runn…
gribnoysup Jan 6, 2021
4dc44b9
feat(node-runtime-worker-thread): Add worker thread runtime
gribnoysup Jan 6, 2021
7a7aa7c
fix(node-runtime-worker-thread): process.send can be undefined
gribnoysup Jan 6, 2021
3a53326
test(node-runtime-worker-thread): Add tests for evaluation listener
gribnoysup Jan 6, 2021
5554462
feat(node-runtime-worker-thread): Add child process proxy
gribnoysup Jan 6, 2021
e672349
chore(node-runtime-worker-thread): Move build process to webpack, a p…
gribnoysup Jan 6, 2021
8983c92
chore(node-runtime-worker-thread): Inline worker source instead of re…
gribnoysup Jan 6, 2021
189c652
fix(node-runtime-worker-thread): Prevent class names minimization to …
gribnoysup Jan 7, 2021
6734130
refactor(node-runtime-worker-thread): Better types for rpc helper and…
gribnoysup Jan 7, 2021
946fe03
test(node-runtime-worker-thread): Better assertions for rejected prom…
gribnoysup Jan 7, 2021
c239c99
feat(node-runtime-worker-thread): Implement worker runtime public int…
gribnoysup Jan 7, 2021
4ffcdb1
chore(node-runtime-worker-thread): Reword child process explanation
gribnoysup Jan 7, 2021
0b587bd
fix(node-runtime-worker-thread): Fix build failing on older node vers…
gribnoysup Jan 7, 2021
eeb542c
fix(node-runtime-worker-thread): Explicitly provide output path in th…
gribnoysup Jan 8, 2021
b2da534
refactor(node-runtime-worker-thread): Use nullish coalescing instead …
gribnoysup Jan 8, 2021
da3b686
fix(node-runtime-worker-thread): Actually wait for the worker child p…
gribnoysup Jan 8, 2021
ad2c5bb
Merge remote-tracking branch 'origin/master' into compass-4513-shell-…
gribnoysup Jan 8, 2021
98f3036
fix(node-runtime-worker-thread): Fix TS errors
gribnoysup Jan 8, 2021
e7da859
test(node-runtime-worker-thread): Use cross-env for windows compat in…
gribnoysup Jan 8, 2021
6aad0ef
fix(node-runtime-worker-thread): Do not 'allowJs' when running tests …
gribnoysup Jan 8, 2021
01c9104
chore: Ignore dist (webpack default output) when checking code coverage
gribnoysup Jan 12, 2021
d5aa34c
fix(node-runtime-worker-thread): Better clean-up for the child proces…
gribnoysup Jan 12, 2021
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 .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"**/test/**/*.{ts,js,tsx,jsx,cjs,mjs}",
"**/tmp/**/*.{ts,js,tsx,jsx,cjs,mjs}",
"**/*.config.{ts,js,tsx,jsx,cjs,mjs}",
"**/lib/*.js"
"**/lib/*.js",
"**/dist/**/*.js"
]
}
2 changes: 2 additions & 0 deletions packages/node-runtime-worker-thread/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib/
1 change: 1 addition & 0 deletions packages/node-runtime-worker-thread/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../config/eslintrc.base');
3 changes: 3 additions & 0 deletions packages/node-runtime-worker-thread/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib/
dist/
src/**/*.js
1 change: 1 addition & 0 deletions packages/node-runtime-worker-thread/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `@mongosh/node-runtime-worker-thread`
119 changes: 119 additions & 0 deletions packages/node-runtime-worker-thread/loaders/inline-entry-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const path = require('path');
const { getOptions } = require('loader-utils');
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');

function inlineEntryLoader() {
/**
* Main loader lifecycle does nothing, everything happens in `pitch` step
* @see {@link https://webpack.js.org/api/loaders/#pitching-loader}
*/
}

/**
* Pitch step will split an import into its own entry, compile it into a single
* bundle and returns the stringified source
*
* @param {string} request
*/
inlineEntryLoader.pitch = function pitch(request) {
const loaderName = `inline-entry-loader ${request}`;

/** @type {import('webpack').loader.LoaderContext} */
const that = this;
/** @type {import('webpack').compilation.Compilation} */
const parentCompilation = that._compilation;

that.cacheable(false);

const { esModule = true, emit = false, ...outputOptions } = getOptions(that);

const cb = that.async();

const inlineEntryOutputOptions = {
...outputOptions,
filename: '[contenthash].inline-entry-loader.js'
};

/**
* @type {import('webpack').Compiler}
* @see {@link https://webpack.js.org/api/compilation-object/#createchildcompiler}
*/
const inlineEntryCompiler = parentCompilation.createChildCompiler(
loaderName,
inlineEntryOutputOptions
);

inlineEntryCompiler.options.optimization = {
...inlineEntryCompiler.options.optimization,
...parentCompilation.compiler.options.optimization
};

if (
inlineEntryCompiler.options &&
inlineEntryCompiler.options.optimization &&
inlineEntryCompiler.options.optimization.splitChunks
) {
inlineEntryCompiler.options.optimization.splitChunks = false;
}

if (this.target !== 'webworker' && this.target !== 'web') {
new NodeTargetPlugin().apply(inlineEntryCompiler);
}

const resourceName = path.parse(that.resourcePath).name;

new SingleEntryPlugin(that.context, `!!${request}`, resourceName).apply(
inlineEntryCompiler
);

let outputFilename;

parentCompilation.compiler.hooks.emit.tapAsync(
loaderName,
(compilation, callback) => {
if (!emit) {
delete compilation.assets[outputFilename];
}
callback();
}
);

/**
*
* @param {Error} err
* @param {import('webpack').compilation.Chunk} entries
* @param {import('webpack').compilation.Compilation} compilation
*/
const handleChildCompilation = (err, entries, compilation) => {
if (err) {
return cb(err);
}

/** @type {import('webpack').compilation.Chunk} */
const chunk = entries[0];

if (chunk) {
outputFilename = chunk.files[0];

/** @type {import('webpack-sources').RawSource} */
const src = compilation.assets[outputFilename];
const contents = JSON.stringify(src.source());

return cb(
null,
`${esModule ? 'export default' : 'module.exports ='} ${contents}`
);
}

return cb(
new Error(
'Something went wrong when trying to inline chunk: no entries returned by child compilation'
)
);
};

inlineEntryCompiler.runAsChild(handleChildCompilation);
};

module.exports = inlineEntryLoader;
Loading