sourcemap.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright 2013 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. // This is a copy from blink dev tools, see:
  28. // http://src.chromium.org/viewvc/blink/trunk/Source/devtools/front_end/SourceMap.js
  29. // revision: 153407
  30. // Added to make the file work without dev tools
  31. export const WebInspector = {};
  32. WebInspector.ParsedURL = {};
  33. WebInspector.ParsedURL.completeURL = function(){};
  34. // start of original file content
  35. /*
  36. * Copyright (C) 2012 Google Inc. All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions are
  40. * met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above
  45. * copyright notice, this list of conditions and the following disclaimer
  46. * in the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name of Google Inc. nor the names of its
  49. * contributors may be used to endorse or promote products derived from
  50. * this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. */
  64. /**
  65. * Implements Source Map V3 model. See http://code.google.com/p/closure-compiler/wiki/SourceMaps
  66. * for format description.
  67. * @constructor
  68. * @param {string} sourceMappingURL
  69. * @param {SourceMapV3} payload
  70. */
  71. WebInspector.SourceMap = function(sourceMappingURL, payload)
  72. {
  73. if (!WebInspector.SourceMap.prototype._base64Map) {
  74. const base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  75. WebInspector.SourceMap.prototype._base64Map = {};
  76. for (let i = 0; i < base64Digits.length; ++i)
  77. WebInspector.SourceMap.prototype._base64Map[base64Digits.charAt(i)] = i;
  78. }
  79. this._sourceMappingURL = sourceMappingURL;
  80. this._reverseMappingsBySourceURL = {};
  81. this._mappings = [];
  82. this._sources = {};
  83. this._sourceContentByURL = {};
  84. this._parseMappingPayload(payload);
  85. }
  86. /**
  87. * @param {string} sourceMapURL
  88. * @param {string} compiledURL
  89. * @param {function(WebInspector.SourceMap)} callback
  90. */
  91. WebInspector.SourceMap.load = function(sourceMapURL, compiledURL, callback)
  92. {
  93. NetworkAgent.loadResourceForFrontend(WebInspector.resourceTreeModel.mainFrame.id, sourceMapURL, undefined, contentLoaded.bind(this));
  94. /**
  95. * @param {?Protocol.Error} error
  96. * @param {number} statusCode
  97. * @param {NetworkAgent.Headers} headers
  98. * @param {string} content
  99. */
  100. function contentLoaded(error, statusCode, headers, content)
  101. {
  102. if (error || !content || statusCode >= 400) {
  103. console.error(`Could not load content for ${sourceMapURL} : ${error || (`HTTP status code: ${statusCode}`)}`);
  104. callback(null);
  105. return;
  106. }
  107. if (content.slice(0, 3) === ")]}")
  108. content = content.substring(content.indexOf('\n'));
  109. try {
  110. const payload = /** @type {SourceMapV3} */ (JSON.parse(content));
  111. const baseURL = sourceMapURL.startsWith("data:") ? compiledURL : sourceMapURL;
  112. callback(new WebInspector.SourceMap(baseURL, payload));
  113. } catch(e) {
  114. console.error(e.message);
  115. callback(null);
  116. }
  117. }
  118. }
  119. WebInspector.SourceMap.prototype = {
  120. /**
  121. * @return {Array.<string>}
  122. */
  123. sources()
  124. {
  125. return Object.keys(this._sources);
  126. },
  127. /**
  128. * @param {string} sourceURL
  129. * @return {string|undefined}
  130. */
  131. sourceContent(sourceURL)
  132. {
  133. return this._sourceContentByURL[sourceURL];
  134. },
  135. /**
  136. * @param {string} sourceURL
  137. * @param {WebInspector.ResourceType} contentType
  138. * @return {WebInspector.ContentProvider}
  139. */
  140. sourceContentProvider(sourceURL, contentType)
  141. {
  142. const lastIndexOfDot = sourceURL.lastIndexOf(".");
  143. const extension = lastIndexOfDot !== -1 ? sourceURL.substr(lastIndexOfDot + 1) : "";
  144. const mimeType = WebInspector.ResourceType.mimeTypesForExtensions[extension.toLowerCase()];
  145. const sourceContent = this.sourceContent(sourceURL);
  146. if (sourceContent)
  147. return new WebInspector.StaticContentProvider(contentType, sourceContent, mimeType);
  148. return new WebInspector.CompilerSourceMappingContentProvider(sourceURL, contentType, mimeType);
  149. },
  150. /**
  151. * @param {SourceMapV3} mappingPayload
  152. */
  153. _parseMappingPayload(mappingPayload)
  154. {
  155. if (mappingPayload.sections)
  156. this._parseSections(mappingPayload.sections);
  157. else
  158. this._parseMap(mappingPayload, 0, 0);
  159. },
  160. /**
  161. * @param {Array.<SourceMapV3.Section>} sections
  162. */
  163. _parseSections(sections)
  164. {
  165. for (let i = 0; i < sections.length; ++i) {
  166. const section = sections[i];
  167. this._parseMap(section.map, section.offset.line, section.offset.column);
  168. }
  169. },
  170. /**
  171. * @param {number} lineNumber in compiled resource
  172. * @param {number} columnNumber in compiled resource
  173. * @return {?Array}
  174. */
  175. findEntry(lineNumber, columnNumber)
  176. {
  177. let first = 0;
  178. let count = this._mappings.length;
  179. while (count > 1) {
  180. const step = count >> 1;
  181. const middle = first + step;
  182. const mapping = this._mappings[middle];
  183. if (lineNumber < mapping[0] || (lineNumber === mapping[0] && columnNumber < mapping[1]))
  184. count = step;
  185. else {
  186. first = middle;
  187. count -= step;
  188. }
  189. }
  190. const entry = this._mappings[first];
  191. if (!first && entry && (lineNumber < entry[0] || (lineNumber === entry[0] && columnNumber < entry[1])))
  192. return null;
  193. return entry;
  194. },
  195. /**
  196. * @param {string} sourceURL of the originating resource
  197. * @param {number} lineNumber in the originating resource
  198. * @return {Array}
  199. */
  200. findEntryReversed(sourceURL, lineNumber)
  201. {
  202. const mappings = this._reverseMappingsBySourceURL[sourceURL];
  203. for ( ; lineNumber < mappings.length; ++lineNumber) {
  204. const mapping = mappings[lineNumber];
  205. if (mapping)
  206. return mapping;
  207. }
  208. return this._mappings[0];
  209. },
  210. /**
  211. * @override
  212. */
  213. _parseMap(map, lineNumber, columnNumber)
  214. {
  215. let sourceIndex = 0;
  216. let sourceLineNumber = 0;
  217. let sourceColumnNumber = 0;
  218. let nameIndex = 0;
  219. const sources = [];
  220. const originalToCanonicalURLMap = {};
  221. for (let i = 0; i < map.sources.length; ++i) {
  222. const originalSourceURL = map.sources[i];
  223. let sourceRoot = map.sourceRoot || "";
  224. if (sourceRoot && !sourceRoot.endsWith("/")) sourceRoot += "/";
  225. const href = sourceRoot + originalSourceURL;
  226. const url = WebInspector.ParsedURL.completeURL(this._sourceMappingURL, href) || href;
  227. originalToCanonicalURLMap[originalSourceURL] = url;
  228. sources.push(url);
  229. this._sources[url] = true;
  230. if (map.sourcesContent && map.sourcesContent[i]) {
  231. this._sourceContentByURL[url] = map.sourcesContent[i];
  232. }
  233. }
  234. const stringCharIterator = new WebInspector.SourceMap.StringCharIterator(map.mappings);
  235. let sourceURL = sources[sourceIndex];
  236. while (true) {
  237. if (stringCharIterator.peek() === ",")
  238. stringCharIterator.next();
  239. else {
  240. while (stringCharIterator.peek() === ";") {
  241. lineNumber += 1;
  242. columnNumber = 0;
  243. stringCharIterator.next();
  244. }
  245. if (!stringCharIterator.hasNext())
  246. break;
  247. }
  248. columnNumber += this._decodeVLQ(stringCharIterator);
  249. if (this._isSeparator(stringCharIterator.peek())) {
  250. this._mappings.push([lineNumber, columnNumber]);
  251. continue;
  252. }
  253. const sourceIndexDelta = this._decodeVLQ(stringCharIterator);
  254. if (sourceIndexDelta) {
  255. sourceIndex += sourceIndexDelta;
  256. sourceURL = sources[sourceIndex];
  257. }
  258. sourceLineNumber += this._decodeVLQ(stringCharIterator);
  259. sourceColumnNumber += this._decodeVLQ(stringCharIterator);
  260. if (!this._isSeparator(stringCharIterator.peek()))
  261. nameIndex += this._decodeVLQ(stringCharIterator);
  262. this._mappings.push([lineNumber, columnNumber, sourceURL, sourceLineNumber, sourceColumnNumber]);
  263. }
  264. for (let i = 0; i < this._mappings.length; ++i) {
  265. const mapping = this._mappings[i];
  266. const url = mapping[2];
  267. if (!url) continue;
  268. if (!this._reverseMappingsBySourceURL[url]) {
  269. this._reverseMappingsBySourceURL[url] = [];
  270. }
  271. const reverseMappings = this._reverseMappingsBySourceURL[url];
  272. const sourceLine = mapping[3];
  273. if (!reverseMappings[sourceLine]) {
  274. reverseMappings[sourceLine] = [mapping[0], mapping[1]];
  275. }
  276. }
  277. },
  278. /**
  279. * @param {string} char
  280. * @return {boolean}
  281. */
  282. _isSeparator(char)
  283. {
  284. return char === "," || char === ";";
  285. },
  286. /**
  287. * @param {WebInspector.SourceMap.StringCharIterator} stringCharIterator
  288. * @return {number}
  289. */
  290. _decodeVLQ(stringCharIterator)
  291. {
  292. // Read unsigned value.
  293. let result = 0;
  294. let shift = 0;
  295. let digit;
  296. do {
  297. digit = this._base64Map[stringCharIterator.next()];
  298. result += (digit & this._VLQ_BASE_MASK) << shift;
  299. shift += this._VLQ_BASE_SHIFT;
  300. } while (digit & this._VLQ_CONTINUATION_MASK);
  301. // Fix the sign.
  302. const negate = result & 1;
  303. // Use unsigned right shift, so that the 32nd bit is properly shifted
  304. // to the 31st, and the 32nd becomes unset.
  305. result >>>= 1;
  306. if (negate) {
  307. // We need to OR 0x80000000 here to ensure the 32nd bit (the sign bit
  308. // in a 32bit int) is always set for negative numbers. If `result`
  309. // were 1, (meaning `negate` is true and all other bits were zeros),
  310. // `result` would now be 0. But -0 doesn't flip the 32nd bit as
  311. // intended. All other numbers will successfully set the 32nd bit
  312. // without issue, so doing this is a noop for them.
  313. return -result | 0x80000000;
  314. }
  315. return result;
  316. },
  317. _VLQ_BASE_SHIFT: 5,
  318. _VLQ_BASE_MASK: (1 << 5) - 1,
  319. _VLQ_CONTINUATION_MASK: 1 << 5
  320. }
  321. /**
  322. * @constructor
  323. * @param {string} string
  324. */
  325. WebInspector.SourceMap.StringCharIterator = function(string)
  326. {
  327. this._string = string;
  328. this._position = 0;
  329. }
  330. WebInspector.SourceMap.StringCharIterator.prototype = {
  331. /**
  332. * @return {string}
  333. */
  334. next()
  335. {
  336. return this._string.charAt(this._position++);
  337. },
  338. /**
  339. * @return {string}
  340. */
  341. peek()
  342. {
  343. return this._string.charAt(this._position);
  344. },
  345. /**
  346. * @return {boolean}
  347. */
  348. hasNext()
  349. {
  350. return this._position < this._string.length;
  351. }
  352. }