moz-uri-utils.js (1382B)
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 /* eslint-env node */ 5 6 const path = require("path"); 7 const [prefixMap, aliasMap, sourceMap] = require("./chrome-map.js"); 8 const projectRoot = path.resolve(__dirname, "../../../../"); 9 10 function rewriteChromeUri(uri) { 11 if (uri in aliasMap) { 12 return rewriteChromeUri(aliasMap[uri]); 13 } 14 for (let [prefix, [bundlePath]] of Object.entries(prefixMap)) { 15 if (uri.startsWith(prefix)) { 16 if (!bundlePath.endsWith("/")) { 17 bundlePath += "/"; 18 } 19 let relativePath = uri.slice(prefix.length); 20 let objdirPath = bundlePath + relativePath; 21 for (let [_objdirPath, [filePath]] of Object.entries(sourceMap)) { 22 if (_objdirPath == objdirPath) { 23 // We're just hoping this is the actual path =\ 24 return filePath; 25 } 26 } 27 } 28 } 29 return ""; 30 } 31 32 function rewriteMozSrcUri(uri) { 33 if (!uri.startsWith("moz-src:///")) { 34 return ""; 35 } 36 const relativePath = uri.replace(/^moz-src:\/\/\//, ""); 37 const resolvedPath = path.resolve(projectRoot, relativePath); 38 if (!resolvedPath.startsWith(projectRoot)) { 39 return ""; 40 } 41 return resolvedPath; 42 } 43 44 module.exports = { 45 rewriteChromeUri, 46 rewriteMozSrcUri, 47 };