CachedSource.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Source = require("./Source");
  7. const streamAndGetSourceAndMap = require("./helpers/streamAndGetSourceAndMap");
  8. const streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");
  9. const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
  10. const {
  11. isDualStringBufferCachingEnabled
  12. } = require("./helpers/stringBufferUtils");
  13. /** @typedef {import("./Source").Hash} Hash */
  14. /** @typedef {import("./Source").MapOptions} MapOptions */
  15. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  16. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  17. /** @typedef {import("./Source").SourceValue} SourceValue */
  18. /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  19. /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
  20. /** @typedef {import("./helpers/streamChunks").OnName} OnName */
  21. /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
  22. /** @typedef {import("./helpers/streamChunks").Options} Options */
  23. /**
  24. * @typedef {object} BufferedMap
  25. * @property {number} version
  26. * @property {string[]} sources
  27. * @property {string[]} names
  28. * @property {string=} sourceRoot
  29. * @property {(Buffer | "")[]=} sourcesContent
  30. * @property {Buffer=} mappings
  31. * @property {string} file
  32. */
  33. /**
  34. * @param {null | RawSourceMap} map map
  35. * @returns {null | BufferedMap} buffered map
  36. */
  37. const mapToBufferedMap = (map) => {
  38. if (typeof map !== "object" || !map) return map;
  39. /** @type {BufferedMap} */
  40. const bufferedMap = Object.assign(/** @type {BufferedMap} */ ({}), map);
  41. if (map.mappings) {
  42. bufferedMap.mappings = Buffer.from(map.mappings, "utf-8");
  43. }
  44. if (map.sourcesContent) {
  45. bufferedMap.sourcesContent = map.sourcesContent.map(
  46. (str) => str && Buffer.from(str, "utf-8")
  47. );
  48. }
  49. return bufferedMap;
  50. };
  51. /**
  52. * @param {null | BufferedMap} bufferedMap buffered map
  53. * @returns {null | RawSourceMap} map
  54. */
  55. const bufferedMapToMap = (bufferedMap) => {
  56. if (typeof bufferedMap !== "object" || !bufferedMap) return bufferedMap;
  57. /** @type {RawSourceMap} */
  58. const map = Object.assign(/** @type {RawSourceMap} */ ({}), bufferedMap);
  59. if (bufferedMap.mappings) {
  60. map.mappings = bufferedMap.mappings.toString("utf-8");
  61. }
  62. if (bufferedMap.sourcesContent) {
  63. map.sourcesContent = bufferedMap.sourcesContent.map(
  64. (buffer) => buffer && buffer.toString("utf-8")
  65. );
  66. }
  67. return map;
  68. };
  69. /** @typedef {{ map?: null | RawSourceMap, bufferedMap?: null | BufferedMap }} BufferEntry */
  70. /** @typedef {Map<string, BufferEntry>} BufferedMaps */
  71. /**
  72. * @typedef {object} CachedData
  73. * @property {boolean=} source
  74. * @property {Buffer} buffer
  75. * @property {number=} size
  76. * @property {BufferedMaps} maps
  77. * @property {(string | Buffer)[]=} hash
  78. */
  79. class CachedSource extends Source {
  80. // eslint-disable-next-line valid-jsdoc
  81. /**
  82. * @param {Source | (() => Source)} source source
  83. * @param {CachedData=} cachedData cached data
  84. */
  85. constructor(source, cachedData) {
  86. super();
  87. this._source = source;
  88. this._cachedSourceType = cachedData ? cachedData.source : undefined;
  89. /**
  90. * @private
  91. * @type {undefined | string}
  92. */
  93. this._cachedSource = undefined;
  94. this._cachedBuffer = cachedData ? cachedData.buffer : undefined;
  95. this._cachedSize = cachedData ? cachedData.size : undefined;
  96. /**
  97. * @private
  98. * @type {BufferedMaps}
  99. */
  100. this._cachedMaps = cachedData ? cachedData.maps : new Map();
  101. this._cachedHashUpdate = cachedData ? cachedData.hash : undefined;
  102. }
  103. /**
  104. * @returns {CachedData} cached data
  105. */
  106. getCachedData() {
  107. /** @type {BufferedMaps} */
  108. const bufferedMaps = new Map();
  109. for (const pair of this._cachedMaps) {
  110. let cacheEntry = pair[1];
  111. if (cacheEntry.bufferedMap === undefined) {
  112. cacheEntry.bufferedMap = mapToBufferedMap(
  113. this._getMapFromCacheEntry(cacheEntry)
  114. );
  115. }
  116. bufferedMaps.set(pair[0], {
  117. map: undefined,
  118. bufferedMap: cacheEntry.bufferedMap
  119. });
  120. }
  121. return {
  122. // We don't want to cache strings
  123. // So if we have a caches sources
  124. // create a buffer from it and only store
  125. // if it was a Buffer or string
  126. buffer: this._cachedSource
  127. ? this.buffer()
  128. : /** @type {Buffer} */ (this._cachedBuffer),
  129. source:
  130. this._cachedSourceType !== undefined
  131. ? this._cachedSourceType
  132. : typeof this._cachedSource === "string"
  133. ? true
  134. : Buffer.isBuffer(this._cachedSource)
  135. ? false
  136. : undefined,
  137. size: this._cachedSize,
  138. maps: bufferedMaps,
  139. hash: this._cachedHashUpdate
  140. };
  141. }
  142. originalLazy() {
  143. return this._source;
  144. }
  145. original() {
  146. if (typeof this._source === "function") this._source = this._source();
  147. return this._source;
  148. }
  149. /**
  150. * @returns {SourceValue} source
  151. */
  152. source() {
  153. const source = this._getCachedSource();
  154. if (source !== undefined) return source;
  155. return (this._cachedSource =
  156. /** @type {string} */
  157. (this.original().source()));
  158. }
  159. /**
  160. * @private
  161. * @param {BufferEntry} cacheEntry cache entry
  162. * @returns {null | RawSourceMap} raw source map
  163. */
  164. _getMapFromCacheEntry(cacheEntry) {
  165. if (cacheEntry.map !== undefined) {
  166. return cacheEntry.map;
  167. } else if (cacheEntry.bufferedMap !== undefined) {
  168. return (cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap));
  169. }
  170. return null;
  171. }
  172. /**
  173. * @private
  174. * @returns {undefined | string} cached source
  175. */
  176. _getCachedSource() {
  177. if (this._cachedSource !== undefined) return this._cachedSource;
  178. if (this._cachedBuffer && this._cachedSourceType !== undefined) {
  179. const value = this._cachedSourceType
  180. ? this._cachedBuffer.toString("utf-8")
  181. : this._cachedBuffer;
  182. if (isDualStringBufferCachingEnabled()) {
  183. this._cachedSource = /** @type {string} */ (value);
  184. }
  185. return /** @type {string} */ (value);
  186. }
  187. }
  188. /**
  189. * @returns {Buffer} buffer
  190. */
  191. buffer() {
  192. if (this._cachedBuffer !== undefined) return this._cachedBuffer;
  193. if (this._cachedSource !== undefined) {
  194. const value = Buffer.isBuffer(this._cachedSource)
  195. ? this._cachedSource
  196. : Buffer.from(this._cachedSource, "utf-8");
  197. if (isDualStringBufferCachingEnabled()) {
  198. this._cachedBuffer = value;
  199. }
  200. return value;
  201. }
  202. if (typeof this.original().buffer === "function") {
  203. return (this._cachedBuffer = this.original().buffer());
  204. }
  205. const bufferOrString = this.source();
  206. if (Buffer.isBuffer(bufferOrString)) {
  207. return (this._cachedBuffer = bufferOrString);
  208. }
  209. const value = Buffer.from(bufferOrString, "utf-8");
  210. if (isDualStringBufferCachingEnabled()) {
  211. this._cachedBuffer = value;
  212. }
  213. return value;
  214. }
  215. /**
  216. * @returns {number} size
  217. */
  218. size() {
  219. if (this._cachedSize !== undefined) return this._cachedSize;
  220. if (this._cachedBuffer !== undefined) {
  221. return (this._cachedSize = this._cachedBuffer.length);
  222. }
  223. const source = this._getCachedSource();
  224. if (source !== undefined) {
  225. return (this._cachedSize = Buffer.byteLength(source));
  226. }
  227. return (this._cachedSize = this.original().size());
  228. }
  229. /**
  230. * @param {MapOptions=} options map options
  231. * @returns {SourceAndMap} source and map
  232. */
  233. sourceAndMap(options) {
  234. const key = options ? JSON.stringify(options) : "{}";
  235. const cacheEntry = this._cachedMaps.get(key);
  236. // Look for a cached map
  237. if (cacheEntry !== undefined) {
  238. // We have a cached map in some representation
  239. const map = this._getMapFromCacheEntry(cacheEntry);
  240. // Either get the cached source or compute it
  241. return { source: this.source(), map };
  242. }
  243. // Look for a cached source
  244. let source = this._getCachedSource();
  245. // Compute the map
  246. let map;
  247. if (source !== undefined) {
  248. map = this.original().map(options);
  249. } else {
  250. // Compute the source and map together.
  251. const sourceAndMap = this.original().sourceAndMap(options);
  252. source = /** @type {string} */ (sourceAndMap.source);
  253. map = sourceAndMap.map;
  254. this._cachedSource = source;
  255. }
  256. this._cachedMaps.set(key, {
  257. map,
  258. bufferedMap: undefined
  259. });
  260. return { source, map };
  261. }
  262. /**
  263. * @param {Options} options options
  264. * @param {OnChunk} onChunk called for each chunk of code
  265. * @param {OnSource} onSource called for each source
  266. * @param {OnName} onName called for each name
  267. * @returns {GeneratedSourceInfo} generated source info
  268. */
  269. streamChunks(options, onChunk, onSource, onName) {
  270. const key = options ? JSON.stringify(options) : "{}";
  271. if (
  272. this._cachedMaps.has(key) &&
  273. (this._cachedBuffer !== undefined || this._cachedSource !== undefined)
  274. ) {
  275. const { source, map } = this.sourceAndMap(options);
  276. if (map) {
  277. return streamChunksOfSourceMap(
  278. /** @type {string} */
  279. (source),
  280. map,
  281. onChunk,
  282. onSource,
  283. onName,
  284. !!(options && options.finalSource),
  285. true
  286. );
  287. } else {
  288. return streamChunksOfRawSource(
  289. /** @type {string} */
  290. (source),
  291. onChunk,
  292. onSource,
  293. onName,
  294. !!(options && options.finalSource)
  295. );
  296. }
  297. }
  298. const { result, source, map } = streamAndGetSourceAndMap(
  299. this.original(),
  300. options,
  301. onChunk,
  302. onSource,
  303. onName
  304. );
  305. this._cachedSource = source;
  306. this._cachedMaps.set(key, {
  307. map: /** @type {RawSourceMap} */ (map),
  308. bufferedMap: undefined
  309. });
  310. return result;
  311. }
  312. /**
  313. * @param {MapOptions=} options map options
  314. * @returns {RawSourceMap | null} map
  315. */
  316. map(options) {
  317. const key = options ? JSON.stringify(options) : "{}";
  318. const cacheEntry = this._cachedMaps.get(key);
  319. if (cacheEntry !== undefined) {
  320. return this._getMapFromCacheEntry(cacheEntry);
  321. }
  322. const map = this.original().map(options);
  323. this._cachedMaps.set(key, {
  324. map,
  325. bufferedMap: undefined
  326. });
  327. return map;
  328. }
  329. /**
  330. * @param {Hash} hash hash
  331. * @returns {void}
  332. */
  333. updateHash(hash) {
  334. if (this._cachedHashUpdate !== undefined) {
  335. for (const item of this._cachedHashUpdate) hash.update(item);
  336. return;
  337. }
  338. /** @type {(string | Buffer)[]} */
  339. const update = [];
  340. /** @type {string | undefined} */
  341. let currentString = undefined;
  342. const tracker = {
  343. /**
  344. * @param {string | Buffer} item item
  345. * @returns {void}
  346. */
  347. update: (item) => {
  348. if (typeof item === "string" && item.length < 10240) {
  349. if (currentString === undefined) {
  350. currentString = item;
  351. } else {
  352. currentString += item;
  353. if (currentString.length > 102400) {
  354. update.push(Buffer.from(currentString));
  355. currentString = undefined;
  356. }
  357. }
  358. } else {
  359. if (currentString !== undefined) {
  360. update.push(Buffer.from(currentString));
  361. currentString = undefined;
  362. }
  363. update.push(item);
  364. }
  365. }
  366. };
  367. this.original().updateHash(/** @type {Hash} */ (tracker));
  368. if (currentString !== undefined) {
  369. update.push(Buffer.from(currentString));
  370. }
  371. for (const item of update) hash.update(item);
  372. this._cachedHashUpdate = update;
  373. }
  374. }
  375. module.exports = CachedSource;