@datum-cloud/strapi-revalidate 0.1.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/.env.example +5 -0
- package/LICENSE +121 -0
- package/README.md +227 -0
- package/dist/cache/drivers/file.d.ts +37 -0
- package/dist/cache/drivers/file.d.ts.map +1 -0
- package/dist/cache/drivers/file.js +151 -0
- package/dist/cache/drivers/file.js.map +1 -0
- package/dist/cache/drivers/memory.d.ts +20 -0
- package/dist/cache/drivers/memory.d.ts.map +1 -0
- package/dist/cache/drivers/memory.js +76 -0
- package/dist/cache/drivers/memory.js.map +1 -0
- package/dist/cache/drivers/redis.d.ts +25 -0
- package/dist/cache/drivers/redis.d.ts.map +1 -0
- package/dist/cache/drivers/redis.js +36 -0
- package/dist/cache/drivers/redis.js.map +1 -0
- package/dist/cache/index.d.ts +28 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +54 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/manager.d.ts +55 -0
- package/dist/cache/manager.d.ts.map +1 -0
- package/dist/cache/manager.js +73 -0
- package/dist/cache/manager.js.map +1 -0
- package/dist/cache/types.d.ts +33 -0
- package/dist/cache/types.d.ts.map +1 -0
- package/dist/cache/types.js +9 -0
- package/dist/cache/types.js.map +1 -0
- package/dist/client/graphql.d.ts +46 -0
- package/dist/client/graphql.d.ts.map +1 -0
- package/dist/client/graphql.js +97 -0
- package/dist/client/graphql.js.map +1 -0
- package/dist/client/index.d.ts +28 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +38 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/rest.d.ts +23 -0
- package/dist/client/rest.d.ts.map +1 -0
- package/dist/client/rest.js +25 -0
- package/dist/client/rest.js.map +1 -0
- package/dist/content/articles.d.ts +36 -0
- package/dist/content/articles.d.ts.map +1 -0
- package/dist/content/articles.js +157 -0
- package/dist/content/articles.js.map +1 -0
- package/dist/content/authors.d.ts +28 -0
- package/dist/content/authors.d.ts.map +1 -0
- package/dist/content/authors.js +115 -0
- package/dist/content/authors.js.map +1 -0
- package/dist/content/index.d.ts +11 -0
- package/dist/content/index.d.ts.map +1 -0
- package/dist/content/index.js +10 -0
- package/dist/content/index.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/types/config.d.ts +108 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +49 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/strapi.d.ts +103 -0
- package/dist/types/strapi.d.ts.map +1 -0
- package/dist/types/strapi.js +11 -0
- package/dist/types/strapi.js.map +1 -0
- package/dist/webhook/handler.d.ts +38 -0
- package/dist/webhook/handler.d.ts.map +1 -0
- package/dist/webhook/handler.js +156 -0
- package/dist/webhook/handler.js.map +1 -0
- package/dist/webhook/index.d.ts +9 -0
- package/dist/webhook/index.d.ts.map +1 -0
- package/dist/webhook/index.js +7 -0
- package/dist/webhook/index.js.map +1 -0
- package/dist/webhook/tags.d.ts +22 -0
- package/dist/webhook/tags.d.ts.map +1 -0
- package/dist/webhook/tags.js +39 -0
- package/dist/webhook/tags.js.map +1 -0
- package/dist/webhook/types.d.ts +60 -0
- package/dist/webhook/types.d.ts.map +1 -0
- package/dist/webhook/types.js +6 -0
- package/dist/webhook/types.js.map +1 -0
- package/package.json +90 -0
package/.env.example
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
Creative Commons Legal Code
|
|
2
|
+
|
|
3
|
+
CC0 1.0 Universal
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/README.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# strapi-revalidate
|
|
2
|
+
|
|
3
|
+
Keep your SSR frontend in sync with Strapi content — automatically.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@datum-cloud/strapi-revalidate)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
[](https://nodejs.org)
|
|
8
|
+
|
|
9
|
+
`strapi-revalidate` is a framework-agnostic Node.js library for SSR frontends using Strapi v5 as a headless CMS. It caches Strapi content locally (file, memory, or Redis), receives Strapi publish webhooks, and invalidates only the affected cache entries — so the next request always serves fresh data without a full rebuild or manual intervention. Battle-tested in production at [datum.net](https://datum.net).
|
|
10
|
+
|
|
11
|
+
## How this differs from Strapi-side cache plugins
|
|
12
|
+
|
|
13
|
+
**strapi-revalidate** solves a different problem than `strapi-cache`, `strapi-plugin-rest-cache`, and the dozens of similar Strapi plugins on npm and GitHub.
|
|
14
|
+
|
|
15
|
+
Those are **Strapi plugins** — they install into `config/plugins.js`, run inside the Strapi server process, and cache Strapi's own API responses before they leave the server. They have no awareness of your frontend at all.
|
|
16
|
+
|
|
17
|
+
**strapi-revalidate** lives in your **frontend SSR process** — Astro, Next.js, SvelteKit, Nuxt, Express, whatever. It caches the data your frontend fetches *from* Strapi, and invalidates that cache the moment Strapi publishes new content via webhook.
|
|
18
|
+
|
|
19
|
+
The two approaches are complementary. If you're already running `strapi-cache` on your Strapi instance, keep it — `strapi-revalidate` works alongside it without conflict.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @datum-cloud/strapi-revalidate
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
28
|
+
|
|
29
|
+
Each snippet below is a complete, copy-pasteable example. Pick your framework.
|
|
30
|
+
|
|
31
|
+
### Astro
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
// src/lib/strapi.ts
|
|
35
|
+
import { createStrapiRevalidate } from '@datum-cloud/strapi-revalidate';
|
|
36
|
+
|
|
37
|
+
export const { client, cache, webhook } = createStrapiRevalidate({
|
|
38
|
+
url: import.meta.env.STRAPI_URL,
|
|
39
|
+
token: import.meta.env.STRAPI_TOKEN,
|
|
40
|
+
webhook: { secret: import.meta.env.STRAPI_WEBHOOK_SECRET },
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```astro
|
|
45
|
+
---
|
|
46
|
+
// src/pages/blog/index.astro
|
|
47
|
+
import { fetchArticles } from '@datum-cloud/strapi-revalidate/content';
|
|
48
|
+
import { client, cache } from '../../lib/strapi';
|
|
49
|
+
|
|
50
|
+
const articles = await fetchArticles({ client, cache });
|
|
51
|
+
---
|
|
52
|
+
<ul>
|
|
53
|
+
{articles.map((a) => <li><a href={`/blog/${a.slug}`}>{a.title}</a></li>)}
|
|
54
|
+
</ul>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// src/pages/api/strapi-webhook.ts
|
|
59
|
+
import type { APIRoute } from 'astro';
|
|
60
|
+
import { webhook } from '../../lib/strapi';
|
|
61
|
+
|
|
62
|
+
export const POST: APIRoute = async ({ request }) => {
|
|
63
|
+
let status = 200;
|
|
64
|
+
let body: unknown = {};
|
|
65
|
+
await webhook(request, {
|
|
66
|
+
status: (code) => {
|
|
67
|
+
status = code;
|
|
68
|
+
},
|
|
69
|
+
json: (value) => {
|
|
70
|
+
body = value;
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
return new Response(JSON.stringify(body), {
|
|
74
|
+
status,
|
|
75
|
+
headers: { 'Content-Type': 'application/json' },
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Next.js (App Router)
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// lib/strapi.ts
|
|
84
|
+
import { createStrapiRevalidate } from '@datum-cloud/strapi-revalidate';
|
|
85
|
+
|
|
86
|
+
export const { client, cache, webhook } = createStrapiRevalidate({
|
|
87
|
+
url: process.env.STRAPI_URL!,
|
|
88
|
+
token: process.env.STRAPI_TOKEN!,
|
|
89
|
+
webhook: { secret: process.env.STRAPI_WEBHOOK_SECRET },
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
// app/blog/page.tsx
|
|
95
|
+
import { fetchArticles } from '@datum-cloud/strapi-revalidate/content';
|
|
96
|
+
import { client, cache } from '@/lib/strapi';
|
|
97
|
+
|
|
98
|
+
export default async function BlogPage() {
|
|
99
|
+
const articles = await fetchArticles({ client, cache });
|
|
100
|
+
return (
|
|
101
|
+
<ul>
|
|
102
|
+
{articles.map((a) => (
|
|
103
|
+
<li key={a.documentId}>
|
|
104
|
+
<a href={`/blog/${a.slug}`}>{a.title}</a>
|
|
105
|
+
</li>
|
|
106
|
+
))}
|
|
107
|
+
</ul>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// app/api/strapi-webhook/route.ts
|
|
114
|
+
import { NextResponse } from 'next/server';
|
|
115
|
+
import { webhook } from '@/lib/strapi';
|
|
116
|
+
|
|
117
|
+
export async function POST(request: Request) {
|
|
118
|
+
let status = 200;
|
|
119
|
+
let body: unknown = {};
|
|
120
|
+
await webhook(request, {
|
|
121
|
+
status: (code) => {
|
|
122
|
+
status = code;
|
|
123
|
+
},
|
|
124
|
+
json: (value) => {
|
|
125
|
+
body = value;
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
return NextResponse.json(body, { status });
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### SvelteKit
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
// src/lib/strapi.ts
|
|
136
|
+
import { createStrapiRevalidate } from '@datum-cloud/strapi-revalidate';
|
|
137
|
+
import { env } from '$env/dynamic/private';
|
|
138
|
+
|
|
139
|
+
export const { client, cache, webhook } = createStrapiRevalidate({
|
|
140
|
+
url: env.STRAPI_URL,
|
|
141
|
+
token: env.STRAPI_TOKEN,
|
|
142
|
+
webhook: { secret: env.STRAPI_WEBHOOK_SECRET },
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
// src/routes/blog/+page.server.ts
|
|
148
|
+
import { fetchArticles } from '@datum-cloud/strapi-revalidate/content';
|
|
149
|
+
import { client, cache } from '$lib/strapi';
|
|
150
|
+
|
|
151
|
+
export const load = async () => {
|
|
152
|
+
const articles = await fetchArticles({ client, cache });
|
|
153
|
+
return { articles };
|
|
154
|
+
};
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
// src/routes/api/strapi-webhook/+server.ts
|
|
159
|
+
import { json } from '@sveltejs/kit';
|
|
160
|
+
import type { RequestHandler } from './$types';
|
|
161
|
+
import { webhook } from '$lib/strapi';
|
|
162
|
+
|
|
163
|
+
export const POST: RequestHandler = async ({ request }) => {
|
|
164
|
+
let status = 200;
|
|
165
|
+
let body: unknown = {};
|
|
166
|
+
await webhook(request, {
|
|
167
|
+
status: (code) => {
|
|
168
|
+
status = code;
|
|
169
|
+
},
|
|
170
|
+
json: (value) => {
|
|
171
|
+
body = value;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
return json(body, { status });
|
|
175
|
+
};
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Environment variables
|
|
179
|
+
|
|
180
|
+
| Variable | Required | Default | Description |
|
|
181
|
+
|---|---|---|---|
|
|
182
|
+
| `STRAPI_URL` | Yes | — | Your Strapi Cloud base URL |
|
|
183
|
+
| `STRAPI_TOKEN` | Yes | — | Strapi API token (Settings → API Tokens) |
|
|
184
|
+
| `STRAPI_WEBHOOK_SECRET` | Recommended | — | Secret set in Strapi webhook config |
|
|
185
|
+
| `STRAPI_CACHE_TTL` | No | `3600` | Cache TTL in seconds |
|
|
186
|
+
| `STRAPI_TIMEOUT` | No | `3` | Request timeout in seconds |
|
|
187
|
+
|
|
188
|
+
## Strapi webhook setup
|
|
189
|
+
|
|
190
|
+
1. In your Strapi Cloud admin, open **Settings → Webhooks → Add new webhook**.
|
|
191
|
+
2. **Name**: `strapi-revalidate` (or whatever you like).
|
|
192
|
+
3. **URL**: the public URL of the API route you mounted in the quickstart, e.g. `https://example.com/api/strapi-webhook`.
|
|
193
|
+
4. **Headers**: add `Authorization: Bearer <STRAPI_WEBHOOK_SECRET>` so the handler can verify the source. The handler also accepts `X-Strapi-Signature` or `strapi-webhook-secret` headers if you'd rather not use `Authorization`.
|
|
194
|
+
5. **Events**: enable at minimum `entry.publish`, `entry.unpublish`, and `entry.update`. Add `entry.delete` if you want deletions to clear cache too.
|
|
195
|
+
6. Save the webhook. Publish any entry — within seconds, the matching cache tags are cleared and the next request serves fresh data.
|
|
196
|
+
|
|
197
|
+
<!-- screenshot: strapi-webhook-config -->
|
|
198
|
+
|
|
199
|
+
## Cache drivers
|
|
200
|
+
|
|
201
|
+
| Driver | Default | Use case |
|
|
202
|
+
|---|---|---|
|
|
203
|
+
| `file` | ✓ | Single-instance servers. Survives process restarts. |
|
|
204
|
+
| `memory` | | Dev and testing. Fast, lost on restart. |
|
|
205
|
+
| `redis` | | Multi-instance / edge deploys. Requires `ioredis`. Coming v1.x. |
|
|
206
|
+
|
|
207
|
+
Select a driver via `cache.driver` in config:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
createStrapiRevalidate({
|
|
211
|
+
url: process.env.STRAPI_URL!,
|
|
212
|
+
token: process.env.STRAPI_TOKEN!,
|
|
213
|
+
cache: { driver: 'memory' },
|
|
214
|
+
});
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## How the fallback cache works
|
|
218
|
+
|
|
219
|
+
Every successful Strapi fetch writes to **two** caches: the primary TTL cache (configurable expiry, cleared by webhook) and a persistent fallback cache (no expiry, file-backed). If Strapi is unreachable on a future fetch, the fallback serves stale data instead of throwing or returning empty — keeping your site online during Strapi outages or deploys. The fallback is on by default and cannot be disabled.
|
|
220
|
+
|
|
221
|
+
## Contributing
|
|
222
|
+
|
|
223
|
+
Issues and PRs welcome at [github.com/datum-cloud/strapi-revalidate](https://github.com/datum-cloud/strapi-revalidate/issues). This library was extracted from production code at [datum.net](https://datum.net); contributions that keep the public surface small and the dependencies few are especially appreciated.
|
|
224
|
+
|
|
225
|
+
## License
|
|
226
|
+
|
|
227
|
+
MIT
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filesystem cache driver.
|
|
3
|
+
*
|
|
4
|
+
* Adapted from datum.net's production `Cache` class. Preserves the original
|
|
5
|
+
* behaviour:
|
|
6
|
+
* - one `.json` payload file per key
|
|
7
|
+
* - sibling `.expires` file storing the absolute expiry timestamp (ms)
|
|
8
|
+
* - empty-file guard: a zero-length cache file is auto-cleared
|
|
9
|
+
* - ENOENT is silent; other read errors clear the corrupt entry
|
|
10
|
+
*
|
|
11
|
+
* Tag membership is persisted alongside the payload in a sidecar `.tags` file
|
|
12
|
+
* so `deleteByTag` survives process restarts.
|
|
13
|
+
*/
|
|
14
|
+
import type { CacheDriver, CacheSetOptions } from '../types.js';
|
|
15
|
+
/** Configuration for the file driver. */
|
|
16
|
+
export interface FileDriverOptions {
|
|
17
|
+
/** Directory where cache files live. Created on first write. */
|
|
18
|
+
dir: string;
|
|
19
|
+
}
|
|
20
|
+
/** Filesystem-backed `CacheDriver`. */
|
|
21
|
+
export declare class FileCacheDriver implements CacheDriver {
|
|
22
|
+
private readonly dir;
|
|
23
|
+
constructor(options: FileDriverOptions);
|
|
24
|
+
get<T>(key: string): Promise<T | null>;
|
|
25
|
+
set<T>(key: string, data: T, options?: CacheSetOptions): Promise<void>;
|
|
26
|
+
delete(key: string): Promise<void>;
|
|
27
|
+
deleteByTag(tag: string): Promise<void>;
|
|
28
|
+
clear(): Promise<void>;
|
|
29
|
+
keys(prefix?: string): Promise<string[]>;
|
|
30
|
+
private payloadPath;
|
|
31
|
+
private expiresPath;
|
|
32
|
+
private tagsPath;
|
|
33
|
+
private ensureDir;
|
|
34
|
+
private readTags;
|
|
35
|
+
private silentUnlink;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/cache/drivers/file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAMhE,yCAAyC;AACzC,MAAM,WAAW,iBAAiB;IAChC,gEAAgE;IAChE,GAAG,EAAE,MAAM,CAAC;CACb;AAED,uCAAuC;AACvC,qBAAa,eAAgB,YAAW,WAAW;IACjD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;gBAEjB,OAAO,EAAE,iBAAiB;IAIhC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAsCtC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB1E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAYvC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAWtB,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAe9C,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,QAAQ;YAIF,SAAS;YAIT,QAAQ;YAUR,YAAY;CAO3B"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filesystem cache driver.
|
|
3
|
+
*
|
|
4
|
+
* Adapted from datum.net's production `Cache` class. Preserves the original
|
|
5
|
+
* behaviour:
|
|
6
|
+
* - one `.json` payload file per key
|
|
7
|
+
* - sibling `.expires` file storing the absolute expiry timestamp (ms)
|
|
8
|
+
* - empty-file guard: a zero-length cache file is auto-cleared
|
|
9
|
+
* - ENOENT is silent; other read errors clear the corrupt entry
|
|
10
|
+
*
|
|
11
|
+
* Tag membership is persisted alongside the payload in a sidecar `.tags` file
|
|
12
|
+
* so `deleteByTag` survives process restarts.
|
|
13
|
+
*/
|
|
14
|
+
import fs from 'node:fs/promises';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
const PAYLOAD_EXT = '.json';
|
|
17
|
+
const EXPIRES_EXT = '.expires';
|
|
18
|
+
const TAGS_EXT = '.tags';
|
|
19
|
+
/** Filesystem-backed `CacheDriver`. */
|
|
20
|
+
export class FileCacheDriver {
|
|
21
|
+
dir;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.dir = options.dir;
|
|
24
|
+
}
|
|
25
|
+
async get(key) {
|
|
26
|
+
const filePath = this.payloadPath(key);
|
|
27
|
+
const expiresPath = this.expiresPath(key);
|
|
28
|
+
try {
|
|
29
|
+
const data = await fs.readFile(filePath, 'utf-8');
|
|
30
|
+
// Empty-file guard preserved from datum.net Cache.
|
|
31
|
+
if (!data.trim()) {
|
|
32
|
+
console.warn(`Cache file for key "${key}" is empty, clearing cache`);
|
|
33
|
+
await this.delete(key);
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
let expirationTime = null;
|
|
37
|
+
try {
|
|
38
|
+
const expiresData = await fs.readFile(expiresPath, 'utf-8');
|
|
39
|
+
expirationTime = JSON.parse(expiresData);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// No expires file — treat as no expiry.
|
|
43
|
+
}
|
|
44
|
+
if (expirationTime !== null && Date.now() > expirationTime) {
|
|
45
|
+
await this.delete(key);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return JSON.parse(data);
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (err.code === 'ENOENT') {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
console.warn(`Failed to read cache file for key "${key}":`, err);
|
|
55
|
+
await this.delete(key);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async set(key, data, options = {}) {
|
|
60
|
+
await this.ensureDir();
|
|
61
|
+
const filePath = this.payloadPath(key);
|
|
62
|
+
await fs.writeFile(filePath, JSON.stringify(data), 'utf-8');
|
|
63
|
+
if (typeof options.ttl === 'number' && options.ttl > 0) {
|
|
64
|
+
const expirationTime = Date.now() + options.ttl * 1000;
|
|
65
|
+
await fs.writeFile(this.expiresPath(key), JSON.stringify(expirationTime), 'utf-8');
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// If a previous entry had an expiry, remove it so the new entry is treated as permanent.
|
|
69
|
+
await this.silentUnlink(this.expiresPath(key));
|
|
70
|
+
}
|
|
71
|
+
if (options.tags && options.tags.length > 0) {
|
|
72
|
+
await fs.writeFile(this.tagsPath(key), JSON.stringify(options.tags), 'utf-8');
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
await this.silentUnlink(this.tagsPath(key));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async delete(key) {
|
|
79
|
+
await Promise.all([
|
|
80
|
+
this.silentUnlink(this.payloadPath(key)),
|
|
81
|
+
this.silentUnlink(this.expiresPath(key)),
|
|
82
|
+
this.silentUnlink(this.tagsPath(key)),
|
|
83
|
+
]);
|
|
84
|
+
}
|
|
85
|
+
async deleteByTag(tag) {
|
|
86
|
+
const keys = await this.keys();
|
|
87
|
+
await Promise.all(keys.map(async (key) => {
|
|
88
|
+
const tags = await this.readTags(key);
|
|
89
|
+
if (tags.includes(tag)) {
|
|
90
|
+
await this.delete(key);
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
async clear() {
|
|
95
|
+
try {
|
|
96
|
+
const files = await fs.readdir(this.dir);
|
|
97
|
+
await Promise.all(files.map((file) => this.silentUnlink(path.join(this.dir, file))));
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
if (err.code !== 'ENOENT') {
|
|
101
|
+
throw err;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async keys(prefix) {
|
|
106
|
+
try {
|
|
107
|
+
const files = await fs.readdir(this.dir);
|
|
108
|
+
const keys = files
|
|
109
|
+
.filter((file) => file.endsWith(PAYLOAD_EXT))
|
|
110
|
+
.map((file) => path.basename(file, PAYLOAD_EXT));
|
|
111
|
+
return prefix ? keys.filter((k) => k.startsWith(prefix)) : keys;
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
if (err.code === 'ENOENT') {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
throw err;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
payloadPath(key) {
|
|
121
|
+
return path.join(this.dir, `${key}${PAYLOAD_EXT}`);
|
|
122
|
+
}
|
|
123
|
+
expiresPath(key) {
|
|
124
|
+
return path.join(this.dir, `${key}${EXPIRES_EXT}`);
|
|
125
|
+
}
|
|
126
|
+
tagsPath(key) {
|
|
127
|
+
return path.join(this.dir, `${key}${TAGS_EXT}`);
|
|
128
|
+
}
|
|
129
|
+
async ensureDir() {
|
|
130
|
+
await fs.mkdir(this.dir, { recursive: true });
|
|
131
|
+
}
|
|
132
|
+
async readTags(key) {
|
|
133
|
+
try {
|
|
134
|
+
const raw = await fs.readFile(this.tagsPath(key), 'utf-8');
|
|
135
|
+
const parsed = JSON.parse(raw);
|
|
136
|
+
return Array.isArray(parsed) ? parsed.filter((t) => typeof t === 'string') : [];
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async silentUnlink(filePath) {
|
|
143
|
+
try {
|
|
144
|
+
await fs.unlink(filePath);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// ENOENT and similar — ignore.
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/cache/drivers/file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,MAAM,WAAW,GAAG,OAAO,CAAC;AAC5B,MAAM,WAAW,GAAG,UAAU,CAAC;AAC/B,MAAM,QAAQ,GAAG,OAAO,CAAC;AAQzB,uCAAuC;AACvC,MAAM,OAAO,eAAe;IACT,GAAG,CAAS;IAE7B,YAAY,OAA0B;QACpC,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,GAAG,CAAI,GAAW;QACtB,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACvC,MAAM,WAAW,GAAG,IAAI,CAAC,WAA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O,CAAC,CAAC;YAC3D,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACxC,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/F,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,YAAY,CAAC,QAAgB;QACzC,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,+BAA+B;QACjC,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory cache driver.
|
|
3
|
+
*
|
|
4
|
+
* Intended for development, testing, and short-lived serverless functions
|
|
5
|
+
* where process restarts naturally evict the cache. Not suitable for
|
|
6
|
+
* multi-instance deployments — use the redis driver for that.
|
|
7
|
+
*/
|
|
8
|
+
import type { CacheDriver, CacheSetOptions } from '../types.js';
|
|
9
|
+
/** Map-based in-memory `CacheDriver`. */
|
|
10
|
+
export declare class MemoryCacheDriver implements CacheDriver {
|
|
11
|
+
private readonly store;
|
|
12
|
+
private readonly tagIndex;
|
|
13
|
+
get<T>(key: string): Promise<T | null>;
|
|
14
|
+
set<T>(key: string, data: T, options?: CacheSetOptions): Promise<void>;
|
|
15
|
+
delete(key: string): Promise<void>;
|
|
16
|
+
deleteByTag(tag: string): Promise<void>;
|
|
17
|
+
clear(): Promise<void>;
|
|
18
|
+
keys(prefix?: string): Promise<string[]>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/cache/drivers/memory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAWhE,yCAAyC;AACzC,qBAAa,iBAAkB,YAAW,WAAW;IACnD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkC;IACxD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkC;IAErD,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAYtC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAyB1E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBlC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUvC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAKtB,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAI/C"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory cache driver.
|
|
3
|
+
*
|
|
4
|
+
* Intended for development, testing, and short-lived serverless functions
|
|
5
|
+
* where process restarts naturally evict the cache. Not suitable for
|
|
6
|
+
* multi-instance deployments — use the redis driver for that.
|
|
7
|
+
*/
|
|
8
|
+
/** Map-based in-memory `CacheDriver`. */
|
|
9
|
+
export class MemoryCacheDriver {
|
|
10
|
+
store = new Map();
|
|
11
|
+
tagIndex = new Map();
|
|
12
|
+
async get(key) {
|
|
13
|
+
const entry = this.store.get(key);
|
|
14
|
+
if (!entry)
|
|
15
|
+
return null;
|
|
16
|
+
if (entry.expiresAt !== null && Date.now() > entry.expiresAt) {
|
|
17
|
+
await this.delete(key);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return entry.data;
|
|
21
|
+
}
|
|
22
|
+
async set(key, data, options = {}) {
|
|
23
|
+
// Detach old tags first so reassignments don't leak.
|
|
24
|
+
const previous = this.store.get(key);
|
|
25
|
+
if (previous) {
|
|
26
|
+
for (const tag of previous.tags) {
|
|
27
|
+
this.tagIndex.get(tag)?.delete(key);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const expiresAt = typeof options.ttl === 'number' && options.ttl > 0 ? Date.now() + options.ttl * 1000 : null;
|
|
31
|
+
const tags = new Set(options.tags ?? []);
|
|
32
|
+
this.store.set(key, { data, expiresAt, tags });
|
|
33
|
+
for (const tag of tags) {
|
|
34
|
+
let bucket = this.tagIndex.get(tag);
|
|
35
|
+
if (!bucket) {
|
|
36
|
+
bucket = new Set();
|
|
37
|
+
this.tagIndex.set(tag, bucket);
|
|
38
|
+
}
|
|
39
|
+
bucket.add(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async delete(key) {
|
|
43
|
+
const entry = this.store.get(key);
|
|
44
|
+
if (!entry)
|
|
45
|
+
return;
|
|
46
|
+
for (const tag of entry.tags) {
|
|
47
|
+
const bucket = this.tagIndex.get(tag);
|
|
48
|
+
if (bucket) {
|
|
49
|
+
bucket.delete(key);
|
|
50
|
+
if (bucket.size === 0) {
|
|
51
|
+
this.tagIndex.delete(tag);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
this.store.delete(key);
|
|
56
|
+
}
|
|
57
|
+
async deleteByTag(tag) {
|
|
58
|
+
const bucket = this.tagIndex.get(tag);
|
|
59
|
+
if (!bucket)
|
|
60
|
+
return;
|
|
61
|
+
// Materialize before iterating — delete() mutates the bucket.
|
|
62
|
+
const keys = Array.from(bucket);
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
await this.delete(key);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async clear() {
|
|
68
|
+
this.store.clear();
|
|
69
|
+
this.tagIndex.clear();
|
|
70
|
+
}
|
|
71
|
+
async keys(prefix) {
|
|
72
|
+
const keys = Array.from(this.store.keys());
|
|
73
|
+
return prefix ? keys.filter((k) => k.startsWith(prefix)) : keys;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../src/cache/drivers/memory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAaH,yCAAyC;AACzC,MAAM,OAAO,iBAAiB;IACX,KAAK,GAAG,IAAI,GAAG,EAAuB,CAAC;IACvC,QAAQ,GAAG,IAAI,GAAG,EAAuB,CAAC;IAE3D,KAAK,CAAC,GAAG,CAAI,GAAW;QACtB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,KAAK;YAAE,OAAO,IAAI,CAAC;QAExB,IAAI,KAAK,CAAC,SAAS,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;YAC7D,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACvB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,KAAK,CAAC,IAAS,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,GAAG,CAAI,GAAW,EAAE,IAAO,EAAE,UAA2B,EAAE;QAC9D,qDAAqD;QACrD,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,QAAQ,EAAE,CAAC;YACb,KAAK,MAAM,GAAG,IAAI,QAAQ,CAAC,IAAI,EAAE,CAAC;gBAChC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC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