UNPKG

@adonis-agora/media

Version:

Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a

145 lines • 7.42 kB
import type { Readable } from 'node:stream'; import type { MediaLibrary } from './media_library.js'; import type { Disk } from './types.js'; /** * Requested READ strategy — the mirror image of `uploads.mode` for writes. * * - `public` — hand back the disk's stable public URL ({@link Disk.getUrl}). * - `signed` — hand back a time-limited signed URL ({@link Disk.getSignedUrl}). * - `proxy` — stream the bytes through the app, so the storage never has to be reachable from the * internet at all (a private bucket on a private network). * - `auto` — ask the disk for the object's visibility: `public` when it is public, else `signed`. */ export type DeliveryMode = 'auto' | 'public' | 'signed' | 'proxy'; /** The concrete strategy after resolution — `auto` has been collapsed to one of these. */ export type ResolvedDeliveryMode = 'public' | 'signed' | 'proxy'; /** Signed-URL lifetime used when neither the call nor `delivery.signedTtlSeconds` sets one. */ export declare const DEFAULT_DELIVERY_SIGNED_TTL_SECONDS = 300; /** * What delivering a media record yields. A discriminated union rather than an HTTP response, * because the library is framework-agnostic: the app turns a `redirect` into `response.redirect(url)` * and a `stream` into `response.stream(stream)` with whatever headers it wants. */ export type DeliveryResult = ({ kind: 'redirect'; url: string; } | { kind: 'stream'; stream: Readable; mimeType: string; size?: number | undefined; fileName?: string | undefined; }) & { /** * `'Accept'` when the format was negotiated from the request's `Accept` header (on-demand * resize): send it as the `Vary` header, or a cache in between hands an AVIF to a browser that * asked for WebP. */ vary?: 'Accept' | undefined; }; /** * Collapse a requested {@link DeliveryMode} to a concrete one for a specific object. * * Only `auto` needs the disk. Visibility is asked of the disk itself (`getVisibility`, which every * `@adonisjs/drive` disk exposes and which the bundled `disks.s3()` answers from its configured * visibility) — never guessed from `getUrl` being callable, since a URL can always be *synthesised* * for a private object and would resolve `auto` to a link that 403s. * * A disk that cannot answer leaves the visibility genuinely unknown, and the safe read of unknown is * "assume private": `auto` then resolves to `signed`, which works on a public object too. */ export declare function resolveDeliveryMode(mode: DeliveryMode | undefined, disk: Disk, key: string): Promise<ResolvedDeliveryMode>; /** What {@link MediaDeliveryHandler.handle} is asked to deliver. */ export interface DeliveryRequest { /** Id of the media record. */ mediaId: string; /** Deliver this named conversion instead of the original (generated lazily if absent). */ conversion?: string | undefined; /** * Deliver the image resized to this width — the `?w=` of a `next/image`-style loader. Must be one * of `delivery.images.widths`; resized once, then cached. A query-string value can be passed as * is: it is parsed and validated here. */ width?: number | string | undefined; /** Quality of the resized image (`?q=`); one of `delivery.images.qualities`. */ quality?: number | string | undefined; /** * The request's `Accept` header: picks the first of `delivery.images.formats` the browser * supports (AVIF before WebP, say). Absent ⇒ the first configured format. */ accept?: string | undefined; /** `?exp=` of a signed URL (`delivery.signing`). */ expires?: number | string | undefined; /** `?sig=` of a signed URL (`delivery.signing`). */ signature?: string | undefined; } /** * `next/image`'s default `deviceSizes` + `imageSizes`: the widths a loader may ask for. The same * list on both sides is what keeps a loader's `srcset` and the server's whitelist in step. */ export declare const DEFAULT_IMAGE_WIDTHS: readonly number[]; /** `next/image`'s default `qualities`: one, so a client cannot multiply the cache by quality. */ export declare const DEFAULT_IMAGE_QUALITIES: readonly number[]; /** On-demand resizing for `?w=`/`?q=` delivery (`delivery.images`). */ export interface OnDemandImageConfig { /** Widths a request may ask for. Default {@link DEFAULT_IMAGE_WIDTHS}. */ widths?: readonly number[]; /** Qualities a request may ask for; the first is the default. Default {@link DEFAULT_IMAGE_QUALITIES}. */ qualities?: readonly number[]; /** * Modern formats to serve, in order of preference, negotiated against the request's `Accept` * (like `next/image`'s `images.formats`). A browser that accepts none of them gets JPEG — or PNG * when the original is a PNG/GIF, to keep transparency. Default `['webp']`: AVIF is markedly * smaller but many times slower to encode, so it is opt-in (`['avif', 'webp']`). */ formats?: readonly ('avif' | 'webp')[]; } export interface MediaDeliveryHandlerOptions { /** The {@link MediaLibrary} records are resolved from — or a `MediaManager`, whose `.library` is used. */ library: MediaLibrary | { library: MediaLibrary; }; /** Default mode for this handler; overrides the library's configured `delivery.mode`. */ mode?: DeliveryMode | undefined; /** Signed-URL lifetime when the resolved mode is `signed`. */ signedTtlSeconds?: number | undefined; /** * Whether this route refuses unsigned requests, when `delivery.signing` is configured. Default: * `delivery.signing.required`. Set `false` on a route that authorizes by itself (a session, * an ownership check) so it keeps serving its own requests, while still honouring a signed one * (any size it names). One app can then sign `<img>` URLs on one route and guard downloads * by session on another. */ requireSignature?: boolean | undefined; } /** * Framework-agnostic read counterpart to {@link TusUploadHandler}: the app mounts ONE route, with * its own middleware, and delegates the "how do these bytes reach the client" question here. * * ```ts * const delivery = new MediaDeliveryHandler({ library: media }) * * router.get('/media/:id', async ({ params, response, auth }) => { * await authorize(auth.user, params.id) // ← the app's job, see below * const result = await delivery.handle({ mediaId: params.id }) * if (result.kind === 'redirect') return response.redirect(result.url) * response.header('content-type', result.mimeType) * return response.stream(result.stream) * }).use(middleware.auth()) * ``` * * **This handler performs NO authorization.** It resolves a media id to bytes or a URL and nothing * else — exactly the split {@link TusUploadHandler} draws. Whoever may read a given record is a * question only the app can answer, so guard the route (middleware, an ownership check, a signed * media token) before calling `handle`. Mounting it unguarded publishes every record to anyone who * can guess an id. */ export declare class MediaDeliveryHandler { private readonly library; private readonly mode; private readonly signedTtlSeconds; private readonly requireSignature; constructor(options: MediaDeliveryHandlerOptions); handle(request: DeliveryRequest): Promise<DeliveryResult>; } //# sourceMappingURL=delivery.d.ts.map