@teyik0/furin 0.1.0-alpha.13 → 0.1.0-alpha.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -232
- package/dist/auto-invalidate/index.d.ts +4 -0
- package/dist/auto-invalidate/plugin.d.ts +38 -0
- package/dist/auto-invalidate/registry.d.ts +9 -0
- package/dist/auto-invalidate/runtime.d.ts +9 -0
- package/dist/auto-invalidate/types.d.ts +12 -0
- package/dist/await.d.ts +67 -0
- package/dist/build/index.js +2095 -185
- package/dist/cli/index.js +2070 -182
- package/dist/client.d.ts +51 -4
- package/dist/client.js +65 -1
- package/dist/deferred-ndjson.d.ts +32 -0
- package/dist/error.d.ts +14 -0
- package/dist/furin.d.ts +8 -0
- package/dist/furin.js +2927 -319
- package/dist/lang-detect.d.ts +5 -0
- package/dist/link.d.ts +1 -0
- package/dist/link.js +1453 -95
- package/dist/plugin/index.js +96 -25
- package/dist/plugin/transform-client.d.ts +2 -1
- package/dist/render/assemble.d.ts +32 -0
- package/dist/render/cache.d.ts +7 -0
- package/dist/render/element.d.ts +17 -1
- package/dist/render/index.d.ts +10 -1
- package/dist/render/index.ts +250 -44
- package/dist/render/loaders.d.ts +46 -3
- package/dist/render/shell.d.ts +16 -6
- package/dist/render/shell.ts +27 -12
- package/dist/router-provider.d.ts +86 -18
- package/dist/router.d.ts +66 -0
- package/dist/router.js +2909 -374
- package/dist/router.ts +586 -54
- package/dist/server-error.d.ts +12 -0
- package/dist/utils.d.ts +7 -0
- package/package.json +8 -7
- package/src/adapter/bun.ts +3 -3
- package/src/adapter/static.ts +73 -31
- package/src/auto-invalidate/index.ts +10 -0
- package/src/auto-invalidate/plugin.ts +25 -0
- package/src/auto-invalidate/registry.ts +60 -0
- package/src/auto-invalidate/runtime.ts +133 -0
- package/src/auto-invalidate/types.ts +16 -0
- package/src/await.tsx +175 -0
- package/src/build/client.ts +4 -8
- package/src/build/hydrate.ts +67 -1
- package/src/build/route-types.ts +29 -3
- package/src/build/scan-server.ts +6 -23
- package/src/client.ts +80 -3
- package/src/deferred-ndjson.ts +212 -0
- package/src/dev-page-plugin.ts +2 -0
- package/src/error.ts +18 -1
- package/src/furin.ts +19 -2
- package/src/lang-detect.ts +46 -0
- package/src/link.tsx +16 -3
- package/src/plugin/index.ts +7 -8
- package/src/plugin/transform-client.ts +108 -39
- package/src/render/assemble.ts +67 -0
- package/src/render/boundaries.tsx +23 -5
- package/src/render/cache.ts +14 -0
- package/src/render/default-screens.tsx +4 -11
- package/src/render/dev-cache.ts +5 -0
- package/src/render/element.tsx +26 -3
- package/src/render/index.ts +250 -44
- package/src/render/loaders.ts +219 -18
- package/src/render/shell.ts +27 -12
- package/src/router-provider.tsx +414 -122
- package/src/router.ts +586 -54
- package/src/server-error.ts +47 -0
- package/src/utils.ts +31 -0
package/src/render/loaders.ts
CHANGED
|
@@ -1,14 +1,82 @@
|
|
|
1
1
|
import type { Context } from "elysia";
|
|
2
|
-
import type
|
|
2
|
+
import { isDeferred, type RuntimeRoute } from "../client";
|
|
3
3
|
import { useLogger } from "../context-logger.ts";
|
|
4
4
|
import { type FurinNotFoundError, isNotFoundError } from "../not-found.ts";
|
|
5
5
|
import type { ResolvedRoute } from "../router";
|
|
6
6
|
|
|
7
7
|
export type LoaderResult =
|
|
8
|
-
| {
|
|
8
|
+
| {
|
|
9
|
+
type: "data";
|
|
10
|
+
/**
|
|
11
|
+
* Synchronous (JSON-serialisable) loader fields. Injected into the initial
|
|
12
|
+
* HTML shell as `__FURIN_DATA__` / the deferred registry's `_data` object.
|
|
13
|
+
*/
|
|
14
|
+
syncData: Record<string, unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* Promise-valued fields from a `defer()` return. `undefined` when the page
|
|
17
|
+
* loader did not call `defer()`. Streamed as late `<script>` resolution
|
|
18
|
+
* chunks (SSR) or as NDJSON chunks via `/_furin/data` (SPA nav).
|
|
19
|
+
*/
|
|
20
|
+
deferredPromises: Record<string, Promise<unknown>> | undefined;
|
|
21
|
+
headers: Record<string, string>;
|
|
22
|
+
}
|
|
9
23
|
| { type: "redirect"; response: Response }
|
|
10
24
|
| { type: "not-found"; error: FurinNotFoundError; headers: Record<string, string> }
|
|
11
|
-
| {
|
|
25
|
+
| {
|
|
26
|
+
type: "error";
|
|
27
|
+
/**
|
|
28
|
+
* Original thrown value, kept for digest computation and server logging.
|
|
29
|
+
* For thrown `Response` objects this is the Response instance whose body
|
|
30
|
+
* has already been consumed by `runLoaders` (do NOT read it again).
|
|
31
|
+
*/
|
|
32
|
+
error: unknown;
|
|
33
|
+
/** HTTP status to return. Default 500; sourced from `Response.status` for thrown Response objects. */
|
|
34
|
+
status: number;
|
|
35
|
+
/**
|
|
36
|
+
* Safe public message extracted at the loader boundary. For thrown
|
|
37
|
+
* `Response` objects: response body or `statusText`. For thrown `Error`
|
|
38
|
+
* / non-Error values: a generic "Something went wrong" string (the raw
|
|
39
|
+
* error/message is never leaked here — `errorMessageOf` decides what to
|
|
40
|
+
* surface from the original `error` value when an `error.tsx` fallback
|
|
41
|
+
* exists).
|
|
42
|
+
*/
|
|
43
|
+
message: string;
|
|
44
|
+
headers: Record<string, string>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Navigation redirect status codes — the exact set `ctx.redirect()` can emit.
|
|
49
|
+
* Other 3xx codes (304 Not Modified, 305/306) are NOT navigation redirects and
|
|
50
|
+
* must not be treated as one even when they carry a `Location` header.
|
|
51
|
+
*/
|
|
52
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `true` only for HTTP responses that are syntactically valid redirects:
|
|
56
|
+
* a navigation redirect status code AND a `Location` header. A redirect status
|
|
57
|
+
* without `Location` is invalid HTTP and almost always a developer mistake —
|
|
58
|
+
* surfaced as an error so it's debuggable rather than silently redirecting to `/`.
|
|
59
|
+
*/
|
|
60
|
+
function isHttpRedirect(res: Response): boolean {
|
|
61
|
+
return REDIRECT_STATUSES.has(res.status) && res.headers.has("location");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Reads the body of a thrown `Response` exactly once. Returns the body text
|
|
66
|
+
* if present, otherwise the response's `statusText`. The body is consumed
|
|
67
|
+
* destructively — callers must NOT read `res.body` / `res.text()` again.
|
|
68
|
+
*/
|
|
69
|
+
async function readResponseMessage(res: Response): Promise<string> {
|
|
70
|
+
if (res.body === null) {
|
|
71
|
+
return res.statusText;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const body = await res.text();
|
|
75
|
+
return body || res.statusText;
|
|
76
|
+
} catch {
|
|
77
|
+
return res.statusText;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
12
80
|
|
|
13
81
|
/**
|
|
14
82
|
* Wraps the Elysia context so that any property NOT present on `ctx` is
|
|
@@ -53,12 +121,109 @@ function createLoaderCtx(
|
|
|
53
121
|
});
|
|
54
122
|
}
|
|
55
123
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Splits a single loader result into sync scalars and deferred Promises.
|
|
126
|
+
*
|
|
127
|
+
* A loader result is considered "deferred" only when wrapped with `defer()`
|
|
128
|
+
* (i.e. carries the `__isDeferred` brand). A loader that returned a plain
|
|
129
|
+
* object keeps all its values in sync — even if some happen to be Promises —
|
|
130
|
+
* preserving the long-standing semantic that only an explicit `defer()` opts
|
|
131
|
+
* into streaming.
|
|
132
|
+
*/
|
|
133
|
+
function splitOneLoaderResult(result: Record<string, unknown>): {
|
|
134
|
+
sync: Record<string, unknown>;
|
|
135
|
+
deferred: Record<string, Promise<unknown>>;
|
|
136
|
+
} {
|
|
137
|
+
const sync: Record<string, unknown> = {};
|
|
138
|
+
const deferred: Record<string, Promise<unknown>> = {};
|
|
139
|
+
const isDef = isDeferred(result);
|
|
140
|
+
for (const [key, value] of Object.entries(result)) {
|
|
141
|
+
if (key === "__isDeferred") {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (isDef && isPromiseLike(value)) {
|
|
145
|
+
deferred[key] = Promise.resolve(value);
|
|
146
|
+
} else {
|
|
147
|
+
sync[key] = value;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return { sync, deferred };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
|
|
154
|
+
return (
|
|
155
|
+
(typeof value === "object" || typeof value === "function") &&
|
|
156
|
+
value !== null &&
|
|
157
|
+
typeof (value as { then?: unknown }).then === "function"
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Merges per-loader results into the unified sync / deferred maps used by
|
|
163
|
+
* `runLoaders`. `results` is ordered ancestor-most → page, so later loaders
|
|
164
|
+
* overwrite earlier ones on key collision.
|
|
165
|
+
*
|
|
166
|
+
* Cross-map cleanup: when a later loader contributes a key, drop any stale
|
|
167
|
+
* entry from the opposite map. Without this, a key that switches between sync
|
|
168
|
+
* and deferred across loaders would leave both maps populated, and the wire
|
|
169
|
+
* would carry two contradictory values for the same field.
|
|
170
|
+
*/
|
|
171
|
+
function mergeLoaderResults(results: unknown[]): {
|
|
172
|
+
allSync: Record<string, unknown>;
|
|
173
|
+
allDeferred: Record<string, Promise<unknown>>;
|
|
174
|
+
} {
|
|
175
|
+
const allSync: Record<string, unknown> = {};
|
|
176
|
+
const allDeferred: Record<string, Promise<unknown>> = {};
|
|
177
|
+
for (const result of results) {
|
|
178
|
+
const { sync, deferred } = splitOneLoaderResult(result as Record<string, unknown>);
|
|
179
|
+
for (const k of Object.keys(sync)) {
|
|
180
|
+
delete allDeferred[k];
|
|
181
|
+
}
|
|
182
|
+
for (const k of Object.keys(deferred)) {
|
|
183
|
+
delete allSync[k];
|
|
184
|
+
}
|
|
185
|
+
Object.assign(allSync, sync);
|
|
186
|
+
Object.assign(allDeferred, deferred);
|
|
187
|
+
}
|
|
188
|
+
return { allSync, allDeferred };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Normalises an error thrown inside a deferred Promise into a value that is
|
|
193
|
+
* safe to send through `toCrossJSON` and that preserves the original semantics
|
|
194
|
+
* after `fromCrossJSON` on the client.
|
|
195
|
+
*
|
|
196
|
+
* Symmetric to the rejection handling in `runLoaders` (lines 247-272) but for
|
|
197
|
+
* the *deferred* path, where the rejection is delivered to a client-side
|
|
198
|
+
* `<Await>` instead of becoming a top-level response.
|
|
199
|
+
*
|
|
200
|
+
* - `notFound()` → `Error` carrying `__furinBrand` + `data` so the
|
|
201
|
+
* client-side `isNotFoundError()` (duck-typed)
|
|
202
|
+
* recognises the rejection.
|
|
203
|
+
* - `Response(status, body)` → `Error` carrying `__furinStatus` and the
|
|
204
|
+
* body/statusText as `message`.
|
|
205
|
+
* - `Error` → returned unchanged.
|
|
206
|
+
* - anything else → wrapped in `new Error(String(value))`.
|
|
207
|
+
*/
|
|
208
|
+
export async function serializeDeferredRejection(err: unknown): Promise<unknown> {
|
|
209
|
+
if (isNotFoundError(err)) {
|
|
210
|
+
const wrapped = new Error(err.message);
|
|
211
|
+
Object.assign(wrapped, { __furinBrand: "FURIN_NOT_FOUND", data: err.data });
|
|
212
|
+
return wrapped;
|
|
213
|
+
}
|
|
214
|
+
if (err instanceof Response) {
|
|
215
|
+
const body = await readResponseMessage(err);
|
|
216
|
+
const wrapped = new Error(body || err.statusText || "Something went wrong");
|
|
217
|
+
Object.assign(wrapped, { __furinStatus: err.status });
|
|
218
|
+
return wrapped;
|
|
219
|
+
}
|
|
220
|
+
if (err instanceof Error) {
|
|
221
|
+
return err;
|
|
222
|
+
}
|
|
223
|
+
return new Error(String(err));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export async function runLoaders(route: ResolvedRoute, ctx: Context): Promise<LoaderResult> {
|
|
62
227
|
try {
|
|
63
228
|
// Inject `log` so loaders can destructure it directly as `({ log })`.
|
|
64
229
|
// useLogger() resolves the correct logger for every rendering context:
|
|
@@ -104,24 +269,60 @@ export async function runLoaders(
|
|
|
104
269
|
? Promise.resolve(route.page.loader(pageCtx)).then((r) => r ?? {})
|
|
105
270
|
: Promise.resolve({});
|
|
106
271
|
|
|
107
|
-
// Await everything in parallel
|
|
272
|
+
// Await everything in parallel. `results` is ordered layout1 → … → page;
|
|
273
|
+
// the per-loader split preserves that order so later loaders (page last)
|
|
274
|
+
// overwrite earlier ones on key collision — same semantic as the previous
|
|
275
|
+
// non-deferred `Object.assign({}, ...results)` flat merge.
|
|
108
276
|
const results = await Promise.all([...loaderMap.values(), pagePromise]);
|
|
109
|
-
const data = Object.assign({}, ...results);
|
|
110
277
|
const headers: Record<string, string> = {};
|
|
111
278
|
Object.assign(headers, ctx.set.headers);
|
|
112
279
|
|
|
113
|
-
|
|
280
|
+
// Any loader wrapped with `defer()` (page OR route/layout) contributes its
|
|
281
|
+
// Promise-valued fields to `allDeferred` and its scalars to `allSync`.
|
|
282
|
+
// Non-deferred loaders keep everything in `allSync` — even Promise values,
|
|
283
|
+
// since only an explicit `defer()` opts into streaming.
|
|
284
|
+
const { allSync, allDeferred } = mergeLoaderResults(results);
|
|
285
|
+
|
|
286
|
+
// Route context is always injected into syncData so components receive
|
|
287
|
+
// params, query and path regardless of the serialisation path (SSR, SPA
|
|
288
|
+
// nav, dev cache).
|
|
289
|
+
const routeCtx = { params: ctx.params, query: ctx.query, path: ctx.path };
|
|
290
|
+
return {
|
|
291
|
+
type: "data",
|
|
292
|
+
syncData: { ...allSync, ...routeCtx },
|
|
293
|
+
deferredPromises: Object.keys(allDeferred).length > 0 ? allDeferred : undefined,
|
|
294
|
+
headers,
|
|
295
|
+
};
|
|
114
296
|
} catch (err) {
|
|
297
|
+
const headers: Record<string, string> = {};
|
|
298
|
+
Object.assign(headers, ctx.set.headers);
|
|
115
299
|
if (isNotFoundError(err)) {
|
|
116
|
-
const headers: Record<string, string> = {};
|
|
117
|
-
Object.assign(headers, ctx.set.headers);
|
|
118
300
|
return { type: "not-found", error: err, headers };
|
|
119
301
|
}
|
|
120
302
|
if (err instanceof Response) {
|
|
121
|
-
|
|
303
|
+
if (isHttpRedirect(err)) {
|
|
304
|
+
return { type: "redirect", response: err };
|
|
305
|
+
}
|
|
306
|
+
// Non-redirect Response → error. Read the body ONCE here so downstream
|
|
307
|
+
// consumers (digest, logging, error UI) all share the same extracted
|
|
308
|
+
// message without consuming the stream a second time.
|
|
309
|
+
//
|
|
310
|
+
// A *malformed redirect* is a redirect-status code WITHOUT a `Location`
|
|
311
|
+
// header (we only reach here when `isHttpRedirect` was false) — invalid
|
|
312
|
+
// HTTP, coerced to 500. Other 3xx codes (304/305/306) are not redirects
|
|
313
|
+
// at all, so they keep their own status rather than being rewritten.
|
|
314
|
+
const isMalformedRedirect = REDIRECT_STATUSES.has(err.status);
|
|
315
|
+
const status = isMalformedRedirect ? 500 : err.status;
|
|
316
|
+
const body = await readResponseMessage(err);
|
|
317
|
+
const message = body || "Something went wrong";
|
|
318
|
+
return { type: "error", error: err, status, message, headers };
|
|
122
319
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
320
|
+
return {
|
|
321
|
+
type: "error",
|
|
322
|
+
error: err,
|
|
323
|
+
status: 500,
|
|
324
|
+
message: "Something went wrong",
|
|
325
|
+
headers,
|
|
326
|
+
};
|
|
126
327
|
}
|
|
127
328
|
}
|
package/src/render/shell.ts
CHANGED
|
@@ -39,10 +39,13 @@ export function safeJson(value: unknown): string {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
export function renderAttrs(obj: Record<string, string | undefined>): string {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
const parts: string[] = [];
|
|
43
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
44
|
+
if (v !== undefined) {
|
|
45
|
+
parts.push(`${k}="${escapeHtml(String(v))}"`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return parts.join(" ");
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
// ---------------------------------------------------------------------------
|
|
@@ -147,17 +150,28 @@ export function generateIndexHtml(): string {
|
|
|
147
150
|
* Called after Bun.build() completes so we can inject the correct entry chunk
|
|
148
151
|
* and CSS paths derived from result.outputs.
|
|
149
152
|
*
|
|
150
|
-
* @param
|
|
151
|
-
*
|
|
152
|
-
* @param
|
|
153
|
-
*
|
|
154
|
-
*
|
|
153
|
+
* @param entryChunk - Hashed URL of the client entry script.
|
|
154
|
+
* @param cssChunks - Hashed URLs of the CSS chunks to inject as `<link>`s.
|
|
155
|
+
* @param buildId - Short hash identifying this specific build (or `undefined`
|
|
156
|
+
* for static exports where stale-deploy detection is not applicable).
|
|
157
|
+
* Injected as a `<meta name="furin-build-id">` tag so the client can
|
|
158
|
+
* detect stale deploys.
|
|
159
|
+
* @param faviconHref - Absolute href for the favicon (e.g. "/furin/favicon.ico"),
|
|
160
|
+
* or `undefined` when no favicon is provided. When set, a `<link rel="icon">`
|
|
161
|
+
* tag is injected so browsers can locate the favicon even when the site is
|
|
162
|
+
* served from a sub-path.
|
|
163
|
+
* @param staticMode - `true` for static exports, `false` for SSR/ISR. When
|
|
164
|
+
* `true`, a `<meta name="furin-mode" content="static">` tag is injected so
|
|
165
|
+
* the SPA client knows to fetch loader data from the per-route
|
|
166
|
+
* `__furin_data.ndjson` file instead of the runtime `/_furin/data` endpoint
|
|
167
|
+
* (which doesn't exist on a static host).
|
|
155
168
|
*/
|
|
156
169
|
export function generateProdIndexHtml(
|
|
157
170
|
entryChunk: string,
|
|
158
171
|
cssChunks: string[],
|
|
159
|
-
buildId
|
|
160
|
-
faviconHref
|
|
172
|
+
buildId: string | undefined,
|
|
173
|
+
faviconHref: string | undefined,
|
|
174
|
+
staticMode: boolean
|
|
161
175
|
): string {
|
|
162
176
|
const cssLinks = cssChunks
|
|
163
177
|
.map((c) => ` <link rel="stylesheet" crossorigin href="${c}">`)
|
|
@@ -169,6 +183,7 @@ export function generateProdIndexHtml(
|
|
|
169
183
|
const faviconLink = faviconHref
|
|
170
184
|
? ` <link rel="icon" href="${escapeHtml(faviconHref)}">\n`
|
|
171
185
|
: "";
|
|
186
|
+
const modeMeta = staticMode ? ` <meta name="furin-mode" content="static">\n` : "";
|
|
172
187
|
|
|
173
188
|
return `<!DOCTYPE html>
|
|
174
189
|
<html lang="en">
|
|
@@ -176,7 +191,7 @@ export function generateProdIndexHtml(
|
|
|
176
191
|
<meta charset="UTF-8">
|
|
177
192
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
178
193
|
${THEME_INIT_SCRIPT}
|
|
179
|
-
${buildIdMeta}${faviconLink}${cssLinks ? `${cssLinks}\n` : ""} <!--ssr-head-->
|
|
194
|
+
${buildIdMeta}${modeMeta}${faviconLink}${cssLinks ? `${cssLinks}\n` : ""} <!--ssr-head-->
|
|
180
195
|
</head>
|
|
181
196
|
<body>
|
|
182
197
|
<div id="root"><!--ssr-outlet--></div>
|