diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-11-05 17:16:09 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-11-05 17:16:09 +0800 |
| commit | d1d9f00b1bd660842addf5e6b1d1b64c852a9eea (patch) | |
| tree | 60fb37ae73a1109d95301de5ec9b61b5726a4b61 /node_modules/source-map/lib/util.js | |
| parent | 7c85166c4cbdd862fc4252007d2df5befcbc2a58 (diff) | |
added hover prefixer
Diffstat (limited to 'node_modules/source-map/lib/util.js')
| -rw-r--r-- | node_modules/source-map/lib/util.js | 225 |
1 files changed, 177 insertions, 48 deletions
diff --git a/node_modules/source-map/lib/util.js b/node_modules/source-map/lib/util.js index 44e0e452..35bd93d6 100644 --- a/node_modules/source-map/lib/util.js +++ b/node_modules/source-map/lib/util.js @@ -20,17 +20,17 @@ function getArg(aArgs, aName, aDefaultValue) { return aArgs[aName]; } else if (arguments.length === 3) { return aDefaultValue; - } else { - throw new Error('"' + aName + '" is a required argument.'); } + throw new Error('"' + aName + '" is a required argument.'); + } exports.getArg = getArg; -var urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.]*)(?::(\d+))?(\S*)$/; -var dataUrlRegexp = /^data:.+\,.+$/; +const urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.-]*)(?::(\d+))?(.*)$/; +const dataUrlRegexp = /^data:.+\,.+$/; function urlParse(aUrl) { - var match = aUrl.match(urlRegexp); + const match = aUrl.match(urlRegexp); if (!match) { return null; } @@ -45,19 +45,19 @@ function urlParse(aUrl) { exports.urlParse = urlParse; function urlGenerate(aParsedUrl) { - var url = ''; + let url = ""; if (aParsedUrl.scheme) { - url += aParsedUrl.scheme + ':'; + url += aParsedUrl.scheme + ":"; } - url += '//'; + url += "//"; if (aParsedUrl.auth) { - url += aParsedUrl.auth + '@'; + url += aParsedUrl.auth + "@"; } if (aParsedUrl.host) { url += aParsedUrl.host; } if (aParsedUrl.port) { - url += ":" + aParsedUrl.port + url += ":" + aParsedUrl.port; } if (aParsedUrl.path) { url += aParsedUrl.path; @@ -66,6 +66,43 @@ function urlGenerate(aParsedUrl) { } exports.urlGenerate = urlGenerate; +const MAX_CACHED_INPUTS = 32; + +/** + * Takes some function `f(input) -> result` and returns a memoized version of + * `f`. + * + * We keep at most `MAX_CACHED_INPUTS` memoized results of `f` alive. The + * memoization is a dumb-simple, linear least-recently-used cache. + */ +function lruMemoize(f) { + const cache = []; + + return function(input) { + for (let i = 0; i < cache.length; i++) { + if (cache[i].input === input) { + const temp = cache[0]; + cache[0] = cache[i]; + cache[i] = temp; + return cache[0].result; + } + } + + const result = f(input); + + cache.unshift({ + input, + result, + }); + + if (cache.length > MAX_CACHED_INPUTS) { + cache.pop(); + } + + return result; + }; +} + /** * Normalizes a path, or the path portion of a URL: * @@ -77,26 +114,45 @@ exports.urlGenerate = urlGenerate; * * @param aPath The path or url to normalize. */ -function normalize(aPath) { - var path = aPath; - var url = urlParse(aPath); +const normalize = lruMemoize(function normalize(aPath) { + let path = aPath; + const url = urlParse(aPath); if (url) { if (!url.path) { return aPath; } path = url.path; } - var isAbsolute = exports.isAbsolute(path); + const isAbsolute = exports.isAbsolute(path); + + // Split the path into parts between `/` characters. This is much faster than + // using `.split(/\/+/g)`. + const parts = []; + let start = 0; + let i = 0; + while (true) { + start = i; + i = path.indexOf("/", start); + if (i === -1) { + parts.push(path.slice(start)); + break; + } else { + parts.push(path.slice(start, i)); + while (i < path.length && path[i] === "/") { + i++; + } + } + } - var parts = path.split(/\/+/); - for (var part, up = 0, i = parts.length - 1; i >= 0; i--) { - part = parts[i]; - if (part === '.') { + let up = 0; + for (i = parts.length - 1; i >= 0; i--) { + const part = parts[i]; + if (part === ".") { parts.splice(i, 1); - } else if (part === '..') { + } else if (part === "..") { up++; } else if (up > 0) { - if (part === '') { + if (part === "") { // The first part is blank if the path is absolute. Trying to go // above the root is a no-op. Therefore we can remove all '..' parts // directly after the root. @@ -108,10 +164,10 @@ function normalize(aPath) { } } } - path = parts.join('/'); + path = parts.join("/"); - if (path === '') { - path = isAbsolute ? '/' : '.'; + if (path === "") { + path = isAbsolute ? "/" : "."; } if (url) { @@ -119,7 +175,7 @@ function normalize(aPath) { return urlGenerate(url); } return path; -} +}); exports.normalize = normalize; /** @@ -145,10 +201,10 @@ function join(aRoot, aPath) { if (aPath === "") { aPath = "."; } - var aPathUrl = urlParse(aPath); - var aRootUrl = urlParse(aRoot); + const aPathUrl = urlParse(aPath); + const aRootUrl = urlParse(aRoot); if (aRootUrl) { - aRoot = aRootUrl.path || '/'; + aRoot = aRootUrl.path || "/"; } // `join(foo, '//www.example.org')` @@ -169,9 +225,9 @@ function join(aRoot, aPath) { return urlGenerate(aRootUrl); } - var joined = aPath.charAt(0) === '/' + const joined = aPath.charAt(0) === "/" ? aPath - : normalize(aRoot.replace(/\/+$/, '') + '/' + aPath); + : normalize(aRoot.replace(/\/+$/, "") + "/" + aPath); if (aRootUrl) { aRootUrl.path = joined; @@ -181,8 +237,8 @@ function join(aRoot, aPath) { } exports.join = join; -exports.isAbsolute = function (aPath) { - return aPath.charAt(0) === '/' || !!aPath.match(urlRegexp); +exports.isAbsolute = function(aPath) { + return aPath.charAt(0) === "/" || urlRegexp.test(aPath); }; /** @@ -196,15 +252,15 @@ function relative(aRoot, aPath) { aRoot = "."; } - aRoot = aRoot.replace(/\/$/, ''); + aRoot = aRoot.replace(/\/$/, ""); // It is possible for the path to be above the root. In this case, simply // checking whether the root is a prefix of the path won't work. Instead, we // need to remove components from the root one by one, until either we find // a prefix that fits, or we run out of components to remove. - var level = 0; - while (aPath.indexOf(aRoot + '/') !== 0) { - var index = aRoot.lastIndexOf("/"); + let level = 0; + while (aPath.indexOf(aRoot + "/") !== 0) { + const index = aRoot.lastIndexOf("/"); if (index < 0) { return aPath; } @@ -225,12 +281,12 @@ function relative(aRoot, aPath) { } exports.relative = relative; -var supportsNullProto = (function () { - var obj = Object.create(null); - return !('__proto__' in obj); +const supportsNullProto = (function() { + const obj = Object.create(null); + return !("__proto__" in obj); }()); -function identity (s) { +function identity(s) { return s; } @@ -245,7 +301,7 @@ function identity (s) { */ function toSetString(aStr) { if (isProtoString(aStr)) { - return '$' + aStr; + return "$" + aStr; } return aStr; @@ -266,12 +322,13 @@ function isProtoString(s) { return false; } - var length = s.length; + const length = s.length; if (length < 9 /* "__proto__".length */) { return false; } + /* eslint-disable no-multi-spaces */ if (s.charCodeAt(length - 1) !== 95 /* '_' */ || s.charCodeAt(length - 2) !== 95 /* '_' */ || s.charCodeAt(length - 3) !== 111 /* 'o' */ || @@ -283,8 +340,9 @@ function isProtoString(s) { s.charCodeAt(length - 9) !== 95 /* '_' */) { return false; } + /* eslint-enable no-multi-spaces */ - for (var i = length - 10; i >= 0; i--) { + for (let i = length - 10; i >= 0; i--) { if (s.charCodeAt(i) !== 36 /* '$' */) { return false; } @@ -302,7 +360,7 @@ function isProtoString(s) { * stubbed out mapping. */ function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) { - var cmp = mappingA.source - mappingB.source; + let cmp = strcmp(mappingA.source, mappingB.source); if (cmp !== 0) { return cmp; } @@ -327,7 +385,7 @@ function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) { return cmp; } - return mappingA.name - mappingB.name; + return strcmp(mappingA.name, mappingB.name); } exports.compareByOriginalPositions = compareByOriginalPositions; @@ -341,7 +399,7 @@ exports.compareByOriginalPositions = compareByOriginalPositions; * mapping with a stubbed out mapping. */ function compareByGeneratedPositionsDeflated(mappingA, mappingB, onlyCompareGenerated) { - var cmp = mappingA.generatedLine - mappingB.generatedLine; + let cmp = mappingA.generatedLine - mappingB.generatedLine; if (cmp !== 0) { return cmp; } @@ -351,7 +409,7 @@ function compareByGeneratedPositionsDeflated(mappingA, mappingB, onlyCompareGene return cmp; } - cmp = mappingA.source - mappingB.source; + cmp = strcmp(mappingA.source, mappingB.source); if (cmp !== 0) { return cmp; } @@ -366,7 +424,7 @@ function compareByGeneratedPositionsDeflated(mappingA, mappingB, onlyCompareGene return cmp; } - return mappingA.name - mappingB.name; + return strcmp(mappingA.name, mappingB.name); } exports.compareByGeneratedPositionsDeflated = compareByGeneratedPositionsDeflated; @@ -375,6 +433,14 @@ function strcmp(aStr1, aStr2) { return 0; } + if (aStr1 === null) { + return 1; // aStr2 !== null + } + + if (aStr2 === null) { + return -1; // aStr1 !== null + } + if (aStr1 > aStr2) { return 1; } @@ -387,7 +453,7 @@ function strcmp(aStr1, aStr2) { * the generated positions are compared. */ function compareByGeneratedPositionsInflated(mappingA, mappingB) { - var cmp = mappingA.generatedLine - mappingB.generatedLine; + let cmp = mappingA.generatedLine - mappingB.generatedLine; if (cmp !== 0) { return cmp; } @@ -415,3 +481,66 @@ function compareByGeneratedPositionsInflated(mappingA, mappingB) { return strcmp(mappingA.name, mappingB.name); } exports.compareByGeneratedPositionsInflated = compareByGeneratedPositionsInflated; + +/** + * Strip any JSON XSSI avoidance prefix from the string (as documented + * in the source maps specification), and then parse the string as + * JSON. + */ +function parseSourceMapInput(str) { + return JSON.parse(str.replace(/^\)]}'[^\n]*\n/, "")); +} +exports.parseSourceMapInput = parseSourceMapInput; + +/** + * Compute the URL of a source given the the source root, the source's + * URL, and the source map's URL. + */ +function computeSourceURL(sourceRoot, sourceURL, sourceMapURL) { + sourceURL = sourceURL || ""; + + if (sourceRoot) { + // This follows what Chrome does. + if (sourceRoot[sourceRoot.length - 1] !== "/" && sourceURL[0] !== "/") { + sourceRoot += "/"; + } + // The spec says: + // Line 4: An optional source root, useful for relocating source + // files on a server or removing repeated values in the + // “sources” entry. This value is prepended to the individual + // entries in the “source” field. + sourceURL = sourceRoot + sourceURL; + } + + // Historically, SourceMapConsumer did not take the sourceMapURL as + // a parameter. This mode is still somewhat supported, which is why + // this code block is conditional. However, it's preferable to pass + // the source map URL to SourceMapConsumer, so that this function + // can implement the source URL resolution algorithm as outlined in + // the spec. This block is basically the equivalent of: + // new URL(sourceURL, sourceMapURL).toString() + // ... except it avoids using URL, which wasn't available in the + // older releases of node still supported by this library. + // + // The spec says: + // If the sources are not absolute URLs after prepending of the + // “sourceRoot”, the sources are resolved relative to the + // SourceMap (like resolving script src in a html document). + if (sourceMapURL) { + const parsed = urlParse(sourceMapURL); + if (!parsed) { + throw new Error("sourceMapURL could not be parsed"); + } + if (parsed.path) { + // Strip the last path component, but keep the "/". + const index = parsed.path.lastIndexOf("/"); + if (index >= 0) { + parsed.path = parsed.path.substring(0, index + 1); + } + } + sourceURL = join(urlGenerate(parsed), sourceURL); + } + + return normalize(sourceURL); +} +exports.computeSourceURL = computeSourceURL; |
