Skip to content

Commit 0b47a26

Browse files
committed
refactor(capture-kit): keep the JPEG transcode behind the PNG worker
The Limrun screenshot path reaches the transcoder only through png-worker-client, so the ./png-transcode subpath and its layering-guard entry are dropped. The PNG signature check lives in png.ts, which both the worker client and the transcoder already load, so the two sniffs cannot drift and no entry's eager closure grows. The corrupt-JPEG worker test now pins details.label and details.reason across the worker boundary. Also restores the root package.json dependency order main has.
1 parent 06976a0 commit 0b47a26

6 files changed

Lines changed: 14 additions & 12 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@
284284
"@agent-device/device-selection": "workspace:*",
285285
"@agent-device/host-kit": "workspace:*",
286286
"@agent-device/kernel": "workspace:*",
287-
"@agent-device/maestro": "workspace:*",
288287
"@agent-device/managed-allocation": "workspace:*",
288+
"@agent-device/maestro": "workspace:*",
289289
"@agent-device/platform-android": "workspace:*",
290290
"@agent-device/platform-apple": "workspace:*",
291291
"@agent-device/platform-harmonyos": "workspace:*",

‎packages/capture-kit/package.json‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@
126126
"types": "./src/png-crop.ts",
127127
"default": "./src/png-crop.ts"
128128
},
129-
"./png-transcode": {
130-
"types": "./src/png-transcode.ts",
131-
"default": "./src/png-transcode.ts"
132-
},
133129
"./png-resize": {
134130
"types": "./src/png-resize.ts",
135131
"default": "./src/png-resize.ts"

‎packages/capture-kit/src/png-transcode.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { AppError } from '@agent-device/kernel/errors';
22
import { decode as decodeJpeg } from 'jpeg-js';
3-
import { PNG } from 'pngjs';
3+
import { hasPngSignature, PNG } from './png.ts';
44

5-
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
65
const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff]);
76

87
export type ScreenshotImageFormat = 'png' | 'jpeg';
98

109
/** Names the container a screenshot arrived in from its magic bytes, or `undefined` for neither. */
1110
export function detectScreenshotImageFormat(bytes: Buffer): ScreenshotImageFormat | undefined {
12-
if (bytes.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) return 'png';
11+
if (hasPngSignature(bytes)) return 'png';
1312
if (bytes.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE)) return 'jpeg';
1413
return undefined;
1514
}

‎packages/capture-kit/src/png-worker-client.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ test('transcodeScreenshotToPngAsync rejects a corrupt JPEG with the canonical de
133133
assert.equal(error instanceof AppError, true);
134134
assert.equal((error as AppError).code, 'COMMAND_FAILED');
135135
assert.match((error as AppError).message, /Failed to decode fixture as JPEG/);
136+
assert.equal((error as AppError).details?.label, 'fixture');
137+
assert.ok(String((error as AppError).details?.reason).length > 0);
136138
return true;
137139
});
138140
});

‎packages/capture-kit/src/png-worker-client.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
33
import { AppError, toAppErrorCode } from '@agent-device/kernel/errors';
44
import type { Rect } from '@agent-device/kernel/snapshot';
55
import { resolveInternalEntryModulePath } from './internal-entry.ts';
6-
import { decodePng, PNG } from './png.ts';
6+
import { decodePng, hasPngSignature, PNG } from './png.ts';
77
import {
88
computeScreenshotDiffPixels,
99
type ScreenshotDiffPixelsJob,
@@ -28,8 +28,6 @@ import {
2828
*/
2929

3030
const PNG_WORKER_ENTRYPOINT = 'png-worker';
31-
// Kept local so the JPEG decoder module stays out of every entry that only needs PNG jobs.
32-
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
3331

3432
/** Worker-infrastructure failure: the generic runner falls back to the sync path. */
3533
class PngWorkerUnavailableError extends Error {}
@@ -265,7 +263,7 @@ export async function computeScreenshotDiffPixelsAsync(
265263
* capture never blocks the daemon event loop. Decode failures carry the canonical `AppError`.
266264
*/
267265
export async function transcodeScreenshotToPngAsync(bytes: Buffer, label: string): Promise<Buffer> {
268-
if (bytes.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) return bytes;
266+
if (hasPngSignature(bytes)) return bytes;
269267
const result = await runPngJob({ kind: 'jpeg-to-png', image: bytes, label }, async () => {
270268
// Read on demand so the JPEG decoder stays out of the import closure of every entry that only
271269
// needs the worker's other jobs.

‎packages/capture-kit/src/png.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { PNG } from 'pngjs';
33

44
export { PNG };
55

6+
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
7+
8+
/** Whether the bytes open with the PNG signature, before any decode is attempted. */
9+
export function hasPngSignature(bytes: Buffer): boolean {
10+
return bytes.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE);
11+
}
12+
613
/**
714
* Decodes a PNG, wrapping failures in the canonical decode `AppError`. Shared
815
* by the in-process sync path and the PNG worker thread (`png-worker.ts`), so

0 commit comments

Comments
 (0)