@teyik0/furin 0.4.0-alpha.2 → 0.5.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build/compile-entry.d.ts +2 -1
- package/dist/build/compile-entry.ts +17 -13
- package/dist/build/elysia-aot.d.ts +9 -0
- package/dist/build/entry-template.d.ts +5 -1
- package/dist/build/entry-template.ts +27 -6
- package/dist/build/index.js +969 -528
- package/dist/build/private-server-sourcemaps.d.ts +2 -0
- package/dist/build/server-app-entry.d.ts +12 -0
- package/dist/build/types.d.ts +2 -0
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +185 -0
- package/dist/cli/index.js +983 -541
- package/dist/client/document.d.ts +2 -0
- package/dist/client/internal/runtime-types.d.ts +8 -1
- package/dist/client/link.js +232 -11
- package/dist/client/sync.d.ts +1 -0
- package/dist/client.js +11 -0
- package/dist/config.d.ts +28 -22
- package/dist/config.js +25 -2
- package/dist/define-route.d.ts +273 -1009
- package/dist/furin.d.ts +20 -5
- package/dist/furin.js +6431 -5457
- package/dist/plugin/routes.d.ts +6 -0
- package/dist/rsc-client.js +9 -1
- package/dist/rsc.js +19 -7
- package/dist/server/assets/production.d.ts +3 -0
- package/dist/server/auto-invalidate/plugin.d.ts +8 -24
- package/dist/server/auto-invalidate/runtime.d.ts +2 -2
- package/dist/server/browser-events/shutdown.d.ts +3 -0
- package/dist/server/cache/invalidation.d.ts +1 -1
- package/dist/server/cache/page-cache-state.d.ts +5 -0
- package/dist/server/cache/page-cache.d.ts +53 -0
- package/dist/server/cache/redis/index.d.ts +41 -0
- package/dist/server/cache/redis/index.js +451 -0
- package/dist/server/cache/redis/scripts.d.ts +6 -0
- package/dist/server/context-logger.d.ts +2 -2
- package/dist/server/dev/plugin.d.ts +1 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +7 -0
- package/dist/server/evlog.d.ts +14 -0
- package/dist/server/internal.d.ts +6 -0
- package/dist/server/internal.js +2539 -0
- package/dist/server/production-server.d.ts +13 -0
- package/dist/server/production-server.js +2906 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ssg.d.ts +5 -0
- package/dist/server/render/static-params.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +6 -0
- package/dist/server/router/hmr.production.d.ts +5 -0
- package/dist/server/router/plugin.d.ts +5 -1
- package/dist/server/router/schemas.d.ts +1 -0
- package/dist/server/sync/index.js +798 -117
- package/dist/server/sync/plugin.d.ts +31 -158
- package/dist/server/sync/stream.d.ts +38 -49
- package/dist/shared/elysia-contract.d.ts +8 -14
- package/dist/shared/production-build.d.ts +2 -0
- package/package.json +19 -8
- package/src/adapter/README.md +9 -2
- package/src/adapter/bun.ts +146 -41
- package/src/adapter/package.ts +6 -2
- package/src/adapter/runtime-build.ts +11 -4
- package/src/adapter/static.ts +8 -6
- package/src/adapter/vercel.ts +64 -31
- package/src/build/client.ts +18 -1
- package/src/build/compile-entry.ts +17 -13
- package/src/build/elysia-aot.ts +102 -0
- package/src/build/entry-template.ts +27 -6
- package/src/build/hydrate.ts +5 -1
- package/src/build/index.ts +5 -5
- package/src/build/private-server-sourcemaps.ts +20 -0
- package/src/build/production-instrumentation.ts +2 -2
- package/src/build/server-app-entry.ts +61 -0
- package/src/build/shared.ts +3 -1
- package/src/build/ssg-cache.ts +5 -10
- package/src/build/types.ts +2 -0
- package/src/build/virtual-entry.ts +5 -4
- package/src/cache.ts +12 -0
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +6 -2
- package/src/client/document.tsx +19 -0
- package/src/client/internal/runtime-types.ts +10 -1
- package/src/client/sync.ts +1 -0
- package/src/config.ts +33 -4
- package/src/define-route.ts +266 -128
- package/src/furin.ts +168 -154
- package/src/plugin/route-config-autofix.ts +11 -2
- package/src/plugin/routes.ts +85 -29
- package/src/rsc/codec.ts +11 -6
- package/src/rsc/render-error.ts +3 -1
- package/src/server/assets/production.ts +99 -0
- package/src/server/auto-invalidate/plugin.ts +4 -4
- package/src/server/auto-invalidate/runtime.ts +74 -42
- package/src/server/browser-events/plugin.ts +13 -1
- package/src/server/browser-events/shutdown.ts +32 -0
- package/src/server/cache/invalidation.ts +61 -20
- package/src/server/cache/page-cache-state.ts +22 -0
- package/src/server/cache/page-cache.ts +300 -0
- package/src/server/cache/redis/index.ts +367 -0
- package/src/server/cache/redis/scripts.ts +200 -0
- package/src/server/context-logger.ts +5 -5
- package/src/server/dev/plugin.ts +10 -1
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev-page-plugin.ts +54 -10
- package/src/server/evlog.ts +106 -0
- package/src/server/internal.ts +7 -0
- package/src/server/production-server.ts +135 -0
- package/src/server/render/document.tsx +7 -1
- package/src/server/render/isr.ts +480 -132
- package/src/server/render/loaders.ts +18 -12
- package/src/server/render/not-found.ts +4 -4
- package/src/server/render/ppr-document.ts +2 -2
- package/src/server/render/ppr-route.ts +193 -7
- package/src/server/render/ssg.ts +148 -4
- package/src/server/render/ssr.ts +6 -7
- package/src/server/render/static-params.ts +174 -0
- package/src/server/render/template.ts +5 -1
- package/src/server/router/defined-route.ts +2 -1
- package/src/server/router/discovery.ts +3 -1
- package/src/server/router/hmr.production.ts +11 -0
- package/src/server/router/hmr.ts +85 -76
- package/src/server/router/plugin.ts +118 -50
- package/src/server/router/schema-merge.ts +10 -3
- package/src/server/router/schemas.ts +174 -61
- package/src/server/sync/plugin.ts +14 -10
- package/src/server/sync/response.ts +3 -3
- package/src/server/sync/stream.ts +43 -2
- package/src/shared/elysia-contract.ts +23 -25
- package/src/shared/production-build.ts +20 -0
|
@@ -33,18 +33,8 @@ function buildEmbeddedContext(
|
|
|
33
33
|
];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* 1. Statically imports every page module of every app so Bun bundles them
|
|
39
|
-
* 2. Points embed-mode contexts at Bun's `compile.assets` directories
|
|
40
|
-
* 3. Sets production mode and registers one CompileContext PER APP
|
|
41
|
-
* 4. Dynamically imports server.ts to boot the composed app
|
|
42
|
-
*/
|
|
43
|
-
export function generateCompileEntry(options: BuildEntryOptions): VirtualBuildEntry {
|
|
44
|
-
const { apps, outDir, serverEntry, publicDir } = options;
|
|
45
|
-
ensureDir(outDir);
|
|
46
|
-
|
|
47
|
-
const preparedApps: EntryAppContext[] = apps.map((app) => {
|
|
36
|
+
export function prepareCompileEntryApps(options: BuildEntryOptions): EntryAppContext[] {
|
|
37
|
+
return options.apps.map((app) => {
|
|
48
38
|
const { embed, ...context } = app;
|
|
49
39
|
if (!embed) {
|
|
50
40
|
return context;
|
|
@@ -53,10 +43,24 @@ export function generateCompileEntry(options: BuildEntryOptions): VirtualBuildEn
|
|
|
53
43
|
...context,
|
|
54
44
|
extraContext: [
|
|
55
45
|
...(context.extraContext ?? []),
|
|
56
|
-
...buildEmbeddedContext(embed.clientDir, publicDir),
|
|
46
|
+
...buildEmbeddedContext(embed.clientDir, options.publicDir),
|
|
57
47
|
],
|
|
58
48
|
};
|
|
59
49
|
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generates a virtual `_compile-entry.ts` that:
|
|
54
|
+
* 1. Statically imports every page module of every app so Bun bundles them
|
|
55
|
+
* 2. Points embed-mode contexts at Bun's `compile.assets` directories
|
|
56
|
+
* 3. Sets production mode and registers one CompileContext PER APP
|
|
57
|
+
* 4. Dynamically imports server.ts to boot the composed app
|
|
58
|
+
*/
|
|
59
|
+
export function generateCompileEntry(options: BuildEntryOptions): VirtualBuildEntry {
|
|
60
|
+
const { outDir, serverEntry } = options;
|
|
61
|
+
ensureDir(outDir);
|
|
62
|
+
|
|
63
|
+
const preparedApps = prepareCompileEntryApps(options);
|
|
60
64
|
|
|
61
65
|
const source = buildEntrySource({
|
|
62
66
|
apps: preparedApps,
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { aotFactory } from "elysia/plugin/aot/unplugin";
|
|
3
|
+
|
|
4
|
+
const AOT_NAMESPACE = "furin-elysia-aot";
|
|
5
|
+
const ELYSIA_RESOLVE_DIR = dirname(Bun.resolveSync("elysia", import.meta.dir));
|
|
6
|
+
const SOURCE_FILTER = /\.[cm]?[jt]sx?$/;
|
|
7
|
+
const WEBSOCKET_STUB_MARKER =
|
|
8
|
+
"[elysia-aot] WebSocket route builder was stripped (strip mode) but a WS route was used.";
|
|
9
|
+
|
|
10
|
+
function loaderFor(path: string): Bun.Loader {
|
|
11
|
+
const extension = path.slice(path.lastIndexOf("."));
|
|
12
|
+
if (extension === ".jsx") {
|
|
13
|
+
return "jsx";
|
|
14
|
+
}
|
|
15
|
+
if (extension === ".tsx") {
|
|
16
|
+
return "tsx";
|
|
17
|
+
}
|
|
18
|
+
if (extension === ".ts" || extension === ".mts" || extension === ".cts") {
|
|
19
|
+
return "ts";
|
|
20
|
+
}
|
|
21
|
+
return "js";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Kiana strips the WebSocket route module when an app has no WS route,
|
|
26
|
+
* but its capability plugin still imports four helpers omitted by that stub.
|
|
27
|
+
* Keep the upstream failure semantics while restoring the complete export
|
|
28
|
+
* surface. Remove this compatibility shim when Elysia ships the full stub.
|
|
29
|
+
*/
|
|
30
|
+
export function patchElysiaWebSocketStub(source: string): string {
|
|
31
|
+
if (!source.includes(WEBSOCKET_STUB_MARKER) || source.includes("accumulateWSOptions")) {
|
|
32
|
+
return source;
|
|
33
|
+
}
|
|
34
|
+
return `${source}
|
|
35
|
+
export function accumulateWSOptions(){return e()}
|
|
36
|
+
export function resolveWSOptions(){return e()}
|
|
37
|
+
export function drainWaiters(){return e()}
|
|
38
|
+
export function handleWSResponse(){return e()}
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Elysia AOT plugin with the no-WebSocket stub compatibility fix. */
|
|
43
|
+
export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
44
|
+
const entryPath = resolve(entry);
|
|
45
|
+
const hooks = aotFactory({ entry, strip: false, target: "bun" });
|
|
46
|
+
return {
|
|
47
|
+
name: "elysia-aot",
|
|
48
|
+
async setup(build) {
|
|
49
|
+
try {
|
|
50
|
+
await hooks.buildStart?.();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (
|
|
53
|
+
error instanceof Error &&
|
|
54
|
+
error.message.startsWith("[elysia-aot]") &&
|
|
55
|
+
error.message.includes("mounts a sub-app")
|
|
56
|
+
) {
|
|
57
|
+
console.warn("[furin] Elysia AOT skipped because the app uses .mount().");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
const entrySource = await Bun.file(entryPath).text();
|
|
63
|
+
const transformedEntry = await hooks.transform?.(entrySource, entryPath);
|
|
64
|
+
if (transformedEntry !== undefined) {
|
|
65
|
+
await Bun.write(entryPath, transformedEntry);
|
|
66
|
+
}
|
|
67
|
+
build.onResolve({ filter: /^elysia(?:\/(?!compiled$|type$).*)?$/ }, ({ path }) => ({
|
|
68
|
+
path: Bun.resolveSync(path, ELYSIA_RESOLVE_DIR),
|
|
69
|
+
}));
|
|
70
|
+
build.onResolve({ filter: /^elysia\/(?:compiled|type)$/ }, ({ path }) => {
|
|
71
|
+
const resolved = hooks.resolveId?.(path);
|
|
72
|
+
return resolved === undefined ? undefined : { namespace: AOT_NAMESPACE, path: resolved };
|
|
73
|
+
});
|
|
74
|
+
build.onLoad({ filter: /.*/, namespace: AOT_NAMESPACE }, ({ path }) => {
|
|
75
|
+
const contents = hooks.load?.(path);
|
|
76
|
+
return contents === undefined
|
|
77
|
+
? undefined
|
|
78
|
+
: { contents, loader: "js", resolveDir: ELYSIA_RESOLVE_DIR };
|
|
79
|
+
});
|
|
80
|
+
build.onLoad({ filter: SOURCE_FILTER }, async ({ path }) => {
|
|
81
|
+
if (resolve(path) === entryPath) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (hooks.transformInclude?.(path) === false) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const original = await Bun.file(path).text();
|
|
88
|
+
const transformed = await hooks.transform?.(original, path);
|
|
89
|
+
if (transformed === undefined) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
contents: patchElysiaWebSocketStub(transformed),
|
|
94
|
+
loader: loaderFor(path),
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
build.onEnd(() => {
|
|
98
|
+
hooks.buildEnd?.();
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -22,6 +22,8 @@ export interface EntryAppContext {
|
|
|
22
22
|
buildId?: string;
|
|
23
23
|
/** Whether the emitted hydration client sends browser log batches. */
|
|
24
24
|
clientLogging?: boolean;
|
|
25
|
+
/** Deployment adapter that owns public page caching for this build. */
|
|
26
|
+
deploymentTarget?: "vercel";
|
|
25
27
|
/** Extra lines injected inside this app's `__setCompileContext({...})` call. */
|
|
26
28
|
extraContext?: string[];
|
|
27
29
|
/** Additional route modules such as filesystem-derived `_route` layouts. */
|
|
@@ -57,13 +59,15 @@ export interface EntryTemplateOptions {
|
|
|
57
59
|
/**
|
|
58
60
|
* - "boot" (default): forces production mode and dynamically imports the
|
|
59
61
|
* server entry after registering contexts — the whole app's entrypoint.
|
|
62
|
+
* - "app": registers contexts and exports the user's Elysia app without
|
|
63
|
+
* starting a listener, so build-time tooling can safely import it.
|
|
60
64
|
* - "register": ONLY registers contexts. Emitted by `--target package` as a
|
|
61
65
|
* side-effect module the HOST app imports; it must not touch dev mode or
|
|
62
66
|
* NODE_ENV (the host decides), and it imports furin internals via the
|
|
63
67
|
* package specifier so the host and the register module share ONE copy of
|
|
64
68
|
* the context registry (`packages: "external"` keeps it unbundled).
|
|
65
69
|
*/
|
|
66
|
-
mode?: "boot" | "register";
|
|
70
|
+
mode?: "app" | "boot" | "register";
|
|
67
71
|
/**
|
|
68
72
|
* Server entry to boot after context registration. Omitted in package
|
|
69
73
|
* (register-module) mode where the entry only registers contexts.
|
|
@@ -152,6 +156,10 @@ function buildAppContextBlock(
|
|
|
152
156
|
const routeMetadataLine = app.routeMetadata
|
|
153
157
|
? ` routeMetadata: ${JSON.stringify(app.routeMetadata)},`
|
|
154
158
|
: "";
|
|
159
|
+
const deploymentTargetLine =
|
|
160
|
+
app.deploymentTarget === undefined
|
|
161
|
+
? ""
|
|
162
|
+
: ` deploymentTarget: ${JSON.stringify(app.deploymentTarget)},`;
|
|
155
163
|
const serveAssetsLine =
|
|
156
164
|
app.serveAssets === undefined ? "" : ` serveAssets: ${JSON.stringify(app.serveAssets)},`;
|
|
157
165
|
const ssgCacheLine = app.ssgCache ? ` ssgCache: ${JSON.stringify(app.ssgCache)},` : "";
|
|
@@ -162,6 +170,7 @@ function buildAppContextBlock(
|
|
|
162
170
|
"__setCompileContext({",
|
|
163
171
|
` buildId: ${JSON.stringify(app.buildId ?? "")},`,
|
|
164
172
|
` clientLogging: ${JSON.stringify(app.clientLogging ?? false)},`,
|
|
173
|
+
deploymentTargetLine,
|
|
165
174
|
` prefix: ${JSON.stringify(app.prefix ?? "")},`,
|
|
166
175
|
` rootPath: ${JSON.stringify(app.rootPath.replace(/\\/g, "/"))},`,
|
|
167
176
|
app.nativeRoutes ? ` nativeRoutes: ${nativeRoutesVar},` : "",
|
|
@@ -188,6 +197,7 @@ export function buildEntrySource(options: EntryTemplateOptions): string {
|
|
|
188
197
|
const mode = options.mode ?? "boot";
|
|
189
198
|
const internalSpecifier =
|
|
190
199
|
mode === "register" ? "@teyik0/furin/internal" : INTERNAL_MODULE_PATH;
|
|
200
|
+
const productionMode = mode !== "register";
|
|
191
201
|
|
|
192
202
|
const importLines: string[] = [];
|
|
193
203
|
const contextBlocks: string[] = [];
|
|
@@ -205,21 +215,21 @@ export function buildEntrySource(options: EntryTemplateOptions): string {
|
|
|
205
215
|
const lines = [
|
|
206
216
|
headerComment,
|
|
207
217
|
`import { __setCompileContext } from ${JSON.stringify(internalSpecifier)};`,
|
|
208
|
-
...(
|
|
218
|
+
...(productionMode
|
|
209
219
|
? [`import { __setDevMode } from ${JSON.stringify(RUNTIME_ENV_MODULE_PATH)};`]
|
|
210
220
|
: []),
|
|
211
221
|
...importLines,
|
|
212
|
-
...(
|
|
222
|
+
...(productionMode
|
|
213
223
|
? [
|
|
214
224
|
"",
|
|
215
225
|
"// Force production mode — Bun may inline process.env.NODE_ENV at bundle time.",
|
|
216
226
|
"__setDevMode(false);",
|
|
217
|
-
'process.env.NODE_ENV = "production";',
|
|
227
|
+
...(mode === "boot" ? ['process.env.NODE_ENV = "production";'] : []),
|
|
218
228
|
]
|
|
219
229
|
: []),
|
|
220
230
|
...contextBlocks,
|
|
221
231
|
"",
|
|
222
|
-
...(serverEntry
|
|
232
|
+
...(serverEntry && mode === "boot"
|
|
223
233
|
? [
|
|
224
234
|
`const __serverModule = await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`,
|
|
225
235
|
"if (typeof __serverModule.startServer === \"function\") {",
|
|
@@ -239,7 +249,18 @@ export function buildEntrySource(options: EntryTemplateOptions): string {
|
|
|
239
249
|
"}",
|
|
240
250
|
"",
|
|
241
251
|
]
|
|
242
|
-
:
|
|
252
|
+
: serverEntry && mode === "app"
|
|
253
|
+
? [
|
|
254
|
+
`const __serverModule = await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`,
|
|
255
|
+
"export const app = __serverModule.default;",
|
|
256
|
+
"export const port = __serverModule.port;",
|
|
257
|
+
"export const idleTimeout = __serverModule.idleTimeout;",
|
|
258
|
+
"export const startServer = __serverModule.startServer;",
|
|
259
|
+
"export const onShutdown = __serverModule.onShutdown;",
|
|
260
|
+
"export default __serverModule.default;",
|
|
261
|
+
"",
|
|
262
|
+
]
|
|
263
|
+
: []),
|
|
243
264
|
];
|
|
244
265
|
|
|
245
266
|
return lines.join("\n");
|
package/src/build/hydrate.ts
CHANGED
|
@@ -206,6 +206,7 @@ const _match = routes.find((r) => r.regex.test(pathname));
|
|
|
206
206
|
// a matched loader threw notFound(). The latter still has a _match; the
|
|
207
207
|
// former does not — so the two cases fork on _match below.
|
|
208
208
|
const dataEl = document.getElementById("__FURIN_DATA__");
|
|
209
|
+
const frameTemplate = document.getElementById("__FURIN_ROUTE_FRAMES__") as HTMLTemplateElement | null;
|
|
209
210
|
let loaderData = dataEl ? JSON.parse(dataEl.textContent || "{}") : {};
|
|
210
211
|
const syncEl = document.getElementById("__FURIN_SYNC__");
|
|
211
212
|
const syncConfig = syncEl ? JSON.parse(syncEl.textContent || "{}") : {};
|
|
@@ -222,6 +223,7 @@ const documentState: DocumentState = {
|
|
|
222
223
|
assets: {
|
|
223
224
|
buildId,
|
|
224
225
|
entryModule: entryEl?.getAttribute("src") ?? undefined,
|
|
226
|
+
extensionErrorFilterScript: document.querySelector("[data-furin-extension-error-filter]")?.textContent ?? undefined,
|
|
225
227
|
faviconHref,
|
|
226
228
|
frameworkModules,
|
|
227
229
|
staticMode: document.querySelector('meta[name="furin-mode"][content="static"]') !== null,
|
|
@@ -231,6 +233,9 @@ const documentState: DocumentState = {
|
|
|
231
233
|
},
|
|
232
234
|
dataJson: dataEl?.textContent ?? undefined,
|
|
233
235
|
head,
|
|
236
|
+
routeFrames: frameTemplate?.hasAttribute("data-furin-document-state")
|
|
237
|
+
? (frameTemplate.content.textContent ?? "")
|
|
238
|
+
: undefined,
|
|
234
239
|
syncJson: syncEl?.textContent ?? undefined,
|
|
235
240
|
};
|
|
236
241
|
|
|
@@ -305,7 +310,6 @@ if (__deferred && __deferred._chunks) {
|
|
|
305
310
|
// Wrapped in an async IIFE to avoid top-level await, which causes Bun's HTML
|
|
306
311
|
// bundler to misidentify which chunk to reference as the entry in index.html.
|
|
307
312
|
(async () => {
|
|
308
|
-
const frameTemplate = document.getElementById("__FURIN_ROUTE_FRAMES__") as HTMLTemplateElement | null;
|
|
309
313
|
if (frameTemplate) {
|
|
310
314
|
const payload = frameTemplate.content.textContent || "";
|
|
311
315
|
const routeFrameStream = (window as unknown as { __FURIN_ROUTE_FRAME_STREAM__?: FurinRouteFrameStream })
|
package/src/build/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { isomorphicTransformPlugin } from "../plugin/transform-isomorphic.ts";
|
|
|
11
11
|
import { normalizePrefix } from "../server/instance.ts";
|
|
12
12
|
import { scanPages } from "../server/router/discovery.ts";
|
|
13
13
|
import { assertNoPrefixSlugCollisions } from "../shared/prefix.ts";
|
|
14
|
+
import { withProductionBuild } from "../shared/production-build.ts";
|
|
14
15
|
import { scanFurinInstances } from "./scan-server";
|
|
15
16
|
import { ensureDir, toBuildRouteManifestEntry, toPosixPath } from "./shared";
|
|
16
17
|
import type { BuildAppOptions, BuildAppResult, BuildManifest } from "./types";
|
|
@@ -82,6 +83,10 @@ function resolveAppSpecs(
|
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult> {
|
|
86
|
+
return withProductionBuild(() => buildAppInternal(options));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function buildAppInternal(options: BuildAppOptions): Promise<BuildAppResult> {
|
|
85
90
|
const rootDir = resolve(options.rootDir ?? process.cwd());
|
|
86
91
|
const buildRoot = join(rootDir, BUILD_OUTPUT_DIR);
|
|
87
92
|
const serverEntry = (() => {
|
|
@@ -230,11 +235,6 @@ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult
|
|
|
230
235
|
options
|
|
231
236
|
);
|
|
232
237
|
break;
|
|
233
|
-
case "node":
|
|
234
|
-
case "cloudflare":
|
|
235
|
-
throw new Error(
|
|
236
|
-
`[furin] \`--target ${target}\` is planned but not implemented yet in this branch.`
|
|
237
|
-
);
|
|
238
238
|
default:
|
|
239
239
|
throw new Error(`[furin] Unsupported build target "${target}"`);
|
|
240
240
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { existsSync, renameSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, relative } from "node:path";
|
|
3
|
+
import { ensureDir } from "./shared.ts";
|
|
4
|
+
|
|
5
|
+
/** Keep server maps out of Bun assets and Vercel's deployment artifact. */
|
|
6
|
+
export function movePrivateServerSourceMaps(
|
|
7
|
+
outputDir: string,
|
|
8
|
+
privateDir: string,
|
|
9
|
+
filenames: readonly string[]
|
|
10
|
+
): void {
|
|
11
|
+
for (const filename of filenames) {
|
|
12
|
+
const source = isAbsolute(filename) ? filename : join(outputDir, filename);
|
|
13
|
+
if (!existsSync(source)) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const destination = join(privateDir, relative(outputDir, source));
|
|
17
|
+
ensureDir(dirname(destination));
|
|
18
|
+
renameSync(source, destination);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -3,12 +3,12 @@ import { dirname, relative, resolve } from "node:path";
|
|
|
3
3
|
const INSTRUMENTATION_IMPORT = /(?:^|[/\\])devtools[/\\]instrumentation(?:\.ts|\.js)?$/;
|
|
4
4
|
const DEV_PAGE_PLUGIN_IMPORT = /(?:^|[/\\])server[/\\]dev-page-plugin(?:\.ts|\.js)?$/;
|
|
5
5
|
const DEV_RUNTIME_IMPORT =
|
|
6
|
-
/(?:^|[/\\])server[/\\]dev[/\\](?:browser-events|diagnostics|plugin)(?:\.ts|\.js)?$/;
|
|
6
|
+
/(?:^|[/\\])server[/\\]dev[/\\](?:browser-events|diagnostics|graph|plugin)(?:\.ts|\.js)?$/;
|
|
7
7
|
const DEV_BUILD_IMPORT =
|
|
8
8
|
/(?:^|[/\\])(?:build[/\\]hydrate|plugin[/\\]route-config-autofix)(?:\.ts|\.js)?$/;
|
|
9
9
|
const HMR_IMPORT = /(?:^|[/\\])server[/\\]router[/\\]hmr(?:\.ts|\.js)?$/;
|
|
10
10
|
const PRODUCTION_BOUNDARY_IMPORT =
|
|
11
|
-
/(?:browser-events|instrumentation|dev-page-plugin|diagnostics|plugin|hmr|hydrate|route-config-autofix)(?:\.ts|\.js)?$/;
|
|
11
|
+
/(?:browser-events|instrumentation|dev-page-plugin|diagnostics|graph|plugin|hmr|hydrate|route-config-autofix)(?:\.ts|\.js)?$/;
|
|
12
12
|
const PRODUCTION_STUB_NAMESPACE = "furin-production-runtime-stub";
|
|
13
13
|
const FURIN_RUNTIME_ROOT = resolve(import.meta.dir, "..");
|
|
14
14
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
materializedRouteModuleSource,
|
|
5
|
+
type RouteInstanceSpec,
|
|
6
|
+
} from "../plugin/routes.ts";
|
|
7
|
+
import { buildEntrySource, type EntryAppContext } from "./entry-template.ts";
|
|
8
|
+
import { ensureDir, toPosixPath } from "./shared.ts";
|
|
9
|
+
|
|
10
|
+
interface MaterializeServerAppEntryOptions {
|
|
11
|
+
apps: EntryAppContext[];
|
|
12
|
+
headerComment: string;
|
|
13
|
+
instances: RouteInstanceSpec[];
|
|
14
|
+
outDir: string;
|
|
15
|
+
serverEntry: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function materializeServerAppEntry(
|
|
19
|
+
options: MaterializeServerAppEntryOptions
|
|
20
|
+
): Promise<string> {
|
|
21
|
+
ensureDir(options.outDir);
|
|
22
|
+
const apps = await Promise.all(
|
|
23
|
+
options.apps.map(async (app, index): Promise<EntryAppContext> => {
|
|
24
|
+
const instance = options.instances[index];
|
|
25
|
+
if (!instance) {
|
|
26
|
+
throw new Error(`[furin] Missing route instance for server app ${index}.`);
|
|
27
|
+
}
|
|
28
|
+
const routePath = join(options.outDir, `_furin-routes-${index}.ts`);
|
|
29
|
+
writeFileSync(routePath, await materializedRouteModuleSource(instance));
|
|
30
|
+
return { ...app, nativeRoutes: toPosixPath(routePath) };
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
const entryPath = join(options.outDir, "_furin-app.ts");
|
|
34
|
+
writeFileSync(
|
|
35
|
+
entryPath,
|
|
36
|
+
buildEntrySource({
|
|
37
|
+
apps,
|
|
38
|
+
headerComment: options.headerComment,
|
|
39
|
+
mode: "app",
|
|
40
|
+
serverEntry: options.serverEntry,
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
return entryPath;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function serverBootSource(appEntry: string): string {
|
|
47
|
+
return `// Auto-generated by furin build - do not edit
|
|
48
|
+
import { startProductionServer } from "@teyik0/furin/production-server";
|
|
49
|
+
import app, { idleTimeout, onShutdown, port, startServer } from ${JSON.stringify(toPosixPath(appEntry))};
|
|
50
|
+
|
|
51
|
+
if (typeof startServer === "function") {
|
|
52
|
+
await startServer();
|
|
53
|
+
} else if (app && typeof app.listen === "function" && !app.server) {
|
|
54
|
+
const serverPort = process.env.PORT === undefined
|
|
55
|
+
? typeof port === "number" ? port : 3000
|
|
56
|
+
: Number(process.env.PORT);
|
|
57
|
+
startProductionServer({ app, idleTimeout, onShutdown, port: serverPort });
|
|
58
|
+
console.log(\`[furin] Server running at http://localhost:\${app.server?.port}\`);
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
}
|
package/src/build/shared.ts
CHANGED
|
@@ -56,7 +56,9 @@ export function toBuildRouteManifestEntry(
|
|
|
56
56
|
mode: route.mode,
|
|
57
57
|
pagePath: toPosixPath(relative(rootDir, route.path)),
|
|
58
58
|
hasLayout: route.routeChain.some((entry) => !!entry.layout),
|
|
59
|
-
hasStaticParams:
|
|
59
|
+
hasStaticParams:
|
|
60
|
+
route.page.staticParams !== undefined ||
|
|
61
|
+
route.routeChain.some((entry) => entry.staticParams !== undefined),
|
|
60
62
|
revalidate: resolveRouteRevalidate(route.page) ?? null,
|
|
61
63
|
};
|
|
62
64
|
}
|
package/src/build/ssg-cache.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { SsgCacheEntry } from "../server/cache/index.ts";
|
|
|
3
3
|
import { resolvePath } from "../server/render/assemble.ts";
|
|
4
4
|
import { prerenderRoute, prerenderSSG } from "../server/render/index.ts";
|
|
5
5
|
import { hasRequestLoader } from "../server/render/loaders.ts";
|
|
6
|
+
import { hasStaticParams, resolveStaticParams } from "../server/render/static-params.ts";
|
|
6
7
|
import { buildRouteMatcher } from "../server/router/patterns.ts";
|
|
7
8
|
import { createSearchRouteMetadata } from "../server/router/schemas.ts";
|
|
8
9
|
import type { ResolvedRoute, RootLayout } from "../server/router/types.ts";
|
|
@@ -52,14 +53,8 @@ export async function buildRoutePrerenders(
|
|
|
52
53
|
let paramSets: Record<string, string>[];
|
|
53
54
|
if (!DYNAMIC_SEGMENT_RE.test(route.pattern)) {
|
|
54
55
|
paramSets = [{}];
|
|
55
|
-
} else if (route
|
|
56
|
-
|
|
57
|
-
if (!Array.isArray(resolvedParams)) {
|
|
58
|
-
throw new TypeError(
|
|
59
|
-
`[furin] staticParams() for "${route.pattern}" must return an array.`
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
paramSets = resolvedParams;
|
|
56
|
+
} else if (hasStaticParams(route)) {
|
|
57
|
+
paramSets = (await resolveStaticParams(route, origin)) ?? [];
|
|
63
58
|
} else {
|
|
64
59
|
continue;
|
|
65
60
|
}
|
|
@@ -102,10 +97,10 @@ export async function buildSSGCacheSnapshot(
|
|
|
102
97
|
const searchRoutes = createSearchRouteMetadata(routes);
|
|
103
98
|
|
|
104
99
|
for (const route of routes) {
|
|
105
|
-
if (route.mode !== "ssg" || !route
|
|
100
|
+
if (route.mode !== "ssg" || !hasStaticParams(route) || hasRequestLoader(route)) {
|
|
106
101
|
continue;
|
|
107
102
|
}
|
|
108
|
-
const paramSets = await route
|
|
103
|
+
const paramSets = (await resolveStaticParams(route, origin)) ?? [];
|
|
109
104
|
for (const params of paramSets) {
|
|
110
105
|
const result = await prerenderSSG(
|
|
111
106
|
route,
|
package/src/build/types.ts
CHANGED
|
@@ -116,6 +116,8 @@ export interface BuildAppOptions {
|
|
|
116
116
|
reactCompiler?: boolean;
|
|
117
117
|
rootDir?: string;
|
|
118
118
|
serverEntry?: string;
|
|
119
|
+
/** Emit server source maps outside deployable output directories. */
|
|
120
|
+
serverSourceMaps?: boolean;
|
|
119
121
|
/** Configuration for the `static` build target. */
|
|
120
122
|
staticConfig?: StaticExportConfig;
|
|
121
123
|
target: BuildTarget | "all";
|
|
@@ -11,14 +11,15 @@ export function createVirtualBuildEntry(
|
|
|
11
11
|
source: string,
|
|
12
12
|
loader: Bun.Loader
|
|
13
13
|
): VirtualBuildEntry {
|
|
14
|
+
const normalizedEntrypoint = entrypoint.replaceAll("\\", "/");
|
|
14
15
|
const entrypointFilter = new RegExp(
|
|
15
|
-
`^${
|
|
16
|
+
`^${normalizedEntrypoint.replace(REGEX_SPECIAL_CHARACTERS_RE, "\\$&")}$`
|
|
16
17
|
);
|
|
17
18
|
return {
|
|
18
|
-
entrypoint,
|
|
19
|
-
files: { [
|
|
19
|
+
entrypoint: normalizedEntrypoint,
|
|
20
|
+
files: { [normalizedEntrypoint]: source },
|
|
20
21
|
plugin: {
|
|
21
|
-
name: `furin-virtual-entry:${
|
|
22
|
+
name: `furin-virtual-entry:${normalizedEntrypoint}`,
|
|
22
23
|
setup(build) {
|
|
23
24
|
build.onLoad({ filter: entrypointFilter }, () => ({ contents: source, loader }));
|
|
24
25
|
},
|
package/src/cache.ts
CHANGED
|
@@ -5,6 +5,18 @@ import {
|
|
|
5
5
|
type RuntimeCacheSetOptions as RuntimeCacheSetOptionsContract,
|
|
6
6
|
} from "./server/cache/runtime-cache.ts";
|
|
7
7
|
|
|
8
|
+
// biome-ignore lint/performance/noBarrelFile: this is the intentional public cache entrypoint.
|
|
9
|
+
export {
|
|
10
|
+
createMemoryPageCache,
|
|
11
|
+
type PageCacheAdapter,
|
|
12
|
+
type PageCacheEntry,
|
|
13
|
+
type PageCacheIdentity,
|
|
14
|
+
type PageCacheInvalidation,
|
|
15
|
+
type PageCacheInvalidationResult,
|
|
16
|
+
type PageCacheLease,
|
|
17
|
+
type PageCacheMode,
|
|
18
|
+
} from "./server/cache/page-cache.ts";
|
|
19
|
+
|
|
8
20
|
export type RuntimeCache = RuntimeCacheContract;
|
|
9
21
|
export type RuntimeCacheOptions = RuntimeCacheOptionsContract;
|
|
10
22
|
export type RuntimeCacheSetOptions = RuntimeCacheSetOptionsContract;
|
package/src/cli/config.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { Validator } from "elysia";
|
|
4
5
|
import { configSchema, type FurinConfig } from "../config.ts";
|
|
5
|
-
import { TypeCompiler } from "../shared/elysia-contract.ts";
|
|
6
6
|
|
|
7
|
-
const compiledConfigSchema =
|
|
7
|
+
const compiledConfigSchema = Validator.create(configSchema);
|
|
8
8
|
|
|
9
9
|
const DEFAULT_CONFIG_FILENAMES = [
|
|
10
10
|
"furin.config.ts",
|
|
@@ -57,7 +57,7 @@ export async function loadCliConfig(
|
|
|
57
57
|
if (!compiledConfigSchema.Check(configToValidate)) {
|
|
58
58
|
const [firstError] = compiledConfigSchema.Errors(configToValidate);
|
|
59
59
|
throw new Error(
|
|
60
|
-
`[furin] Invalid config at ${configPath}: ${firstError?.message ?? "unknown error"} (path: ${firstError?.
|
|
60
|
+
`[furin] Invalid config at ${configPath}: ${firstError?.message ?? "unknown error"} (path: ${firstError?.instancePath ?? "/"})`
|
|
61
61
|
);
|
|
62
62
|
}
|
|
63
63
|
|
package/src/cli/index.ts
CHANGED
|
@@ -96,8 +96,11 @@ if (command === "preview") {
|
|
|
96
96
|
};
|
|
97
97
|
const config = await loadCliConfig(process.cwd(), values.config);
|
|
98
98
|
const port = values.port === undefined ? 3000 : Number(values.port);
|
|
99
|
-
if (
|
|
100
|
-
|
|
99
|
+
if (
|
|
100
|
+
(values.port !== undefined && !/^\d+$/.test(values.port)) ||
|
|
101
|
+
!(Number.isInteger(port) && port >= 0 && port <= 65_535)
|
|
102
|
+
) {
|
|
103
|
+
bail(`Invalid preview port "${values.port}". Expected an integer between 0 and 65535.`);
|
|
101
104
|
}
|
|
102
105
|
|
|
103
106
|
const distDir = resolve(config.rootDir, values.dir ?? config.static?.outDir ?? "dist");
|
|
@@ -181,6 +184,7 @@ if (command === "preview") {
|
|
|
181
184
|
reactCompiler: config.reactCompiler,
|
|
182
185
|
rootDir: config.rootDir,
|
|
183
186
|
serverEntry: resolvedServerEntry,
|
|
187
|
+
serverSourceMaps: config.serverSourceMaps,
|
|
184
188
|
staticConfig: config.static,
|
|
185
189
|
target: target as BuildTarget | "all",
|
|
186
190
|
vercelConfig: config.vercel,
|
package/src/client/document.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import type { HeadOptions, MetaDescriptor } from "../client.ts";
|
|
|
4
4
|
export interface DocumentAssets {
|
|
5
5
|
buildId: string | undefined;
|
|
6
6
|
entryModule: string | undefined;
|
|
7
|
+
extensionErrorFilterScript?: string;
|
|
7
8
|
faviconHref: string | undefined;
|
|
8
9
|
frameworkModules: readonly string[];
|
|
9
10
|
staticMode: boolean;
|
|
@@ -14,6 +15,7 @@ export interface DocumentState {
|
|
|
14
15
|
assets: DocumentAssets;
|
|
15
16
|
dataJson: string | undefined;
|
|
16
17
|
head: HeadOptions | undefined;
|
|
18
|
+
routeFrames?: string;
|
|
17
19
|
syncJson: string | undefined;
|
|
18
20
|
}
|
|
19
21
|
|
|
@@ -70,6 +72,13 @@ export function HeadContent(): ReactNode {
|
|
|
70
72
|
|
|
71
73
|
return (
|
|
72
74
|
<>
|
|
75
|
+
{state.assets.extensionErrorFilterScript === undefined ? null : (
|
|
76
|
+
<script
|
|
77
|
+
// biome-ignore lint/security/noDangerouslySetInnerHtml: framework-owned development script.
|
|
78
|
+
dangerouslySetInnerHTML={{ __html: state.assets.extensionErrorFilterScript }}
|
|
79
|
+
data-furin-extension-error-filter=""
|
|
80
|
+
/>
|
|
81
|
+
)}
|
|
73
82
|
<meta charSet="utf-8" data-furin-head="" />
|
|
74
83
|
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
|
|
75
84
|
{state.assets.buildId ? <meta content={state.assets.buildId} name="furin-build-id" /> : null}
|
|
@@ -129,6 +138,16 @@ export function Scripts(): ReactNode {
|
|
|
129
138
|
type="application/json"
|
|
130
139
|
/>
|
|
131
140
|
)}
|
|
141
|
+
{state.routeFrames === undefined ? null : (
|
|
142
|
+
<template
|
|
143
|
+
// biome-ignore lint/security/noDangerouslySetInnerHtml: route frames are escaped before insertion.
|
|
144
|
+
dangerouslySetInnerHTML={{
|
|
145
|
+
__html: state.routeFrames.replaceAll("&", "&").replaceAll("<", "<"),
|
|
146
|
+
}}
|
|
147
|
+
data-furin-document-state=""
|
|
148
|
+
id="__FURIN_ROUTE_FRAMES__"
|
|
149
|
+
/>
|
|
150
|
+
)}
|
|
132
151
|
<script
|
|
133
152
|
// biome-ignore lint/security/noDangerouslySetInnerHtml: value is escaped framework JSON.
|
|
134
153
|
dangerouslySetInnerHTML={{ __html: serializeJson(state.head ?? {}) }}
|
|
@@ -6,6 +6,14 @@ export type RuntimeData = Record<string, unknown>;
|
|
|
6
6
|
|
|
7
7
|
type Awaitable<T> = Promise<T> | T;
|
|
8
8
|
|
|
9
|
+
export interface RuntimeParams {
|
|
10
|
+
[key: string]: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type RuntimeStaticParams = (
|
|
14
|
+
context: RuntimeData & { params: RuntimeParams }
|
|
15
|
+
) => Awaitable<readonly RuntimeParams[]>;
|
|
16
|
+
|
|
9
17
|
export interface RuntimeRoute {
|
|
10
18
|
__type: "FURIN_ROUTE";
|
|
11
19
|
layout?: React.FC<RuntimeData & { children: React.ReactNode }>;
|
|
@@ -17,6 +25,7 @@ export interface RuntimeRoute {
|
|
|
17
25
|
requestLoader?: (ctx: RequestLoaderContext) => Awaitable<object>;
|
|
18
26
|
revalidate?: number;
|
|
19
27
|
sourcePath?: string;
|
|
28
|
+
staticParams?: RuntimeStaticParams;
|
|
20
29
|
tags?: string[];
|
|
21
30
|
}
|
|
22
31
|
|
|
@@ -28,6 +37,6 @@ export interface RuntimePage {
|
|
|
28
37
|
loader?: (ctx: RuntimeData) => Awaitable<RuntimeData>;
|
|
29
38
|
mode?: RenderingMode;
|
|
30
39
|
revalidate?: number;
|
|
31
|
-
staticParams?:
|
|
40
|
+
staticParams?: RuntimeStaticParams;
|
|
32
41
|
tags?: string[];
|
|
33
42
|
}
|