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

70 lines • 3.83 kB
/** * How many leading bytes {@link detectMimeType} needs to decide. This — and never the file size — * is what the attach paths read: the whole point of `attachExisting` is not buffering the object, * so content validation must stay a short head read. * * 189 = two MPEG-TS sync bytes (offsets 0 and 188), the deepest probe in the table; every other * signature resolves within the first few dozen bytes. A file (or a TUS first chunk) shorter than * a signature's deepest offset simply cannot match that signature — short heads degrade to * "unrecognised", never to a false positive. */ export declare const SIGNATURE_HEAD_BYTES = 189; /** * Identify the real content type from the leading bytes of a file, or `undefined` when no signature * in the embedded table matches. * * `undefined` means "unknown", NOT "invalid": plenty of legitimate types (SVG, CSV, plain text, * office formats) have no fixed signature, so callers treat it as "no evidence either way" and fall * back to the declared MIME type rather than rejecting. */ export declare function detectMimeType(head: Uint8Array): string | undefined; /** Can {@link detectMimeType} ever return this type — i.e. is it in the embedded table? */ export declare function isDetectableMimeType(mimeType: string): boolean; /** * Is every type in this whitelist signature-detectable — a *closed* whitelist? * * The distinction is what makes "unrecognised signature" actionable. Under a closed whitelist * (`['application/pdf']`, `['image/png', 'image/jpeg']`) unrecognised content cannot be any accepted * type, so it is provably wrong. Under an open one (anything containing `image/svg+xml`, `text/csv`, * `text/plain`, an office format…) unrecognised is the NORMAL case for a legitimate file, so there * is no evidence either way. An empty list is not closed: it whitelists nothing to reason from. */ export declare function isClosedSignatureWhitelist(accepted: readonly string[]): boolean; /** * The outcome of checking real bytes against a collection's `acceptsMimeTypes`. Returned rather * than thrown so every caller (the two attach paths, the TUS handler) maps it onto its own failure * mode — an exception in the library, an HTTP status in the handler — off one shared decision. */ export type ContentVerdict = { outcome: 'accepted'; } /** A signature matched, and it contradicts the type the caller declared. */ | { outcome: 'mismatch'; detected: string; } /** A signature matched, and the collection does not accept that type. */ | { outcome: 'not-accepted'; detected: string; } /** No signature matched, under a closed whitelist ⇒ the content is none of the accepted types. */ | { outcome: 'unrecognized'; }; /** * Decide whether the leading bytes of a file are acceptable for a collection, given its * `acceptsMimeTypes` and (optionally) the type the caller declared. * * - a signature matches and agrees with the declared type (or none was declared and the type is * whitelisted) ⇒ `accepted`. * - a signature matches and contradicts the declared type ⇒ `mismatch`, whether or not the detected * type is itself whitelisted: a record whose `mimeType` misdescribes its bytes poisons every * downstream consumer (conversions, `Content-Type` on delivery). * - a signature matches, nothing was declared, and the type is not whitelisted ⇒ `not-accepted`. * - no signature matches ⇒ `unrecognized` under a closed whitelist (see * {@link isClosedSignatureWhitelist}), otherwise `accepted` — "unknown" is not "invalid" when a * legitimate accepted type could have no signature at all. */ export declare function verifyContentAgainstWhitelist(head: Uint8Array, accepted: readonly string[], declared?: string): ContentVerdict; //# sourceMappingURL=content_type.d.ts.map