forked from mirrors/gecko-dev
This used to be a node package. Let's make it become regular commonjs devtools modules. This will make it trivial to migrate this to ES Modules. Also possibly make this code become the unique layer in m-c on top of the source-map package. We no longer use webpack to build the two bundles (index.js and worker.js), instead, we are using the toolkit worker loader (require.js) in order to load all this code without any build step. As this is no longer a node package, I removed node-specific modules (assertRoot/wasmAsset) and simplify the definition of wasm file URIs as they are now fixed. Also moving the debugger to load internal "devtools/client/shared/source-map/source-map.js" module in jest as running the Web Worker instantiated by source-map/index.js is too complex. Differential Revision: https://phabricator.services.mozilla.com/D159115
103 lines
2.2 KiB
JavaScript
103 lines
2.2 KiB
JavaScript
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
|
|
|
|
"use strict";
|
|
|
|
const md5 = require("resource://devtools/client/shared/vendor/md5.js");
|
|
|
|
function originalToGeneratedId(sourceId) {
|
|
if (isGeneratedId(sourceId)) {
|
|
return sourceId;
|
|
}
|
|
|
|
const lastIndex = sourceId.lastIndexOf("/originalSource");
|
|
|
|
return lastIndex !== -1 ? sourceId.slice(0, lastIndex) : "";
|
|
}
|
|
|
|
const getMd5 = memoize(url => md5(url));
|
|
|
|
function generatedToOriginalId(generatedId, url) {
|
|
return `${generatedId}/originalSource-${getMd5(url)}`;
|
|
}
|
|
|
|
function isOriginalId(id) {
|
|
return id.includes("/originalSource");
|
|
}
|
|
|
|
function isGeneratedId(id) {
|
|
return !isOriginalId(id);
|
|
}
|
|
|
|
/**
|
|
* Trims the query part or reference identifier of a URL string, if necessary.
|
|
*/
|
|
function trimUrlQuery(url) {
|
|
const length = url.length;
|
|
|
|
for (let i = 0; i < length; ++i) {
|
|
if (url[i] === "?" || url[i] === "&" || url[i] === "#") {
|
|
return url.slice(0, i);
|
|
}
|
|
}
|
|
|
|
return url;
|
|
}
|
|
|
|
// Map suffix to content type.
|
|
const contentMap = {
|
|
js: "text/javascript",
|
|
jsm: "text/javascript",
|
|
mjs: "text/javascript",
|
|
ts: "text/typescript",
|
|
tsx: "text/typescript-jsx",
|
|
jsx: "text/jsx",
|
|
vue: "text/vue",
|
|
coffee: "text/coffeescript",
|
|
elm: "text/elm",
|
|
cljc: "text/x-clojure",
|
|
cljs: "text/x-clojurescript",
|
|
};
|
|
|
|
/**
|
|
* Returns the content type for the specified URL. If no specific
|
|
* content type can be determined, "text/plain" is returned.
|
|
*
|
|
* @return String
|
|
* The content type.
|
|
*/
|
|
function getContentType(url) {
|
|
url = trimUrlQuery(url);
|
|
const dot = url.lastIndexOf(".");
|
|
if (dot >= 0) {
|
|
const name = url.substring(dot + 1);
|
|
if (name in contentMap) {
|
|
return contentMap[name];
|
|
}
|
|
}
|
|
return "text/plain";
|
|
}
|
|
|
|
function memoize(func) {
|
|
const map = new Map();
|
|
|
|
return arg => {
|
|
if (map.has(arg)) {
|
|
return map.get(arg);
|
|
}
|
|
|
|
const result = func(arg);
|
|
map.set(arg, result);
|
|
return result;
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
originalToGeneratedId,
|
|
generatedToOriginalId,
|
|
isOriginalId,
|
|
isGeneratedId,
|
|
getContentType,
|
|
contentMapForTesting: contentMap,
|
|
};
|