forked from TypeStrong/ts-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consistently perform bootstrap and encode Brotli config for improved …
…caching/reduced complexity Additionally, this PR streamlines the boostrap mechanism to always call into the child script, resulting in reduced complexity, and also improved caching for user-initiated forked processes. i.e. the tsconfig resolution is not repeated multiple-times because forked processes are expected to preserve the existing ts-node project. More details can be found here TypeStrong#1831. Fixes TypeStrong#1812.
- Loading branch information
1 parent
0e0da59
commit a7e6486
Showing
36 changed files
with
467 additions
and
217 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
import { BootstrapState, bootstrap } from '../bin'; | ||
import { completeBootstrap, BootstrapStateForChild } from '../bin'; | ||
import { argPrefix, compress, decompress } from './argv-payload'; | ||
|
||
const base64ConfigArg = process.argv[2]; | ||
if (!base64ConfigArg.startsWith(argPrefix)) throw new Error('unexpected argv'); | ||
const base64Payload = base64ConfigArg.slice(argPrefix.length); | ||
const state = decompress(base64Payload) as BootstrapState; | ||
const state = decompress(base64Payload) as BootstrapStateForChild; | ||
|
||
state.isInChildProcess = true; | ||
state.tsNodeScript = __filename; | ||
state.parseArgvResult.argv = process.argv; | ||
state.parseArgvResult.restArgs = process.argv.slice(3); | ||
|
||
// Modify and re-compress the payload delivered to subsequent child processes. | ||
// This logic may be refactored into bin.ts by https://github.com/TypeStrong/ts-node/issues/1831 | ||
if (state.isCli) { | ||
const stateForChildren: BootstrapState = { | ||
...state, | ||
isCli: false, | ||
}; | ||
state.parseArgvResult.argv[2] = `${argPrefix}${compress(stateForChildren)}`; | ||
} | ||
|
||
bootstrap(state); | ||
completeBootstrap(state); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { pathToFileURL } from 'url'; | ||
import { brotliCompressSync } from 'zlib'; | ||
import type { BootstrapStateForChild } from '../bin'; | ||
import { versionGteLt } from '../util'; | ||
import { argPrefix } from './argv-payload'; | ||
|
||
export function getChildProcessArguments( | ||
enableEsmLoader: boolean, | ||
state: BootstrapStateForChild | ||
) { | ||
if (enableEsmLoader && !versionGteLt(process.versions.node, '12.17.0')) { | ||
throw new Error( | ||
'`ts-node-esm` and `ts-node --esm` require node version 12.17.0 or newer.' | ||
); | ||
} | ||
|
||
const nodeExecArgs = []; | ||
|
||
if (enableEsmLoader) { | ||
nodeExecArgs.push( | ||
'--require', | ||
require.resolve('./child-require.js'), | ||
'--loader', | ||
// Node on Windows doesn't like `c:\` absolute paths here; must be `file:///c:/` | ||
pathToFileURL(require.resolve('../../child-loader.mjs')).toString() | ||
); | ||
} | ||
|
||
const childScriptArgs = [ | ||
`${argPrefix}${brotliCompressSync( | ||
Buffer.from(JSON.stringify(state), 'utf8') | ||
).toString('base64')}`, | ||
]; | ||
|
||
return { | ||
nodeExecArgs, | ||
childScriptArgs, | ||
childScriptPath: require.resolve('./child-entrypoint.js'), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { fork } from 'child_process'; | ||
import type { BootstrapStateForChild } from '../bin'; | ||
import { getChildProcessArguments } from './child-exec-args'; | ||
|
||
/** | ||
* @internal | ||
* @param state Bootstrap state to be transferred into the child process. | ||
* @param enableEsmLoader Whether to enable the ESM loader or not. This option may | ||
* be removed in the future when `--esm` is no longer a choice. | ||
* @param targetCwd Working directory to be preserved when transitioning to | ||
* the child process. | ||
*/ | ||
export function callInChildWithEsm( | ||
state: BootstrapStateForChild, | ||
targetCwd: string | ||
) { | ||
const { childScriptArgs, childScriptPath, nodeExecArgs } = | ||
getChildProcessArguments(/* enableEsmLoader */ true, state); | ||
|
||
childScriptArgs.push(...state.parseArgvResult.restArgs); | ||
|
||
const child = fork(childScriptPath, childScriptArgs, { | ||
stdio: 'inherit', | ||
execArgv: [...process.execArgv, ...nodeExecArgs], | ||
cwd: targetCwd, | ||
}); | ||
child.on('error', (error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
child.on('exit', (code) => { | ||
child.removeAllListeners(); | ||
process.off('SIGINT', sendSignalToChild); | ||
process.off('SIGTERM', sendSignalToChild); | ||
process.exitCode = code === null ? 1 : code; | ||
}); | ||
// Ignore sigint and sigterm in parent; pass them to child | ||
process.on('SIGINT', sendSignalToChild); | ||
process.on('SIGTERM', sendSignalToChild); | ||
function sendSignalToChild(signal: string) { | ||
process.kill(child.pid, signal); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.