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
97f9afd
commit ee025e8
Showing
36 changed files
with
464 additions
and
212 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 { argPrefix, compress, decompress } from './argv-payload'; | ||
import { completeBootstrap, BootstrapStateForChild } from '../bin'; | ||
import { argPrefix, 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,33 @@ | ||
import { pathToFileURL } from 'url'; | ||
import { brotliCompressSync } from 'zlib'; | ||
import type { BootstrapStateForChild } from '../bin'; | ||
import { argPrefix } from './argv-payload'; | ||
|
||
export function getChildProcessArguments( | ||
enableEsmLoader: boolean, | ||
state: BootstrapStateForChild | ||
) { | ||
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
8 changes: 8 additions & 0 deletions
8
tests/esm-child-process/process-forking-esm-worker-next/tsconfig.json
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext" | ||
}, | ||
"ts-node": { | ||
"transpileOnly": true | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.