tor-browser

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

resourceUriPlugin.js (2702B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This plugin supports finding files with particular resource:// URIs
      6 // and translating the uri into a relative filesytem path where the file may be
      7 // found when running within the Karma / Mocha test framework.
      8 
      9 /* eslint-env node */
     10 const path = require("path");
     11 
     12 module.exports = {
     13  ResourceUriPlugin: class ResourceUriPlugin {
     14    /**
     15     * @typedef {RegEx} ResourceReplacement0
     16     *   A regular expression matching a resource:// URI substring to be
     17     *   replaced.
     18     * @typedef {string} ResourceReplacement1
     19     *   A string to replace the matched substring with.
     20     * @typedef {[ResourceReplacement0, ResourceReplacement1]} ResourceReplacement
     21     */
     22 
     23    /**
     24     * Maps regular expressions representing resource URIs to strings that
     25     * should replace matches for those regular expressions.
     26     *
     27     * @type {ResourceReplacement[]}
     28     */
     29    #resourcePathRegExes;
     30 
     31    /**
     32     * @param {object} options
     33     *  Object passed during the instantiation of ResourceUriPlugin
     34     * @param {ResourceReplacement[]} options.resourcePathRegExes
     35     *   An array of regex/string tuples to perform replacements on for
     36     *   imports involving resource:// URIs.
     37     */
     38    constructor({ resourcePathRegExes }) {
     39      this.#resourcePathRegExes = resourcePathRegExes;
     40    }
     41 
     42    apply(compiler) {
     43      compiler.hooks.compilation.tap(
     44        "ResourceUriPlugin",
     45        (compilation, { normalModuleFactory }) => {
     46          normalModuleFactory.hooks.resolveForScheme
     47            .for("resource")
     48            .tap("ResourceUriPlugin", resourceData => {
     49              const url = new URL(resourceData.resource);
     50 
     51              for (let [regex, replacement] of this.#resourcePathRegExes) {
     52                if (!url.href.match(regex)) {
     53                  continue;
     54                }
     55                // path.join() is necessary to normalize the path on Windows.
     56                // Without it, the path may contain backslashes, resulting in
     57                // different build output on Windows than on Unix systems.
     58                const pathname = path.join(
     59                  url.href.replace(regex, replacement)
     60                );
     61                resourceData.path = pathname;
     62                resourceData.query = url.search;
     63                resourceData.fragment = url.hash;
     64                resourceData.resource = pathname + url.search + url.hash;
     65                return true;
     66              }
     67 
     68              return true;
     69            });
     70        }
     71      );
     72    }
     73  },
     74 };