util.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. const URL = require("./url");
  8. /**
  9. * This is a helper function for getting values from parameter/options
  10. * objects.
  11. *
  12. * @param args The object we are extracting values from
  13. * @param name The name of the property we are getting.
  14. * @param defaultValue An optional value to return if the property is missing
  15. * from the object. If this is not specified and the property is missing, an
  16. * error will be thrown.
  17. */
  18. function getArg(aArgs, aName, aDefaultValue) {
  19. if (aName in aArgs) {
  20. return aArgs[aName];
  21. } else if (arguments.length === 3) {
  22. return aDefaultValue;
  23. }
  24. throw new Error('"' + aName + '" is a required argument.');
  25. }
  26. exports.getArg = getArg;
  27. const supportsNullProto = (function () {
  28. const obj = Object.create(null);
  29. return !("__proto__" in obj);
  30. })();
  31. function identity(s) {
  32. return s;
  33. }
  34. /**
  35. * Because behavior goes wacky when you set `__proto__` on objects, we
  36. * have to prefix all the strings in our set with an arbitrary character.
  37. *
  38. * See https://github.com/mozilla/source-map/pull/31 and
  39. * https://github.com/mozilla/source-map/issues/30
  40. *
  41. * @param String aStr
  42. */
  43. function toSetString(aStr) {
  44. if (isProtoString(aStr)) {
  45. return "$" + aStr;
  46. }
  47. return aStr;
  48. }
  49. exports.toSetString = supportsNullProto ? identity : toSetString;
  50. function fromSetString(aStr) {
  51. if (isProtoString(aStr)) {
  52. return aStr.slice(1);
  53. }
  54. return aStr;
  55. }
  56. exports.fromSetString = supportsNullProto ? identity : fromSetString;
  57. function isProtoString(s) {
  58. if (!s) {
  59. return false;
  60. }
  61. const length = s.length;
  62. if (length < 9 /* "__proto__".length */) {
  63. return false;
  64. }
  65. /* eslint-disable no-multi-spaces */
  66. if (
  67. s.charCodeAt(length - 1) !== 95 /* '_' */ ||
  68. s.charCodeAt(length - 2) !== 95 /* '_' */ ||
  69. s.charCodeAt(length - 3) !== 111 /* 'o' */ ||
  70. s.charCodeAt(length - 4) !== 116 /* 't' */ ||
  71. s.charCodeAt(length - 5) !== 111 /* 'o' */ ||
  72. s.charCodeAt(length - 6) !== 114 /* 'r' */ ||
  73. s.charCodeAt(length - 7) !== 112 /* 'p' */ ||
  74. s.charCodeAt(length - 8) !== 95 /* '_' */ ||
  75. s.charCodeAt(length - 9) !== 95 /* '_' */
  76. ) {
  77. return false;
  78. }
  79. /* eslint-enable no-multi-spaces */
  80. for (let i = length - 10; i >= 0; i--) {
  81. if (s.charCodeAt(i) !== 36 /* '$' */) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. function strcmp(aStr1, aStr2) {
  88. if (aStr1 === aStr2) {
  89. return 0;
  90. }
  91. if (aStr1 === null) {
  92. return 1; // aStr2 !== null
  93. }
  94. if (aStr2 === null) {
  95. return -1; // aStr1 !== null
  96. }
  97. if (aStr1 > aStr2) {
  98. return 1;
  99. }
  100. return -1;
  101. }
  102. /**
  103. * Comparator between two mappings with inflated source and name strings where
  104. * the generated positions are compared.
  105. */
  106. function compareByGeneratedPositionsInflated(mappingA, mappingB) {
  107. let cmp = mappingA.generatedLine - mappingB.generatedLine;
  108. if (cmp !== 0) {
  109. return cmp;
  110. }
  111. cmp = mappingA.generatedColumn - mappingB.generatedColumn;
  112. if (cmp !== 0) {
  113. return cmp;
  114. }
  115. cmp = strcmp(mappingA.source, mappingB.source);
  116. if (cmp !== 0) {
  117. return cmp;
  118. }
  119. cmp = mappingA.originalLine - mappingB.originalLine;
  120. if (cmp !== 0) {
  121. return cmp;
  122. }
  123. cmp = mappingA.originalColumn - mappingB.originalColumn;
  124. if (cmp !== 0) {
  125. return cmp;
  126. }
  127. return strcmp(mappingA.name, mappingB.name);
  128. }
  129. exports.compareByGeneratedPositionsInflated =
  130. compareByGeneratedPositionsInflated;
  131. /**
  132. * Strip any JSON XSSI avoidance prefix from the string (as documented
  133. * in the source maps specification), and then parse the string as
  134. * JSON.
  135. */
  136. function parseSourceMapInput(str) {
  137. return JSON.parse(str.replace(/^\)]}'[^\n]*\n/, ""));
  138. }
  139. exports.parseSourceMapInput = parseSourceMapInput;
  140. // We use 'http' as the base here because we want URLs processed relative
  141. // to the safe base to be treated as "special" URLs during parsing using
  142. // the WHATWG URL parsing. This ensures that backslash normalization
  143. // applies to the path and such.
  144. const PROTOCOL = "http:";
  145. const PROTOCOL_AND_HOST = `${PROTOCOL}//host`;
  146. /**
  147. * Make it easy to create small utilities that tweak a URL's path.
  148. */
  149. function createSafeHandler(cb) {
  150. return input => {
  151. const type = getURLType(input);
  152. const base = buildSafeBase(input);
  153. const url = new URL(input, base);
  154. cb(url);
  155. const result = url.toString();
  156. if (type === "absolute") {
  157. return result;
  158. } else if (type === "scheme-relative") {
  159. return result.slice(PROTOCOL.length);
  160. } else if (type === "path-absolute") {
  161. return result.slice(PROTOCOL_AND_HOST.length);
  162. }
  163. // This assumes that the callback will only change
  164. // the path, search and hash values.
  165. return computeRelativeURL(base, result);
  166. };
  167. }
  168. function withBase(url, base) {
  169. return new URL(url, base).toString();
  170. }
  171. function buildUniqueSegment(prefix, str) {
  172. let id = 0;
  173. do {
  174. const ident = prefix + id++;
  175. if (str.indexOf(ident) === -1) return ident;
  176. } while (true);
  177. }
  178. function buildSafeBase(str) {
  179. const maxDotParts = str.split("..").length - 1;
  180. // If we used a segment that also existed in `str`, then we would be unable
  181. // to compute relative paths. For example, if `segment` were just "a":
  182. //
  183. // const url = "../../a/"
  184. // const base = buildSafeBase(url); // http://host/a/a/
  185. // const joined = "http://host/a/";
  186. // const result = relative(base, joined);
  187. //
  188. // Expected: "../../a/";
  189. // Actual: "a/"
  190. //
  191. const segment = buildUniqueSegment("p", str);
  192. let base = `${PROTOCOL_AND_HOST}/`;
  193. for (let i = 0; i < maxDotParts; i++) {
  194. base += `${segment}/`;
  195. }
  196. return base;
  197. }
  198. const ABSOLUTE_SCHEME = /^[A-Za-z0-9\+\-\.]+:/;
  199. function getURLType(url) {
  200. if (url[0] === "/") {
  201. if (url[1] === "/") return "scheme-relative";
  202. return "path-absolute";
  203. }
  204. return ABSOLUTE_SCHEME.test(url) ? "absolute" : "path-relative";
  205. }
  206. /**
  207. * Given two URLs that are assumed to be on the same
  208. * protocol/host/user/password build a relative URL from the
  209. * path, params, and hash values.
  210. *
  211. * @param rootURL The root URL that the target will be relative to.
  212. * @param targetURL The target that the relative URL points to.
  213. * @return A rootURL-relative, normalized URL value.
  214. */
  215. function computeRelativeURL(rootURL, targetURL) {
  216. if (typeof rootURL === "string") rootURL = new URL(rootURL);
  217. if (typeof targetURL === "string") targetURL = new URL(targetURL);
  218. const targetParts = targetURL.pathname.split("/");
  219. const rootParts = rootURL.pathname.split("/");
  220. // If we've got a URL path ending with a "/", we remove it since we'd
  221. // otherwise be relative to the wrong location.
  222. if (rootParts.length > 0 && !rootParts[rootParts.length - 1]) {
  223. rootParts.pop();
  224. }
  225. while (
  226. targetParts.length > 0 &&
  227. rootParts.length > 0 &&
  228. targetParts[0] === rootParts[0]
  229. ) {
  230. targetParts.shift();
  231. rootParts.shift();
  232. }
  233. const relativePath = rootParts
  234. .map(() => "..")
  235. .concat(targetParts)
  236. .join("/");
  237. return relativePath + targetURL.search + targetURL.hash;
  238. }
  239. /**
  240. * Given a URL, ensure that it is treated as a directory URL.
  241. *
  242. * @param url
  243. * @return A normalized URL value.
  244. */
  245. const ensureDirectory = createSafeHandler(url => {
  246. url.pathname = url.pathname.replace(/\/?$/, "/");
  247. });
  248. /**
  249. * Given a URL, strip off any filename if one is present.
  250. *
  251. * @param url
  252. * @return A normalized URL value.
  253. */
  254. const trimFilename = createSafeHandler(url => {
  255. url.href = new URL(".", url.toString()).toString();
  256. });
  257. /**
  258. * Normalize a given URL.
  259. * * Convert backslashes.
  260. * * Remove any ".." and "." segments.
  261. *
  262. * @param url
  263. * @return A normalized URL value.
  264. */
  265. const normalize = createSafeHandler(url => {});
  266. exports.normalize = normalize;
  267. /**
  268. * Joins two paths/URLs.
  269. *
  270. * All returned URLs will be normalized.
  271. *
  272. * @param aRoot The root path or URL. Assumed to reference a directory.
  273. * @param aPath The path or URL to be joined with the root.
  274. * @return A joined and normalized URL value.
  275. */
  276. function join(aRoot, aPath) {
  277. const pathType = getURLType(aPath);
  278. const rootType = getURLType(aRoot);
  279. aRoot = ensureDirectory(aRoot);
  280. if (pathType === "absolute") {
  281. return withBase(aPath, undefined);
  282. }
  283. if (rootType === "absolute") {
  284. return withBase(aPath, aRoot);
  285. }
  286. if (pathType === "scheme-relative") {
  287. return normalize(aPath);
  288. }
  289. if (rootType === "scheme-relative") {
  290. return withBase(aPath, withBase(aRoot, PROTOCOL_AND_HOST)).slice(
  291. PROTOCOL.length
  292. );
  293. }
  294. if (pathType === "path-absolute") {
  295. return normalize(aPath);
  296. }
  297. if (rootType === "path-absolute") {
  298. return withBase(aPath, withBase(aRoot, PROTOCOL_AND_HOST)).slice(
  299. PROTOCOL_AND_HOST.length
  300. );
  301. }
  302. const base = buildSafeBase(aPath + aRoot);
  303. const newPath = withBase(aPath, withBase(aRoot, base));
  304. return computeRelativeURL(base, newPath);
  305. }
  306. exports.join = join;
  307. /**
  308. * Make a path relative to a URL or another path. If returning a
  309. * relative URL is not possible, the original target will be returned.
  310. * All returned URLs will be normalized.
  311. *
  312. * @param aRoot The root path or URL.
  313. * @param aPath The path or URL to be made relative to aRoot.
  314. * @return A rootURL-relative (if possible), normalized URL value.
  315. */
  316. function relative(rootURL, targetURL) {
  317. const result = relativeIfPossible(rootURL, targetURL);
  318. return typeof result === "string" ? result : normalize(targetURL);
  319. }
  320. exports.relative = relative;
  321. function relativeIfPossible(rootURL, targetURL) {
  322. const urlType = getURLType(rootURL);
  323. if (urlType !== getURLType(targetURL)) {
  324. return null;
  325. }
  326. const base = buildSafeBase(rootURL + targetURL);
  327. const root = new URL(rootURL, base);
  328. const target = new URL(targetURL, base);
  329. try {
  330. new URL("", target.toString());
  331. } catch (err) {
  332. // Bail if the URL doesn't support things being relative to it,
  333. // For example, data: and blob: URLs.
  334. return null;
  335. }
  336. if (
  337. target.protocol !== root.protocol ||
  338. target.user !== root.user ||
  339. target.password !== root.password ||
  340. target.hostname !== root.hostname ||
  341. target.port !== root.port
  342. ) {
  343. return null;
  344. }
  345. return computeRelativeURL(root, target);
  346. }
  347. /**
  348. * Compute the URL of a source given the the source root, the source's
  349. * URL, and the source map's URL.
  350. */
  351. function computeSourceURL(sourceRoot, sourceURL, sourceMapURL) {
  352. // The source map spec states that "sourceRoot" and "sources" entries are to be appended. While
  353. // that is a little vague, implementations have generally interpreted that as joining the
  354. // URLs with a `/` between then, assuming the "sourceRoot" doesn't already end with one.
  355. // For example,
  356. //
  357. // sourceRoot: "some-dir",
  358. // sources: ["/some-path.js"]
  359. //
  360. // and
  361. //
  362. // sourceRoot: "some-dir/",
  363. // sources: ["/some-path.js"]
  364. //
  365. // must behave as "some-dir/some-path.js".
  366. //
  367. // With this library's the transition to a more URL-focused implementation, that behavior is
  368. // preserved here. To acheive that, we trim the "/" from absolute-path when a sourceRoot value
  369. // is present in order to make the sources entries behave as if they are relative to the
  370. // "sourceRoot", as they would have if the two strings were simply concated.
  371. if (sourceRoot && getURLType(sourceURL) === "path-absolute") {
  372. sourceURL = sourceURL.replace(/^\//, "");
  373. }
  374. let url = normalize(sourceURL || "");
  375. // Parsing URLs can be expensive, so we only perform these joins when needed.
  376. if (sourceRoot) url = join(sourceRoot, url);
  377. if (sourceMapURL) url = join(trimFilename(sourceMapURL), url);
  378. return url;
  379. }
  380. exports.computeSourceURL = computeSourceURL;