index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. Object.defineProperty(exports, 'commentRegex', {
  3. get: function getCommentRegex () {
  4. // Groups: 1: media type, 2: MIME type, 3: charset, 4: encoding, 5: data.
  5. return /^\s*?\/[\/\*][@#]\s+?sourceMappingURL=data:(((?:application|text)\/json)(?:;charset=([^;,]+?)?)?)?(?:;(base64))?,(.*?)$/mg;
  6. }
  7. });
  8. Object.defineProperty(exports, 'mapFileCommentRegex', {
  9. get: function getMapFileCommentRegex () {
  10. // Matches sourceMappingURL in either // or /* comment styles.
  11. return /(?:\/\/[@#][ \t]+?sourceMappingURL=([^\s'"`]+?)[ \t]*?$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*?(?:\*\/){1}[ \t]*?$)/mg;
  12. }
  13. });
  14. var decodeBase64;
  15. if (typeof Buffer !== 'undefined') {
  16. if (typeof Buffer.from === 'function') {
  17. decodeBase64 = decodeBase64WithBufferFrom;
  18. } else {
  19. decodeBase64 = decodeBase64WithNewBuffer;
  20. }
  21. } else {
  22. decodeBase64 = decodeBase64WithAtob;
  23. }
  24. function decodeBase64WithBufferFrom(base64) {
  25. return Buffer.from(base64, 'base64').toString();
  26. }
  27. function decodeBase64WithNewBuffer(base64) {
  28. if (typeof value === 'number') {
  29. throw new TypeError('The value to decode must not be of type number.');
  30. }
  31. return new Buffer(base64, 'base64').toString();
  32. }
  33. function decodeBase64WithAtob(base64) {
  34. return decodeURIComponent(escape(atob(base64)));
  35. }
  36. function stripComment(sm) {
  37. return sm.split(',').pop();
  38. }
  39. function readFromFileMap(sm, read) {
  40. var r = exports.mapFileCommentRegex.exec(sm);
  41. // for some odd reason //# .. captures in 1 and /* .. */ in 2
  42. var filename = r[1] || r[2];
  43. try {
  44. var sm = read(filename);
  45. if (sm != null && typeof sm.catch === 'function') {
  46. return sm.catch(throwError);
  47. } else {
  48. return sm;
  49. }
  50. } catch (e) {
  51. throwError(e);
  52. }
  53. function throwError(e) {
  54. throw new Error('An error occurred while trying to read the map file at ' + filename + '\n' + e.stack);
  55. }
  56. }
  57. function Converter (sm, opts) {
  58. opts = opts || {};
  59. if (opts.hasComment) {
  60. sm = stripComment(sm);
  61. }
  62. if (opts.encoding === 'base64') {
  63. sm = decodeBase64(sm);
  64. } else if (opts.encoding === 'uri') {
  65. sm = decodeURIComponent(sm);
  66. }
  67. if (opts.isJSON || opts.encoding) {
  68. sm = JSON.parse(sm);
  69. }
  70. this.sourcemap = sm;
  71. }
  72. Converter.prototype.toJSON = function (space) {
  73. return JSON.stringify(this.sourcemap, null, space);
  74. };
  75. if (typeof Buffer !== 'undefined') {
  76. if (typeof Buffer.from === 'function') {
  77. Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
  78. } else {
  79. Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
  80. }
  81. } else {
  82. Converter.prototype.toBase64 = encodeBase64WithBtoa;
  83. }
  84. function encodeBase64WithBufferFrom() {
  85. var json = this.toJSON();
  86. return Buffer.from(json, 'utf8').toString('base64');
  87. }
  88. function encodeBase64WithNewBuffer() {
  89. var json = this.toJSON();
  90. if (typeof json === 'number') {
  91. throw new TypeError('The json to encode must not be of type number.');
  92. }
  93. return new Buffer(json, 'utf8').toString('base64');
  94. }
  95. function encodeBase64WithBtoa() {
  96. var json = this.toJSON();
  97. return btoa(unescape(encodeURIComponent(json)));
  98. }
  99. Converter.prototype.toURI = function () {
  100. var json = this.toJSON();
  101. return encodeURIComponent(json);
  102. };
  103. Converter.prototype.toComment = function (options) {
  104. var encoding, content, data;
  105. if (options != null && options.encoding === 'uri') {
  106. encoding = '';
  107. content = this.toURI();
  108. } else {
  109. encoding = ';base64';
  110. content = this.toBase64();
  111. }
  112. data = 'sourceMappingURL=data:application/json;charset=utf-8' + encoding + ',' + content;
  113. return options != null && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  114. };
  115. // returns copy instead of original
  116. Converter.prototype.toObject = function () {
  117. return JSON.parse(this.toJSON());
  118. };
  119. Converter.prototype.addProperty = function (key, value) {
  120. if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
  121. return this.setProperty(key, value);
  122. };
  123. Converter.prototype.setProperty = function (key, value) {
  124. this.sourcemap[key] = value;
  125. return this;
  126. };
  127. Converter.prototype.getProperty = function (key) {
  128. return this.sourcemap[key];
  129. };
  130. exports.fromObject = function (obj) {
  131. return new Converter(obj);
  132. };
  133. exports.fromJSON = function (json) {
  134. return new Converter(json, { isJSON: true });
  135. };
  136. exports.fromURI = function (uri) {
  137. return new Converter(uri, { encoding: 'uri' });
  138. };
  139. exports.fromBase64 = function (base64) {
  140. return new Converter(base64, { encoding: 'base64' });
  141. };
  142. exports.fromComment = function (comment) {
  143. var m, encoding;
  144. comment = comment
  145. .replace(/^\/\*/g, '//')
  146. .replace(/\*\/$/g, '');
  147. m = exports.commentRegex.exec(comment);
  148. encoding = m && m[4] || 'uri';
  149. return new Converter(comment, { encoding: encoding, hasComment: true });
  150. };
  151. function makeConverter(sm) {
  152. return new Converter(sm, { isJSON: true });
  153. }
  154. exports.fromMapFileComment = function (comment, read) {
  155. if (typeof read === 'string') {
  156. throw new Error(
  157. 'String directory paths are no longer supported with `fromMapFileComment`\n' +
  158. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  159. )
  160. }
  161. var sm = readFromFileMap(comment, read);
  162. if (sm != null && typeof sm.then === 'function') {
  163. return sm.then(makeConverter);
  164. } else {
  165. return makeConverter(sm);
  166. }
  167. };
  168. // Finds last sourcemap comment in file or returns null if none was found
  169. exports.fromSource = function (content) {
  170. var m = content.match(exports.commentRegex);
  171. return m ? exports.fromComment(m.pop()) : null;
  172. };
  173. // Finds last sourcemap comment in file or returns null if none was found
  174. exports.fromMapFileSource = function (content, read) {
  175. if (typeof read === 'string') {
  176. throw new Error(
  177. 'String directory paths are no longer supported with `fromMapFileSource`\n' +
  178. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  179. )
  180. }
  181. var m = content.match(exports.mapFileCommentRegex);
  182. return m ? exports.fromMapFileComment(m.pop(), read) : null;
  183. };
  184. exports.removeComments = function (src) {
  185. return src.replace(exports.commentRegex, '');
  186. };
  187. exports.removeMapFileComments = function (src) {
  188. return src.replace(exports.mapFileCommentRegex, '');
  189. };
  190. exports.generateMapFileComment = function (file, options) {
  191. var data = 'sourceMappingURL=' + file;
  192. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  193. };