tor-browser

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

annotateFrames.js (2086B)


      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 { getLibraryFromUrl } from "./getLibraryFromUrl";
      6 
      7 /**
      8 * Augment all frame objects with a 'library' attribute.
      9 */
     10 export function annotateFramesWithLibrary(frames) {
     11  for (const frame of frames) {
     12    frame.library = getLibraryFromUrl(frame, frames);
     13  }
     14 
     15  // Babel need some special treatment to recognize some particular async stack pattern
     16  for (const idx of getBabelFrameIndexes(frames)) {
     17    const frame = frames[idx];
     18    frame.library = "Babel";
     19  }
     20 }
     21 
     22 /**
     23 * Returns all the indexes that are part of a babel async call stack.
     24 *
     25 * @param {Array<object>} frames
     26 * @returns Array<Integer>
     27 */
     28 function getBabelFrameIndexes(frames) {
     29  const startIndexes = [];
     30  const endIndexes = [];
     31 
     32  for (let index = 0, length = frames.length; index < length; index++) {
     33    const frame = frames[index];
     34    const frameUrl = frame.location.source.url;
     35 
     36    if (
     37      frame.displayName === "tryCatch" &&
     38      frameUrl.match(/regenerator-runtime/i)
     39    ) {
     40      startIndexes.push(index);
     41    }
     42 
     43    if (startIndexes.length > endIndexes.length) {
     44      if (frame.displayName === "flush" && frameUrl.match(/_microtask/i)) {
     45        endIndexes.push(index);
     46      }
     47      if (frame.displayName === "_asyncToGenerator/<") {
     48        endIndexes.push(index + 1);
     49      }
     50    }
     51  }
     52 
     53  if (startIndexes.length != endIndexes.length || startIndexes.length === 0) {
     54    return [];
     55  }
     56 
     57  const babelFrameIndexes = [];
     58  // We have the same number of start and end indexes, we can loop through one of them to
     59  // build our async call stack index ranges
     60  // e.g. if we have startIndexes: [1,5] and endIndexes: [3,8], we want to return [1,2,3,5,6,7,8]
     61  startIndexes.forEach((startIndex, index) => {
     62    const matchingEndIndex = endIndexes[index];
     63    for (let i = startIndex; i <= matchingEndIndex; i++) {
     64      babelFrameIndexes.push(i);
     65    }
     66  });
     67  return babelFrameIndexes;
     68 }