Skip to content

Commit c9c5081

Browse files
committed
ref: remove init and mount channels as they aren't that useful
1 parent b60af42 commit c9c5081

2 files changed

Lines changed: 10 additions & 162 deletions

File tree

src/tracing.ts

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { H3Event } from "./event.ts";
2-
import type { H3Core } from "./h3.ts";
32
import {
43
definePlugin,
54
type H3Plugin,
@@ -8,18 +7,8 @@ import {
87
} from "./types/h3.ts";
98
import type { EventHandler, Middleware } from "./types/handler.ts";
109

11-
export interface H3InitPayload {
12-
app: H3Core;
13-
}
14-
1510
export type HandlerType = "middleware" | "route";
1611

17-
export interface H3MountPayload {
18-
app: H3Core;
19-
base: string;
20-
mountedApp: unknown;
21-
}
22-
2312
export interface H3THandlerTracePayload {
2413
event: H3Event;
2514
route?: string;
@@ -39,39 +28,9 @@ export interface TracingPluginOptions {
3928
*/
4029
export const tracingPlugin = (traceOpts?: TracingPluginOptions): H3Plugin => {
4130
return definePlugin((h3) => {
42-
const { tracingChannel, channel } =
31+
const { tracingChannel } =
4332
globalThis.process.getBuiltinModule?.("diagnostics_channel") ?? {};
4433

45-
// Skip if diagnostics_channel is not available
46-
// if channel isn't available, then tracingChannel is also not available since its built on top of it
47-
if (!channel) {
48-
return;
49-
}
50-
51-
const initChannel = channel("h3.init");
52-
const mountChannel = channel("h3.mount");
53-
54-
// Store original mount before overriding
55-
const originalMount = h3.mount;
56-
57-
// Override the mount method to publish the mount event when a nested app is mounted.
58-
h3.mount = (base, input) => {
59-
if (mountChannel.hasSubscribers) {
60-
mountChannel.publish({
61-
app: h3,
62-
base,
63-
mountedApp: input,
64-
} satisfies H3MountPayload);
65-
}
66-
67-
return originalMount.call(h3, base, input);
68-
};
69-
70-
// Publish the init event ASAP
71-
if (initChannel.hasSubscribers) {
72-
initChannel.publish({ app: h3 } satisfies H3InitPayload);
73-
}
74-
7534
// If tracingChannel is not available, then we can't trace request handlers
7635
if (!tracingChannel) {
7736
return;
@@ -169,6 +128,8 @@ export const tracingPlugin = (traceOpts?: TracingPluginOptions): H3Plugin => {
169128
return originalUse.call(h3, wrapMiddleware(fn), opts);
170129
};
171130

131+
// TODO: Trace mount
132+
172133
return h3;
173134
})();
174135
};

test/tracing.test.ts

Lines changed: 7 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import { channel, tracingChannel } from "node:diagnostics_channel";
2-
import { describeMatrix } from "./_setup.ts";
3-
import { H3 } from "../src/h3.ts";
4-
import {
5-
type H3InitPayload,
6-
type H3MountPayload,
7-
type H3THandlerTracePayload,
8-
tracingPlugin,
9-
} from "../src/tracing.ts";
1+
import { tracingChannel } from "node:diagnostics_channel";
2+
import { describeMatrix, type TestOptions } from "./_setup.ts";
3+
import { type H3THandlerTracePayload } from "../src/tracing.ts";
104

115
type TracingEvent = {
126
start?: { data: H3THandlerTracePayload };
@@ -16,14 +10,6 @@ type TracingEvent = {
1610
error?: { data: H3THandlerTracePayload; error: Error };
1711
};
1812

19-
function createH3WithTracing() {
20-
const app = new H3({
21-
plugins: [tracingPlugin()],
22-
});
23-
24-
return app;
25-
}
26-
2713
function createTracingListener() {
2814
const events: TracingEvent[] = [];
2915

@@ -69,111 +55,12 @@ function createTracingListener() {
6955
};
7056
}
7157

58+
// Matrix is configured with tracing plugin enabled
59+
const testOpts: TestOptions = { tracing: true };
60+
7261
describeMatrix(
7362
"tracing channels",
7463
(t, { it, expect }) => {
75-
it("h3.init channel fires when app is initialized", async () => {
76-
const events: H3InitPayload[] = [];
77-
const initChannel = channel("h3.init");
78-
79-
const handler = (message: unknown) => {
80-
events.push(message as H3InitPayload);
81-
};
82-
initChannel.subscribe(handler);
83-
84-
try {
85-
const app = createH3WithTracing();
86-
expect(events).toHaveLength(1);
87-
expect(events[0].app).toBe(app);
88-
} finally {
89-
initChannel.unsubscribe(handler);
90-
}
91-
});
92-
93-
it("h3.init listener can configure global error handler", async () => {
94-
const initChannel = channel("h3.init");
95-
96-
const customError = (error: any) => {
97-
return new Response(
98-
JSON.stringify({ custom: true, message: error.message }),
99-
{
100-
status: error.status || 500,
101-
headers: { "content-type": "application/json" },
102-
},
103-
);
104-
};
105-
106-
const handler = (message: unknown) => {
107-
const { app } = message as H3InitPayload;
108-
app.config.onError = customError;
109-
};
110-
initChannel.subscribe(handler);
111-
112-
try {
113-
const app = createH3WithTracing();
114-
app.get("/error", () => {
115-
throw new Error("Test error");
116-
});
117-
118-
const res = await app.request("/error");
119-
expect(res.status).toBe(500);
120-
const body = await res.json();
121-
expect(body.custom).toBe(true);
122-
expect(body.message).toBe("Test error");
123-
} finally {
124-
initChannel.unsubscribe(handler);
125-
}
126-
});
127-
128-
it("h3.mount channel fires when nested app is mounted", async () => {
129-
const events: H3MountPayload[] = [];
130-
const mountChannel = channel("h3.mount");
131-
132-
const handler = (message: unknown) => {
133-
events.push(message as H3MountPayload);
134-
};
135-
mountChannel.subscribe(handler);
136-
137-
try {
138-
const nestedApp = createH3WithTracing();
139-
nestedApp.get("/test", () => "nested");
140-
141-
t.app.mount("/api", nestedApp);
142-
143-
expect(events).toHaveLength(1);
144-
expect(events[0].app).toBe(t.app);
145-
expect(events[0].base).toBe("/api");
146-
expect(events[0].mountedApp).toBe(nestedApp);
147-
} finally {
148-
mountChannel.unsubscribe(handler);
149-
}
150-
});
151-
152-
it("h3.mount channel fires for fetchable objects", async () => {
153-
const events: H3MountPayload[] = [];
154-
const mountChannel = channel("h3.mount");
155-
156-
const handler = (message: unknown) => {
157-
events.push(message as H3MountPayload);
158-
};
159-
mountChannel.subscribe(handler);
160-
161-
try {
162-
const fetchHandler = {
163-
fetch: () => new Response("fetchable"),
164-
};
165-
166-
t.app.mount("/fetch", fetchHandler);
167-
168-
expect(events).toHaveLength(1);
169-
expect(events[0].app).toBe(t.app);
170-
expect(events[0].base).toBe("/fetch");
171-
expect(events[0].mountedApp).toBe(fetchHandler);
172-
} finally {
173-
mountChannel.unsubscribe(handler);
174-
}
175-
});
176-
17764
it("tracing channels fire for handlers", async () => {
17865
const listener = createTracingListener();
17966

@@ -466,5 +353,5 @@ describeMatrix(
466353
}
467354
});
468355
},
469-
{ tracing: true },
356+
testOpts,
470357
);

0 commit comments

Comments
 (0)