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

106 lines • 3.76 kB
import { Readable } from 'node:stream'; /** * In-memory {@link Disk} satisfying the same structural contract as an `@adonisjs/drive` disk. * Use it (via {@link inMemoryDiskResolver}) to drive the media-library and attachment layers in * tests without a real filesystem or S3. */ export class InMemoryDisk { baseUrl; files = new Map(); /** Options from the most recent `getSignedUrl` call, so tests can assert the contract. */ lastSignedUrlOptions; /** * Reports the object visibility `delivery.mode: 'auto'` branches on. Defined as an own property * rather than a prototype method so `'unknown'` genuinely REMOVES it — that is the "disk that * cannot answer" case, which `resolveDeliveryMode` detects by probing for the method. */ getVisibility; constructor( /** Used to render `getUrl`/`getSignedUrl`. */ baseUrl = 'memory://disk', /** What `getVisibility` reports; `'unknown'` omits the method entirely. */ visibility = 'private') { this.baseUrl = baseUrl; if (visibility !== 'unknown') this.getVisibility = async () => visibility; } async put(key, contents, options) { this.files.set(key, { data: Buffer.from(contents), contentType: options?.contentType, lastModified: new Date(), }); } async putStream(key, contents, options) { const chunks = []; for await (const chunk of contents) chunks.push(Buffer.from(chunk)); this.files.set(key, { data: Buffer.concat(chunks), contentType: options?.contentType, lastModified: new Date(), contentLength: options?.contentLength, }); } async getBytes(key) { const file = this.require(key); return Buffer.from(file.data); } async getStream(key) { return Readable.from(this.require(key).data); } async exists(key) { return this.files.has(key); } async delete(key) { this.files.delete(key); } async getUrl(key) { return `${this.baseUrl}/${key}`; } async getSignedUrl(key, options) { this.lastSignedUrlOptions = options; const expires = options?.expiresIn ?? ''; return `${this.baseUrl}/${key}?signature=fake&expires=${expires}`; } async getMetaData(key) { const file = this.require(key); return { contentLength: file.data.byteLength, contentType: file.contentType, lastModified: file.lastModified, }; } require(key) { const file = this.files.get(key); if (!file) throw new Error(`InMemoryDisk: file not found: ${key}`); return file; } } /** * Build a {@link DiskResolver} over named in-memory disks. The first name is the default; a single * unnamed disk is named `default`. Returns the resolver plus the disk map for assertions. * * ```ts * const { resolve, disks } = inMemoryDiskResolver(['fs']) * const storage = new StorageManager({ default: 'fs', resolve }) * ``` */ export function inMemoryDiskResolver(names = ['default'], /** Visibility every disk reports; `'unknown'` builds disks with no `getVisibility` at all. */ visibility = 'private') { const disks = {}; for (const name of names) disks[name] = new InMemoryDisk(`memory://${name}`, visibility); const fallback = names[0] ?? 'default'; const resolve = (name) => { const key = name ?? fallback; const disk = disks[key]; if (!disk) throw new Error(`InMemoryDisk resolver: unknown disk "${key}"`); return disk; }; return { resolve, disks }; } //# sourceMappingURL=in_memory_disk.js.map