summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/actions/utils/middleware/log.js
blob: b9592ce22cc9f899c0e63146d252d0933485dc99 (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
/* 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/>. */

import flags from "devtools/shared/flags";
import { prefs } from "../../../utils/prefs";

const ignoreList = [
  "ADD_BREAKPOINT_POSITIONS",
  "SET_SYMBOLS",
  "OUT_OF_SCOPE_LOCATIONS",
  "MAP_SCOPES",
  "MAP_FRAMES",
  "ADD_SCOPES",
  "IN_SCOPE_LINES",
  "REMOVE_BREAKPOINT",
  "NODE_PROPERTIES_LOADED",
  "SET_FOCUSED_SOURCE_ITEM",
  "NODE_EXPAND",
  "IN_SCOPE_LINES",
  "SET_PREVIEW",
];

function cloneAction(action) {
  action = action || {};
  action = { ...action };

  // ADD_TAB, ...
  if (action.source?.text) {
    const source = { ...action.source, text: "" };
    action.source = source;
  }

  if (action.sources) {
    const sources = action.sources.slice(0, 20).map(source => {
      const url = !source.url || source.url.includes("data:") ? "" : source.url;
      return { ...source, url };
    });
    action.sources = sources;
  }

  // LOAD_SOURCE_TEXT
  if (action.text) {
    action.text = "";
  }

  if (action.value?.text) {
    const value = { ...action.value, text: "" };
    action.value = value;
  }

  return action;
}

function formatPause(pause) {
  return {
    ...pause,
    pauseInfo: { why: pause.why },
    scopes: [],
    loadedObjects: [],
  };
}

function serializeAction(action) {
  try {
    action = cloneAction(action);
    if (ignoreList.includes(action.type)) {
      action = {};
    }

    if (action.type === "PAUSED") {
      action = formatPause(action);
    }

    const serializer = function (key, value) {
      // Serialize Object/LongString fronts
      if (value?.getGrip) {
        return value.getGrip();
      }
      return value;
    };

    // dump(`> ${action.type}...\n ${JSON.stringify(action, serializer)}\n`);
    return JSON.stringify(action, serializer);
  } catch (e) {
    console.error(e);
    return "";
  }
}

/**
 * A middleware that logs all actions coming through the system
 * to the console.
 */
export function log({ dispatch, getState }) {
  return next => action => {
    const asyncMsg = !action.status ? "" : `[${action.status}]`;

    if (prefs.logActions) {
      if (flags.testing) {
        dump(
          `[ACTION] ${action.type} ${asyncMsg} - ${serializeAction(action)}\n`
        );
      } else {
        console.log(action, asyncMsg);
      }
    }

    next(action);
  };
}