Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/bcode-browser/src/cdp/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ export class Session implements Transport {
const ws = this.ws;
this.ws = undefined;
this.activeSessionId = undefined;
for (const [, pending] of this.pending) pending.reject(error);
this.pending.clear();
try { ws?.close(); } catch { /* ignore */ }
}

Expand Down
13 changes: 12 additions & 1 deletion packages/bcode-browser/test/cdp-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const server = Bun.serve({
ws.subscribe(channel)
},
message(ws, message) {
const request = JSON.parse(String(message)) as { id?: number }
const request = JSON.parse(String(message)) as { id?: number; method?: string }
if (request.method === "Test.pending") return
if (typeof request.id === "number") ws.send(JSON.stringify({ id: request.id, result: {} }))
},
},
Expand Down Expand Up @@ -160,3 +161,13 @@ test("invalidate while the socket is connecting closes it and rejects", async ()
await expect(s._call("Runtime.evaluate", { expression: "1" })).rejects.toThrow("retired by test")
await expect(s.connect({ wsUrl: `ws://127.0.0.1:${server.port}/` })).rejects.toThrow("retired by test")
})

test("invalidate rejects in-flight CDP calls", async () => {
const s = new Session()
await s.connect({ wsUrl: `ws://127.0.0.1:${server.port}/` })
const pending = s._call("Test.pending", {})

s.invalidate(new Error("retired with a pending call"))

await expect(pending).rejects.toThrow("retired with a pending call")
})