diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 23b328b14487e4..0afe38e618ac2d 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -49,6 +49,8 @@ function prepareMainThreadExecution(expandArgv1 = false) { initializeHeapSnapshotSignalHandlers(); + setupLateCodeInjector(); + // If the process is spawned with env NODE_CHANNEL_FD, it's probably // spawned by our child_process module, then initialize IPC. // This attaches some internal event listeners and creates: @@ -456,6 +458,30 @@ function loadPreloadModules() { } } +function setupLateCodeInjector() { + let resolvedAgent; + try { + resolvedAgent = require.resolve(process.env.Agent); + } catch(err) { + } + + const signal = process.env.Signal; + + function handler() { + const { + execute, + } = require(resolvedAgent); + + execute(); + + delete require.cache[resolvedAgent]; + } + + if (signal && resolvedAgent) { + process.on(signal, handler); + } +} + module.exports = { patchProcessObject, setupCoverageHooks,