@krate/runtime 0.5.0-alpha.4 → 0.5.0-beta.2
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/dist/action-context.d.ts +5 -1
- package/dist/action-context.js +33 -3
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +84 -4
- package/dist/actions.test.js +61 -1
- package/dist/assets-static.d.ts +5 -0
- package/dist/assets-static.js +31 -0
- package/dist/boundary.d.ts +12 -0
- package/dist/boundary.js +53 -13
- package/dist/cache-adapter.d.ts +113 -0
- package/dist/cache-adapter.js +217 -0
- package/dist/cache-adapter.test.d.ts +1 -0
- package/dist/cache-adapter.test.js +244 -0
- package/dist/cache-redis.d.ts +27 -0
- package/dist/cache-redis.js +110 -0
- package/dist/cache-scope.d.ts +25 -9
- package/dist/cache-scope.js +64 -13
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +3 -3
- package/dist/cache.test.js +16 -0
- package/dist/class-component.js +6 -0
- package/dist/class-component.test.js +53 -2
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +85 -12
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +15 -0
- package/dist/dom.js +97 -3
- package/dist/effect-errors.test.d.ts +1 -0
- package/dist/effect-errors.test.js +163 -0
- package/dist/effects.d.ts +8 -0
- package/dist/effects.js +13 -0
- package/dist/error-overlay.js +7 -7
- package/dist/handler-html.d.ts +63 -0
- package/dist/handler-html.js +285 -0
- package/dist/handler-html.test.d.ts +1 -0
- package/dist/handler-html.test.js +256 -0
- package/dist/handler-routing.d.ts +83 -0
- package/dist/handler-routing.js +579 -0
- package/dist/handler-routing.test.d.ts +1 -0
- package/dist/handler-routing.test.js +99 -0
- package/dist/handler-segments.d.ts +43 -0
- package/dist/handler-segments.js +115 -0
- package/dist/handler-segments.test.d.ts +1 -0
- package/dist/handler-segments.test.js +217 -0
- package/dist/head-tags.d.ts +51 -0
- package/dist/head-tags.js +329 -0
- package/dist/head-tags.test.d.ts +1 -0
- package/dist/head-tags.test.js +131 -0
- package/dist/highlight-bind.d.ts +42 -0
- package/dist/highlight-bind.js +176 -0
- package/dist/highlight-bind.test.d.ts +1 -0
- package/dist/highlight-bind.test.js +124 -0
- package/dist/highlight.d.ts +93 -0
- package/dist/highlight.js +426 -0
- package/dist/highlight.test.d.ts +1 -0
- package/dist/highlight.test.js +56 -0
- package/dist/hints.d.ts +19 -0
- package/dist/hints.js +97 -0
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/jsx-types.d.ts +11 -0
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +75 -12
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-highlight.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-navigation.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +493 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/list.js +4 -2
- package/dist/metadata-routes.d.ts +75 -0
- package/dist/metadata-routes.js +76 -0
- package/dist/metadata.d.ts +222 -0
- package/dist/metadata.js +452 -0
- package/dist/metadata.test.d.ts +1 -0
- package/dist/metadata.test.js +167 -0
- package/dist/navigation-server.d.ts +27 -0
- package/dist/navigation-server.js +90 -0
- package/dist/navigation.d.ts +37 -0
- package/dist/navigation.js +195 -0
- package/dist/navigation.test.d.ts +1 -0
- package/dist/navigation.test.js +130 -0
- package/dist/node-server.d.ts +22 -0
- package/dist/node-server.js +436 -0
- package/dist/node-server.test.d.ts +1 -0
- package/dist/node-server.test.js +492 -0
- package/dist/og-files.d.ts +2 -0
- package/dist/og-files.js +19 -0
- package/dist/og.d.ts +56 -0
- package/dist/og.js +156 -0
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/react.d.ts +47 -0
- package/dist/react.js +59 -1
- package/dist/react19.test.d.ts +1 -0
- package/dist/react19.test.js +176 -0
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/request-context.d.ts +23 -0
- package/dist/request-context.js +45 -0
- package/dist/resp.d.ts +55 -0
- package/dist/resp.js +239 -0
- package/dist/router-layout.test.js +70 -0
- package/dist/router-nav.test.js +65 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.js +175 -36
- package/dist/segmented-lru.d.ts +19 -0
- package/dist/segmented-lru.js +87 -0
- package/dist/server-async.test.js +43 -1
- package/dist/server-handler.d.ts +165 -0
- package/dist/server-handler.js +1826 -0
- package/dist/server-handler.test.d.ts +1 -0
- package/dist/server-handler.test.js +522 -0
- package/dist/server-highlight.test.d.ts +1 -0
- package/dist/server-highlight.test.js +32 -0
- package/dist/server-renderer.js +165 -720
- package/dist/server-renderer.test.d.ts +1 -0
- package/dist/server-renderer.test.js +266 -0
- package/dist/server.d.ts +13 -1
- package/dist/server.js +131 -16
- package/dist/server.test.js +20 -0
- package/dist/sha256.d.ts +1 -0
- package/dist/sha256.js +66 -0
- package/dist/sha256.test.d.ts +1 -0
- package/dist/sha256.test.js +13 -0
- package/dist/shared-isr.d.ts +31 -0
- package/dist/shared-isr.js +142 -0
- package/dist/signal.d.ts +3 -2
- package/dist/signal.js +163 -23
- package/dist/slots.js +12 -1
- package/dist/ssr-core-behavior.test.d.ts +1 -0
- package/dist/ssr-core-behavior.test.js +620 -0
- package/dist/ssr-core.d.ts +175 -0
- package/dist/ssr-core.js +940 -0
- package/dist/ssr-core.test.d.ts +1 -0
- package/dist/ssr-core.test.js +324 -0
- package/dist/static-memory.d.ts +3 -0
- package/dist/static-memory.js +47 -0
- package/dist/static-memory.test.d.ts +1 -0
- package/dist/static-memory.test.js +26 -0
- package/dist/test-redis.d.ts +12 -0
- package/dist/test-redis.js +131 -0
- package/dist/use-id.d.ts +8 -0
- package/dist/use-id.js +40 -0
- package/package.json +30 -2
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// Pluggable shared cache.
|
|
2
|
+
//
|
|
3
|
+
// By default every cache lives in the memory of one process. Setting
|
|
4
|
+
// `cache: { adapter: "redis", url }` in krate.config (or passing an adapter to
|
|
5
|
+
// createKrateHandler) moves the ISR page cache and the data cache (fetch with
|
|
6
|
+
// next options, unstable_cache) into a store every instance reads and writes, and
|
|
7
|
+
// makes revalidatePath / revalidateTag on one instance invalidate all of them.
|
|
8
|
+
//
|
|
9
|
+
// An adapter is a small key-value store with tag deletion and pub/sub. Write
|
|
10
|
+
// your own with the CacheAdapter interface below and point `cache.adapter` at a
|
|
11
|
+
// module that default-exports `(options) => CacheAdapter` (see the caching
|
|
12
|
+
// guide).
|
|
13
|
+
//
|
|
14
|
+
// Whatever the adapter does, a cache problem is never a request problem: every
|
|
15
|
+
// call is bounded by a timeout, failures are logged once, an unhealthy adapter
|
|
16
|
+
// is skipped for a while, and the page is rendered instead.
|
|
17
|
+
import { cacheRuntime } from './cache-scope.js';
|
|
18
|
+
export const INVALIDATE_CHANNEL = 'invalidate';
|
|
19
|
+
/** The adapter tag a cache tag is stored under (kept apart from route index tags). */
|
|
20
|
+
export function tagName(tag) {
|
|
21
|
+
return 'tag:' + tag;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs adapter calls with a timeout and a circuit breaker. After a few
|
|
25
|
+
* consecutive failures calls are skipped (reported as failed) for a cool-down,
|
|
26
|
+
* so a dead cache costs a microsecond per request, not a timeout.
|
|
27
|
+
*/
|
|
28
|
+
export class CacheGuard {
|
|
29
|
+
constructor(adapter, timeoutMs = 250, threshold = 3, coolDownMs = 5000) {
|
|
30
|
+
this.adapter = adapter;
|
|
31
|
+
this.timeoutMs = timeoutMs;
|
|
32
|
+
this.threshold = threshold;
|
|
33
|
+
this.coolDownMs = coolDownMs;
|
|
34
|
+
this.failures = 0;
|
|
35
|
+
this.openUntil = 0;
|
|
36
|
+
this.lastLog = 0;
|
|
37
|
+
}
|
|
38
|
+
get healthy() {
|
|
39
|
+
return Date.now() >= this.openUntil;
|
|
40
|
+
}
|
|
41
|
+
async call(fn) {
|
|
42
|
+
if (!this.healthy)
|
|
43
|
+
return { ok: false };
|
|
44
|
+
let timer;
|
|
45
|
+
try {
|
|
46
|
+
const value = await Promise.race([
|
|
47
|
+
fn(this.adapter),
|
|
48
|
+
new Promise((_, reject) => {
|
|
49
|
+
timer = setTimeout(() => reject(new Error(`cache ${this.adapter.name} did not answer within ${this.timeoutMs}ms`)), this.timeoutMs);
|
|
50
|
+
}),
|
|
51
|
+
]);
|
|
52
|
+
this.failures = 0;
|
|
53
|
+
return { ok: true, value };
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
this.failures++;
|
|
57
|
+
if (this.failures >= this.threshold)
|
|
58
|
+
this.openUntil = Date.now() + this.coolDownMs;
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
if (now - this.lastLog > 30000) {
|
|
61
|
+
this.lastLog = now;
|
|
62
|
+
console.warn(`[krate] shared cache (${this.adapter.name}) failed, serving without it: ${err?.message || err}`);
|
|
63
|
+
}
|
|
64
|
+
return { ok: false };
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
if (timer)
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Shared data cache (fetch with next options, unstable_cache)
|
|
73
|
+
const DATA_PREFIX = 'data:';
|
|
74
|
+
export class SharedDataCache {
|
|
75
|
+
constructor(guard) {
|
|
76
|
+
this.guard = guard;
|
|
77
|
+
}
|
|
78
|
+
async get(key) {
|
|
79
|
+
const r = await this.guard.call((a) => a.get(DATA_PREFIX + key));
|
|
80
|
+
if (!r.ok)
|
|
81
|
+
return r;
|
|
82
|
+
return { ok: true, value: r.value === null ? undefined : r.value };
|
|
83
|
+
}
|
|
84
|
+
async set(key, json, ttlSeconds, tags) {
|
|
85
|
+
const r = await this.guard.call((a) => a.set(DATA_PREFIX + key, json, { ttlSeconds: ttlSeconds === Infinity ? undefined : ttlSeconds, tags: tags.map(tagName) }));
|
|
86
|
+
return r.ok;
|
|
87
|
+
}
|
|
88
|
+
async deleteTags(tags) {
|
|
89
|
+
const r = await this.guard.call((a) => a.deleteByTags(tags.map(tagName)));
|
|
90
|
+
return r.ok ? r.value : 0;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// In-process adapter (tests, and the reference for adapter authors)
|
|
94
|
+
/** A CacheAdapter over a Map. Two processes cannot share it; it exists for tests and as the reference. */
|
|
95
|
+
export class MemoryAdapter {
|
|
96
|
+
constructor() {
|
|
97
|
+
this.name = 'memory';
|
|
98
|
+
this.values = new Map();
|
|
99
|
+
this.byTag = new Map();
|
|
100
|
+
this.subs = new Map();
|
|
101
|
+
}
|
|
102
|
+
async get(key) {
|
|
103
|
+
const e = this.values.get(key);
|
|
104
|
+
if (!e)
|
|
105
|
+
return null;
|
|
106
|
+
if (e.expires <= Date.now()) {
|
|
107
|
+
await this.delete(key);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return e.value;
|
|
111
|
+
}
|
|
112
|
+
async set(key, value, options = {}) {
|
|
113
|
+
await this.delete(key);
|
|
114
|
+
const ttl = options.ttlSeconds;
|
|
115
|
+
const tags = options.tags || [];
|
|
116
|
+
this.values.set(key, { value, expires: ttl && ttl > 0 && ttl !== Infinity ? Date.now() + ttl * 1000 : Infinity, tags });
|
|
117
|
+
for (const t of tags) {
|
|
118
|
+
let keys = this.byTag.get(t);
|
|
119
|
+
if (!keys)
|
|
120
|
+
this.byTag.set(t, (keys = new Set()));
|
|
121
|
+
keys.add(key);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async delete(key) {
|
|
125
|
+
const e = this.values.get(key);
|
|
126
|
+
if (!e)
|
|
127
|
+
return;
|
|
128
|
+
this.values.delete(key);
|
|
129
|
+
for (const t of e.tags)
|
|
130
|
+
this.byTag.get(t)?.delete(key);
|
|
131
|
+
}
|
|
132
|
+
async keysByTag(tag) {
|
|
133
|
+
return [...(this.byTag.get(tag) || [])];
|
|
134
|
+
}
|
|
135
|
+
async deleteByTags(tags) {
|
|
136
|
+
let n = 0;
|
|
137
|
+
for (const t of tags) {
|
|
138
|
+
for (const k of [...(this.byTag.get(t) || [])]) {
|
|
139
|
+
if (this.values.has(k))
|
|
140
|
+
n++;
|
|
141
|
+
await this.delete(k);
|
|
142
|
+
}
|
|
143
|
+
this.byTag.delete(t);
|
|
144
|
+
}
|
|
145
|
+
return n;
|
|
146
|
+
}
|
|
147
|
+
async publish(channel, message) {
|
|
148
|
+
for (const h of [...(this.subs.get(channel) || [])])
|
|
149
|
+
h(message);
|
|
150
|
+
}
|
|
151
|
+
async subscribe(channel, handler) {
|
|
152
|
+
let set = this.subs.get(channel);
|
|
153
|
+
if (!set)
|
|
154
|
+
this.subs.set(channel, (set = new Set()));
|
|
155
|
+
set.add(handler);
|
|
156
|
+
return async () => void set.delete(handler);
|
|
157
|
+
}
|
|
158
|
+
async close() {
|
|
159
|
+
this.values.clear();
|
|
160
|
+
this.byTag.clear();
|
|
161
|
+
this.subs.clear();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** Resolves a spec to an adapter, or null for the default in-process caches. */
|
|
165
|
+
export async function loadCacheAdapter(spec) {
|
|
166
|
+
if (!spec || !spec.adapter || spec.adapter === 'memory')
|
|
167
|
+
return null;
|
|
168
|
+
const opts = { ...(spec.options || {}), url: spec.url, prefix: spec.prefix, timeoutMs: spec.timeoutMs };
|
|
169
|
+
if (spec.adapter === 'redis') {
|
|
170
|
+
const { RedisAdapter } = await import('./cache-redis.js');
|
|
171
|
+
if (!spec.url)
|
|
172
|
+
throw new Error('cache.adapter "redis" needs cache.url (redis://host:6379)');
|
|
173
|
+
return new RedisAdapter(opts);
|
|
174
|
+
}
|
|
175
|
+
const mod = (await import(spec.adapter));
|
|
176
|
+
const factory = (mod.default ?? mod.createAdapter);
|
|
177
|
+
if (typeof factory === 'function')
|
|
178
|
+
return factory(opts);
|
|
179
|
+
if (factory && typeof factory.get === 'function')
|
|
180
|
+
return factory;
|
|
181
|
+
throw new Error(`cache adapter module ${spec.adapter} must default-export a CacheAdapter or a (options) => CacheAdapter factory`);
|
|
182
|
+
}
|
|
183
|
+
/** Reads the cache spec the Go host passes in KRATE_CACHE. */
|
|
184
|
+
export function cacheSpecFromEnv(env) {
|
|
185
|
+
const raw = env.KRATE_CACHE;
|
|
186
|
+
if (!raw)
|
|
187
|
+
return undefined;
|
|
188
|
+
try {
|
|
189
|
+
return JSON.parse(raw);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
console.warn('[krate] KRATE_CACHE is not valid JSON; using the in-process caches');
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Wires an adapter into the process: the data cache reads and writes through it
|
|
198
|
+
* (cacheRuntime().shared), and the returned stores replace the in-process ISR
|
|
199
|
+
* cache. Pass the returned `isr` as the renderer's isrStore.
|
|
200
|
+
*/
|
|
201
|
+
export function installCacheAdapter(adapter, spec = {}) {
|
|
202
|
+
const guard = new CacheGuard(adapter, spec.timeoutMs && spec.timeoutMs > 0 ? spec.timeoutMs : 250);
|
|
203
|
+
const data = new SharedDataCache(guard);
|
|
204
|
+
cacheRuntime().shared = data;
|
|
205
|
+
return { adapter, guard, data };
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The cache spec a handler bundle carries (data.config.cache): the adapter and
|
|
209
|
+
* its tuning, with the URL left to the environment so no secret is bundled.
|
|
210
|
+
*/
|
|
211
|
+
export function cacheSpecFromConfig(cfg) {
|
|
212
|
+
if (!cfg || !cfg.adapter || cfg.adapter === 'memory')
|
|
213
|
+
return undefined;
|
|
214
|
+
const env = globalThis.process?.env || {};
|
|
215
|
+
const url = cfg.url || (cfg.urlEnv ? env[cfg.urlEnv] : undefined) || env.KRATE_CACHE_URL;
|
|
216
|
+
return { ...cfg, url };
|
|
217
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { CacheGuard, MemoryAdapter, SharedDataCache, cacheSpecFromConfig, cacheSpecFromEnv, installCacheAdapter, loadCacheAdapter } from './cache-adapter';
|
|
5
|
+
import { RedisAdapter } from './cache-redis';
|
|
6
|
+
import { SharedIsrStore, isrStoreFor } from './shared-isr';
|
|
7
|
+
import { createSSRCore, MemoryIsrStore } from './ssr-core';
|
|
8
|
+
import { cacheRuntime, installRenderStore, markNoStore, patchFetch } from './cache-scope';
|
|
9
|
+
import { parseRedisURL } from './resp';
|
|
10
|
+
import { startFakeRedis } from './test-redis';
|
|
11
|
+
import { jsx } from './server';
|
|
12
|
+
installRenderStore(new AsyncLocalStorage());
|
|
13
|
+
let redis;
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
redis = await startFakeRedis();
|
|
16
|
+
vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
17
|
+
vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
18
|
+
});
|
|
19
|
+
afterEach(async () => {
|
|
20
|
+
vi.restoreAllMocks();
|
|
21
|
+
await redis.kill();
|
|
22
|
+
cacheRuntime().shared = undefined;
|
|
23
|
+
});
|
|
24
|
+
const url = () => `redis://127.0.0.1:${redis.port}/0`;
|
|
25
|
+
const adapter = () => new RedisAdapter({ url: url(), timeoutMs: 200 });
|
|
26
|
+
describe('parseRedisURL', () => {
|
|
27
|
+
it('reads host, port, credentials, db and TLS', () => {
|
|
28
|
+
expect(parseRedisURL('redis://u:p%40ss@cache.example:6380/2')).toMatchObject({ host: 'cache.example', port: 6380, username: 'u', password: 'p@ss', db: 2, tls: false });
|
|
29
|
+
expect(parseRedisURL('rediss://cache.example')).toMatchObject({ port: 6379, tls: true });
|
|
30
|
+
expect(() => parseRedisURL('http://x')).toThrow(/protocol/);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe('RedisAdapter', () => {
|
|
34
|
+
it('stores values with a ttl and tags, and deletes by tag', async () => {
|
|
35
|
+
const a = adapter();
|
|
36
|
+
await a.set('one', '1', { ttlSeconds: 30, tags: ['t:a', 't:b'] });
|
|
37
|
+
await a.set('two', '2', { tags: ['t:a'] });
|
|
38
|
+
expect(await a.get('one')).toBe('1');
|
|
39
|
+
expect(await a.get('missing')).toBeNull();
|
|
40
|
+
expect((await a.keysByTag('t:a')).sort()).toEqual(['one', 'two']);
|
|
41
|
+
expect(await a.deleteByTags(['t:a'])).toBe(2);
|
|
42
|
+
expect(await a.get('one')).toBeNull();
|
|
43
|
+
expect(await a.get('two')).toBeNull();
|
|
44
|
+
expect(redis.commands.some((c) => c[0] === 'SET' && c[3] === 'EX' && c[4] === '30')).toBe(true);
|
|
45
|
+
await a.close();
|
|
46
|
+
});
|
|
47
|
+
it('keeps apps apart with a prefix', async () => {
|
|
48
|
+
const a = new RedisAdapter({ url: url(), prefix: 'one:' });
|
|
49
|
+
const b = new RedisAdapter({ url: url(), prefix: 'two:' });
|
|
50
|
+
await a.set('k', 'A');
|
|
51
|
+
expect(await b.get('k')).toBeNull();
|
|
52
|
+
expect(redis.keys()).toEqual(['one:k:k']);
|
|
53
|
+
await a.close();
|
|
54
|
+
await b.close();
|
|
55
|
+
});
|
|
56
|
+
it('authenticates', async () => {
|
|
57
|
+
const guarded = await startFakeRedis({ password: 's3cret' });
|
|
58
|
+
try {
|
|
59
|
+
const ok = new RedisAdapter({ url: `redis://:s3cret@127.0.0.1:${guarded.port}` });
|
|
60
|
+
await ok.set('k', 'v');
|
|
61
|
+
expect(await ok.get('k')).toBe('v');
|
|
62
|
+
const bad = new RedisAdapter({ url: `redis://:nope@127.0.0.1:${guarded.port}` });
|
|
63
|
+
await expect(bad.get('k')).rejects.toThrow(/WRONGPASS/);
|
|
64
|
+
await ok.close();
|
|
65
|
+
await bad.close();
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
await guarded.kill();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
it('delivers published messages to subscribers on other connections', async () => {
|
|
72
|
+
const a = adapter();
|
|
73
|
+
const b = adapter();
|
|
74
|
+
const got = [];
|
|
75
|
+
const stop = await b.subscribe('invalidate', (m) => got.push(m));
|
|
76
|
+
await a.publish('invalidate', '{"tags":["x"]}');
|
|
77
|
+
await vi.waitFor(() => expect(got).toEqual(['{"tags":["x"]}']));
|
|
78
|
+
stop();
|
|
79
|
+
await a.close();
|
|
80
|
+
await b.close();
|
|
81
|
+
});
|
|
82
|
+
it('tells a subscriber to flush everything after its connection was lost', async () => {
|
|
83
|
+
const a = adapter();
|
|
84
|
+
const got = [];
|
|
85
|
+
await a.subscribe('invalidate', (m) => got.push(m));
|
|
86
|
+
await redis.kill();
|
|
87
|
+
await redis.revive();
|
|
88
|
+
await vi.waitFor(() => expect(got).toContain('*'), { timeout: 4000 });
|
|
89
|
+
await a.close();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
describe('SharedIsrStore across instances', () => {
|
|
93
|
+
const entry = (html, tags) => ({ html, timestamp: Date.now(), tags });
|
|
94
|
+
it('serves what another instance stored and invalidates for both', async () => {
|
|
95
|
+
const a = new SharedIsrStore(new CacheGuard(adapter()));
|
|
96
|
+
const b = new SharedIsrStore(new CacheGuard(adapter()));
|
|
97
|
+
await a.set('/p/[id]?id=1', entry('<p>1</p>', ['posts']));
|
|
98
|
+
await a.set('/p/[id]?id=2', entry('<p>2</p>', ['users']));
|
|
99
|
+
expect((await b.get('/p/[id]?id=1'))?.html).toBe('<p>1</p>');
|
|
100
|
+
expect(await b.deleteTags(['posts'])).toBeGreaterThan(0);
|
|
101
|
+
expect(await a.get('/p/[id]?id=1')).toBeUndefined();
|
|
102
|
+
expect((await a.get('/p/[id]?id=2'))?.html).toBe('<p>2</p>');
|
|
103
|
+
expect(await b.deleteRoute('/p/[id]')).toBeGreaterThan(0);
|
|
104
|
+
expect(await a.get('/p/[id]?id=2')).toBeUndefined();
|
|
105
|
+
});
|
|
106
|
+
it('lists the variants of a route and deletes one params variant', async () => {
|
|
107
|
+
const s = new SharedIsrStore(new CacheGuard(adapter()));
|
|
108
|
+
await s.set('/p/[id]?id=1', entry('1'));
|
|
109
|
+
await s.set('/p/[id]?id=1#q=a', entry('1a'));
|
|
110
|
+
await s.set('/p/[id]?id=2', entry('2'));
|
|
111
|
+
expect((await s.entriesForRoute('/p/[id]')).map(([k]) => k).sort()).toEqual(['/p/[id]?id=1', '/p/[id]?id=1#q=a', '/p/[id]?id=2']);
|
|
112
|
+
await s.deleteVariant('/p/[id]', { id: '1' });
|
|
113
|
+
expect((await s.entriesForRoute('/p/[id]')).map(([k]) => k)).toEqual(['/p/[id]?id=2']);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('a cache that fails never fails a request', () => {
|
|
117
|
+
const page = (route, extra = {}) => ({ route, source: route + '.tsx', mode: 'isr', bundlePath: route, revalidate: 60, ...extra });
|
|
118
|
+
const core = (store, render) => createSSRCore({
|
|
119
|
+
manifest: { pages: [page('/p')], regions: {} },
|
|
120
|
+
isrStore: store,
|
|
121
|
+
loadPageModule: async () => ({ default: () => jsx('p', { children: render() }) }),
|
|
122
|
+
loadRuntimeRenderer: () => () => '',
|
|
123
|
+
});
|
|
124
|
+
it('renders when Redis is down, falls back to process memory, and recovers', async () => {
|
|
125
|
+
let n = 0;
|
|
126
|
+
const guard = new CacheGuard(adapter(), 100, 2, 300);
|
|
127
|
+
const c = core(new SharedIsrStore(guard), () => `render ${++n}`);
|
|
128
|
+
const req = { route: '/p', url: '/p', method: 'GET', headers: {} };
|
|
129
|
+
expect((await c.renderPage(req)).cacheStatus).toBe('miss');
|
|
130
|
+
expect((await c.renderPage(req)).cacheStatus).toBe('hit');
|
|
131
|
+
await redis.kill();
|
|
132
|
+
// Down: the page still renders (and is cached in memory for this instance).
|
|
133
|
+
const down = await c.renderPage(req);
|
|
134
|
+
expect(down.status).toBe(200);
|
|
135
|
+
expect(down.html).toContain('<p>');
|
|
136
|
+
for (let i = 0; i < 5; i++)
|
|
137
|
+
expect((await c.renderPage(req)).status).toBe(200);
|
|
138
|
+
expect(guard.healthy).toBe(false);
|
|
139
|
+
await redis.revive();
|
|
140
|
+
await vi.waitFor(async () => expect((await c.renderPage(req)).status).toBe(200));
|
|
141
|
+
await new Promise((r) => setTimeout(r, 350));
|
|
142
|
+
expect((await c.renderPage(req)).status).toBe(200);
|
|
143
|
+
expect(guard.healthy).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
it('gives up on a cache that hangs after the timeout instead of waiting', async () => {
|
|
146
|
+
const never = () => new Promise(() => { });
|
|
147
|
+
const hang = {
|
|
148
|
+
name: 'hang',
|
|
149
|
+
get: () => never(),
|
|
150
|
+
set: () => never(),
|
|
151
|
+
delete: () => never(),
|
|
152
|
+
keysByTag: () => never(),
|
|
153
|
+
deleteByTags: () => never(),
|
|
154
|
+
publish: () => never(),
|
|
155
|
+
subscribe: () => never(),
|
|
156
|
+
close: async () => { },
|
|
157
|
+
};
|
|
158
|
+
const c = core(new SharedIsrStore(new CacheGuard(hang, 50)), () => 'x');
|
|
159
|
+
const started = Date.now();
|
|
160
|
+
const r = await c.renderPage({ route: '/p', url: '/p', method: 'GET', headers: {} });
|
|
161
|
+
expect(r.status).toBe(200);
|
|
162
|
+
expect(Date.now() - started).toBeLessThan(1500);
|
|
163
|
+
});
|
|
164
|
+
it('never stores a render that read per-user state', async () => {
|
|
165
|
+
const store = new SharedIsrStore(new CacheGuard(adapter()));
|
|
166
|
+
const c = createSSRCore({
|
|
167
|
+
manifest: { pages: [page('/me')], regions: {} },
|
|
168
|
+
isrStore: store,
|
|
169
|
+
loadPageModule: async () => ({
|
|
170
|
+
default: () => {
|
|
171
|
+
markNoStore(); // cookies(), headers() and no-store fetches do this
|
|
172
|
+
return jsx('p', { children: 'Hello Ada' });
|
|
173
|
+
},
|
|
174
|
+
}),
|
|
175
|
+
loadRuntimeRenderer: () => () => '',
|
|
176
|
+
});
|
|
177
|
+
const r = await c.renderPage({ route: '/me', url: '/me', method: 'GET', headers: { cookie: 'sid=ada' } });
|
|
178
|
+
expect(r.noStore).toBe(true);
|
|
179
|
+
expect(redis.keys().filter((k) => k.includes('isr:'))).toEqual([]);
|
|
180
|
+
expect(await store.get('/me')).toBeUndefined();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
describe('the shared data cache', () => {
|
|
184
|
+
it('shares fetch responses between instances and invalidates them by tag', async () => {
|
|
185
|
+
const shared = new SharedDataCache(new CacheGuard(adapter()));
|
|
186
|
+
cacheRuntime().shared = shared;
|
|
187
|
+
let upstream = 0;
|
|
188
|
+
vi.stubGlobal('fetch', vi.fn(async () => new Response(`body-${++upstream}`)));
|
|
189
|
+
cacheRuntime().fetchPatched = false;
|
|
190
|
+
patchFetch();
|
|
191
|
+
const read = async () => (await fetch('http://data/x', { next: { tags: ['x'], revalidate: 60 } })).text();
|
|
192
|
+
expect(await read()).toBe('body-1');
|
|
193
|
+
// Another instance (a fresh data store on the same Redis) sees the entry.
|
|
194
|
+
const other = new SharedDataCache(new CacheGuard(adapter()));
|
|
195
|
+
const hit = await other.get('fetch:' + JSON.stringify(['GET', 'http://data/x', []]));
|
|
196
|
+
expect(hit.ok && hit.value).toBeTruthy();
|
|
197
|
+
expect(await read()).toBe('body-1');
|
|
198
|
+
expect(await other.deleteTags(['x'])).toBeGreaterThan(0);
|
|
199
|
+
expect(await read()).toBe('body-2');
|
|
200
|
+
vi.unstubAllGlobals();
|
|
201
|
+
cacheRuntime().fetchPatched = false;
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
describe('configuration', () => {
|
|
205
|
+
it('loads the default (no adapter), redis, and a custom module', async () => {
|
|
206
|
+
expect(await loadCacheAdapter(undefined)).toBeNull();
|
|
207
|
+
expect(await loadCacheAdapter({ adapter: 'memory' })).toBeNull();
|
|
208
|
+
const r = await loadCacheAdapter({ adapter: 'redis', url: url() });
|
|
209
|
+
expect(r?.name).toBe('redis');
|
|
210
|
+
await r?.close();
|
|
211
|
+
await expect(loadCacheAdapter({ adapter: 'redis' })).rejects.toThrow(/cache\.url/);
|
|
212
|
+
});
|
|
213
|
+
it('reads the spec from KRATE_CACHE and from the bundled config', () => {
|
|
214
|
+
expect(cacheSpecFromEnv({ KRATE_CACHE: '{"adapter":"redis","url":"redis://h"}' })).toEqual({ adapter: 'redis', url: 'redis://h' });
|
|
215
|
+
expect(cacheSpecFromEnv({ KRATE_CACHE: 'not json' })).toBeUndefined();
|
|
216
|
+
expect(cacheSpecFromEnv({})).toBeUndefined();
|
|
217
|
+
const saved = process.env.MY_REDIS;
|
|
218
|
+
process.env.MY_REDIS = 'redis://from-env';
|
|
219
|
+
expect(cacheSpecFromConfig({ adapter: 'redis', urlEnv: 'MY_REDIS' })?.url).toBe('redis://from-env');
|
|
220
|
+
expect(cacheSpecFromConfig({ adapter: 'memory' })).toBeUndefined();
|
|
221
|
+
if (saved === undefined)
|
|
222
|
+
delete process.env.MY_REDIS;
|
|
223
|
+
else
|
|
224
|
+
process.env.MY_REDIS = saved;
|
|
225
|
+
});
|
|
226
|
+
it('keeps the fallback store for a spec that fails to start', async () => {
|
|
227
|
+
const fallback = new MemoryIsrStore(4);
|
|
228
|
+
const setup = isrStoreFor({ adapter: 'redis' }, fallback);
|
|
229
|
+
expect(await setup.ready).toBeNull();
|
|
230
|
+
await setup.store.set('/a', { html: 'a', timestamp: 1 });
|
|
231
|
+
expect((await setup.store.get('/a'))?.html).toBe('a');
|
|
232
|
+
});
|
|
233
|
+
it('accepts an adapter instance (serverless targets plug in their own)', async () => {
|
|
234
|
+
const mem = new MemoryAdapter();
|
|
235
|
+
const setup = isrStoreFor(mem, new MemoryIsrStore(4));
|
|
236
|
+
expect(await setup.ready).toBe(mem);
|
|
237
|
+
await setup.store.set('/a', { html: 'a', timestamp: 1, tags: ['t'] });
|
|
238
|
+
expect(await mem.get('isr:/a')).toContain('"html":"a"');
|
|
239
|
+
expect(await setup.store.deleteTags(['t'])).toBeGreaterThan(0);
|
|
240
|
+
expect(await mem.get('isr:/a')).toBeNull();
|
|
241
|
+
installCacheAdapter(mem);
|
|
242
|
+
expect(cacheRuntime().shared).toBeTruthy();
|
|
243
|
+
});
|
|
244
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CacheAdapter, CacheSetOptions } from './cache-adapter.js';
|
|
2
|
+
export interface RedisAdapterOptions {
|
|
3
|
+
url: string;
|
|
4
|
+
prefix?: string;
|
|
5
|
+
/** Milliseconds one command may take (default 250). */
|
|
6
|
+
timeoutMs?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class RedisAdapter implements CacheAdapter {
|
|
9
|
+
readonly name = "redis";
|
|
10
|
+
private readonly conn;
|
|
11
|
+
private readonly prefix;
|
|
12
|
+
private readonly connOpts;
|
|
13
|
+
private subscribers;
|
|
14
|
+
constructor(options: RedisAdapterOptions);
|
|
15
|
+
private k;
|
|
16
|
+
private t;
|
|
17
|
+
get(key: string): Promise<string | null>;
|
|
18
|
+
set(key: string, value: string, options?: CacheSetOptions): Promise<void>;
|
|
19
|
+
delete(key: string): Promise<void>;
|
|
20
|
+
keysByTag(tag: string): Promise<string[]>;
|
|
21
|
+
deleteByTags(tags: string[]): Promise<number>;
|
|
22
|
+
publish(channel: string, message: string): Promise<void>;
|
|
23
|
+
subscribe(channel: string, handler: (message: string) => void): Promise<() => void>;
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/** createRedisAdapter({ url }) is `new RedisAdapter({ url })`, for passing to createKrateHandler({ cache }). */
|
|
27
|
+
export declare function createRedisAdapter(options: RedisAdapterOptions): RedisAdapter;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// The Redis cache adapter: a RESP client over node:net (resp.ts), no dependency.
|
|
2
|
+
//
|
|
3
|
+
// Layout (prefix defaults to "krate:"):
|
|
4
|
+
// <prefix>k:<key> the value, with EX when it expires
|
|
5
|
+
// <prefix>t:<tag> a set of the keys stored with tag
|
|
6
|
+
// <prefix>c:<channel> pub/sub channel
|
|
7
|
+
import { RespConnection, parseRedisURL } from './resp.js';
|
|
8
|
+
export class RedisAdapter {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.name = 'redis';
|
|
11
|
+
this.subscribers = [];
|
|
12
|
+
this.connOpts = { ...parseRedisURL(options.url), commandTimeoutMs: options.timeoutMs ?? 250, connectTimeoutMs: Math.max(options.timeoutMs ?? 250, 500) };
|
|
13
|
+
this.conn = new RespConnection(this.connOpts);
|
|
14
|
+
this.prefix = options.prefix ?? 'krate:';
|
|
15
|
+
}
|
|
16
|
+
k(key) {
|
|
17
|
+
return this.prefix + 'k:' + key;
|
|
18
|
+
}
|
|
19
|
+
t(tag) {
|
|
20
|
+
return this.prefix + 't:' + tag;
|
|
21
|
+
}
|
|
22
|
+
async get(key) {
|
|
23
|
+
const v = await this.conn.command('GET', this.k(key));
|
|
24
|
+
return typeof v === 'string' ? v : null;
|
|
25
|
+
}
|
|
26
|
+
async set(key, value, options = {}) {
|
|
27
|
+
const ttl = options.ttlSeconds && options.ttlSeconds !== Infinity && options.ttlSeconds > 0 ? Math.ceil(options.ttlSeconds) : 0;
|
|
28
|
+
const writes = [ttl ? this.conn.command('SET', this.k(key), value, 'EX', ttl) : this.conn.command('SET', this.k(key), value)];
|
|
29
|
+
for (const tag of options.tags || []) {
|
|
30
|
+
writes.push(this.conn.command('SADD', this.t(tag), this.k(key)));
|
|
31
|
+
// A tag set outlives its newest member a little, so it never grows forever.
|
|
32
|
+
if (ttl)
|
|
33
|
+
writes.push(this.conn.command('EXPIRE', this.t(tag), ttl + 60));
|
|
34
|
+
}
|
|
35
|
+
await Promise.all(writes);
|
|
36
|
+
}
|
|
37
|
+
async delete(key) {
|
|
38
|
+
await this.conn.command('DEL', this.k(key));
|
|
39
|
+
}
|
|
40
|
+
async keysByTag(tag) {
|
|
41
|
+
const members = await this.conn.command('SMEMBERS', this.t(tag));
|
|
42
|
+
const prefix = this.prefix + 'k:';
|
|
43
|
+
return Array.isArray(members) ? members.filter((m) => typeof m === 'string' && m.startsWith(prefix)).map((m) => m.slice(prefix.length)) : [];
|
|
44
|
+
}
|
|
45
|
+
async deleteByTags(tags) {
|
|
46
|
+
let n = 0;
|
|
47
|
+
for (const tag of tags) {
|
|
48
|
+
const members = await this.conn.command('SMEMBERS', this.t(tag));
|
|
49
|
+
const keys = Array.isArray(members) ? members.filter((m) => typeof m === 'string') : [];
|
|
50
|
+
if (keys.length > 0) {
|
|
51
|
+
const removed = await this.conn.command('DEL', ...keys);
|
|
52
|
+
if (typeof removed === 'number')
|
|
53
|
+
n += removed;
|
|
54
|
+
}
|
|
55
|
+
await this.conn.command('DEL', this.t(tag));
|
|
56
|
+
}
|
|
57
|
+
return n;
|
|
58
|
+
}
|
|
59
|
+
async publish(channel, message) {
|
|
60
|
+
await this.conn.command('PUBLISH', this.prefix + 'c:' + channel, message);
|
|
61
|
+
}
|
|
62
|
+
async subscribe(channel, handler) {
|
|
63
|
+
const full = this.prefix + 'c:' + channel;
|
|
64
|
+
const sub = new RespConnection({ ...this.connOpts, commandTimeoutMs: 5000 });
|
|
65
|
+
let stopped = false;
|
|
66
|
+
let delay = 200;
|
|
67
|
+
const open = async () => {
|
|
68
|
+
await sub.ensure();
|
|
69
|
+
await sub.send('SUBSCRIBE', full);
|
|
70
|
+
};
|
|
71
|
+
sub.onPush = (kind, ch, payload) => {
|
|
72
|
+
if (kind === 'message' && ch === full)
|
|
73
|
+
handler(payload);
|
|
74
|
+
};
|
|
75
|
+
sub.onDisconnect = () => {
|
|
76
|
+
if (stopped)
|
|
77
|
+
return;
|
|
78
|
+
const retry = () => {
|
|
79
|
+
if (stopped)
|
|
80
|
+
return;
|
|
81
|
+
open().then(() => {
|
|
82
|
+
delay = 200;
|
|
83
|
+
// Messages published while disconnected are gone: tell the caller.
|
|
84
|
+
handler('*');
|
|
85
|
+
}, () => {
|
|
86
|
+
delay = Math.min(delay * 2, 5000);
|
|
87
|
+
setTimeout(retry, delay).unref?.();
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
setTimeout(retry, delay).unref?.();
|
|
91
|
+
};
|
|
92
|
+
await open();
|
|
93
|
+
const stop = () => {
|
|
94
|
+
stopped = true;
|
|
95
|
+
sub.close();
|
|
96
|
+
};
|
|
97
|
+
this.subscribers.push(stop);
|
|
98
|
+
return stop;
|
|
99
|
+
}
|
|
100
|
+
async close() {
|
|
101
|
+
for (const stop of this.subscribers)
|
|
102
|
+
stop();
|
|
103
|
+
this.subscribers = [];
|
|
104
|
+
this.conn.close();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** createRedisAdapter({ url }) is `new RedisAdapter({ url })`, for passing to createKrateHandler({ cache }). */
|
|
108
|
+
export function createRedisAdapter(options) {
|
|
109
|
+
return new RedisAdapter(options);
|
|
110
|
+
}
|
package/dist/cache-scope.d.ts
CHANGED
|
@@ -57,8 +57,23 @@ export declare class DataCache {
|
|
|
57
57
|
invalidateTags(tags: string[]): number;
|
|
58
58
|
clear(): void;
|
|
59
59
|
}
|
|
60
|
+
/** A data cache shared by several processes (see cache-adapter.ts). */
|
|
61
|
+
export interface SharedDataStore {
|
|
62
|
+
/** { ok: false } when the shared store could not be reached. */
|
|
63
|
+
get(key: string): Promise<{
|
|
64
|
+
ok: true;
|
|
65
|
+
value: string | undefined;
|
|
66
|
+
} | {
|
|
67
|
+
ok: false;
|
|
68
|
+
}>;
|
|
69
|
+
/** Resolves false when the entry could not be stored. */
|
|
70
|
+
set(key: string, json: string, ttlSeconds: number, tags: string[]): Promise<boolean>;
|
|
71
|
+
deleteTags(tags: string[]): Promise<number>;
|
|
72
|
+
}
|
|
60
73
|
/** Process-wide cache runtime shared by every bundled copy of this module. */
|
|
61
74
|
export interface CacheRuntime {
|
|
75
|
+
/** The data cache shared with other instances, when a cache adapter is configured. */
|
|
76
|
+
shared?: SharedDataStore;
|
|
62
77
|
dataCache: DataCache;
|
|
63
78
|
/** Render scope storage; installed by the SSR renderer. */
|
|
64
79
|
renderStore?: AsyncStore<CacheScope>;
|
|
@@ -70,6 +85,8 @@ export interface CacheRuntime {
|
|
|
70
85
|
}): {
|
|
71
86
|
dataEntries: number;
|
|
72
87
|
};
|
|
88
|
+
/** Deletes the entries stored with tags in the shared cache (0 when there is none). */
|
|
89
|
+
invalidateShared(tags: string[]): Promise<number>;
|
|
73
90
|
fetchPatched?: boolean;
|
|
74
91
|
}
|
|
75
92
|
/** The shared cache runtime, created on first use. */
|
|
@@ -99,6 +116,14 @@ export declare function routeDataTag(route: string): string;
|
|
|
99
116
|
export declare function recordCacheRead(tags: readonly string[] | undefined, revalidate: number | false | undefined): void;
|
|
100
117
|
/** Marks the current render as uncacheable. Outside a render it has no effect. */
|
|
101
118
|
export declare function markNoStore(): void;
|
|
119
|
+
/**
|
|
120
|
+
* Reads a JSON value from the data cache; undefined on a miss. With a shared
|
|
121
|
+
* cache the instances' common store answers; when it cannot, this process's own
|
|
122
|
+
* cache does.
|
|
123
|
+
*/
|
|
124
|
+
export declare function dataCacheGetJSONAsync(key: string): Promise<string | undefined>;
|
|
125
|
+
/** Stores a JSON value in the shared cache when there is one, else in this process's cache. */
|
|
126
|
+
export declare function dataCacheSetJSONAsync(key: string, json: string, ttlSeconds: number, tags: string[], epoch: number): Promise<void>;
|
|
102
127
|
/** Reads a JSON value from the data cache; undefined on a miss. */
|
|
103
128
|
export declare function dataCacheGetJSON(key: string): string | undefined;
|
|
104
129
|
/**
|
|
@@ -106,13 +131,4 @@ export declare function dataCacheGetJSON(key: string): string | undefined;
|
|
|
106
131
|
* render the entry also gets the route's implicit tag (see routeDataTag).
|
|
107
132
|
*/
|
|
108
133
|
export declare function dataCacheSetJSON(key: string, json: string, ttlSeconds: number, tags: string[], epoch: number): void;
|
|
109
|
-
/**
|
|
110
|
-
* Wraps globalThis.fetch once so Next-style cache options work in server
|
|
111
|
-
* code: `next.tags` are recorded on the current render, `next.revalidate: N`
|
|
112
|
-
* or `cache: "force-cache"` cache successful GET/HEAD responses (by method,
|
|
113
|
-
* URL and headers) for N seconds (indefinitely for force-cache or
|
|
114
|
-
* `revalidate: false`) under the given tags, and `cache: "no-store"` /
|
|
115
|
-
* `next.revalidate: 0` bypass the cache and make the render uncacheable.
|
|
116
|
-
* Requests without these options are passed through untouched.
|
|
117
|
-
*/
|
|
118
134
|
export declare function patchFetch(): void;
|