-
-
Notifications
You must be signed in to change notification settings - Fork 849
fix(dev): proxy dev WebSocket upgrades natively across runtimes #4376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
10b6edd
cd784a1
f40203a
231d7f6
9a8b926
8f7ac8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { defineWebSocketHandler } from "h3"; | ||
|
|
||
| export default defineWebSocketHandler({ | ||
| open(peer) { | ||
| peer.send("connected"); | ||
| }, | ||
| message(peer, message) { | ||
| if (message.text() === "ping") { | ||
| peer.send("pong"); | ||
| } else { | ||
| peer.send(`echo:${message.text()}`); | ||
| } | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,28 @@ describe("nitro:preset:nitro-dev", async () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(status).toBe(200); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe("websocket", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("upgrades and echoes messages in dev mode", async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const wsURL = ctx.server!.url.replace(/^http/, "ws") + "ws"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ws = new WebSocket(wsURL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const messages: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await new Promise<void>((resolve, reject) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const timer = setTimeout(() => reject(new Error("websocket timeout")), 5000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ws.addEventListener("error", (error) => reject(error as any)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ws.addEventListener("message", (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages.push(String(event.data)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (messages.length === 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clearTimeout(timer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ws.close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ws.addEventListener("open", () => ws.send("ping")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Close socket on timeout/error to avoid dangling test resources. Failure paths currently reject without guaranteed socket cleanup, which can leave open handles and make the suite flaky. Suggested fix await new Promise<void>((resolve, reject) => {
- const timer = setTimeout(() => reject(new Error("websocket timeout")), 5000);
- ws.addEventListener("error", (error) => reject(error as any));
+ const fail = (error: unknown) => {
+ clearTimeout(timer);
+ ws.close();
+ reject(error);
+ };
+ const timer = setTimeout(() => fail(new Error("websocket timeout")), 5000);
+ ws.addEventListener("error", (error) => fail(error as any), { once: true });
ws.addEventListener("message", (event) => {
messages.push(String(event.data));
if (messages.length === 2) {
clearTimeout(timer);
ws.close();
resolve();
}
});
ws.addEventListener("open", () => ws.send("ping"));
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(messages).toEqual(["connected", "pong"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe("tasks", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("GET /_nitro/tasks lists tasks", async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { data, status } = await callHandler({ url: "/_nitro/tasks" }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Remove line-explaining comments in runtime entry.
These comments restate behavior inline and should be removed to align with repo style for
src/**/*.{ts,js}.As per coding guidelines,
src/**/*.{ts,js}says: "Do not add comments explaining what the line does unless prompted."🤖 Prompt for AI Agents
Source: Coding guidelines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hcharly637-uxCould you clarify what you'd like me to do here? It looks like your message may have been sent by accident. If you'd like, I can:Let me know how you'd like to proceed.
🧠 Learnings used