tor-browser

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

source-blackbox.js (4074B)


      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 /**
      6 * Reducer containing all data about sources being "black boxed".
      7 *
      8 * i.e. sources which should be ignored by the debugger.
      9 * Typically, these sources should be hidden from paused stack frames,
     10 * and any debugger statement or breakpoint should be ignored.
     11 */
     12 
     13 export function initialSourceBlackBoxState(state) {
     14  return {
     15    /* FORMAT:
     16     * blackboxedRanges: {
     17     *  [source url]: [range, range, ...], -- source lines blackboxed
     18     *  [source url]: [], -- whole source blackboxed
     19     *  ...
     20     * }
     21     */
     22    blackboxedRanges: state?.blackboxedRanges ?? {},
     23 
     24    blackboxedSet: state?.blackboxedRanges
     25      ? new Set(Object.keys(state.blackboxedRanges))
     26      : new Set(),
     27 
     28    sourceMapIgnoreListUrls: [],
     29  };
     30 }
     31 
     32 function update(state = initialSourceBlackBoxState(), action) {
     33  switch (action.type) {
     34    case "BLACKBOX_WHOLE_SOURCES": {
     35      const { sources } = action;
     36 
     37      const currentBlackboxedRanges = { ...state.blackboxedRanges };
     38      const currentBlackboxedSet = new Set(state.blackboxedSet);
     39 
     40      for (const source of sources) {
     41        currentBlackboxedRanges[source.url] = [];
     42        currentBlackboxedSet.add(source.url);
     43      }
     44 
     45      return {
     46        ...state,
     47        blackboxedRanges: currentBlackboxedRanges,
     48        blackboxedSet: currentBlackboxedSet,
     49      };
     50    }
     51 
     52    case "BLACKBOX_SOURCE_RANGES": {
     53      const { source, ranges } = action;
     54 
     55      const currentBlackboxedRanges = { ...state.blackboxedRanges };
     56      const currentBlackboxedSet = new Set(state.blackboxedSet);
     57 
     58      if (!currentBlackboxedRanges[source.url]) {
     59        currentBlackboxedRanges[source.url] = [];
     60        currentBlackboxedSet.add(source.url);
     61      } else {
     62        currentBlackboxedRanges[source.url] = [
     63          ...state.blackboxedRanges[source.url],
     64        ];
     65      }
     66 
     67      // Add new blackboxed lines in acsending order
     68      for (const newRange of ranges) {
     69        const index = currentBlackboxedRanges[source.url].findIndex(
     70          range =>
     71            range.end.line <= newRange.start.line &&
     72            range.end.column <= newRange.start.column
     73        );
     74        currentBlackboxedRanges[source.url].splice(index + 1, 0, newRange);
     75      }
     76 
     77      return {
     78        ...state,
     79        blackboxedRanges: currentBlackboxedRanges,
     80        blackboxedSet: currentBlackboxedSet,
     81      };
     82    }
     83 
     84    case "UNBLACKBOX_WHOLE_SOURCES": {
     85      const { sources } = action;
     86 
     87      const currentBlackboxedRanges = { ...state.blackboxedRanges };
     88      const currentBlackboxedSet = new Set(state.blackboxedSet);
     89 
     90      for (const source of sources) {
     91        delete currentBlackboxedRanges[source.url];
     92        currentBlackboxedSet.delete(source.url);
     93      }
     94 
     95      return {
     96        ...state,
     97        blackboxedRanges: currentBlackboxedRanges,
     98        blackboxedSet: currentBlackboxedSet,
     99      };
    100    }
    101 
    102    case "UNBLACKBOX_SOURCE_RANGES": {
    103      const { source, ranges } = action;
    104 
    105      const currentBlackboxedRanges = {
    106        ...state.blackboxedRanges,
    107        [source.url]: [...state.blackboxedRanges[source.url]],
    108      };
    109 
    110      for (const newRange of ranges) {
    111        const index = currentBlackboxedRanges[source.url].findIndex(
    112          range =>
    113            range.start.line === newRange.start.line &&
    114            range.end.line === newRange.end.line
    115        );
    116 
    117        if (index !== -1) {
    118          currentBlackboxedRanges[source.url].splice(index, 1);
    119        }
    120      }
    121 
    122      return {
    123        ...state,
    124        blackboxedRanges: currentBlackboxedRanges,
    125      };
    126    }
    127 
    128    case "ADD_SOURCEMAP_IGNORE_LIST_SOURCES": {
    129      return {
    130        ...state,
    131        sourceMapIgnoreListUrls: [
    132          ...state.sourceMapIgnoreListUrls,
    133          ...action.ignoreListUrls,
    134        ],
    135      };
    136    }
    137 
    138    case "NAVIGATE":
    139      return initialSourceBlackBoxState(state);
    140  }
    141 
    142  return state;
    143 }
    144 
    145 export default update;