Skip to content

Commit 4e92bc2

Browse files
authored
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
1 parent b3a65b9 commit 4e92bc2

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

packages/terser/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const taskInfo = Symbol('taskInfo');
22
export const freeWorker = Symbol('freeWorker');
3+
export const workerPoolWorkerFlag = 'WorkerPoolWorker';

packages/terser/src/worker-pool.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EventEmitter } from 'events';
55

66
import serializeJavascript from 'serialize-javascript';
77

8-
import { freeWorker, taskInfo } from './constants';
8+
import { freeWorker, taskInfo, workerPoolWorkerFlag } from './constants';
99

1010
import type {
1111
WorkerCallback,
@@ -81,7 +81,9 @@ export class WorkerPool extends EventEmitter {
8181
}
8282

8383
private addNewWorker() {
84-
const worker: WorkerWithTaskInfo = new Worker(this.filePath);
84+
const worker: WorkerWithTaskInfo = new Worker(this.filePath, {
85+
workerData: workerPoolWorkerFlag
86+
});
8587

8688
worker.on('message', (result) => {
8789
worker[taskInfo]?.done(null, result);

packages/terser/src/worker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { isMainThread, parentPort } from 'worker_threads';
1+
import { isMainThread, parentPort, workerData } from 'worker_threads';
22

33
import { hasOwnProperty, isObject } from 'smob';
44

55
import { minify } from 'terser';
66

7+
import { workerPoolWorkerFlag } from './constants';
8+
79
import type { WorkerContextSerialized, WorkerOutput } from './type';
810

911
/**
@@ -22,7 +24,7 @@ function isWorkerContextSerialized(input: unknown): input is WorkerContextSerial
2224
}
2325

2426
export function runWorker() {
25-
if (isMainThread || !parentPort) {
27+
if (isMainThread || !parentPort || workerData !== workerPoolWorkerFlag) {
2628
return;
2729
}
2830

0 commit comments

Comments
 (0)