()
| 891 | } |
| 892 | |
| 893 | function spawnWorker(): Promise<WorkerInfo> { |
| 894 | const workerId = nextWorkerId++ |
| 895 | spawnInProgress++ |
| 896 | let spawnSettled = false |
| 897 | let childProcess: ChildProcess | null = null |
| 898 | |
| 899 | const settleSpawnInProgress = () => { |
| 900 | if (spawnSettled) { |
| 901 | return false |
| 902 | } |
| 903 | spawnSettled = true |
| 904 | spawnInProgress-- |
| 905 | return true |
| 906 | } |
| 907 | |
| 908 | const workerInfo: WorkerInfo = { |
| 909 | get process() { |
| 910 | if (!childProcess) { |
| 911 | throw new Error('Worker process is not initialized') |
| 912 | } |
| 913 | return childProcess |
| 914 | }, |
| 915 | ready: false, |
| 916 | readyPromise: null, |
| 917 | activeExecutions: 0, |
| 918 | pendingExecutions: new Map(), |
| 919 | idleTimeout: null, |
| 920 | id: workerId, |
| 921 | lifetimeExecutions: 0, |
| 922 | retiring: false, |
| 923 | } |
| 924 | |
| 925 | workerInfo.readyPromise = new Promise<void>((resolve, reject) => { |
| 926 | if (!checkNodeAvailable()) { |
| 927 | settleSpawnInProgress() |
| 928 | reject( |
| 929 | new Error( |
| 930 | 'Node.js is required for code execution but was not found. ' + |
| 931 | 'Please install Node.js (v20+) from https://nodejs.org' |
| 932 | ) |
| 933 | ) |
| 934 | return |
| 935 | } |
| 936 | |
| 937 | const currentDir = path.dirname(fileURLToPath(import.meta.url)) |
| 938 | const candidatePaths = [ |
| 939 | path.join(currentDir, 'isolated-vm-worker.cjs'), |
| 940 | path.join(currentDir, '..', '..', 'lib', 'execution', 'isolated-vm-worker.cjs'), |
| 941 | path.join(process.cwd(), 'apps', 'sim', 'lib', 'execution', 'isolated-vm-worker.cjs'), |
| 942 | path.join(process.cwd(), 'lib', 'execution', 'isolated-vm-worker.cjs'), |
| 943 | ] |
| 944 | const workerPath = candidatePaths.find((p) => fs.existsSync(p)) |
| 945 | |
| 946 | if (!workerPath) { |
| 947 | settleSpawnInProgress() |
| 948 | reject(new Error(`Worker file not found at any of: ${candidatePaths.join(', ')}`)) |
| 949 | return |
| 950 | } |
no test coverage detected