UNPKG

@dudousxd/nestjs-diagnostics

Version:

A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.

119 lines • 5.63 kB
import { createDiagnosticsRedisRelay } from '../redis-relay.js'; export const DURABLE_DELIVERY_MODULE = '@dudousxd/nestjs-durable-diagnostics/delivery'; const DURABLE_PACKAGE = '@dudousxd/nestjs-durable-diagnostics'; export const DURABLE_NOT_INSTALLED = `\`transports.durable()\` requires ${DURABLE_PACKAGE}. Install it and import DurableModule in your app, or pick another transport.`; export const DURABLE_OUTDATED = `\`transports.durable()\` requires a version of ${DURABLE_PACKAGE} that exports \`${DURABLE_DELIVERY_MODULE}\`. Upgrade it, or pick another transport.`; export const DURABLE_NOT_CONFIGURED = '`transports.durable()` requires durable to be configured: no durable engine resolves in this app. Import DurableModule, or pick another transport.'; export const BULLMQ_NOT_INSTALLED = '`transports.queue()` requires bullmq. Install it (`pnpm add bullmq`), or pick another transport.'; const importDurable = () => import(/* @vite-ignore */ DURABLE_DELIVERY_MODULE); const importBullmq = () => import(/* @vite-ignore */ 'bullmq'); const loaders = { durable: importDurable, bullmq: importBullmq }; /** Replace how optional modules are imported. Internal, for tests. */ export function setTransportModuleLoader(name, loader) { if (name === 'durable') { loaders.durable = loader ?? importDurable; } else { loaders.bullmq = loader ?? importBullmq; } } /** `'missing'` when `pkg` is not installed, `'outdated'` when it lacks the subpath, else `null`. */ function classifyImportError(error, pkg) { const { code, message } = (error ?? {}); const text = typeof message === 'string' ? message : ''; if (!text.includes(pkg)) return null; if (code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') return 'outdated'; if (code === 'ERR_MODULE_NOT_FOUND' || code === 'MODULE_NOT_FOUND') return 'missing'; return null; } const kinds = new WeakMap(); function tagKind(kind, provider) { kinds.set(provider, kind); return provider; } /** Which built-in factory made `provider`, or `undefined` for a custom one. */ export function transportKind(provider) { return typeof provider === 'function' ? kinds.get(provider) : undefined; } export const transports = { /** * Broadcast over Redis pub/sub, best-effort: listeners run in every process the event reaches. * Events of libs with listeners are forwarded automatically; `libs`/`channels`/`all` forward more. */ redis(config) { return tagKind('redis', async (ctx) => { const libs = [...new Set([...(config.libs ?? []), ...ctx.listenerLibs])]; const receive = ctx.role !== 'api'; const stop = createDiagnosticsRedisRelay({ pub: config.pub, ...(receive ? (config.sub !== undefined ? { sub: config.sub } : {}) : { receive: false }), libs, ...(config.channels !== undefined ? { channels: config.channels } : {}), ...(config.all !== undefined ? { all: config.all } : {}), ...(config.redisChannel !== undefined ? { redisChannel: config.redisChannel } : {}), nodeId: ctx.nodeId, }); return { stop }; }); }, /** * Run listeners through BullMQ: one job per `(listener, event)`, retried when the handler throws * (at-least-once). Every process that is not `role: 'api'` runs a worker for the queue. * Requires the optional peer `bullmq`. */ queue(config) { return tagKind('queue', async (ctx) => { let bullmq; try { bullmq = await loaders.bullmq(); } catch (error) { if (classifyImportError(error, 'bullmq') === 'missing') { throw new Error(BULLMQ_NOT_INSTALLED, { cause: error }); } throw error; } const { createQueueTransport } = await import('./queue-transport.js'); return createQueueTransport(bullmq, config, ctx); }); }, /** * Run listeners as durable runs: one run per `(listener, event)`, retried with backoff and * idempotent per event id. Imports `@dudousxd/nestjs-durable-diagnostics/delivery` only when this * transport is the selected `default`; bootstrap fails with a clear error when it is missing or * durable is not configured. */ durable(config = {}) { return tagKind('durable', async (ctx) => { let durable; try { durable = await loaders.durable(); } catch (error) { const kind = classifyImportError(error, DURABLE_PACKAGE); if (kind === 'missing') throw new Error(DURABLE_NOT_INSTALLED, { cause: error }); if (kind === 'outdated') throw new Error(DURABLE_OUTDATED, { cause: error }); throw error; } if (typeof durable?.isConfigured !== 'function' || typeof durable.createDeliveryTransport !== 'function') { throw new Error(DURABLE_OUTDATED); } if (!(await durable.isConfigured(ctx.moduleRef))) throw new Error(DURABLE_NOT_CONFIGURED); return durable.createDeliveryTransport({ moduleRef: ctx.moduleRef, nodeId: ctx.nodeId, role: ctx.role, invoke: ctx.invoke, listenerIds: ctx.listenerIds, }, config); }); }, }; //# sourceMappingURL=transports.js.map