Repository navigation
Expand file tree
/
Copy pathpng-worker.ts
More file actions
108 lines (102 loc) · 3.7 KB
/
Copy pathpng-worker.ts
File metadata and controls
108 lines (102 loc) · 3.7 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
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
import { parentPort } from 'node:worker_threads';
import { normalizeError } from '@agent-device/kernel/errors';
import { cropPngBytes } from './png-crop-bytes.ts';
import { decodePng, PNG } from './png.ts';
import { computeScreenshotDiffPixels } from './screenshot-diff-pixels.ts';
import { computePngRgbDifference } from './png-rgb-difference.ts';
import {
toBuffer,
type PngWorkerJobResult,
type PngWorkerRequest,
type PngWorkerResponse,
} from './png-worker-contract.ts';
/**
* Worker thread entry that runs CPU-heavy PNG decode/encode and screenshot
* pixel-diff jobs off the daemon event loop. Spawned lazily by
* `png-worker-client.ts`; published as the `internal/png-worker` build entry.
*/
// The daemon prewarms this worker at startup, so the JPEG decoder loads only for a transcode job.
async function runJob(request: PngWorkerRequest): Promise<PngWorkerJobResult> {
switch (request.kind) {
case 'decode': {
const png = decodePng(toBuffer(request.png), request.label);
return { kind: 'decode', width: png.width, height: png.height, data: png.data };
}
case 'encode': {
const png = new PNG({ width: request.width, height: request.height });
png.data = toBuffer(request.data);
return { kind: 'encode', png: PNG.sync.write(png) };
}
case 'crop': {
return {
kind: 'crop',
png: cropPngBytes(toBuffer(request.png), request.box, request.label),
};
}
case 'rgb-difference': {
const first = decodePng(toBuffer(request.firstPng), request.label);
const second = decodePng(toBuffer(request.secondPng), request.label);
return { kind: 'rgb-difference', ...computePngRgbDifference(first, second) };
}
case 'diff-pixels': {
return { kind: 'diff-pixels', ...computeScreenshotDiffPixels(request) };
}
case 'jpeg-to-png': {
const { transcodeScreenshotToPng } = await import('./png-transcode.ts');
return {
kind: 'jpeg-to-png',
png: transcodeScreenshotToPng(toBuffer(request.image), request.label),
};
}
}
}
/**
* True when the view fully owns a real ArrayBuffer. Views over a slice of a
* larger buffer (e.g. Node's shared pool for small Buffers) do not qualify:
* transferring their backing store would detach unrelated Buffers.
*/
function ownsEntireArrayBuffer(view: Uint8Array): view is Uint8Array<ArrayBuffer> {
return (
view.buffer instanceof ArrayBuffer &&
view.byteOffset === 0 &&
view.byteLength === view.buffer.byteLength
);
}
/**
* Transfers result buffers instead of structured-cloning them, but only when a
* view fully owns its ArrayBuffer. Exported for direct unit coverage; the
* worker itself is the only runtime caller.
*/
export function resultTransferList(result: PngWorkerJobResult): ArrayBuffer[] {
return resultBufferViews(result)
.filter(ownsEntireArrayBuffer)
.map((view) => view.buffer);
}
function resultBufferViews(result: PngWorkerJobResult): Uint8Array[] {
switch (result.kind) {
case 'decode':
return [result.data];
case 'encode':
return [result.png];
case 'crop':
return result.png === null ? [] : [result.png];
case 'rgb-difference':
return [];
case 'diff-pixels':
return [result.diffData, result.diffMask];
case 'jpeg-to-png':
return [result.png];
}
}
const port = parentPort;
if (port) {
port.on('message', async (request: PngWorkerRequest) => {
let response: PngWorkerResponse;
try {
response = { id: request.id, ok: true, result: await runJob(request) };
} catch (error) {
response = { id: request.id, ok: false, error: normalizeError(error) };
}
port.postMessage(response, response.ok ? resultTransferList(response.result) : []);
});
}