Skip to content

Commit

Permalink
fix: prevent preflight from running in loader
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 25, 2023
1 parent 0c9e7a4 commit 79b6f16
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/preflight.cts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { isMainThread } from 'node:worker_threads';
import { constants as osConstants } from 'os';
import './suppress-warnings.cts';

type BaseEventListener = () => void;

/**
* Hook require() to transform to CJS
*
* This needs to be loaded via --require flag so subsequent --require
* flags can support TypeScript.
*
* This is also added in loader.ts for the loader API.
* Although it is required twice, it's not executed twice because
* it's cached.
*/
// eslint-disable-next-line import/no-unresolved
require('./cjs/index.cjs');

const bindHiddenSignalsHandler = (
signals: NodeJS.Signals[],
handler: NodeJS.SignalsListener,
Expand Down Expand Up @@ -47,20 +35,39 @@ const bindHiddenSignalsHandler = (
};
};

// If a parent process is detected
if (process.send) {
bindHiddenSignalsHandler(['SIGINT', 'SIGTERM'], (signal: NodeJS.Signals) => {
process.send!({
type: 'kill',
signal,
});
/**
* Seems module.register() calls the loader with the same Node arguments
* which causes this preflight to be loaded in the loader thread
*/
if (isMainThread) {
/**
* Hook require() to transform to CJS
*
* This needs to be loaded via --require flag so subsequent --require
* flags can support TypeScript.
*
* This is also added in loader.ts for the loader API.
* Although it is required twice, it's not executed twice because
* it's cached.
*/
// eslint-disable-next-line import/no-unresolved
require('./cjs/index.cjs');

/**
* Since we're setting a custom signal handler, we need to emulate the
* default behavior when there are no other handlers set
*/
if (process.listenerCount(signal) === 0) {
process.exit(128 + osConstants.signals[signal]);
}
});
// If a parent process is detected
if (process.send) {
bindHiddenSignalsHandler(['SIGINT', 'SIGTERM'], (signal: NodeJS.Signals) => {
process.send!({
type: 'kill',
signal,
});

/**
* Since we're setting a custom signal handler, we need to emulate the
* default behavior when there are no other handlers set
*/
if (process.listenerCount(signal) === 0) {
process.exit(128 + osConstants.signals[signal]);
}
});
}
}

0 comments on commit 79b6f16

Please sign in to comment.