Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 12 additions & 8 deletions src/build/virtual/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export default function routing(nitro: Nitro) {
"_importHash"
);

const traceH3 = !!nitro.options.tracingChannel?.h3;

return /* js */ `
import * as __routeRules__ from "#nitro/runtime/route-rules";
import * as srvxNode from "srvx/node"
import * as h3 from "h3";
import * as h3 from "h3";${traceH3 ? `\nimport { wrapHandlerWithTracing } from "h3/tracing";` : ""}

export const findRouteRules = ${nitro.routing.routeRules.compileToString({ serialize: serializeRouteRule, matchAll: true })}

Expand All @@ -41,7 +43,7 @@ ${allHandlers
)
.join("\n")}

export const findRoute = ${nitro.routing.routes.compileToString({ serialize: serializeHandler })}
export const findRoute = ${nitro.routing.routes.compileToString({ serialize: (h) => serializeHandler(h, { tracing: traceH3 }) })}

export const findRoutedMiddleware = ${nitro.routing.routedMiddleware.compileToString({ serialize: serializeHandler, matchAll: true })};

Expand All @@ -61,18 +63,20 @@ function uniqueBy<T>(arr: T[], key: keyof T): T[] {

type MaybeArray<T> = T | T[];

function serializeHandler(h: MaybeArray<NitroEventHandler & { _importHash: string }>): string {
function serializeHandler(
h: MaybeArray<NitroEventHandler & { _importHash: string }>,
opts: { tracing?: boolean } = {}
): string {
const meta = Array.isArray(h) ? h[0] : h;
const handler = Array.isArray(h)
? `multiHandler(${h.map((handler) => serializeHandlerFn(handler)).join(",")})`
: serializeHandlerFn(h);

return `{${[
`route:${JSON.stringify(meta.route)}`,
meta.method && `method:${JSON.stringify(meta.method)}`,
meta.meta && `meta:${JSON.stringify(meta.meta)}`,
`handler:${
Array.isArray(h)
? `multiHandler(${h.map((handler) => serializeHandlerFn(handler)).join(",")})`
: serializeHandlerFn(h)
}`,
`handler:${opts.tracing ? `wrapHandlerWithTracing(${handler})` : handler}`,
]
.filter(Boolean)
.join(",")}}`;
Expand Down
56 changes: 56 additions & 0 deletions test/unit/virtual-routing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from "vitest";
import type { Nitro, NitroEventHandler } from "nitro/types";

import routing from "../../src/build/virtual/routing.ts";

function createNitroStub(tracingChannel: Nitro["options"]["tracingChannel"]): Nitro {
const handler: NitroEventHandler & { _importHash: string } = {
route: "/foo",
method: "GET",
handler: "/path/to/handler.ts",
_importHash: "_abc123",
};

return {
options: {
tracingChannel,
},
routing: {
routes: {
routes: [{ route: "/foo", method: "GET", data: handler }],
compileToString: ({ serialize }: { serialize: (h: unknown) => string }) =>
`{"/foo":${serialize(handler)}}`,
},
routedMiddleware: {
routes: [],
compileToString: () => `{}`,
},
globalMiddleware: [],
routeRules: {
compileToString: () => `() => []`,
},
},
} as unknown as Nitro;
}

describe("virtual/routing template", () => {
it("does not wrap route handlers when tracingChannel is disabled", () => {
const template = routing(createNitroStub(undefined)).template();
expect(template).not.toContain("h3/tracing");
expect(template).not.toContain("wrapHandlerWithTracing");
});

it("does not wrap route handlers when tracingChannel.h3 is false", () => {
const template = routing(
createNitroStub({ srvx: true, h3: false, unstorage: true })
).template();
expect(template).not.toContain("h3/tracing");
expect(template).not.toContain("wrapHandlerWithTracing");
});

it("wraps route handlers with wrapHandlerWithTracing when tracingChannel.h3 is true", () => {
const template = routing(createNitroStub({ srvx: true, h3: true, unstorage: true })).template();
expect(template).toContain(`import { wrapHandlerWithTracing } from "h3/tracing"`);
expect(template).toContain("wrapHandlerWithTracing(h3.toEventHandler(_abc123))");
});
});
Loading