Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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';
shellscape marked this conversation as resolved.
Show resolved Hide resolved
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