MCPcopy Create free account
hub / github.com/CopilotKit/aimock / upgradeToWebSocket

Function upgradeToWebSocket

src/ws-framing.ts:230–262  ·  view source on GitHub ↗
(
  req: http.IncomingMessage,
  socket: net.Socket,
)

Source from the content-addressed store, hash-verified

228}
229
230export function upgradeToWebSocket(
231 req: http.IncomingMessage,
232 socket: net.Socket,
233): WebSocketConnection {
234 const key = req.headers["sec-websocket-key"];
235 if (!key) {
236 socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
237 socket.destroy();
238 throw new Error("Missing Sec-WebSocket-Key header");
239 }
240
241 const acceptKey = computeAcceptKey(key);
242
243 let responseHeaders =
244 "HTTP/1.1 101 Switching Protocols\r\n" +
245 "Upgrade: websocket\r\n" +
246 "Connection: Upgrade\r\n" +
247 `Sec-WebSocket-Accept: ${acceptKey}\r\n`;
248
249 // Echo back requested subprotocol if present
250 const protocol = req.headers["sec-websocket-protocol"];
251 if (protocol) {
252 // Take the first offered protocol
253 const first = protocol.split(",")[0].trim();
254 responseHeaders += `Sec-WebSocket-Protocol: ${first}\r\n`;
255 }
256
257 responseHeaders += "\r\n";
258
259 socket.write(responseHeaders);
260
261 return new WebSocketConnection(socket);
262}

Callers 3

handleUpgradeRequestFunction · 0.85
createTestServerFunction · 0.85
ws-framing.test.tsFile · 0.85

Calls 3

computeAcceptKeyFunction · 0.85
writeMethod · 0.80
destroyMethod · 0.45

Tested by 1

createTestServerFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…