@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.3
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/actions.d.ts +7 -0
- package/dist/actions.js +23 -8
- package/dist/actions.test.js +8 -410
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/after.test.js +58 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/builtin-tags.test.js +12 -0
- package/dist/cache-adapter.test.js +4 -203
- package/dist/cache-scope.d.ts +2 -0
- package/dist/cache.test.js +1 -197
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +469 -76
- package/dist/dom.js +17 -2
- package/dist/events.test.js +2 -407
- package/dist/handler-html.test.js +1 -202
- package/dist/handler-routing.js +3 -0
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +16 -2
- package/dist/handler-segments.test.js +8 -200
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +85 -1
- package/dist/head-tags.test.js +18 -125
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/inline-action-token.test.js +31 -0
- package/dist/jsx-svg-types.test.js +58 -0
- package/dist/jsx-types.d.ts +92 -1
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +384 -65
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +2 -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/link-base-path.test.js +28 -0
- package/dist/metadata.js +3 -0
- package/dist/node-server.test.js +1 -355
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/platform.test.js +44 -0
- package/dist/router-lite.js +3 -9
- package/dist/router-nav.test.js +15 -444
- package/dist/router.js +51 -18
- package/dist/server-handler.d.ts +49 -0
- package/dist/server-handler.js +311 -21
- package/dist/server-handler.test.js +56 -344
- package/dist/server-renderer.test.js +0 -104
- package/dist/server.d.ts +15 -5
- package/dist/server.js +150 -22
- package/dist/server.test.js +52 -60
- package/dist/site-config.d.ts +13 -0
- package/dist/site-config.js +28 -0
- package/dist/site-config.test.js +30 -0
- package/dist/ssr-core-behavior.test.js +3 -515
- package/dist/testing.d.ts +59 -0
- package/dist/testing.js +88 -0
- package/dist/testing.test.js +73 -0
- package/package.json +9 -1
- package/dist/action-context.test.js +0 -72
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.js +0 -57
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.js +0 -165
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.js +0 -39
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -199
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -435
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -125
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -133
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -187
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- package/dist/suspense.test.js +0 -556
- /package/dist/{action-context.test.d.ts → after.test.d.ts} +0 -0
- /package/dist/{class-component.test.d.ts → builtin-tags.test.d.ts} +0 -0
- /package/dist/{class-helpers.test.d.ts → inline-action-token.test.d.ts} +0 -0
- /package/dist/{context.test.d.ts → jsx-svg-types.test.d.ts} +0 -0
- /package/dist/{derived.test.d.ts → link-base-path.test.d.ts} +0 -0
- /package/dist/{dev-overlay.test.d.ts → platform.test.d.ts} +0 -0
- /package/dist/{discrete.test.d.ts → site-config.test.d.ts} +0 -0
- /package/dist/{dom.test.d.ts → testing.test.d.ts} +0 -0
package/dist/testing.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers, exported from `@krate/runtime/testing`. They call an API route
|
|
3
|
+
* module or a server action in-process, without a running server. Node only
|
|
4
|
+
* (it uses AsyncLocalStorage); import it from your test files, never from app
|
|
5
|
+
* code.
|
|
6
|
+
*/
|
|
7
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
|
+
const METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
|
|
9
|
+
/**
|
|
10
|
+
* Calls a route module the way the server does: the named export for `method`
|
|
11
|
+
* (HEAD falls back to GET without a body, OPTIONS answers `Allow`), else a
|
|
12
|
+
* default Web-style handler, else a 405 with `Allow`. `target` is a path
|
|
13
|
+
* (`"/api/users/7?x=1"`), an absolute URL, or a ready `Request`.
|
|
14
|
+
*/
|
|
15
|
+
export async function callRoute(mod, method, target, options = {}) {
|
|
16
|
+
const request = toRequest(method, target, options);
|
|
17
|
+
const context = { params: options.params ?? {} };
|
|
18
|
+
const named = (m) => (typeof mod[m] === "function" ? mod[m] : undefined);
|
|
19
|
+
const fallback = typeof mod.default === "function" ? mod.default : undefined;
|
|
20
|
+
if (fallback && fallback.length >= 2) {
|
|
21
|
+
throw new TypeError("callRoute: a default export with a (req, res) signature is the legacy Node style and is not supported; export named GET/POST handlers instead");
|
|
22
|
+
}
|
|
23
|
+
const direct = named(method);
|
|
24
|
+
if (direct)
|
|
25
|
+
return direct(request, context);
|
|
26
|
+
if (method === "HEAD" && named("GET")) {
|
|
27
|
+
const res = await named("GET")(request, context);
|
|
28
|
+
return new Response(null, { status: res.status, statusText: res.statusText, headers: res.headers });
|
|
29
|
+
}
|
|
30
|
+
if (fallback && !METHODS.some((m) => named(m)))
|
|
31
|
+
return (await fallback(request, context));
|
|
32
|
+
const allow = METHODS.filter((m) => named(m) || (m === "HEAD" && named("GET")));
|
|
33
|
+
if (allow.indexOf("OPTIONS") < 0)
|
|
34
|
+
allow.push("OPTIONS");
|
|
35
|
+
if (method === "OPTIONS")
|
|
36
|
+
return new Response(null, { status: 204, headers: { Allow: allow.join(", ") } });
|
|
37
|
+
return new Response("Method Not Allowed", { status: 405, headers: { Allow: allow.join(", ") } });
|
|
38
|
+
}
|
|
39
|
+
function toRequest(method, target, options) {
|
|
40
|
+
if (typeof target !== "string")
|
|
41
|
+
return target;
|
|
42
|
+
const url = /^https?:\/\//i.test(target) ? target : "http://localhost" + (target.startsWith("/") ? target : "/" + target);
|
|
43
|
+
const headers = new Headers(options.headers);
|
|
44
|
+
let body = options.body;
|
|
45
|
+
if (options.json !== undefined) {
|
|
46
|
+
body = JSON.stringify(options.json);
|
|
47
|
+
if (!headers.has("content-type"))
|
|
48
|
+
headers.set("content-type", "application/json");
|
|
49
|
+
}
|
|
50
|
+
const hasBody = method !== "GET" && method !== "HEAD" && body !== undefined && body !== null;
|
|
51
|
+
return new Request(url, { method, headers, body: hasBody ? body : undefined });
|
|
52
|
+
}
|
|
53
|
+
const ACTION_STORE = Symbol.for("krate.action.store");
|
|
54
|
+
const als = new AsyncLocalStorage();
|
|
55
|
+
/**
|
|
56
|
+
* Runs a server action function with the request context a real call has, so
|
|
57
|
+
* `cookies()`, `headers()` and `getActionContext()` from `@krate/runtime/server`
|
|
58
|
+
* work. The action is the plain exported function you imported from the module.
|
|
59
|
+
*/
|
|
60
|
+
export async function runAction(action, options = {}) {
|
|
61
|
+
const jar = new Map(Object.entries(options.cookies ?? {}));
|
|
62
|
+
const setCookies = [];
|
|
63
|
+
const cookies = {
|
|
64
|
+
get: (name) => jar.get(name),
|
|
65
|
+
getAll: () => Array.from(jar, ([name, value]) => ({ name, value })),
|
|
66
|
+
has: (name) => jar.has(name),
|
|
67
|
+
set: (name, value, opts) => {
|
|
68
|
+
setCookies.push({ name, value: String(value), options: opts, deleted: false });
|
|
69
|
+
jar.set(name, String(value));
|
|
70
|
+
},
|
|
71
|
+
delete: (name, opts) => {
|
|
72
|
+
setCookies.push({ name, value: "", options: opts, deleted: true });
|
|
73
|
+
jar.delete(name);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
const context = {
|
|
77
|
+
actionId: options.actionId ?? "test-action",
|
|
78
|
+
url: options.url ?? "/",
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: new Headers(options.headers),
|
|
81
|
+
cookies,
|
|
82
|
+
};
|
|
83
|
+
// Published once and left in place: concurrent runAction calls share it, and
|
|
84
|
+
// each call's context lives in its own async scope.
|
|
85
|
+
globalThis[ACTION_STORE] = als;
|
|
86
|
+
const result = await als.run(context, async () => action());
|
|
87
|
+
return { result, setCookies };
|
|
88
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { cookies, headers, redirect } from "./server.js";
|
|
4
|
+
import { callRoute, runAction } from "./testing.js";
|
|
5
|
+
describe("callRoute", () => {
|
|
6
|
+
const route = {
|
|
7
|
+
async GET(req, ctx) {
|
|
8
|
+
const url = new URL(req.url);
|
|
9
|
+
return Response.json({ id: ctx.params.id, q: url.searchParams.get("q") });
|
|
10
|
+
},
|
|
11
|
+
async POST(req) {
|
|
12
|
+
return Response.json(await req.json(), { status: 201 });
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
it("passes the request and route params to a named export", async () => {
|
|
16
|
+
const res = await callRoute(route, "GET", "/api/users/7?q=x", { params: { id: "7" } });
|
|
17
|
+
expect(res.status).toBe(200);
|
|
18
|
+
expect(await res.json()).toEqual({ id: "7", q: "x" });
|
|
19
|
+
});
|
|
20
|
+
it("sends a JSON body", async () => {
|
|
21
|
+
const res = await callRoute(route, "POST", "/api/users", { json: { name: "ada" } });
|
|
22
|
+
expect(res.status).toBe(201);
|
|
23
|
+
expect(await res.json()).toEqual({ name: "ada" });
|
|
24
|
+
});
|
|
25
|
+
it("answers HEAD from GET, OPTIONS and 405 like the server", async () => {
|
|
26
|
+
const head = await callRoute(route, "HEAD", "/api/users/7");
|
|
27
|
+
expect(head.status).toBe(200);
|
|
28
|
+
expect(await head.text()).toBe("");
|
|
29
|
+
const options = await callRoute(route, "OPTIONS", "/api/users");
|
|
30
|
+
expect(options.headers.get("allow")).toBe("GET, POST, HEAD, OPTIONS");
|
|
31
|
+
const missing = await callRoute(route, "DELETE", "/api/users");
|
|
32
|
+
expect(missing.status).toBe(405);
|
|
33
|
+
expect(missing.headers.get("allow")).toContain("GET");
|
|
34
|
+
});
|
|
35
|
+
it("calls a default Web handler for every method and rejects the legacy signature", async () => {
|
|
36
|
+
const any = { default: async (req) => new Response(req.method) };
|
|
37
|
+
expect(await (await callRoute(any, "PUT", "/x")).text()).toBe("PUT");
|
|
38
|
+
const legacy = { default: (_req, res) => res.json({}) };
|
|
39
|
+
await expect(callRoute(legacy, "GET", "/x")).rejects.toThrow(/legacy/);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe("runAction", () => {
|
|
43
|
+
it("gives the action cookies() and headers() and records cookie writes", async () => {
|
|
44
|
+
async function login() {
|
|
45
|
+
const jar = cookies();
|
|
46
|
+
const before = jar.get("session");
|
|
47
|
+
jar.set("session", "abc", { httpOnly: true });
|
|
48
|
+
jar.delete("old");
|
|
49
|
+
return { before, agent: headers().get("user-agent") };
|
|
50
|
+
}
|
|
51
|
+
const { result, setCookies } = await runAction(login, {
|
|
52
|
+
cookies: { session: "prev", old: "1" },
|
|
53
|
+
headers: { "user-agent": "vitest" },
|
|
54
|
+
});
|
|
55
|
+
expect(result).toEqual({ before: { name: "session", value: "prev" }, agent: "vitest" });
|
|
56
|
+
expect(setCookies).toEqual([
|
|
57
|
+
{ name: "session", value: "abc", options: { httpOnly: true }, deleted: false },
|
|
58
|
+
{ name: "old", value: "", options: undefined, deleted: true },
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
it("rejects with what the action throws, including redirect()", async () => {
|
|
62
|
+
await expect(runAction(async () => redirect("/done"))).rejects.toBeDefined();
|
|
63
|
+
await expect(runAction(async () => { throw new Error("nope"); })).rejects.toThrow("nope");
|
|
64
|
+
});
|
|
65
|
+
it("keeps parallel actions apart", async () => {
|
|
66
|
+
const read = async () => {
|
|
67
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
68
|
+
return cookies().get("who");
|
|
69
|
+
};
|
|
70
|
+
const [a, b] = await Promise.all([runAction(read, { cookies: { who: "a" } }), runAction(read, { cookies: { who: "b" } })]);
|
|
71
|
+
expect([a.result, b.result]).toEqual([{ name: "who", value: "a" }, { name: "who", value: "b" }]);
|
|
72
|
+
});
|
|
73
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krate/runtime",
|
|
3
|
-
"version": "0.5.0-rc.
|
|
3
|
+
"version": "0.5.0-rc.3",
|
|
4
4
|
"description": "Client-side runtime for Krate - signals, hydration, SPA router, resources, and context.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -78,6 +78,10 @@
|
|
|
78
78
|
"types": "./dist/node-server.d.ts",
|
|
79
79
|
"default": "./dist/node-server.js"
|
|
80
80
|
},
|
|
81
|
+
"./cloudflare-workers": {
|
|
82
|
+
"types": "./dist/cloudflare-workers.d.ts",
|
|
83
|
+
"default": "./dist/cloudflare-workers.js"
|
|
84
|
+
},
|
|
81
85
|
"./assets-static": {
|
|
82
86
|
"types": "./dist/assets-static.d.ts",
|
|
83
87
|
"default": "./dist/assets-static.js"
|
|
@@ -93,6 +97,10 @@
|
|
|
93
97
|
"./og": {
|
|
94
98
|
"types": "./dist/og.d.ts",
|
|
95
99
|
"default": "./dist/og.js"
|
|
100
|
+
},
|
|
101
|
+
"./testing": {
|
|
102
|
+
"types": "./dist/testing.d.ts",
|
|
103
|
+
"default": "./dist/testing.js"
|
|
96
104
|
}
|
|
97
105
|
},
|
|
98
106
|
"files": [
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
3
|
-
import { cookies, getActionContext, headers, redirect } from './action-context.js';
|
|
4
|
-
import { installRenderStore, withCacheScope } from './cache-scope.js';
|
|
5
|
-
import { jsx, renderToString } from './server.js';
|
|
6
|
-
const KEY = Symbol.for('krate.action.store');
|
|
7
|
-
const REQUEST_KEY = Symbol.for('krate.request.store');
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
delete globalThis[KEY];
|
|
10
|
-
delete globalThis[REQUEST_KEY];
|
|
11
|
-
});
|
|
12
|
-
describe('server action context', () => {
|
|
13
|
-
it('throws outside an action with an actionable message', () => {
|
|
14
|
-
expect(getActionContext()).toBeUndefined();
|
|
15
|
-
expect(() => headers()).toThrow(/only be called while a server action runs/);
|
|
16
|
-
});
|
|
17
|
-
it('reads headers and cookies from the sidecar store', () => {
|
|
18
|
-
const jar = new Map([['sid', 'abc']]);
|
|
19
|
-
const ctx = {
|
|
20
|
-
actionId: 'a1',
|
|
21
|
-
url: '/__krate/action/a1',
|
|
22
|
-
method: 'POST',
|
|
23
|
-
headers: new Headers({ 'x-test': 'yes' }),
|
|
24
|
-
cookies: {
|
|
25
|
-
get: (n) => jar.get(n),
|
|
26
|
-
getAll: () => [],
|
|
27
|
-
has: (n) => jar.has(n),
|
|
28
|
-
set: () => { },
|
|
29
|
-
delete: () => { },
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
globalThis[KEY] = { getStore: () => ctx };
|
|
33
|
-
expect(headers().get('x-test')).toBe('yes');
|
|
34
|
-
expect(cookies().get('sid')).toEqual({ name: 'sid', value: 'abc' });
|
|
35
|
-
expect(cookies().get('sid')?.value).toBe('abc');
|
|
36
|
-
expect(cookies().get('missing')).toBeUndefined();
|
|
37
|
-
expect(getActionContext()?.cookies.get('sid')).toEqual({ name: 'sid', value: 'abc' });
|
|
38
|
-
expect(getActionContext()?.actionId).toBe('a1');
|
|
39
|
-
});
|
|
40
|
-
it('marks a render uncacheable when headers or cookies are read', async () => {
|
|
41
|
-
const ctx = {
|
|
42
|
-
url: '/p',
|
|
43
|
-
method: 'GET',
|
|
44
|
-
headers: new Headers({ 'x-test': 'yes' }),
|
|
45
|
-
cookies: new Map([['sid', 'abc']]),
|
|
46
|
-
params: {},
|
|
47
|
-
searchParams: {},
|
|
48
|
-
};
|
|
49
|
-
globalThis[REQUEST_KEY] = { getStore: () => ctx };
|
|
50
|
-
installRenderStore(new AsyncLocalStorage());
|
|
51
|
-
const { scope } = await withCacheScope('/p', () => {
|
|
52
|
-
headers();
|
|
53
|
-
cookies();
|
|
54
|
-
return 'ok';
|
|
55
|
-
});
|
|
56
|
-
expect(scope.noStore).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
it('redirect() throws the marker the sidecar turns into a navigation', () => {
|
|
59
|
-
let caught;
|
|
60
|
-
try {
|
|
61
|
-
redirect('/done');
|
|
62
|
-
}
|
|
63
|
-
catch (e) {
|
|
64
|
-
caught = e;
|
|
65
|
-
}
|
|
66
|
-
expect(caught.__krateRedirect).toBe('/done');
|
|
67
|
-
});
|
|
68
|
-
it('does not render client function actions as attributes', () => {
|
|
69
|
-
const html = renderToString(jsx('form', { action: async () => { }, children: 'x' }));
|
|
70
|
-
expect(html).toBe('<form>x</form>');
|
|
71
|
-
});
|
|
72
|
-
});
|