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

81 lines • 2.92 kB
import { Readable } from 'node:stream'; /** * Coerce attach/create inputs (a `Buffer` or a `Readable`) to a `Uint8Array` for the disk's `put`, * which (matching flydrive) does not accept a stream. A `Buffer` is already a `Uint8Array`. */ export async function toBytes(contents) { if (Buffer.isBuffer(contents)) return contents; const chunks = []; // `Buffer.concat` copies once; wrapping a chunk that already is a Buffer would copy it twice. for await (const chunk of contents) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); return Buffer.concat(chunks); } /** Read back disk bytes as a Node `Buffer` for the image processor (which works on Buffers). */ export function asBuffer(bytes) { return Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes); } /** * Read the first `length` bytes of an attach payload WITHOUT consuming it, so the caller can sniff * its real content type and still write every byte. * * A `Readable` cannot be rewound, so the head is re-emitted in front of the rest through a fresh * stream driven by the SAME async iterator — no buffering of the full payload, which is what keeps * the streaming attach path (large uploads, no conversions) streaming. */ export async function peekContents(contents, length) { if (Buffer.isBuffer(contents)) { return { head: contents.subarray(0, length), contents }; } const iterator = contents[Symbol.asyncIterator](); const chunks = []; let read = 0; let done = false; while (read < length) { const next = await iterator.next(); if (next.done) { done = true; break; } const chunk = Buffer.from(next.value); chunks.push(chunk); read += chunk.byteLength; } const buffered = Buffer.concat(chunks); const replayed = Readable.from((async function* () { if (buffered.byteLength > 0) yield buffered; if (done) return; while (true) { const next = await iterator.next(); if (next.done) return; yield next.value; } })()); return { head: buffered.subarray(0, length), contents: replayed }; } /** * Read the first `length` bytes of an object already on a disk and stop — the stream is destroyed * as soon as enough bytes are in hand, so adopting a 2 GiB scan in place costs one short ranged * read rather than a download. */ export async function peekStream(stream, length) { const chunks = []; let read = 0; try { for await (const chunk of stream) { chunks.push(Buffer.from(chunk)); read += chunk.length; if (read >= length) break; } } finally { stream.destroy(); } return Buffer.concat(chunks).subarray(0, length); } //# sourceMappingURL=contents.js.map