@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.91 kB
Source Map (JSON)
{"version":3,"file":"streaming.mjs","names":[],"sources":["../../../../src/libs/http/utils/streaming.ts"],"sourcesContent":["import type { LucidHonoContext } from \"../../../types/hono.js\";\nimport { formatETag } from \"../../../utils/http/etag.js\";\n\nconst SVG_CONTENT_SECURITY_POLICY =\n\t\"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:\";\n\nexport const applyStreamingHeaders = (\n\tc: LucidHonoContext,\n\topts: {\n\t\tcontentLength?: number;\n\t\tcontentType?: string;\n\t\tkey?: string;\n\t\tetag?: string;\n\t},\n) => {\n\tc.header(\"Accept-Ranges\", \"bytes\");\n\tc.header(\"X-Content-Type-Options\", \"nosniff\");\n\tif (\n\t\topts.contentType?.split(\";\", 1)[0]?.trim().toLowerCase() === \"image/svg+xml\"\n\t) {\n\t\tc.header(\"Content-Security-Policy\", SVG_CONTENT_SECURITY_POLICY);\n\t}\n\tif (opts.key)\n\t\tc.header(\"Content-Disposition\", `inline; filename=\"${opts.key}\"`);\n\tif (opts.contentLength !== undefined)\n\t\tc.header(\"Content-Length\", String(opts.contentLength));\n\tif (opts.contentType) c.header(\"Content-Type\", opts.contentType);\n\tif (opts.etag) c.header(\"ETag\", formatETag(opts.etag));\n};\n\nexport const applyRangeHeaders = (\n\tc: LucidHonoContext,\n\tinfo: {\n\t\tisPartial?: boolean;\n\t\trange?: { start: number; end: number };\n\t\ttotalSize?: number;\n\t\tcacheControl?: string;\n\t},\n) => {\n\tif (info.isPartial && info.range && info.totalSize !== undefined) {\n\t\tc.status(206);\n\t\tc.header(\n\t\t\t\"Content-Range\",\n\t\t\t`bytes ${info.range.start}-${info.range.end}/${info.totalSize}`,\n\t\t);\n\t} else {\n\t\tc.header(\n\t\t\t\"Cache-Control\",\n\t\t\tinfo.cacheControl ?? \"public, max-age=31536000, immutable\",\n\t\t);\n\t}\n};\n\nexport const parseRangeHeader = (header: string | undefined) => {\n\tif (!header) return undefined;\n\tconst match = header.match(/bytes=(\\d+)-(\\d*)/);\n\tif (match?.[1]) {\n\t\tconst start = Number.parseInt(match[1], 10);\n\t\tconst end = match[2] ? Number.parseInt(match[2], 10) : undefined;\n\t\treturn { start, end };\n\t}\n\treturn undefined;\n};\n"],"mappings":"0DAGA,MAGa,GACZ,EACA,IAMI,CACJ,EAAE,OAAO,gBAAiB,OAAO,EACjC,EAAE,OAAO,yBAA0B,SAAS,EAE3C,EAAK,aAAa,MAAM,IAAK,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,YAAY,IAAM,iBAE7D,EAAE,OAAO,0BAA2B,uEAA2B,EAE5D,EAAK,KACR,EAAE,OAAO,sBAAuB,qBAAqB,EAAK,IAAI,EAAE,EAC7D,EAAK,gBAAkB,IAAA,IAC1B,EAAE,OAAO,iBAAkB,OAAO,EAAK,aAAa,CAAC,EAClD,EAAK,aAAa,EAAE,OAAO,eAAgB,EAAK,WAAW,EAC3D,EAAK,MAAM,EAAE,OAAO,OAAQ,EAAW,EAAK,IAAI,CAAC,CACtD,EAEa,GACZ,EACA,IAMI,CACA,EAAK,WAAa,EAAK,OAAS,EAAK,YAAc,IAAA,IACtD,EAAE,OAAO,GAAG,EACZ,EAAE,OACD,gBACA,SAAS,EAAK,MAAM,MAAM,GAAG,EAAK,MAAM,IAAI,GAAG,EAAK,WACrD,GAEA,EAAE,OACD,gBACA,EAAK,cAAgB,qCACtB,CAEF,EAEa,EAAoB,GAA+B,CAC/D,GAAI,CAAC,EAAQ,OACb,IAAM,EAAQ,EAAO,MAAM,mBAAmB,EAC9C,GAAI,IAAQ,GAGX,MAAO,CAAE,MAFK,OAAO,SAAS,EAAM,GAAI,EAE3B,EAAG,IADJ,EAAM,GAAK,OAAO,SAAS,EAAM,GAAI,EAAE,EAAI,IAAA,EACnC,CAGtB"}