openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
73 lines (72 loc) • 3.18 kB
JavaScript
import { t as isSelfContainedSvg } from "./svg-image-ClSksOzI.mjs";
import { d as normalizeMimeType } from "./mime-D2wVBzpa.mjs";
import { n as matchesHttpIfNoneMatch } from "./http-conditional-BnFeJ7I4.mjs";
import path from "node:path";
import { createHash } from "node:crypto";
//#region src/gateway/http-image-response.ts
/** Authenticated UI images are deliberately small, bounded presentation assets. */
const HTTP_IMAGE_MAX_BYTES = 524288;
/** Vector images are markup the renderer must parse, so they get a tighter cap. */
const HTTP_SVG_MAX_BYTES = 65536;
const SVG_MIME_TYPE = "image/svg+xml";
const ICO_MIME_TYPE = "image/x-icon";
/** Image types accepted by the authenticated Control UI image routes. */
const ALLOWED_HTTP_IMAGE_MIME_TYPES = /* @__PURE__ */ new Set([
"image/avif",
"image/gif",
"image/jpeg",
"image/png",
SVG_MIME_TYPE,
"image/webp",
ICO_MIME_TYPE
]);
function resolveHttpImageMimeType(e) {
const t = normalizeMimeType(e);
const n = t === "image/vnd.microsoft.icon" ? ICO_MIME_TYPE : t;
return n && ALLOWED_HTTP_IMAGE_MIME_TYPES.has(n) ? n : void 0;
}
/** Hash final, validated response bytes once when their cached representation is created. */
function createHttpImageRepresentation(e, t) {
return {
body: e,
contentType: t,
etag: `"${createHash("sha256").update(e).digest("base64url")}"`
};
}
/** Sniffs and validates bytes before they become a browser image response. */
async function resolveHttpImageRepresentation(e, t) {
if (t.byteLength === 0 || t.byteLength > 524288) return;
let n;
if (path.extname(e).toLowerCase() === ".svg") n = t.byteLength <= 65536 && isSelfContainedSvg(t.toString("utf8")) ? SVG_MIME_TYPE : void 0;
else {
const { fileTypeFromBuffer: e } = await import("file-type");
n = resolveHttpImageMimeType((await e(t))?.mime);
}
if (!n) return;
return createHttpImageRepresentation(t, n);
}
/** Prevent image documents from executing scripts or making cross-origin requests. */
function applyHttpImageContentSecurityPolicy(e) {
e.setHeader("content-security-policy", "default-src 'none'; base-uri 'none'; object-src 'none'; frame-ancestors 'none'; sandbox");
}
/** Writes the shared private-cache and document-sandbox policy for image bytes. */
function sendHttpImageResponse(e) {
const { req: t, res: n, image: r } = e;
n.setHeader("etag", r.etag);
n.setHeader("cache-control", e.cacheControl ?? "private, max-age=3600");
n.setHeader("cross-origin-resource-policy", "same-origin");
n.setHeader("x-content-type-options", "nosniff");
applyHttpImageContentSecurityPolicy(n);
n.setHeader("content-disposition", `attachment; filename="${e.filename}"`);
if (matchesHttpIfNoneMatch(t.headers["if-none-match"], r.etag)) {
n.statusCode = 304;
n.end();
return;
}
n.statusCode = 200;
n.setHeader("content-type", r.contentType);
n.setHeader("content-length", String(r.body.byteLength));
n.end(t.method === "HEAD" ? void 0 : r.body);
}
//#endregion
export { resolveHttpImageMimeType as a, createHttpImageRepresentation as i, HTTP_SVG_MAX_BYTES as n, resolveHttpImageRepresentation as o, applyHttpImageContentSecurityPolicy as r, sendHttpImageResponse as s, HTTP_IMAGE_MAX_BYTES as t };