summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/reducers/messages.js
blob: 27d7da28c7f38ec255a65fb60856f636652c87b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  SELECT_REQUEST,
  MSG_ADD,
  MSG_SELECT,
  MSG_OPEN_DETAILS,
  MSG_CLEAR,
  MSG_TOGGLE_FILTER_TYPE,
  MSG_TOGGLE_CONTROL,
  MSG_SET_FILTER_TEXT,
  MSG_TOGGLE_COLUMN,
  MSG_RESET_COLUMNS,
  MSG_CLOSE_CONNECTION,
  CHANNEL_TYPE,
  SET_EVENT_STREAM_FLAG,
} = require("resource://devtools/client/netmonitor/src/constants.js");

/**
 * The default column states for the MessageListItem component.
 */
const defaultColumnsState = {
  data: true,
  size: false,
  time: true,
};

const defaultWSColumnsState = {
  ...defaultColumnsState,
  opCode: false,
  maskBit: false,
  finBit: false,
};

const defaultSSEColumnsState = {
  ...defaultColumnsState,
  eventName: false,
  lastEventId: false,
  retry: false,
};

/**
 * Returns a new object of default cols.
 */
function getMessageDefaultColumnsState(channelType) {
  let columnsState = defaultColumnsState;
  const { EVENT_STREAM, WEB_SOCKET } = CHANNEL_TYPE;

  if (channelType === WEB_SOCKET) {
    columnsState = defaultWSColumnsState;
  } else if (channelType === EVENT_STREAM) {
    columnsState = defaultSSEColumnsState;
  }

  return Object.assign({}, columnsState);
}

/**
 * This structure stores list of all WebSocket and EventSource messages received
 * from the backend.
 */
function Messages(initialState = {}) {
  const { EVENT_STREAM, WEB_SOCKET } = CHANNEL_TYPE;

  return {
    // Map with all requests (key = resourceId, value = array of message objects)
    messages: new Map(),
    messageFilterText: "",
    // Default filter type is "all",
    messageFilterType: "all",
    showControlFrames: false,
    selectedMessage: null,
    messageDetailsOpen: false,
    currentChannelId: null,
    currentChannelType: null,
    currentRequestId: null,
    closedConnections: new Map(),
    columns: null,
    sseColumns: getMessageDefaultColumnsState(EVENT_STREAM),
    wsColumns: getMessageDefaultColumnsState(WEB_SOCKET),
    ...initialState,
  };
}

/**
 * When a network request is selected,
 * set the current resourceId affiliated with the connection.
 */
function setCurrentChannel(state, action) {
  if (!action.request) {
    return state;
  }

  const { id, cause, resourceId, isEventStream } = action.request;
  const { EVENT_STREAM, WEB_SOCKET } = CHANNEL_TYPE;
  let currentChannelType = null;
  let columnsKey = "columns";
  if (cause.type === "websocket") {
    currentChannelType = WEB_SOCKET;
    columnsKey = "wsColumns";
  } else if (isEventStream) {
    currentChannelType = EVENT_STREAM;
    columnsKey = "sseColumns";
  }

  return {
    ...state,
    columns:
      currentChannelType === state.currentChannelType
        ? { ...state.columns }
        : { ...state[columnsKey] },
    currentChannelId: resourceId,
    currentChannelType,
    currentRequestId: id,
    // Default filter text is empty string for a new connection
    messageFilterText: "",
  };
}

/**
 * If the request is already selected and isEventStream flag
 * is added later, we need to update currentChannelType & columns.
 */
function updateCurrentChannel(state, action) {
  if (state.currentRequestId === action.id) {
    const currentChannelType = CHANNEL_TYPE.EVENT_STREAM;
    return {
      ...state,
      columns: { ...state.sseColumns },
      currentChannelType,
    };
  }
  return state;
}

/**
 * Appending new message into the map.
 */
function addMessage(state, action) {
  const { httpChannelId } = action;
  const nextState = { ...state };

  const newMessage = {
    httpChannelId,
    ...action.data,
  };

  nextState.messages = mapSet(
    nextState.messages,
    newMessage.httpChannelId,
    newMessage
  );

  return nextState;
}

/**
 * Select specific message.
 */
function selectMessage(state, action) {
  return {
    ...state,
    selectedMessage: action.message,
    messageDetailsOpen: action.open,
  };
}

/**
 * Shows/Hides the MessagePayload component.
 */
function openMessageDetails(state, action) {
  return {
    ...state,
    messageDetailsOpen: action.open,
  };
}

/**
 * Clear messages of the request from the state.
 */
function clearMessages(state) {
  const nextState = { ...state };
  const defaultState = Messages();
  nextState.messages = new Map(state.messages);
  nextState.messages.delete(nextState.currentChannelId);

  // Reset fields to default state.
  nextState.selectedMessage = defaultState.selectedMessage;
  nextState.messageDetailsOpen = defaultState.messageDetailsOpen;

  return nextState;
}

/**
 * Toggle the message filter type of the connection.
 */
function toggleMessageFilterType(state, action) {
  return {
    ...state,
    messageFilterType: action.filter,
  };
}

/**
 * Toggle control frames for the WebSocket connection.
 */
function toggleControlFrames(state, action) {
  return {
    ...state,
    showControlFrames: !state.showControlFrames,
  };
}

/**
 * Set the filter text of the current channelId.
 */
function setMessageFilterText(state, action) {
  return {
    ...state,
    messageFilterText: action.text,
  };
}

/**
 * Toggle the user specified column view state.
 */
function toggleColumn(state, action) {
  const { column } = action;
  let columnsKey = null;
  if (state.currentChannelType === CHANNEL_TYPE.WEB_SOCKET) {
    columnsKey = "wsColumns";
  } else {
    columnsKey = "sseColumns";
  }
  const newColumnsState = {
    ...state[columnsKey],
    [column]: !state[columnsKey][column],
  };
  return {
    ...state,
    columns: newColumnsState,
    [columnsKey]: newColumnsState,
  };
}

/**
 * Reset back to default columns view state.
 */
function resetColumns(state) {
  let columnsKey = null;
  if (state.currentChannelType === CHANNEL_TYPE.WEB_SOCKET) {
    columnsKey = "wsColumns";
  } else {
    columnsKey = "sseColumns";
  }
  const newColumnsState = getMessageDefaultColumnsState(
    state.currentChannelType
  );
  return {
    ...state,
    [columnsKey]: newColumnsState,
    columns: newColumnsState,
  };
}

function closeConnection(state, action) {
  const { httpChannelId, code, reason } = action;
  const nextState = { ...state };

  nextState.closedConnections.set(httpChannelId, {
    code,
    reason,
  });

  return nextState;
}

/**
 * Append new item into existing map and return new map.
 */
function mapSet(map, key, value) {
  const newMap = new Map(map);
  if (newMap.has(key)) {
    const messagesArray = [...newMap.get(key)];
    messagesArray.push(value);
    newMap.set(key, messagesArray);
    return newMap;
  }
  return newMap.set(key, [value]);
}

/**
 * This reducer is responsible for maintaining list of
 * messages within the Network panel.
 */
function messages(state = Messages(), action) {
  switch (action.type) {
    case SELECT_REQUEST:
      return setCurrentChannel(state, action);
    case SET_EVENT_STREAM_FLAG:
      return updateCurrentChannel(state, action);
    case MSG_ADD:
      return addMessage(state, action);
    case MSG_SELECT:
      return selectMessage(state, action);
    case MSG_OPEN_DETAILS:
      return openMessageDetails(state, action);
    case MSG_CLEAR:
      return clearMessages(state);
    case MSG_TOGGLE_FILTER_TYPE:
      return toggleMessageFilterType(state, action);
    case MSG_TOGGLE_CONTROL:
      return toggleControlFrames(state, action);
    case MSG_SET_FILTER_TEXT:
      return setMessageFilterText(state, action);
    case MSG_TOGGLE_COLUMN:
      return toggleColumn(state, action);
    case MSG_RESET_COLUMNS:
      return resetColumns(state);
    case MSG_CLOSE_CONNECTION:
      return closeConnection(state, action);
    default:
      return state;
  }
}

module.exports = {
  Messages,
  messages,
  getMessageDefaultColumnsState,
};