Skip to content
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

feat: support ESM engines #3424

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
59 changes: 30 additions & 29 deletions packages/core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,41 @@ module.exports = {
}
};

function loadEngines(
async function loadEngines(
script,
ee,
warnings = {
engines: {}
}
) {
const loadedEngines = _.map(
Object.assign({}, Engines, script.config.engines),
function loadEngine(engineConfig, engineName) {
let moduleName = 'artillery-engine-' + engineName;
try {
let Engine;
if (typeof Engines[engineName] !== 'undefined') {
Engine = Engines[engineName];
} else {
Engine = require(moduleName);
const loadedEngines = await Promise.all(
[...Object.entries(Engines), ...Object.entries(script.config.engines)].map(
async ([engineName, engineConfig]) => {
let moduleName = 'artillery-engine-' + engineName;
try {
let Engine;
if (typeof Engines[engineName] !== 'undefined') {
Engine = Engines[engineName];
} else {
Engine = (await import(moduleName)).default;
}
const engine = new Engine(script, ee, engineUtil);
engine.__name = engineName;
return engine;
} catch (err) {
console.log(
'WARNING: engine %s specified but module %s could not be loaded',
engineName,
moduleName
);
console.log(err.stack);
warnings.engines[engineName] = {
message: 'Could not load',
error: err
};
}
const engine = new Engine(script, ee, engineUtil);
engine.__name = engineName;
return engine;
} catch (err) {
console.log(
'WARNING: engine %s specified but module %s could not be loaded',
engineName,
moduleName
);
console.log(err.stack);
warnings.engines[engineName] = {
message: 'Could not load',
error: err
};
}
}
)
);

return { loadedEngines, warnings };
Expand Down Expand Up @@ -159,7 +160,7 @@ async function runner(script, payload, options, callback) {
//
// load engines:
//
const { loadedEngines: runnerEngines } = loadEngines(
const { loadedEngines: runnerEngines } = await loadEngines(
runnableScript,
ee,
warnings
Expand Down Expand Up @@ -508,12 +509,12 @@ function $randomString(length = 10) {
return s;
}

function handleScriptHook(hook, script, hookEvents, contextVars = {}) {
async function handleScriptHook(hook, script, hookEvents, contextVars = {}) {
if (!script[hook]) {
return {};
}

const { loadedEngines: engines } = loadEngines(script, hookEvents);
const { loadedEngines: engines } = await loadEngines(script, hookEvents);
const ee = new EventEmitter();

return new Promise(function (resolve, reject) {
Expand Down
Loading