UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.32 kB
import{t as isSelfContainedSvg}from"./svg-image-CTMopoti.mjs";import{d as normalizeMimeType}from"./mime-ZmYZsEjx.mjs";import{n as matchesHttpIfNoneMatch}from"./http-conditional-CBtEomUz.mjs";import path from"node:path";import{createHash}from"node:crypto"; //!src/gateway/http-image-response.ts const HTTP_IMAGE_MAX_BYTES=524288;const HTTP_SVG_MAX_BYTES=65536;const SVG_MIME_TYPE=`image/svg+xml`;const ICO_MIME_TYPE=`image/x-icon`;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}function createHttpImageRepresentation(e,t){return{body:e,contentType:t,etag:`"${createHash(`sha256`).update(e).digest(`base64url`)}"`}}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)}function applyHttpImageContentSecurityPolicy(e){e.setHeader(`content-security-policy`,`default-src 'none'; base-uri 'none'; object-src 'none'; frame-ancestors 'none'; sandbox`)}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)}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};