node/test/common/wpt/worker.js
Michaël Zasso 4b22d335d1
test: move execution of WPT to worker threads
Running outside of the main Node.js context prevents us from upgrading
the WPT harness because new versions more aggressively check the
identity of globals like error constructors. Instead of exposing
globals used by the tests on vm sandboxes, use worker threads to run
everything.

PR-URL: https://github.com/nodejs/node/pull/34796
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-09-22 19:55:46 +02:00

55 lines
1.3 KiB
JavaScript

/* eslint-disable node-core/required-modules,node-core/require-common-first */
'use strict';
const { runInThisContext } = require('vm');
const { parentPort, workerData } = require('worker_threads');
const { ResourceLoader } = require(workerData.wptRunner);
const resource = new ResourceLoader(workerData.wptPath);
global.self = global;
global.GLOBAL = {
isWindow() { return false; }
};
global.require = require;
// This is a mock, because at the moment fetch is not implemented
// in Node.js, but some tests and harness depend on this to pull
// resources.
global.fetch = function fetch(file) {
return resource.read(workerData.filename, file, true);
};
if (workerData.initScript) {
runInThisContext(workerData.initScript);
}
runInThisContext(workerData.harness.code, {
filename: workerData.harness.filename
});
// eslint-disable-next-line no-undef
add_result_callback((result) => {
parentPort.postMessage({
type: 'result',
result: {
status: result.status,
name: result.name,
message: result.message,
stack: result.stack,
},
});
});
// eslint-disable-next-line no-undef
add_completion_callback((_, status) => {
parentPort.postMessage({
type: 'completion',
status,
});
});
for (const scriptToRun of workerData.scriptsToRun) {
runInThisContext(scriptToRun.code, { filename: scriptToRun.filename });
}