@sigil-dev/grimoire 0.9.29 → 0.9.30
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/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- 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 +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- 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 +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/test/routing.test.ts
CHANGED
|
@@ -1,197 +1,225 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { generateManifest } from "../src/routing/manifest-gen";
|
|
3
|
-
import { matchRoute } from "../src/routing/router";
|
|
4
|
-
import type { RouteFile, RouteTree } from "../src/routing/scanner";
|
|
5
|
-
|
|
6
|
-
const makeRoute = (path: string, type: RouteFile["type"]): RouteFile => ({
|
|
7
|
-
path,
|
|
8
|
-
filePath: `/app/routes${path}`,
|
|
9
|
-
clientPath: `/src/routes${path}`,
|
|
10
|
-
type,
|
|
11
|
-
paramNames: path
|
|
12
|
-
.split("/")
|
|
13
|
-
.filter((p) => p.startsWith(":") && !p.endsWith("?"))
|
|
14
|
-
.map((p) => p.slice(1)),
|
|
15
|
-
optionalParamNames: path
|
|
16
|
-
.split("/")
|
|
17
|
-
.filter((p) => p.startsWith(":") && p.endsWith("?"))
|
|
18
|
-
.map((p) => p.slice(1, -1)),
|
|
19
|
-
restParamNames: path
|
|
20
|
-
.split("/")
|
|
21
|
-
.filter((p) => p.startsWith("*"))
|
|
22
|
-
.map((p) => p.slice(1)),
|
|
23
|
-
paramMatchers: {},
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const noMatcherFiles = { paramMatcherFiles: [] as never[] };
|
|
27
|
-
|
|
28
|
-
const tree: RouteTree = {
|
|
29
|
-
routes: [
|
|
30
|
-
makeRoute("/", "page"),
|
|
31
|
-
makeRoute("/about", "simple"),
|
|
32
|
-
makeRoute("/blog", "page"),
|
|
33
|
-
makeRoute("/blog/:slug", "page"),
|
|
34
|
-
makeRoute("/blog/:slug", "pageServer"),
|
|
35
|
-
],
|
|
36
|
-
layouts: [makeRoute("/", "layout"), makeRoute("/blog", "layout")],
|
|
37
|
-
servers: [makeRoute("/api/todos", "server")],
|
|
38
|
-
errors: [],
|
|
39
|
-
...noMatcherFiles,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
describe("matchRoute", () => {
|
|
43
|
-
test("matches static route", () => {
|
|
44
|
-
const match = matchRoute(tree, new URL("http://localhost/about"));
|
|
45
|
-
expect(match?.route.path).toBe("/about");
|
|
46
|
-
expect(match?.params).toEqual({});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("matches dynamic route and extracts params", () => {
|
|
50
|
-
const match = matchRoute(tree, new URL("http://localhost/blog/hello"));
|
|
51
|
-
expect(match?.route.path).toBe("/blog/:slug");
|
|
52
|
-
expect(match?.params).toEqual({ slug: "hello" });
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("returns null for unknown route", () => {
|
|
56
|
-
const match = matchRoute(tree, new URL("http://localhost/unknown"));
|
|
57
|
-
expect(match).toBeNull();
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe("matchRoute — rest params (catch-all)", () => {
|
|
62
|
-
const restTree: RouteTree = {
|
|
63
|
-
routes: [],
|
|
64
|
-
layouts: [],
|
|
65
|
-
servers: [makeRoute("/docs/*path", "server")],
|
|
66
|
-
errors: [],
|
|
67
|
-
...noMatcherFiles,
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
test("matches single remaining segment", () => {
|
|
71
|
-
const match = matchRoute(restTree, new URL("http://localhost/docs/abc"));
|
|
72
|
-
expect(match?.route.path).toBe("/docs/*path");
|
|
73
|
-
expect(match?.params).toEqual({ path: "abc" });
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("matches multiple remaining segments joined by /", () => {
|
|
77
|
-
const match = matchRoute(restTree, new URL("http://localhost/docs/a/b/c"));
|
|
78
|
-
expect(match?.route.path).toBe("/docs/*path");
|
|
79
|
-
expect(match?.params).toEqual({ path: "a/b/c" });
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("matches zero remaining segments as empty string", () => {
|
|
83
|
-
const match = matchRoute(restTree, new URL("http://localhost/docs/"));
|
|
84
|
-
expect(match?.route.path).toBe("/docs/*path");
|
|
85
|
-
expect(match?.params).toEqual({ path: "" });
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test("does not match when prefix segments don't line up", () => {
|
|
89
|
-
const match = matchRoute(restTree, new URL("http://localhost/other/a"));
|
|
90
|
-
expect(match).toBeNull();
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
describe("matchRoute — optional params", () => {
|
|
95
|
-
// ordered as the scanner would: static (0) < optional (1) < required (2)
|
|
96
|
-
const optTree: RouteTree = {
|
|
97
|
-
routes: [
|
|
98
|
-
makeRoute("/about", "simple"),
|
|
99
|
-
makeRoute("/:lang?", "page"),
|
|
100
|
-
makeRoute("/:slug", "page"),
|
|
101
|
-
],
|
|
102
|
-
layouts: [],
|
|
103
|
-
servers: [],
|
|
104
|
-
errors: [],
|
|
105
|
-
...noMatcherFiles,
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
test("optional segment consumes one path segment", () => {
|
|
109
|
-
const match = matchRoute(optTree, new URL("http://localhost/en"));
|
|
110
|
-
expect(match?.route.path).toBe("/:lang?");
|
|
111
|
-
expect(match?.params).toEqual({ lang: "en" });
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test("optional segment matches zero segments", () => {
|
|
115
|
-
const match = matchRoute(optTree, new URL("http://localhost/"));
|
|
116
|
-
expect(match?.route.path).toBe("/:lang?");
|
|
117
|
-
expect(match?.params).toEqual({});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test("static beats optional", () => {
|
|
121
|
-
const match = matchRoute(optTree, new URL("http://localhost/about"));
|
|
122
|
-
expect(match?.route.path).toBe("/about");
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test("optional beats required param", () => {
|
|
126
|
-
const match = matchRoute(optTree, new URL("http://localhost/en"));
|
|
127
|
-
expect(match?.route.path).toBe("/:lang?");
|
|
128
|
-
expect(match?.route.path).not.toBe("/:slug");
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test("mid-pattern optional backtracks: /about matches /[[lang]]/about", () => {
|
|
132
|
-
const nested: RouteTree = {
|
|
133
|
-
routes: [makeRoute("/:lang?/about", "page")],
|
|
134
|
-
layouts: [],
|
|
135
|
-
servers: [],
|
|
136
|
-
errors: [],
|
|
137
|
-
...noMatcherFiles,
|
|
138
|
-
};
|
|
139
|
-
const zero = matchRoute(nested, new URL("http://localhost/about"));
|
|
140
|
-
expect(zero?.params).toEqual({});
|
|
141
|
-
const one = matchRoute(nested, new URL("http://localhost/en/about"));
|
|
142
|
-
expect(one?.params).toEqual({ lang: "en" });
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
describe("matchRoute — custom matchers", () => {
|
|
147
|
-
const intMatch = (v: string) => /^\d+$/.test(v);
|
|
148
|
-
const matcherTree: RouteTree = {
|
|
149
|
-
routes: [makeRoute("/post/:id", "page")],
|
|
150
|
-
layouts: [],
|
|
151
|
-
servers: [],
|
|
152
|
-
errors: [],
|
|
153
|
-
...noMatcherFiles,
|
|
154
|
-
};
|
|
155
|
-
matcherTree.routes[0]!.paramMatchers = { id: "int" };
|
|
156
|
-
|
|
157
|
-
test("matcher passes → route matches", () => {
|
|
158
|
-
const match = matchRoute(matcherTree, new URL("http://localhost/post/42"), {
|
|
159
|
-
int: intMatch,
|
|
160
|
-
});
|
|
161
|
-
expect(match?.params).toEqual({ id: "42" });
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
test("matcher fails → route is rejected (404)", () => {
|
|
165
|
-
const match = matchRoute(
|
|
166
|
-
matcherTree,
|
|
167
|
-
new URL("http://localhost/post/abc"),
|
|
168
|
-
{
|
|
169
|
-
int: intMatch,
|
|
170
|
-
},
|
|
171
|
-
);
|
|
172
|
-
expect(match).toBeNull();
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test("missing matcher module rejects the route", () => {
|
|
176
|
-
const match = matchRoute(matcherTree, new URL("http://localhost/post/42"));
|
|
177
|
-
expect(match).toBeNull();
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
describe("generateManifest — param matchers", () => {
|
|
182
|
-
test("emits eager matcher imports and record", () => {
|
|
183
|
-
const manifest = generateManifest([makeRoute("/", "page")], undefined, [
|
|
184
|
-
{ name: "int", filePath: "/app/params/int.ts" },
|
|
185
|
-
]);
|
|
186
|
-
expect(manifest).toContain(
|
|
187
|
-
'import * as __param_int from "/app/params/int.ts"',
|
|
188
|
-
);
|
|
189
|
-
expect(manifest).toContain('"int": __param_int.match');
|
|
190
|
-
expect(manifest).toContain("export const matchers = {");
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
test("emits matchers record even when none exist", () => {
|
|
194
|
-
const manifest = generateManifest([makeRoute("/", "page")]);
|
|
195
|
-
expect(manifest).toMatch(/export const matchers = \{\s*\};/);
|
|
196
|
-
});
|
|
197
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { generateManifest } from "../src/routing/manifest-gen";
|
|
3
|
+
import { matchRoute } from "../src/routing/router";
|
|
4
|
+
import type { RouteFile, RouteTree } from "../src/routing/scanner";
|
|
5
|
+
|
|
6
|
+
const makeRoute = (path: string, type: RouteFile["type"]): RouteFile => ({
|
|
7
|
+
path,
|
|
8
|
+
filePath: `/app/routes${path}`,
|
|
9
|
+
clientPath: `/src/routes${path}`,
|
|
10
|
+
type,
|
|
11
|
+
paramNames: path
|
|
12
|
+
.split("/")
|
|
13
|
+
.filter((p) => p.startsWith(":") && !p.endsWith("?"))
|
|
14
|
+
.map((p) => p.slice(1)),
|
|
15
|
+
optionalParamNames: path
|
|
16
|
+
.split("/")
|
|
17
|
+
.filter((p) => p.startsWith(":") && p.endsWith("?"))
|
|
18
|
+
.map((p) => p.slice(1, -1)),
|
|
19
|
+
restParamNames: path
|
|
20
|
+
.split("/")
|
|
21
|
+
.filter((p) => p.startsWith("*"))
|
|
22
|
+
.map((p) => p.slice(1)),
|
|
23
|
+
paramMatchers: {},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const noMatcherFiles = { paramMatcherFiles: [] as never[] };
|
|
27
|
+
|
|
28
|
+
const tree: RouteTree = {
|
|
29
|
+
routes: [
|
|
30
|
+
makeRoute("/", "page"),
|
|
31
|
+
makeRoute("/about", "simple"),
|
|
32
|
+
makeRoute("/blog", "page"),
|
|
33
|
+
makeRoute("/blog/:slug", "page"),
|
|
34
|
+
makeRoute("/blog/:slug", "pageServer"),
|
|
35
|
+
],
|
|
36
|
+
layouts: [makeRoute("/", "layout"), makeRoute("/blog", "layout")],
|
|
37
|
+
servers: [makeRoute("/api/todos", "server")],
|
|
38
|
+
errors: [],
|
|
39
|
+
...noMatcherFiles,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe("matchRoute", () => {
|
|
43
|
+
test("matches static route", () => {
|
|
44
|
+
const match = matchRoute(tree, new URL("http://localhost/about"));
|
|
45
|
+
expect(match?.route.path).toBe("/about");
|
|
46
|
+
expect(match?.params).toEqual({});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("matches dynamic route and extracts params", () => {
|
|
50
|
+
const match = matchRoute(tree, new URL("http://localhost/blog/hello"));
|
|
51
|
+
expect(match?.route.path).toBe("/blog/:slug");
|
|
52
|
+
expect(match?.params).toEqual({ slug: "hello" });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("returns null for unknown route", () => {
|
|
56
|
+
const match = matchRoute(tree, new URL("http://localhost/unknown"));
|
|
57
|
+
expect(match).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("matchRoute — rest params (catch-all)", () => {
|
|
62
|
+
const restTree: RouteTree = {
|
|
63
|
+
routes: [],
|
|
64
|
+
layouts: [],
|
|
65
|
+
servers: [makeRoute("/docs/*path", "server")],
|
|
66
|
+
errors: [],
|
|
67
|
+
...noMatcherFiles,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
test("matches single remaining segment", () => {
|
|
71
|
+
const match = matchRoute(restTree, new URL("http://localhost/docs/abc"));
|
|
72
|
+
expect(match?.route.path).toBe("/docs/*path");
|
|
73
|
+
expect(match?.params).toEqual({ path: "abc" });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("matches multiple remaining segments joined by /", () => {
|
|
77
|
+
const match = matchRoute(restTree, new URL("http://localhost/docs/a/b/c"));
|
|
78
|
+
expect(match?.route.path).toBe("/docs/*path");
|
|
79
|
+
expect(match?.params).toEqual({ path: "a/b/c" });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("matches zero remaining segments as empty string", () => {
|
|
83
|
+
const match = matchRoute(restTree, new URL("http://localhost/docs/"));
|
|
84
|
+
expect(match?.route.path).toBe("/docs/*path");
|
|
85
|
+
expect(match?.params).toEqual({ path: "" });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("does not match when prefix segments don't line up", () => {
|
|
89
|
+
const match = matchRoute(restTree, new URL("http://localhost/other/a"));
|
|
90
|
+
expect(match).toBeNull();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("matchRoute — optional params", () => {
|
|
95
|
+
// ordered as the scanner would: static (0) < optional (1) < required (2)
|
|
96
|
+
const optTree: RouteTree = {
|
|
97
|
+
routes: [
|
|
98
|
+
makeRoute("/about", "simple"),
|
|
99
|
+
makeRoute("/:lang?", "page"),
|
|
100
|
+
makeRoute("/:slug", "page"),
|
|
101
|
+
],
|
|
102
|
+
layouts: [],
|
|
103
|
+
servers: [],
|
|
104
|
+
errors: [],
|
|
105
|
+
...noMatcherFiles,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
test("optional segment consumes one path segment", () => {
|
|
109
|
+
const match = matchRoute(optTree, new URL("http://localhost/en"));
|
|
110
|
+
expect(match?.route.path).toBe("/:lang?");
|
|
111
|
+
expect(match?.params).toEqual({ lang: "en" });
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("optional segment matches zero segments", () => {
|
|
115
|
+
const match = matchRoute(optTree, new URL("http://localhost/"));
|
|
116
|
+
expect(match?.route.path).toBe("/:lang?");
|
|
117
|
+
expect(match?.params).toEqual({});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("static beats optional", () => {
|
|
121
|
+
const match = matchRoute(optTree, new URL("http://localhost/about"));
|
|
122
|
+
expect(match?.route.path).toBe("/about");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("optional beats required param", () => {
|
|
126
|
+
const match = matchRoute(optTree, new URL("http://localhost/en"));
|
|
127
|
+
expect(match?.route.path).toBe("/:lang?");
|
|
128
|
+
expect(match?.route.path).not.toBe("/:slug");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("mid-pattern optional backtracks: /about matches /[[lang]]/about", () => {
|
|
132
|
+
const nested: RouteTree = {
|
|
133
|
+
routes: [makeRoute("/:lang?/about", "page")],
|
|
134
|
+
layouts: [],
|
|
135
|
+
servers: [],
|
|
136
|
+
errors: [],
|
|
137
|
+
...noMatcherFiles,
|
|
138
|
+
};
|
|
139
|
+
const zero = matchRoute(nested, new URL("http://localhost/about"));
|
|
140
|
+
expect(zero?.params).toEqual({});
|
|
141
|
+
const one = matchRoute(nested, new URL("http://localhost/en/about"));
|
|
142
|
+
expect(one?.params).toEqual({ lang: "en" });
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("matchRoute — custom matchers", () => {
|
|
147
|
+
const intMatch = (v: string) => /^\d+$/.test(v);
|
|
148
|
+
const matcherTree: RouteTree = {
|
|
149
|
+
routes: [makeRoute("/post/:id", "page")],
|
|
150
|
+
layouts: [],
|
|
151
|
+
servers: [],
|
|
152
|
+
errors: [],
|
|
153
|
+
...noMatcherFiles,
|
|
154
|
+
};
|
|
155
|
+
matcherTree.routes[0]!.paramMatchers = { id: "int" };
|
|
156
|
+
|
|
157
|
+
test("matcher passes → route matches", () => {
|
|
158
|
+
const match = matchRoute(matcherTree, new URL("http://localhost/post/42"), {
|
|
159
|
+
int: intMatch,
|
|
160
|
+
});
|
|
161
|
+
expect(match?.params).toEqual({ id: "42" });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("matcher fails → route is rejected (404)", () => {
|
|
165
|
+
const match = matchRoute(
|
|
166
|
+
matcherTree,
|
|
167
|
+
new URL("http://localhost/post/abc"),
|
|
168
|
+
{
|
|
169
|
+
int: intMatch,
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
expect(match).toBeNull();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("missing matcher module rejects the route", () => {
|
|
176
|
+
const match = matchRoute(matcherTree, new URL("http://localhost/post/42"));
|
|
177
|
+
expect(match).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("generateManifest — param matchers", () => {
|
|
182
|
+
test("emits eager matcher imports and record", () => {
|
|
183
|
+
const manifest = generateManifest([makeRoute("/", "page")], undefined, [
|
|
184
|
+
{ name: "int", filePath: "/app/params/int.ts" },
|
|
185
|
+
]);
|
|
186
|
+
expect(manifest).toContain(
|
|
187
|
+
'import * as __param_int from "/app/params/int.ts"',
|
|
188
|
+
);
|
|
189
|
+
expect(manifest).toContain('"int": __param_int.match');
|
|
190
|
+
expect(manifest).toContain("export const matchers = {");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("emits matchers record even when none exist", () => {
|
|
194
|
+
const manifest = generateManifest([makeRoute("/", "page")]);
|
|
195
|
+
expect(manifest).toMatch(/export const matchers = \{\s*\};/);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("params are decoded", () => {
|
|
200
|
+
const page = (path: string, clientPath: string) =>
|
|
201
|
+
({ type: "page", path, filePath: `/x${clientPath}`, clientPath }) as never;
|
|
202
|
+
const tree = {
|
|
203
|
+
servers: [],
|
|
204
|
+
layouts: [],
|
|
205
|
+
errors: [],
|
|
206
|
+
routes: [
|
|
207
|
+
page("/blog/:slug", "/src/routes/blog/[slug]/+page.tsx"),
|
|
208
|
+
page("/files/*rest", "/src/routes/files/[...rest]/+page.tsx"),
|
|
209
|
+
],
|
|
210
|
+
} as never;
|
|
211
|
+
const params = (path: string) =>
|
|
212
|
+
matchRoute(tree, new URL(`http://x${path}`))?.params;
|
|
213
|
+
|
|
214
|
+
test("as the page reads them", () => {
|
|
215
|
+
expect(params("/blog/caf%C3%A9%20au%20lait")).toEqual({
|
|
216
|
+
slug: "café au lait",
|
|
217
|
+
});
|
|
218
|
+
expect(params("/blog/100%25")).toEqual({ slug: "100%" });
|
|
219
|
+
expect(params("/files/a%20b/c")).toEqual({ rest: "a b/c" });
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("a malformed escape is passed on as sent", () => {
|
|
223
|
+
expect(params("/blog/%E0%A4%A")).toEqual({ slug: "%E0%A4%A" });
|
|
224
|
+
});
|
|
225
|
+
});
|
package/test/scanning.test.ts
CHANGED
|
@@ -6,33 +6,33 @@ import { filePathToRoutePath, scanRoutes } from "../src/routing/scanner";
|
|
|
6
6
|
|
|
7
7
|
describe("File scanning", () => {
|
|
8
8
|
test("index.tsx → /", () => {
|
|
9
|
-
expect(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
);
|
|
9
|
+
expect(
|
|
10
|
+
filePathToRoutePath("/app/routes/index.tsx", "/app/routes"),
|
|
11
|
+
).toMatchObject({
|
|
12
|
+
pattern: "/",
|
|
13
|
+
params: [],
|
|
14
|
+
optionalParams: [],
|
|
15
|
+
paramMatchers: {},
|
|
16
|
+
restParams: [],
|
|
17
|
+
});
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
test("about.tsx → /about", () => {
|
|
21
|
-
expect(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
);
|
|
21
|
+
expect(
|
|
22
|
+
filePathToRoutePath("/app/routes/about.tsx", "/app/routes"),
|
|
23
|
+
).toMatchObject({
|
|
24
|
+
pattern: "/about",
|
|
25
|
+
params: [],
|
|
26
|
+
optionalParams: [],
|
|
27
|
+
paramMatchers: {},
|
|
28
|
+
restParams: [],
|
|
29
|
+
});
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
test("[slug].tsx → /:slug", () => {
|
|
33
33
|
expect(
|
|
34
34
|
filePathToRoutePath("/app/routes/[slug].tsx", "/app/routes"),
|
|
35
|
-
).
|
|
35
|
+
).toMatchObject({
|
|
36
36
|
pattern: "/:slug",
|
|
37
37
|
params: ["slug"],
|
|
38
38
|
optionalParams: [],
|
|
@@ -44,7 +44,7 @@ describe("File scanning", () => {
|
|
|
44
44
|
test("blog/+page.tsx → /blog", () => {
|
|
45
45
|
expect(
|
|
46
46
|
filePathToRoutePath("/app/routes/blog/+page.tsx", "/app/routes"),
|
|
47
|
-
).
|
|
47
|
+
).toMatchObject({
|
|
48
48
|
pattern: "/blog",
|
|
49
49
|
params: [],
|
|
50
50
|
optionalParams: [],
|
|
@@ -56,7 +56,7 @@ describe("File scanning", () => {
|
|
|
56
56
|
test("blog/[slug]/+page.tsx → /blog/:slug", () => {
|
|
57
57
|
expect(
|
|
58
58
|
filePathToRoutePath("/app/routes/blog/[slug]/+page.tsx", "/app/routes"),
|
|
59
|
-
).
|
|
59
|
+
).toMatchObject({
|
|
60
60
|
pattern: "/blog/:slug",
|
|
61
61
|
params: ["slug"],
|
|
62
62
|
optionalParams: [],
|
|
@@ -71,7 +71,7 @@ describe("File scanning", () => {
|
|
|
71
71
|
"/app/routes/blog/[slug]/+page.server.ts",
|
|
72
72
|
"/app/routes",
|
|
73
73
|
),
|
|
74
|
-
).
|
|
74
|
+
).toMatchObject({
|
|
75
75
|
pattern: "/blog/:slug",
|
|
76
76
|
params: ["slug"],
|
|
77
77
|
optionalParams: [],
|
|
@@ -83,7 +83,7 @@ describe("File scanning", () => {
|
|
|
83
83
|
test("+layout.tsx → /", () => {
|
|
84
84
|
expect(
|
|
85
85
|
filePathToRoutePath("/app/routes/+layout.tsx", "/app/routes"),
|
|
86
|
-
).
|
|
86
|
+
).toMatchObject({
|
|
87
87
|
pattern: "/",
|
|
88
88
|
params: [],
|
|
89
89
|
optionalParams: [],
|
|
@@ -95,7 +95,7 @@ describe("File scanning", () => {
|
|
|
95
95
|
test("nested +layout.tsx → /blog", () => {
|
|
96
96
|
expect(
|
|
97
97
|
filePathToRoutePath("/app/routes/blog/+layout.tsx", "/app/routes"),
|
|
98
|
-
).
|
|
98
|
+
).toMatchObject({
|
|
99
99
|
pattern: "/blog",
|
|
100
100
|
params: [],
|
|
101
101
|
optionalParams: [],
|
|
@@ -107,7 +107,7 @@ describe("File scanning", () => {
|
|
|
107
107
|
test("[...rest].tsx → /*rest (catch-all)", () => {
|
|
108
108
|
expect(
|
|
109
109
|
filePathToRoutePath("/app/routes/[...rest].tsx", "/app/routes"),
|
|
110
|
-
).
|
|
110
|
+
).toMatchObject({
|
|
111
111
|
pattern: "/*rest",
|
|
112
112
|
params: [],
|
|
113
113
|
optionalParams: [],
|
|
@@ -122,7 +122,7 @@ describe("File scanning", () => {
|
|
|
122
122
|
"/app/routes/docs/[...path]/+server.ts",
|
|
123
123
|
"/app/routes",
|
|
124
124
|
),
|
|
125
|
-
).
|
|
125
|
+
).toMatchObject({
|
|
126
126
|
pattern: "/docs/*path",
|
|
127
127
|
params: [],
|
|
128
128
|
optionalParams: [],
|
|
@@ -275,14 +275,25 @@ describe("Duplicate route detection", () => {
|
|
|
275
275
|
test("test files beside routes are not routes", async () => {
|
|
276
276
|
const dir = join(tmpDir, "tests-beside");
|
|
277
277
|
await mkdir(join(dir, "blog"), { recursive: true });
|
|
278
|
-
await writeFile(
|
|
279
|
-
|
|
280
|
-
|
|
278
|
+
await writeFile(
|
|
279
|
+
join(dir, "blog", "+page.tsx"),
|
|
280
|
+
"export default () => null",
|
|
281
|
+
);
|
|
282
|
+
await writeFile(
|
|
283
|
+
join(dir, "blog", "+page.server.ts"),
|
|
284
|
+
"export async function load() {}",
|
|
285
|
+
);
|
|
286
|
+
await writeFile(
|
|
287
|
+
join(dir, "blog", "+page.test.tsx"),
|
|
288
|
+
"export default () => null",
|
|
289
|
+
);
|
|
281
290
|
await writeFile(join(dir, "blog", "load.server.test.ts"), "export {}");
|
|
282
291
|
await writeFile(join(dir, "blog", "util.spec.ts"), "export {}");
|
|
283
292
|
await writeFile(join(dir, "blog", "util_test.ts"), "export {}");
|
|
284
293
|
const tree = await scanRoutes(dir);
|
|
285
|
-
const files = [...tree.routes, ...tree.layouts, ...tree.servers].map(
|
|
294
|
+
const files = [...tree.routes, ...tree.layouts, ...tree.servers].map(
|
|
295
|
+
(r) => r.clientPath,
|
|
296
|
+
);
|
|
286
297
|
expect(files.some((f) => /[._](test|spec)\./.test(f))).toBe(false);
|
|
287
298
|
expect(tree.routes.map((r) => r.path)).toEqual(["/blog", "/blog"]);
|
|
288
299
|
});
|
|
@@ -290,7 +301,10 @@ describe("Duplicate route detection", () => {
|
|
|
290
301
|
test("throws when a +page.server.ts has no page beside it", async () => {
|
|
291
302
|
const dir = join(tmpDir, "orphan-page-server");
|
|
292
303
|
await mkdir(join(dir, "logout"), { recursive: true });
|
|
293
|
-
await writeFile(
|
|
304
|
+
await writeFile(
|
|
305
|
+
join(dir, "logout", "+page.server.ts"),
|
|
306
|
+
"export async function POST() {}",
|
|
307
|
+
);
|
|
294
308
|
const err = await scanRoutes(dir).then(
|
|
295
309
|
() => null,
|
|
296
310
|
(e: Error) => e,
|
|
@@ -302,8 +316,14 @@ describe("Duplicate route detection", () => {
|
|
|
302
316
|
test("a +page.server.ts beside an index.tsx is fine", async () => {
|
|
303
317
|
const dir = join(tmpDir, "simple-plus-server");
|
|
304
318
|
await mkdir(join(dir, "blog"), { recursive: true });
|
|
305
|
-
await writeFile(
|
|
306
|
-
|
|
319
|
+
await writeFile(
|
|
320
|
+
join(dir, "blog", "index.tsx"),
|
|
321
|
+
"export default () => null",
|
|
322
|
+
);
|
|
323
|
+
await writeFile(
|
|
324
|
+
join(dir, "blog", "+page.server.ts"),
|
|
325
|
+
"export async function load() {}",
|
|
326
|
+
);
|
|
307
327
|
await expect(scanRoutes(dir)).resolves.toBeDefined();
|
|
308
328
|
});
|
|
309
329
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
3
|
+
import { runWithContext } from "../src/server/context";
|
|
4
|
+
|
|
5
|
+
describe("server context store", () => {
|
|
6
|
+
test("context set outside a request is kept, and every request sees it", async () => {
|
|
7
|
+
const K = createContext<string>();
|
|
8
|
+
setContext(K, "root");
|
|
9
|
+
expect(getContext(K)).toBe("root");
|
|
10
|
+
expect(await runWithContext(async () => getContext(K))).toBe("root");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("a request's own context does not leak into the root or other requests", async () => {
|
|
14
|
+
const K = createContext<string>();
|
|
15
|
+
await runWithContext(async () => {
|
|
16
|
+
setContext(K, "mine");
|
|
17
|
+
expect(getContext(K)).toBe("mine");
|
|
18
|
+
});
|
|
19
|
+
expect(getContext(K)).toBeUndefined();
|
|
20
|
+
expect(await runWithContext(async () => getContext(K))).toBeUndefined();
|
|
21
|
+
});
|
|
22
|
+
});
|