@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
59 lines • 2.56 kB
JavaScript
import { TransformerConflictError } from './errors.js';
import { PlaceholderTransformer } from './transformers/placeholder.js';
/** Widths a `responsive: true` collection generates (those below the original's width). */
export const DEFAULT_RESPONSIVE_WIDTHS = [320, 640, 960, 1280, 1920, 2560];
export class MediaCollectionRegistry {
map = new Map();
constructor(collections = []) {
for (const config of collections) {
const c = withResponsive(config);
assertDistinctDerivativeNames(c);
this.map.set(c.name, c);
}
}
/** Registered config for a collection, or a permissive default. */
get(name) {
return this.map.get(name) ?? { name };
}
}
/**
* Presets and transformers both persist under `record.conversions[name]`, so within one collection
* every derivative name must be unique across BOTH lists. Checked eagerly at registry construction
* — a collision is a config bug, and failing at boot beats failing on the first attach.
*/
function assertDistinctDerivativeNames(collection) {
const seen = new Set();
for (const preset of collection.conversions ?? []) {
if (seen.has(preset.name))
throw new TransformerConflictError(collection.name, preset.name);
seen.add(preset.name);
}
for (const transformer of collection.transformers ?? []) {
if (seen.has(transformer.name)) {
throw new TransformerConflictError(collection.name, transformer.name);
}
seen.add(transformer.name);
}
}
/** Expand `responsive` into its presets (and the placeholder, when missing). */
function withResponsive(collection) {
if (!collection.responsive)
return collection;
const options = collection.responsive === true ? {} : collection.responsive;
const presets = [...(options.widths ?? DEFAULT_RESPONSIVE_WIDTHS)].map((width) => ({
name: `responsive-${width}`,
width,
eager: true,
onlyDownscale: true,
...(options.format ? { format: options.format } : {}),
...(options.quality !== undefined ? { quality: options.quality } : {}),
}));
const transformers = collection.transformers ?? [];
const hasPlaceholder = transformers.some((t) => t instanceof PlaceholderTransformer);
return {
...collection,
conversions: [...(collection.conversions ?? []), ...presets],
transformers: hasPlaceholder ? transformers : [...transformers, new PlaceholderTransformer()],
};
}
//# sourceMappingURL=media_collection.js.map