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

127 lines • 6.28 kB
import { disks } from './disks/factory.js'; import { processors } from './processors/factory.js'; import { stores } from './stores/factory.js'; import { PARTITION_PATTERN } from './topology/protocol.js'; import { engines, transformers } from './transformers/factory.js'; import { uploadSessions } from './upload_sessions/factory.js'; /** What a tenant does not have: its files live on the control plane. */ const TENANT_FORBIDDEN = [ 'disk', 'disks', 'store', 'stores', 'imageProcessor', 'uploads', 'tenancy', 'consoleCommands', 'accountConsole', 'verifyTenant', 'controlPlane', ]; /** The client fields shared by `tenant` and `accounts`. */ function assertControlPlaneClient(key, client, options = {}) { // `partial`: a tenant's config before authkit-client filled it in; only what is set is checked. const check = (field) => !options.partial || client[field] !== undefined; if (check('partition') && (!client.partition || !PARTITION_PATTERN.test(client.partition))) { throw new Error(`media: ${key}.partition is required (1-64 characters of A-Z a-z 0-9 _ . -)`); } if (check('token') && !client.token) { throw new Error(`media: ${key}.token is required (authkitTenantToken({ issuer, clientId, clientSecret, resource: 'urn:agora:media' }) or a shared secret)`); } if (typeof client.url === 'string' && !/^https?:\/\//.test(client.url)) { throw new Error(`media: ${key}.url must be an absolute http(s) URL or a function`); } for (const field of ['timeoutMs', 'uploadTimeoutMs']) { const value = client[field]; if (value !== undefined && !(Number.isFinite(value) && value > 0)) { throw new Error(`media: ${key}.${field} must be a positive number of milliseconds`); } } if (client.retries !== undefined && !(Number.isInteger(client.retries) && client.retries >= 0)) { throw new Error(`media: ${key}.retries must be a whole number, 0 or more`); } } /** * Refuses a topology that cannot work, at boot, with the change to make. `partial`: the config as * written, before `@adonis-agora/authkit-client` supplies a tenant's `url`/`partition`/`token` * (`defineConfig`); the provider checks the filled-in config in full. */ export function assertMediaTopology(config, options = {}) { const topology = config.topology ?? 'standalone'; if (!['standalone', 'control-plane', 'tenant'].includes(topology)) { throw new Error(`media: unknown topology "${String(topology)}"`); } if (topology === 'tenant') { const tenant = config.tenant ?? {}; if (!options.partial && !tenant.url) { throw new Error('media: tenant.url is required on a tenant'); } assertControlPlaneClient('tenant', tenant, options); if (config.accounts !== undefined) { throw new Error('media: `accounts` does not apply to a tenant: its media.accounts uses `tenant`'); } if (tenant.urls !== undefined && tenant.urls !== 'app' && tenant.urls !== 'control-plane') { throw new Error("media: tenant.urls is 'app' or 'control-plane'"); } for (const key of TENANT_FORBIDDEN) { if (config[key] !== undefined) { throw new Error(`media: \`${key}\` does not apply to a tenant: its media lives on the control plane`); } } return; } if (config.tenant !== undefined) { throw new Error('media: `tenant` only applies to topology "tenant"'); } if (config.accounts !== undefined) { if (topology !== 'standalone') { throw new Error('media: `accounts` only applies to topology "standalone": a control plane keeps account files itself (`controlPlane.accounts`)'); } if (!config.accounts.url) throw new Error('media: accounts.url is required'); assertControlPlaneClient('accounts', config.accounts); } if (topology === 'control-plane') { const verify = config.verifyTenant; if (typeof verify !== 'function' && !(verify && typeof verify === 'object' && typeof verify.verify === 'function')) { throw new Error("media: a control plane needs `verifyTenant` (authkitTenantVerifier({ issuer, resource: 'urn:agora:media' }) or sharedSecretVerifier({ partitions })): a tenant is never trusted without a token"); } const prefix = config.controlPlane?.prefix; if (prefix !== undefined && !/^\/[A-Za-z0-9_.~/-]*$/.test(prefix)) { throw new Error("media: controlPlane.prefix is a path starting with '/' (like '/_media')"); } for (const [key, value] of [ ['controlPlane.maxBytes', config.controlPlane?.maxBytes], ['controlPlane.accounts.maxBytes', config.controlPlane?.accounts?.maxBytes], ]) { if (value !== undefined && !(Number.isFinite(value) && value > 0)) { throw new Error(`media: ${key} must be a positive number of bytes`); } } const accounts = config.controlPlane?.accounts; if (accounts && typeof accounts.verifyUser !== 'function') { throw new Error('media: controlPlane.accounts.verifyUser is required'); } if (accounts && (!Array.isArray(accounts.collections) || accounts.collections.length === 0)) { throw new Error('media: controlPlane.accounts.collections lists at least one collection'); } return; } if (config.verifyTenant !== undefined || config.controlPlane !== undefined) { throw new Error('media: `verifyTenant` and `controlPlane` only apply to topology "control-plane"'); } } /** * Identity helper giving `config/media.ts` full type-checking. The `const` type parameter keeps * collection/conversion/transformer names as literal types, so {@link InferConversions} / * {@link InferTransformers} can extract them from `typeof config` — no `as const` needed. */ export function defineConfig(config = {}) { assertMediaTopology(config, { partial: true }); return config; } export { disks, engines, processors, stores, transformers, uploadSessions }; //# sourceMappingURL=define_config.js.map