|
| 1 | +import { DurableObject } from 'cloudflare:workers' |
| 2 | +import JSZip from 'jszip' |
| 3 | + |
| 4 | +import type { Env } from './dex-analysis.context' |
| 5 | + |
| 6 | +// Helper for reading large WARP diag zip archives. |
| 7 | +// Holds the contents in memory between requests from the agent for specific files |
| 8 | +// instead of having the worker download the zip on every request. |
| 9 | +// |
| 10 | +// Each DO represents one remote capture zip |
| 11 | +export class WarpDiagReader extends DurableObject<Env> { |
| 12 | + #cache?: { files: string[]; zip: JSZip } |
| 13 | + |
| 14 | + // List the files in the zip for the agent |
| 15 | + async list(accessToken: string, url: string) { |
| 16 | + const { files } = await this.#getZip(accessToken, url) |
| 17 | + return files |
| 18 | + } |
| 19 | + |
| 20 | + // Return the contents of a file by path |
| 21 | + async read(accessToken: string, url: string, filepath: string) { |
| 22 | + const { zip } = await this.#getZip(accessToken, url) |
| 23 | + const file = zip.file(filepath) |
| 24 | + const content = await file?.async('text') |
| 25 | + return content |
| 26 | + } |
| 27 | + |
| 28 | + async #getZip(accessToken: string, url: string) { |
| 29 | + if (this.#cache) { |
| 30 | + return this.#cache |
| 31 | + } |
| 32 | + |
| 33 | + console.log(`WarpDiagReader fetching `, url) |
| 34 | + |
| 35 | + const res = await fetch(url, { |
| 36 | + headers: { |
| 37 | + Authorization: `Bearer ${accessToken}`, |
| 38 | + }, |
| 39 | + }) |
| 40 | + |
| 41 | + if (res.status !== 200) { |
| 42 | + throw new Error(`failed to download zip, non-200 status code: ${res.status}`) |
| 43 | + } |
| 44 | + |
| 45 | + const zip = await new JSZip().loadAsync(await res.arrayBuffer()) |
| 46 | + const files: string[] = [] |
| 47 | + for (const [relativePath, file] of Object.entries(zip.files)) { |
| 48 | + if (!file.dir) { |
| 49 | + files.push(relativePath) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const cache = { files, zip } |
| 54 | + this.#cache = cache |
| 55 | + return cache |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function hashToken(accessToken: string) { |
| 60 | + const hashArr = Array.from( |
| 61 | + new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(accessToken))) |
| 62 | + ) |
| 63 | + return hashArr.map((b) => b.toString(16).padStart(2, '0')).join('') |
| 64 | +} |
| 65 | + |
| 66 | +// Create unique name based on accessToken hash and download url. In order to read cached zip from memory |
| 67 | +// you need to have the same access token that was used to fetch it. |
| 68 | +async function readerName(accessToken: string, url: string) { |
| 69 | + return (await hashToken(accessToken)) + url |
| 70 | +} |
| 71 | + |
| 72 | +export async function getReader(env: Env, accessToken: string, download: string) { |
| 73 | + const name = await readerName(accessToken, download) |
| 74 | + const id = env.WARP_DIAG_READER.idFromName(name) |
| 75 | + return env.WARP_DIAG_READER.get(id) |
| 76 | +} |
0 commit comments