-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathimage-pool.ts
140 lines (133 loc) · 3.46 KB
/
image-pool.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { isMainThread } from 'node:worker_threads';
import { cpus } from 'os';
import { fileURLToPath } from 'url';
import type { OutputFormat } from '../../loaders/index.js';
import execOnce from '../../utils/execOnce.js';
import WorkerPool from '../../utils/workerPool.js';
import type { Operation } from './image.js';
import * as impl from './impl.js';
const getWorker = execOnce(
() => {
return new WorkerPool(
// There will be at most 7 workers needed since each worker will take
// at least 1 operation type.
Math.max(1, Math.min(cpus().length - 1, 7)),
fileURLToPath(import.meta.url)
);
}
)
type DecodeParams = {
operation: 'decode',
buffer: Buffer
};
type ResizeParams = {
operation: 'resize',
imageData: ImageData,
height?: number,
width?: number
};
type RotateParams = {
operation: 'rotate',
imageData: ImageData,
numRotations: number
};
type EncodeAvifParams = {
operation: 'encodeavif',
imageData: ImageData,
quality: number
}
type EncodeJpegParams = {
operation: 'encodejpeg',
imageData: ImageData,
quality: number
}
type EncodePngParams = {
operation: 'encodepng',
imageData: ImageData
}
type EncodeWebpParams = {
operation: 'encodewebp',
imageData: ImageData,
quality: number
}
type JobMessage = DecodeParams | ResizeParams | RotateParams | EncodeAvifParams | EncodeJpegParams | EncodePngParams | EncodeWebpParams
function handleJob(params: JobMessage) {
switch (params.operation) {
case 'decode':
return impl.decodeBuffer(params.buffer)
case 'resize':
return impl.resize({ image: params.imageData as any, width: params.width, height: params.height })
case 'rotate':
return impl.rotate(params.imageData as any, params.numRotations);
case 'encodeavif':
return impl.encodeAvif(params.imageData as any, { quality: params.quality })
case 'encodejpeg':
return impl.encodeJpeg(params.imageData as any, { quality: params.quality })
case 'encodepng':
return impl.encodePng(params.imageData as any)
case 'encodewebp':
return impl.encodeWebp(params.imageData as any, { quality: params.quality })
default:
throw Error(`Invalid job "${(params as any).operation}"`);
}
}
export async function processBuffer(
buffer: Buffer,
operations: Operation[],
encoding: OutputFormat,
quality?: number
): Promise<Uint8Array> {
// @ts-ignore
const worker = await getWorker()
let imageData = await worker.dispatchJob({
operation: 'decode',
buffer,
})
for (const operation of operations) {
if (operation.type === 'rotate') {
imageData = await worker.dispatchJob({
operation: 'rotate',
imageData,
numRotations: operation.numRotations
});
} else if (operation.type === 'resize') {
imageData = await worker.dispatchJob({
operation: 'resize',
imageData,
height: operation.height,
width: operation.width
})
}
}
switch (encoding) {
case 'avif':
return await worker.dispatchJob({
operation: 'encodeavif',
imageData,
quality,
}) as Uint8Array;
case 'jpeg':
case 'jpg':
return await worker.dispatchJob({
operation: 'encodejpeg',
imageData,
quality,
}) as Uint8Array;
case 'png':
return await worker.dispatchJob({
operation: 'encodepng',
imageData,
}) as Uint8Array;
case 'webp':
return await worker.dispatchJob({
operation: 'encodewebp',
imageData,
quality,
}) as Uint8Array;
default:
throw Error(`Unsupported encoding format`)
}
}
if (!isMainThread) {
WorkerPool.useThisThreadAsWorker(handleJob);
}