From efacfd27cb380c00a9b834df7c3082387aca702e Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 1 Sep 2022 10:38:10 +0200 Subject: [PATCH 1/5] make consistent with main bridge --- packages/insomnia/src/main/ipc/main.ts | 6 +- .../insomnia/src/main/network/websocket.ts | 48 +++++++----- packages/insomnia/src/preload.ts | 77 +++---------------- .../use-ws-connection-events.ts | 9 +-- .../websocket-client/use-ws-ready-state.ts | 8 +- 5 files changed, 48 insertions(+), 100 deletions(-) diff --git a/packages/insomnia/src/main/ipc/main.ts b/packages/insomnia/src/main/ipc/main.ts index 0978913b8d4..bf096094477 100644 --- a/packages/insomnia/src/main/ipc/main.ts +++ b/packages/insomnia/src/main/ipc/main.ts @@ -2,9 +2,9 @@ import { app, ipcMain, IpcRendererEvent } from 'electron'; import { writeFile } from 'fs/promises'; import { authorizeUserInWindow } from '../../network/o-auth-2/misc'; -import { WSConnection } from '../../preload'; import installPlugin from '../install-plugin'; import { cancelCurlRequest, curlRequest } from '../network/libcurl-promise'; +import { WebSocketBridgeAPI } from '../network/websocket'; export interface MainBridgeAPI { restart: () => void; @@ -14,8 +14,8 @@ export interface MainBridgeAPI { writeFile: (options: { path: string; content: string }) => Promise; cancelCurlRequest: typeof cancelCurlRequest; curlRequest: typeof curlRequest; - on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) => Function; - webSocketConnection: WSConnection; + on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) => () => void; + webSocketConnection: WebSocketBridgeAPI; } export function registerMainHandlers() { ipcMain.handle('authorizeUserInWindow', (_, options: Parameters[0]) => { diff --git a/packages/insomnia/src/main/network/websocket.ts b/packages/insomnia/src/main/network/websocket.ts index 6400352151a..a618e141541 100644 --- a/packages/insomnia/src/main/network/websocket.ts +++ b/packages/insomnia/src/main/network/websocket.ts @@ -121,7 +121,7 @@ const parseResponseAndBuildTimeline = (url: string, incomingMessage: IncomingMes async function createWebSocketConnection( event: Electron.IpcMainInvokeEvent, options: { requestId: string; workspaceId: string } -) { +): Promise { const existingConnection = WebSocketConnections.get(options.requestId); if (existingConnection) { @@ -142,8 +142,9 @@ async function createWebSocketConnection( timelineFileStreams.set(options.requestId, fs.createWriteStream(timelinePath)); try { - const eventChannel = `webSocketRequest.connection.${responseId}.event`; - const readyStateChannel = `webSocketRequest.connection.${request._id}.readyState`; + const eventChannel = `webSocket.${responseId}.event`; + const readyStateChannel = `webSocket.${request._id}.readyState`; + // @TODO: Render nunjucks tags in these headers const reduceArrayToLowerCaseKeyedDictionary = (acc: { [key: string]: string }, { name, value }: BaseWebSocketRequest['headers'][0]) => ({ ...acc, [name.toLowerCase() || '']: value || '' }); @@ -349,17 +350,16 @@ async function deleteRequestMaps(requestId: string, message: string, event?: Web WebSocketConnections.delete(requestId); } -function getWebSocketReadyState( - _event: Electron.IpcMainInvokeEvent, +async function getWebSocketReadyState( options: { requestId: string } -): WebSocketConnection['readyState'] { - return WebSocketConnections.get(options.requestId)?.readyState ?? 0; +): Promise { + return Promise.resolve(WebSocketConnections.get(options.requestId)?.readyState ?? 0); } async function sendWebSocketEvent( event: Electron.IpcMainInvokeEvent, options: { message: string; requestId: string } -) { +): Promise { const ws = WebSocketConnections.get(options.requestId); if (!ws) { @@ -399,9 +399,8 @@ async function sendWebSocketEvent( } async function closeWebSocketConnection( - _event: Electron.IpcMainInvokeEvent, options: { requestId: string } -) { +): Promise { const ws = WebSocketConnections.get(options.requestId); if (!ws) { return; @@ -410,9 +409,8 @@ async function closeWebSocketConnection( } async function findMany( - _event: Electron.IpcMainInvokeEvent, options: { responseId: string } -) { +): Promise { const response = await models.response.getById(options.responseId); if (!response || !response.bodyPath) { return []; @@ -425,7 +423,7 @@ async function findMany( /** * Sets the CTS flag; sent when the UI is ready for more events. */ -function signalClearToSend(event: Electron.IpcMainInvokeEvent) { +function signalClearToSend(event: Electron.IpcMainInvokeEvent): void { const nextChannel = sendQueueMap.keys().next(); // There are no pending events; just set the CTS flag. @@ -444,13 +442,25 @@ function signalClearToSend(event: Electron.IpcMainInvokeEvent) { sendQueueMap.delete(nextChannel.value); } +export interface WebSocketBridgeAPI { + create: (options: { requestId: string; workspaceId: string }) => void; + close: typeof closeWebSocketConnection; + readyState: { + getCurrent: typeof getWebSocketReadyState; + }; + event: { + findMany: typeof findMany; + send: (options: { requestId: string; message: string }) => void; + clearToSend: () => void; + }; +} export function registerWebSocketHandlers() { - ipcMain.handle('webSocketRequest.connection.create', createWebSocketConnection); - ipcMain.handle('webSocketRequest.connection.readyState', getWebSocketReadyState); - ipcMain.handle('webSocketRequest.connection.event.send', sendWebSocketEvent); - ipcMain.handle('webSocketRequest.connection.close', closeWebSocketConnection); - ipcMain.handle('webSocketRequest.connection.event.findMany', findMany); - ipcMain.handle('webSocketRequest.connection.clearToSend', signalClearToSend); + ipcMain.handle('webSocket.create', createWebSocketConnection); + ipcMain.handle('webSocket.event.send', sendWebSocketEvent); + ipcMain.handle('webSocket.clearToSend', signalClearToSend); + ipcMain.handle('webSocket.close', (_, options: Parameters[0]) => closeWebSocketConnection(options)); + ipcMain.handle('webSocket.readyState', (_, options: Parameters[0]) => getWebSocketReadyState(options)); + ipcMain.handle('webSocket.event.findMany', (_, options: Parameters[0]) => findMany(options)); } electron.app.on('window-all-closed', () => { diff --git a/packages/insomnia/src/preload.ts b/packages/insomnia/src/preload.ts index b65c73312b3..7092604fc21 100644 --- a/packages/insomnia/src/preload.ts +++ b/packages/insomnia/src/preload.ts @@ -1,78 +1,21 @@ -import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'; +import { contextBridge, ipcRenderer } from 'electron'; -import type { WebSocketEvent } from './main/network/websocket'; +import type { WebSocketBridgeAPI } from './main/network/websocket'; -const webSocketConnection = { - create: (options: { requestId: string; workspaceId: string }) => { - return ipcRenderer.invoke('webSocketRequest.connection.create', options); - }, - close: (options: { requestId: string }) => { - return ipcRenderer.invoke('webSocketRequest.connection.close', options); - }, +const webSocketConnection: WebSocketBridgeAPI = { + create: options => ipcRenderer.invoke('webSocket.create', options), + close: options => ipcRenderer.invoke('webSocket.close', options), readyState: { - getCurrent: (options: { requestId: string }) => { - return ipcRenderer.invoke('webSocketRequest.connection.readyState', options); - }, - subscribe: ( - options: { requestId: string }, - listener: (readyState: WebSocket['readyState']) => any - ) => { - const channel = `webSocketRequest.connection.${options.requestId}.readyState`; - - function onReadyStateChange(_event: IpcRendererEvent, readyState: WebSocket['readyState']) { - listener(readyState); - } - - ipcRenderer.on(channel, onReadyStateChange); - - const unsubscribe = () => { - ipcRenderer.off(channel, onReadyStateChange); - }; - - return unsubscribe; - }, + getCurrent: options => ipcRenderer.invoke('webSocket.readyState', options), }, event: { - findMany: (options: { - responseId: string; - }): Promise => { - return ipcRenderer.invoke( - 'webSocketRequest.connection.event.findMany', - options - ); - }, - subscribe: ( - options: { responseId: string }, - listener: (webSocketEvents: WebSocketEvent[]) => any - ) => { - const channel = `webSocketRequest.connection.${options.responseId}.event`; - - function onNewEvent(_event: IpcRendererEvent, webSocketEvents: WebSocketEvent[]) { - listener(webSocketEvents); - } - - ipcRenderer.on(channel, onNewEvent); - - const unsubscribe = () => { - ipcRenderer.off(channel, onNewEvent); - }; - - return unsubscribe; - }, - send(options: { requestId: string; message: string }) { - return ipcRenderer.invoke( - 'webSocketRequest.connection.event.send', - options - ); - }, - clearToSend: () => { - return ipcRenderer.invoke('webSocketRequest.connection.clearToSend'); - }, + findMany: options => ipcRenderer.invoke('webSocket.event.findMany', options), + send: options => ipcRenderer.invoke('webSocket.event.send', options), + clearToSend: () => ipcRenderer.invoke('webSocket.clearToSend'), }, }; -export type WSConnection = typeof webSocketConnection; // using 'WS' because main/network/websocket.ts already has WebSocketConnection reserved. -const main: Window['main'] & { webSocketConnection: WSConnection } = { +const main: Window['main'] = { restart: () => ipcRenderer.send('restart'), authorizeUserInWindow: options => ipcRenderer.invoke('authorizeUserInWindow', options), setMenuBarVisibility: options => ipcRenderer.send('setMenuBarVisibility', options), diff --git a/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts b/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts index ceaba48a934..82e7a1c4890 100644 --- a/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts +++ b/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts @@ -19,16 +19,13 @@ export function useWebSocketConnectionEvents({ responseId }: { responseId: strin // we don't lose any. async function fetchAndSubscribeToEvents() { // Fetch all existing events for this connection - const allEvents = await window.main.webSocketConnection.event.findMany({ - responseId, - }); + const allEvents = await window.main.webSocketConnection.event.findMany({ responseId }); if (isMounted) { setEvents(allEvents); } // Subscribe to new events and update the state. - unsubscribe = window.main.webSocketConnection.event.subscribe( - { responseId }, - events => { + unsubscribe = window.main.on(`webSocket.${responseId}.event`, + (_, events: WebSocketEvent[]) => { if (isMounted) { setEvents(allEvents => allEvents.concat(events)); } diff --git a/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts b/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts index 32d2780e852..1c2352aea86 100644 --- a/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts +++ b/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts @@ -17,12 +17,10 @@ export function useWSReadyState(requestId: string): ReadyState { }, [requestId]); useEffect(() => { - const unsubscribe = window.main.webSocketConnection.readyState.subscribe( - { requestId }, - (incomingReadyState: ReadyState) => { + const unsubscribe = window.main.on(`webSocket.${requestId}.readyState`, + (_, incomingReadyState: ReadyState) => { setReadyState(incomingReadyState); - } - ); + }); return unsubscribe; }, [requestId]); From be963a94456e7395effce82aea4320b7396f4d72 Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 1 Sep 2022 10:52:23 +0200 Subject: [PATCH 2/5] rename webSocket --- packages/insomnia/src/main/ipc/main.ts | 2 +- packages/insomnia/src/main/network/websocket.ts | 2 +- packages/insomnia/src/preload.ts | 4 ++-- .../insomnia/src/ui/components/websockets/action-bar.tsx | 4 ++-- .../ui/components/websockets/websocket-request-pane.tsx | 2 +- .../ui/components/websockets/websocket-response-pane.tsx | 2 +- .../context/websocket-client/use-ws-connection-events.ts | 7 ++++--- .../src/ui/context/websocket-client/use-ws-ready-state.ts | 2 +- 8 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/insomnia/src/main/ipc/main.ts b/packages/insomnia/src/main/ipc/main.ts index bf096094477..d77781525d8 100644 --- a/packages/insomnia/src/main/ipc/main.ts +++ b/packages/insomnia/src/main/ipc/main.ts @@ -15,7 +15,7 @@ export interface MainBridgeAPI { cancelCurlRequest: typeof cancelCurlRequest; curlRequest: typeof curlRequest; on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) => () => void; - webSocketConnection: WebSocketBridgeAPI; + webSocket: WebSocketBridgeAPI; } export function registerMainHandlers() { ipcMain.handle('authorizeUserInWindow', (_, options: Parameters[0]) => { diff --git a/packages/insomnia/src/main/network/websocket.ts b/packages/insomnia/src/main/network/websocket.ts index a618e141541..aae7b798adc 100644 --- a/packages/insomnia/src/main/network/websocket.ts +++ b/packages/insomnia/src/main/network/websocket.ts @@ -394,7 +394,7 @@ async function sendWebSocketEvent( console.error('something went wrong'); return; } - const eventChannel = `webSocketRequest.connection.${response._id}.event`; + const eventChannel = `webSocket.${response._id}.event`; dispatchWebSocketEvent(event.sender, eventChannel, lastMessage); } diff --git a/packages/insomnia/src/preload.ts b/packages/insomnia/src/preload.ts index 7092604fc21..b02a647abb6 100644 --- a/packages/insomnia/src/preload.ts +++ b/packages/insomnia/src/preload.ts @@ -2,7 +2,7 @@ import { contextBridge, ipcRenderer } from 'electron'; import type { WebSocketBridgeAPI } from './main/network/websocket'; -const webSocketConnection: WebSocketBridgeAPI = { +const webSocket: WebSocketBridgeAPI = { create: options => ipcRenderer.invoke('webSocket.create', options), close: options => ipcRenderer.invoke('webSocket.close', options), readyState: { @@ -27,7 +27,7 @@ const main: Window['main'] = { ipcRenderer.on(channel, listener); return () => ipcRenderer.removeListener(channel, listener); }, - webSocketConnection, + webSocket, }; const dialog: Window['dialog'] = { showOpenDialog: options => ipcRenderer.invoke('showOpenDialog', options), diff --git a/packages/insomnia/src/ui/components/websockets/action-bar.tsx b/packages/insomnia/src/ui/components/websockets/action-bar.tsx index 3fcd599cb1c..e76b50877dc 100644 --- a/packages/insomnia/src/ui/components/websockets/action-bar.tsx +++ b/packages/insomnia/src/ui/components/websockets/action-bar.tsx @@ -39,7 +39,7 @@ const ActionButton: FC = ({ requestId, readyState }) => { name="websocketActionCloseBtn" type="button" onClick={() => { - window.main.webSocketConnection.close({ requestId }); + window.main.webSocket.close({ requestId }); }} > Close @@ -79,7 +79,7 @@ const WebSocketIcon = styled.span({ export const WebSocketActionBar: FC = ({ requestId, workspaceId, defaultValue, onChange, readyState }) => { const handleSubmit = (event: FormEvent) => { event.preventDefault(); - window.main.webSocketConnection.create({ requestId, workspaceId }); + window.main.webSocket.create({ requestId, workspaceId }); }; return ( diff --git a/packages/insomnia/src/ui/components/websockets/websocket-request-pane.tsx b/packages/insomnia/src/ui/components/websockets/websocket-request-pane.tsx index 4a4233422f2..2261a9422f5 100644 --- a/packages/insomnia/src/ui/components/websockets/websocket-request-pane.tsx +++ b/packages/insomnia/src/ui/components/websockets/websocket-request-pane.tsx @@ -52,7 +52,7 @@ const WebSocketRequestForm: FC<{ requestId: string }> = ({ requestId }) => { const handleSubmit = async (event: FormEvent) => { event.preventDefault(); const message = editorRef.current?.getValue() || ''; - window.main.webSocketConnection.event.send({ requestId, message }); + window.main.webSocket.event.send({ requestId, message }); }; return ( diff --git a/packages/insomnia/src/ui/components/websockets/websocket-response-pane.tsx b/packages/insomnia/src/ui/components/websockets/websocket-response-pane.tsx index 821fb51d31b..95ba4748c28 100644 --- a/packages/insomnia/src/ui/components/websockets/websocket-response-pane.tsx +++ b/packages/insomnia/src/ui/components/websockets/websocket-response-pane.tsx @@ -77,7 +77,7 @@ const WebSocketActiveResponsePane: FC<{ requestId: string; response: Response; h const setActiveResponseAndDisconnect = (requestId: string, response: Response | null) => { handleSetActiveResponse(requestId, response); - window.main.webSocketConnection.close({ requestId }); + window.main.webSocket.close({ requestId }); }; useEffect(() => { diff --git a/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts b/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts index 82e7a1c4890..01a331803ad 100644 --- a/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts +++ b/packages/insomnia/src/ui/context/websocket-client/use-ws-connection-events.ts @@ -19,13 +19,14 @@ export function useWebSocketConnectionEvents({ responseId }: { responseId: strin // we don't lose any. async function fetchAndSubscribeToEvents() { // Fetch all existing events for this connection - const allEvents = await window.main.webSocketConnection.event.findMany({ responseId }); + const allEvents = await window.main.webSocket.event.findMany({ responseId }); if (isMounted) { setEvents(allEvents); } // Subscribe to new events and update the state. unsubscribe = window.main.on(`webSocket.${responseId}.event`, (_, events: WebSocketEvent[]) => { + console.log('received events', events); if (isMounted) { setEvents(allEvents => allEvents.concat(events)); } @@ -33,11 +34,11 @@ export function useWebSocketConnectionEvents({ responseId }: { responseId: strin // Wait to give the CTS signal until we've rendered a frame. // This gives the UI a chance to render and respond to user interactions between receiving events. // Note that we do this even if the component isn't mounted, to ensure that CTS gets set even if a race occurs. - window.requestAnimationFrame(window.main.webSocketConnection.event.clearToSend); + window.requestAnimationFrame(window.main.webSocket.event.clearToSend); } ); - window.main.webSocketConnection.event.clearToSend(); + window.main.webSocket.event.clearToSend(); } fetchAndSubscribeToEvents(); diff --git a/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts b/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts index 1c2352aea86..7ea8fcaf036 100644 --- a/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts +++ b/packages/insomnia/src/ui/context/websocket-client/use-ws-ready-state.ts @@ -10,7 +10,7 @@ export function useWSReadyState(requestId: string): ReadyState { const [readyState, setReadyState] = useState(ReadyState.CLOSED); useEffect(() => { - window.main.webSocketConnection.readyState.getCurrent({ requestId }) + window.main.webSocket.readyState.getCurrent({ requestId }) .then((currentReadyState: ReadyState) => { setReadyState(currentReadyState); }); From cb535cb8e803b7a0c0089d09a6f8ceb0d8f9f75b Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 1 Sep 2022 17:25:41 +0200 Subject: [PATCH 3/5] remove deviated mock --- .../ui/context/websocket-client/test-utils.ts | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 packages/insomnia/src/ui/context/websocket-client/test-utils.ts diff --git a/packages/insomnia/src/ui/context/websocket-client/test-utils.ts b/packages/insomnia/src/ui/context/websocket-client/test-utils.ts deleted file mode 100644 index a38ec8f9d91..00000000000 --- a/packages/insomnia/src/ui/context/websocket-client/test-utils.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { jest } from '@jest/globals'; -import EventEmitter from 'events'; - -import { WebSocketEvent } from '../../../main/network/websocket'; -import { ReadyState } from './use-ws-ready-state'; - -export const getMockWsClient = (onChangeEvent: EventEmitter) => { - return { - event: { - findMany: jest.fn(), - send: jest.fn(), - subscribe: (options: { requestId: string }, listener: (event: WebSocketEvent) => any) => { - const channel = `webSocketRequest.connection.${options.requestId}.event`; - onChangeEvent.on(channel, listener); - return () => onChangeEvent.removeListener(channel, listener); - }, - clearToSend: jest.fn(), - }, - create: jest.fn(), - close: jest.fn(), - send: jest.fn(), - onReadyState: (options: { requestId: string }, listener: (readyState: ReadyState) => any) => { - const channel = `webSocketRequest.connection.${options.requestId}.readyState`; - onChangeEvent.on(channel, listener); - return () => onChangeEvent.removeListener(channel, listener); - }, - getReadyState: jest.fn(), - }; -}; From a4c3d9cf21789f41d90183153a6d55c4ec31bb8f Mon Sep 17 00:00:00 2001 From: jackkav Date: Thu, 1 Sep 2022 18:15:27 +0200 Subject: [PATCH 4/5] use consistent arrow function defintions --- .../insomnia/src/main/network/websocket.ts | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/insomnia/src/main/network/websocket.ts b/packages/insomnia/src/main/network/websocket.ts index aae7b798adc..ea7e1871131 100644 --- a/packages/insomnia/src/main/network/websocket.ts +++ b/packages/insomnia/src/main/network/websocket.ts @@ -84,7 +84,7 @@ const sendQueueMap = new Map(); * When CTS is set, the events are sent immediately. * If CTS is cleared, the events are batched into the send queue. */ -function dispatchWebSocketEvent(target: Electron.WebContents, eventChannel: string, wsEvent: WebSocketEvent): void { +const dispatchWebSocketEvent = (target: Electron.WebContents, eventChannel: string, wsEvent: WebSocketEvent): void => { // If the CTS flag is already set, just send immediately. if (clearToSend) { target.send(eventChannel, [wsEvent]); @@ -99,7 +99,7 @@ function dispatchWebSocketEvent(target: Electron.WebContents, eventChannel: stri } else { sendQueueMap.set(eventChannel, [wsEvent]); } -} +}; const parseResponseAndBuildTimeline = (url: string, incomingMessage: IncomingMessage, clientRequestHeaders: string) => { const statusMessage = incomingMessage.statusMessage || ''; @@ -118,10 +118,10 @@ const parseResponseAndBuildTimeline = (url: string, incomingMessage: IncomingMes return { timeline, responseHeaders, statusCode, statusMessage, httpVersion }; }; -async function createWebSocketConnection( +const createWebSocketConnection = async ( event: Electron.IpcMainInvokeEvent, options: { requestId: string; workspaceId: string } -): Promise { +): Promise => { const existingConnection = WebSocketConnections.get(options.requestId); if (existingConnection) { @@ -323,9 +323,9 @@ async function createWebSocketConnection( deleteRequestMaps(request._id, e.message || 'Something went wrong'); createErrorResponse(responseId, request._id, timelinePath, e.message || 'Something went wrong'); } -} +}; -async function createErrorResponse(responseId: string, requestId: string, timelinePath: string, message: string) { +const createErrorResponse = async (responseId: string, requestId: string, timelinePath: string, message: string) => { const settings = await models.settings.getOrCreate(); const responsePatch = { _id: responseId, @@ -336,9 +336,9 @@ async function createErrorResponse(responseId: string, requestId: string, timeli }; models.response.create(responsePatch, settings.maxHistoryResponses); models.requestMeta.updateOrCreateByParentId(requestId, { activeResponseId: null }); -} +}; -async function deleteRequestMaps(requestId: string, message: string, event?: WebSocketCloseEvent | WebSocketErrorEvent,) { +const deleteRequestMaps = async (requestId: string, message: string, event?: WebSocketCloseEvent | WebSocketErrorEvent) => { if (event) { eventLogFileStreams.get(requestId)?.write(JSON.stringify(event) + '\n'); } @@ -348,18 +348,18 @@ async function deleteRequestMaps(requestId: string, message: string, event?: Web timelineFileStreams.get(requestId)?.end(); timelineFileStreams.delete(requestId); WebSocketConnections.delete(requestId); -} +}; -async function getWebSocketReadyState( +const getWebSocketReadyState = async ( options: { requestId: string } -): Promise { +): Promise => { return Promise.resolve(WebSocketConnections.get(options.requestId)?.readyState ?? 0); -} +}; -async function sendWebSocketEvent( +const sendWebSocketEvent = async ( event: Electron.IpcMainInvokeEvent, options: { message: string; requestId: string } -): Promise { +): Promise => { const ws = WebSocketConnections.get(options.requestId); if (!ws) { @@ -396,21 +396,21 @@ async function sendWebSocketEvent( } const eventChannel = `webSocket.${response._id}.event`; dispatchWebSocketEvent(event.sender, eventChannel, lastMessage); -} +}; -async function closeWebSocketConnection( +const closeWebSocketConnection = async ( options: { requestId: string } -): Promise { +): Promise => { const ws = WebSocketConnections.get(options.requestId); if (!ws) { return; } ws.close(); -} +}; -async function findMany( +const findMany = async ( options: { responseId: string } -): Promise { +): Promise => { const response = await models.response.getById(options.responseId); if (!response || !response.bodyPath) { return []; @@ -418,12 +418,12 @@ async function findMany( const body = await fs.promises.readFile(response.bodyPath); return body.toString().split('\n').filter(e => e?.trim()) .map(e => JSON.parse(e)) || []; -} +}; /** * Sets the CTS flag; sent when the UI is ready for more events. */ -function signalClearToSend(event: Electron.IpcMainInvokeEvent): void { +const signalClearToSend = (event: Electron.IpcMainInvokeEvent): void => { const nextChannel = sendQueueMap.keys().next(); // There are no pending events; just set the CTS flag. @@ -440,7 +440,7 @@ function signalClearToSend(event: Electron.IpcMainInvokeEvent): void { event.sender.send(nextChannel.value, sendQueue); sendQueueMap.delete(nextChannel.value); -} +}; export interface WebSocketBridgeAPI { create: (options: { requestId: string; workspaceId: string }) => void; @@ -454,14 +454,14 @@ export interface WebSocketBridgeAPI { clearToSend: () => void; }; } -export function registerWebSocketHandlers() { +export const registerWebSocketHandlers = () => { ipcMain.handle('webSocket.create', createWebSocketConnection); ipcMain.handle('webSocket.event.send', sendWebSocketEvent); ipcMain.handle('webSocket.clearToSend', signalClearToSend); ipcMain.handle('webSocket.close', (_, options: Parameters[0]) => closeWebSocketConnection(options)); ipcMain.handle('webSocket.readyState', (_, options: Parameters[0]) => getWebSocketReadyState(options)); ipcMain.handle('webSocket.event.findMany', (_, options: Parameters[0]) => findMany(options)); -} +}; electron.app.on('window-all-closed', () => { WebSocketConnections.forEach(ws => { From f24b993d19d3210b3310ace7b7c7930704dbce0e Mon Sep 17 00:00:00 2001 From: Jack Kavanagh Date: Fri, 2 Sep 2022 10:12:57 +0200 Subject: [PATCH 5/5] Update packages/insomnia/src/main/network/websocket.ts Co-authored-by: James Gatz --- packages/insomnia/src/main/network/websocket.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/insomnia/src/main/network/websocket.ts b/packages/insomnia/src/main/network/websocket.ts index ea7e1871131..beb017d26d0 100644 --- a/packages/insomnia/src/main/network/websocket.ts +++ b/packages/insomnia/src/main/network/websocket.ts @@ -353,7 +353,7 @@ const deleteRequestMaps = async (requestId: string, message: string, event?: Web const getWebSocketReadyState = async ( options: { requestId: string } ): Promise => { - return Promise.resolve(WebSocketConnections.get(options.requestId)?.readyState ?? 0); + return WebSocketConnections.get(options.requestId)?.readyState ?? 0; }; const sendWebSocketEvent = async (