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

110 lines • 4.66 kB
/** * The `@adonis-agora/diagnostics` emit capability, published on this global slot at that * package's module load. `@adonis-agora/media` reads it STRUCTURALLY — it never imports or * depends on the diagnostics package. When diagnostics isn't installed the slot is empty * and emitting is an inert no-op. */ const EMIT_SLOT = Symbol.for('@agora/diagnostics:emit'); /** * The `@adonis-agora/diagnostics` trace capability, published on this global slot. Like * {@link EMIT_SLOT} it is read STRUCTURALLY — media never imports the diagnostics package. * A trace slot wraps a unit of work so an observer can time/inspect it; when no slot is * installed the wrapped function simply runs and its result (or error) passes through. */ const TRACE_SLOT = Symbol.for('@agora/diagnostics:trace'); /** * Every media milestone published on `agora:media:<event>`. The single runtime source for the * {@link MediaDiagnosticEvent} union — a Telescope watcher iterates this to subscribe/claim. */ export const MEDIA_DIAGNOSTIC_EVENTS = [ 'attach', 'delete', 'conversion', 'attachment.create', 'attachment.delete', 'upload.start', 'upload.progress', 'upload.complete', 'upload.abort', ]; /** * The claim registry, published by `@adonis-agora/diagnostics` under this global slot as a * reference-counted `Map<`${lib}:${event}`, number>`. Read STRUCTURALLY — same decoupling as * {@link EMIT_SLOT}: media never imports the diagnostics package. A lib-specific Telescope watcher * claims the channels it records here so the generic `DiagnosticsWatcher` skips them (its * `recordClaimed: false` default), avoiding double-recording. The raw convention is documented by * `@adonis-agora/diagnostics`' `claims.ts` precisely so a non-dependent package can participate. */ const CLAIMS_SLOT = Symbol.for('@agora/diagnostics:claims'); /** The shared claim registry, creating the slot Map on first claim (converges with the diagnostics package's own `globalSlot`, since a `Symbol.for` slot holds one instance). */ function claimsRegistry() { const g = globalThis; let registry = g[CLAIMS_SLOT]; if (registry === undefined) { registry = new Map(); g[CLAIMS_SLOT] = registry; } return registry; } /** * Claim `media:<event>` for every event, so the generic diagnostics→telescope bridge skips them. * Reference-counted (mirrors `@adonis-agora/diagnostics`' `claimDiagnostics`): claiming an already * claimed key increments its count; the returned release decrements, deleting the key only at zero. * Idempotent release. */ export function claimMediaDiagnostics(events) { const registry = claimsRegistry(); const keys = events.map((event) => `media:${event}`); for (const key of keys) registry.set(key, (registry.get(key) ?? 0) + 1); let released = false; return () => { if (released) return; released = true; for (const key of keys) { const count = registry.get(key); if (count === undefined) continue; if (count <= 1) registry.delete(key); else registry.set(key, count - 1); } }; } /** Whether `media:<event>` is currently claimed — the structural mirror of the diagnostics package's `isDiagnosticClaimed('media', event)`. */ export function isMediaDiagnosticClaimed(event) { const registry = globalThis[CLAIMS_SLOT]; return registry?.has(`media:${event}`) ?? false; } /** * Publish a media event on `agora:media:<event>` via the structural diagnostics slot. * No-op when diagnostics isn't installed (the slot is empty) — and it never throws back * into the library. */ export function publishMedia(event, payload) { const emit = globalThis[EMIT_SLOT]; if (typeof emit === 'function') { try { emit('media', event, payload); } catch { // diagnostics must never break a media operation } } } /** * Wrap a unit of media work in a structural trace span (`upload.policy.*` and friends). When a * diagnostics trace slot is installed it observes the call (lib hard-coded to `'media'`); otherwise * the wrapped function runs directly. Unlike {@link publishMedia}, errors are NOT swallowed — a * traced policy hook must surface its failure to the caller so the handler can map it. */ export function traceMedia(event, fn, payload) { const trace = globalThis[TRACE_SLOT]; if (typeof trace === 'function') { return trace('media', event, fn, payload); } return fn(); } //# sourceMappingURL=diagnostics.js.map