tor-browser

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

worker.js (2481B)


      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 { workerHandler } from "../../../../shared/worker-utils";
      6 import { prettyFast } from "./pretty-fast";
      7 
      8 var { SourceMapGenerator } = require("source-map");
      9 
     10 const sourceMapGeneratorByTaskId = new Map();
     11 
     12 function prettyPrint({ url, indent, sourceText }) {
     13  const { code, map: sourceMapGenerator } = prettyFast(sourceText, {
     14    url,
     15    indent,
     16  });
     17 
     18  return {
     19    code,
     20    sourceMap: sourceMapGenerator.toJSON(),
     21  };
     22 }
     23 
     24 function prettyPrintInlineScript({
     25  taskId,
     26  url,
     27  indent,
     28  sourceText,
     29  originalStartLine,
     30  originalStartColumn,
     31  generatedStartLine,
     32 }) {
     33  let taskSourceMapGenerator;
     34  if (!sourceMapGeneratorByTaskId.has(taskId)) {
     35    taskSourceMapGenerator = new SourceMapGenerator({ file: url });
     36    sourceMapGeneratorByTaskId.set(taskId, taskSourceMapGenerator);
     37  } else {
     38    taskSourceMapGenerator = sourceMapGeneratorByTaskId.get(taskId);
     39  }
     40 
     41  const { code } = prettyFast(sourceText, {
     42    url,
     43    indent,
     44    sourceMapGenerator: taskSourceMapGenerator,
     45    /*
     46     * By default prettyPrint will trim the text, and we'd have the pretty text displayed
     47     * just after the script tag, e.g.:
     48     *
     49     * ```
     50     * <script>if (true) {
     51     *   something()
     52     * }
     53     * </script>
     54     * ```
     55     *
     56     * We want the text to start on a new line, so prepend a line break, so we get
     57     * something like:
     58     *
     59     * ```
     60     * <script>
     61     * if (true) {
     62     *   something()
     63     * }
     64     * </script>
     65     * ```
     66     */
     67    prefixWithNewLine: true,
     68    originalStartLine,
     69    originalStartColumn,
     70    generatedStartLine,
     71  });
     72 
     73  // When a taskId was passed, we only return the pretty printed text.
     74  // The source map should be retrieved with getSourceMapForTask.
     75  return code;
     76 }
     77 
     78 /**
     79 * Get the source map for a pretty-print task
     80 *
     81 * @param {Integer} taskId: The taskId that was used to call prettyPrint
     82 * @returns {object} A source map object
     83 */
     84 function getSourceMapForTask(taskId) {
     85  if (!sourceMapGeneratorByTaskId.has(taskId)) {
     86    return null;
     87  }
     88 
     89  const taskSourceMapGenerator = sourceMapGeneratorByTaskId.get(taskId);
     90  sourceMapGeneratorByTaskId.delete(taskId);
     91  return taskSourceMapGenerator.toJSON();
     92 }
     93 
     94 self.onmessage = workerHandler({
     95  prettyPrint,
     96  prettyPrintInlineScript,
     97  getSourceMapForTask,
     98 });