Skip to content

Commit

Permalink
fix(terser): don't assume code is running in worker created by the wo…
Browse files Browse the repository at this point in the history
…rker pool (#1483)

* fix(terser): don't assume code is running in worker created by the worker pool

* sort imports
  • Loading branch information
dasa authored May 16, 2023
1 parent b3a65b9 commit 4e92bc2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/terser/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const taskInfo = Symbol('taskInfo');
export const freeWorker = Symbol('freeWorker');
export const workerPoolWorkerFlag = 'WorkerPoolWorker';
6 changes: 4 additions & 2 deletions packages/terser/src/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions packages/terser/src/worker.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -22,7 +24,7 @@ function isWorkerContextSerialized(input: unknown): input is WorkerContextSerial
}

export function runWorker() {
if (isMainThread || !parentPort) {
if (isMainThread || !parentPort || workerData !== workerPoolWorkerFlag) {
return;
}

Expand Down

0 comments on commit 4e92bc2

Please sign in to comment.