@krate/runtime 0.5.0-alpha.2 → 0.5.0-alpha.4
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 +56 -0
- package/dist/action-context.js +73 -0
- package/dist/action-context.test.d.ts +1 -0
- package/dist/action-context.test.js +47 -0
- package/dist/actions.d.ts +120 -0
- package/dist/actions.js +550 -0
- package/dist/actions.test.d.ts +1 -0
- package/dist/actions.test.js +417 -0
- package/dist/boundary.d.ts +121 -0
- package/dist/boundary.js +429 -0
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +311 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +217 -0
- package/dist/class-component.d.ts +66 -0
- package/dist/class-component.js +633 -0
- package/dist/class-component.test.d.ts +1 -0
- package/dist/class-component.test.js +411 -0
- package/dist/context.d.ts +13 -0
- package/dist/context.js +58 -32
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +57 -0
- package/dist/derived.test.d.ts +1 -0
- package/dist/derived.test.js +140 -0
- package/dist/discrete.test.d.ts +1 -0
- package/dist/discrete.test.js +100 -0
- package/dist/dom.d.ts +55 -0
- package/dist/dom.js +414 -63
- package/dist/effects.d.ts +88 -0
- package/dist/effects.js +790 -0
- package/dist/effects.test.d.ts +1 -0
- package/dist/effects.test.js +485 -0
- package/dist/events.d.ts +58 -0
- package/dist/events.js +503 -0
- package/dist/events.test.d.ts +1 -0
- package/dist/events.test.js +447 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.js +9 -3
- package/dist/jsx-types.d.ts +2 -0
- package/dist/krate-hydrate-lite.js +7 -1
- package/dist/krate-hydrate.js +7 -1
- package/dist/krate-runtime.js +7 -1
- package/dist/krate-suspense.js +2 -0
- package/dist/lanes.test.d.ts +1 -0
- package/dist/lanes.test.js +59 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.js +466 -0
- package/dist/list.test.d.ts +1 -0
- package/dist/list.test.js +344 -0
- package/dist/optimistic-lanes.test.d.ts +1 -0
- package/dist/optimistic-lanes.test.js +45 -0
- package/dist/portal.d.ts +47 -0
- package/dist/portal.js +189 -0
- package/dist/portal.test.d.ts +1 -0
- package/dist/portal.test.js +99 -0
- package/dist/react-effects.test.d.ts +1 -0
- package/dist/react-effects.test.js +96 -0
- package/dist/react-suspense.test.d.ts +1 -0
- package/dist/react-suspense.test.js +194 -0
- package/dist/react.d.ts +132 -32
- package/dist/react.js +335 -81
- package/dist/react.test.js +12 -2
- package/dist/reconcile.js +13 -0
- package/dist/refs.d.ts +33 -0
- package/dist/refs.js +142 -0
- package/dist/refs.test.d.ts +1 -0
- package/dist/refs.test.js +257 -0
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/resource.js +20 -1
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +399 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1535 -650
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +174 -0
- package/dist/server-renderer.js +494 -119
- package/dist/server.d.ts +27 -1
- package/dist/server.js +187 -1
- package/dist/server.test.js +20 -0
- package/dist/signal-lanes.test.d.ts +1 -0
- package/dist/signal-lanes.test.js +32 -0
- package/dist/signal.d.ts +186 -1
- package/dist/signal.js +896 -66
- package/dist/slots.d.ts +22 -0
- package/dist/slots.js +134 -0
- package/dist/slots.test.d.ts +1 -0
- package/dist/slots.test.js +169 -0
- package/dist/snapshots.test.d.ts +1 -0
- package/dist/snapshots.test.js +208 -0
- package/dist/suspense-actions.test.d.ts +1 -0
- package/dist/suspense-actions.test.js +72 -0
- package/dist/suspense.d.ts +220 -0
- package/dist/suspense.js +901 -0
- package/dist/suspense.test.d.ts +1 -0
- package/dist/suspense.test.js +518 -0
- package/package.json +5 -1
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { DataCache, cacheRuntime, currentRenderScope, installRenderStore, patchFetch, withCacheScope, } from './cache-scope.js';
|
|
4
|
+
import { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
5
|
+
const RUNTIME_KEY = Symbol.for('krate.cache.runtime');
|
|
6
|
+
const REVALIDATE_KEY = Symbol.for('krate.revalidate.store');
|
|
7
|
+
function resetRuntime() {
|
|
8
|
+
delete globalThis[RUNTIME_KEY];
|
|
9
|
+
delete globalThis[REVALIDATE_KEY];
|
|
10
|
+
installRenderStore(new AsyncLocalStorage());
|
|
11
|
+
}
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
resetRuntime();
|
|
14
|
+
vi.unstubAllEnvs();
|
|
15
|
+
});
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
vi.unstubAllGlobals();
|
|
18
|
+
vi.unstubAllEnvs();
|
|
19
|
+
delete globalThis._go_revalidate;
|
|
20
|
+
});
|
|
21
|
+
describe('DataCache', () => {
|
|
22
|
+
it('indexes entries by tag and expires them', () => {
|
|
23
|
+
vi.useFakeTimers();
|
|
24
|
+
try {
|
|
25
|
+
const c = new DataCache(10);
|
|
26
|
+
c.set('a', 1, 10, ['t1']);
|
|
27
|
+
c.set('b', 2, Infinity, ['t1', 't2']);
|
|
28
|
+
c.set('c', 3, 1, []);
|
|
29
|
+
expect(c.get('a')).toBe(1);
|
|
30
|
+
vi.advanceTimersByTime(1500);
|
|
31
|
+
expect(c.get('c')).toBeUndefined();
|
|
32
|
+
expect(c.invalidateTags(['t2'])).toBe(1);
|
|
33
|
+
expect(c.get('b')).toBeUndefined();
|
|
34
|
+
expect(c.get('a')).toBe(1);
|
|
35
|
+
expect(c.invalidateTags(['t1'])).toBe(1);
|
|
36
|
+
expect(c.size).toBe(0);
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
vi.useRealTimers();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
it('evicts the oldest entry when full', () => {
|
|
43
|
+
const c = new DataCache(2);
|
|
44
|
+
c.set('a', 1, 10, ['x']);
|
|
45
|
+
c.set('b', 2, 10, []);
|
|
46
|
+
c.set('c', 3, 10, []);
|
|
47
|
+
expect(c.get('a')).toBeUndefined();
|
|
48
|
+
expect(c.invalidateTags(['x'])).toBe(0);
|
|
49
|
+
expect(c.size).toBe(2);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe('withCacheScope', () => {
|
|
53
|
+
it('records tags and the smallest revalidate window across awaits', async () => {
|
|
54
|
+
const cached = unstable_cache(async (n) => n * 2, ['double'], { tags: ['math'], revalidate: 30 });
|
|
55
|
+
const other = unstable_cache(async () => 'x', ['other'], { tags: ['letters'], revalidate: 10 });
|
|
56
|
+
const { result, scope, invalidated } = await withCacheScope('/p', async () => {
|
|
57
|
+
const a = await cached(2);
|
|
58
|
+
await new Promise((r) => setTimeout(r, 1));
|
|
59
|
+
const b = await other();
|
|
60
|
+
expect(currentRenderScope()?.route).toBe('/p');
|
|
61
|
+
return `${a}${b}`;
|
|
62
|
+
});
|
|
63
|
+
expect(result).toBe('4x');
|
|
64
|
+
expect([...scope.tags].sort()).toEqual(['letters', 'math']);
|
|
65
|
+
expect(scope.revalidate).toBe(10);
|
|
66
|
+
expect(scope.noStore).toBe(false);
|
|
67
|
+
expect(invalidated).toBe(false);
|
|
68
|
+
expect(currentRenderScope()).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
it('keeps concurrent scopes apart', async () => {
|
|
71
|
+
const tagged = (tag) => unstable_cache(async () => tag, [tag], { tags: [tag] });
|
|
72
|
+
const [a, b] = await Promise.all([
|
|
73
|
+
withCacheScope('/a', async () => {
|
|
74
|
+
await new Promise((r) => setTimeout(r, 2));
|
|
75
|
+
return tagged('a')();
|
|
76
|
+
}),
|
|
77
|
+
withCacheScope('/b', () => tagged('b')()),
|
|
78
|
+
]);
|
|
79
|
+
expect([...a.scope.tags]).toEqual(['a']);
|
|
80
|
+
expect([...b.scope.tags]).toEqual(['b']);
|
|
81
|
+
});
|
|
82
|
+
it('marks unstable_noStore renders and invalidations that land mid-render', async () => {
|
|
83
|
+
const r1 = await withCacheScope('/n', () => {
|
|
84
|
+
unstable_noStore();
|
|
85
|
+
return 1;
|
|
86
|
+
});
|
|
87
|
+
expect(r1.scope.noStore).toBe(true);
|
|
88
|
+
const r2 = await withCacheScope('/i', async () => {
|
|
89
|
+
cacheRuntime().invalidate({ tags: ['z'] });
|
|
90
|
+
return 2;
|
|
91
|
+
});
|
|
92
|
+
expect(r2.invalidated).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
describe('unstable_cache', () => {
|
|
96
|
+
it('caches by key parts and arguments until the tag is revalidated', async () => {
|
|
97
|
+
let calls = 0;
|
|
98
|
+
const getPost = unstable_cache(async (id) => ({ id, n: ++calls }), ['post'], { tags: ['posts'] });
|
|
99
|
+
expect(await getPost('a')).toEqual({ id: 'a', n: 1 });
|
|
100
|
+
expect(await getPost('a')).toEqual({ id: 'a', n: 1 });
|
|
101
|
+
expect(await getPost('b')).toEqual({ id: 'b', n: 2 });
|
|
102
|
+
const scope = { kind: 'action', paths: [], tags: [] };
|
|
103
|
+
const store = new AsyncLocalStorage();
|
|
104
|
+
globalThis[REVALIDATE_KEY] = store;
|
|
105
|
+
store.run(scope, () => revalidateTag('posts'));
|
|
106
|
+
expect(scope.tags).toEqual(['posts']);
|
|
107
|
+
expect(await getPost('a')).toEqual({ id: 'a', n: 3 });
|
|
108
|
+
});
|
|
109
|
+
it('dedupes concurrent misses', async () => {
|
|
110
|
+
let calls = 0;
|
|
111
|
+
const slow = unstable_cache(async () => {
|
|
112
|
+
calls++;
|
|
113
|
+
await new Promise((r) => setTimeout(r, 2));
|
|
114
|
+
return calls;
|
|
115
|
+
}, ['slow']);
|
|
116
|
+
const [a, b] = await Promise.all([slow(), slow()]);
|
|
117
|
+
expect(a).toBe(1);
|
|
118
|
+
expect(b).toBe(1);
|
|
119
|
+
expect(calls).toBe(1);
|
|
120
|
+
});
|
|
121
|
+
it('does not cache with revalidate 0 and rejects non-JSON results', async () => {
|
|
122
|
+
let calls = 0;
|
|
123
|
+
const fresh = unstable_cache(async () => ++calls, ['fresh'], { revalidate: 0 });
|
|
124
|
+
await fresh();
|
|
125
|
+
await fresh();
|
|
126
|
+
expect(calls).toBe(2);
|
|
127
|
+
const bad = unstable_cache(async () => undefined, ['bad']);
|
|
128
|
+
await expect(bad()).rejects.toThrow(/JSON-serializable/);
|
|
129
|
+
expect(() => unstable_cache(async () => 1, [], { revalidate: -1 })).toThrow(/revalidate/);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
describe('revalidatePath / revalidateTag', () => {
|
|
133
|
+
it('queue on the request scope of an action or API route', () => {
|
|
134
|
+
const scope = { kind: 'action', paths: [], tags: [] };
|
|
135
|
+
const store = new AsyncLocalStorage();
|
|
136
|
+
globalThis[REVALIDATE_KEY] = store;
|
|
137
|
+
store.run(scope, () => {
|
|
138
|
+
revalidatePath('/blog/[slug]');
|
|
139
|
+
revalidatePath('/blog', 'layout');
|
|
140
|
+
revalidateTag('posts');
|
|
141
|
+
});
|
|
142
|
+
expect(scope.paths).toEqual([
|
|
143
|
+
{ path: '/blog/[slug]', type: 'page' },
|
|
144
|
+
{ path: '/blog', type: 'layout' },
|
|
145
|
+
]);
|
|
146
|
+
expect(scope.tags).toEqual(['posts']);
|
|
147
|
+
});
|
|
148
|
+
it('throw while a page renders', async () => {
|
|
149
|
+
await expect(withCacheScope('/p', () => revalidateTag('x'))).rejects.toThrow(/while a page renders/);
|
|
150
|
+
});
|
|
151
|
+
it('throw outside the Krate server instead of doing nothing', () => {
|
|
152
|
+
expect(() => revalidatePath('/x')).toThrow(/KRATE_HOST_URL is not set/);
|
|
153
|
+
expect(() => revalidateTag('x')).toThrow(/KRATE_HOST_URL is not set/);
|
|
154
|
+
});
|
|
155
|
+
it('validate their arguments', () => {
|
|
156
|
+
expect(() => revalidatePath('relative')).toThrow(TypeError);
|
|
157
|
+
expect(() => revalidatePath('/x', 'tree')).toThrow(TypeError);
|
|
158
|
+
expect(() => revalidateTag('')).toThrow(TypeError);
|
|
159
|
+
});
|
|
160
|
+
it('call the Go host synchronously in the embedded runtime', () => {
|
|
161
|
+
const calls = [];
|
|
162
|
+
globalThis._go_revalidate = (json) => {
|
|
163
|
+
calls.push(json);
|
|
164
|
+
return JSON.stringify({ revalidated: [], skipped: [{ target: '/about', reason: 'static page' }] });
|
|
165
|
+
};
|
|
166
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
167
|
+
try {
|
|
168
|
+
revalidatePath('/about');
|
|
169
|
+
expect(JSON.parse(calls[0])).toEqual({ paths: [{ path: '/about', type: 'page' }], tags: [] });
|
|
170
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('/about: static page'));
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
warn.mockRestore();
|
|
174
|
+
}
|
|
175
|
+
globalThis._go_revalidate = () => JSON.stringify({ error: 'boom' });
|
|
176
|
+
expect(() => revalidateTag('t')).toThrow(/boom/);
|
|
177
|
+
});
|
|
178
|
+
it('post to the host outside a request scope when the host is configured', async () => {
|
|
179
|
+
vi.stubEnv('KRATE_HOST_URL', 'http://127.0.0.1:9');
|
|
180
|
+
vi.stubEnv('KRATE_INTERNAL_SECRET', 's3cret');
|
|
181
|
+
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ revalidated: [], skipped: [] }), { status: 200 }));
|
|
182
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
183
|
+
revalidateTag('posts');
|
|
184
|
+
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1));
|
|
185
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
186
|
+
expect(url).toBe('http://127.0.0.1:9/__krate/internal/revalidate');
|
|
187
|
+
expect(init.headers['x-krate-internal']).toBe('s3cret');
|
|
188
|
+
expect(JSON.parse(String(init.body))).toEqual({ paths: [], tags: ['posts'] });
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
describe('patchFetch', () => {
|
|
192
|
+
it('caches tagged GET responses, records tags, and bypasses no-store', async () => {
|
|
193
|
+
let n = 0;
|
|
194
|
+
const base = vi.fn(async () => new Response(`body-${++n}`, { status: 200, headers: { 'x-n': String(n) } }));
|
|
195
|
+
vi.stubGlobal('fetch', base);
|
|
196
|
+
cacheRuntime().fetchPatched = false;
|
|
197
|
+
patchFetch();
|
|
198
|
+
const read = () => fetch('http://data/posts', { next: { tags: ['posts'], revalidate: 60 } });
|
|
199
|
+
const { result, scope } = await withCacheScope('/p', async () => (await read()).text());
|
|
200
|
+
expect(result).toBe('body-1');
|
|
201
|
+
expect([...scope.tags]).toEqual(['posts']);
|
|
202
|
+
expect(scope.revalidate).toBe(60);
|
|
203
|
+
expect(await (await read()).text()).toBe('body-1');
|
|
204
|
+
expect(base).toHaveBeenCalledTimes(1);
|
|
205
|
+
cacheRuntime().invalidate({ tags: ['posts'] });
|
|
206
|
+
expect(await (await read()).text()).toBe('body-2');
|
|
207
|
+
const ns = await withCacheScope('/q', async () => (await fetch('http://data/posts', { cache: 'no-store' })).text());
|
|
208
|
+
expect(ns.result).toBe('body-3');
|
|
209
|
+
expect(ns.scope.noStore).toBe(true);
|
|
210
|
+
// Plain requests and non-GET requests are passed through uncached.
|
|
211
|
+
await fetch('http://data/posts');
|
|
212
|
+
await fetch('http://data/posts', { method: 'POST', next: { revalidate: 60 } });
|
|
213
|
+
expect(base).toHaveBeenCalledTimes(5);
|
|
214
|
+
const plainInit = base.mock.calls[4][1];
|
|
215
|
+
expect(plainInit.next).toBeUndefined();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type ErrorInfo } from './boundary.js';
|
|
2
|
+
type Obj = Record<string, unknown>;
|
|
3
|
+
/** Lifecycle methods and statics of a lowered class, as closures. */
|
|
4
|
+
export interface ClassSpec {
|
|
5
|
+
name?: string;
|
|
6
|
+
/** PureComponent: shallow-compare props and state. */
|
|
7
|
+
pure?: boolean;
|
|
8
|
+
defaultProps?: Obj | null;
|
|
9
|
+
/** Reads the value of `static contextType`. */
|
|
10
|
+
context?: () => unknown;
|
|
11
|
+
/** Writes the committed context the lowered render reads. */
|
|
12
|
+
setContext?: (value: unknown) => void;
|
|
13
|
+
getDerivedStateFromProps?: (props: Obj, state: Obj) => Obj | null | undefined;
|
|
14
|
+
getDerivedStateFromError?: (error: unknown) => Obj | null | undefined;
|
|
15
|
+
shouldComponentUpdate?: (nextProps: Obj, nextState: Obj, nextContext: unknown) => unknown;
|
|
16
|
+
getSnapshotBeforeUpdate?: (prevProps: Obj, prevState: Obj) => unknown;
|
|
17
|
+
componentDidMount?: () => void;
|
|
18
|
+
componentDidUpdate?: (prevProps: Obj, prevState: Obj, snapshot: unknown) => void;
|
|
19
|
+
componentWillUnmount?: () => void;
|
|
20
|
+
componentDidCatch?: (error: unknown, info: ErrorInfo) => void;
|
|
21
|
+
componentWillReceiveProps?: (nextProps: Obj, nextContext: unknown) => void;
|
|
22
|
+
componentWillUpdate?: (nextProps: Obj, nextState: Obj, nextContext: unknown) => void;
|
|
23
|
+
/** Public methods to put on the instance (read once, at mount). */
|
|
24
|
+
expose?: () => Obj;
|
|
25
|
+
/** An error a descendant threw while the page was built (see build-time boundaries). */
|
|
26
|
+
buildError?: {
|
|
27
|
+
name?: string;
|
|
28
|
+
message?: string;
|
|
29
|
+
} | null;
|
|
30
|
+
}
|
|
31
|
+
/** The object lowered code uses as `this`. Instance fields are added freely. */
|
|
32
|
+
export interface ClassInstance {
|
|
33
|
+
readonly props: Obj;
|
|
34
|
+
state: Obj;
|
|
35
|
+
readonly context: unknown;
|
|
36
|
+
setState(update: Obj | ((state: Obj, props: Obj) => Obj | null | undefined) | null, callback?: () => void): void;
|
|
37
|
+
forceUpdate(callback?: () => void): void;
|
|
38
|
+
[field: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates the instance behind a lowered class component. `props` is the
|
|
42
|
+
* component's props registry object, `getState`/`setState` the committed-state
|
|
43
|
+
* signal its render reads.
|
|
44
|
+
*/
|
|
45
|
+
export declare function __krateClass(props: Obj | null | undefined, getState: () => Obj, setState: (v: Obj) => void, spec: ClassSpec): ClassInstance;
|
|
46
|
+
/** Shape of a runtime (not compiler-lowered) class component instance. */
|
|
47
|
+
interface RuntimeClassInstance {
|
|
48
|
+
props?: Obj;
|
|
49
|
+
state?: Obj | null;
|
|
50
|
+
context?: unknown;
|
|
51
|
+
render(): unknown;
|
|
52
|
+
setState?: unknown;
|
|
53
|
+
forceUpdate?: unknown;
|
|
54
|
+
[method: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
type RuntimeClass = (new (props: Obj) => RuntimeClassInstance) & Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Mounts a class component at runtime (React.createElement(MyClass) in code
|
|
59
|
+
* the compiler did not lower). The instance's setState/forceUpdate go through
|
|
60
|
+
* the same controller as lowered classes; each committed update re-runs
|
|
61
|
+
* render() and replaces the host's content.
|
|
62
|
+
*/
|
|
63
|
+
export declare function mountClassElement(Klass: RuntimeClass, props: Obj, toNode: (v: unknown) => Node): Node;
|
|
64
|
+
/** Reports an error from lowered class code to the nearest boundary. */
|
|
65
|
+
export declare function __krateClassError(err: unknown): void;
|
|
66
|
+
export {};
|