Skip to content

An open-source, faster Devin alternative

TypeScript

fix(sdk): give WebSockets their own HTTP/1.1 dispatcher so the SDK's global HTTP/2 agent stops breaking its own upgrades, and wait for the socket to open before returning a PTY session

Brian Reardon41b7905d5754
diff --git a/sdks/typescript/src/agent.ts b/sdks/typescript/src/agent.ts
index 7b133101..a2eba8c9 100644
--- a/sdks/typescript/src/agent.ts
+++ b/sdks/typescript/src/agent.ts
@@ -1,3 +1,4 @@
+import { openWebSocket } from "./websocket.js";
export interface SandboxAgentEvent {
type:
| "ready"
@@ -111,8 +112,7 @@ export class SandboxAgent {
: "";
const wsEndpoint = `${wsUrl}/sandboxes/${this.sandboxId}/exec/${sessionId}${authParam}`;
- const ws = new WebSocket(wsEndpoint);
- ws.binaryType = "arraybuffer";
+ const ws = await openWebSocket(wsEndpoint);
let gotExit = false;
let resolveDone: (code: number) => void;
diff --git a/sdks/typescript/src/exec.ts b/sdks/typescript/src/exec.ts
index 96bd52a1..29d12b43 100644
--- a/sdks/typescript/src/exec.ts
+++ b/sdks/typescript/src/exec.ts
@@ -1,3 +1,4 @@
+import { openWebSocket } from "./websocket.js";
import { ShellImpl, type Shell, type ShellOpts } from "./shell.js";
export interface ProcessResult {
@@ -175,27 +176,12 @@ export class Exec {
: "";
const wsEndpoint = `${wsUrl}/sandboxes/${this.sandboxId}/exec/${sessionId}${authParam}`;
- const ws = new WebSocket(wsEndpoint);
- ws.binaryType = "arraybuffer";
+ const ws = await openWebSocket(wsEndpoint);
let gotExit = false;
- let opened = false;
let resolveDone: (code: number) => void;
const done = new Promise<number>((resolve) => { resolveDone = resolve; });
- await new Promise<void>((resolve, reject) => {
- ws.onopen = () => {
- opened = true;
- resolve();
- };
- ws.onerror = () => {
- if (!opened) reject(new Error(`WebSocket connection failed: ${wsEndpoint}`));
- };
- ws.onclose = () => {
- if (!opened) reject(new Error(`WebSocket closed before opening: ${wsEndpoint}`));
- };
- });
-
ws.onmessage = (event) => {
const buf = new Uint8Array(event.data as ArrayBuffer);
if (buf.length < 1) return;
diff --git a/sdks/typescript/src/pty.ts b/sdks/typescript/src/pty.ts
index e33abb51..0c7bdf25 100644
--- a/sdks/typescript/src/pty.ts
+++ b/sdks/typescript/src/pty.ts
@@ -1,3 +1,4 @@
+import { openWebSocket } from "./websocket.js";
export interface PtyOpts {
cols?: number;
rows?: number;
@@ -57,8 +58,7 @@ export class Pty {
: "";
const wsEndpoint = `${wsUrl}/sandboxes/${this.sandboxId}/pty/${sessionId}${authParam}`;
- const ws = new WebSocket(wsEndpoint);
- ws.binaryType = "arraybuffer";
+ const ws = await openWebSocket(wsEndpoint);
if (opts.onOutput) {
const onOutput = opts.onOutput;
diff --git a/sdks/typescript/src/websocket.ts b/sdks/typescript/src/websocket.ts
new file mode 100644
index 00000000..ac3eb944
--- /dev/null
+++ b/sdks/typescript/src/websocket.ts
@@ -0,0 +1,75 @@
+// websocket.ts — opening a WebSocket without tripping over our own HTTP/2 agent.
+//
+// configureHttp2 installs a GLOBAL undici dispatcher with `allowH2: true`,
+// which is right for the REST surface: h2 multiplexes a burst of creates down
+// one connection. But Node's built-in WebSocket is undici's, and it uses that
+// same global dispatcher — so it negotiates h2 via ALPN and then tries to
+// upgrade. A WebSocket over h2 needs RFC 8441 extended CONNECT, which the
+// server's WebSocket library does not implement (nor do most), so the upgrade
+// simply fails.
+//
+// Measured against dev, same URL, back to back:
+//
+// default dispatcher (no allowH2): OPEN
+// allowH2:true dispatcher : ERROR
+//
+// So WebSockets get their own dispatcher with h2 off. The REST path keeps h2
+// and its prewarmed pool; only the upgrade is forced down HTTP/1.1, which is
+// what an upgrade requires anyway.
+
+let wsDispatcher: unknown | null = null;
+let wsCtor: typeof WebSocket | null = null;
+
+async function nodeWebSocket(): Promise<typeof WebSocket | null> {
+ const isNode = typeof process !== "undefined" && !!process.versions?.node;
+ if (!isNode) return null; // browsers speak WebSocket natively; nothing to fix
+ try {
+ const undici = await import("undici");
+ if (!wsDispatcher) {
+ // allowH2 omitted (defaults false) rather than set — an h1-only agent is
+ // the point, and spelling it out invites someone to "align" it with the
+ // global one later.
+ wsDispatcher = new undici.Agent({ keepAliveTimeout: 60_000 });
+ }
+ return undici.WebSocket as unknown as typeof WebSocket;
+ } catch {
+ return null; // stripped-down install — fall back to whatever global exists
+ }
+}
+
+// openWebSocket resolves once the socket is OPEN, and rejects if it errors or
+// closes first.
+//
+// Resolving early is its own bug: a CONNECTING socket throws "Sent before
+// connected" on the first send, and creating a PTY then immediately typing
+// into it is the normal way to use one.
+export async function openWebSocket(url: string): Promise<WebSocket> {
+ if (!wsCtor) wsCtor = await nodeWebSocket();
+ const Ctor = wsCtor ?? (globalThis as { WebSocket?: typeof WebSocket }).WebSocket;
+ if (!Ctor) throw new Error("no WebSocket implementation available in this environment");
+
+ const ws = wsDispatcher
+ ? new (Ctor as unknown as new (u: string, o: unknown) => WebSocket)(url, { dispatcher: wsDispatcher })
+ : new Ctor(url);
+ ws.binaryType = "arraybuffer";
+
+ await new Promise<void>((resolve, reject) => {
+ let opened = false;
+ ws.onopen = () => {
+ opened = true;
+ resolve();
+ };
+ ws.onerror = () => {
+ if (!opened) reject(new Error(`WebSocket connection failed: ${url}`));
+ };
+ ws.onclose = () => {
+ if (!opened) reject(new Error(`WebSocket closed before opening: ${url}`));
+ };
+ });
+ // Handlers are cleared so a caller's own onerror/onclose are not shadowed by
+ // the ones used to await the open.
+ ws.onopen = null;
+ ws.onerror = null;
+ ws.onclose = null;
+ return ws;
+}