@klnap/cache-invalidation 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +139 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/dist/consumers/next/compile-tag-template.d.ts +16 -0
- package/dist/consumers/next/compile-tag-template.js +83 -0
- package/dist/consumers/next/consumer-types.d.ts +98 -0
- package/dist/consumers/next/consumer-types.js +3 -0
- package/dist/consumers/next/define-cache.d.ts +4 -0
- package/dist/consumers/next/define-cache.js +281 -0
- package/dist/consumers/next/host-types.d.ts +44 -0
- package/dist/consumers/next/host-types.js +3 -0
- package/dist/consumers/next/index.d.ts +5 -0
- package/dist/consumers/next/index.js +2 -0
- package/dist/consumers/next/param-values.d.ts +8 -0
- package/dist/consumers/next/param-values.js +23 -0
- package/dist/consumers/next/profile.d.ts +2 -0
- package/dist/consumers/next/profile.js +18 -0
- package/dist/consumers/next/registry-builders.d.ts +3 -0
- package/dist/consumers/next/registry-builders.js +155 -0
- package/dist/consumers/next/revalidation-handler.d.ts +6 -0
- package/dist/consumers/next/revalidation-handler.js +2 -0
- package/dist/consumers/next/runtime.d.ts +5 -0
- package/dist/consumers/next/runtime.js +560 -0
- package/dist/core/cloudevents.d.ts +27 -0
- package/dist/core/cloudevents.js +172 -0
- package/dist/core/dispatch-outcome.d.ts +4 -0
- package/dist/core/dispatch-outcome.js +17 -0
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/errors.js +28 -0
- package/dist/core/event-limits.d.ts +15 -0
- package/dist/core/event-limits.js +20 -0
- package/dist/core/event-selector.d.ts +9 -0
- package/dist/core/event-selector.js +5 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +6 -0
- package/dist/core/resource-changed-schema.d.ts +35 -0
- package/dist/core/resource-changed-schema.js +12 -0
- package/dist/core/standard-schema.d.ts +2 -0
- package/dist/core/standard-schema.js +7 -0
- package/dist/core/types.d.ts +35 -0
- package/dist/core/types.js +1 -0
- package/dist/core/url-segment.d.ts +2 -0
- package/dist/core/url-segment.js +17 -0
- package/dist/dispatchers/http/index.d.ts +20 -0
- package/dist/dispatchers/http/index.js +274 -0
- package/dist/dispatchers/http/targets.d.ts +14 -0
- package/dist/dispatchers/http/targets.js +26 -0
- package/dist/dispatchers/index.d.ts +1 -0
- package/dist/dispatchers/index.js +1 -0
- package/dist/producers/payload/exclude-fields.d.ts +19 -0
- package/dist/producers/payload/exclude-fields.js +75 -0
- package/dist/producers/payload/index.d.ts +4 -0
- package/dist/producers/payload/index.js +3 -0
- package/dist/producers/payload/lifecycle/deferred.d.ts +55 -0
- package/dist/producers/payload/lifecycle/deferred.js +111 -0
- package/dist/producers/payload/lifecycle/dispatch.d.ts +18 -0
- package/dist/producers/payload/lifecycle/dispatch.js +21 -0
- package/dist/producers/payload/lifecycle/doc-content.d.ts +8 -0
- package/dist/producers/payload/lifecycle/doc-content.js +88 -0
- package/dist/producers/payload/lifecycle/draft-status.d.ts +6 -0
- package/dist/producers/payload/lifecycle/draft-status.js +15 -0
- package/dist/producers/payload/lifecycle/helpers.d.ts +13 -0
- package/dist/producers/payload/lifecycle/helpers.js +12 -0
- package/dist/producers/payload/lifecycle/identifier-field.d.ts +8 -0
- package/dist/producers/payload/lifecycle/identifier-field.js +48 -0
- package/dist/producers/payload/lifecycle/identifiers.d.ts +4 -0
- package/dist/producers/payload/lifecycle/identifiers.js +5 -0
- package/dist/producers/payload/lifecycle/locale-published.d.ts +7 -0
- package/dist/producers/payload/lifecycle/locale-published.js +19 -0
- package/dist/producers/payload/lifecycle/locale.d.ts +24 -0
- package/dist/producers/payload/lifecycle/locale.js +44 -0
- package/dist/producers/payload/lifecycle/log.d.ts +31 -0
- package/dist/producers/payload/lifecycle/log.js +67 -0
- package/dist/producers/payload/lifecycle/media-equality.d.ts +4 -0
- package/dist/producers/payload/lifecycle/media-equality.js +36 -0
- package/dist/producers/payload/lifecycle/operation.d.ts +2 -0
- package/dist/producers/payload/lifecycle/operation.js +7 -0
- package/dist/producers/payload/lifecycle/orderable-hooks.d.ts +21 -0
- package/dist/producers/payload/lifecycle/orderable-hooks.js +26 -0
- package/dist/producers/payload/lifecycle/orderable-neighbors.d.ts +21 -0
- package/dist/producers/payload/lifecycle/orderable-neighbors.js +151 -0
- package/dist/producers/payload/lifecycle/public-snapshots.d.ts +16 -0
- package/dist/producers/payload/lifecycle/public-snapshots.js +173 -0
- package/dist/producers/payload/lifecycle/revalidate.d.ts +16 -0
- package/dist/producers/payload/lifecycle/revalidate.js +162 -0
- package/dist/producers/payload/lifecycle/skip-rules.d.ts +19 -0
- package/dist/producers/payload/lifecycle/skip-rules.js +57 -0
- package/dist/producers/payload/lifecycle/types.d.ts +32 -0
- package/dist/producers/payload/lifecycle/types.js +1 -0
- package/dist/producers/payload/media/media-references.d.ts +31 -0
- package/dist/producers/payload/media/media-references.js +127 -0
- package/dist/producers/payload/media/resolve-media-affected-locales.d.ts +22 -0
- package/dist/producers/payload/media/resolve-media-affected-locales.js +70 -0
- package/dist/producers/payload/media/resolve-media-dependents.d.ts +39 -0
- package/dist/producers/payload/media/resolve-media-dependents.js +157 -0
- package/dist/producers/payload/media/resolve-media-slug.d.ts +7 -0
- package/dist/producers/payload/media/resolve-media-slug.js +17 -0
- package/dist/producers/payload/media/revalidate-media-dependents.d.ts +14 -0
- package/dist/producers/payload/media/revalidate-media-dependents.js +222 -0
- package/dist/producers/payload/plugin.d.ts +36 -0
- package/dist/producers/payload/plugin.js +235 -0
- package/dist/producers/payload/publish-all/context.d.ts +2 -0
- package/dist/producers/payload/publish-all/context.js +2 -0
- package/dist/producers/payload/publish-all/edited-locale.d.ts +5 -0
- package/dist/producers/payload/publish-all/edited-locale.js +5 -0
- package/dist/producers/payload/publish-all/index.d.ts +3 -0
- package/dist/producers/payload/publish-all/index.js +3 -0
- package/dist/producers/payload/publish-all/localization.d.ts +7 -0
- package/dist/producers/payload/publish-all/localization.js +1 -0
- package/dist/producers/payload/publish-all/mirror-publish-all-locales-to-req.d.ts +19 -0
- package/dist/producers/payload/publish-all/mirror-publish-all-locales-to-req.js +32 -0
- package/dist/producers/payload/publish-all/preserve-locales-on-publish-all.d.ts +2 -0
- package/dist/producers/payload/publish-all/preserve-locales-on-publish-all.js +145 -0
- package/dist/producers/payload/publish-all/read-localized-scalar.d.ts +2 -0
- package/dist/producers/payload/publish-all/read-localized-scalar.js +24 -0
- package/dist/producers/payload/publish-all/request-flags.d.ts +13 -0
- package/dist/producers/payload/publish-all/request-flags.js +9 -0
- package/dist/producers/payload/publish-all/restore-guard.d.ts +2 -0
- package/dist/producers/payload/publish-all/restore-guard.js +12 -0
- package/dist/producers/payload/publish-all/restore-locales-after-publish-all.d.ts +2 -0
- package/dist/producers/payload/publish-all/restore-locales-after-publish-all.js +85 -0
- package/dist/producers/payload/revalidation-custom.d.ts +37 -0
- package/dist/producers/payload/revalidation-custom.js +54 -0
- package/dist/producers/payload/schema.d.ts +3 -0
- package/dist/producers/payload/schema.js +23 -0
- package/docs/concepts/architecture.md +32 -0
- package/docs/concepts/cache-tags.md +97 -0
- package/docs/concepts/event-contract.md +38 -0
- package/docs/consumers/custom.md +19 -0
- package/docs/consumers/next.md +277 -0
- package/docs/dispatchers/custom.md +38 -0
- package/docs/dispatchers/http.md +66 -0
- package/docs/extending.md +26 -0
- package/docs/migration/0.3.0-to-0.4.0.md +67 -0
- package/docs/migration/0.4.0-to-0.5.0.md +60 -0
- package/docs/migration/0.5.x-to-0.0.0.md +143 -0
- package/docs/producers/custom.md +63 -0
- package/docs/producers/payload-media-dependencies.md +45 -0
- package/docs/producers/payload.md +140 -0
- package/docs/reliability.md +35 -0
- package/docs/security.md +34 -0
- package/docs/troubleshooting.md +14 -0
- package/package.json +120 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Package history
|
|
4
|
+
|
|
5
|
+
Entries through `0.5.7` below describe releases of the previous package, `@klnap/revalidation`.
|
|
6
|
+
The renamed `@klnap/cache-invalidation` package starts a separate version lifecycle at `0.0.0`.
|
|
7
|
+
Its first normal feature release is planned as `0.1.0`, after the initial `0.0.0` staged package is approved.
|
|
8
|
+
|
|
9
|
+
## Historical `@klnap/revalidation` releases
|
|
10
|
+
|
|
11
|
+
## 0.5.7
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 949fae9: Release workflow only: accept npm 12 single-value output when waiting for the published version. No runtime change.
|
|
16
|
+
|
|
17
|
+
## 0.5.6
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 7319f99: Release workflow only: enforce a ten-minute registry deadline and verify GitHub Release immutability.
|
|
22
|
+
|
|
23
|
+
## 0.5.5
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- 51bb54c: Release workflow and CI only: install the Bun version from packageManager instead of the latest Bun. No runtime change.
|
|
28
|
+
|
|
29
|
+
## 0.5.4
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- e3d1be0: Release workflow only: allow up to ten minutes for npm to serve provenance attestations before verifying a release. No runtime change.
|
|
34
|
+
|
|
35
|
+
## 0.5.3
|
|
36
|
+
|
|
37
|
+
### Patch Changes
|
|
38
|
+
|
|
39
|
+
- 11633cd: Release workflow only: verify each published release after publication, matching npm-package-template. No runtime change.
|
|
40
|
+
- 9226da0: Release workflow only: wait for npm to serve provenance attestations before verifying a release. No runtime change.
|
|
41
|
+
|
|
42
|
+
## 0.5.2
|
|
43
|
+
|
|
44
|
+
### Patch Changes
|
|
45
|
+
|
|
46
|
+
- 5ca5026: Release workflow only: retry the pack artifact download once after a short wait, matching npm-package-template. No runtime change.
|
|
47
|
+
|
|
48
|
+
## 0.5.1
|
|
49
|
+
|
|
50
|
+
### Patch Changes
|
|
51
|
+
|
|
52
|
+
- cc59fe4: Report missing and malformed HTTP target signing secrets with the target name and a clear remediation, without echoing the secret value.
|
|
53
|
+
|
|
54
|
+
## 0.5.0
|
|
55
|
+
|
|
56
|
+
### Minor Changes
|
|
57
|
+
|
|
58
|
+
- 0090aca: Require explicit Payload CloudEvents sources, resolve consumer schemas by event type, and use native
|
|
59
|
+
Next.js cache profiles and path scopes with optional source allowlists.
|
|
60
|
+
|
|
61
|
+
## 0.4.0
|
|
62
|
+
|
|
63
|
+
### Minor Changes
|
|
64
|
+
|
|
65
|
+
- 2c50b41: Replace the legacy consumer and event APIs with CloudEvents, Standard Schema validation, named HTTP targets, and composable cache rules. This release includes breaking public API changes.
|
|
66
|
+
|
|
67
|
+
## 0.3.0
|
|
68
|
+
|
|
69
|
+
### Minor Changes
|
|
70
|
+
|
|
71
|
+
- 1ba2679: Replace `invalidate` with declarative `rules` and context-based callbacks. Bind each `POST` handler to
|
|
72
|
+
its configuration, remove process-wide target registries and obsolete exports, improve tag and path
|
|
73
|
+
inference, and refresh the Next.js and Payload examples. This intentionally breaks the pre-1.0
|
|
74
|
+
consumer and HTTP dispatcher APIs.
|
|
75
|
+
|
|
76
|
+
## [Unreleased]
|
|
77
|
+
|
|
78
|
+
## [0.2.2] - 2026-11-24
|
|
79
|
+
|
|
80
|
+
### Added
|
|
81
|
+
|
|
82
|
+
- `maxEventItems` option on the HTTP dispatcher (default 100, hardest allowed 1000). The producer splits change sets into events of this size, so a larger value lets one event carry more items.
|
|
83
|
+
- Consumer limits validated at configuration time against hard ceilings: `maxBodyBytes` up to 16 MiB and `maxEvents` up to 1000. Out-of-range values throw at startup. Defaults are unchanged.
|
|
84
|
+
|
|
85
|
+
## [0.2.1] - 2026-11-24
|
|
86
|
+
|
|
87
|
+
### Added
|
|
88
|
+
|
|
89
|
+
- `allowInsecure` option on the HTTP dispatcher (default `false`). When `true`, plain `http://` targets are accepted on any host; the bearer secret is then sent unencrypted.
|
|
90
|
+
|
|
91
|
+
### Changed
|
|
92
|
+
|
|
93
|
+
- The HTTP dispatcher accepts plain `http://` targets on loopback hosts (`localhost`, `127.0.0.1`, `[::1]`) in every environment, so a local production build can reach a local receiver. Other plain `http://` targets are still refused outside `development` and `test`.
|
|
94
|
+
|
|
95
|
+
## [0.2.0] - 2026-11-24
|
|
96
|
+
|
|
97
|
+
### Breaking
|
|
98
|
+
|
|
99
|
+
- Stop exporting test-only reset functions and internal registry, lifecycle, and Next execution helpers. Use the documented producer and consumer APIs.
|
|
100
|
+
|
|
101
|
+
### Fixed
|
|
102
|
+
|
|
103
|
+
- Tag and path references must come from the active configuration, including low-effort declarations.
|
|
104
|
+
- Media dependency traversal uses stable ID cursors and preserves completed results when another query fails.
|
|
105
|
+
- Media dependency reads retain the current Payload hook request and transaction context.
|
|
106
|
+
- Oversized streamed requests return without waiting for stream cancellation to settle.
|
|
107
|
+
- Payload version-history lookup now scans beyond the first page.
|
|
108
|
+
- Include linked documentation in the packed package.
|
|
109
|
+
|
|
110
|
+
### Changed
|
|
111
|
+
|
|
112
|
+
- Cache tag helpers infer required parameters from literal templates.
|
|
113
|
+
- Production rejects a second, different `defineRevalidation` configuration instead of reusing stale declarations.
|
|
114
|
+
|
|
115
|
+
## [0.1.1] - 2026-11-24
|
|
116
|
+
|
|
117
|
+
### Fixed
|
|
118
|
+
|
|
119
|
+
- A `null` template parameter no longer renders the tag text `null`. Direct tag calls throw, and events skip the tag.
|
|
120
|
+
- An event whose field is `null` no longer fails with a `TypeError` (HTTP 500).
|
|
121
|
+
- After `defineRevalidation` is called again (development reload), the Route Handler uses the new declarations.
|
|
122
|
+
- Media-triggered events carry the parent document's route identifier and identifier values, not only its slug.
|
|
123
|
+
- A read error while building a Payload event is logged and no longer rejects the save.
|
|
124
|
+
- A layer that has no `execute` handler and is not `next` fails the request with `500`, instead of running Next revalidation.
|
|
125
|
+
- Items skipped for a missing template parameter are logged as `missing_param` when a logger is configured.
|
|
126
|
+
|
|
127
|
+
### Changed
|
|
128
|
+
|
|
129
|
+
- The boundary check (`check:boundaries`) analyses modules again; it had analysed zero modules with TypeScript 7.
|
|
130
|
+
- Documentation rewritten around the architecture; see `docs/`.
|
|
131
|
+
|
|
132
|
+
## [0.1.0] - 2026-11-24
|
|
133
|
+
|
|
134
|
+
First public release.
|
|
135
|
+
|
|
136
|
+
- Core pipeline (`@klnap/revalidation`) with framework-independent types, errors, and dispatcher contract.
|
|
137
|
+
- Payload producer (`@klnap/revalidation/producers/payload`): the `revalidation()` plugin emits events from `afterChange` and `afterDelete` hooks on collections and globals that set `custom.revalidation`. Handles locales, drafts, publish-all, orderable neighbors, and excluded fields. Media-dependent revalidation finds the documents that reference a changed or deleted media file from the Payload config and revalidates them.
|
|
138
|
+
- HTTP dispatcher (`@klnap/revalidation/dispatchers/http`) with bearer secret, retries, deadline, and bounded concurrency.
|
|
139
|
+
- Next.js consumer (`@klnap/revalidation/consumers/next`): `defineRevalidation`, `createCache`, and a `POST` route handler that expires or revalidates cache tags and paths.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Klnap
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,177 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@klnap/cache-invalidation`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Declarative, type-safe cache invalidation across independent applications, using a framework-independent CloudEvents protocol and native Payload CMS, HTTP, and Next.js integrations.
|
|
4
|
+
|
|
5
|
+
## Introduction
|
|
6
|
+
|
|
7
|
+
Content, commerce, and indexing systems often change data cached by another application. This package lets producers describe those changes without knowing the consumer's cache keys or routes.
|
|
8
|
+
|
|
9
|
+
## Why?
|
|
10
|
+
|
|
11
|
+
The producer knows what changed. The cache-owning application knows which cached data depends on it. A shared event protocol connects them without coupling either application to the other's framework or domain model.
|
|
12
|
+
|
|
13
|
+
Use Next.js native invalidation directly when the write and cache live in one application. Use this package when a separate service needs to notify the cache owner.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- Framework-independent CloudEvents 1.0 contracts and optional Resource Change V1 snapshots.
|
|
18
|
+
- Payload collection and global producers with localized public-state snapshots.
|
|
19
|
+
- HTTP delivery with Standard Webhooks signing, named destinations, bounded retries, and observable outcomes.
|
|
20
|
+
- Typed recursive cache tags and paths with exact event-type rules.
|
|
21
|
+
- Native Next.js profiles, schema-validated custom events, and programmable rules.
|
|
22
|
+
- Optional Payload and Next.js peer dependencies.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
bun add @klnap/cache-invalidation
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @klnap/cache-invalidation
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Install Payload or Next.js only in applications using those integrations. Importing the package root does not load either framework.
|
|
35
|
+
|
|
36
|
+
## Quick example
|
|
37
|
+
|
|
38
|
+
Define a tag and map the event type that changes it:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import 'server-only'
|
|
42
|
+
import { defineCache } from '@klnap/cache-invalidation/consumers/next'
|
|
43
|
+
|
|
44
|
+
export const cache = defineCache({
|
|
45
|
+
tags: {
|
|
46
|
+
article: 'article:[id]',
|
|
47
|
+
},
|
|
48
|
+
rules: ({ tags }) => ({
|
|
49
|
+
'payload.collection.articles': [tags.article],
|
|
50
|
+
}),
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Attach the tag where cached data is produced:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { cacheTag } from 'next/cache'
|
|
58
|
+
import { cache } from '@/cache'
|
|
59
|
+
|
|
60
|
+
export async function getArticle(id: string) {
|
|
61
|
+
'use cache'
|
|
62
|
+
cacheTag(cache.tags.article({ id }))
|
|
63
|
+
return loadArticle(id)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Next.js 16 requires `cacheComponents: true` in `next.config.ts` for `use cache` and `cacheTag()`.
|
|
68
|
+
|
|
69
|
+
## Usage
|
|
70
|
+
|
|
71
|
+
### Payload producer
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { cacheInvalidation } from '@klnap/cache-invalidation/producers/payload'
|
|
75
|
+
|
|
76
|
+
export default buildConfig({
|
|
77
|
+
plugins: [
|
|
78
|
+
cacheInvalidation({
|
|
79
|
+
source: 'https://cms.example.com',
|
|
80
|
+
collections: { articles: true },
|
|
81
|
+
dispatcher: {
|
|
82
|
+
targets: {
|
|
83
|
+
website: {
|
|
84
|
+
url: process.env.CACHE_INVALIDATION_URL!,
|
|
85
|
+
secret: process.env.CACHE_INVALIDATION_SECRET!,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
}),
|
|
90
|
+
],
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Resources are opt-in. Collection and global configuration can also be declared locally with `custom.cacheInvalidation`. See the [Payload guide](./docs/producers/payload.md).
|
|
95
|
+
|
|
96
|
+
### Next.js Route Handler
|
|
97
|
+
|
|
98
|
+
Bind the reusable definition to a signed endpoint:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// app/api/invalidate/route.ts
|
|
102
|
+
import { cacheInvalidationHandler } from '@klnap/cache-invalidation/consumers/next'
|
|
103
|
+
import { cache } from '@/cache'
|
|
104
|
+
|
|
105
|
+
export const POST = cacheInvalidationHandler({
|
|
106
|
+
config: cache,
|
|
107
|
+
secret: process.env.CACHE_INVALIDATION_SECRET!,
|
|
108
|
+
})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Credentials and HTTP limits belong to the handler. `defineCache()` has no authentication or network side effects.
|
|
112
|
+
|
|
113
|
+
### Nested declarations
|
|
114
|
+
|
|
115
|
+
Use nested objects to organize tags, paths, and event rules. Their keys do not change rendered tag values or event matching:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
defineCache({
|
|
119
|
+
tags: {
|
|
120
|
+
articles: {
|
|
121
|
+
list: 'articles:[locale]',
|
|
122
|
+
detail: 'article:[id]',
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
rules: ({ tags }) => ({
|
|
126
|
+
payload: {
|
|
127
|
+
content: {
|
|
128
|
+
'payload.collection.articles': [tags.articles.detail],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
})
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The only matched key is `payload.collection.articles`. See [cache tags and paths](./docs/concepts/cache-tags.md) and the [Next.js guide](./docs/consumers/next.md) for path patterns, profile groups, `rule()` descriptors, and programmable rules.
|
|
136
|
+
|
|
137
|
+
### Other integrations
|
|
138
|
+
|
|
139
|
+
Custom producers can emit CloudEvents through the root contract. Custom dispatchers implement `Dispatcher`. Custom consumers can use the protocol parser and Standard Schema validator. See the [custom producer](./docs/producers/custom.md), [custom dispatcher](./docs/dispatchers/custom.md), and [custom consumer](./docs/consumers/custom.md) guides.
|
|
140
|
+
|
|
141
|
+
## Configuration
|
|
142
|
+
|
|
143
|
+
`defineCache()` separates `config`, `tags`, `paths`, and `rules`. Tag profiles use `config.tags.profile`, with `{ expire: 0 }` as this library's default. Local rule groups use `[profile, [tagReferences...]]`; paths do not accept profiles.
|
|
144
|
+
|
|
145
|
+
Concrete path templates interpolate typed parameters. Native `[routePattern, 'page' | 'layout']` declarations keep the route pattern literal. Rules match exact CloudEvents `type` strings.
|
|
146
|
+
|
|
147
|
+
`cacheLife()` configures a cache's lifetime. `revalidateTag(tag, profile)` requests on-demand invalidation; its native named profile values retain their Next.js meaning.
|
|
148
|
+
|
|
149
|
+
## Compatibility
|
|
150
|
+
|
|
151
|
+
- Node.js `^22.23.5 || ^24.22.3 || >=26.13.1`.
|
|
152
|
+
- Next.js `>=16.4.5 <17` for the Next.js consumer.
|
|
153
|
+
- Payload `>=3.94.1 <4` for the Payload producer.
|
|
154
|
+
|
|
155
|
+
Payload hooks ordinarily run before the database transaction commits. HTTP delivery is best effort: retries may duplicate events, and process failure can lose them. This package does not provide durable delivery, exactly-once processing, or atomic invalidation. See [reliability](./docs/reliability.md) and [security](./docs/security.md).
|
|
156
|
+
|
|
157
|
+
The renamed package starts a separate version history at `0.0.0`. Its initial package is submitted through npm Staged Publishing; the first normal Changesets feature release is `0.1.0`. Staged artifacts remain unavailable until a maintainer reviews and approves them with npm two-factor authentication. See the [migration and release guide](./docs/migration/0.5.x-to-0.0.0.md).
|
|
158
|
+
|
|
159
|
+
## Documentation
|
|
160
|
+
|
|
161
|
+
- [Architecture](./docs/concepts/architecture.md)
|
|
162
|
+
- [Event contract](./docs/concepts/event-contract.md)
|
|
163
|
+
- [Cache tags and paths](./docs/concepts/cache-tags.md)
|
|
164
|
+
- [Payload producer](./docs/producers/payload.md)
|
|
165
|
+
- [Custom producer](./docs/producers/custom.md)
|
|
166
|
+
- [HTTP dispatcher](./docs/dispatchers/http.md)
|
|
167
|
+
- [Custom dispatcher](./docs/dispatchers/custom.md)
|
|
168
|
+
- [Next.js consumer](./docs/consumers/next.md)
|
|
169
|
+
- [Custom consumer](./docs/consumers/custom.md)
|
|
170
|
+
- [Reliability](./docs/reliability.md)
|
|
171
|
+
- [Security](./docs/security.md)
|
|
172
|
+
- [Troubleshooting](./docs/troubleshooting.md)
|
|
173
|
+
- [Migration from 0.5.x and release lifecycle](./docs/migration/0.5.x-to-0.0.0.md)
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TagParamsBag } from './host-types.js';
|
|
2
|
+
export declare const TAG_TEMPLATE_KEYS: unique symbol;
|
|
3
|
+
export declare const MAX_TAG_LENGTH = 256;
|
|
4
|
+
/** Keeps long tags stable and within Next.js's limit without dropping cache dependencies. */
|
|
5
|
+
export declare const fitTagLength: (value: string) => string;
|
|
6
|
+
export type CompiledTagTemplate = {
|
|
7
|
+
keys: readonly string[];
|
|
8
|
+
usesSlug: boolean;
|
|
9
|
+
render: (params: TagParamsBag) => string | null;
|
|
10
|
+
};
|
|
11
|
+
export declare const compileTagTemplate: (template: string) => CompiledTagTemplate;
|
|
12
|
+
export type TagTemplateMeta = CompiledTagTemplate;
|
|
13
|
+
export type TemplateMetaCarrier = {
|
|
14
|
+
[TAG_TEMPLATE_KEYS]?: TagTemplateMeta;
|
|
15
|
+
};
|
|
16
|
+
export declare const getTagTemplateMeta: (builder: TemplateMetaCarrier) => TagTemplateMeta | undefined;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export const TAG_TEMPLATE_KEYS = Symbol.for('@klnap/cache-invalidation/tagTemplateKeys');
|
|
2
|
+
export const MAX_TAG_LENGTH = 256;
|
|
3
|
+
const hashTag = (value) => {
|
|
4
|
+
let hash = 0xcbf29ce484222325n;
|
|
5
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
6
|
+
hash ^= BigInt(value.charCodeAt(index));
|
|
7
|
+
hash = BigInt.asUintN(64, hash * 0x100000001b3n);
|
|
8
|
+
}
|
|
9
|
+
return hash.toString(16).padStart(16, '0');
|
|
10
|
+
};
|
|
11
|
+
/** Keeps long tags stable and within Next.js's limit without dropping cache dependencies. */
|
|
12
|
+
export const fitTagLength = (value) => {
|
|
13
|
+
if (value.length <= MAX_TAG_LENGTH)
|
|
14
|
+
return value;
|
|
15
|
+
const suffix = `:${hashTag(value)}`;
|
|
16
|
+
let prefix = '';
|
|
17
|
+
for (const character of value) {
|
|
18
|
+
if (prefix.length + character.length > MAX_TAG_LENGTH - suffix.length)
|
|
19
|
+
break;
|
|
20
|
+
prefix += character;
|
|
21
|
+
}
|
|
22
|
+
return `${prefix}${suffix}`;
|
|
23
|
+
};
|
|
24
|
+
const KEY_PATTERN = /^[a-zA-Z][a-zA-Z0-9_]*$/;
|
|
25
|
+
const parseTemplate = (template) => {
|
|
26
|
+
const parts = [];
|
|
27
|
+
let i = 0;
|
|
28
|
+
let literalStart = 0;
|
|
29
|
+
while (i < template.length) {
|
|
30
|
+
if (template[i] !== '[') {
|
|
31
|
+
i += 1;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (i > literalStart) {
|
|
35
|
+
parts.push({ kind: 'literal', value: template.slice(literalStart, i) });
|
|
36
|
+
}
|
|
37
|
+
const close = template.indexOf(']', i + 1);
|
|
38
|
+
if (close === -1) {
|
|
39
|
+
throw new Error(`Tag template: unclosed "[" in "${template}"`);
|
|
40
|
+
}
|
|
41
|
+
const key = template.slice(i + 1, close);
|
|
42
|
+
if (!KEY_PATTERN.test(key)) {
|
|
43
|
+
throw new Error(`Tag template: invalid placeholder [${key}] in "${template}"`);
|
|
44
|
+
}
|
|
45
|
+
parts.push({ kind: 'key', value: key });
|
|
46
|
+
i = close + 1;
|
|
47
|
+
literalStart = i;
|
|
48
|
+
}
|
|
49
|
+
if (literalStart < template.length) {
|
|
50
|
+
parts.push({ kind: 'literal', value: template.slice(literalStart) });
|
|
51
|
+
}
|
|
52
|
+
return parts;
|
|
53
|
+
};
|
|
54
|
+
export const compileTagTemplate = (template) => {
|
|
55
|
+
const parts = parseTemplate(template);
|
|
56
|
+
const keys = [];
|
|
57
|
+
const seen = new Set();
|
|
58
|
+
for (const part of parts) {
|
|
59
|
+
if (part.kind !== 'key')
|
|
60
|
+
continue;
|
|
61
|
+
if (!seen.has(part.value)) {
|
|
62
|
+
seen.add(part.value);
|
|
63
|
+
keys.push(part.value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const usesSlug = seen.has('slug');
|
|
67
|
+
const render = (params) => {
|
|
68
|
+
let out = '';
|
|
69
|
+
for (const part of parts) {
|
|
70
|
+
if (part.kind === 'literal') {
|
|
71
|
+
out += part.value;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const value = Object.hasOwn(params, part.value) ? params[part.value] : undefined;
|
|
75
|
+
if (value === undefined || value === null || value === '')
|
|
76
|
+
return null;
|
|
77
|
+
out += value;
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
};
|
|
81
|
+
return { keys, usesSlug, render };
|
|
82
|
+
};
|
|
83
|
+
export const getTagTemplateMeta = (builder) => builder[TAG_TEMPLATE_KEYS];
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import type { CloudEvent } from '../../core/cloudevents.js';
|
|
3
|
+
import type { Logger } from '../../core/types.js';
|
|
4
|
+
import type { BuiltPaths, BuiltTags, PathDeclarations, PathReference, TagDeclarations, TagReference } from './host-types.js';
|
|
5
|
+
export type SchemaMap = Record<string, StandardSchemaV1>;
|
|
6
|
+
export type SchemaOutput<TSchema> = TSchema extends StandardSchemaV1<unknown, infer TOutput> ? TOutput : never;
|
|
7
|
+
export type CacheProfile = NonNullable<Parameters<typeof import('next/cache.js').revalidateTag>[1]>;
|
|
8
|
+
export declare const RULE_EVENT_TYPE: unique symbol;
|
|
9
|
+
export declare const RULE_DESCRIPTOR: unique symbol;
|
|
10
|
+
type NonEmpty<T> = readonly [T, ...T[]];
|
|
11
|
+
type RecursiveLeaves<T> = T extends (...args: never[]) => string ? T : T extends object ? RecursiveLeaves<T[keyof T]> : never;
|
|
12
|
+
type TagLeaves<TTags extends TagDeclarations> = [keyof TTags] extends [never] ? TagReference : RecursiveLeaves<BuiltTags<TTags>>;
|
|
13
|
+
type PathLeaves<TPaths extends PathDeclarations> = [keyof TPaths] extends [never] ? PathReference : RecursiveLeaves<BuiltPaths<TPaths>>;
|
|
14
|
+
type TagRuleEntry<TTags extends TagDeclarations> = TagLeaves<TTags> | string | readonly [CacheProfile, NonEmpty<TagLeaves<TTags>>];
|
|
15
|
+
type PathRuleEntry<TPaths extends PathDeclarations> = PathLeaves<TPaths> | string;
|
|
16
|
+
type RuleOperation<TTags extends TagDeclarations, TPaths extends PathDeclarations> = TagRuleEntry<TTags> | PathRuleEntry<TPaths>;
|
|
17
|
+
type RuleDescriptorWithTags<TTags extends TagDeclarations, TPaths extends PathDeclarations> = {
|
|
18
|
+
tags: NonEmpty<TagRuleEntry<TTags>>;
|
|
19
|
+
paths?: readonly PathRuleEntry<TPaths>[];
|
|
20
|
+
};
|
|
21
|
+
type RuleDescriptorWithPaths<TTags extends TagDeclarations, TPaths extends PathDeclarations> = {
|
|
22
|
+
tags?: readonly TagRuleEntry<TTags>[];
|
|
23
|
+
paths: NonEmpty<PathRuleEntry<TPaths>>;
|
|
24
|
+
};
|
|
25
|
+
export type CacheRuleDescriptor<TTags extends TagDeclarations = Record<never, never>, TPaths extends PathDeclarations = Record<never, never>> = RuleDescriptorWithTags<TTags, TPaths> | RuleDescriptorWithPaths<TTags, TPaths>;
|
|
26
|
+
export type MarkedCacheRuleDescriptor<TTags extends TagDeclarations = Record<never, never>, TPaths extends PathDeclarations = Record<never, never>> = CacheRuleDescriptor<TTags, TPaths> & {
|
|
27
|
+
readonly [RULE_DESCRIPTOR]: true;
|
|
28
|
+
};
|
|
29
|
+
export type CacheInvalidationRuleResult<TTags extends TagDeclarations, TPaths extends PathDeclarations, TLayer extends string = string> = {
|
|
30
|
+
tags?: readonly TagRuleEntry<TTags>[];
|
|
31
|
+
paths?: readonly PathRuleEntry<TPaths>[];
|
|
32
|
+
operations?: readonly {
|
|
33
|
+
layer: TLayer;
|
|
34
|
+
value: unknown;
|
|
35
|
+
}[];
|
|
36
|
+
};
|
|
37
|
+
export type CacheInvalidationRuleContext<TData, TTags extends TagDeclarations, TPaths extends PathDeclarations> = {
|
|
38
|
+
event: CloudEvent<TData>;
|
|
39
|
+
data: TData;
|
|
40
|
+
tags: BuiltTags<TTags>;
|
|
41
|
+
paths: BuiltPaths<TPaths>;
|
|
42
|
+
};
|
|
43
|
+
export type CacheInvalidationExecutor<TData = unknown, TValue = unknown> = (context: CacheInvalidationExecutionContext<TData, TValue>) => void | Promise<void>;
|
|
44
|
+
export type ExecutorMap<TData = unknown> = Record<string, CacheInvalidationExecutor<TData>>;
|
|
45
|
+
export type CacheInvalidationExecutionContext<TData = unknown, TValue = unknown> = {
|
|
46
|
+
event: CloudEvent<TData>;
|
|
47
|
+
data: TData;
|
|
48
|
+
value: TValue;
|
|
49
|
+
signal: AbortSignal;
|
|
50
|
+
};
|
|
51
|
+
type Resolver<TData, TTags extends TagDeclarations, TPaths extends PathDeclarations, TLayer extends string> = (context: CacheInvalidationRuleContext<TData, TTags, TPaths>) => CacheInvalidationRuleResult<TTags, TPaths, TLayer> | null | undefined;
|
|
52
|
+
type SchemaData<TSchemas extends SchemaMap> = keyof TSchemas extends never ? unknown : SchemaOutput<TSchemas[keyof TSchemas]>;
|
|
53
|
+
type BoundRuleNode<TSchemas extends SchemaMap, TTags extends TagDeclarations, TPaths extends PathDeclarations, TLayer extends string> = Resolver<SchemaData<TSchemas>, TTags, TPaths, TLayer> & {
|
|
54
|
+
readonly [RULE_EVENT_TYPE]: string;
|
|
55
|
+
};
|
|
56
|
+
type RuleNode<TSchemas extends SchemaMap, TTags extends TagDeclarations, TPaths extends PathDeclarations, TLayer extends string> = readonly RuleOperation<TTags, TPaths>[] | MarkedCacheRuleDescriptor<TTags, TPaths> | Resolver<SchemaData<TSchemas>, TTags, TPaths, TLayer> | BoundRuleNode<TSchemas, TTags, TPaths, TLayer> | {
|
|
57
|
+
readonly [key: string]: RuleNode<TSchemas, TTags, TPaths, TLayer>;
|
|
58
|
+
};
|
|
59
|
+
export type CacheInvalidationHandlerOptions = {
|
|
60
|
+
allowedSources?: readonly string[];
|
|
61
|
+
maxBodyBytes?: number;
|
|
62
|
+
bodyTimeoutMs?: number;
|
|
63
|
+
maxEvents?: number;
|
|
64
|
+
maxOperations?: number;
|
|
65
|
+
executionTimeoutMs?: number;
|
|
66
|
+
logger?: Logger;
|
|
67
|
+
};
|
|
68
|
+
export type DefineCacheInput<TTags extends TagDeclarations = Record<never, never>, TPaths extends PathDeclarations = Record<never, never>, TSchemas extends SchemaMap = Record<never, never>, TExecutors extends ExecutorMap = ExecutorMap> = {
|
|
69
|
+
config?: {
|
|
70
|
+
tags?: {
|
|
71
|
+
profile?: CacheProfile;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
tags?: TTags;
|
|
75
|
+
paths?: TPaths;
|
|
76
|
+
schemas?: TSchemas;
|
|
77
|
+
rules: (helpers: {
|
|
78
|
+
tags: BuiltTags<TTags>;
|
|
79
|
+
paths: BuiltPaths<TPaths>;
|
|
80
|
+
rule: (descriptor: CacheRuleDescriptor<TTags, TPaths>) => MarkedCacheRuleDescriptor<TTags, TPaths>;
|
|
81
|
+
on: <TType extends keyof TSchemas & string>(type: TType, resolve: Resolver<SchemaOutput<TSchemas[TType]>, TTags, TPaths, keyof TExecutors & string>) => BoundRuleNode<TSchemas, TTags, TPaths, keyof TExecutors & string>;
|
|
82
|
+
}) => RuleNode<TSchemas, TTags, TPaths, keyof TExecutors & string>;
|
|
83
|
+
execute?: TExecutors;
|
|
84
|
+
};
|
|
85
|
+
export type CacheDefinition<TTags extends TagDeclarations, TPaths extends PathDeclarations, TSchemas extends SchemaMap, TExecutors extends ExecutorMap> = {
|
|
86
|
+
readonly tags: BuiltTags<TTags>;
|
|
87
|
+
readonly paths: BuiltPaths<TPaths>;
|
|
88
|
+
readonly [CACHE_DEFINITION]: {
|
|
89
|
+
schemas: TSchemas;
|
|
90
|
+
rules: Readonly<Record<string, unknown>>;
|
|
91
|
+
profile?: CacheProfile;
|
|
92
|
+
execute?: TExecutors;
|
|
93
|
+
tagReferences: Readonly<Record<string, (...args: never[]) => string>>;
|
|
94
|
+
pathReferences: Readonly<Record<string, (...args: never[]) => string>>;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
export declare const CACHE_DEFINITION: unique symbol;
|
|
98
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type CacheDefinition, type DefineCacheInput, type ExecutorMap, type SchemaMap } from './consumer-types.js';
|
|
2
|
+
import { type PathDeclarations, type TagDeclarations } from './host-types.js';
|
|
3
|
+
export type { CacheDefinition, CacheInvalidationExecutionContext, CacheInvalidationExecutor, CacheInvalidationHandlerOptions, CacheInvalidationRuleContext, CacheInvalidationRuleResult, CacheProfile, DefineCacheInput, } from './consumer-types.js';
|
|
4
|
+
export declare const defineCache: <const TTags extends TagDeclarations = Record<never, never>, const TPaths extends PathDeclarations = Record<never, never>, const TSchemas extends SchemaMap = Record<never, never>, const TExecutors extends ExecutorMap = ExecutorMap>(input: DefineCacheInput<TTags, TPaths, TSchemas, TExecutors>) => CacheDefinition<TTags, TPaths, TSchemas, TExecutors>;
|