tor-browser

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

sourceMapRequests.js (3080B)


      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 "use strict";
      6 
      7 const {
      8  generatedToOriginalId,
      9 } = require("resource://devtools/client/shared/source-map-loader/utils/index.js");
     10 
     11 const sourceMapRequests = new Map();
     12 
     13 function clearSourceMaps() {
     14  for (const [, metadataPromise] of sourceMapRequests) {
     15    // The source-map module leaks memory unless `.destroy` is called on
     16    // the consumer instances when they are no longer being used.
     17    metadataPromise.then(
     18      metadata => {
     19        if (metadata) {
     20          metadata.map.destroy();
     21        }
     22      },
     23      // We don't want this to cause any unhandled rejection errors.
     24      () => {}
     25    );
     26  }
     27 
     28  sourceMapRequests.clear();
     29 }
     30 
     31 /**
     32 * For a given generated source, retrieve an object with many attributes:
     33 *
     34 * @param {string} generatedSourceId
     35 *        The id of the generated source
     36 *
     37 * @return {object} Meta data object with many attributes
     38 *     - map: The SourceMapConsumer or WasmRemap instance
     39 *     - urlsById Map of Original Source ID (string) to Source URL (string)
     40 *     - sources: Array of object with the two following attributes:
     41 *       - id: Original Source ID (string)
     42 *       - url: Original Source URL (string)
     43 */
     44 function getSourceMapWithMetadata(generatedSourceId) {
     45  return sourceMapRequests.get(generatedSourceId);
     46 }
     47 
     48 /**
     49 * Retrieve the SourceMapConsumer or WasmRemap instance for a given generated source.
     50 *
     51 * @param {string} generatedSourceId
     52 *        The id of the generated source
     53 *
     54 * @return null | Promise<SourceMapConsumer | WasmRemap>
     55 */
     56 function getSourceMap(generatedSourceId) {
     57  const request = getSourceMapWithMetadata(generatedSourceId);
     58  if (!request) {
     59    return null;
     60  }
     61 
     62  return request.then(result => result?.map);
     63 }
     64 
     65 /**
     66 * Record the SourceMapConsumer or WasmRemap instance for a given generated source.
     67 *
     68 * @param {string} generatedId
     69 *        The generated source ID.
     70 * @param {Promise<SourceMapConsumer or WasmRemap>} request
     71 *        A promise which should resolve to either a SourceMapConsume or WasmRemap instance.
     72 */
     73 function setSourceMap(generatedId, request) {
     74  sourceMapRequests.set(
     75    generatedId,
     76    request.then(map => {
     77      if (!map || !map.sources) {
     78        return null;
     79      }
     80 
     81      const urlsById = new Map();
     82 
     83      for (const url of map.sources) {
     84        const id = generatedToOriginalId(generatedId, url);
     85        urlsById.set(id, url);
     86      }
     87      return { map, urlsById };
     88    })
     89  );
     90 }
     91 
     92 /**
     93 * Clear any existing SourceMapConsumer or WasmRemap instance for a given list of generated source.
     94 *
     95 * @param {Array<string>} generatedIds
     96 *        The generated source ID's.
     97 */
     98 function clearSourceMapForSources(generatedIds) {
     99  for (const generatedId of generatedIds) {
    100    sourceMapRequests.delete(generatedId);
    101  }
    102 }
    103 
    104 module.exports = {
    105  clearSourceMaps,
    106  getSourceMapWithMetadata,
    107  getSourceMap,
    108  setSourceMap,
    109  clearSourceMapForSources,
    110 };