tor-browser

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

index.js (2227B)


      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 md5 = require("resource://devtools/client/shared/vendor/md5.js");
      8 
      9 function originalToGeneratedId(sourceId) {
     10  if (isGeneratedId(sourceId)) {
     11    return sourceId;
     12  }
     13 
     14  const lastIndex = sourceId.lastIndexOf("/originalSource");
     15 
     16  return lastIndex !== -1 ? sourceId.slice(0, lastIndex) : "";
     17 }
     18 
     19 const getMd5 = memoize(url => md5(url));
     20 
     21 function generatedToOriginalId(generatedId, url) {
     22  return `${generatedId}/originalSource-${getMd5(url)}`;
     23 }
     24 
     25 function isOriginalId(id) {
     26  return id.includes("/originalSource");
     27 }
     28 
     29 function isGeneratedId(id) {
     30  return !isOriginalId(id);
     31 }
     32 
     33 /**
     34 * Trims the query part or reference identifier of a URL string, if necessary.
     35 */
     36 function trimUrlQuery(url) {
     37  const length = url.length;
     38 
     39  for (let i = 0; i < length; ++i) {
     40    if (url[i] === "?" || url[i] === "&" || url[i] === "#") {
     41      return url.slice(0, i);
     42    }
     43  }
     44 
     45  return url;
     46 }
     47 
     48 // Map suffix to content type.
     49 const contentMap = {
     50  js: "text/javascript",
     51  mjs: "text/javascript",
     52  ts: "text/typescript",
     53  tsx: "text/typescript-jsx",
     54  jsx: "text/jsx",
     55  vue: "text/vue",
     56  coffee: "text/coffeescript",
     57  elm: "text/elm",
     58  cljc: "text/x-clojure",
     59  cljs: "text/x-clojurescript",
     60 };
     61 
     62 /**
     63 * Returns the content type for the specified URL.  If no specific
     64 * content type can be determined, "text/plain" is returned.
     65 *
     66 * @return String
     67 *         The content type.
     68 */
     69 function getContentType(url) {
     70  url = trimUrlQuery(url);
     71  const dot = url.lastIndexOf(".");
     72  if (dot >= 0) {
     73    const name = url.substring(dot + 1);
     74    if (name in contentMap) {
     75      return contentMap[name];
     76    }
     77  }
     78  return "text/plain";
     79 }
     80 
     81 function memoize(func) {
     82  const map = new Map();
     83 
     84  return arg => {
     85    if (map.has(arg)) {
     86      return map.get(arg);
     87    }
     88 
     89    const result = func(arg);
     90    map.set(arg, result);
     91    return result;
     92  };
     93 }
     94 
     95 module.exports = {
     96  originalToGeneratedId,
     97  generatedToOriginalId,
     98  isOriginalId,
     99  isGeneratedId,
    100  getContentType,
    101  contentMapForTesting: contentMap,
    102 };