1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
import { FileSystemRouter } from "bun";
import { it, expect } from "bun:test";
import path, { dirname, resolve } from "path";
import fs, { mkdirSync, realpathSync, rmSync } from "fs";
import { tmpdir } from "os";
const tempdir = realpathSync(tmpdir()) + "/";
function createTree(basedir, paths) {
for (const end of paths) {
const abs = path.join(basedir, end);
try {
const dir = dirname(abs);
if (dir.length > 0 && dir !== "/") fs.mkdirSync(dir, { recursive: true });
} catch (e) {}
fs.writeFileSync(abs, "export default " + JSON.stringify(end) + ";\n");
}
}
var count = 0;
function make(files) {
const dir = tempdir + `fs-router-test-${count++}`;
rmSync(dir, {
recursive: true,
force: true,
});
createTree(dir, files);
if (files.length === 0) mkdirSync(dir, { recursive: true });
return {
dir,
};
}
it("should find files", () => {
const { dir } = make([
`index.tsx`,
`[id].tsx`,
`a.tsx`,
`abc/index.tsx`,
`abc/[id].tsx`,
`abc/def/[id].tsx`,
`abc/def/ghi/index.tsx`,
`abc/def/ghi/[id].tsx`,
`abc/def/ghi/jkl/index.tsx`,
`abc/def/ghi/jkl/[id].tsx`,
`abc/def/index.tsx`,
`b.tsx`,
`foo/[id].tsx`,
`catch-all/[[...id]].tsx`,
]);
const router = new FileSystemRouter({
dir,
fileExtensions: [".tsx"],
style: "nextjs",
});
const routes = router.routes;
const fixture = {
"/": `${dir}/index.tsx`,
"/[id]": `${dir}/[id].tsx`,
"/a": `${dir}/a.tsx`,
"/abc": `${dir}/abc/index.tsx`,
"/abc/[id]": `${dir}/abc/[id].tsx`,
"/abc/def/[id]": `${dir}/abc/def/[id].tsx`,
"/abc/def/ghi": `${dir}/abc/def/ghi/index.tsx`,
"/abc/def/ghi/[id]": `${dir}/abc/def/ghi/[id].tsx`,
"/abc/def/ghi/jkl": `${dir}/abc/def/ghi/jkl/index.tsx`,
"/abc/def/ghi/jkl/[id]": `${dir}/abc/def/ghi/jkl/[id].tsx`,
"/abc/def": `${dir}/abc/def/index.tsx`,
"/b": `${dir}/b.tsx`,
"/foo/[id]": `${dir}/foo/[id].tsx`,
"/catch-all/[[...id]]": `${dir}/catch-all/[[...id]].tsx`,
};
for (const route in fixture) {
if (!(route in routes)) {
throw new Error(`Route ${route} not found`);
}
expect(routes[route]).toBe(fixture[route]);
}
expect(Object.keys(routes).length).toBe(Object.keys(fixture).length);
expect(Object.values(routes).length).toBe(Object.values(fixture).length);
});
it("should handle empty dirs", () => {
const { dir } = make([]);
const router = new FileSystemRouter({
dir,
fileExtensions: [".tsx"],
style: "nextjs",
});
// assert this doesn't crash
expect(router.bar).toBeUndefined();
const routes = router.routes;
expect(Object.keys(routes).length).toBe(0);
expect(Object.values(routes).length).toBe(0);
});
it("should support dynamic routes", () => {
// set up the test
const { dir } = make(["index.tsx", "posts/[id].tsx", "posts.tsx"]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
});
const {
name,
params: { id },
filePath,
} = router.match("/posts/hello-world");
expect(id).toBe("hello-world");
expect(name).toBe("/posts/[id]");
expect(filePath).toBe(`${dir}/[id].tsx`);
});
it("should support static routes", () => {
// set up the test
const { dir } = make([
"index.tsx",
"posts/[id].tsx",
"posts.tsx",
"posts/hey.tsx",
]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
});
const { name, params, filePath } = router.match("/posts/hey");
expect(name).toBe("/posts/hey");
expect(filePath).toBe(`${dir}/posts/hey.tsx`);
});
it("should support optional catch-all routes", () => {
// set up the test
const { dir } = make([
"index.tsx",
"posts/[id].tsx",
"posts.tsx",
"posts/hey.tsx",
"posts/[[...id]].tsx",
]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
});
for (let fixture of [
"/posts/123",
"/posts/hey",
"/posts/zorp",
"/posts",
"/index",
"/posts/",
]) {
expect(router.match(fixture)?.name).not.toBe("/posts/[[...id]]");
}
for (let fixture of [
"/posts/hey/there",
"/posts/hey/there/you",
"/posts/zorp/123",
]) {
const { name, params, filePath } = router.match(fixture);
expect(name).toBe("/posts/[[...id]]");
expect(filePath).toBe(`${dir}/posts/[[...id]].tsx`);
expect(params.id).toBe(fixture.split("/").slice(2).join("/"));
}
});
it("should support catch-all routes", () => {
// set up the test
const { dir } = make([
"index.tsx",
"posts/[id].tsx",
"posts.tsx",
"posts/hey.tsx",
"posts/[...id].tsx",
"posts/wow/[[...id]].tsx",
]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
});
for (let fixture of [
"/posts/123",
"/posts/hey",
"/posts/zorp",
"/posts",
"/index",
"/posts/",
]) {
expect(router.match(fixture)?.name).not.toBe("/posts/[...id]");
}
for (let fixture of [
"/posts/hey/there",
"/posts/hey/there/you",
"/posts/zorp/123",
"/posts/wow/hey/there",
]) {
const { name, params, filePath } = router.match(fixture);
expect(name).toBe("/posts/[...id]");
expect(filePath).toBe(`${dir}/posts/[...id].tsx`);
expect(params.id).toBe(fixture.split("/").slice(2).join("/"));
}
});
it("should support index routes", () => {
// set up the test
const { dir } = make([
"index.tsx",
"posts/[id].tsx",
"posts.tsx",
"posts/hey.tsx",
]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
});
for (let route of ["/", "/index"]) {
const { name, params, filePath } = router.match(route);
expect(name).toBe("/");
expect(filePath).toBe(`${dir}/index.tsx`);
expect(Object.keys(params).length).toBe(0);
}
for (let route of ["/posts", "/posts/index", "/posts/"]) {
const { name, params, filePath } = router.match(route);
expect(name).toBe("/posts");
expect(filePath).toBe(`${dir}/posts.tsx`);
expect(Object.keys(params).length).toBe(0);
}
});
it("should support Request", async () => {
// set up the test
const { dir } = make(["index.tsx", "posts/[id].tsx", "posts.tsx"]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
});
for (let current of [
new Request({ url: "/posts/hello-world" }),
new Request({ url: "http://example.com/posts/hello-world" }),
]) {
const {
name,
params: { id },
filePath,
} = router.match(current);
expect(name).toBe("/posts/[id]");
expect(filePath).toBe(`${dir}/posts/[id].tsx`);
expect(id).toBe("hello-world");
}
});
it("assetPrefix, src, and origin", async () => {
// set up the test
const { dir } = make(["index.tsx", "posts/[id].tsx", "posts.tsx"]);
const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
assetPrefix: "/_next/static/",
origin: "https://nextjs.org",
});
for (let current of [
// Reuqest
new Request({ url: "/posts/hello-world" }),
new Request({ url: "https://nextjs.org/posts/hello-world" }),
]) {
const { name, src, filePath, checkThisDoesntCrash } = router.match(current);
expect(name).toBe("/posts/[id]");
// check nothing is weird on the MatchedRoute object
expect(checkThisDoesntCrash).toBeUndefined();
expect(src).toBe("https://nextjs.org/_next/static/posts/[id].tsx");
expect(filePath).toBe(`${dir}/posts/[id].tsx`);
}
});
|