@teyik0/furin 0.2.0-alpha.1 → 0.2.0-alpha.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/LICENSE +21 -21
- package/README.md +33 -33
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/index.js +1724 -687
- package/dist/build/route-types.d.ts +3 -1
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/build/shared.d.ts +1 -0
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/cli/index.js +1673 -658
- package/dist/client/link.js +883 -89
- package/dist/client/router/index.d.ts +4 -0
- package/dist/client/router/link-utils.d.ts +3 -1
- package/dist/client/router/navigation.d.ts +10 -0
- package/dist/client/router/provider.d.ts +1 -1
- package/dist/client/router/search/index.d.ts +18 -0
- package/dist/client/router/search-store.d.ts +21 -0
- package/dist/client/router/types.d.ts +14 -1
- package/dist/client/sync.d.ts +25 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +44 -0
- package/dist/furin.d.ts +9 -1
- package/dist/furin.js +1578 -258
- package/dist/server/auto-invalidate/runtime.d.ts +1 -1
- package/dist/server/dev-page-plugin.d.ts +3 -0
- package/dist/server/render/assemble.d.ts +2 -0
- package/dist/server/render/isr.d.ts +2 -1
- package/dist/server/render/ssg.d.ts +3 -2
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/router/hmr.d.ts +4 -3
- package/dist/server/router/index.d.ts +1 -1
- package/dist/server/router/index.js +1 -2
- package/dist/server/router/index.ts +1 -2
- package/dist/server/router/plugin.d.ts +2 -1
- package/dist/server/router/schemas.d.ts +25 -17
- package/dist/server/sync/config.d.ts +6 -0
- package/dist/server/sync/index.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +369 -0
- package/dist/server/sync/stream.d.ts +47 -0
- package/dist/shared/search-params.d.ts +17 -0
- package/package.json +10 -5
- package/src/adapter/README.md +13 -13
- package/src/adapter/static.ts +23 -6
- package/src/build/client.ts +152 -149
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -331
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -115
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -78
- package/src/build/ssg-cache.ts +32 -31
- package/src/build/types.ts +89 -89
- package/src/client/link.tsx +13 -4
- package/src/client/router/context.ts +2 -0
- package/src/client/router/index.ts +9 -0
- package/src/client/router/link-utils.ts +184 -180
- package/src/client/router/navigation.ts +61 -0
- package/src/client/router/provider.tsx +804 -723
- package/src/client/router/search/index.ts +99 -0
- package/src/client/router/search-store.ts +86 -0
- package/src/client/router/types.ts +276 -263
- package/src/client/sync.ts +98 -0
- package/src/client.ts +369 -355
- package/src/furin.ts +465 -437
- package/src/server/auto-invalidate/runtime.ts +134 -133
- package/src/server/dev-page-plugin.ts +454 -449
- package/src/server/render/assemble.ts +160 -133
- package/src/server/render/isr.ts +323 -295
- package/src/server/render/not-found.ts +2 -0
- package/src/server/render/ssg.ts +15 -4
- package/src/server/render/ssr.ts +585 -545
- package/src/server/router/hmr.ts +14 -10
- package/src/server/router/index.ts +1 -2
- package/src/server/router/patterns.ts +5 -0
- package/src/server/router/plugin.ts +384 -401
- package/src/server/router/schemas.ts +172 -57
- package/src/server/sync/config.ts +29 -0
- package/src/server/sync/index.ts +14 -0
- package/src/server/sync/plugin.ts +108 -0
- package/src/server/sync/stream.ts +144 -0
- package/src/shared/search-params.ts +182 -0
package/src/furin.ts
CHANGED
|
@@ -1,437 +1,465 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { staticPlugin } from "@elysiajs/static";
|
|
5
|
-
import { type AnyElysia, Elysia, file } from "elysia";
|
|
6
|
-
import { type DrainContext, initLogger } from "evlog";
|
|
7
|
-
import { type EvlogElysiaOptions, evlog } from "evlog/elysia";
|
|
8
|
-
import {
|
|
9
|
-
_runWithRequestInvalidationScope,
|
|
10
|
-
consumePendingInvalidations,
|
|
11
|
-
getBuildId,
|
|
12
|
-
setBuildId,
|
|
13
|
-
} from "./server/cache/invalidation.ts";
|
|
14
|
-
import { setSSGCache } from "./server/cache/ssg.ts";
|
|
15
|
-
import type { CompileContext, EmbeddedAppData } from "./server/internal.ts";
|
|
16
|
-
import { getCompileContext } from "./server/internal.ts";
|
|
17
|
-
import { renderRootNotFound, warmSSGCache } from "./server/render/index.ts";
|
|
18
|
-
import {
|
|
19
|
-
setProductionTemplateContent,
|
|
20
|
-
setProductionTemplatePath,
|
|
21
|
-
} from "./server/render/template.ts";
|
|
22
|
-
import {
|
|
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
|
-
const
|
|
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
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
)
|
|
130
|
-
if (
|
|
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
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
.use(
|
|
318
|
-
.
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
})
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (
|
|
400
|
-
|
|
401
|
-
.get("/favicon.ico",
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { staticPlugin } from "@elysiajs/static";
|
|
5
|
+
import { type AnyElysia, Elysia, file } from "elysia";
|
|
6
|
+
import { type DrainContext, initLogger } from "evlog";
|
|
7
|
+
import { type EvlogElysiaOptions, evlog } from "evlog/elysia";
|
|
8
|
+
import {
|
|
9
|
+
_runWithRequestInvalidationScope,
|
|
10
|
+
consumePendingInvalidations,
|
|
11
|
+
getBuildId,
|
|
12
|
+
setBuildId,
|
|
13
|
+
} from "./server/cache/invalidation.ts";
|
|
14
|
+
import { setSSGCache } from "./server/cache/ssg.ts";
|
|
15
|
+
import type { CompileContext, EmbeddedAppData } from "./server/internal.ts";
|
|
16
|
+
import { getCompileContext } from "./server/internal.ts";
|
|
17
|
+
import { renderRootNotFound, warmSSGCache } from "./server/render/index.ts";
|
|
18
|
+
import {
|
|
19
|
+
setProductionTemplateContent,
|
|
20
|
+
setProductionTemplatePath,
|
|
21
|
+
} from "./server/render/template.ts";
|
|
22
|
+
import {
|
|
23
|
+
createDataEndpoint,
|
|
24
|
+
createRoutePlugin,
|
|
25
|
+
createSearchRouteMetadata,
|
|
26
|
+
loadProdRoutes,
|
|
27
|
+
} from "./server/router/index.ts";
|
|
28
|
+
import { IS_DEV } from "./server/runtime-env.ts";
|
|
29
|
+
import {
|
|
30
|
+
createSyncStreamPlugin,
|
|
31
|
+
type FurinSyncOption,
|
|
32
|
+
resolveSyncStreamPath,
|
|
33
|
+
runWithSyncStreamPath,
|
|
34
|
+
} from "./server/sync/index.ts";
|
|
35
|
+
|
|
36
|
+
// biome-ignore lint/suspicious/noEmptyInterface: intentionally augmentable via furin-env.d.ts
|
|
37
|
+
export interface FurinCacheTags {}
|
|
38
|
+
|
|
39
|
+
export type CacheTag = keyof FurinCacheTags extends never ? string : keyof FurinCacheTags;
|
|
40
|
+
|
|
41
|
+
function resolveClientDirFromArgv(): string {
|
|
42
|
+
return (
|
|
43
|
+
resolveClientDirFromEnv() ??
|
|
44
|
+
resolveClientDirFromModuleUrl() ??
|
|
45
|
+
resolveClientDirFromProcessArgs() ??
|
|
46
|
+
resolveFallbackClientDir()
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolveClientDirFromEnv(): string | null {
|
|
51
|
+
const envClientDir = process.env.FURIN_CLIENT_DIR;
|
|
52
|
+
if (!envClientDir) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return envClientDir.startsWith("/") ? envClientDir : resolve(process.cwd(), envClientDir);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function resolveClientDirFromModuleUrl(): string | null {
|
|
59
|
+
try {
|
|
60
|
+
const moduleUrl = new URL(import.meta.url);
|
|
61
|
+
if (moduleUrl.protocol !== "file:") {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const modulePath = fileURLToPath(moduleUrl);
|
|
65
|
+
if (modulePath.includes("/$bunfs/")) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const moduleClientDir = join(dirname(modulePath), "client");
|
|
69
|
+
if (existsSync(join(moduleClientDir, "index.html"))) {
|
|
70
|
+
return moduleClientDir;
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
// ignore, fallback to argv-based resolution
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function resolveClientDirFromProcessArgs(): string | null {
|
|
79
|
+
const candidates = [
|
|
80
|
+
process.argv[1],
|
|
81
|
+
process.argv[0],
|
|
82
|
+
(process as { argv0?: string }).argv0,
|
|
83
|
+
process.execPath,
|
|
84
|
+
].filter((value): value is string => typeof value === "string" && value.length > 0);
|
|
85
|
+
|
|
86
|
+
for (const candidate of candidates) {
|
|
87
|
+
const resolved = resolveClientDirFromCandidate(candidate);
|
|
88
|
+
if (resolved) {
|
|
89
|
+
return resolved;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function resolveClientDirFromCandidate(candidate: string): string | null {
|
|
97
|
+
const name = basename(candidate);
|
|
98
|
+
if (name === "bun" || name === "node") {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
if (candidate.includes("/$bunfs/") || candidate.startsWith("bunfs:")) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const absolute = candidate.startsWith("/") ? candidate : resolve(process.cwd(), candidate);
|
|
106
|
+
if (existsSync(absolute)) {
|
|
107
|
+
return join(dirname(absolute), "client");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!candidate.includes("/")) {
|
|
111
|
+
return resolveClientDirFromPath(candidate);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function resolveClientDirFromPath(candidate: string): string | null {
|
|
118
|
+
const pathEntries = process.env.PATH?.split(":") ?? [];
|
|
119
|
+
for (const dir of pathEntries) {
|
|
120
|
+
const fullPath = join(dir, candidate);
|
|
121
|
+
if (existsSync(fullPath)) {
|
|
122
|
+
return join(dirname(fullPath), "client");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function resolveFallbackClientDir(): string {
|
|
129
|
+
const defaultClientDir = resolve(process.cwd(), ".furin/build/bun/client");
|
|
130
|
+
if (existsSync(join(defaultClientDir, "index.html"))) {
|
|
131
|
+
return defaultClientDir;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return join(process.cwd(), "client");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function setupProdTemplate(
|
|
138
|
+
embedded: EmbeddedAppData | undefined,
|
|
139
|
+
clientDir: string
|
|
140
|
+
): Promise<void> {
|
|
141
|
+
if (embedded) {
|
|
142
|
+
if (!embedded.template) {
|
|
143
|
+
throw new Error("[furin] Embedded app is missing its HTML template (index.html).");
|
|
144
|
+
}
|
|
145
|
+
const html = await Bun.file(embedded.template).text();
|
|
146
|
+
setProductionTemplateContent(html);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const templatePath = join(clientDir, "index.html");
|
|
151
|
+
if (!existsSync(templatePath)) {
|
|
152
|
+
throw new Error("[furin] No pre-built assets found. Run `bun run build` first.");
|
|
153
|
+
}
|
|
154
|
+
setProductionTemplatePath(templatePath);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Registers a HigherOrderFunction on the Elysia instance so that every request
|
|
159
|
+
* runs inside a fresh `AsyncLocalStorage` scope. This isolates
|
|
160
|
+
* `pendingInvalidations` per request, preventing concurrent requests from
|
|
161
|
+
* stealing each other's `revalidatePath()` calls.
|
|
162
|
+
*
|
|
163
|
+
* Uses `app.wrap()` (Elysia HigherOrderFunction) instead of mutating
|
|
164
|
+
* `app.handle`, because handle mutations are lost when the Furin plugin is
|
|
165
|
+
* `.use()`-d by a parent Elysia instance. HigherOrderFunctions survive the
|
|
166
|
+
* plugin merge and wrap the entire composed `map` handler.
|
|
167
|
+
*/
|
|
168
|
+
function wrapWithRequestScope(app: AnyElysia, syncStreamPath: string | undefined): Elysia {
|
|
169
|
+
return app.wrap(
|
|
170
|
+
(handler, _request) => (ctx: unknown) =>
|
|
171
|
+
runWithSyncStreamPath(syncStreamPath, () =>
|
|
172
|
+
_runWithRequestInvalidationScope(() => handler(ctx))
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
178
|
+
if (!ctx.ssgCache) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
for (const [path, entry] of Object.entries(ctx.ssgCache)) {
|
|
182
|
+
setSSGCache(path, entry);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Main Furin plugin.
|
|
188
|
+
*
|
|
189
|
+
* Returns a standalone Elysia instance (async function) so that routes are
|
|
190
|
+
* properly registered in Elysia's router for SPA navigation to work.
|
|
191
|
+
*
|
|
192
|
+
* ## Usage
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
* new Elysia()
|
|
196
|
+
* .use(await furin({ ... }))
|
|
197
|
+
* .listen(3000)
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export async function furin({
|
|
201
|
+
pagesDir,
|
|
202
|
+
logger,
|
|
203
|
+
clientLogging,
|
|
204
|
+
sync,
|
|
205
|
+
}: {
|
|
206
|
+
pagesDir?: string;
|
|
207
|
+
logger?: EvlogElysiaOptions;
|
|
208
|
+
/**
|
|
209
|
+
* Initialize the browser HTTP log drain in the hydration entry. Off by
|
|
210
|
+
* default — enabling it adds `evlog/http` drain setup and points browser
|
|
211
|
+
* events at `/_furin/ingest`. Server-side logging is configured via `logger`
|
|
212
|
+
* and unaffected.
|
|
213
|
+
*/
|
|
214
|
+
clientLogging?: boolean;
|
|
215
|
+
/**
|
|
216
|
+
* Enables Furin's built-in sync event stream. The opinionated default mounts
|
|
217
|
+
* Server-Sent Events at `/_furin/sync`; pass an object only for constrained
|
|
218
|
+
* reverse-proxy deployments that need a custom internal path.
|
|
219
|
+
*/
|
|
220
|
+
sync?: FurinSyncOption;
|
|
221
|
+
}) {
|
|
222
|
+
const syncStreamPath = resolveSyncStreamPath(sync);
|
|
223
|
+
const { exclude: userExclude, ...evlogOptions } = logger ?? {};
|
|
224
|
+
initLogger({ env: { service: "furin" } });
|
|
225
|
+
|
|
226
|
+
const loggerPlugin = new Elysia()
|
|
227
|
+
.use(
|
|
228
|
+
evlog({
|
|
229
|
+
...evlogOptions,
|
|
230
|
+
exclude: [
|
|
231
|
+
"/_client/**",
|
|
232
|
+
"/public/**",
|
|
233
|
+
"/favicon.ico",
|
|
234
|
+
"/_bun_hmr_entry/**",
|
|
235
|
+
...(syncStreamPath ? [syncStreamPath] : []),
|
|
236
|
+
// Note: /_furin/data is logged with the *logical* path rewritten by
|
|
237
|
+
// createDataEndpoint via useLogger().set({ path }), so SPA navigations
|
|
238
|
+
// appear as "GET /board/123 200" — same shape as a normal SSR nav.
|
|
239
|
+
// /_furin/ingest is kept loggable so browser-side events show up.
|
|
240
|
+
// evlog's `matchesPattern` only supports `*`, `**`, `?` — extglob
|
|
241
|
+
// like `!(...)` matches nothing, so don't add patterns relying on it.
|
|
242
|
+
...(userExclude ?? []),
|
|
243
|
+
],
|
|
244
|
+
})
|
|
245
|
+
)
|
|
246
|
+
.post(
|
|
247
|
+
"/_furin/ingest",
|
|
248
|
+
({ body, log, status }) => {
|
|
249
|
+
if (!Array.isArray(body)) {
|
|
250
|
+
return status("Bad Request");
|
|
251
|
+
}
|
|
252
|
+
// Cap batch size to prevent abuse
|
|
253
|
+
const batch = (body as DrainContext[]).slice(0, 100);
|
|
254
|
+
for (const entry of batch) {
|
|
255
|
+
if (!entry || typeof entry !== "object" || !("event" in entry)) {
|
|
256
|
+
log.set({ msg: "[furin] ingest: skipping malformed entry" });
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
// Pick only safe, known fields from the event to prevent prototype pollution
|
|
260
|
+
const event = entry.event as Record<string, unknown> | undefined;
|
|
261
|
+
if (!event || typeof event !== "object") {
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const {
|
|
265
|
+
__proto__,
|
|
266
|
+
constructor: _ctor,
|
|
267
|
+
prototype,
|
|
268
|
+
environment: _browserEnv,
|
|
269
|
+
...safeEvent
|
|
270
|
+
} = event as Record<string, unknown>;
|
|
271
|
+
log.set({ ...safeEvent, service: "furin:browser" });
|
|
272
|
+
}
|
|
273
|
+
return status("No Content");
|
|
274
|
+
},
|
|
275
|
+
{ parse: "json" }
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const cwd = process.cwd();
|
|
279
|
+
const ctx = getCompileContext();
|
|
280
|
+
const resolvedPagesDir = ctx?.rootPath
|
|
281
|
+
? dirname(ctx.rootPath)
|
|
282
|
+
: resolve(cwd, pagesDir ?? "src/pages");
|
|
283
|
+
|
|
284
|
+
// Unique name per pagesDir to avoid Elysia's name-based plugin dedup.
|
|
285
|
+
const instanceName = `furin-${resolvedPagesDir.replaceAll("\\", "/")}`;
|
|
286
|
+
|
|
287
|
+
// ── Dev: Bun native HMR ────────────────────────────────────────────────
|
|
288
|
+
if (IS_DEV) {
|
|
289
|
+
const furinDir = resolve(cwd, ".furin");
|
|
290
|
+
// Lazy import — build pipeline has native deps not available in compiled binaries
|
|
291
|
+
const { registerDevPagePlugin } = await import("./server/dev-page-plugin.ts");
|
|
292
|
+
registerDevPagePlugin();
|
|
293
|
+
|
|
294
|
+
const { createDevInspectorPlugin } = await import("./server/dev-inspector.ts");
|
|
295
|
+
|
|
296
|
+
const { scanPages } = await import("./server/router/index.ts");
|
|
297
|
+
const { root, routes } = await scanPages(resolvedPagesDir);
|
|
298
|
+
const searchRoutes = createSearchRouteMetadata(routes);
|
|
299
|
+
|
|
300
|
+
const { writeDevFiles } = await import("./build/hydrate.ts");
|
|
301
|
+
writeDevFiles(
|
|
302
|
+
routes,
|
|
303
|
+
{
|
|
304
|
+
outDir: furinDir,
|
|
305
|
+
rootLayout: root.path,
|
|
306
|
+
basePath: "",
|
|
307
|
+
publicPath: "/_client/",
|
|
308
|
+
clientLogging: clientLogging ?? false,
|
|
309
|
+
},
|
|
310
|
+
cwd
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const publicDir = resolve(cwd, "public");
|
|
314
|
+
const publicExists = existsSync(publicDir);
|
|
315
|
+
|
|
316
|
+
const devApp = new Elysia({ name: instanceName, seed: resolvedPagesDir })
|
|
317
|
+
.use(loggerPlugin)
|
|
318
|
+
.onError({ as: "global" }, async ({ code, request }) => {
|
|
319
|
+
if (code === "NOT_FOUND") {
|
|
320
|
+
return await renderRootNotFound(root, request);
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
.onAfterHandle({ as: "global" }, ({ set }) => {
|
|
324
|
+
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
325
|
+
const pending = consumePendingInvalidations();
|
|
326
|
+
if (pending.length > 0) {
|
|
327
|
+
set.headers["x-furin-revalidate"] = pending.join(",");
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
.use(await staticPlugin({ assets: furinDir, prefix: "/_bun_hmr_entry", bunFullstack: true }))
|
|
331
|
+
.use(
|
|
332
|
+
publicExists ? await staticPlugin({ assets: publicDir, prefix: "/public" }) : new Elysia()
|
|
333
|
+
)
|
|
334
|
+
.get(
|
|
335
|
+
"/favicon.ico",
|
|
336
|
+
publicExists
|
|
337
|
+
? file(join(publicDir, "favicon.ico"))
|
|
338
|
+
: () => new Response(null, { status: 404 })
|
|
339
|
+
)
|
|
340
|
+
.use(createDevInspectorPlugin())
|
|
341
|
+
.use(syncStreamPath ? createSyncStreamPlugin(syncStreamPath) : new Elysia())
|
|
342
|
+
.use(createDataEndpoint(routes))
|
|
343
|
+
.use((app) => {
|
|
344
|
+
for (const route of routes) {
|
|
345
|
+
app.use(createRoutePlugin(route, root, undefined, searchRoutes));
|
|
346
|
+
}
|
|
347
|
+
return app;
|
|
348
|
+
});
|
|
349
|
+
return wrapWithRequestScope(devApp, syncStreamPath);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ── Production ──────────────────────────────────────────────────────────
|
|
353
|
+
if (!ctx) {
|
|
354
|
+
throw new Error("[furin] No pre-built assets found. Run `bunx furin build` first.");
|
|
355
|
+
}
|
|
356
|
+
const { root, routes } = loadProdRoutes(ctx);
|
|
357
|
+
const searchRoutes = createSearchRouteMetadata(routes);
|
|
358
|
+
const prodBuildId = ctx.buildId ?? "";
|
|
359
|
+
setBuildId(prodBuildId);
|
|
360
|
+
hydrateSSGCacheFromCompileContext(ctx);
|
|
361
|
+
|
|
362
|
+
const embedded = ctx?.embedded;
|
|
363
|
+
const clientDir = embedded ? "" : resolveClientDirFromArgv();
|
|
364
|
+
await setupProdTemplate(embedded, clientDir);
|
|
365
|
+
|
|
366
|
+
const prodApp = new Elysia({ name: instanceName, seed: resolvedPagesDir })
|
|
367
|
+
.use(loggerPlugin)
|
|
368
|
+
.onError({ as: "global" }, async ({ code, request }) => {
|
|
369
|
+
if (code === "NOT_FOUND") {
|
|
370
|
+
return await renderRootNotFound(root, request);
|
|
371
|
+
}
|
|
372
|
+
})
|
|
373
|
+
.onAfterHandle({ as: "global" }, ({ path, set }) => {
|
|
374
|
+
// Content-hashed client assets are permanently cacheable — browsers never need to
|
|
375
|
+
// revalidate them because any change produces a new filename.
|
|
376
|
+
if (path.startsWith("/_client/")) {
|
|
377
|
+
set.headers["cache-control"] = "public, max-age=31536000, immutable";
|
|
378
|
+
}
|
|
379
|
+
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
380
|
+
const pending = consumePendingInvalidations();
|
|
381
|
+
if (pending.length > 0) {
|
|
382
|
+
set.headers["x-furin-revalidate"] = pending.join(",");
|
|
383
|
+
}
|
|
384
|
+
// Tell the client the current build ID so it can detect stale deploys
|
|
385
|
+
const buildId = getBuildId();
|
|
386
|
+
if (buildId) {
|
|
387
|
+
set.headers["x-furin-build-id"] = buildId;
|
|
388
|
+
}
|
|
389
|
+
})
|
|
390
|
+
.onStart(async ({ server }) => {
|
|
391
|
+
if (ctx.ssgCache) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const origin = server?.url?.origin ?? "http://localhost:3000";
|
|
395
|
+
await warmSSGCache(routes, root, origin, searchRoutes);
|
|
396
|
+
})
|
|
397
|
+
.use(
|
|
398
|
+
await (async () => {
|
|
399
|
+
if (embedded) {
|
|
400
|
+
return new Elysia()
|
|
401
|
+
.get("/favicon.ico", ({ status }) => {
|
|
402
|
+
const asset = embedded.assets["/public/favicon.ico"];
|
|
403
|
+
if (!asset) {
|
|
404
|
+
return status("Not Found");
|
|
405
|
+
}
|
|
406
|
+
return Bun.file(asset);
|
|
407
|
+
})
|
|
408
|
+
.get("/_client/*", ({ params, status }) => {
|
|
409
|
+
const asset = embedded.assets[`/_client/${params["*"]}`];
|
|
410
|
+
if (!asset) {
|
|
411
|
+
return status("Not Found");
|
|
412
|
+
}
|
|
413
|
+
return Bun.file(asset);
|
|
414
|
+
})
|
|
415
|
+
.get("/public/*", ({ params, status }) => {
|
|
416
|
+
const asset = embedded.assets[`/public/${params["*"]}`];
|
|
417
|
+
if (!asset) {
|
|
418
|
+
return status("Not Found");
|
|
419
|
+
}
|
|
420
|
+
return Bun.file(asset);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
const publicDir = join(dirname(clientDir), "public");
|
|
424
|
+
const app = new Elysia();
|
|
425
|
+
if (existsSync(publicDir)) {
|
|
426
|
+
app
|
|
427
|
+
.get("/favicon.ico", file(join(publicDir, "favicon.ico")))
|
|
428
|
+
.use(await staticPlugin({ assets: publicDir, prefix: "/public" }));
|
|
429
|
+
}
|
|
430
|
+
app.use(await staticPlugin({ assets: clientDir, prefix: "/_client" }));
|
|
431
|
+
return app;
|
|
432
|
+
})()
|
|
433
|
+
)
|
|
434
|
+
.use(syncStreamPath ? createSyncStreamPlugin(syncStreamPath) : new Elysia())
|
|
435
|
+
.use(createDataEndpoint(routes))
|
|
436
|
+
.use((app) => {
|
|
437
|
+
for (const route of routes) {
|
|
438
|
+
app.use(createRoutePlugin(route, root, prodBuildId, searchRoutes));
|
|
439
|
+
}
|
|
440
|
+
return app;
|
|
441
|
+
});
|
|
442
|
+
return wrapWithRequestScope(prodApp, syncStreamPath);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// biome-ignore lint/performance/noBarrelFile: furin.ts is the public package entry
|
|
446
|
+
export { FurinErrorBoundary, FurinNotFoundBoundary } from "./client/boundaries.tsx";
|
|
447
|
+
// ── Public API re-export ──────────────────────────────────────────────────────
|
|
448
|
+
// biome-ignore-start lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
449
|
+
export type { DeferredData, RuntimePage, RuntimeRoute } from "./client.ts";
|
|
450
|
+
export { defer, isDeferred } from "./client.ts";
|
|
451
|
+
export type { InvalidationInput, InvalidationRule } from "./server/auto-invalidate/index.ts";
|
|
452
|
+
export { furinInvalidate, revalidateTag } from "./server/auto-invalidate/index.ts";
|
|
453
|
+
export { revalidatePath, setCachePurger } from "./server/cache/invalidation.ts";
|
|
454
|
+
export { buildElement, buildErrorElement, renderRootNotFound } from "./server/render/index.ts";
|
|
455
|
+
export type { ResolvedRoute, SegmentBoundary } from "./server/router/index.ts";
|
|
456
|
+
export { furinSync, type SyncInput } from "./server/sync/index.ts";
|
|
457
|
+
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
458
|
+
export type { ErrorComponent, ErrorProps } from "./shared/error.ts";
|
|
459
|
+
export type {
|
|
460
|
+
NotFoundComponent,
|
|
461
|
+
NotFoundOptions,
|
|
462
|
+
NotFoundProps,
|
|
463
|
+
} from "./shared/not-found.ts";
|
|
464
|
+
export { isNotFoundError, notFound } from "./shared/not-found.ts";
|
|
465
|
+
// biome-ignore-end lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|