MCPcopy
hub / github.com/Kong/insomnia / openWebSocketConnection

Function openWebSocketConnection

packages/insomnia/src/main/network/websocket.ts:104–383  ·  view source on GitHub ↗
(
  _event: Electron.IpcMainInvokeEvent,
  options: OpenWebSocketRequestOptions
)

Source from the content-addressed store, hash-verified

102 initialPayload?: string;
103}
104const openWebSocketConnection = async (
105 _event: Electron.IpcMainInvokeEvent,
106 options: OpenWebSocketRequestOptions
107): Promise<void> => {
108 const existingConnection = WebSocketConnections.get(options.requestId);
109
110 if (existingConnection) {
111 console.warn('Connection still open to ' + existingConnection.url);
112 return;
113 }
114 const request = await webSocketRequest.getById(options.requestId);
115 const responseId = generateId('res');
116 if (!request) {
117 return;
118 }
119
120 const responsesDir = path.join(process.env['INSOMNIA_DATA_PATH'] || electron.app.getPath('userData'), 'responses');
121 fs.mkdirSync(responsesDir, { recursive: true });
122
123 const responseBodyPath = path.join(responsesDir, uuidV4() + '.response');
124 eventLogFileStreams.set(options.requestId, fs.createWriteStream(responseBodyPath));
125 const timelinePath = path.join(responsesDir, responseId + '.timeline');
126 timelineFileStreams.set(options.requestId, fs.createWriteStream(timelinePath));
127
128 const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(options.workspaceId);
129 // fallback to base environment
130 const activeEnvironmentId = workspaceMeta.activeEnvironmentId;
131 const activeEnvironment = activeEnvironmentId && await models.environment.getById(activeEnvironmentId);
132 const environment = activeEnvironment || await models.environment.getOrCreateForParentId(options.workspaceId);
133 invariant(environment, 'failed to find environment ' + activeEnvironmentId);
134 const responseEnvironmentId = environment ? environment._id : null;
135
136 const caCert = await models.caCertificate.findByParentId(options.workspaceId);
137 const caCertficatePath = caCert?.path;
138 // attempt to read CA Certificate PEM from disk, fallback to root certificates
139 const caCertificate = (caCertficatePath && (await fs.promises.readFile(caCertficatePath)).toString()) || tls.rootCertificates.join('\n');
140
141 try {
142 if (!options.url) {
143 throw new Error('URL is required');
144 }
145 const readyStateChannel = `webSocket.${request._id}.readyState`;
146
147 const reduceArrayToLowerCaseKeyedDictionary = (acc: { [key: string]: string }, { name, value }: BaseWebSocketRequest['headers'][0]) =>
148 ({ ...acc, [name.toLowerCase() || '']: value || '' });
149 const headers = options.headers;
150 let url = options.url;
151 let authCookie = null;
152 if (!options.authentication.disabled) {
153 if (options.authentication.type === AUTH_BASIC) {
154 const { username, password, useISO88591 } = options.authentication;
155 const encoding = useISO88591 ? 'latin1' : 'utf8';
156 headers.push(getBasicAuthHeader(username, password, encoding));
157 }
158 if (options.authentication.type === AUTH_API_KEY) {
159 const { key, value, addTo } = options.authentication;
160 if (addTo === HEADER) {
161 headers.push({ name: key, value: value });

Callers

nothing calls this directly

Calls 15

generateIdFunction · 0.90
invariantFunction · 0.90
getBasicAuthHeaderFunction · 0.90
joinUrlAndQueryStringFunction · 0.90
getBearerAuthHeaderFunction · 0.90
urlMatchesCertHostFunction · 0.90
setDefaultProtocolFunction · 0.90
jarFromCookiesFunction · 0.90
getSetCookieHeadersFunction · 0.90

Tested by

no test coverage detected