The best way to control Claude Code, Codex and more from any computer or phone

main
ts 24 lines 957 Bytes
Raw
1 const PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
2
3 /**
4 * Returns the first complete PNG in `bytes`, or undefined. `adb exec-out screencap -p` can print
5 * warnings, such as a foldable's multiple displays, around the image.
6 */
7 export const extractPng = <Buffer extends ArrayBufferLike>(
8 bytes: Uint8Array<Buffer>,
9 ): Uint8Array<Buffer> | undefined => {
10 const start = bytes.findIndex((_, index) =>
11 PNG_SIGNATURE.every((value, offset) => bytes[index + offset] === value),
12 );
13 if (start < 0) return undefined;
14 const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
15 for (let chunk = start + PNG_SIGNATURE.length; chunk + 12 <= bytes.length;) {
16 const end = chunk + 12 + view.getUint32(chunk);
17 if (end > bytes.length) return undefined;
18 if (String.fromCharCode(...bytes.subarray(chunk + 4, chunk + 8)) === "IEND") {
19 return bytes.subarray(start, end);
20 }
21 chunk = end;
22 }
23 return undefined;
24 };