SourceMap.js 11 KB

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