-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
35 lines (28 loc) · 948 Bytes
/
worker.js
File metadata and controls
35 lines (28 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// worker.js - Worker thread implementation
import { parentPort, workerData } from "worker_threads";
async function executeFunction() {
try {
// Deserialize the function and arguments
const fnStr = workerData.fn;
const args = workerData.args;
// Convert string representation back to function
// Note: This approach has security implications if untrusted code is passed
const fn = new Function(`return ${fnStr}`)();
// Execute the function with provided arguments
const result = await fn(...args);
// Send result back to main thread
parentPort.postMessage(result);
} catch (error) {
// Convert error to a serializable object
const serializableError = {
message: error.message,
stack: error.stack,
name: error.name,
};
throw serializableError;
}
}
executeFunction().catch((error) => {
// Forward any errors to the parent
parentPort.emit("error", error);
});