Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ export function tracingPlugin(traceOpts?: TracingPluginOptions): H3Plugin {
} satisfies H3Route;
});

// Wrap route handlers returned by ~findRoute so frameworks that resolve
// routes at request time (e.g. nitro file-based routes) without pushing
// into ~routes still emit route traces. Use a getter/setter so later
// reassignment of ~findRoute remains wrapped.
let originalFindRoute = h3["~findRoute"];
const wrappedFindRoute = (event: H3Event) => {
const route = originalFindRoute.call(h3, event);
if (route?.data.handler) {
route.data.handler = wrapEventHandler(route.data.handler);
}
if (route?.data.middleware) {
route.data.middleware = route.data.middleware.map((m) => wrapMiddleware(m));
}
return route;
};
Object.defineProperty(h3, "~findRoute", {
configurable: true,
enumerable: true,
get: () => wrappedFindRoute,
set: (fn: typeof originalFindRoute) => {
originalFindRoute = fn;
Comment thread
logaretm marked this conversation as resolved.
Outdated
},
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

if ("on" in h3 && typeof h3.on === "function") {
const originalOn = h3.on;

Expand Down
46 changes: 46 additions & 0 deletions test/tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,4 +1156,50 @@ describe("tracing channels for H3Core instances", () => {
listener.cleanup();
}
});

it("traces handlers returned from a custom ~findRoute (nitro-style file routes)", async () => {
const listener = createTracingListener();
const { H3Core } = await import("../src/h3.ts");
const { tracingPlugin } = await import("../src/tracing.ts");
const { H3Event } = await import("../src/event.ts");

try {
const app = new H3Core();
const routeHandler = () => "file-based response";

// Simulate a framework that resolves routes at request time and never
// pushes them into app["~routes"]. The handler only exists inside the
// custom ~findRoute implementation.
app["~findRoute"] = (event: any) => {
if (event.url.pathname === "/file-route" && event.req.method === "GET") {
return {
data: {
method: "GET",
route: "/file-route",
handler: routeHandler,
},
params: {},
};
}
return undefined;
};

// Apply tracing plugin after ~findRoute is set (mirrors the order
// documented in the suggested fix).
tracingPlugin()(app as any);

const request = new Request("http://localhost/file-route", { method: "GET" });
const event = new H3Event(request, undefined, app as any);

await app.handler(event);

await new Promise((resolve) => setTimeout(resolve, 10));

const routeEvents = listener.events.filter((e) => e.asyncStart?.data.type === "route");

expect(routeEvents.length).toBeGreaterThan(0);
Comment thread
logaretm marked this conversation as resolved.
Outdated
} finally {
Comment thread
logaretm marked this conversation as resolved.
Outdated
listener.cleanup();
}
});
});
Loading