-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.js
More file actions
48 lines (44 loc) · 1.21 KB
/
Copy paththread.js
File metadata and controls
48 lines (44 loc) · 1.21 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
const {
Worker,
isMainThread,
workerData,
parentPort,
} = require("worker_threads");
const sharp = require("sharp");
if (isMainThread) {
function runService() {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: {
imagePath: "input.jpg",
outputPath: "output.jpg",
width: 24000,
height: 16000,
},
});
worker.on("message", resolve);
worker.on("error", reject);
worker.on("exit", (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
}
async function run() {
const result = await runService("world");
console.log(result);
}
run().catch((err) => console.error(err));
console.log("Resizing image");
} else {
const { imagePath, outputPath, width, height } = workerData;
sharp(imagePath)
.resize(width, height)
.toFile(outputPath, (err) => {
if (err) {
throw err;
}
// Send a message back to the main thread
parentPort.postMessage("Image resized successfully!");
});
}