@teyik0/furin 0.5.0-alpha.2 → 0.6.0-alpha.1
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/adapter/runtime-build.d.ts +2 -0
- package/dist/build/entry-template.d.ts +2 -0
- package/dist/build/entry-template.ts +2 -0
- package/dist/build/index.js +8082 -6573
- package/dist/build/request-keys.d.ts +4 -0
- package/dist/cli/index.js +8071 -6562
- package/dist/client/browser-events-runtime.d.ts +2 -1
- package/dist/client/define-route.d.ts +0 -1
- package/dist/client/dev-error-overlay.ts +1 -0
- package/dist/client/document.d.ts +1 -0
- package/dist/client/hmr.d.ts +1 -1
- package/dist/client/link.js +101 -61
- package/dist/client.js +13 -8
- package/dist/define-route.d.ts +163 -101
- package/dist/furin.d.ts +3 -0
- package/dist/furin.js +7663 -6480
- package/dist/plugin/index.js +462 -127
- package/dist/plugin/transform-dev-page.d.ts +7 -0
- package/dist/server/auto-invalidate/registry.d.ts +4 -2
- package/dist/server/browser-events/plugin.d.ts +3 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +1 -0
- package/dist/server/internal.d.ts +2 -0
- package/dist/server/internal.js +67 -39
- package/dist/server/production-server.js +66 -38
- package/dist/server/render/assemble.d.ts +6 -6
- package/dist/server/render/document.d.ts +1 -1
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +11 -3
- package/dist/server/render/mixed-cache.d.ts +6 -0
- package/dist/server/render/ssr.d.ts +3 -3
- package/dist/server/router/discovery.d.ts +1 -0
- package/dist/server/router/patterns.d.ts +3 -0
- package/dist/server/router/plugin.d.ts +3 -0
- package/dist/server/router/types.d.ts +4 -0
- package/dist/server/security/csp.d.ts +12 -0
- package/dist/server/sync/index.js +65 -37
- package/dist/server/sync/postgres/index.js +8 -0
- package/package.json +12 -11
- package/src/adapter/README.md +3 -0
- package/src/adapter/bun.ts +3 -0
- package/src/adapter/package.ts +4 -0
- package/src/adapter/runtime-build.ts +36 -2
- package/src/adapter/static.ts +10 -4
- package/src/adapter/vercel.ts +18 -10
- package/src/build/entry-template.ts +2 -0
- package/src/build/hydrate.ts +1 -0
- package/src/build/request-keys.ts +93 -0
- package/src/build/ssg-cache.ts +9 -4
- package/src/client/boundaries.tsx +1 -1
- package/src/client/browser-events-runtime.ts +69 -16
- package/src/client/define-route.ts +0 -1
- package/src/client/dev-error-overlay.ts +1 -0
- package/src/client/document.tsx +19 -2
- package/src/client/hmr.ts +3 -2
- package/src/client/router/boundary-tree.tsx +3 -4
- package/src/client/router/provider.tsx +109 -61
- package/src/define-route.ts +356 -127
- package/src/furin.ts +138 -25
- package/src/plugin/route-config-autofix.ts +2 -2
- package/src/plugin/routes.ts +16 -5
- package/src/plugin/transform-dev-page.ts +231 -0
- package/src/rsc/version.ts +5 -0
- package/src/server/auto-invalidate/registry.ts +53 -25
- package/src/server/browser-events/plugin.ts +112 -1
- package/src/server/cache/dev-loader.ts +1 -1
- package/src/server/cache/invalidation.ts +2 -0
- package/src/server/cache/isr-ssg.ts +1 -1
- package/src/server/cache/ssg.ts +1 -1
- package/src/server/dev/diagnostics.ts +4 -2
- package/src/server/dev/error.ts +2 -1
- package/src/server/dev/graph.ts +55 -6
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev/symbolicate.ts +2 -0
- package/src/server/dev-page-plugin.ts +67 -4
- package/src/server/evlog.ts +1 -1
- package/src/server/internal.ts +2 -0
- package/src/server/render/assemble.ts +15 -11
- package/src/server/render/document.tsx +3 -1
- package/src/server/render/element.tsx +5 -5
- package/src/server/render/isr.ts +11 -5
- package/src/server/render/loaders.ts +243 -56
- package/src/server/render/mixed-cache.ts +298 -0
- package/src/server/render/not-found.ts +10 -4
- package/src/server/render/ppr-document.ts +45 -10
- package/src/server/render/ppr-route.ts +26 -12
- package/src/server/render/ssg.ts +2 -1
- package/src/server/render/ssr.ts +55 -31
- package/src/server/render/static-params.ts +1 -1
- package/src/server/router/discovery.ts +14 -0
- package/src/server/router/hmr.ts +106 -16
- package/src/server/router/patterns.ts +27 -0
- package/src/server/router/plugin.ts +69 -9
- package/src/server/router/source-version.ts +20 -2
- package/src/server/router/types.ts +4 -0
- package/src/server/security/csp.ts +44 -0
- package/src/server/sync/postgres/index.ts +8 -0
|
@@ -2,17 +2,31 @@ import { type FurinInstance, instanceSlot } from "../instance.ts";
|
|
|
2
2
|
|
|
3
3
|
export class AutoInvalidateRegistry {
|
|
4
4
|
private readonly pathToTags = new Map<string, Set<string>>();
|
|
5
|
+
private readonly pathOwners = new Map<string, Map<string, Set<string>>>();
|
|
5
6
|
private readonly tagToPaths = new Map<string, Set<string>>();
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
private reindexPath(urlPath: string): void {
|
|
9
|
+
const previous = this.pathToTags.get(urlPath);
|
|
10
|
+
if (previous) {
|
|
11
|
+
for (const tag of previous) {
|
|
12
|
+
const paths = this.tagToPaths.get(tag);
|
|
13
|
+
paths?.delete(urlPath);
|
|
14
|
+
if (paths?.size === 0) {
|
|
15
|
+
this.tagToPaths.delete(tag);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
11
18
|
}
|
|
12
19
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
const uniqueTags = new Set<string>();
|
|
21
|
+
for (const tags of this.pathOwners.get(urlPath)?.values() ?? []) {
|
|
22
|
+
for (const tag of tags) {
|
|
23
|
+
uniqueTags.add(tag);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (uniqueTags.size === 0) {
|
|
27
|
+
this.pathToTags.delete(urlPath);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
16
30
|
this.pathToTags.set(urlPath, uniqueTags);
|
|
17
31
|
for (const tag of uniqueTags) {
|
|
18
32
|
let paths = this.tagToPaths.get(tag);
|
|
@@ -24,6 +38,24 @@ export class AutoInvalidateRegistry {
|
|
|
24
38
|
}
|
|
25
39
|
}
|
|
26
40
|
|
|
41
|
+
registerLoaderTags(urlPath: string, tags: readonly string[] | undefined, owner?: string): void {
|
|
42
|
+
const ownerKey = owner ?? "default";
|
|
43
|
+
let owners = this.pathOwners.get(urlPath);
|
|
44
|
+
if (tags === undefined || tags.length === 0) {
|
|
45
|
+
owners?.delete(ownerKey);
|
|
46
|
+
if (owners?.size === 0) {
|
|
47
|
+
this.pathOwners.delete(urlPath);
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
if (!owners) {
|
|
51
|
+
owners = new Map<string, Set<string>>();
|
|
52
|
+
this.pathOwners.set(urlPath, owners);
|
|
53
|
+
}
|
|
54
|
+
owners.set(ownerKey, new Set(tags));
|
|
55
|
+
}
|
|
56
|
+
this.reindexPath(urlPath);
|
|
57
|
+
}
|
|
58
|
+
|
|
27
59
|
pathsForTags(tags: readonly string[]): string[] {
|
|
28
60
|
const paths = new Set<string>();
|
|
29
61
|
for (const tag of tags) {
|
|
@@ -34,27 +66,22 @@ export class AutoInvalidateRegistry {
|
|
|
34
66
|
return [...paths];
|
|
35
67
|
}
|
|
36
68
|
|
|
37
|
-
unregisterPath(urlPath: string): void {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (!paths) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
paths.delete(urlPath);
|
|
49
|
-
if (paths.size === 0) {
|
|
50
|
-
this.tagToPaths.delete(tag);
|
|
69
|
+
unregisterPath(urlPath: string, owner?: string): void {
|
|
70
|
+
if (owner === undefined) {
|
|
71
|
+
this.pathOwners.delete(urlPath);
|
|
72
|
+
} else {
|
|
73
|
+
const owners = this.pathOwners.get(urlPath);
|
|
74
|
+
owners?.delete(owner);
|
|
75
|
+
if (owners?.size === 0) {
|
|
76
|
+
this.pathOwners.delete(urlPath);
|
|
51
77
|
}
|
|
52
78
|
}
|
|
53
|
-
this.
|
|
79
|
+
this.reindexPath(urlPath);
|
|
54
80
|
}
|
|
55
81
|
|
|
56
82
|
reset(): void {
|
|
57
83
|
this.pathToTags.clear();
|
|
84
|
+
this.pathOwners.clear();
|
|
58
85
|
this.tagToPaths.clear();
|
|
59
86
|
}
|
|
60
87
|
}
|
|
@@ -75,8 +102,9 @@ export const autoInvalidateRegistry: Pick<
|
|
|
75
102
|
"registerLoaderTags" | "pathsForTags" | "unregisterPath" | "reset"
|
|
76
103
|
> = {
|
|
77
104
|
pathsForTags: (tags) => instanceAutoInvalidateRegistry().pathsForTags(tags),
|
|
78
|
-
registerLoaderTags: (urlPath, tags) =>
|
|
79
|
-
instanceAutoInvalidateRegistry().registerLoaderTags(urlPath, tags),
|
|
105
|
+
registerLoaderTags: (urlPath, tags, owner) =>
|
|
106
|
+
instanceAutoInvalidateRegistry().registerLoaderTags(urlPath, tags, owner),
|
|
80
107
|
reset: () => instanceAutoInvalidateRegistry().reset(),
|
|
81
|
-
unregisterPath: (urlPath) =>
|
|
108
|
+
unregisterPath: (urlPath, owner) =>
|
|
109
|
+
instanceAutoInvalidateRegistry().unregisterPath(urlPath, owner),
|
|
82
110
|
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { type AnyElysia, Elysia } from "elysia";
|
|
2
2
|
import { websocket } from "elysia/websocket";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
browserEventsClientSource,
|
|
5
|
+
browserEventsSseClientSource,
|
|
6
|
+
} from "../../client/browser-events-runtime.ts";
|
|
4
7
|
import {
|
|
5
8
|
BROWSER_EVENT_PROTOCOL_VERSION,
|
|
6
9
|
type BrowserEventEnvelope,
|
|
@@ -15,8 +18,11 @@ import type { BrowserEventSource, BrowserEventSubscription } from "./types.ts";
|
|
|
15
18
|
const CLIENT_PATH = "/_furin/events/client.js";
|
|
16
19
|
const SOCKET_PATH = "/_furin/events";
|
|
17
20
|
const HEARTBEAT_INTERVAL_MS = 30_000;
|
|
21
|
+
const SSE_CONNECTION_LIFETIME_MS = 60_000;
|
|
18
22
|
const MAX_BROWSER_EVENT_CONNECTIONS = 100;
|
|
19
23
|
const CLIENT_SOURCE = browserEventsClientSource();
|
|
24
|
+
const SSE_CLIENT_SOURCE = browserEventsSseClientSource();
|
|
25
|
+
const SSE_ENCODER = new TextEncoder();
|
|
20
26
|
|
|
21
27
|
interface BrowserEventsPluginOptions {
|
|
22
28
|
sources?: readonly BrowserEventSource[];
|
|
@@ -165,6 +171,111 @@ export function createBrowserEventsPlugin(options: BrowserEventsPluginOptions):
|
|
|
165
171
|
});
|
|
166
172
|
}
|
|
167
173
|
|
|
174
|
+
export function createSseBrowserEventsPlugin({ sync }: { sync: FurinSyncOptions }): AnyElysia {
|
|
175
|
+
const connections = new Set<() => void>();
|
|
176
|
+
return new Elysia({ name: "furin-browser-events-sse" })
|
|
177
|
+
.get(CLIENT_PATH, ({ request, server }) => {
|
|
178
|
+
const forbidden = forbiddenBrowserRequest(request, server);
|
|
179
|
+
if (forbidden) {
|
|
180
|
+
return forbidden;
|
|
181
|
+
}
|
|
182
|
+
return new Response(SSE_CLIENT_SOURCE, {
|
|
183
|
+
headers: {
|
|
184
|
+
"cache-control": "no-store",
|
|
185
|
+
"content-type": "text/javascript; charset=utf-8",
|
|
186
|
+
"x-content-type-options": "nosniff",
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
})
|
|
190
|
+
.get(SOCKET_PATH, ({ request, server }) => {
|
|
191
|
+
const forbidden = forbiddenBrowserRequest(request, server);
|
|
192
|
+
if (forbidden) {
|
|
193
|
+
return forbidden;
|
|
194
|
+
}
|
|
195
|
+
if (connections.size >= MAX_BROWSER_EVENT_CONNECTIONS) {
|
|
196
|
+
return new Response("retry: 5000\n\n", {
|
|
197
|
+
headers: {
|
|
198
|
+
"cache-control": "no-cache, no-transform",
|
|
199
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let release = (): void => undefined;
|
|
205
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
206
|
+
cancel() {
|
|
207
|
+
release();
|
|
208
|
+
},
|
|
209
|
+
start(controller) {
|
|
210
|
+
let closed = false;
|
|
211
|
+
let subscription: BrowserEventSubscription | undefined;
|
|
212
|
+
let lifetime: ReturnType<typeof setTimeout> | undefined;
|
|
213
|
+
const send = (event: BrowserEventEnvelope): void => {
|
|
214
|
+
if (!closed) {
|
|
215
|
+
controller.enqueue(SSE_ENCODER.encode(`data: ${serialized(event)}\n\n`));
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
const heartbeat = setInterval(() => {
|
|
219
|
+
if (!closed) {
|
|
220
|
+
controller.enqueue(SSE_ENCODER.encode(": ping\n\n"));
|
|
221
|
+
}
|
|
222
|
+
}, HEARTBEAT_INTERVAL_MS);
|
|
223
|
+
heartbeat.unref?.();
|
|
224
|
+
const finish = (): void => {
|
|
225
|
+
if (closed) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
release();
|
|
229
|
+
controller.close();
|
|
230
|
+
};
|
|
231
|
+
const onAbort = finish;
|
|
232
|
+
release = () => {
|
|
233
|
+
if (closed) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
closed = true;
|
|
237
|
+
clearInterval(heartbeat);
|
|
238
|
+
if (lifetime) {
|
|
239
|
+
clearTimeout(lifetime);
|
|
240
|
+
}
|
|
241
|
+
request.signal.removeEventListener("abort", onAbort);
|
|
242
|
+
subscription?.unsubscribe();
|
|
243
|
+
connections.delete(release);
|
|
244
|
+
};
|
|
245
|
+
connections.add(release);
|
|
246
|
+
request.signal.addEventListener("abort", onAbort, { once: true });
|
|
247
|
+
if (request.signal.aborted) {
|
|
248
|
+
finish();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
// Bun Vercel Functions do not expose request cancellation; bound the subscription.
|
|
252
|
+
lifetime = setTimeout(finish, SSE_CONNECTION_LIFETIME_MS);
|
|
253
|
+
lifetime.unref?.();
|
|
254
|
+
controller.enqueue(SSE_ENCODER.encode(": connected\n\n"));
|
|
255
|
+
subscribeSyncCursor(sync, (cursor) =>
|
|
256
|
+
send({ channel: "sync", data: { cursor }, version: BROWSER_EVENT_PROTOCOL_VERSION })
|
|
257
|
+
)
|
|
258
|
+
.then((next) => {
|
|
259
|
+
if (closed) {
|
|
260
|
+
next.unsubscribe();
|
|
261
|
+
} else {
|
|
262
|
+
subscription = next;
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
.catch(() => {
|
|
266
|
+
finish();
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
return new Response(stream, {
|
|
271
|
+
headers: {
|
|
272
|
+
"cache-control": "no-cache, no-transform",
|
|
273
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
168
279
|
export function browserEventsClientScript(basePath: string): string {
|
|
169
280
|
return `<script data-furin-framework-module="" type="module" src="${basePath}${CLIENT_PATH}"></script>`;
|
|
170
281
|
}
|
|
@@ -76,7 +76,7 @@ function createDevLoaderCache(name: string, index: Map<string, Set<string>>) {
|
|
|
76
76
|
unindexEntryDependencies(index, key, entry.dependencies);
|
|
77
77
|
const urlPath = urlPathWithSearchFromCacheKey(key);
|
|
78
78
|
if (urlPath) {
|
|
79
|
-
autoInvalidateRegistry.unregisterPath(urlPath);
|
|
79
|
+
autoInvalidateRegistry.unregisterPath(urlPath, name);
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
82
|
onSet: (key, entry, previous) => {
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
runWithInstanceScope,
|
|
14
14
|
withInstance,
|
|
15
15
|
} from "../instance.ts";
|
|
16
|
+
import { clearMixedPublicCache } from "../render/mixed-cache.ts";
|
|
16
17
|
import { clearPprRouteCache } from "../render/ppr-route.ts";
|
|
17
18
|
import { IS_DEV } from "../runtime-env.ts";
|
|
18
19
|
import { clearDevLoaderCaches } from "./dev-loader";
|
|
@@ -188,6 +189,7 @@ export function __resetCacheState(): void {
|
|
|
188
189
|
ssgRouteCache(instance).clear();
|
|
189
190
|
clearDevLoaderCaches(instance);
|
|
190
191
|
clearPprRouteCache(instance);
|
|
192
|
+
clearMixedPublicCache(instance);
|
|
191
193
|
clearPendingISRRevalidations(instance);
|
|
192
194
|
resetPageCacheAdapter(instance);
|
|
193
195
|
});
|
|
@@ -42,7 +42,7 @@ export function createHtmlRouteCache<Entry>(
|
|
|
42
42
|
maxSize: MAX_HTML_CACHE_SIZE,
|
|
43
43
|
name: `render:${mode}-html`,
|
|
44
44
|
onDelete: (key, entry) => {
|
|
45
|
-
autoInvalidateRegistry.unregisterPath(key);
|
|
45
|
+
autoInvalidateRegistry.unregisterPath(key, `render:${mode}-html`);
|
|
46
46
|
options?.onDelete?.(key, entry);
|
|
47
47
|
},
|
|
48
48
|
pathFromKey: pathWithoutSearch,
|
package/src/server/cache/ssg.ts
CHANGED
|
@@ -26,5 +26,5 @@ export function getSSGCache(key: string): SsgCacheEntry | undefined {
|
|
|
26
26
|
|
|
27
27
|
export function setSSGCache(key: string, entry: SsgCacheEntry): void {
|
|
28
28
|
instanceSsgCache().set(key, entry);
|
|
29
|
-
autoInvalidateRegistry.registerLoaderTags(key, entry.tags);
|
|
29
|
+
autoInvalidateRegistry.registerLoaderTags(key, entry.tags, "render:ssg-html");
|
|
30
30
|
}
|
|
@@ -29,8 +29,9 @@ export interface ClientErrorReport {
|
|
|
29
29
|
stack: string | undefined;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
const STACK_POSITION_RE =
|
|
33
|
-
|
|
32
|
+
const STACK_POSITION_RE =
|
|
33
|
+
/(?:^|\s)\(?((?:file:\/\/|furin-dev-(?:page|render):)?\S+):(\d+):(\d+)\)?$/;
|
|
34
|
+
const DEV_PAGE_PREFIX_RE = /^furin-dev-(?:page|render):/;
|
|
34
35
|
const QUERY_RE = /\?.*$/;
|
|
35
36
|
const EVENT_LIMIT = 100;
|
|
36
37
|
|
|
@@ -91,6 +92,7 @@ function stackLocation(
|
|
|
91
92
|
const locations: DevSourceLocation[] = [];
|
|
92
93
|
for (const line of stack.split("\n")) {
|
|
93
94
|
const match = STACK_POSITION_RE.exec(line.trim());
|
|
95
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: stack lines need not match the position pattern.
|
|
94
96
|
if (!(match?.[1] && match[2] && match[3])) {
|
|
95
97
|
continue;
|
|
96
98
|
}
|
package/src/server/dev/error.ts
CHANGED
|
@@ -22,7 +22,7 @@ interface SourcePosition {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const STACK_POSITION_RE = /(?:^|\s)\(?((?:file:\/\/)?\S+):(\d+):(\d+)\)?$/;
|
|
25
|
-
const DEV_PAGE_PREFIX_RE = /^furin-dev-page:/;
|
|
25
|
+
const DEV_PAGE_PREFIX_RE = /^furin-dev-(?:page|render):/;
|
|
26
26
|
const QUERY_RE = /\?.*$/;
|
|
27
27
|
|
|
28
28
|
function errorMessage(error: unknown): string {
|
|
@@ -46,6 +46,7 @@ function stackPosition(stack: string | undefined, entryPath: string): SourcePosi
|
|
|
46
46
|
const positions: SourcePosition[] = [];
|
|
47
47
|
for (const line of stack.split("\n")) {
|
|
48
48
|
const match = STACK_POSITION_RE.exec(line.trim());
|
|
49
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: stack lines need not match the position pattern.
|
|
49
50
|
if (!(match?.[1] && match[2] && match[3])) {
|
|
50
51
|
continue;
|
|
51
52
|
}
|
package/src/server/dev/graph.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { readFileSync, statSync } from "node:fs";
|
|
3
|
-
import { dirname, extname } from "node:path";
|
|
3
|
+
import { dirname, extname, resolve } from "node:path";
|
|
4
4
|
import type { FurinRouteDispatcher } from "../../define-route.ts";
|
|
5
5
|
import { currentInstance, type FurinInstance } from "../instance.ts";
|
|
6
6
|
import type { ResolvedRoute, RootLayout } from "../router/types.ts";
|
|
@@ -77,7 +77,13 @@ export function resolveDevSourceImports(
|
|
|
77
77
|
const transpiler = new Bun.Transpiler({ loader });
|
|
78
78
|
for (const imported of transpiler.scanImports(source)) {
|
|
79
79
|
try {
|
|
80
|
-
const
|
|
80
|
+
const extension = extname(imported.path);
|
|
81
|
+
const isSourceExtension = [".js", ".jsx", ".ts", ".tsx"].includes(extension);
|
|
82
|
+
const absolute =
|
|
83
|
+
imported.path.startsWith(".") && extension && !isSourceExtension
|
|
84
|
+
? resolve(dirname(path), imported.path)
|
|
85
|
+
: Bun.resolveSync(imported.path, dirname(path));
|
|
86
|
+
const resolved = normalizeModulePath(absolute);
|
|
81
87
|
if (!resolved.includes("/node_modules/")) {
|
|
82
88
|
imports.push(resolved);
|
|
83
89
|
}
|
|
@@ -116,6 +122,7 @@ function transformErrorPosition(
|
|
|
116
122
|
*/
|
|
117
123
|
export class DevGraph<Snapshot> {
|
|
118
124
|
readonly #dependencies = new Map<string, Set<string>>();
|
|
125
|
+
readonly #dependents = new Map<string, Set<string>>();
|
|
119
126
|
readonly #events: DevGraphEvent[] = [];
|
|
120
127
|
readonly #listeners = new Set<(event: DevGraphEvent) => void>();
|
|
121
128
|
readonly #moduleCaches = new WeakMap<
|
|
@@ -280,10 +287,52 @@ export class DevGraph<Snapshot> {
|
|
|
280
287
|
}
|
|
281
288
|
|
|
282
289
|
recordImports(importer: string, imports: string[]): void {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
290
|
+
const path = normalizeModulePath(importer);
|
|
291
|
+
// Learning a module's dependencies during its first evaluation is not an
|
|
292
|
+
// edit. Rebase unchanged ancestors so contract and render imports share
|
|
293
|
+
// the same ESM instance. Actual edits must still advance their versions.
|
|
294
|
+
const ancestors = new Set([path]);
|
|
295
|
+
const pending = [path];
|
|
296
|
+
while (pending.length > 0) {
|
|
297
|
+
const dependency = pending.pop();
|
|
298
|
+
if (dependency === undefined) {
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
for (const parent of this.#dependents.get(dependency) ?? []) {
|
|
302
|
+
if (!ancestors.has(parent)) {
|
|
303
|
+
ancestors.add(parent);
|
|
304
|
+
pending.push(parent);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const unchanged = this.#dependencies.has(path)
|
|
309
|
+
? []
|
|
310
|
+
: [...ancestors].flatMap((modulePath) => {
|
|
311
|
+
const revision = this.#moduleRevisions.get(modulePath);
|
|
312
|
+
return revision?.fingerprint === this.#sourceFingerprint(modulePath, new Set())
|
|
313
|
+
? [[modulePath, revision] as const]
|
|
314
|
+
: [];
|
|
315
|
+
});
|
|
316
|
+
for (const dependency of this.#dependencies.get(path) ?? []) {
|
|
317
|
+
const dependents = this.#dependents.get(dependency);
|
|
318
|
+
dependents?.delete(path);
|
|
319
|
+
if (dependents?.size === 0) {
|
|
320
|
+
this.#dependents.delete(dependency);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const nextImports = new Set(imports.map(normalizeModulePath));
|
|
324
|
+
this.#dependencies.set(path, nextImports);
|
|
325
|
+
for (const dependency of nextImports) {
|
|
326
|
+
let dependents = this.#dependents.get(dependency);
|
|
327
|
+
if (!dependents) {
|
|
328
|
+
dependents = new Set();
|
|
329
|
+
this.#dependents.set(dependency, dependents);
|
|
330
|
+
}
|
|
331
|
+
dependents.add(path);
|
|
332
|
+
}
|
|
333
|
+
for (const [modulePath, revision] of unchanged) {
|
|
334
|
+
revision.fingerprint = this.#sourceFingerprint(modulePath, new Set());
|
|
335
|
+
}
|
|
287
336
|
}
|
|
288
337
|
|
|
289
338
|
recordSourceError(message: string, position: DevSourcePosition): void {
|
|
@@ -23,6 +23,10 @@ export function devGraph(): never {
|
|
|
23
23
|
throw new Error("[furin] Development route state is unavailable in production.");
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
export function developmentGraphs(): readonly never[] {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
export function injectDevDiagnosticClient(html: string, _basePath: string): string {
|
|
27
31
|
return html;
|
|
28
32
|
}
|
|
@@ -30,6 +30,7 @@ function parseGeneratedFrames(stack: string | undefined): readonly GeneratedFram
|
|
|
30
30
|
const frames: GeneratedFrame[] = [];
|
|
31
31
|
for (const line of stack.split("\n")) {
|
|
32
32
|
const match = STACK_FRAME_RE.exec(line);
|
|
33
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: stack lines need not match the frame pattern.
|
|
33
34
|
if (!(match?.[2] && match[3] && match[4])) {
|
|
34
35
|
continue;
|
|
35
36
|
}
|
|
@@ -61,6 +62,7 @@ async function loadSourceMap(url: URL, origin: string): Promise<TraceMap | undef
|
|
|
61
62
|
}
|
|
62
63
|
const generatedResponse = await fetch(url);
|
|
63
64
|
const generated = generatedResponse.ok ? await generatedResponse.text() : "";
|
|
65
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: generated code may omit a source map comment.
|
|
64
66
|
const sourceMapReference = SOURCE_MAP_RE.exec(generated)?.[1];
|
|
65
67
|
if (!sourceMapReference) {
|
|
66
68
|
return;
|
|
@@ -53,17 +53,21 @@
|
|
|
53
53
|
* rewritten while string literals in non-import positions are left untouched.
|
|
54
54
|
*/
|
|
55
55
|
|
|
56
|
-
import {
|
|
56
|
+
import { statSync } from "node:fs";
|
|
57
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
57
58
|
import { fileURLToPath } from "node:url";
|
|
59
|
+
import { splitDevPage } from "../plugin/transform-dev-page.ts";
|
|
58
60
|
import { transformIsomorphicFunctions } from "../plugin/transform-isomorphic.ts";
|
|
59
61
|
import { invalidateDevLoaderCacheBySource } from "./cache/dev-loader.ts";
|
|
60
62
|
import { publishDevError } from "./dev/error.ts";
|
|
61
63
|
import { developmentGraphs, resolveDevSourceImports } from "./dev/graph.ts";
|
|
62
64
|
import { rewriteModuleSpecifiers } from "./dev/rewrite-module-specifiers.ts";
|
|
63
65
|
import { DevTransformFailure } from "./dev/transform-failure.ts";
|
|
66
|
+
import { routeModuleSourceVersion } from "./router/source-version.ts";
|
|
64
67
|
|
|
65
68
|
// Matches ?furin-server with an optional &t=<ms> cache-buster.
|
|
66
69
|
const FURIN_SERVER_FILTER = /\?furin-server(?:&t=\d+)?$/;
|
|
70
|
+
const FURIN_RENDER_FILTER = /\?furin-render&t=\d+$/;
|
|
67
71
|
const ANY_FILTER = /.*/;
|
|
68
72
|
export const WORKSPACE_SOURCE_FILTER =
|
|
69
73
|
/^(?!.*(?:[\\/]node_modules[\\/]|[\\/]\.bun[\\/]))(?!.*\.(?:test|spec)\.[jt]sx?$).*\.[jt]sx?$/;
|
|
@@ -73,6 +77,7 @@ const STRIP_T_PARAM_RE = /\?t=\d+$/;
|
|
|
73
77
|
const DELETED_DEV_PAGE_CONTENTS = "export const route = undefined;";
|
|
74
78
|
|
|
75
79
|
let _pluginRegistered = false;
|
|
80
|
+
const pluginSourceStamps = new Map<string, string>();
|
|
76
81
|
|
|
77
82
|
type SourceLoader = "js" | "jsx" | "ts" | "tsx";
|
|
78
83
|
|
|
@@ -323,6 +328,28 @@ function shouldSkipWorkspaceTransform(filePath: string): boolean {
|
|
|
323
328
|
|
|
324
329
|
function recordDevImports(source: string, filePath: string): void {
|
|
325
330
|
const { imports } = resolveDevSourceImports(source, filePath, getSourceLoader(filePath) ?? "tsx");
|
|
331
|
+
for (const path of imports) {
|
|
332
|
+
if (!isAbsolute(path) || getSourceLoader(path)) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
// Plugin loaders match the original extension (e.g. .mdx), so a query
|
|
336
|
+
// suffix cannot version these modules. Evict only an edited source.
|
|
337
|
+
const stats = statSync(path, { bigint: true, throwIfNoEntry: false });
|
|
338
|
+
if (!stats) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
const stamp = `${stats.mtimeNs}:${stats.size}`;
|
|
342
|
+
const previous = pluginSourceStamps.get(path);
|
|
343
|
+
if (previous !== undefined && previous !== stamp) {
|
|
344
|
+
Reflect.deleteProperty(require.cache, path);
|
|
345
|
+
for (const cachedPath of Object.keys(require.cache)) {
|
|
346
|
+
if (toImportSpecifier(cachedPath) === toImportSpecifier(path)) {
|
|
347
|
+
Reflect.deleteProperty(require.cache, cachedPath);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
pluginSourceStamps.set(path, stamp);
|
|
352
|
+
}
|
|
326
353
|
for (const graph of developmentGraphs()) {
|
|
327
354
|
graph.recordImports(toImportSpecifier(filePath), imports);
|
|
328
355
|
}
|
|
@@ -364,7 +391,11 @@ function rethrowWithSourcePath(error: unknown, filePath: string): never {
|
|
|
364
391
|
export function transformDevSource(
|
|
365
392
|
raw: string,
|
|
366
393
|
filePath: string,
|
|
367
|
-
options: {
|
|
394
|
+
options: {
|
|
395
|
+
renderFragment?: boolean;
|
|
396
|
+
rewriteBareImports: boolean;
|
|
397
|
+
rewriteRelativeImports: boolean;
|
|
398
|
+
}
|
|
368
399
|
): string {
|
|
369
400
|
const loader = getSourceLoader(filePath);
|
|
370
401
|
if (!loader) {
|
|
@@ -375,8 +406,18 @@ export function transformDevSource(
|
|
|
375
406
|
graph.clearSourceErrors(filePath);
|
|
376
407
|
}
|
|
377
408
|
try {
|
|
378
|
-
|
|
379
|
-
|
|
409
|
+
if (!options.renderFragment) {
|
|
410
|
+
recordDevImports(raw, filePath);
|
|
411
|
+
}
|
|
412
|
+
const isomorphicSource = transformIsomorphicFunctions(raw, filePath, "server").code;
|
|
413
|
+
const split = options.renderFragment
|
|
414
|
+
? undefined
|
|
415
|
+
: splitDevPage(
|
|
416
|
+
isomorphicSource,
|
|
417
|
+
filePath,
|
|
418
|
+
`${filePath}?furin-render&t=${routeModuleSourceVersion(filePath)}`
|
|
419
|
+
);
|
|
420
|
+
const serverSource = split?.contract ?? isomorphicSource;
|
|
380
421
|
const sourceForTranspile = options.rewriteRelativeImports
|
|
381
422
|
? rewriteRelativeImportsWithVersion(serverSource, filePath, true)
|
|
382
423
|
: serverSource;
|
|
@@ -445,6 +486,27 @@ export function registerDevPagePlugin(): void {
|
|
|
445
486
|
Bun.plugin({
|
|
446
487
|
name: "furin-dev-page-loader",
|
|
447
488
|
setup(build) {
|
|
489
|
+
build.onResolve({ filter: FURIN_RENDER_FILTER }, (args) => ({
|
|
490
|
+
namespace: "furin-dev-render",
|
|
491
|
+
path: args.path,
|
|
492
|
+
}));
|
|
493
|
+
build.onLoad({ filter: ANY_FILTER, namespace: "furin-dev-render" }, async (args) => {
|
|
494
|
+
const filePath = args.path.slice(0, args.path.indexOf("?furin-render"));
|
|
495
|
+
const raw = await Bun.file(filePath).text();
|
|
496
|
+
const source = transformIsomorphicFunctions(raw, filePath, "server").code;
|
|
497
|
+
const split = splitDevPage(source, filePath, args.path);
|
|
498
|
+
if (!split) {
|
|
499
|
+
throw new Error(`[furin] Page changed while loading its render module: ${filePath}`);
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
contents: transformDevSource(split.render, filePath, {
|
|
503
|
+
renderFragment: true,
|
|
504
|
+
rewriteBareImports: true,
|
|
505
|
+
rewriteRelativeImports: true,
|
|
506
|
+
}),
|
|
507
|
+
loader: "js",
|
|
508
|
+
};
|
|
509
|
+
});
|
|
448
510
|
// Keep an already-loaded module generation usable if its source is
|
|
449
511
|
// deleted while Bun finishes that same generation. A later generation
|
|
450
512
|
// receives a tombstone instead of reviving the removed route.
|
|
@@ -457,6 +519,7 @@ export function registerDevPagePlugin(): void {
|
|
|
457
519
|
build.onResolve({ filter: FURIN_SERVER_FILTER }, (args) => {
|
|
458
520
|
const tMatch = T_PARAM_RE.exec(args.path);
|
|
459
521
|
const filePath = args.path.replace(STRIP_FURIN_SERVER_RE, "");
|
|
522
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: the timestamp query is optional.
|
|
460
523
|
const resolvedPath = tMatch ? `${filePath}?t=${tMatch[1]}` : filePath;
|
|
461
524
|
return { namespace: "furin-dev-page", path: resolvedPath };
|
|
462
525
|
});
|
package/src/server/evlog.ts
CHANGED
|
@@ -50,7 +50,7 @@ export function createFurinEvlog(options: FurinEvlogOptions) {
|
|
|
50
50
|
|
|
51
51
|
return new Elysia({ name: "furin-evlog" })
|
|
52
52
|
.derive("global", ({ request }) => {
|
|
53
|
-
const log = requestLoggers.get(request);
|
|
53
|
+
const log = requestLoggers.get(request) ?? loggerStorage.useLogger();
|
|
54
54
|
if (log === undefined) {
|
|
55
55
|
throw new Error("[furin] Request logger was not initialized");
|
|
56
56
|
}
|
package/src/server/internal.ts
CHANGED
|
@@ -25,11 +25,14 @@ export const FURIN_DEFERRED_GLOBAL = "__FURIN_DEFERRED__";
|
|
|
25
25
|
* Synchronous loader data is NOT carried here — it lives exclusively in the
|
|
26
26
|
* `__FURIN_DATA__` JSON script that the hydrate entry reads.
|
|
27
27
|
*
|
|
28
|
-
* CSP note: this is an inline `<script>` — nonce support is a v2 concern.
|
|
29
28
|
*/
|
|
30
|
-
export function
|
|
29
|
+
export function nonceAttribute(nonce?: string): string {
|
|
30
|
+
return nonce === undefined ? "" : ` nonce="${nonce}"`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function buildDeferredScript(deferredKeys: string[], nonce?: string): string {
|
|
31
34
|
const keysJson = safeJson(deferredKeys);
|
|
32
|
-
return `<script id="${FURIN_DEFERRED_GLOBAL}">
|
|
35
|
+
return `<script id="${FURIN_DEFERRED_GLOBAL}"${nonceAttribute(nonce)}>
|
|
33
36
|
window.${FURIN_DEFERRED_GLOBAL} = {
|
|
34
37
|
_deferredKeys: ${keysJson},
|
|
35
38
|
_chunks: {},
|
|
@@ -63,10 +66,11 @@ window.${FURIN_DEFERRED_GLOBAL} = {
|
|
|
63
66
|
export function buildDeferredResolution(
|
|
64
67
|
key: string,
|
|
65
68
|
chunk: ReturnType<typeof toCrossJSON>,
|
|
66
|
-
action: "resolve" | "reject"
|
|
69
|
+
action: "resolve" | "reject",
|
|
70
|
+
nonce?: string
|
|
67
71
|
): string {
|
|
68
72
|
const chunkJson = safeJson(chunk);
|
|
69
|
-
return `<script>window.${FURIN_DEFERRED_GLOBAL}.${action}(${safeJson(key)},${chunkJson})</script>`;
|
|
73
|
+
return `<script${nonceAttribute(nonce)}>window.${FURIN_DEFERRED_GLOBAL}.${action}(${safeJson(key)},${chunkJson})</script>`;
|
|
70
74
|
}
|
|
71
75
|
|
|
72
76
|
/** Minimal context passed to background / synthetic render helpers — only `request` is needed. */
|
|
@@ -108,8 +112,8 @@ export function buildRouteFrameTemplate(payload: string): string {
|
|
|
108
112
|
return `<template id="__FURIN_ROUTE_FRAMES__">${escaped}</template>`;
|
|
109
113
|
}
|
|
110
114
|
|
|
111
|
-
export function buildRouteFrameStreamScript(): string {
|
|
112
|
-
return `<script id="__FURIN_ROUTE_FRAME_STREAM__">
|
|
115
|
+
export function buildRouteFrameStreamScript(nonce?: string): string {
|
|
116
|
+
return `<script id="__FURIN_ROUTE_FRAME_STREAM__"${nonceAttribute(nonce)}>
|
|
113
117
|
window.__FURIN_ROUTE_FRAME_STREAM__ = {
|
|
114
118
|
_chunks: [],
|
|
115
119
|
_closed: false,
|
|
@@ -149,12 +153,12 @@ window.__FURIN_ROUTE_FRAME_STREAM__ = {
|
|
|
149
153
|
</script>`;
|
|
150
154
|
}
|
|
151
155
|
|
|
152
|
-
export function buildRouteFramePushScript(payload: string): string {
|
|
153
|
-
return `<script>window.__FURIN_ROUTE_FRAME_STREAM__.push(${safeJson(payload)})</script>`;
|
|
156
|
+
export function buildRouteFramePushScript(payload: string, nonce?: string): string {
|
|
157
|
+
return `<script${nonceAttribute(nonce)}>window.__FURIN_ROUTE_FRAME_STREAM__.push(${safeJson(payload)})</script>`;
|
|
154
158
|
}
|
|
155
159
|
|
|
156
|
-
export function buildRouteFrameCloseScript(): string {
|
|
157
|
-
return
|
|
160
|
+
export function buildRouteFrameCloseScript(nonce?: string): string {
|
|
161
|
+
return `<script${nonceAttribute(nonce)}>window.__FURIN_ROUTE_FRAME_STREAM__.close()</script>`;
|
|
158
162
|
}
|
|
159
163
|
|
|
160
164
|
export function buildSyncRuntimeScript(): string {
|
|
@@ -14,7 +14,8 @@ export function withDocumentState(
|
|
|
14
14
|
element: ReactNode,
|
|
15
15
|
assets: DocumentAssets,
|
|
16
16
|
head: HeadOptions | undefined,
|
|
17
|
-
data: object | undefined
|
|
17
|
+
data: object | undefined,
|
|
18
|
+
nonce?: string
|
|
18
19
|
): ReactNode {
|
|
19
20
|
const syncPath = getSyncPath();
|
|
20
21
|
const browserEventsClientPath = `${currentInstance().prefix}/_furin/events/client.js`;
|
|
@@ -33,6 +34,7 @@ export function withDocumentState(
|
|
|
33
34
|
assets: resolvedAssets,
|
|
34
35
|
dataJson: data === undefined || routeFrames !== undefined ? undefined : safeJson(data),
|
|
35
36
|
head,
|
|
37
|
+
nonce,
|
|
36
38
|
routeFrames,
|
|
37
39
|
syncJson: syncPath === undefined ? undefined : safeJson({ path: syncPath }),
|
|
38
40
|
};
|