@sigil-dev/grimoire 0.9.21 → 0.9.22
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/README.md +83 -83
- package/index.ts +50 -48
- package/package.json +16 -5
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +870 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -749
- package/src/rendering/preload.ts +101 -101
- package/src/rendering/ssrPlugin.ts +108 -108
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +262 -262
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -44
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +1138 -1018
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +92 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +798 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +378 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +805 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +131 -131
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/test/middleware.test.ts
CHANGED
|
@@ -1,243 +1,243 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { createCookies } from "../src/server/cookie-utils";
|
|
3
|
-
import type { Handle, RequestEvent } from "../src/server/hooks";
|
|
4
|
-
import { createHooks, sequence } from "../src/server/hooks";
|
|
5
|
-
|
|
6
|
-
function fakeEvent(overrides?: Partial<RequestEvent>): RequestEvent {
|
|
7
|
-
return {
|
|
8
|
-
request: new Request("http://localhost/"),
|
|
9
|
-
url: new URL("http://localhost/"),
|
|
10
|
-
params: {},
|
|
11
|
-
locals: {},
|
|
12
|
-
cookies: {
|
|
13
|
-
get: () => undefined,
|
|
14
|
-
set: () => {},
|
|
15
|
-
delete: () => {},
|
|
16
|
-
},
|
|
17
|
-
route: { id: "/" },
|
|
18
|
-
fetch: globalThis.fetch,
|
|
19
|
-
setHeaders: () => {},
|
|
20
|
-
...overrides,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
describe("sequence()", () => {
|
|
25
|
-
test("chains two handlers in order", async () => {
|
|
26
|
-
const order: string[] = [];
|
|
27
|
-
|
|
28
|
-
const a: Handle = async ({ event, resolve }) => {
|
|
29
|
-
order.push("a-before");
|
|
30
|
-
const res = await resolve(event);
|
|
31
|
-
order.push("a-after");
|
|
32
|
-
return res;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const b: Handle = async ({ event, resolve }) => {
|
|
36
|
-
order.push("b-before");
|
|
37
|
-
const res = await resolve(event);
|
|
38
|
-
order.push("b-after");
|
|
39
|
-
return res;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const handler = sequence(a, b);
|
|
43
|
-
const res = await handler({
|
|
44
|
-
event: fakeEvent(),
|
|
45
|
-
resolve: async (_evt) => new Response("OK"),
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
expect(order).toEqual(["a-before", "b-before", "b-after", "a-after"]);
|
|
49
|
-
expect(await res.text()).toBe("OK");
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("can short-circuit", async () => {
|
|
53
|
-
let reached = false;
|
|
54
|
-
|
|
55
|
-
const a: Handle = async () => {
|
|
56
|
-
return new Response("Blocked", { status: 403 });
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const b: Handle = async ({ resolve, event }) => {
|
|
60
|
-
reached = true;
|
|
61
|
-
return resolve(event);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const handler = sequence(a, b);
|
|
65
|
-
const res = await handler({
|
|
66
|
-
event: fakeEvent(),
|
|
67
|
-
resolve: async () => new Response("OK"),
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
expect(reached).toBe(false);
|
|
71
|
-
expect(res.status).toBe(403);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("can modify event.locals", async () => {
|
|
75
|
-
const a: Handle = async ({ event, resolve }) => {
|
|
76
|
-
(event.locals as any).user = "alice";
|
|
77
|
-
return resolve(event);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const b: Handle = async ({ event, resolve }) => {
|
|
81
|
-
(event.locals as any).greet = `hello ${(event.locals as any).user}`;
|
|
82
|
-
return resolve(event);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const handler = sequence(a, b);
|
|
86
|
-
const event = fakeEvent();
|
|
87
|
-
await handler({
|
|
88
|
-
event,
|
|
89
|
-
resolve: async (evt) => {
|
|
90
|
-
return new Response(JSON.stringify(evt.locals));
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
expect((event.locals as any).user).toBe("alice");
|
|
95
|
-
expect((event.locals as any).greet).toBe("hello alice");
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test("can modify response", async () => {
|
|
99
|
-
const a: Handle = async ({ event, resolve }) => {
|
|
100
|
-
const res = await resolve(event);
|
|
101
|
-
const headers = new Headers(res.headers);
|
|
102
|
-
headers.set("X-First", "1");
|
|
103
|
-
return new Response(res.body, { status: res.status, headers });
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const b: Handle = async ({ event, resolve }) => {
|
|
107
|
-
const res = await resolve(event);
|
|
108
|
-
const headers = new Headers(res.headers);
|
|
109
|
-
headers.set("X-Second", "2");
|
|
110
|
-
return new Response(res.body, { status: res.status, headers });
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const handler = sequence(a, b);
|
|
114
|
-
const res = await handler({
|
|
115
|
-
event: fakeEvent(),
|
|
116
|
-
resolve: async () => new Response("OK"),
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
expect(res.headers.get("X-First")).toBe("1");
|
|
120
|
-
expect(res.headers.get("X-Second")).toBe("2");
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test("single handler works", async () => {
|
|
124
|
-
const handler: Handle = async ({ event, resolve }) => {
|
|
125
|
-
return resolve(event);
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const res = await sequence(handler)({
|
|
129
|
-
event: fakeEvent(),
|
|
130
|
-
resolve: async () => new Response("Single"),
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
expect(await res.text()).toBe("Single");
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test("three handlers chain correctly", async () => {
|
|
137
|
-
const order: string[] = [];
|
|
138
|
-
|
|
139
|
-
const a: Handle = async ({ event, resolve }) => {
|
|
140
|
-
order.push("a");
|
|
141
|
-
return resolve(event);
|
|
142
|
-
};
|
|
143
|
-
const b: Handle = async ({ event, resolve }) => {
|
|
144
|
-
order.push("b");
|
|
145
|
-
return resolve(event);
|
|
146
|
-
};
|
|
147
|
-
const c: Handle = async ({ event, resolve }) => {
|
|
148
|
-
order.push("c");
|
|
149
|
-
return resolve(event);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
await sequence(
|
|
153
|
-
a,
|
|
154
|
-
b,
|
|
155
|
-
c,
|
|
156
|
-
)({
|
|
157
|
-
event: fakeEvent(),
|
|
158
|
-
resolve: async () => new Response("OK"),
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
expect(order).toEqual(["a", "b", "c"]);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe("Cookies", () => {
|
|
166
|
-
function makeEvent(cookieHeader = ""): RequestEvent {
|
|
167
|
-
return fakeEvent({
|
|
168
|
-
cookies: createCookies(cookieHeader),
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
test("cookies.get reads from header", async () => {
|
|
173
|
-
let value: string | undefined;
|
|
174
|
-
const handler: Handle = async ({ event, resolve }) => {
|
|
175
|
-
value = event.cookies.get("session");
|
|
176
|
-
return resolve(event);
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
await handler({
|
|
180
|
-
event: makeEvent("session=abc123; theme=dark"),
|
|
181
|
-
resolve: async () => new Response("OK"),
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
expect(value).toBe("abc123");
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
test("cookies.set adds Set-Cookie header", async () => {
|
|
188
|
-
const event = makeEvent();
|
|
189
|
-
const handler: Handle = async ({ event: evt, resolve }) => {
|
|
190
|
-
evt.cookies.set("token", "xyz", { httpOnly: true, path: "/" });
|
|
191
|
-
return resolve(evt);
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
await handler({
|
|
195
|
-
event,
|
|
196
|
-
resolve: async () => new Response("OK"),
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
expect(event.cookies.get("token")).toBe("xyz");
|
|
200
|
-
const cookieUtils = event.cookies as ReturnType<typeof createCookies>;
|
|
201
|
-
expect(cookieUtils.toHeaders().length).toBe(1);
|
|
202
|
-
expect(cookieUtils.toHeaders()[0]).toContain("token=xyz");
|
|
203
|
-
expect(cookieUtils.toHeaders()[0]).toContain("HttpOnly");
|
|
204
|
-
expect(cookieUtils.toHeaders()[0]).toContain("Path=/");
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
describe("Cookies round-trip", () => {
|
|
209
|
-
test("values set with special characters read back decoded", () => {
|
|
210
|
-
const c = createCookies("prefs=%7B%22a%22%3A1%7D; bad=%E0%A4%A");
|
|
211
|
-
expect(c.get("prefs")).toBe('{"a":1}');
|
|
212
|
-
expect(c.get("bad")).toBe("%E0%A4%A"); // malformed stays raw
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
test("set and delete default to Path=/", () => {
|
|
216
|
-
const c = createCookies("");
|
|
217
|
-
c.set("session", "abc");
|
|
218
|
-
c.delete("old");
|
|
219
|
-
c.set("scoped", "x", { path: "/admin" });
|
|
220
|
-
const [set, del, scoped] = c.toHeaders();
|
|
221
|
-
expect(set).toContain("Path=/");
|
|
222
|
-
expect(del).toContain("Path=/");
|
|
223
|
-
expect(del).toContain("Max-Age=0");
|
|
224
|
-
expect(scoped).toContain("Path=/admin");
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
describe("createHooks()", () => {
|
|
229
|
-
test("returns handle and init", async () => {
|
|
230
|
-
const handle: Handle = async ({ resolve, event }) => resolve(event);
|
|
231
|
-
const init = () => {};
|
|
232
|
-
const hooks = createHooks(handle, init);
|
|
233
|
-
expect(hooks.handle).toBe(handle);
|
|
234
|
-
expect(hooks.init).toBe(init);
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test("returns handle without init", () => {
|
|
238
|
-
const handle: Handle = async ({ resolve, event }) => resolve(event);
|
|
239
|
-
const hooks = createHooks(handle);
|
|
240
|
-
expect(hooks.handle).toBe(handle);
|
|
241
|
-
expect(hooks.init).toBeUndefined();
|
|
242
|
-
});
|
|
243
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { createCookies } from "../src/server/cookie-utils";
|
|
3
|
+
import type { Handle, RequestEvent } from "../src/server/hooks";
|
|
4
|
+
import { createHooks, sequence } from "../src/server/hooks";
|
|
5
|
+
|
|
6
|
+
function fakeEvent(overrides?: Partial<RequestEvent>): RequestEvent {
|
|
7
|
+
return {
|
|
8
|
+
request: new Request("http://localhost/"),
|
|
9
|
+
url: new URL("http://localhost/"),
|
|
10
|
+
params: {},
|
|
11
|
+
locals: {},
|
|
12
|
+
cookies: {
|
|
13
|
+
get: () => undefined,
|
|
14
|
+
set: () => {},
|
|
15
|
+
delete: () => {},
|
|
16
|
+
},
|
|
17
|
+
route: { id: "/" },
|
|
18
|
+
fetch: globalThis.fetch,
|
|
19
|
+
setHeaders: () => {},
|
|
20
|
+
...overrides,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("sequence()", () => {
|
|
25
|
+
test("chains two handlers in order", async () => {
|
|
26
|
+
const order: string[] = [];
|
|
27
|
+
|
|
28
|
+
const a: Handle = async ({ event, resolve }) => {
|
|
29
|
+
order.push("a-before");
|
|
30
|
+
const res = await resolve(event);
|
|
31
|
+
order.push("a-after");
|
|
32
|
+
return res;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const b: Handle = async ({ event, resolve }) => {
|
|
36
|
+
order.push("b-before");
|
|
37
|
+
const res = await resolve(event);
|
|
38
|
+
order.push("b-after");
|
|
39
|
+
return res;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const handler = sequence(a, b);
|
|
43
|
+
const res = await handler({
|
|
44
|
+
event: fakeEvent(),
|
|
45
|
+
resolve: async (_evt) => new Response("OK"),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(order).toEqual(["a-before", "b-before", "b-after", "a-after"]);
|
|
49
|
+
expect(await res.text()).toBe("OK");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("can short-circuit", async () => {
|
|
53
|
+
let reached = false;
|
|
54
|
+
|
|
55
|
+
const a: Handle = async () => {
|
|
56
|
+
return new Response("Blocked", { status: 403 });
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const b: Handle = async ({ resolve, event }) => {
|
|
60
|
+
reached = true;
|
|
61
|
+
return resolve(event);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const handler = sequence(a, b);
|
|
65
|
+
const res = await handler({
|
|
66
|
+
event: fakeEvent(),
|
|
67
|
+
resolve: async () => new Response("OK"),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(reached).toBe(false);
|
|
71
|
+
expect(res.status).toBe(403);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("can modify event.locals", async () => {
|
|
75
|
+
const a: Handle = async ({ event, resolve }) => {
|
|
76
|
+
(event.locals as any).user = "alice";
|
|
77
|
+
return resolve(event);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const b: Handle = async ({ event, resolve }) => {
|
|
81
|
+
(event.locals as any).greet = `hello ${(event.locals as any).user}`;
|
|
82
|
+
return resolve(event);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const handler = sequence(a, b);
|
|
86
|
+
const event = fakeEvent();
|
|
87
|
+
await handler({
|
|
88
|
+
event,
|
|
89
|
+
resolve: async (evt) => {
|
|
90
|
+
return new Response(JSON.stringify(evt.locals));
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect((event.locals as any).user).toBe("alice");
|
|
95
|
+
expect((event.locals as any).greet).toBe("hello alice");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("can modify response", async () => {
|
|
99
|
+
const a: Handle = async ({ event, resolve }) => {
|
|
100
|
+
const res = await resolve(event);
|
|
101
|
+
const headers = new Headers(res.headers);
|
|
102
|
+
headers.set("X-First", "1");
|
|
103
|
+
return new Response(res.body, { status: res.status, headers });
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const b: Handle = async ({ event, resolve }) => {
|
|
107
|
+
const res = await resolve(event);
|
|
108
|
+
const headers = new Headers(res.headers);
|
|
109
|
+
headers.set("X-Second", "2");
|
|
110
|
+
return new Response(res.body, { status: res.status, headers });
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const handler = sequence(a, b);
|
|
114
|
+
const res = await handler({
|
|
115
|
+
event: fakeEvent(),
|
|
116
|
+
resolve: async () => new Response("OK"),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(res.headers.get("X-First")).toBe("1");
|
|
120
|
+
expect(res.headers.get("X-Second")).toBe("2");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("single handler works", async () => {
|
|
124
|
+
const handler: Handle = async ({ event, resolve }) => {
|
|
125
|
+
return resolve(event);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const res = await sequence(handler)({
|
|
129
|
+
event: fakeEvent(),
|
|
130
|
+
resolve: async () => new Response("Single"),
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(await res.text()).toBe("Single");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("three handlers chain correctly", async () => {
|
|
137
|
+
const order: string[] = [];
|
|
138
|
+
|
|
139
|
+
const a: Handle = async ({ event, resolve }) => {
|
|
140
|
+
order.push("a");
|
|
141
|
+
return resolve(event);
|
|
142
|
+
};
|
|
143
|
+
const b: Handle = async ({ event, resolve }) => {
|
|
144
|
+
order.push("b");
|
|
145
|
+
return resolve(event);
|
|
146
|
+
};
|
|
147
|
+
const c: Handle = async ({ event, resolve }) => {
|
|
148
|
+
order.push("c");
|
|
149
|
+
return resolve(event);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
await sequence(
|
|
153
|
+
a,
|
|
154
|
+
b,
|
|
155
|
+
c,
|
|
156
|
+
)({
|
|
157
|
+
event: fakeEvent(),
|
|
158
|
+
resolve: async () => new Response("OK"),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
expect(order).toEqual(["a", "b", "c"]);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe("Cookies", () => {
|
|
166
|
+
function makeEvent(cookieHeader = ""): RequestEvent {
|
|
167
|
+
return fakeEvent({
|
|
168
|
+
cookies: createCookies(cookieHeader),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
test("cookies.get reads from header", async () => {
|
|
173
|
+
let value: string | undefined;
|
|
174
|
+
const handler: Handle = async ({ event, resolve }) => {
|
|
175
|
+
value = event.cookies.get("session");
|
|
176
|
+
return resolve(event);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
await handler({
|
|
180
|
+
event: makeEvent("session=abc123; theme=dark"),
|
|
181
|
+
resolve: async () => new Response("OK"),
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
expect(value).toBe("abc123");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("cookies.set adds Set-Cookie header", async () => {
|
|
188
|
+
const event = makeEvent();
|
|
189
|
+
const handler: Handle = async ({ event: evt, resolve }) => {
|
|
190
|
+
evt.cookies.set("token", "xyz", { httpOnly: true, path: "/" });
|
|
191
|
+
return resolve(evt);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
await handler({
|
|
195
|
+
event,
|
|
196
|
+
resolve: async () => new Response("OK"),
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
expect(event.cookies.get("token")).toBe("xyz");
|
|
200
|
+
const cookieUtils = event.cookies as ReturnType<typeof createCookies>;
|
|
201
|
+
expect(cookieUtils.toHeaders().length).toBe(1);
|
|
202
|
+
expect(cookieUtils.toHeaders()[0]).toContain("token=xyz");
|
|
203
|
+
expect(cookieUtils.toHeaders()[0]).toContain("HttpOnly");
|
|
204
|
+
expect(cookieUtils.toHeaders()[0]).toContain("Path=/");
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe("Cookies round-trip", () => {
|
|
209
|
+
test("values set with special characters read back decoded", () => {
|
|
210
|
+
const c = createCookies("prefs=%7B%22a%22%3A1%7D; bad=%E0%A4%A");
|
|
211
|
+
expect(c.get("prefs")).toBe('{"a":1}');
|
|
212
|
+
expect(c.get("bad")).toBe("%E0%A4%A"); // malformed stays raw
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("set and delete default to Path=/", () => {
|
|
216
|
+
const c = createCookies("");
|
|
217
|
+
c.set("session", "abc");
|
|
218
|
+
c.delete("old");
|
|
219
|
+
c.set("scoped", "x", { path: "/admin" });
|
|
220
|
+
const [set, del, scoped] = c.toHeaders();
|
|
221
|
+
expect(set).toContain("Path=/");
|
|
222
|
+
expect(del).toContain("Path=/");
|
|
223
|
+
expect(del).toContain("Max-Age=0");
|
|
224
|
+
expect(scoped).toContain("Path=/admin");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("createHooks()", () => {
|
|
229
|
+
test("returns handle and init", async () => {
|
|
230
|
+
const handle: Handle = async ({ resolve, event }) => resolve(event);
|
|
231
|
+
const init = () => {};
|
|
232
|
+
const hooks = createHooks(handle, init);
|
|
233
|
+
expect(hooks.handle).toBe(handle);
|
|
234
|
+
expect(hooks.init).toBe(init);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("returns handle without init", () => {
|
|
238
|
+
const handle: Handle = async ({ resolve, event }) => resolve(event);
|
|
239
|
+
const hooks = createHooks(handle);
|
|
240
|
+
expect(hooks.handle).toBe(handle);
|
|
241
|
+
expect(hooks.init).toBeUndefined();
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { transformSync } from "@babel/core";
|
|
3
|
-
import type { ParserPlugin } from "@babel/core";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The parser configuration the SSR plugin uses, pinned here so a change to
|
|
7
|
-
* it has to be made in two places deliberately.
|
|
8
|
-
*
|
|
9
|
-
* Kept in sync by hand rather than imported: the plugin builds its own
|
|
10
|
-
* parserOpts inline, and a test that imported that would be testing the
|
|
11
|
-
* import rather than the behaviour. The failing case is named below, and it
|
|
12
|
-
* was found by a real adapter rather than by reading the config.
|
|
13
|
-
*/
|
|
14
|
-
const parserOptsFor = (path: string): { plugins: ParserPlugin[] } =>
|
|
15
|
-
path.endsWith(".tsx")
|
|
16
|
-
? { plugins: ["typescript", "jsx"] }
|
|
17
|
-
: { plugins: ["typescript"] };
|
|
18
|
-
|
|
19
|
-
const parse = (source: string, filename: string) =>
|
|
20
|
-
transformSync(source, {
|
|
21
|
-
configFile: false,
|
|
22
|
-
babelrc: false,
|
|
23
|
-
parserOpts: parserOptsFor(filename),
|
|
24
|
-
filename,
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
describe("the jsx parser plugin is scoped to .tsx", () => {
|
|
28
|
-
test("a generic arrow function compiles in a .ts file", () => {
|
|
29
|
-
// This is the regression. With ["typescript", "jsx"] on every file,
|
|
30
|
-
// Babel reads `<R>(x: R) => x` as the start of a JSX element and
|
|
31
|
-
// fails with "Unexpected token `>`". Generic arrows are ordinary
|
|
32
|
-
// TypeScript, and this is the exact line that lets a Drizzle adapter
|
|
33
|
-
// drop an `as any`:
|
|
34
|
-
//
|
|
35
|
-
// transaction: <R>(db: DrizzleDb, fn: (tx: DrizzleTx) => Promise<R>) =>
|
|
36
|
-
// db.transaction(fn)
|
|
37
|
-
expect(() =>
|
|
38
|
-
parse(
|
|
39
|
-
"const o = { f: <R>(x: R): R => x };\nexport const out = o;",
|
|
40
|
-
"adapter.ts",
|
|
41
|
-
),
|
|
42
|
-
).not.toThrow();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("a generic arrow in an object literal, the failing shape", () => {
|
|
46
|
-
// The minimal repro. Inside a call argument the parser is in a
|
|
47
|
-
// different state, which is why the one above passes and this failed.
|
|
48
|
-
expect(() =>
|
|
49
|
-
parse("define({ f: <R>(x: R): R => x });", "adapter.ts"),
|
|
50
|
-
).not.toThrow();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("JSX still compiles in a .tsx file", () => {
|
|
54
|
-
// The whole reason the jsx plugin exists must keep working.
|
|
55
|
-
expect(() =>
|
|
56
|
-
parse("const a = <div x={1} />;\nexport default a;", "page.tsx"),
|
|
57
|
-
).not.toThrow();
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("a JSX fragment compiles in a .tsx file", () => {
|
|
61
|
-
expect(() =>
|
|
62
|
-
parse("const a = <><b/></>;\nexport default a;", "page.tsx"),
|
|
63
|
-
).not.toThrow();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test("a generic arrow in a .tsx file needs the comma, as TypeScript says", () => {
|
|
67
|
-
// Genuinely ambiguous, so the error is correct behaviour rather than a
|
|
68
|
-
// bug — and worth pinning so the fix is not "regressed" later by
|
|
69
|
-
// someone trying to make this pass without the comma.
|
|
70
|
-
expect(() =>
|
|
71
|
-
parse("const o = { f: <R,>(x: R): R => x };\nexport default o;", "a.tsx"),
|
|
72
|
-
).not.toThrow();
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe("other TypeScript syntax in a .ts file", () => {
|
|
77
|
-
// Dropping jsx from .ts must not cost anything else, which is the risk a
|
|
78
|
-
// one-line parser change carries.
|
|
79
|
-
for (const [label, source] of [
|
|
80
|
-
["as const", "const x = { a: 1 } as const;\nexport default x;"],
|
|
81
|
-
["satisfies", "const x = { a: 1 } satisfies Record<string, number>;\nexport default x;"],
|
|
82
|
-
["a generic interface", "interface I<T> { v: T }\nexport default I;"],
|
|
83
|
-
["an enum", "enum E { A }\nexport default E;"],
|
|
84
|
-
["optional chaining", "const x = a?.b?.c;\nexport default x;"],
|
|
85
|
-
["a type-only import", 'import type { A } from "./a.ts";\nexport default A;'],
|
|
86
|
-
["decorators-free classes", "class C { x = 1 }\nexport default C;"],
|
|
87
|
-
] as Array<[string, string]>) {
|
|
88
|
-
test(label, () => {
|
|
89
|
-
expect(() => parse(source, "file.ts")).not.toThrow();
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { transformSync } from "@babel/core";
|
|
3
|
+
import type { ParserPlugin } from "@babel/core";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The parser configuration the SSR plugin uses, pinned here so a change to
|
|
7
|
+
* it has to be made in two places deliberately.
|
|
8
|
+
*
|
|
9
|
+
* Kept in sync by hand rather than imported: the plugin builds its own
|
|
10
|
+
* parserOpts inline, and a test that imported that would be testing the
|
|
11
|
+
* import rather than the behaviour. The failing case is named below, and it
|
|
12
|
+
* was found by a real adapter rather than by reading the config.
|
|
13
|
+
*/
|
|
14
|
+
const parserOptsFor = (path: string): { plugins: ParserPlugin[] } =>
|
|
15
|
+
path.endsWith(".tsx")
|
|
16
|
+
? { plugins: ["typescript", "jsx"] }
|
|
17
|
+
: { plugins: ["typescript"] };
|
|
18
|
+
|
|
19
|
+
const parse = (source: string, filename: string) =>
|
|
20
|
+
transformSync(source, {
|
|
21
|
+
configFile: false,
|
|
22
|
+
babelrc: false,
|
|
23
|
+
parserOpts: parserOptsFor(filename),
|
|
24
|
+
filename,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("the jsx parser plugin is scoped to .tsx", () => {
|
|
28
|
+
test("a generic arrow function compiles in a .ts file", () => {
|
|
29
|
+
// This is the regression. With ["typescript", "jsx"] on every file,
|
|
30
|
+
// Babel reads `<R>(x: R) => x` as the start of a JSX element and
|
|
31
|
+
// fails with "Unexpected token `>`". Generic arrows are ordinary
|
|
32
|
+
// TypeScript, and this is the exact line that lets a Drizzle adapter
|
|
33
|
+
// drop an `as any`:
|
|
34
|
+
//
|
|
35
|
+
// transaction: <R>(db: DrizzleDb, fn: (tx: DrizzleTx) => Promise<R>) =>
|
|
36
|
+
// db.transaction(fn)
|
|
37
|
+
expect(() =>
|
|
38
|
+
parse(
|
|
39
|
+
"const o = { f: <R>(x: R): R => x };\nexport const out = o;",
|
|
40
|
+
"adapter.ts",
|
|
41
|
+
),
|
|
42
|
+
).not.toThrow();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a generic arrow in an object literal, the failing shape", () => {
|
|
46
|
+
// The minimal repro. Inside a call argument the parser is in a
|
|
47
|
+
// different state, which is why the one above passes and this failed.
|
|
48
|
+
expect(() =>
|
|
49
|
+
parse("define({ f: <R>(x: R): R => x });", "adapter.ts"),
|
|
50
|
+
).not.toThrow();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("JSX still compiles in a .tsx file", () => {
|
|
54
|
+
// The whole reason the jsx plugin exists must keep working.
|
|
55
|
+
expect(() =>
|
|
56
|
+
parse("const a = <div x={1} />;\nexport default a;", "page.tsx"),
|
|
57
|
+
).not.toThrow();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("a JSX fragment compiles in a .tsx file", () => {
|
|
61
|
+
expect(() =>
|
|
62
|
+
parse("const a = <><b/></>;\nexport default a;", "page.tsx"),
|
|
63
|
+
).not.toThrow();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("a generic arrow in a .tsx file needs the comma, as TypeScript says", () => {
|
|
67
|
+
// Genuinely ambiguous, so the error is correct behaviour rather than a
|
|
68
|
+
// bug — and worth pinning so the fix is not "regressed" later by
|
|
69
|
+
// someone trying to make this pass without the comma.
|
|
70
|
+
expect(() =>
|
|
71
|
+
parse("const o = { f: <R,>(x: R): R => x };\nexport default o;", "a.tsx"),
|
|
72
|
+
).not.toThrow();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("other TypeScript syntax in a .ts file", () => {
|
|
77
|
+
// Dropping jsx from .ts must not cost anything else, which is the risk a
|
|
78
|
+
// one-line parser change carries.
|
|
79
|
+
for (const [label, source] of [
|
|
80
|
+
["as const", "const x = { a: 1 } as const;\nexport default x;"],
|
|
81
|
+
["satisfies", "const x = { a: 1 } satisfies Record<string, number>;\nexport default x;"],
|
|
82
|
+
["a generic interface", "interface I<T> { v: T }\nexport default I;"],
|
|
83
|
+
["an enum", "enum E { A }\nexport default E;"],
|
|
84
|
+
["optional chaining", "const x = a?.b?.c;\nexport default x;"],
|
|
85
|
+
["a type-only import", 'import type { A } from "./a.ts";\nexport default A;'],
|
|
86
|
+
["decorators-free classes", "class C { x = 1 }\nexport default C;"],
|
|
87
|
+
] as Array<[string, string]>) {
|
|
88
|
+
test(label, () => {
|
|
89
|
+
expect(() => parse(source, "file.ts")).not.toThrow();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|