@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
112 lines • 5.67 kB
TypeScript
import type { DeliveryResult } from '../delivery.js';
import type { MediaLibrary } from '../media_library.js';
/** Identifies one file of a media's HLS package, handed to the URL callbacks. */
export interface HlsFileRef {
mediaId: string;
/** Package-relative artifact path (`'index.m3u8'`, `'playlist-1.m3u8'`, `'segment-0-3.ts'`). */
file: string;
}
/** Builds the URL a rewritten playlist reference should point at. */
export type HlsUrlBuilder = (ref: HlsFileRef) => string | Promise<string>;
/** What {@link HlsDeliveryHandler.handle} is asked to serve. */
export interface HlsDeliveryRequest {
mediaId: string;
/**
* Which file of the package, relative to the conversion prefix. Omit for the entry (master)
* playlist. Validated against the conversion's persisted artifact list, so a request can never
* reach outside the package.
*/
file?: string | undefined;
}
/**
* What serving one HLS package file yields: playlists come back as rewritten text (they are
* *always* rewritten, never proxied raw — raw ones reference the storage layout, not URLs), and
* media files come back as the usual {@link DeliveryResult} (`redirect` to a signed URL, or a
* `stream` to pipe).
*/
export type HlsDeliveryResult = {
kind: 'playlist';
content: string;
contentType: string;
fileName: string;
} | DeliveryResult;
export interface HlsDeliveryHandlerOptions {
/** The {@link MediaLibrary} records are resolved from — or a `MediaManager`, whose `.library` is used. */
library: MediaLibrary | {
library: MediaLibrary;
};
/** Name of the video conversion on the record. Default `'video'` (the `transformers.video()` default; its HLS master is the entry). */
conversion?: string | undefined;
/**
* URL for a **sub-playlist** reference (master → media playlists, audio renditions). Required,
* and it should point back at a route of yours that calls this handler again — playlists must be
* rewritten on every hop, and your route is where your authorization runs. There is no safe
* library default: presigning a playlist would serve it raw from storage, whose relative
* references then resolve against the storage host and break (or leak).
*/
urlForPlaylist: HlsUrlBuilder;
/**
* URL for a **media** reference (segments, init sections). Default: a presigned URL straight to
* the object on the disk (`segmentTtlSeconds` lifetime) — the common case, letting players pull
* segments from storage/CDN without the bytes transiting your app. Point it at your own proxy
* route instead when storage isn't reachable from clients (then serve those requests with
* {@link HlsDeliveryHandler.handle}, which streams media files per `segmentDelivery`).
*/
urlForSegment?: HlsUrlBuilder | undefined;
/** Lifetime of default presigned segment URLs, seconds. Default 300. */
segmentTtlSeconds?: number | undefined;
/**
* How {@link HlsDeliveryHandler.handle} answers a request for a media file (it always answers
* playlists with rewritten text): `'redirect'` (default) to a presigned URL, or `'stream'` to
* proxy the bytes through the app.
*/
segmentDelivery?: 'redirect' | 'stream' | undefined;
}
/**
* Framework-agnostic HLS read path — the {@link MediaDeliveryHandler} of a transformer-generated
* HLS package. One route of yours receives `(:mediaId, :file?)` and delegates here; the handler
* resolves the record's HLS conversion, validates the requested file against the package's
* persisted artifact list, and:
*
* - for a **playlist**: returns its content with every relative reference rewritten — sub-playlist
* references through your `urlForPlaylist` (auth stays yours), media references through
* `urlForSegment` or a presigned URL;
* - for a **media file**: returns a `redirect` (presigned) or a `stream`, per `segmentDelivery`.
*
* ```ts
* const hls = new HlsDeliveryHandler({
* library: media,
* urlForPlaylist: ({ mediaId, file }) => router.makeUrl('videos.hls', { id: mediaId, file }),
* })
*
* router.get('/videos/:id/hls/:file?', async ({ params, response, auth }) => {
* await authorize(auth.user, params.id) // ← the app's job, as everywhere
* const result = await hls.handle({ mediaId: params.id, file: params.file })
* if (result.kind === 'redirect') return response.redirect(result.url)
* if (result.kind === 'stream') {
* response.header('content-type', result.mimeType)
* return response.stream(result.stream)
* }
* response.header('content-type', result.contentType)
* return response.send(result.content)
* }).use(middleware.auth())
* ```
*
* **This handler performs NO authorization** — the same split as {@link MediaDeliveryHandler} and
* `TusUploadHandler`: guard the route before calling `handle`. Note that a presigned segment URL
* (the default) is itself a capability: whoever holds it can fetch that segment until the TTL
* expires. That is the standard HLS trade-off — keep `segmentTtlSeconds` short, or use
* `segmentDelivery: 'stream'` with a segment `urlForSegment` pointing back at your route to keep
* every byte behind your auth.
*/
export declare class HlsDeliveryHandler {
private readonly library;
private readonly conversion;
private readonly urlForPlaylist;
private readonly urlForSegment;
private readonly segmentTtlSeconds;
private readonly segmentDelivery;
constructor(options: HlsDeliveryHandlerOptions);
handle(request: HlsDeliveryRequest): Promise<HlsDeliveryResult>;
}
//# sourceMappingURL=delivery.d.ts.map