(workerId: number, message: unknown)
| 728 | } |
| 729 | |
| 730 | function handleWorkerMessage(workerId: number, message: unknown) { |
| 731 | if (typeof message !== 'object' || message === null) return |
| 732 | const msg = message as Record<string, unknown> |
| 733 | const workerInfo = workers.get(workerId) |
| 734 | |
| 735 | if (msg.type === 'result') { |
| 736 | const execId = msg.executionId as number |
| 737 | const pending = workerInfo?.pendingExecutions.get(execId) |
| 738 | if (pending) { |
| 739 | clearTimeout(pending.timeout) |
| 740 | workerInfo!.pendingExecutions.delete(execId) |
| 741 | workerInfo!.activeExecutions-- |
| 742 | totalActiveExecutions-- |
| 743 | const owner = ownerStates.get(pending.ownerKey) |
| 744 | if (owner) { |
| 745 | owner.activeExecutions = Math.max(0, owner.activeExecutions - 1) |
| 746 | maybeCleanupOwner(owner.ownerKey) |
| 747 | } |
| 748 | workerInfo!.lifetimeExecutions++ |
| 749 | if (workerInfo!.lifetimeExecutions >= MAX_EXECUTIONS_PER_WORKER && !workerInfo!.retiring) { |
| 750 | workerInfo!.retiring = true |
| 751 | logger.info('Worker marked for retirement', { |
| 752 | workerId, |
| 753 | lifetimeExecutions: workerInfo!.lifetimeExecutions, |
| 754 | }) |
| 755 | } |
| 756 | if (workerInfo!.retiring && workerInfo!.activeExecutions === 0) { |
| 757 | cleanupWorker(workerId) |
| 758 | } else { |
| 759 | resetWorkerIdleTimeout(workerId) |
| 760 | } |
| 761 | // If the caller aborted, the outer Promise is already resolved with |
| 762 | // AbortError. Still run all the bookkeeping above so pool counters stay |
| 763 | // accurate; just skip re-resolving. |
| 764 | if (!pending.cancelled) { |
| 765 | pending.resolve(msg.result as IsolatedVMExecutionResult) |
| 766 | } |
| 767 | drainQueue() |
| 768 | } |
| 769 | return |
| 770 | } |
| 771 | |
| 772 | if (msg.type === 'broker') { |
| 773 | handleBrokerMessage(workerInfo, msg) |
| 774 | return |
| 775 | } |
| 776 | |
| 777 | if (msg.type === 'fetch') { |
| 778 | const { fetchId, requestId, url, optionsJson } = msg as { |
| 779 | fetchId: number |
| 780 | requestId: string |
| 781 | url: string |
| 782 | optionsJson?: string |
| 783 | } |
| 784 | if (typeof url !== 'string' || url.length === 0) { |
| 785 | workerInfo?.process.send({ |
| 786 | type: 'fetchResponse', |
| 787 | fetchId, |
no test coverage detected