@teyik0/furin 0.4.0-alpha.1 → 0.4.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/adapter/bun.d.ts +2 -18
- package/dist/adapter/package.d.ts +2 -2
- package/dist/adapter/runtime-build.d.ts +33 -0
- package/dist/adapter/vercel.d.ts +3 -0
- package/dist/build/client.d.ts +1 -1
- package/dist/build/compile-entry.d.ts +4 -3
- package/dist/build/compile-entry.ts +24 -69
- package/dist/build/entry-template.d.ts +4 -2
- package/dist/build/entry-template.ts +31 -8
- package/dist/build/index.d.ts +1 -1
- package/dist/build/index.js +8596 -6375
- package/dist/build/ssg-cache.d.ts +13 -0
- package/dist/build/types.d.ts +25 -2
- package/dist/build/virtual-entry.d.ts +8 -0
- package/dist/cache.d.ts +5 -0
- package/dist/cache.js +97 -0
- package/dist/cli/index.js +9099 -6771
- package/dist/cli/preview.d.ts +7 -0
- package/dist/client/boundaries.d.ts +1 -0
- package/dist/client/browser-events-runtime.d.ts +2 -0
- package/dist/client/browser-events.d.ts +2 -0
- package/dist/client/define-route.d.ts +9 -8
- package/dist/client/dev-error-overlay.d.ts +1 -0
- package/dist/client/dev-error-overlay.ts +201 -0
- package/dist/client/document.d.ts +1 -0
- package/dist/client/hmr.d.ts +4 -2
- package/dist/client/link.js +251 -82
- package/dist/client/router/provider.d.ts +1 -1
- package/dist/client/router/sync-catch-up.d.ts +1 -1
- package/dist/client/router/types.d.ts +8 -10
- package/dist/client.js +37 -23
- package/dist/config.d.ts +17 -0
- package/dist/config.js +9 -1
- package/dist/define-route.d.ts +11 -8
- package/dist/devtools/protocol.d.ts +2 -2
- package/dist/furin.d.ts +1 -1
- package/dist/furin.js +12991 -9491
- package/dist/plugin/index.js +476 -7
- package/dist/plugin/routes.d.ts +4 -3
- package/dist/rsc/build/index.d.ts +5 -1
- package/dist/server/browser-events/plugin.d.ts +11 -0
- package/dist/server/browser-events/types.d.ts +9 -0
- package/dist/server/cache/invalidation.d.ts +1 -2
- package/dist/server/cache/isr.d.ts +1 -0
- package/dist/server/cache/purger.d.ts +7 -0
- package/dist/server/cache/runtime-cache.d.ts +21 -0
- package/dist/server/dev/browser-events.d.ts +4 -0
- package/dist/server/dev/diagnostics.d.ts +39 -0
- package/dist/server/dev/error.d.ts +17 -0
- package/dist/server/dev/graph.d.ts +86 -0
- package/dist/server/dev/plugin.d.ts +9 -0
- package/dist/server/dev/request-security.d.ts +1 -0
- package/dist/server/dev/rewrite-module-specifiers.d.ts +7 -0
- package/dist/server/dev/runtime.production.d.ts +5 -0
- package/dist/server/dev/symbolicate.d.ts +11 -0
- package/dist/server/dev/transform-failure.d.ts +4 -0
- package/dist/server/dev-page-plugin.production.d.ts +1 -0
- package/dist/server/devtools/hub.d.ts +3 -2
- package/dist/server/devtools/instrumentation.d.ts +1 -1
- package/dist/server/devtools/instrumentation.production.d.ts +1 -1
- package/dist/server/devtools/plugin.d.ts +1 -1
- package/dist/server/external-prerender.d.ts +2 -0
- package/dist/server/instance.d.ts +2 -2
- package/dist/server/internal.d.ts +14 -2
- package/dist/server/internal.js +436 -1
- package/dist/server/render/element.d.ts +1 -0
- package/dist/server/render/index.d.ts +1 -1
- package/dist/server/render/index.ts +1 -1
- package/dist/server/render/ppr-document.d.ts +21 -0
- package/dist/server/render/ppr-request.d.ts +26 -0
- package/dist/server/render/ppr-route.d.ts +3 -1
- package/dist/server/render/shell.ts +0 -12
- package/dist/server/render/ssg.d.ts +2 -0
- package/dist/server/render/ssr.d.ts +17 -6
- package/dist/server/router/hmr.production.d.ts +2 -0
- package/dist/server/router/patterns.d.ts +1 -0
- package/dist/server/router/plugin.d.ts +3 -1
- package/dist/server/sync/adapter.d.ts +13 -0
- package/dist/server/sync/config.d.ts +5 -5
- package/dist/server/sync/index.d.ts +6 -6
- package/dist/server/sync/index.js +491 -516
- package/dist/server/sync/notifier.d.ts +1 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +101 -6
- package/dist/server/sync/stream.d.ts +4 -13
- package/dist/shared/await.d.ts +3 -3
- package/dist/shared/browser-events.d.ts +22 -0
- package/dist/shared/compact-json.d.ts +8 -0
- package/dist/shared/deferred-ndjson.d.ts +6 -5
- package/dist/shared/dev-diagnostics.d.ts +36 -0
- package/dist/shared/prefix.d.ts +2 -0
- package/package.json +16 -7
- package/src/adapter/README.md +45 -0
- package/src/adapter/bun.ts +45 -268
- package/src/adapter/package.ts +15 -9
- package/src/adapter/runtime-build.ts +375 -0
- package/src/adapter/static.ts +79 -2
- package/src/adapter/vercel.ts +857 -0
- package/src/build/client.ts +38 -13
- package/src/build/compile-entry.ts +24 -69
- package/src/build/entry-template.ts +31 -8
- package/src/build/hydrate.ts +60 -20
- package/src/build/index.ts +19 -5
- package/src/build/production-instrumentation.ts +56 -4
- package/src/build/shared.ts +2 -1
- package/src/build/ssg-cache.ts +97 -11
- package/src/build/types.ts +31 -2
- package/src/build/virtual-entry.ts +27 -0
- package/src/cache.ts +14 -0
- package/src/cli/index.ts +55 -3
- package/src/cli/preview.ts +90 -0
- package/src/client/boundaries.tsx +10 -1
- package/src/client/browser-events-runtime.ts +204 -0
- package/src/client/browser-events.ts +22 -0
- package/src/client/define-route.ts +7 -17
- package/src/client/dev-error-overlay.ts +201 -0
- package/src/client/document.tsx +4 -7
- package/src/client/hmr.ts +26 -3
- package/src/client/router/boundary-tree.tsx +4 -4
- package/src/client/router/provider.tsx +165 -63
- package/src/client/router/sync-catch-up.ts +3 -4
- package/src/client/router/types.ts +6 -6
- package/src/config.ts +25 -0
- package/src/define-route.ts +20 -7
- package/src/devtools/devtools-element.js +56 -77
- package/src/devtools/protocol.ts +2 -2
- package/src/furin.ts +157 -77
- package/src/plugin/routes.ts +206 -36
- package/src/plugin/transform-client.ts +685 -3
- package/src/react-dom-edge.d.ts +6 -0
- package/src/rsc/build/index.ts +15 -3
- package/src/server/auto-invalidate/runtime.ts +5 -2
- package/src/server/browser-events/plugin.ts +168 -0
- package/src/server/browser-events/types.ts +11 -0
- package/src/server/cache/dev-loader.ts +30 -29
- package/src/server/cache/invalidation.ts +13 -22
- package/src/server/cache/isr.ts +9 -0
- package/src/server/cache/purger.ts +41 -0
- package/src/server/cache/runtime-cache.ts +137 -0
- package/src/server/dev/browser-events.ts +54 -0
- package/src/server/dev/diagnostics.ts +297 -0
- package/src/server/dev/error.ts +148 -0
- package/src/server/dev/graph.ts +438 -0
- package/src/server/dev/plugin.ts +130 -0
- package/src/server/dev/request-security.ts +38 -0
- package/src/server/dev/rewrite-module-specifiers.ts +81 -0
- package/src/server/dev/runtime.production.ts +24 -0
- package/src/server/dev/symbolicate.ts +135 -0
- package/src/server/dev/transform-failure.ts +8 -0
- package/src/server/dev-page-plugin.production.ts +3 -0
- package/src/server/dev-page-plugin.ts +92 -53
- package/src/server/devtools/hub.ts +6 -5
- package/src/server/devtools/instrumentation.production.ts +1 -1
- package/src/server/devtools/instrumentation.ts +12 -6
- package/src/server/devtools/plugin.ts +7 -150
- package/src/server/external-prerender.ts +10 -0
- package/src/server/instance.ts +3 -3
- package/src/server/internal.ts +39 -2
- package/src/server/render/assemble.ts +4 -4
- package/src/server/render/document.tsx +13 -4
- package/src/server/render/element.tsx +18 -14
- package/src/server/render/index.ts +1 -1
- package/src/server/render/isr.ts +44 -19
- package/src/server/render/loaders.ts +13 -3
- package/src/server/render/not-found.ts +28 -4
- package/src/server/render/ppr-document.ts +239 -0
- package/src/server/render/ppr-request.ts +104 -0
- package/src/server/render/ppr-route.ts +152 -31
- package/src/server/render/shell.ts +0 -12
- package/src/server/render/ssg.ts +38 -20
- package/src/server/render/ssr.ts +211 -83
- package/src/server/render/template.ts +47 -11
- package/src/server/router/defined-route.ts +7 -39
- package/src/server/router/hmr.production.ts +7 -0
- package/src/server/router/hmr.ts +106 -67
- package/src/server/router/patterns.ts +5 -1
- package/src/server/router/plugin.ts +124 -35
- package/src/server/router/source-version.ts +3 -10
- package/src/server/sync/adapter.ts +13 -0
- package/src/server/sync/config.ts +12 -12
- package/src/server/sync/index.ts +8 -8
- package/src/server/sync/notifier.ts +1 -0
- package/src/server/sync/plugin.ts +4 -1
- package/src/server/sync/postgres/index.ts +111 -5
- package/src/server/sync/runtime.ts +12 -0
- package/src/server/sync/stream.ts +78 -113
- package/src/shared/await.tsx +3 -3
- package/src/shared/browser-events.ts +28 -0
- package/src/shared/compact-json.ts +77 -0
- package/src/shared/deferred-ndjson.ts +20 -6
- package/src/shared/dev-diagnostics.ts +44 -0
- package/src/shared/prefix.ts +8 -0
- package/src/shared/route-map.ts +10 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BROWSER_EVENT_PROTOCOL_VERSION } from "../../shared/browser-events.ts";
|
|
2
|
+
import type { BrowserEventSource } from "../browser-events/types.ts";
|
|
3
|
+
import { subscribeDevtoolsEventsAfter } from "../devtools/hub.ts";
|
|
4
|
+
import type { FurinInstance } from "../instance.ts";
|
|
5
|
+
import type { DevDiagnosticStore } from "./diagnostics.ts";
|
|
6
|
+
|
|
7
|
+
export function createDevelopmentBrowserEventSources(
|
|
8
|
+
instance: FurinInstance,
|
|
9
|
+
diagnostics: DevDiagnosticStore
|
|
10
|
+
): readonly BrowserEventSource[] {
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
subscribe(listener) {
|
|
14
|
+
const subscription = diagnostics.subscribe(0, undefined, (event) =>
|
|
15
|
+
listener({
|
|
16
|
+
channel: "diagnostic",
|
|
17
|
+
data: event,
|
|
18
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
for (const event of subscription.replay) {
|
|
22
|
+
listener({
|
|
23
|
+
channel: "diagnostic",
|
|
24
|
+
data: event,
|
|
25
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return { unsubscribe: subscription.unsubscribe };
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
subscribe(listener) {
|
|
33
|
+
const subscription = subscribeDevtoolsEventsAfter(
|
|
34
|
+
0,
|
|
35
|
+
(event) =>
|
|
36
|
+
listener({
|
|
37
|
+
channel: "devtools",
|
|
38
|
+
data: event,
|
|
39
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
40
|
+
}),
|
|
41
|
+
instance
|
|
42
|
+
);
|
|
43
|
+
for (const event of subscription.replay) {
|
|
44
|
+
listener({
|
|
45
|
+
channel: "devtools",
|
|
46
|
+
data: event,
|
|
47
|
+
version: BROWSER_EVENT_PROTOCOL_VERSION,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return { unsubscribe: subscription.unsubscribe };
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, relative } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import {
|
|
6
|
+
DEV_DIAGNOSTIC_PROTOCOL_VERSION,
|
|
7
|
+
type DevDiagnostic,
|
|
8
|
+
type DevDiagnosticEvent,
|
|
9
|
+
type DevDiagnosticPhase,
|
|
10
|
+
type DevSourceLocation,
|
|
11
|
+
} from "../../shared/dev-diagnostics.ts";
|
|
12
|
+
import { type FurinInstance, instanceSlot } from "../instance.ts";
|
|
13
|
+
import { buildRouteMatcher } from "../router/patterns.ts";
|
|
14
|
+
import { devGraph } from "./graph.ts";
|
|
15
|
+
import { symbolicateStack } from "./symbolicate.ts";
|
|
16
|
+
|
|
17
|
+
interface DiagnosticContext {
|
|
18
|
+
entryPath: string;
|
|
19
|
+
importChain: readonly string[] | undefined;
|
|
20
|
+
phase: DevDiagnosticPhase;
|
|
21
|
+
route: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ClientErrorReport {
|
|
25
|
+
cause: string | undefined;
|
|
26
|
+
message: string;
|
|
27
|
+
phase: "client-render" | "hydrate";
|
|
28
|
+
route: string;
|
|
29
|
+
stack: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const STACK_POSITION_RE = /(?:^|\s)\(?((?:file:\/\/|furin-dev-page:)?\S+):(\d+):(\d+)\)?$/;
|
|
33
|
+
const DEV_PAGE_PREFIX_RE = /^furin-dev-page:/;
|
|
34
|
+
const QUERY_RE = /\?.*$/;
|
|
35
|
+
const EVENT_LIMIT = 100;
|
|
36
|
+
|
|
37
|
+
function messageOf(error: unknown): string {
|
|
38
|
+
return error instanceof Error ? error.message : String(error);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function causeOf(error: unknown): string | undefined {
|
|
42
|
+
if (!(error instanceof Error) || error.cause === undefined) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
return messageOf(error.cause);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function displayPath(path: string): string {
|
|
49
|
+
const projected = relative(process.cwd(), path).replaceAll("\\", "/");
|
|
50
|
+
return projected.startsWith("../") ? path : projected;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function sourceFilePath(file: string): string {
|
|
54
|
+
if (!file.startsWith("file://")) {
|
|
55
|
+
return file;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
return fileURLToPath(file);
|
|
59
|
+
} catch {
|
|
60
|
+
return file;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function canonicalSourcePath(path: string): string {
|
|
65
|
+
try {
|
|
66
|
+
return realpathSync(path).replaceAll("\\", "/");
|
|
67
|
+
} catch {
|
|
68
|
+
return path.replaceAll("\\", "/");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function logicalDiagnosticRoute(route: string, instance: FurinInstance | undefined): string {
|
|
73
|
+
const prefix = instance?.prefix;
|
|
74
|
+
if (
|
|
75
|
+
prefix &&
|
|
76
|
+
route.startsWith(prefix) &&
|
|
77
|
+
(route.length === prefix.length || route[prefix.length] === "/")
|
|
78
|
+
) {
|
|
79
|
+
return route.slice(prefix.length) || "/";
|
|
80
|
+
}
|
|
81
|
+
return route;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function stackLocation(
|
|
85
|
+
stack: string | undefined,
|
|
86
|
+
entryPath: string
|
|
87
|
+
): DevSourceLocation | undefined {
|
|
88
|
+
if (!stack) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const locations: DevSourceLocation[] = [];
|
|
92
|
+
for (const line of stack.split("\n")) {
|
|
93
|
+
const match = STACK_POSITION_RE.exec(line.trim());
|
|
94
|
+
if (!(match?.[1] && match[2] && match[3])) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const file = sourceFilePath(match[1].replace(DEV_PAGE_PREFIX_RE, "").replace(QUERY_RE, ""));
|
|
98
|
+
locations.push({
|
|
99
|
+
column: Number.parseInt(match[3], 10),
|
|
100
|
+
file,
|
|
101
|
+
line: Number.parseInt(match[2], 10),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return locations.find((location) => location.file === entryPath) ?? locations[0];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function explicitLocation(error: unknown, entryPath: string): DevSourceLocation | undefined {
|
|
108
|
+
if (typeof error !== "object" || error === null) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const candidate = error as {
|
|
112
|
+
furinPosition?: { column?: unknown; file?: unknown; line?: unknown };
|
|
113
|
+
position?: { column?: unknown; file?: unknown; line?: unknown };
|
|
114
|
+
};
|
|
115
|
+
const position = candidate.furinPosition ?? candidate.position;
|
|
116
|
+
if (!position || typeof position.column !== "number" || typeof position.line !== "number") {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
column: position.column,
|
|
121
|
+
file:
|
|
122
|
+
typeof position.file === "string" &&
|
|
123
|
+
(isAbsolute(position.file) || position.file.startsWith("file://"))
|
|
124
|
+
? sourceFilePath(position.file)
|
|
125
|
+
: entryPath,
|
|
126
|
+
line: position.line,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function createDevDiagnostic(error: unknown, context: DiagnosticContext): DevDiagnostic {
|
|
131
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
132
|
+
const sourceLocation =
|
|
133
|
+
explicitLocation(error, context.entryPath) ?? stackLocation(stack, context.entryPath);
|
|
134
|
+
const location = sourceLocation
|
|
135
|
+
? { ...sourceLocation, file: displayPath(sourceLocation.file) }
|
|
136
|
+
: undefined;
|
|
137
|
+
const importChain =
|
|
138
|
+
context.importChain ??
|
|
139
|
+
devGraph(undefined).importChain(context.entryPath, sourceLocation?.file ?? context.entryPath);
|
|
140
|
+
return {
|
|
141
|
+
cause: causeOf(error),
|
|
142
|
+
frames: [],
|
|
143
|
+
importChain: importChain.map(displayPath),
|
|
144
|
+
location,
|
|
145
|
+
message: messageOf(error),
|
|
146
|
+
phase: context.phase,
|
|
147
|
+
route: context.route,
|
|
148
|
+
stack,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export class DevDiagnosticStore {
|
|
153
|
+
readonly #events: DevDiagnosticEvent[] = [];
|
|
154
|
+
readonly #listeners = new Set<(event: DevDiagnosticEvent) => void>();
|
|
155
|
+
readonly #serverId = randomUUID();
|
|
156
|
+
#activeError: Extract<DevDiagnosticEvent, { type: "error" }> | undefined;
|
|
157
|
+
#eventId = 0;
|
|
158
|
+
#revision = 0;
|
|
159
|
+
|
|
160
|
+
constructor() {
|
|
161
|
+
this.#publish({ type: "ready" });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
get revision(): number {
|
|
165
|
+
return this.#revision;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
markReady(route: string): DevDiagnosticEvent | undefined {
|
|
169
|
+
if (!this.#activeError || this.#activeError.diagnostic.route !== route) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
this.#activeError = undefined;
|
|
173
|
+
this.#revision += 1;
|
|
174
|
+
return this.#publish({ type: "ready" });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
publish(diagnostic: DevDiagnostic): Extract<DevDiagnosticEvent, { type: "error" }> {
|
|
178
|
+
if (
|
|
179
|
+
this.#activeError &&
|
|
180
|
+
JSON.stringify(this.#activeError.diagnostic) === JSON.stringify(diagnostic)
|
|
181
|
+
) {
|
|
182
|
+
return this.#activeError;
|
|
183
|
+
}
|
|
184
|
+
const event = this.#publish({ diagnostic, type: "error" }) as Extract<
|
|
185
|
+
DevDiagnosticEvent,
|
|
186
|
+
{ type: "error" }
|
|
187
|
+
>;
|
|
188
|
+
this.#activeError = event;
|
|
189
|
+
return event;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
subscribe(
|
|
193
|
+
after: number,
|
|
194
|
+
serverId: string | undefined,
|
|
195
|
+
listener: (event: DevDiagnosticEvent) => void
|
|
196
|
+
): { replay: readonly DevDiagnosticEvent[]; unsubscribe: () => void } {
|
|
197
|
+
this.#listeners.add(listener);
|
|
198
|
+
let replay: readonly DevDiagnosticEvent[] = [];
|
|
199
|
+
if (serverId === this.#serverId) {
|
|
200
|
+
replay = this.#events.filter((event) => event.id > after);
|
|
201
|
+
} else {
|
|
202
|
+
const latest = this.#activeError ?? this.#events.at(-1);
|
|
203
|
+
replay = latest ? [latest] : [];
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
replay,
|
|
207
|
+
unsubscribe: () => {
|
|
208
|
+
this.#listeners.delete(listener);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#publish(
|
|
214
|
+
event: { diagnostic: DevDiagnostic; type: "error" } | { type: "ready" }
|
|
215
|
+
): DevDiagnosticEvent {
|
|
216
|
+
this.#eventId += 1;
|
|
217
|
+
const complete = {
|
|
218
|
+
...event,
|
|
219
|
+
id: this.#eventId,
|
|
220
|
+
revision: this.#revision,
|
|
221
|
+
serverId: this.#serverId,
|
|
222
|
+
version: DEV_DIAGNOSTIC_PROTOCOL_VERSION,
|
|
223
|
+
} as DevDiagnosticEvent;
|
|
224
|
+
this.#events.push(complete);
|
|
225
|
+
if (this.#events.length > EVENT_LIMIT) {
|
|
226
|
+
this.#events.shift();
|
|
227
|
+
}
|
|
228
|
+
for (const listener of this.#listeners) {
|
|
229
|
+
listener(complete);
|
|
230
|
+
}
|
|
231
|
+
return complete;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const diagnosticStoreSlot = instanceSlot(() => new DevDiagnosticStore());
|
|
236
|
+
|
|
237
|
+
export function devDiagnosticStore(instance?: FurinInstance): DevDiagnosticStore {
|
|
238
|
+
return diagnosticStoreSlot(instance);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function publishDevDiagnostic(
|
|
242
|
+
error: unknown,
|
|
243
|
+
context: Omit<DiagnosticContext, "importChain"> & { importChain?: readonly string[] }
|
|
244
|
+
): Extract<DevDiagnosticEvent, { type: "error" }> {
|
|
245
|
+
const diagnostic = createDevDiagnostic(error, {
|
|
246
|
+
...context,
|
|
247
|
+
importChain: context.importChain,
|
|
248
|
+
});
|
|
249
|
+
return devDiagnosticStore().publish(diagnostic);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export async function publishClientDiagnostic(
|
|
253
|
+
store: DevDiagnosticStore,
|
|
254
|
+
report: ClientErrorReport,
|
|
255
|
+
origin: string,
|
|
256
|
+
instance: FurinInstance | undefined
|
|
257
|
+
): Promise<Extract<DevDiagnosticEvent, { type: "error" }>> {
|
|
258
|
+
const symbolicated = await symbolicateStack({ origin, stack: report.stack });
|
|
259
|
+
const location = symbolicated.location
|
|
260
|
+
? {
|
|
261
|
+
...symbolicated.location,
|
|
262
|
+
file: displayPath(sourceFilePath(symbolicated.location.file)),
|
|
263
|
+
}
|
|
264
|
+
: undefined;
|
|
265
|
+
const graph = devGraph(instance);
|
|
266
|
+
const { snapshot } = graph;
|
|
267
|
+
const logicalRoute = logicalDiagnosticRoute(report.route, instance);
|
|
268
|
+
const routeMatch = snapshot ? buildRouteMatcher(snapshot.routes)(logicalRoute) : null;
|
|
269
|
+
const routeEntry = routeMatch?.route.path;
|
|
270
|
+
const sourceFile = symbolicated.location
|
|
271
|
+
? canonicalSourcePath(sourceFilePath(symbolicated.location.file))
|
|
272
|
+
: routeEntry;
|
|
273
|
+
let importChain: readonly string[] = [];
|
|
274
|
+
if (routeEntry && sourceFile) {
|
|
275
|
+
importChain = graph.importChain(canonicalSourcePath(routeEntry), sourceFile).map(displayPath);
|
|
276
|
+
} else if (sourceFile) {
|
|
277
|
+
importChain = [displayPath(sourceFile)];
|
|
278
|
+
}
|
|
279
|
+
let displayedImportChain: readonly string[] = [];
|
|
280
|
+
if (importChain.length > 1) {
|
|
281
|
+
displayedImportChain = importChain;
|
|
282
|
+
} else if (routeEntry && location) {
|
|
283
|
+
displayedImportChain = [displayPath(routeEntry), location.file];
|
|
284
|
+
} else if (location) {
|
|
285
|
+
displayedImportChain = [location.file];
|
|
286
|
+
}
|
|
287
|
+
return store.publish({
|
|
288
|
+
cause: report.cause,
|
|
289
|
+
frames: symbolicated.frames,
|
|
290
|
+
importChain: displayedImportChain,
|
|
291
|
+
location,
|
|
292
|
+
message: report.message,
|
|
293
|
+
phase: report.phase,
|
|
294
|
+
route: routeMatch?.route.pattern ?? logicalRoute,
|
|
295
|
+
stack: report.stack,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import type { DevErrorPayload, DevErrorPhase, DevGraph, DevGraphEvent } from "./graph.ts";
|
|
4
|
+
|
|
5
|
+
interface DevErrorContext {
|
|
6
|
+
entryPath: string;
|
|
7
|
+
importChain: string[];
|
|
8
|
+
phase: DevErrorPhase;
|
|
9
|
+
route: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface PublishDevErrorContext {
|
|
13
|
+
entryPath: string;
|
|
14
|
+
phase: DevErrorPhase;
|
|
15
|
+
route: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface SourcePosition {
|
|
19
|
+
column: number;
|
|
20
|
+
file: string;
|
|
21
|
+
line: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const STACK_POSITION_RE = /(?:^|\s)\(?((?:file:\/\/)?\S+):(\d+):(\d+)\)?$/;
|
|
25
|
+
const DEV_PAGE_PREFIX_RE = /^furin-dev-page:/;
|
|
26
|
+
const QUERY_RE = /\?.*$/;
|
|
27
|
+
|
|
28
|
+
function errorMessage(error: unknown): string {
|
|
29
|
+
if (error instanceof Error) {
|
|
30
|
+
return error.message;
|
|
31
|
+
}
|
|
32
|
+
return String(error);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function errorCause(error: unknown): string | null {
|
|
36
|
+
if (!(error instanceof Error) || error.cause === undefined) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return errorMessage(error.cause);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function stackPosition(stack: string | undefined, entryPath: string): SourcePosition | null {
|
|
43
|
+
if (!stack) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const positions: SourcePosition[] = [];
|
|
47
|
+
for (const line of stack.split("\n")) {
|
|
48
|
+
const match = STACK_POSITION_RE.exec(line.trim());
|
|
49
|
+
if (!(match?.[1] && match[2] && match[3])) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
let file = match[1].replace(DEV_PAGE_PREFIX_RE, "").replace(QUERY_RE, "");
|
|
53
|
+
if (file.startsWith("file://")) {
|
|
54
|
+
try {
|
|
55
|
+
file = fileURLToPath(file);
|
|
56
|
+
} catch {
|
|
57
|
+
// Keep the URL when it cannot be projected to a local file.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
positions.push({
|
|
61
|
+
column: Number.parseInt(match[3], 10),
|
|
62
|
+
file,
|
|
63
|
+
line: Number.parseInt(match[2], 10),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return positions.find((position) => position.file === entryPath) ?? positions[0] ?? null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function buildPosition(error: unknown, entryPath: string): SourcePosition | null {
|
|
70
|
+
if (typeof error !== "object" || error === null) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const { furinPosition, position } = error as {
|
|
74
|
+
furinPosition?: { column?: unknown; file?: unknown; line?: unknown };
|
|
75
|
+
position?: { column?: unknown; file?: unknown; line?: unknown };
|
|
76
|
+
};
|
|
77
|
+
if (
|
|
78
|
+
furinPosition &&
|
|
79
|
+
typeof furinPosition.file === "string" &&
|
|
80
|
+
typeof furinPosition.line === "number" &&
|
|
81
|
+
typeof furinPosition.column === "number"
|
|
82
|
+
) {
|
|
83
|
+
return {
|
|
84
|
+
column: furinPosition.column,
|
|
85
|
+
file: furinPosition.file,
|
|
86
|
+
line: furinPosition.line,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (!position || typeof position.line !== "number" || typeof position.column !== "number") {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const reportedFile = typeof position.file === "string" ? position.file : entryPath;
|
|
93
|
+
return {
|
|
94
|
+
column: position.column,
|
|
95
|
+
file: reportedFile === "input.tsx" || reportedFile === "input.ts" ? entryPath : reportedFile,
|
|
96
|
+
line: position.line,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function createDevErrorPayload(error: unknown, context: DevErrorContext): DevErrorPayload {
|
|
101
|
+
const stack = error instanceof Error ? (error.stack ?? null) : null;
|
|
102
|
+
const position =
|
|
103
|
+
buildPosition(error, context.entryPath) ?? stackPosition(stack ?? undefined, context.entryPath);
|
|
104
|
+
return {
|
|
105
|
+
cause: errorCause(error),
|
|
106
|
+
column: position?.column ?? null,
|
|
107
|
+
file: position?.file ?? context.entryPath,
|
|
108
|
+
importChain: context.importChain,
|
|
109
|
+
line: position?.line ?? null,
|
|
110
|
+
message: errorMessage(error),
|
|
111
|
+
phase: context.phase,
|
|
112
|
+
route: context.route,
|
|
113
|
+
stack,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function displaySourcePath(path: string): string {
|
|
118
|
+
const projected = relative(process.cwd(), path).replaceAll("\\", "/");
|
|
119
|
+
return projected.startsWith("../") ? path : projected;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function publishDevError<Snapshot>(
|
|
123
|
+
graph: DevGraph<Snapshot>,
|
|
124
|
+
error: unknown,
|
|
125
|
+
context: PublishDevErrorContext
|
|
126
|
+
): Extract<DevGraphEvent, { type: "error" }> {
|
|
127
|
+
const payload = createDevErrorPayload(error, {
|
|
128
|
+
...context,
|
|
129
|
+
importChain: [context.entryPath],
|
|
130
|
+
});
|
|
131
|
+
const sourceError =
|
|
132
|
+
context.phase === "import" || context.phase === "transform"
|
|
133
|
+
? (graph.diagnoseTransformError(context.entryPath, payload.message) ??
|
|
134
|
+
graph.sourceError(payload.message, context.entryPath))
|
|
135
|
+
: undefined;
|
|
136
|
+
if (sourceError) {
|
|
137
|
+
payload.column = sourceError.column;
|
|
138
|
+
payload.file = sourceError.file;
|
|
139
|
+
payload.line = sourceError.line;
|
|
140
|
+
payload.phase = "transform";
|
|
141
|
+
}
|
|
142
|
+
const sourceFile = payload.file ?? context.entryPath;
|
|
143
|
+
payload.file = displaySourcePath(sourceFile);
|
|
144
|
+
payload.importChain = graph
|
|
145
|
+
.importChain(context.entryPath, sourceFile)
|
|
146
|
+
.map((path) => displaySourcePath(path));
|
|
147
|
+
return graph.publishError(payload);
|
|
148
|
+
}
|