From 4e92bc2bce099812e1ce17f92ab240fdecc9c474 Mon Sep 17 00:00:00 2001 From: Dasa Paddock Date: Tue, 16 May 2023 07:29:25 -0700 Subject: [PATCH] fix(terser): don't assume code is running in worker created by the worker pool (#1483) * fix(terser): don't assume code is running in worker created by the worker pool * sort imports --- packages/terser/src/constants.ts | 1 + packages/terser/src/worker-pool.ts | 6 ++++-- packages/terser/src/worker.ts | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/terser/src/constants.ts b/packages/terser/src/constants.ts index 36a95c890..205d46dc0 100644 --- a/packages/terser/src/constants.ts +++ b/packages/terser/src/constants.ts @@ -1,2 +1,3 @@ export const taskInfo = Symbol('taskInfo'); export const freeWorker = Symbol('freeWorker'); +export const workerPoolWorkerFlag = 'WorkerPoolWorker'; diff --git a/packages/terser/src/worker-pool.ts b/packages/terser/src/worker-pool.ts index 5630fd963..92d6902c4 100644 --- a/packages/terser/src/worker-pool.ts +++ b/packages/terser/src/worker-pool.ts @@ -5,7 +5,7 @@ import { EventEmitter } from 'events'; import serializeJavascript from 'serialize-javascript'; -import { freeWorker, taskInfo } from './constants'; +import { freeWorker, taskInfo, workerPoolWorkerFlag } from './constants'; import type { WorkerCallback, @@ -81,7 +81,9 @@ export class WorkerPool extends EventEmitter { } private addNewWorker() { - const worker: WorkerWithTaskInfo = new Worker(this.filePath); + const worker: WorkerWithTaskInfo = new Worker(this.filePath, { + workerData: workerPoolWorkerFlag + }); worker.on('message', (result) => { worker[taskInfo]?.done(null, result); diff --git a/packages/terser/src/worker.ts b/packages/terser/src/worker.ts index d2c528c1e..ea904b9fd 100644 --- a/packages/terser/src/worker.ts +++ b/packages/terser/src/worker.ts @@ -1,9 +1,11 @@ -import { isMainThread, parentPort } from 'worker_threads'; +import { isMainThread, parentPort, workerData } from 'worker_threads'; import { hasOwnProperty, isObject } from 'smob'; import { minify } from 'terser'; +import { workerPoolWorkerFlag } from './constants'; + import type { WorkerContextSerialized, WorkerOutput } from './type'; /** @@ -22,7 +24,7 @@ function isWorkerContextSerialized(input: unknown): input is WorkerContextSerial } export function runWorker() { - if (isMainThread || !parentPort) { + if (isMainThread || !parentPort || workerData !== workerPoolWorkerFlag) { return; }