MCPcopy
hub / github.com/di-sukharev/opencommit / establishWebSocketConnection

Function establishWebSocketConnection

out/cli.cjs:63159–63233  ·  view source on GitHub ↗
(url2, protocols, ws, onEstablish, options)

Source from the content-addressed store, hash-verified

63157 channels.open = diagnosticsChannel.channel("undici:websocket:open");
63158 channels.close = diagnosticsChannel.channel("undici:websocket:close");
63159 channels.socketError = diagnosticsChannel.channel("undici:websocket:socket_error");
63160 var crypto3;
63161 try {
63162 crypto3 = require("crypto");
63163 } catch {
63164 }
63165 function establishWebSocketConnection(url2, protocols, ws, onEstablish, options) {
63166 const requestURL = url2;
63167 requestURL.protocol = url2.protocol === "ws:" ? "http:" : "https:";
63168 const request3 = makeRequest2({
63169 urlList: [requestURL],
63170 serviceWorkers: "none",
63171 referrer: "no-referrer",
63172 mode: "websocket",
63173 credentials: "include",
63174 cache: "no-store",
63175 redirect: "error"
63176 });
63177 if (options.headers) {
63178 const headersList = new Headers6(options.headers)[kHeadersList];
63179 request3.headersList = headersList;
63180 }
63181 const keyValue = crypto3.randomBytes(16).toString("base64");
63182 request3.headersList.append("sec-websocket-key", keyValue);
63183 request3.headersList.append("sec-websocket-version", "13");
63184 for (const protocol of protocols) {
63185 request3.headersList.append("sec-websocket-protocol", protocol);
63186 }
63187 const permessageDeflate = "";
63188 const controller = fetching({
63189 request: request3,
63190 useParallelQueue: true,
63191 dispatcher: options.dispatcher ?? getGlobalDispatcher(),
63192 processResponse(response) {
63193 if (response.type === "error" || response.status !== 101) {
63194 failWebsocketConnection(ws, "Received network error or non-101 status code.");
63195 return;
63196 }
63197 if (protocols.length !== 0 && !response.headersList.get("Sec-WebSocket-Protocol")) {
63198 failWebsocketConnection(ws, "Server did not respond with sent protocols.");
63199 return;
63200 }
63201 if (response.headersList.get("Upgrade")?.toLowerCase() !== "websocket") {
63202 failWebsocketConnection(ws, 'Server did not set Upgrade header to "websocket".');
63203 return;
63204 }
63205 if (response.headersList.get("Connection")?.toLowerCase() !== "upgrade") {
63206 failWebsocketConnection(ws, 'Server did not set Connection header to "upgrade".');
63207 return;
63208 }
63209 const secWSAccept = response.headersList.get("Sec-WebSocket-Accept");
63210 const digest = crypto3.createHash("sha1").update(keyValue + uid).digest("base64");
63211 if (secWSAccept !== digest) {
63212 failWebsocketConnection(ws, "Incorrect hash received in Sec-WebSocket-Accept header.");
63213 return;
63214 }
63215 const secExtension = response.headersList.get("Sec-WebSocket-Extensions");
63216 if (secExtension !== null && secExtension !== permessageDeflate) {

Callers 1

constructorMethod · 0.85

Calls 5

makeRequest2Function · 0.85
fetchingFunction · 0.85
getGlobalDispatcherFunction · 0.85
toStringMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…