@teyik0/furin 0.8.0-alpha.0 → 0.9.0-alpha.0
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 +37 -0
- package/dist/adapter/bun/app.d.ts +1 -0
- package/dist/adapter/bun.d.ts +3 -1
- package/dist/adapter/runtime-build.d.ts +8 -5
- package/dist/adapter/vercel.js +8 -8
- package/dist/build/client.d.ts +2 -2
- package/dist/build/discover-mounts-process.d.ts +1 -1
- package/dist/build/discover-pages-process.d.ts +1 -0
- package/dist/build/discover-pages.d.ts +11 -0
- package/dist/build/discovery-module.d.ts +3 -0
- package/dist/build/discovery-plugins.d.ts +34 -0
- package/dist/build/elysia-aot.d.ts +1 -1
- package/dist/build/entry-template.ts +6 -1
- package/dist/build/hydrate.d.ts +3 -2
- package/dist/build/index.js +7688 -8458
- package/dist/build/preload-manifest.d.ts +2 -2
- package/dist/build/route-metadata.d.ts +31 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/shared.d.ts +2 -2
- package/dist/build/types.d.ts +11 -0
- package/dist/cli/dev-worker.d.ts +1 -0
- package/dist/cli/dev-worker.ts +123 -0
- package/dist/cli/dev.d.ts +1 -0
- package/dist/cli/index.js +7811 -8477
- package/dist/client/browser-events-runtime.d.ts +2 -2
- package/dist/client/browser-events.d.ts +2 -0
- package/dist/client/chunks/{client-brdfd773.js → client-rjhxgt64.js} +187 -4
- package/dist/client/dev-error-overlay.ts +18 -1
- package/dist/client/link.js +110 -156
- package/dist/client/query-store.d.ts +5 -0
- package/dist/client/router/navigation-controller.d.ts +2 -0
- package/dist/client/router/use-fetch-interception.d.ts +1 -1
- package/dist/client/router/use-sync-stream.d.ts +1 -1
- package/dist/client/{router/sync-catch-up.d.ts → sync-catch-up.d.ts} +5 -3
- package/dist/client/sync-client.d.ts +74 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +533 -145
- package/dist/config.d.ts +6 -1
- package/dist/config.js +3 -1
- package/dist/define-route.d.ts +3 -3
- package/dist/furin.d.ts +2 -1
- package/dist/furin.js +1119 -346
- package/dist/plugin/index.js +5 -5
- package/dist/plugin/route-registry.d.ts +22 -2
- package/dist/rsc/build/index.d.ts +4 -3
- package/dist/server/dev/server-only-module.d.ts +5 -0
- package/dist/server/internal.js +201 -91
- package/dist/server/logging/ingest-plugin.d.ts +1 -2
- package/dist/server/production-server.js +36 -33
- package/dist/server/server-entry.d.ts +5 -0
- package/dist/server/server-entry.js +36 -0
- package/dist/server/static-export.js +201 -91
- package/dist/server/sync/adapter.d.ts +4 -3
- package/dist/server/sync/config.d.ts +7 -0
- package/dist/server/sync/connection.d.ts +13 -0
- package/dist/server/sync/drizzle/index.js +5 -4
- package/dist/server/sync/index.d.ts +3 -0
- package/dist/server/sync/index.js +1361 -243
- package/dist/server/sync/invalidate-sync.d.ts +6 -2
- package/dist/server/sync/local-client.d.ts +9 -0
- package/dist/server/sync/postgres/index.d.ts +1 -1
- package/dist/server/sync/postgres/index.js +2 -2
- package/dist/server/sync/prisma/index.js +2 -2
- package/dist/server/sync/redis/index.d.ts +1 -1
- package/dist/server/sync/redis/index.js +4 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/runtime.d.ts +3 -0
- package/dist/server/sync/sqlite/index.d.ts +1 -1
- package/dist/server/sync/sqlite/index.js +3 -2
- package/dist/shared/browser-events.d.ts +4 -0
- package/dist/sync-client.d.ts +3 -0
- package/dist/sync-client.js +1600 -0
- package/package.json +12 -7
- package/src/adapter/README.md +7 -0
- package/src/adapter/build.ts +34 -33
- package/src/adapter/bun/app.ts +5 -0
- package/src/adapter/bun/server.ts +2 -2
- package/src/adapter/bun.ts +18 -2
- package/src/adapter/furin-app.d.ts +1 -0
- package/src/adapter/package.ts +1 -0
- package/src/adapter/runtime-build.ts +18 -12
- package/src/adapter/static.ts +17 -13
- package/src/build/client.ts +7 -5
- package/src/build/discover-mounts-process.ts +5 -1
- package/src/build/discover-pages-process.ts +151 -0
- package/src/build/discover-pages.ts +69 -0
- package/src/build/discovery-module.ts +70 -0
- package/src/build/discovery-plugins.ts +101 -0
- package/src/build/elysia-aot-worker.ts +9 -8
- package/src/build/elysia-aot.ts +110 -75
- package/src/build/entry-template.ts +6 -1
- package/src/build/hydrate.ts +11 -8
- package/src/build/index.ts +6 -50
- package/src/build/preload-manifest.ts +3 -3
- package/src/build/route-metadata.ts +90 -0
- package/src/build/route-types.ts +1 -1
- package/src/build/shared.ts +6 -8
- package/src/build/types.ts +9 -0
- package/src/cli/config.ts +4 -0
- package/src/cli/dev-worker.ts +123 -0
- package/src/cli/dev.ts +91 -0
- package/src/cli/index.ts +24 -1
- package/src/client/browser-events-runtime.ts +97 -23
- package/src/client/browser-events.ts +8 -0
- package/src/client/dev-error-overlay.ts +18 -1
- package/src/client/query-store.ts +54 -3
- package/src/client/router/navigation-controller.ts +45 -12
- package/src/client/router/provider.tsx +6 -2
- package/src/client/router/use-fetch-interception.ts +1 -1
- package/src/client/router/use-sync-stream.ts +96 -44
- package/src/client/{router/sync-catch-up.ts → sync-catch-up.ts} +28 -12
- package/src/client/sync-client.ts +528 -0
- package/src/client/sync.ts +11 -4
- package/src/client.ts +10 -0
- package/src/config.ts +17 -10
- package/src/furin.ts +5 -0
- package/src/plugin/route-registry.ts +53 -7
- package/src/rsc/build/index.ts +6 -3
- package/src/server/browser-events/plugin.ts +79 -25
- package/src/server/dev/rewrite-module-specifiers.ts +2 -1
- package/src/server/dev/server-only-module.ts +23 -0
- package/src/server/dev/symbolicate.ts +4 -3
- package/src/server/dev-page-imports.ts +3 -1
- package/src/server/dev-page-plugin.ts +18 -1
- package/src/server/logging/ingest-plugin.ts +3 -9
- package/src/server/server-entry.ts +39 -0
- package/src/server/sync/adapter.ts +7 -3
- package/src/server/sync/config.ts +12 -6
- package/src/server/sync/connection.ts +71 -0
- package/src/server/sync/index.ts +3 -0
- package/src/server/sync/invalidate-sync.ts +30 -5
- package/src/server/sync/local-client.ts +84 -0
- package/src/server/sync/plugin.ts +27 -20
- package/src/server/sync/postgres/index.ts +9 -2
- package/src/server/sync/redis/index.ts +5 -1
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/runtime.ts +56 -18
- package/src/server/sync/sqlite/index.ts +6 -2
- package/src/server/sync/stream.ts +17 -5
- package/src/shared/browser-events.ts +1 -1
- package/src/sync-client.ts +14 -0
- package/dist/server/logger.d.ts +0 -10
- package/src/server/logger.ts +0 -34
|
@@ -32,10 +32,23 @@ const SERVER_ROUTES_FILTER = /^(?:@teyik0\/furin|furin)\/routes\?instance=.+$/;
|
|
|
32
32
|
const VIRTUAL_ROUTES_FILTER = /.*/;
|
|
33
33
|
|
|
34
34
|
export interface CreateRoutesPluginOptions {
|
|
35
|
-
instances:
|
|
35
|
+
instances: RegistryInstance[];
|
|
36
36
|
target: "client" | "server";
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
interface RegistryInstance extends RouteInstanceSpec {
|
|
40
|
+
routeRegistry?: RouteRegistrySnapshot;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Build-time route evaluation is completed before the discovery process exits. */
|
|
44
|
+
export interface RouteRegistrySnapshot {
|
|
45
|
+
client: string;
|
|
46
|
+
complete: GeneratedServerInstance;
|
|
47
|
+
composable: GeneratedServerInstance;
|
|
48
|
+
files: [string, RouteModuleInfo][];
|
|
49
|
+
materialized: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
39
52
|
export function composableRouteModuleSpecifier(instance: RouteInstanceSpec): string {
|
|
40
53
|
return `${routeModuleSpecifier(instance)}&composable`;
|
|
41
54
|
}
|
|
@@ -112,11 +125,19 @@ async function retainComposableRoutes(node: RouteTreeNode): Promise<void> {
|
|
|
112
125
|
}
|
|
113
126
|
|
|
114
127
|
async function generateServerInstance(
|
|
115
|
-
instance:
|
|
128
|
+
instance: RegistryInstance,
|
|
116
129
|
routeFilesBySpecifier: Map<string, RouteModuleInfo>,
|
|
117
130
|
materialized: boolean,
|
|
118
131
|
includeRootCatchAll: boolean
|
|
119
132
|
): Promise<GeneratedServerInstance> {
|
|
133
|
+
if (instance.routeRegistry) {
|
|
134
|
+
for (const [specifier, info] of instance.routeRegistry.files) {
|
|
135
|
+
routeFilesBySpecifier.set(specifier, info);
|
|
136
|
+
}
|
|
137
|
+
return includeRootCatchAll
|
|
138
|
+
? instance.routeRegistry.complete
|
|
139
|
+
: instance.routeRegistry.composable;
|
|
140
|
+
}
|
|
120
141
|
const instanceId = Bun.hash(instanceKey(instance)).toString(16);
|
|
121
142
|
const tree = buildRouteTree(instance.pagesDir, instanceId);
|
|
122
143
|
await retainComposableRoutes(tree);
|
|
@@ -141,7 +162,7 @@ async function generateServerInstance(
|
|
|
141
162
|
}
|
|
142
163
|
|
|
143
164
|
async function serverRegistrySource(
|
|
144
|
-
instances:
|
|
165
|
+
instances: RegistryInstance[],
|
|
145
166
|
routeFilesBySpecifier: Map<string, RouteModuleInfo>,
|
|
146
167
|
materialized: boolean,
|
|
147
168
|
includeRootCatchAll: boolean
|
|
@@ -165,7 +186,10 @@ ${generated
|
|
|
165
186
|
* Emits a real server route module for build-time consumers such as Elysia's
|
|
166
187
|
* AOT plugin, which imports the application outside Bun's virtual module graph.
|
|
167
188
|
*/
|
|
168
|
-
export async function materializedRouteModuleSource(instance:
|
|
189
|
+
export async function materializedRouteModuleSource(instance: RegistryInstance): Promise<string> {
|
|
190
|
+
if (instance.routeRegistry) {
|
|
191
|
+
return instance.routeRegistry.materialized;
|
|
192
|
+
}
|
|
169
193
|
const routeFilesBySpecifier = new Map<string, RouteModuleInfo>();
|
|
170
194
|
const source = await serverRegistrySource([instance], routeFilesBySpecifier, true, false);
|
|
171
195
|
await validateRouteModules(routeFilesBySpecifier.values());
|
|
@@ -179,7 +203,10 @@ interface ClientRouteMetadata {
|
|
|
179
203
|
pattern: string;
|
|
180
204
|
}
|
|
181
205
|
|
|
182
|
-
async function clientRoutesSource(instance:
|
|
206
|
+
async function clientRoutesSource(instance: RegistryInstance): Promise<string> {
|
|
207
|
+
if (instance.routeRegistry) {
|
|
208
|
+
return instance.routeRegistry.client;
|
|
209
|
+
}
|
|
183
210
|
const tree = buildRouteTree(instance.pagesDir, Bun.hash(instanceKey(instance)).toString(16));
|
|
184
211
|
const routes = collectRouteFiles(tree)
|
|
185
212
|
.filter((route) => route.path !== "")
|
|
@@ -198,7 +225,24 @@ async function clientRoutesSource(instance: RouteInstanceSpec): Promise<string>
|
|
|
198
225
|
return `export const routes = ${JSON.stringify(metadata)};\n`;
|
|
199
226
|
}
|
|
200
227
|
|
|
201
|
-
|
|
228
|
+
/** Evaluated only inside build discovery; runtime/dev keep loading live modules. */
|
|
229
|
+
export async function snapshotRouteRegistry(
|
|
230
|
+
instance: RouteInstanceSpec
|
|
231
|
+
): Promise<RouteRegistrySnapshot> {
|
|
232
|
+
const files = new Map<string, RouteModuleInfo>();
|
|
233
|
+
const complete = await generateServerInstance(instance, files, false, true);
|
|
234
|
+
const composable = await generateServerInstance(instance, files, false, false);
|
|
235
|
+
await validateRouteModules(files.values());
|
|
236
|
+
return {
|
|
237
|
+
client: await clientRoutesSource(instance),
|
|
238
|
+
complete,
|
|
239
|
+
composable,
|
|
240
|
+
files: [...files],
|
|
241
|
+
materialized: await materializedRouteModuleSource(instance),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function primaryInstance(instances: RegistryInstance[]): RegistryInstance {
|
|
202
246
|
const instance = instances.find(({ prefix }) => prefix === "") ?? instances[0];
|
|
203
247
|
if (!instance) {
|
|
204
248
|
throw new Error("[furin] The routes plugin requires at least one instance");
|
|
@@ -279,7 +323,9 @@ export function createRoutesPlugin(options: CreateRoutesPluginOptions): Bun.BunP
|
|
|
279
323
|
false,
|
|
280
324
|
path === registryVirtualPath
|
|
281
325
|
);
|
|
282
|
-
|
|
326
|
+
if (!options.instances.every((instance) => instance.routeRegistry)) {
|
|
327
|
+
await validateRouteModules(routeFilesBySpecifier.values());
|
|
328
|
+
}
|
|
283
329
|
return { contents, loader: "ts" };
|
|
284
330
|
});
|
|
285
331
|
build.onResolve({ filter: ROUTE_FILE_FILTER }, ({ path }) => {
|
package/src/rsc/build/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { mkdirSync, writeFileSync } from "node:fs";
|
|
|
2
2
|
import { basename, join } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { PRODUCTION_DEFINE, runBunBuild } from "../../build/bun-build.ts";
|
|
5
|
+
import type { BuildRoot } from "../../build/route-metadata.ts";
|
|
5
6
|
import { CLIENT_MODULE_PATH, LINK_MODULE_PATH } from "../../build/shared.ts";
|
|
6
7
|
import { isomorphicTransformPlugin } from "../../plugin/transform-isomorphic.ts";
|
|
7
8
|
import type { ResolvedRoute, RootLayout } from "../../server/router/types.ts";
|
|
@@ -27,8 +28,8 @@ export interface RscManifest {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export interface RscBuildApp {
|
|
30
|
-
root: RootLayout;
|
|
31
|
-
routes: ResolvedRoute[];
|
|
31
|
+
root: RootLayout | BuildRoot;
|
|
32
|
+
routes: Pick<ResolvedRoute, "path">[];
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
// Both builds run in the react-server graph with the same transforms.
|
|
@@ -63,7 +64,8 @@ export async function buildRscGraph(
|
|
|
63
64
|
apps: RscBuildApp[],
|
|
64
65
|
outDir: string,
|
|
65
66
|
buildId: string,
|
|
66
|
-
userPlugins: Bun.BunPlugin[] | undefined
|
|
67
|
+
userPlugins: Bun.BunPlugin[] | undefined,
|
|
68
|
+
external?: string[]
|
|
67
69
|
): Promise<RscManifest> {
|
|
68
70
|
await assertInstalledRscVersions();
|
|
69
71
|
const graphDir = join(outDir, "rsc");
|
|
@@ -95,6 +97,7 @@ export async function buildRscGraph(
|
|
|
95
97
|
const result = await runBunBuild({
|
|
96
98
|
...reactServerBuild,
|
|
97
99
|
entrypoints,
|
|
100
|
+
external,
|
|
98
101
|
metafile: true,
|
|
99
102
|
outdir: graphDir,
|
|
100
103
|
splitting: true,
|
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
} from "../../shared/browser-events.ts";
|
|
11
11
|
import { forbiddenDevelopmentRequest } from "../dev/request-security.ts";
|
|
12
12
|
import type { FurinSyncOptions } from "../sync/config.ts";
|
|
13
|
+
import { createSyncConnectionLifetime, verifiedSyncPrincipal } from "../sync/connection.ts";
|
|
14
|
+
import { prepareSyncRuntime } from "../sync/runtime.ts";
|
|
13
15
|
import { subscribeSyncCursor } from "../sync/stream.ts";
|
|
14
16
|
import { registerBrowserEventConnection, unregisterBrowserEventConnection } from "./shutdown.ts";
|
|
15
17
|
import type { BrowserEventSource, BrowserEventSubscription } from "./types.ts";
|
|
@@ -20,7 +22,6 @@ const HEARTBEAT_INTERVAL_MS = 30_000;
|
|
|
20
22
|
const SSE_CONNECTION_LIFETIME_MS = 60_000;
|
|
21
23
|
const MAX_BROWSER_EVENT_CONNECTIONS = 100;
|
|
22
24
|
const MAX_BROWSER_EVENT_CONNECTIONS_PER_PEER = 10;
|
|
23
|
-
const CLIENT_SOURCE = browserEventsClientSource();
|
|
24
25
|
const SSE_CLIENT_SOURCE = browserEventsSseClientSource();
|
|
25
26
|
const SSE_ENCODER = new TextEncoder();
|
|
26
27
|
|
|
@@ -41,10 +42,9 @@ interface ConnectionState {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
/** Who holds a connection: a verified principal, or the peer address without one. */
|
|
44
|
-
|
|
45
|
-
key: string;
|
|
46
|
-
sync:
|
|
47
|
-
}
|
|
45
|
+
type BrowserEventPeer =
|
|
46
|
+
| { key: string; sync: false }
|
|
47
|
+
| { key: string; sync: true; context: Context };
|
|
48
48
|
|
|
49
49
|
/** Bounds connections globally and per peer so one client cannot take the whole capacity. */
|
|
50
50
|
function createConnectionCapacity() {
|
|
@@ -75,19 +75,6 @@ function createConnectionCapacity() {
|
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
/** Resolves the Sync principal; a throwing or empty resolver means the caller is unauthenticated. */
|
|
79
|
-
async function verifiedPrincipal(
|
|
80
|
-
sync: FurinSyncOptions,
|
|
81
|
-
context: Context
|
|
82
|
-
): Promise<string | undefined> {
|
|
83
|
-
try {
|
|
84
|
-
const principal = await sync.principal(context);
|
|
85
|
-
return principal.length > 0 ? principal : undefined;
|
|
86
|
-
} catch {
|
|
87
|
-
// Resolvers reject signed-out requests by throwing, as `requireUser` does in the docs.
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
78
|
function peerAddressKey(request: Request, server: Bun.Server<unknown> | null | undefined): string {
|
|
92
79
|
return `address:${server?.requestIP(request)?.address ?? "unknown"}`;
|
|
93
80
|
}
|
|
@@ -129,6 +116,23 @@ export function createBrowserEventsPlugin(options: BrowserEventsPluginOptions):
|
|
|
129
116
|
const capacity = createConnectionCapacity();
|
|
130
117
|
const peers = new WeakMap<Request, BrowserEventPeer>();
|
|
131
118
|
const { development, sources = [], sync } = options;
|
|
119
|
+
if (sync) {
|
|
120
|
+
prepareSyncRuntime(sync, development);
|
|
121
|
+
}
|
|
122
|
+
const clientSource = browserEventsClientSource(sync !== undefined);
|
|
123
|
+
const authenticate = async (context: Context): Promise<Response> => {
|
|
124
|
+
const forbidden = forbiddenBrowserRequest(context.request, context.server, development);
|
|
125
|
+
if (forbidden) {
|
|
126
|
+
return forbidden;
|
|
127
|
+
}
|
|
128
|
+
return new Response(null, {
|
|
129
|
+
status:
|
|
130
|
+
!sync || sources.length > 0 || (await verifiedSyncPrincipal(sync, context)) !== undefined
|
|
131
|
+
? 200
|
|
132
|
+
: 401,
|
|
133
|
+
headers: { "cache-control": "no-store" },
|
|
134
|
+
});
|
|
135
|
+
};
|
|
132
136
|
return new Elysia({ name: "furin-browser-events" })
|
|
133
137
|
.use(websocket())
|
|
134
138
|
.get(CLIENT_PATH, ({ request, server }) => {
|
|
@@ -136,7 +140,7 @@ export function createBrowserEventsPlugin(options: BrowserEventsPluginOptions):
|
|
|
136
140
|
if (forbidden) {
|
|
137
141
|
return forbidden;
|
|
138
142
|
}
|
|
139
|
-
return new Response(
|
|
143
|
+
return new Response(clientSource, {
|
|
140
144
|
headers: {
|
|
141
145
|
"cache-control": "no-store",
|
|
142
146
|
"content-type": "text/javascript; charset=utf-8",
|
|
@@ -144,6 +148,7 @@ export function createBrowserEventsPlugin(options: BrowserEventsPluginOptions):
|
|
|
144
148
|
},
|
|
145
149
|
});
|
|
146
150
|
})
|
|
151
|
+
.head(SOCKET_PATH, (context) => authenticate(context as Context))
|
|
147
152
|
.ws(SOCKET_PATH, {
|
|
148
153
|
async beforeHandle(context) {
|
|
149
154
|
const { request, server } = context;
|
|
@@ -151,13 +156,13 @@ export function createBrowserEventsPlugin(options: BrowserEventsPluginOptions):
|
|
|
151
156
|
if (forbidden) {
|
|
152
157
|
return forbidden;
|
|
153
158
|
}
|
|
154
|
-
const principal = sync && (await
|
|
159
|
+
const principal = sync && (await verifiedSyncPrincipal(sync, context as Context));
|
|
155
160
|
// Development sources stay available to signed-out pages; their Sync cursor does not.
|
|
156
161
|
if (sync && principal === undefined && sources.length === 0) {
|
|
157
162
|
return new Response("Unauthorized", { status: 401 });
|
|
158
163
|
}
|
|
159
164
|
const peer: BrowserEventPeer = principal
|
|
160
|
-
? { key: `principal:${principal}`, sync: true }
|
|
165
|
+
? { key: `principal:${principal}`, sync: true, context: context as Context }
|
|
161
166
|
: { key: peerAddressKey(request, server), sync: false };
|
|
162
167
|
if (capacity.full(peer.key)) {
|
|
163
168
|
return Response.json({ code: "FURIN_BROWSER_EVENTS_CAPACITY" }, { status: 503 });
|
|
@@ -214,6 +219,21 @@ export function createBrowserEventsPlugin(options: BrowserEventsPluginOptions):
|
|
|
214
219
|
|
|
215
220
|
try {
|
|
216
221
|
if (sync !== undefined && peer.sync) {
|
|
222
|
+
const access = createSyncConnectionLifetime(sync, peer.context, () => {
|
|
223
|
+
send({
|
|
224
|
+
channel: "sync",
|
|
225
|
+
data: { context: "changed" },
|
|
226
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
227
|
+
});
|
|
228
|
+
releaseConnection(state);
|
|
229
|
+
connections.delete(ws.id);
|
|
230
|
+
ws.close(4410, "Sync context changed");
|
|
231
|
+
});
|
|
232
|
+
keep({ unsubscribe: access.close });
|
|
233
|
+
await access.acquire();
|
|
234
|
+
if (state.closed) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
217
237
|
const subscription = await subscribeSyncCursor(sync, development, (cursor) =>
|
|
218
238
|
send({
|
|
219
239
|
channel: "sync",
|
|
@@ -253,8 +273,19 @@ export function createSseBrowserEventsPlugin({
|
|
|
253
273
|
development: boolean;
|
|
254
274
|
sync: FurinSyncOptions;
|
|
255
275
|
}): AnyElysia {
|
|
276
|
+
prepareSyncRuntime(sync, development);
|
|
256
277
|
const capacity = createConnectionCapacity();
|
|
257
278
|
return new Elysia({ name: "furin-browser-events-sse" })
|
|
279
|
+
.head(SOCKET_PATH, async (context) => {
|
|
280
|
+
const forbidden = forbiddenBrowserRequest(context.request, context.server, development);
|
|
281
|
+
if (forbidden) {
|
|
282
|
+
return forbidden;
|
|
283
|
+
}
|
|
284
|
+
return new Response(null, {
|
|
285
|
+
status: (await verifiedSyncPrincipal(sync, context as Context)) === undefined ? 401 : 200,
|
|
286
|
+
headers: { "cache-control": "no-store" },
|
|
287
|
+
});
|
|
288
|
+
})
|
|
258
289
|
.get(CLIENT_PATH, ({ request, server }) => {
|
|
259
290
|
const forbidden = forbiddenBrowserRequest(request, server, development);
|
|
260
291
|
if (forbidden) {
|
|
@@ -274,7 +305,7 @@ export function createSseBrowserEventsPlugin({
|
|
|
274
305
|
if (forbidden) {
|
|
275
306
|
return forbidden;
|
|
276
307
|
}
|
|
277
|
-
const principal = await
|
|
308
|
+
const principal = await verifiedSyncPrincipal(sync, context as Context);
|
|
278
309
|
if (principal === undefined) {
|
|
279
310
|
return new Response("Unauthorized", { status: 401 });
|
|
280
311
|
}
|
|
@@ -297,6 +328,7 @@ export function createSseBrowserEventsPlugin({
|
|
|
297
328
|
let closed = false;
|
|
298
329
|
let subscription: BrowserEventSubscription | undefined;
|
|
299
330
|
let lifetime: ReturnType<typeof setTimeout> | undefined;
|
|
331
|
+
let access: ReturnType<typeof createSyncConnectionLifetime> | undefined;
|
|
300
332
|
const send = (event: BrowserEventEnvelope): void => {
|
|
301
333
|
if (!closed) {
|
|
302
334
|
controller.enqueue(SSE_ENCODER.encode(`data: ${serialized(event)}\n\n`));
|
|
@@ -327,6 +359,7 @@ export function createSseBrowserEventsPlugin({
|
|
|
327
359
|
clearTimeout(lifetime);
|
|
328
360
|
}
|
|
329
361
|
request.signal.removeEventListener("abort", onAbort);
|
|
362
|
+
access?.close();
|
|
330
363
|
subscription?.unsubscribe();
|
|
331
364
|
releaseCapacity();
|
|
332
365
|
};
|
|
@@ -339,10 +372,31 @@ export function createSseBrowserEventsPlugin({
|
|
|
339
372
|
lifetime = setTimeout(finish, SSE_CONNECTION_LIFETIME_MS);
|
|
340
373
|
lifetime.unref?.();
|
|
341
374
|
controller.enqueue(SSE_ENCODER.encode(": connected\n\n"));
|
|
342
|
-
|
|
343
|
-
send({
|
|
344
|
-
|
|
375
|
+
access = createSyncConnectionLifetime(sync, context as Context, () => {
|
|
376
|
+
send({
|
|
377
|
+
channel: "sync",
|
|
378
|
+
data: { context: "changed" },
|
|
379
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
380
|
+
});
|
|
381
|
+
finish();
|
|
382
|
+
});
|
|
383
|
+
access
|
|
384
|
+
.acquire()
|
|
385
|
+
.then(() =>
|
|
386
|
+
closed
|
|
387
|
+
? undefined
|
|
388
|
+
: subscribeSyncCursor(sync, development, (cursor) =>
|
|
389
|
+
send({
|
|
390
|
+
channel: "sync",
|
|
391
|
+
data: { cursor },
|
|
392
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
393
|
+
})
|
|
394
|
+
)
|
|
395
|
+
)
|
|
345
396
|
.then((next) => {
|
|
397
|
+
if (!next) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
346
400
|
if (closed) {
|
|
347
401
|
next.unsubscribe();
|
|
348
402
|
} else {
|
|
@@ -5,6 +5,7 @@ import { parseSource } from "../../shared/parser.ts";
|
|
|
5
5
|
import { walkAST } from "../../shared/utils/ast-walk.ts";
|
|
6
6
|
import { detectLangFromPath, SCRIPT_FILE_FILTER } from "../lang-detect.ts";
|
|
7
7
|
import { routeModuleSourceVersion } from "../router/source-version.ts";
|
|
8
|
+
import { isServerOnlyModule } from "./server-only-module.ts";
|
|
8
9
|
|
|
9
10
|
interface RewriteModuleSpecifiersInput {
|
|
10
11
|
code: string;
|
|
@@ -41,7 +42,7 @@ function relativeSpecifier(node: Node): StringLiteral | undefined {
|
|
|
41
42
|
function resolvedSpecifier(specifier: string, directory: string, versioned: boolean): string {
|
|
42
43
|
const path = versioned ? Bun.resolveSync(specifier, directory) : resolve(directory, specifier);
|
|
43
44
|
const normalized = path.replaceAll("\\", "/");
|
|
44
|
-
return versioned && SCRIPT_FILE_FILTER.test(normalized)
|
|
45
|
+
return versioned && SCRIPT_FILE_FILTER.test(normalized) && !isServerOnlyModule(path)
|
|
45
46
|
? `${normalized}?furin-server&t=${routeModuleSourceVersion(normalized)}`
|
|
46
47
|
: normalized;
|
|
47
48
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
const SERVER_ONLY_IMPORT = /^\s*import\s+["'](?:@teyik0\/)?furin\/server-only["'];?\s*$/m;
|
|
4
|
+
const cache = new Map<string, { mtimeMs: number; serverOnly: boolean }>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Server-only modules hold server state and render no component, so development keeps their
|
|
8
|
+
* file identity: versioning them would evaluate a second copy beside the server entry's.
|
|
9
|
+
*/
|
|
10
|
+
export function isServerOnlyModule(path: string): boolean {
|
|
11
|
+
const stats = statSync(path, { throwIfNoEntry: false });
|
|
12
|
+
if (!stats) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const { mtimeMs } = stats;
|
|
16
|
+
const cached = cache.get(path);
|
|
17
|
+
if (cached?.mtimeMs === mtimeMs) {
|
|
18
|
+
return cached.serverOnly;
|
|
19
|
+
}
|
|
20
|
+
const serverOnly = SERVER_ONLY_IMPORT.test(readFileSync(path, "utf8"));
|
|
21
|
+
cache.set(path, { mtimeMs, serverOnly });
|
|
22
|
+
return serverOnly;
|
|
23
|
+
}
|
|
@@ -18,7 +18,8 @@ interface SymbolicatedStack {
|
|
|
18
18
|
|
|
19
19
|
const STACK_FRAME_RE =
|
|
20
20
|
/^\s*at\s+(?:(.*?)\s+\()?((?:https?:\/\/|file:\/\/|bun:\/\/).+):(\d+):(\d+)\)?$/;
|
|
21
|
-
const
|
|
21
|
+
const WEBKIT_FRAME_RE = /^(.*?)@((?:https?:\/\/|file:\/\/|bun:\/\/).+):(\d+):(\d+)$/;
|
|
22
|
+
const SOURCE_MAP_RE = /^\s*\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/m;
|
|
22
23
|
const BUN_ROOT_CHUNK_RE = /^\/chunk-[^/]+\.js$/;
|
|
23
24
|
const SOURCE_MAP_CACHE_LIMIT = 100;
|
|
24
25
|
const sourceMaps = new Map<string, Promise<TraceMap | undefined>>();
|
|
@@ -29,8 +30,8 @@ function parseGeneratedFrames(stack: string | undefined): readonly GeneratedFram
|
|
|
29
30
|
}
|
|
30
31
|
const frames: GeneratedFrame[] = [];
|
|
31
32
|
for (const line of stack.split("\n")) {
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: exec returns null for nonmatching stack formats.
|
|
34
|
+
const match = STACK_FRAME_RE.exec(line) ?? WEBKIT_FRAME_RE.exec(line);
|
|
34
35
|
if (!(match?.[2] && match[3] && match[4])) {
|
|
35
36
|
continue;
|
|
36
37
|
}
|
|
@@ -11,6 +11,7 @@ import MagicString from "magic-string";
|
|
|
11
11
|
import { parseSource } from "../shared/parser.ts";
|
|
12
12
|
import { walkAST } from "../shared/utils/ast-walk.ts";
|
|
13
13
|
import { rewriteModuleSpecifiers } from "./dev/rewrite-module-specifiers.ts";
|
|
14
|
+
import { isServerOnlyModule } from "./dev/server-only-module.ts";
|
|
14
15
|
import { routeModuleSourceVersion } from "./router/source-version.ts";
|
|
15
16
|
|
|
16
17
|
export const WORKSPACE_SOURCE_FILTER =
|
|
@@ -173,7 +174,8 @@ export function rewriteBareImports(source: string, dir: string, loader: SourceLo
|
|
|
173
174
|
specifier !== "furin" &&
|
|
174
175
|
!specifier.startsWith("furin/") &&
|
|
175
176
|
specifier !== "@teyik0/furin" &&
|
|
176
|
-
!specifier.startsWith("@teyik0/furin/")
|
|
177
|
+
!specifier.startsWith("@teyik0/furin/") &&
|
|
178
|
+
!isServerOnlyModule(resolved)
|
|
177
179
|
) {
|
|
178
180
|
resolved = `${resolved}?furin-server&t=${routeModuleSourceVersion(resolved)}`;
|
|
179
181
|
}
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
|
|
54
54
|
import { statSync } from "node:fs";
|
|
55
55
|
import { dirname, isAbsolute } from "node:path";
|
|
56
|
+
import { parseModule, removeRange } from "../plugin/module-transform.ts";
|
|
56
57
|
import { splitDevPage } from "../plugin/transform-dev-page.ts";
|
|
57
58
|
import { transformIsomorphicFunctions } from "../plugin/transform-isomorphic.ts";
|
|
58
59
|
import { invalidateDevLoaderCacheBySource } from "./cache/dev-loader.ts";
|
|
@@ -80,6 +81,7 @@ const T_PARAM_RE = /&t=(\d+)/;
|
|
|
80
81
|
const STRIP_FURIN_SERVER_RE = /\?furin-server.*$/;
|
|
81
82
|
const STRIP_T_PARAM_RE = /\?t=\d+$/;
|
|
82
83
|
const DELETED_DEV_PAGE_CONTENTS = "export const route = undefined;";
|
|
84
|
+
const CSS_IMPORT = /\.css(?:\?.*)?$/;
|
|
83
85
|
|
|
84
86
|
let _pluginRegistered = false;
|
|
85
87
|
const pluginSourceStamps = new Map<string, string>();
|
|
@@ -181,7 +183,22 @@ export function transformDevSource(
|
|
|
181
183
|
if (!options.renderFragment) {
|
|
182
184
|
recordDevImports(raw, filePath);
|
|
183
185
|
}
|
|
184
|
-
|
|
186
|
+
// CSS belongs to Bun's browser graph; importing it here also registers it with --hot.
|
|
187
|
+
const server = parseModule(raw, filePath);
|
|
188
|
+
for (const statement of server.program.body) {
|
|
189
|
+
if (
|
|
190
|
+
statement.type === "ImportDeclaration" &&
|
|
191
|
+
statement.specifiers.length === 0 &&
|
|
192
|
+
CSS_IMPORT.test(statement.source.value)
|
|
193
|
+
) {
|
|
194
|
+
removeRange(server, statement.start, statement.end);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const isomorphicSource = transformIsomorphicFunctions(
|
|
198
|
+
server.source.toString(),
|
|
199
|
+
filePath,
|
|
200
|
+
"server"
|
|
201
|
+
).code;
|
|
185
202
|
const split = options.renderFragment
|
|
186
203
|
? undefined
|
|
187
204
|
: splitDevPage(
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { Elysia, problem } from "elysia";
|
|
2
|
-
import type { DrainContext
|
|
2
|
+
import type { DrainContext } from "evlog";
|
|
3
3
|
import { readBoundedBytes } from "../bounded-body.ts";
|
|
4
4
|
import { instrumentationLoggerExclusions } from "../devtools/instrumentation.ts";
|
|
5
5
|
import { createFurinEvlog, type FurinEvlogOptions, setFurinEvlogOptions } from "../evlog.ts";
|
|
6
6
|
import type { FurinInstance } from "../instance.ts";
|
|
7
|
-
import { initializeFurinLogger } from "../logger.ts";
|
|
8
7
|
|
|
9
8
|
const MAX_BROWSER_INGEST_BYTES = 64 * 1024;
|
|
10
9
|
const MAX_BROWSER_INGEST_EVENTS = 100;
|
|
11
10
|
|
|
12
|
-
export type FurinLoggerOptions = FurinEvlogOptions
|
|
11
|
+
export type FurinLoggerOptions = FurinEvlogOptions;
|
|
13
12
|
|
|
14
13
|
const PROTOTYPE_POLLUTION_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
15
14
|
|
|
@@ -79,12 +78,7 @@ export function createLoggerPlugin(
|
|
|
79
78
|
logger: FurinLoggerOptions | undefined,
|
|
80
79
|
clientLogging: boolean
|
|
81
80
|
) {
|
|
82
|
-
const
|
|
83
|
-
initializeFurinLogger({
|
|
84
|
-
env: { service: "furin" },
|
|
85
|
-
...(sampling ? { sampling } : {}),
|
|
86
|
-
});
|
|
87
|
-
const options = createLoggerOptions(instance.prefix, instance.syncPath, elysiaLoggerOptions);
|
|
81
|
+
const options = createLoggerOptions(instance.prefix, instance.syncPath, logger ?? {});
|
|
88
82
|
setFurinEvlogOptions(instance, options);
|
|
89
83
|
const app = new Elysia().use(createFurinEvlog(options));
|
|
90
84
|
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type AnyElysia, Elysia } from "elysia";
|
|
2
|
+
|
|
3
|
+
/** Normalize entry-owned listening without opening a socket or running startup hooks. */
|
|
4
|
+
export async function loadServerEntry<Module extends object>(
|
|
5
|
+
load: () => Promise<Module>
|
|
6
|
+
): Promise<Module & { default: AnyElysia }> {
|
|
7
|
+
const { listen } = Elysia.prototype;
|
|
8
|
+
let capturing = true;
|
|
9
|
+
let captured: AnyElysia | undefined;
|
|
10
|
+
Elysia.prototype.listen = function (this: AnyElysia, ...args) {
|
|
11
|
+
// desktopApp() can retain this method in its own listener wrapper. Once
|
|
12
|
+
// import finishes, that retained method must delegate to the real listener.
|
|
13
|
+
if (!capturing) {
|
|
14
|
+
return listen.apply(this, args);
|
|
15
|
+
}
|
|
16
|
+
if (captured && captured !== this) {
|
|
17
|
+
throw new Error("[furin] Server entry must listen on only one Elysia app.");
|
|
18
|
+
}
|
|
19
|
+
captured = this;
|
|
20
|
+
const [options] = args;
|
|
21
|
+
const serve = { ...this["~config"]?.serve };
|
|
22
|
+
Object.assign(serve, typeof options === "object" ? options : { port: Number(options) });
|
|
23
|
+
this["~config"] = { ...this["~config"], serve };
|
|
24
|
+
return this;
|
|
25
|
+
};
|
|
26
|
+
try {
|
|
27
|
+
const module = await load();
|
|
28
|
+
const app = (module as { default?: AnyElysia }).default ?? captured;
|
|
29
|
+
if (!app || typeof app.listen !== "function") {
|
|
30
|
+
throw new Error(
|
|
31
|
+
"[furin] Server entry must export the Elysia app as default or call app.listen()."
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return { ...module, default: app };
|
|
35
|
+
} finally {
|
|
36
|
+
capturing = false;
|
|
37
|
+
Elysia.prototype.listen = listen;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -57,9 +57,12 @@ export interface SyncAdapter {
|
|
|
57
57
|
abortMutation: (lease: MutationLease) => Promise<void>;
|
|
58
58
|
/**
|
|
59
59
|
* Journals invalidations no mutation lease owns (`invalidateSync()`, `idempotent: false`
|
|
60
|
-
* routes) and returns their cursor.
|
|
60
|
+
* routes) and returns their cursor. Without explicit provenance, readers reset.
|
|
61
61
|
*/
|
|
62
|
-
appendInvalidations: (
|
|
62
|
+
appendInvalidations: (
|
|
63
|
+
invalidations: readonly SyncInvalidation[],
|
|
64
|
+
principal?: string
|
|
65
|
+
) => Promise<string>;
|
|
63
66
|
beginMutation: (input: BeginMutationInput) => Promise<BeginMutationResult>;
|
|
64
67
|
completeMutation: (input: CompleteMutationInput) => Promise<CompleteMutationResult>;
|
|
65
68
|
currentCursor: () => Promise<string>;
|
|
@@ -128,7 +131,8 @@ export type SyncMutation<Adapter> = <T>(
|
|
|
128
131
|
) => Promise<Awaited<T>>;
|
|
129
132
|
|
|
130
133
|
export interface SyncRuntimeOptions<Adapter extends SyncAdapter = SyncAdapter> {
|
|
131
|
-
|
|
134
|
+
/** Factories run once during runtime composition, never during AOT or on a request. */
|
|
135
|
+
adapter: Adapter | (() => Adapter);
|
|
132
136
|
notifier?: SyncNotifier;
|
|
133
137
|
/** Authenticate the request and return its verified identity, including for journal recovery. */
|
|
134
138
|
principal: (context: Context) => Promise<string> | string;
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
+
import type { Context } from "elysia";
|
|
1
2
|
import { currentInstance } from "../instance.ts";
|
|
2
3
|
import type { SyncRuntimeOptions } from "./adapter.ts";
|
|
3
4
|
|
|
4
5
|
const SYNC_DEFAULT_PATH = "/_furin/sync";
|
|
5
6
|
|
|
6
7
|
export interface FurinSyncOptions extends SyncRuntimeOptions {
|
|
8
|
+
/** Optional application-owned validity of an authenticated event connection. */
|
|
9
|
+
connection?: (
|
|
10
|
+
context: Context
|
|
11
|
+
) => SyncConnectionLease | undefined | Promise<SyncConnectionLease | undefined>;
|
|
7
12
|
path?: string;
|
|
8
13
|
}
|
|
9
14
|
|
|
15
|
+
export interface SyncConnectionLease {
|
|
16
|
+
release: () => void;
|
|
17
|
+
signal: AbortSignal;
|
|
18
|
+
}
|
|
19
|
+
|
|
10
20
|
export type FurinSyncOption = FurinSyncOptions | false;
|
|
11
21
|
|
|
12
22
|
export function resolveSyncPath(sync: FurinSyncOption | undefined): string | undefined {
|
|
@@ -25,10 +35,6 @@ export function getSyncPath(): string | undefined {
|
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
export function syncRuntimeOptions(sync: FurinSyncOptions): SyncRuntimeOptions {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
notifier: sync.notifier,
|
|
31
|
-
principal: sync.principal,
|
|
32
|
-
secret: sync.secret,
|
|
33
|
-
};
|
|
38
|
+
// Preserve the identity shared by mutation and transport plugins.
|
|
39
|
+
return sync;
|
|
34
40
|
}
|