UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

112 lines (111 loc) • 4.67 kB
import { d as normalizeMimeType } from "./mime-D2wVBzpa.mjs"; import { a as getRemoteControlUiIngressContext, r as assertRemoteControlUiIngressCurrent } from "./ingress-attribution-DAAQgd6y.mjs"; import { r as respondNotFound } from "./control-ui-http-utils-DH5C3tE0.mjs"; import { t as ARTIFACT_DOWNLOAD_PATH } from "./artifact-download-CXaDlLGk.mjs"; import { d as sendMethodNotAllowed } from "./http-common-FfhIIwOW.mjs"; import { c as retainArtifactDownloadStream, s as resolveArtifactDownloadGrant, t as assertArtifactDownloadGrantCurrent } from "./artifact-download-grants-D5a8V4KO.mjs"; import { n as normalizeChatImageRenditionWidth } from "./chat-image-renditions-swq0mtjT.mjs"; import { t as buildAssistantMediaContentDisposition } from "./assistant-media-content-disposition-y--dAvCz.mjs"; import { i as writeByteHeaders, r as resolveByteResponse } from "./http-byte-range-D1yrfDRK.mjs"; import { r as resolveManagedImageThumbnail, t as encodeImageThumbnail } from "./managed-image-thumbnail-cache-B4-EHveZ.mjs"; import { createHash } from "node:crypto"; import { pipeline } from "node:stream/promises"; //#region src/gateway/artifact-downloads.ts /** The RPC chooses and authorizes the resource; HTTP never accepts a replacement query. */ async function handleArtifactDownloadHttpRequest(e, t, n) { const r = new URL(e.url ?? "/", "http://localhost"); const i = n.basePath && r.pathname.startsWith(`${n.basePath}${"/api/artifacts/download/"}`) ? r.pathname.slice(n.basePath.length) : r.pathname; if (!i.startsWith("/api/artifacts/download/")) return false; if (e.method !== "GET" && e.method !== "HEAD") { sendMethodNotAllowed(t, "GET, HEAD"); return true; } const a = resolveArtifactDownloadGrant(n.clients, i.slice(ARTIFACT_DOWNLOAD_PATH.length)); if (!a || a.grant.source.kind !== "bytes") { respondNotFound(t); return true; } const { client: o, grant: s, ticket: c } = a; const l = a.grant.source; let assertGrantCurrent = () => assertArtifactDownloadGrantCurrent(n.clients, o, c, s); const assertCurrent = () => { const t = getRemoteControlUiIngressContext(e); assertRemoteControlUiIngressCurrent(t); if (t && o.internal?.remoteIngressPrincipal?.assertCurrent !== t.resolvePrincipal().assertCurrent) throw new Error("Artifact download belongs to another ingress grant"); assertGrantCurrent(); }; let u; const d = l.image && r.searchParams.get("variant") === "thumbnail"; try { assertCurrent(); u = await l.read({ expectedDigest: l.digest, method: d ? "GET" : e.method, headers: d ? {} : { range: e.headers.range, "if-range": e.headers["if-range"], "if-none-match": e.headers["if-none-match"] } }); assertCurrent(); if (d) { if (!u?.body || u.stream) throw new Error("Artifact image preview bytes are unavailable"); const t = u.body; const n = normalizeChatImageRenditionWidth(r.searchParams.get("width")); const i = `${createHash("sha256").update(t).digest("hex")}\0${n}`; const a = new Uint8Array(await resolveManagedImageThumbnail(i, () => encodeImageThumbnail(t, n))); u.artifact = { ...u.artifact, mimeType: "image/webp", title: `${u.artifact.title}-preview.webp` }; const o = resolveByteResponse({ file: { size: a.byteLength }, method: e.method, request: e }); u.response = o; u.body = e.method === "HEAD" || o.kind === "not-modified" || o.kind === "unsatisfiable" ? void 0 : o.kind === "partial" ? a.subarray(o.range.start, o.range.end + 1) : a; assertCurrent(); } } catch { u = void 0; } if (!u) { respondNotFound(t); return true; } const { artifact: f, response: p, body: m } = u; const h = normalizeMimeType(f.mimeType); const g = h && /^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/i.test(h) ? h : "application/octet-stream"; t.setHeader("content-type", g); t.setHeader("content-disposition", buildAssistantMediaContentDisposition(f.title)); t.setHeader("content-security-policy", "default-src 'none'; sandbox"); t.setHeader("x-content-type-options", "nosniff"); t.setHeader("referrer-policy", "no-referrer"); t.setHeader("cache-control", "private, no-store"); writeByteHeaders(t, p); if (u.stream) { const e = u.stream; let r; try { r = retainArtifactDownloadStream(n.clients, o, c, s); assertGrantCurrent = r.assertCurrent; await pipeline(async function* (t) { const n = t.signal; for await (const t of e(n)) { n.throwIfAborted(); assertCurrent(); yield t; } }, t, { signal: o.connectionSignal }); } catch { t.destroy(); } finally { r?.release(); } } else t.end(m); return true; } //#endregion export { handleArtifactDownloadHttpRequest };