tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

log.js (2438B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 import flags from "devtools/shared/flags";
      6 import { prefs } from "../../../utils/prefs";
      7 
      8 const ignoreList = [
      9  "ADD_BREAKPOINT_POSITIONS",
     10  "OUT_OF_SCOPE_LOCATIONS",
     11  "MAP_SCOPES",
     12  "MAP_FRAMES",
     13  "ADD_SCOPES",
     14  "IN_SCOPE_LINES",
     15  "REMOVE_BREAKPOINT",
     16  "NODE_PROPERTIES_LOADED",
     17  "SET_FOCUSED_SOURCE_ITEM",
     18  "NODE_EXPAND",
     19  "IN_SCOPE_LINES",
     20  "SET_PREVIEW",
     21 ];
     22 
     23 function cloneAction(action) {
     24  action = action || {};
     25  action = { ...action };
     26 
     27  // ADD_TAB, ...
     28  if (action.source?.text) {
     29    const source = { ...action.source, text: "" };
     30    action.source = source;
     31  }
     32 
     33  if (action.sources) {
     34    const sources = action.sources.slice(0, 20).map(source => {
     35      const url = !source.url || source.url.includes("data:") ? "" : source.url;
     36      return { ...source, url };
     37    });
     38    action.sources = sources;
     39  }
     40 
     41  // LOAD_SOURCE_TEXT
     42  if (action.text) {
     43    action.text = "";
     44  }
     45 
     46  if (action.value?.text) {
     47    const value = { ...action.value, text: "" };
     48    action.value = value;
     49  }
     50 
     51  return action;
     52 }
     53 
     54 function formatPause(pause) {
     55  return {
     56    ...pause,
     57    pauseInfo: { why: pause.why },
     58    scopes: [],
     59    loadedObjects: [],
     60  };
     61 }
     62 
     63 function serializeAction(action) {
     64  try {
     65    action = cloneAction(action);
     66    if (ignoreList.includes(action.type)) {
     67      action = {};
     68    }
     69 
     70    if (action.type === "PAUSED") {
     71      action = formatPause(action);
     72    }
     73 
     74    const serializer = function (key, value) {
     75      // Serialize Object/LongString fronts
     76      if (value?.getGrip) {
     77        return value.getGrip();
     78      }
     79      return value;
     80    };
     81 
     82    // dump(`> ${action.type}...\n ${JSON.stringify(action, serializer)}\n`);
     83    return JSON.stringify(action, serializer);
     84  } catch (e) {
     85    console.error(e);
     86    return "";
     87  }
     88 }
     89 
     90 /**
     91 * A middleware that logs all actions coming through the system
     92 * to the console.
     93 */
     94 export function log() {
     95  return next => action => {
     96    const asyncMsg = !action.status ? "" : `[${action.status}]`;
     97 
     98    if (prefs.logActions) {
     99      if (flags.testing) {
    100        dump(
    101          `[ACTION] ${action.type} ${asyncMsg} - ${serializeAction(action)}\n`
    102        );
    103      } else {
    104        console.log(action, asyncMsg);
    105      }
    106    }
    107 
    108    next(action);
    109  };
    110 }