UNPKG

@nullplatform/ingest

Version:

Client SDK for the nullplatform ingest API: push snapshots and delta events from a collector into the catalog and other targets, follow runs, replay and drift checks

181 lines (180 loc) • 5.76 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_types = require("./types-BKzpjXNm.cjs"); //#region src/testing/in-memory-ingest.ts const NO_COUNTERS = { received: 0, filtered: 0, created: 0, updated: 0, unchanged: 0, deleted: 0, skipped: 0, dropped: 0, rejected: 0, failed: 0 }; const destinationKey = (recordType, target, endpoint) => `${recordType}:${target}:${endpoint}`; const iso = (value) => { if (value === void 0) return; return value instanceof Date ? value.toISOString() : value; }; /** * An `IngestPort` that records everything and never networks: what a test or * a local run feeds. Sources, mappings, destinations, runs and events are * kept in public fields to assert on. * @example * const ingest = new InMemoryIngest(); * await runSnapshot(collector, ingest, { source: { slug: 'test' }, log: console }); * ingest.runs[0].records; // what the snapshot sent */ var InMemoryIngest = class { /** By slug, each with the id this port assigned it (a UUIDv7, standing in for the API's). */ sources = /* @__PURE__ */ new Map(); /** By source id, then record type: each mapping's definition. */ mappings = /* @__PURE__ */ new Map(); /** By source id, then record type, target and endpoint: where each mapping is written. */ destinations = /* @__PURE__ */ new Map(); runs = []; emitted = []; #options; constructor(options = {}) { this.#options = options; } async upsertSource(source) { const id = this.sources.get(source.slug)?.id ?? require_types.uuidv7(); this.sources.set(source.slug, { ...source, id }); this.#tell(`source ${source.slug} (${source.type}): ${id}`); return { id, slug: source.slug }; } /** * Like the API's upserts: a mapping's definition is kept per record type; a * destination per record type, target and endpoint, so another endpoint is * another destination beside the first (a record type may go to several). */ async upsertMappings(sourceId, mappings) { const definitions = { ...this.mappings.get(sourceId) }; const destinations = { ...this.destinations.get(sourceId) }; const changed = Object.entries(mappings).filter(([recordType, mapping]) => { const definition = { engine: mapping.engine, expression: mapping.expression }; const target = mapping.target ?? "catalog"; const key = destinationKey(recordType, target, mapping.endpoint); const existing = destinations[key]; const enabled = mapping.enabled ?? existing?.enabled ?? true; const definitionChanged = JSON.stringify(definitions[recordType]) !== JSON.stringify(definition); const destinationChanged = !existing || existing.enabled !== enabled; definitions[recordType] = definition; destinations[key] = { recordType, target, endpoint: mapping.endpoint, enabled }; return definitionChanged || destinationChanged; }); this.mappings.set(sourceId, definitions); this.destinations.set(sourceId, destinations); return changed.map(([recordType]) => recordType); } async appliedTypes(sourceId, types) { const own = Object.values(this.destinations.get(sourceId) ?? {}).filter((destination) => destination.enabled).map((destination) => destination.recordType); const bound = this.#options.applied ?? types; return types.filter((type) => own.includes(type) || bound.includes(type)); } /** The source's newest run among `runs`, of any status: the latest opened (the last on a tie). */ async lastRun(sourceId) { const latest = this.runs.filter((run) => run.sourceId === sourceId).reduce((newest, run) => !newest || Date.parse(run.createdAt) >= Date.parse(newest.createdAt) ? run : newest, null); return latest ? { id: latest.id, createdAt: latest.createdAt } : null; } async openRun(sourceId, types) { this.runs.filter((open) => open.sourceId === sourceId && open.status === "open").forEach((open) => { Object.assign(open, { status: "aborted" }); }); const run = { id: `run-${this.runs.length + 1}`, sourceId, types, records: [], status: "open", flushes: [], createdAt: (/* @__PURE__ */ new Date()).toISOString() }; this.runs.push(run); this.#tell(`run ${run.id} opened (${types.join(", ")})`); return { id: run.id, add: (record) => { run.records.push({ ...record, observedAt: iso(record.observedAt) }); }, flush: async () => { run.flushes.push(run.records.length); }, finalize: async (options = {}) => { if (!options.dryRun) run.status = "completed"; run.finalizeOptions = options; this.#tell(`run ${run.id} ${options.dryRun ? "assessed (dry run)" : "finalized"}: ${run.records.length} record(s), nothing sent (in-memory ingest)`); return { runId: run.id, status: run.status, records: run.records.length, counters: { ...NO_COUNTERS, received: run.records.length }, reconciliation: { seen: run.records.length, stale: 0, deleted: 0, guardTripped: false, guardReason: null, dryRun: options.dryRun === true } }; }, abort: async () => { run.status = "aborted"; this.#tell(`run ${run.id} aborted`); } }; } events(sourceId) { const push = (op) => (type, payload, options = {}) => { this.emitted.push({ sourceId, op, record: { type, payload, id: options.id, observedAt: iso(options.observedAt) ?? (/* @__PURE__ */ new Date()).toISOString() } }); }; return { upsert: push("upsert"), delete: push("delete"), flush: async () => void 0, close: async () => void 0 }; } #tell(line) { this.#options.log?.info(`[ingest:memory] ${line}`); } }; //#endregion exports.InMemoryIngest = InMemoryIngest; //# sourceMappingURL=testing.cjs.map