Skip to content

Commit

Permalink
lib: add late code injector
Browse files Browse the repository at this point in the history
  • Loading branch information
RaisinTen committed Feb 24, 2021
1 parent 2d52dc3 commit 450e173
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 450e173

Please sign in to comment.