summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/selectors/messages.js
blob: d12b465b94bf718bd2054fdbc754c5dc91711e4c (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
/* 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 {
  createSelector,
} = require("resource://devtools/client/shared/vendor/reselect.js");

/**
 * Returns list of messages that are visible to the user.
 * Filtered messages by types and text are factored in.
 */
const getDisplayedMessages = createSelector(
  state => state.messages,
  ({
    messages,
    messageFilterType,
    showControlFrames,
    messageFilterText,
    currentChannelId,
  }) => {
    if (!currentChannelId || !messages.get(currentChannelId)) {
      return [];
    }

    const messagesArray = messages.get(currentChannelId);
    if (messageFilterType === "all" && messageFilterText.length === 0) {
      return messagesArray.filter(message =>
        typeFilter(message, messageFilterType, showControlFrames)
      );
    }

    const filter = searchFilter(messageFilterText);

    // If message payload is > 10,000 characters long, we check the LongStringActor payload string
    return messagesArray.filter(
      message =>
        (message.payload.initial
          ? filter(message.payload.initial)
          : filter(message.payload)) &&
        typeFilter(message, messageFilterType, showControlFrames)
    );
  }
);

function typeFilter(message, messageFilterType, showControlFrames) {
  const controlFrames = [0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf];
  const isControlFrame = controlFrames.includes(message.opCode);
  if (messageFilterType === "all" || messageFilterType === message.type) {
    return showControlFrames || !isControlFrame;
  }
  return false;
}

function searchFilter(messageFilterText) {
  let regex;
  if (looksLikeRegex(messageFilterText)) {
    try {
      regex = regexFromText(messageFilterText);
    } catch (e) {}
  }

  return regex
    ? payload => regex.test(payload)
    : payload => payload.includes(messageFilterText);
}

function looksLikeRegex(text) {
  return text.startsWith("/") && text.endsWith("/") && text.length > 2;
}

function regexFromText(text) {
  return new RegExp(text.slice(1, -1), "im");
}

/**
 * Checks if the selected message is visible.
 * If the selected message is not visible, the SplitBox component
 * should not show the MessagePayload component.
 */
const isSelectedMessageVisible = createSelector(
  state => state.messages,
  getDisplayedMessages,
  ({ selectedMessage }, displayedMessages) =>
    displayedMessages.some(message => message === selectedMessage)
);

/**
 * Returns the current selected message.
 */
const getSelectedMessage = createSelector(
  state => state.messages,
  ({ selectedMessage }) => (selectedMessage ? selectedMessage : undefined)
);

/**
 * Returns summary data of the list of messages that are visible to the user.
 * Filtered messages by types and text are factored in.
 */
const getDisplayedMessagesSummary = createSelector(
  getDisplayedMessages,
  displayedMessages => {
    let firstStartedMs = +Infinity;
    let lastEndedMs = -Infinity;
    let sentSize = 0;
    let receivedSize = 0;
    let totalSize = 0;

    displayedMessages.forEach(message => {
      if (message.type == "received") {
        receivedSize += message.payload.length;
      } else if (message.type == "sent") {
        sentSize += message.payload.length;
      }
      totalSize += message.payload.length;
      if (message.timeStamp < firstStartedMs) {
        firstStartedMs = message.timeStamp;
      }
      if (message.timeStamp > lastEndedMs) {
        lastEndedMs = message.timeStamp;
      }
    });

    return {
      count: displayedMessages.length,
      totalMs: (lastEndedMs - firstStartedMs) / 1000,
      sentSize,
      receivedSize,
      totalSize,
    };
  }
);

/**
 * Returns if the currentChannelId is closed
 */
const isCurrentChannelClosed = createSelector(
  state => state.messages,
  ({ closedConnections, currentChannelId }) =>
    closedConnections.has(currentChannelId)
);

/**
 * Returns the closed connection details of the currentChannelId
 * Null, if the connection is still open
 */
const getClosedConnectionDetails = createSelector(
  state => state.messages,
  ({ closedConnections, currentChannelId }) =>
    closedConnections.get(currentChannelId)
);

module.exports = {
  getSelectedMessage,
  isSelectedMessageVisible,
  getDisplayedMessages,
  getDisplayedMessagesSummary,
  isCurrentChannelClosed,
  getClosedConnectionDetails,
};