Skip to content

feat: universal event-driven cache invalidation - #60

Merged
klnap merged 10 commits into
mainfrom
worktree-v0-5-0-universal-ingress
Dec 6, 2026
Merged

klnap merged 10 commits into
mainfrom
worktree-v0-5-0-universal-ingress

Conversation

@klnap

@klnap klnap commented Dec 6, 2026

Copy link
Copy Markdown
Owner

Summary

Version 0.5.0 makes @klnap/cache-invalidation a framework-agnostic, standards-based, multi-target cache invalidation library with first-class Payload CMS and Next.js integrations. The release is carried by .changeset/extensible-webhook-ingestion.md (minor).

  • Architecture. Five entrypoints: ., /producers/payload, /webhooks, /consumers, /consumers/next. Source, transport and cache destination are independent and joined only by CloudEvents; module boundaries are enforced by dependency-cruiser.
  • Universal webhook ingress. Exactly one of { secret }, { secret, decode } or { adapter }. Decoder and adapter output is untrusted: shape-checked, copied as plain data, bounded by maxEvents, maxNormalizedBytes and adapterTimeoutMs, and validated as CloudEvents.
  • Generic consumer. createCacheConsumer() with application-owned executors and direct consume(events); the Next.js handler is now the native consumer behind the shared receiver (createNextCacheConsumer()).
  • Payload. webhooks configuration, collections: true / globals: true, absolute exclude, additive excludeFields, one effective configuration for hooks, media discovery and routing validation.
  • Routing. Per-target model filters (including media through affected) and functional predicates; a failing predicate is a target failure, not a skip.
  • Security and correctness. No exception messages in library logs or responses; consumer limits apply to native batches; de-duplication distinguishes binary from JSON data; media exclusion and disablement are no longer overridden; centrally enabled globals are discovered as media dependents.
  • Documentation. One restructured README with Contentful and Sanity adapters tested against signatures from the providers' official SDKs (development dependencies only); migration notes; performance methodology.

Intentional breaking changes (pre-1.0)

/dispatchers, /dispatchers/http, /consumers/http → /webhooks; http() → createWebhookDispatcher(); { config: cache.config } → { cache }; DefineCacheInput → CacheConfig; Payload hooks removed (setting it throws); excludeFields lists combine instead of replacing; resolveExcludeFields and readCacheInvalidationExcludeFields removed; Payload requires exactly one of webhooks / dispatcher; Payload log lines no longer carry exception messages. The event protocol is unchanged, so 0.4 and 0.5 applications interoperate.

Verification (head dbef5e8)

  • bun run check: pass (435 unit tests, type tests including augmented Payload types, knip, boundaries, publint, attw).
  • Real Payload (SQLite) integration: 24/24, including three real HTTP receivers, shared-collection routing, exclusions and media.
  • PostgreSQL 17 integration: 12/12 (transaction commit, rollback, deferral, localization). Run on the final src; no source change since.
  • Packed 0.5.0 candidate (produced with Changesets in an isolated copy): installed-package smoke, optional peer isolation, framework-free consumer, native, decoder and adapter ingress, and a production Next.js build, with unauthorized requests leaving the cache untouched.
  • Receiver benchmark against 0.4.3 (method and samples in PERFORMANCE.md): native callback 33.5 → 34.2 µs/op, Next.js handler 35.4 → 38.7 µs/op.
  • Signed-body integrity: the provider adapters decode with ignoreBOM, and regression tests show a BOM added after signing is rejected for Contentful, Sanity and the native receiver.

Accepted limitations

No hosted Sanity or Contentful delivery was used; the adapters are verified with the official SDKs and signed fixtures, and the README lists the payload assumptions to confirm.

klnap added 10 commits December 6, 2026 15:26
…t consumer

- Move the HTTP dispatcher and receiver into one /webhooks entrypoint and
  rename http() to createWebhookDispatcher().
- Add /consumers with createCacheConsumer() and a shared operation runner.
- Add createNextCacheConsumer(); createCacheInvalidationHandler() now takes
  the cache definition directly and composes the shared ingress.
- Receiver supports { secret }, { secret, decode } and { adapter } with
  bounded output validation, normalized size limits and adapter timeouts.
- Fix de-duplication treating Uint8Array([1]) and {"0":1} as identical.
…additive excludeFields

- cacheInvalidation() accepts webhooks (native dispatcher) or dispatcher.
- Per-target model filters (collections/globals) or a functional predicate,
  validated against the effective configuration; affected models route too.
- exclude removes models from every path; one effective configuration now
  drives hooks, media dependency discovery and routing validation.
- excludeFields from global, central, local and media levels are combined.
- Remove the public hooks option; the plugin installs every lifecycle hook.
- Media: centrally enabled globals are discovered, media:false is supported,
  explicit disablement and exclusion are no longer overridden, and the media
  collection slug is no longer hard-coded in locale resolution.
- Dispatcher targets accept a synchronous event predicate; a failing predicate
  is a target failure, not a skip.
- Real-HTTP multi-target and shared-collection routing integration tests.
- Next.js production fixture exercises adapter and decoder modes.
- Type tests for ingress, consumer and Payload configuration contracts.
- Fix a native-path regression: canonicalize events only on identity
  collisions and mark validated batches only when handing them to a consumer.
- Export CacheForbiddenError; update README, AGENTS, SECURITY, PERFORMANCE
  and add the release changeset.
…ization depth

- Native batches are no longer marked as normalized, so a consumer
  re-validates them under its own maxNormalizedBytes.
- Duplicate-identity comparison rejects deeply nested data instead of
  overflowing the stack.
- Move the Payload and Next.js example apps to the 0.5 API.
…apters against official SDKs

- Payload logs keep the error class and a short machine code instead of the
  exception message, and dispatcher failures log codes only.
- Add regression tests proving secrets, signatures, bodies and exception
  messages cannot reach receiver, consumer or Payload logs.
- Add reference Contentful and Sanity adapters tested with signatures made by
  @contentful/node-apps-toolkit and @sanity/webhook (dev dependencies only).
- Correct the README examples: Contentful signs the query string and accepts
  future timestamps, Sanity has no freshness check, and unsigned headers must
  not provide identity or source.
- Typecheck against augmented Payload types (tsconfig.types-generated.json).
- Integration tests: combined plugin/central/local excludeFields, excluded
  models never emit or appear in affected; Resource Change V1 for
  non-Payload event types; framework-free consumer in the installed smoke.
- Measure plain native batches for a consumer's normalized-size limit instead
  of copying them, restoring the Next.js handler path to about +9%.
- Record the benchmark method and raw samples; add a receiver benchmark.
- Document how README examples are classified.
…uting examples

- README reorganized (overview, generic producer, webhooks, consumers, CMS
  examples, security and reliability); architecture diagram corrected;
  unauthenticated adapter example removed; adapter contract shown as a type.
- Neutral custom-JSON decoder example; Sanity adapter works from before() and
  after() snapshots with an operation-qualified identity; Contentful identity
  prefers a signed idempotency key. Fixtures and tests updated to match.
- Robust shared-collection routing predicate, tested for create, update, move,
  delete, unpublish and media with real Payload.
- AGENTS.md condensed; PERFORMANCE.md leads with current results.
- SECURITY.md covers documentation examples; changeset extended.
…der selection

- Decode request bodies with ignoreBOM so a leading BOM cannot be hidden
  from signature verification (Contentful, Sanity examples and fixtures).
- Contentful signed-header list is read exactly as the SDK reads it.
- Regression tests for BOM tampering (Contentful, Sanity, native receiver).
- README: decoder/adapter authentication wording, media vs ordinary opt-in,
  handler maxNormalizedBytes relationship.
@klnap
klnap merged commit f328658 into main Dec 6, 2026
4 checks passed
@klnap
klnap deleted the worktree-v0-5-0-universal-ingress branch December 6, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant