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

148 lines • 5.87 kB
/** * Structural reader for the tenancy contract `@adonis-agora/context` publishes on its global slots. * Never imports that package: with the slot missing, too old, or throwing, tenancy reads as OFF, so * media behaves exactly as it does without tenancy. * * The tenant here is the customer a piece of data belongs to (an organization, a clinic…), not a * process topology. */ const ACCESSOR = Symbol.for('@agora/context:accessor'); const TENANCY = Symbol.for('@agora/context:tenancy'); /** Tenancy off: nothing is stamped, filtered or prefixed. */ export const TENANCY_OFF = Object.freeze({ enabled: false, nullRows: 'tenantless', pathPrefix: false, }); function slot(key) { const value = globalThis[key]; return value !== null && typeof value === 'object' ? value : undefined; } /** * The tenancy reader: context's versioned `:tenancy` slot (v1) when present, else the `:accessor` * slot's tenancy members (an earlier context), else nothing. */ function reader() { const tenancy = slot(TENANCY); if (tenancy?.version === 1 && typeof tenancy.tenantFilter === 'function') return tenancy; const accessor = slot(ACCESSOR); return typeof accessor?.tenantFilter === 'function' ? accessor : undefined; } /** Resolve media's `tenancy` option. With no (or an old) context, tenancy is off. */ export function resolveMediaTenancy(option) { if (option === false) return TENANCY_OFF; const accessor = reader(); if (accessor === undefined) return TENANCY_OFF; const own = typeof option === 'object' ? option : {}; let context; try { context = accessor.tenancy?.(); } catch { context = undefined; } const enabled = own.enabled === undefined || own.enabled === 'inherit' ? context?.enabled === true : own.enabled; if (!enabled) return TENANCY_OFF; const kinds = own.kinds ?? context?.kinds; return { enabled: true, ...(kinds !== undefined ? { kinds: [...kinds] } : {}), nullRows: own.nullRows ?? 'tenantless', pathPrefix: own.pathPrefix !== false, }; } /** * How the current request (or job) must be scoped. Tenancy off → `system` (nothing filtered). A * tenant of a kind media does not honour reads as `tenantless`. A context configured with * `required: true` throws here when there is no tenant. */ export function currentTenantFilter(tenancy) { if (!tenancy.enabled) return { mode: 'system' }; const filter = reader()?.tenantFilter?.(tenancy.kinds !== undefined ? { kinds: tenancy.kinds } : undefined); if (filter === undefined || filter === null || typeof filter !== 'object') { return { mode: 'system' }; } if (filter.mode === 'tenant') { const kind = filter.tenant?.kind; if (tenancy.kinds !== undefined && !tenancy.kinds.includes(kind)) return { mode: 'tenantless' }; } return filter; } /** The tenant id to stamp on a write right now: the tenant's id in `tenant` mode, else `null`. */ export function currentTenantId(tenancy) { const filter = currentTenantFilter(tenancy); return filter.mode === 'tenant' ? filter.tenant.id : null; } /** The {@link TenantScope} of a filter, or `undefined` for "no filtering" (`system` / `all`). */ export function scopeOfFilter(filter, nullRows = 'tenantless') { switch (filter.mode) { case 'system': case 'all': return undefined; case 'tenantless': return { tenants: [], includeNull: true }; case 'tenant': return { tenants: [filter.tenant.id], includeNull: nullRows === 'shared' }; } } /** The scope reads must apply right now (`undefined` = unfiltered). */ export function currentTenantScope(tenancy) { if (!tenancy.enabled) return undefined; return scopeOfFilter(currentTenantFilter(tenancy), tenancy.nullRows); } /** Whether a row stamped `tenantId` is inside `scope` (`undefined` scope = everything). */ export function inScope(scope, tenantId) { if (scope === undefined) return true; const id = tenantId ?? null; return id === null ? scope.includeNull : scope.tenants.includes(id); } /** The scope as a list of tenant ids with `null` standing for "no tenant" (`undefined` = all). */ export function scopeIds(scope) { if (scope === undefined) return undefined; return scope.includeNull ? [...scope.tenants, null] : [...scope.tenants]; } /** Run `fn` inside `tenant` (or no tenant), through context; plain `fn()` without context. */ export function withTenant(tenant, fn) { const withTenantFn = slot(TENANCY)?.withTenant; return typeof withTenantFn === 'function' ? withTenantFn(tenant, fn) : fn(); } /** Run `fn` across every tenant (context's `allTenants`); plain `fn()` without context. */ export function allTenants(fn) { const all = slot(TENANCY)?.allTenants; return typeof all === 'function' ? all(fn) : fn(); } /** Display labels for tenant ids, from context's label resolver; ids map to themselves otherwise. */ export async function tenantLabels(ids, kind) { const labels = Object.fromEntries(ids.map((id) => [id, id])); const tenancy = slot(TENANCY); if (tenancy?.version !== 1 || typeof tenancy.tenantLabels !== 'function') return labels; try { const resolved = await tenancy.tenantLabels(ids, kind); for (const id of ids) { const label = resolved?.[id]; if (typeof label === 'string' && label !== '') labels[id] = label; } } catch { // labels are display only } return labels; } /** The storage prefix of a tenant's new files: `t/<tenantId>/`. */ export function tenantPathPrefix(tenantId) { return `t/${encodeURIComponent(tenantId)}/`; } //# sourceMappingURL=tenancy.js.map