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

main
ts 96 lines 3.77 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off
2 // Effect has no inflate, and Azure's blob batch only comes as a deflate zip.
3 import * as NodeZlib from "node:zlib";
4
5 import type { AzureDevOpsItemContent } from "./azureDevOpsPullRequestJson.ts";
6
7 const END_OF_CENTRAL_DIRECTORY = 0x06054b50;
8 const CENTRAL_DIRECTORY_ENTRY = 0x02014b50;
9 const LOCAL_FILE_HEADER = 0x04034b50;
10 const STORED = 0;
11 const DEFLATED = 8;
12 const ZIP64_MARKER = 0xffffffff;
13
14 const entryName = new TextDecoder();
15
16 /**
17 * The entries of the zip Azure's Blobs API answers with, keyed by lowercase blob id. Azure streams
18 * entries with data descriptors, so sizes come from the central directory. Null for an archive
19 * this cannot read; an entry over `maxEntryBytes`, past `maxTotalBytes` in all, or in another method
20 * is left out.
21 */
22 export function readBlobZip(
23 zip: Uint8Array,
24 maxEntryBytes: number,
25 maxTotalBytes: number,
26 ): ReadonlyMap<string, Uint8Array> | null {
27 const view = new DataView(zip.buffer, zip.byteOffset, zip.byteLength);
28 // The end record is 22 bytes followed by a comment of at most 65,535.
29 let end = -1;
30 for (let at = zip.length - 22; at >= 0 && at >= zip.length - 22 - 0xffff; at -= 1) {
31 if (view.getUint32(at, true) === END_OF_CENTRAL_DIRECTORY) {
32 end = at;
33 break;
34 }
35 }
36 if (end === -1) return null;
37 const count = view.getUint16(end + 10, true);
38 let at = view.getUint32(end + 16, true);
39 if (count === 0xffff || at === ZIP64_MARKER) return null;
40
41 const entries = new Map<string, Uint8Array>();
42 let total = 0;
43 for (let entry = 0; entry < count; entry += 1) {
44 if (at + 46 > zip.length || view.getUint32(at, true) !== CENTRAL_DIRECTORY_ENTRY) return null;
45 const method = view.getUint16(at + 10, true);
46 const compressedSize = view.getUint32(at + 20, true);
47 const size = view.getUint32(at + 24, true);
48 const nameLength = view.getUint16(at + 28, true);
49 const local = view.getUint32(at + 42, true);
50 const name = entryName.decode(zip.subarray(at + 46, at + 46 + nameLength)).toLowerCase();
51 at += 46 + nameLength + view.getUint16(at + 30, true) + view.getUint16(at + 32, true);
52 if (compressedSize === ZIP64_MARKER || size === ZIP64_MARKER || local === ZIP64_MARKER) {
53 return null;
54 }
55 if (local + 30 > zip.length || view.getUint32(local, true) !== LOCAL_FILE_HEADER) return null;
56 const start = local + 30 + view.getUint16(local + 26, true) + view.getUint16(local + 28, true);
57 if (start + compressedSize > zip.length) return null;
58 if (size > maxEntryBytes || total + size > maxTotalBytes) continue;
59 const data = zip.subarray(start, start + compressedSize);
60 if (method === STORED) {
61 if (compressedSize !== size) return null;
62 entries.set(name, data);
63 total += size;
64 } else if (method === DEFLATED) {
65 try {
66 const inflated = NodeZlib.inflateRawSync(data, { maxOutputLength: maxEntryBytes });
67 if (inflated.length !== size) return null;
68 entries.set(name, inflated);
69 total += size;
70 } catch {
71 return null;
72 }
73 }
74 }
75 return entries;
76 }
77
78 /** Strips a UTF-8 BOM, as Azure's `content` does. */
79 const strictUtf8 = new TextDecoder("utf-8", { fatal: true });
80
81 /**
82 * A blob's text as Azure's item read would give it, or null where Azure decodes the bytes itself
83 * (UTF-16, or text that is not UTF-8 such as Windows-1252) and only that read is faithful.
84 */
85 export function blobContent(bytes: Uint8Array): AzureDevOpsItemContent | null {
86 const utf16 =
87 (bytes[0] === 0xff && bytes[1] === 0xfe) || (bytes[0] === 0xfe && bytes[1] === 0xff);
88 if (utf16) return null;
89 // Azure flags a binary on the same evidence.
90 if (bytes.includes(0)) return { contents: "", isBinary: true };
91 try {
92 return { contents: strictUtf8.decode(bytes), isBinary: false };
93 } catch {
94 return null;
95 }
96 }