UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 4.03 kB
import e from"../../../../constants/constants.mjs";import t from"../../../../utils/errors/lucid-api-error.mjs";import{getTenantConfig as n}from"../../../../utils/helpers/tenants.mjs";import{getMediaKeyTenantKey as r}from"../../../../utils/media/media-key-tenant.mjs";import i from"../../../../utils/media/normalize-media-key.mjs";import a from"../../utils/create-service-context.mjs";import o from"../../../../services/cdn/stream-fallback-media.mjs";import s from"../../../../services/cdn/stream-media.mjs";import c from"../../../../utils/services/service-wrapper.mjs";import l from"../../middleware/authorize-private-media.mjs";import u from"../../middleware/rate-limiter.mjs";import d from"../../middleware/validate.mjs";import f from"../../openapi/index.mjs";import{applyRangeHeaders as p,applyStreamingHeaders as m,parseRangeHeader as h}from"../../utils/streaming.mjs";import{controllerSchemas as g}from"../../../../schemas/cdn.mjs";import{minutesToMilliseconds as _}from"date-fns";import{Readable as v}from"node:stream";import{createFactory as y}from"hono/factory";import{describeRoute as b}from"hono-openapi";import{stream as x}from"hono/streaming";const S=y().createHandlers(b({description:`Streams a piece of media based on the given key. If its an image, you can resize and format it on request. These will count towards the processed image usage that is unique to each image. This limit is configurable on a per project bases. Once it has been hit, instead of returning the processed image, it will return the original image. This is to prevent abuse of the endpoint.`,tags:[`cdn`],summary:`Stream Media`,parameters:f.parameters({params:g.streamSingle.params,query:g.streamSingle.query.string}),responses:{200:{description:`Successfully streamed media content`,content:{"*/*":{schema:{type:`string`,format:`binary`}}},headers:{"Content-Type":{schema:{type:`string`}},"Content-Length":{schema:{type:`integer`}},"Content-Disposition":{schema:{type:`string`}},"Cache-Control":{schema:{type:`string`}},"Accept-Ranges":{schema:{type:`string`}}}},206:{description:`Partial content - range request response`,content:{"*/*":{schema:{type:`string`,format:`binary`}}},headers:{"Content-Type":{schema:{type:`string`}},"Content-Length":{schema:{type:`integer`}},"Content-Range":{schema:{type:`string`}},"Accept-Ranges":{schema:{type:`string`}}}},404:{description:`Media not found - returns an error image`,content:{"image/*":{schema:{type:`string`,format:`binary`}}}},416:{description:`Range Not Satisfiable`,content:{"application/json":{schema:f.defaultErrorResponse}}},default:f.defaultErrorResponse}}),u({mode:`ip`,scope:e.rateLimit.scopes.stream.scopeKey,limit:e.rateLimit.scopes.stream.limit,windowMs:_(1)}),d(`param`,g.streamSingle.params),l,d(`query`,g.streamSingle.query.string),async e=>{let l=e.req.valid(`param`),u=e.req.valid(`query`),d=i(l.key),f=r(d),g=f?n(e.get(`config`),f):void 0;e.set(`tenant`,g?{key:g.key}:null);let _=a(e),y=h(e.req.header(`range`)),b=e.req.header(`if-none-match`),S=`public, max-age=0, must-revalidate`,C,w=await c(s,{transaction:!1})(_,{key:d,query:u,accept:e.req.header(`accept`),ifNoneMatch:y?void 0:b,range:y});if(w.error){let n=await c(o,{transaction:!1})(_,{fallback:u?.fallback?!!u?.fallback:void 0,secFetchDest:e.req.header(`sec-fetch-dest`),error:w.error});if(n.error)throw new t(n.error);return e.redirect(n.data.redirectUrl,302)}return C=w.data.etag??void 0,w.data.notModified?(p(e,{cacheControl:S}),m(e,{key:w.data.key,contentType:w.data.contentType,etag:C}),e.status(304),e.body(null)):(p(e,{isPartial:w.data.isPartialContent,range:w.data.range,totalSize:w.data.totalSize,cacheControl:S}),m(e,{key:w.data.key,contentLength:w.data.contentLength,contentType:w.data.contentType,etag:C}),x(e,async e=>{if(w.data.body instanceof ReadableStream)await e.pipe(w.data.body);else if(w.data.body instanceof Uint8Array)await e.write(w.data.body);else if(w.data.body instanceof v)for await(let t of w.data.body)await e.write(t)}))});export{S as default}; //# sourceMappingURL=stream-single.mjs.map