source-map-utils.js (1437B)
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 exports.getSourcemapBaseURL = getSourcemapBaseURL; 8 function getSourcemapBaseURL(url, global) { 9 let sourceMapBaseURL = null; 10 if (url) { 11 // Sources that have explicit URLs can be used directly as the base. 12 sourceMapBaseURL = url; 13 } else if (global?.location?.href) { 14 // If there is no URL for the source, the map comment is relative to the 15 // page being viewed, so we use the document href. 16 sourceMapBaseURL = global.location.href; 17 } else { 18 // If there is no valid base, the sourcemap URL will need to be an absolute 19 // URL of some kind. 20 return null; 21 } 22 23 // A data URL is large and will never be a valid base, so we can just treat 24 // it as if there is no base at all to avoid a sending it to the client 25 // for no reason. 26 if (sourceMapBaseURL.startsWith("data:")) { 27 return null; 28 } 29 30 // If the base URL is a blob, we want to resolve relative to the origin 31 // that created the blob URL, if there is one. 32 if (sourceMapBaseURL.startsWith("blob:")) { 33 const parsedBaseURL = URL.parse(sourceMapBaseURL); 34 if (parsedBaseURL) { 35 return parsedBaseURL.origin === "null" ? null : parsedBaseURL.origin; 36 } 37 return null; 38 } 39 40 return sourceMapBaseURL; 41 }