@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
93 lines • 4.48 kB
TypeScript
import type { MediaRecord } from '../media_record.js';
import type { Transformer, TransformerContext, TransformResult } from '../transformer.js';
/** What a placeholder run yields — persisted verbatim as the conversion's `meta`. */
export interface ImagePlaceholder {
/**
* A tiny (≤ `size` px on the long edge) WebP of the image, as a `data:` URL — typically a few
* hundred bytes. Inline it as the `<img>` background (or `src`) and blur it with CSS while the
* real image loads; no client-side decoder needed.
*/
dataUrl: string;
/** Width of the original, after EXIF orientation. Use it with {@link height} to reserve layout. */
width: number;
/** Height of the original, after EXIF orientation. */
height: number;
/** Dominant colour as `#rrggbb` — a flat fallback for the placeholder. */
color: string;
}
export interface PlaceholderOptions {
/** Long edge of the tiny preview, in px. Default `16`. */
size: number;
/** WebP quality of the tiny preview. Default `50`. */
quality: number;
}
/**
* The engine seam of {@link PlaceholderTransformer}: image bytes in, placeholder out. The default
* engine is sharp (lazy); tests inject a fake.
*/
export interface PlaceholderEngine {
generate(input: Uint8Array, options: PlaceholderOptions): Promise<ImagePlaceholder>;
}
export interface PlaceholderTransformerOptions<Name extends string = 'placeholder'> {
/** Conversion name on the record. Default `'placeholder'`. */
name?: Name;
/**
* Run inside attach. Default `true`: a placeholder is only useful if it exists by the time the
* image is first rendered, and generating one is a single small decode. It does read the whole
* original, though — on a remote disk (S3) that is a download per attach; set `false` and run
* `media.library.transform(id, name)` from a job if that matters more than having it at once.
*/
eager?: boolean;
/** Long edge of the tiny preview, in px. Default `16` — bigger is sharper but heavier inline. */
size?: number;
/** WebP quality of the tiny preview (1–100). Default `50`. */
quality?: number;
/** Replace the sharp engine (mainly for tests). */
engine?: PlaceholderEngine;
}
/**
* The built-in **image placeholder** {@link Transformer} — a metadata-only transform: it writes no
* artifact, and persists on `record.conversions['placeholder'].meta` a tiny inline preview
* (`dataUrl`), the original's dimensions and its dominant colour. Everything a page needs to
* render a blurred LQIP with the right aspect ratio before the real image arrives, without a
* second request.
*
* ```ts
* // config/media.ts
* { name: 'avatar', acceptsMimeTypes: ['image/png', 'image/jpeg'], transformers: [transformers.placeholder()] }
*
* const record = await media.library.find(id)
* record.conversions.placeholder?.meta // { dataUrl: 'data:image/webp;base64,…', width, height, color }
* ```
*
* Eager by default. A record whose MIME type is not `image/*`, or is an SVG, is skipped (`meta: {}`) rather than
* failing the attach, so declaring it on a mixed collection (images + PDFs) is safe.
*/
export declare class PlaceholderTransformer<Name extends string = 'placeholder'> implements Transformer {
readonly name: Name;
readonly eager: boolean;
private readonly options;
private readonly engine;
constructor(options?: PlaceholderTransformerOptions<Name>);
transform(context: TransformerContext): Promise<TransformResult>;
}
/**
* Read the blur-up placeholder off a record: the first conversion whose `meta` has the
* {@link ImagePlaceholder} shape, so a renamed transformer (`name: 'lqip'`) is found too. `null`
* when the collection has no placeholder transformer, it hasn't run, or it skipped a non-image.
*
* ```ts
* const record = await media.library.find(user.avatarMediaId)
* return { avatarUrl, avatarPlaceholder: record && placeholderOf(record) }
* ```
*/
export declare function placeholderOf(record: MediaRecord): ImagePlaceholder | null;
/**
* sharp-backed {@link PlaceholderEngine}, importing the optional peer on first use.
*
* @internal `importSharp` is the seam tests use to simulate a host without the peer.
*/
export declare function loadSharpPlaceholderEngine(transformerName: string, importSharp?: () => Promise<{
default: typeof import('sharp').default;
}>): Promise<PlaceholderEngine>;
//# sourceMappingURL=placeholder.d.ts.map