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").HashLike} HashLike */
  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 version
  26. * @property {string[]} sources sources
  27. * @property {string[]} names name
  28. * @property {string=} sourceRoot source root
  29. * @property {(Buffer | "")[]=} sourcesContent sources content
  30. * @property {Buffer=} mappings mappings
  31. * @property {string} file 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. const bufferedMap =
  40. /** @type {BufferedMap} */
  41. (/** @type {unknown} */ ({ ...map }));
  42. if (map.mappings) {
  43. bufferedMap.mappings = Buffer.from(map.mappings, "utf8");
  44. }
  45. if (map.sourcesContent) {
  46. bufferedMap.sourcesContent = map.sourcesContent.map(
  47. (str) => str && Buffer.from(str, "utf8"),
  48. );
  49. }
  50. return bufferedMap;
  51. };
  52. /**
  53. * @param {null | BufferedMap} bufferedMap buffered map
  54. * @returns {null | RawSourceMap} map
  55. */
  56. const bufferedMapToMap = (bufferedMap) => {
  57. if (typeof bufferedMap !== "object" || !bufferedMap) return bufferedMap;
  58. const map =
  59. /** @type {RawSourceMap} */
  60. (/** @type {unknown} */ ({ ...bufferedMap }));
  61. if (bufferedMap.mappings) {
  62. map.mappings = bufferedMap.mappings.toString("utf8");
  63. }
  64. if (bufferedMap.sourcesContent) {
  65. map.sourcesContent = bufferedMap.sourcesContent.map(
  66. (buffer) => buffer && buffer.toString("utf8"),
  67. );
  68. }
  69. return map;
  70. };
  71. /** @typedef {{ map?: null | RawSourceMap, bufferedMap?: null | BufferedMap }} BufferEntry */
  72. /** @typedef {Map<string, BufferEntry>} BufferedMaps */
  73. /**
  74. * @typedef {object} CachedData
  75. * @property {boolean=} source source
  76. * @property {Buffer} buffer buffer
  77. * @property {number=} size size
  78. * @property {BufferedMaps} maps maps
  79. * @property {(string | Buffer)[]=} hash hash
  80. */
  81. class CachedSource extends Source {
  82. /**
  83. * @param {Source | (() => Source)} source source
  84. * @param {CachedData=} cachedData cached data
  85. */
  86. constructor(source, cachedData) {
  87. super();
  88. this._source = source;
  89. this._cachedSourceType = cachedData ? cachedData.source : undefined;
  90. /**
  91. * @private
  92. * @type {undefined | string}
  93. */
  94. this._cachedSource = undefined;
  95. this._cachedBuffer = cachedData ? cachedData.buffer : undefined;
  96. this._cachedSize = cachedData ? cachedData.size : undefined;
  97. /**
  98. * @private
  99. * @type {BufferedMaps}
  100. */
  101. this._cachedMaps = cachedData ? cachedData.maps : new Map();
  102. this._cachedHashUpdate = cachedData ? cachedData.hash : undefined;
  103. }
  104. /**
  105. * @returns {CachedData} cached data
  106. */
  107. getCachedData() {
  108. /** @type {BufferedMaps} */
  109. const bufferedMaps = new Map();
  110. for (const pair of this._cachedMaps) {
  111. const [, cacheEntry] = pair;
  112. if (cacheEntry.bufferedMap === undefined) {
  113. cacheEntry.bufferedMap = mapToBufferedMap(
  114. this._getMapFromCacheEntry(cacheEntry),
  115. );
  116. }
  117. bufferedMaps.set(pair[0], {
  118. map: undefined,
  119. bufferedMap: cacheEntry.bufferedMap,
  120. });
  121. }
  122. return {
  123. // We don't want to cache strings
  124. // So if we have a caches sources
  125. // create a buffer from it and only store
  126. // if it was a Buffer or string
  127. buffer: this._cachedSource
  128. ? this.buffer()
  129. : /** @type {Buffer} */ (this._cachedBuffer),
  130. source:
  131. this._cachedSourceType !== undefined
  132. ? this._cachedSourceType
  133. : typeof this._cachedSource === "string"
  134. ? true
  135. : Buffer.isBuffer(this._cachedSource)
  136. ? false
  137. : undefined,
  138. size: this._cachedSize,
  139. maps: bufferedMaps,
  140. hash: this._cachedHashUpdate,
  141. };
  142. }
  143. originalLazy() {
  144. return this._source;
  145. }
  146. original() {
  147. if (typeof this._source === "function") this._source = this._source();
  148. return this._source;
  149. }
  150. /**
  151. * @returns {SourceValue} source
  152. */
  153. source() {
  154. const source = this._getCachedSource();
  155. if (source !== undefined) return source;
  156. return (this._cachedSource =
  157. /** @type {string} */
  158. (this.original().source()));
  159. }
  160. /**
  161. * @private
  162. * @param {BufferEntry} cacheEntry cache entry
  163. * @returns {null | RawSourceMap} raw source map
  164. */
  165. _getMapFromCacheEntry(cacheEntry) {
  166. if (cacheEntry.map !== undefined) {
  167. return cacheEntry.map;
  168. } else if (cacheEntry.bufferedMap !== undefined) {
  169. return (cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap));
  170. }
  171. return null;
  172. }
  173. /**
  174. * @private
  175. * @returns {undefined | string} cached source
  176. */
  177. _getCachedSource() {
  178. if (this._cachedSource !== undefined) return this._cachedSource;
  179. if (this._cachedBuffer && this._cachedSourceType !== undefined) {
  180. const value = this._cachedSourceType
  181. ? this._cachedBuffer.toString("utf8")
  182. : this._cachedBuffer;
  183. if (isDualStringBufferCachingEnabled()) {
  184. this._cachedSource = /** @type {string} */ (value);
  185. }
  186. return /** @type {string} */ (value);
  187. }
  188. }
  189. /**
  190. * @returns {Buffer} buffer
  191. */
  192. buffer() {
  193. if (this._cachedBuffer !== undefined) return this._cachedBuffer;
  194. if (this._cachedSource !== undefined) {
  195. const value = Buffer.isBuffer(this._cachedSource)
  196. ? this._cachedSource
  197. : Buffer.from(this._cachedSource, "utf8");
  198. if (isDualStringBufferCachingEnabled()) {
  199. this._cachedBuffer = value;
  200. }
  201. return value;
  202. }
  203. if (typeof this.original().buffer === "function") {
  204. return (this._cachedBuffer = this.original().buffer());
  205. }
  206. const bufferOrString = this.source();
  207. if (Buffer.isBuffer(bufferOrString)) {
  208. return (this._cachedBuffer = bufferOrString);
  209. }
  210. const value = Buffer.from(bufferOrString, "utf8");
  211. if (isDualStringBufferCachingEnabled()) {
  212. this._cachedBuffer = value;
  213. }
  214. return value;
  215. }
  216. /**
  217. * @returns {number} size
  218. */
  219. size() {
  220. if (this._cachedSize !== undefined) return this._cachedSize;
  221. if (this._cachedBuffer !== undefined) {
  222. return (this._cachedSize = this._cachedBuffer.length);
  223. }
  224. const source = this._getCachedSource();
  225. if (source !== undefined) {
  226. return (this._cachedSize = Buffer.byteLength(source));
  227. }
  228. return (this._cachedSize = this.original().size());
  229. }
  230. /**
  231. * @param {MapOptions=} options map options
  232. * @returns {SourceAndMap} source and map
  233. */
  234. sourceAndMap(options) {
  235. const key = options ? JSON.stringify(options) : "{}";
  236. const cacheEntry = this._cachedMaps.get(key);
  237. // Look for a cached map
  238. if (cacheEntry !== undefined) {
  239. // We have a cached map in some representation
  240. const map = this._getMapFromCacheEntry(cacheEntry);
  241. // Either get the cached source or compute it
  242. return { source: this.source(), map };
  243. }
  244. // Look for a cached source
  245. let source = this._getCachedSource();
  246. // Compute the map
  247. let map;
  248. if (source !== undefined) {
  249. map = this.original().map(options);
  250. } else {
  251. // Compute the source and map together.
  252. const sourceAndMap = this.original().sourceAndMap(options);
  253. source = /** @type {string} */ (sourceAndMap.source);
  254. map = sourceAndMap.map;
  255. this._cachedSource = source;
  256. }
  257. this._cachedMaps.set(key, {
  258. map,
  259. bufferedMap: undefined,
  260. });
  261. return { source, map };
  262. }
  263. /**
  264. * @param {Options} options options
  265. * @param {OnChunk} onChunk called for each chunk of code
  266. * @param {OnSource} onSource called for each source
  267. * @param {OnName} onName called for each name
  268. * @returns {GeneratedSourceInfo} generated source info
  269. */
  270. streamChunks(options, onChunk, onSource, onName) {
  271. const key = options ? JSON.stringify(options) : "{}";
  272. if (
  273. this._cachedMaps.has(key) &&
  274. (this._cachedBuffer !== undefined || this._cachedSource !== undefined)
  275. ) {
  276. const { source, map } = this.sourceAndMap(options);
  277. if (map) {
  278. return streamChunksOfSourceMap(
  279. /** @type {string} */
  280. (source),
  281. map,
  282. onChunk,
  283. onSource,
  284. onName,
  285. Boolean(options && options.finalSource),
  286. true,
  287. );
  288. }
  289. return streamChunksOfRawSource(
  290. /** @type {string} */
  291. (source),
  292. onChunk,
  293. onSource,
  294. onName,
  295. Boolean(options && options.finalSource),
  296. );
  297. }
  298. const sourceAndMap = streamAndGetSourceAndMap(
  299. this.original(),
  300. options,
  301. onChunk,
  302. onSource,
  303. onName,
  304. );
  305. this._cachedSource = sourceAndMap.source;
  306. this._cachedMaps.set(key, {
  307. map: /** @type {RawSourceMap} */ (sourceAndMap.map),
  308. bufferedMap: undefined,
  309. });
  310. return sourceAndMap.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 {HashLike} 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;
  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 {HashLike} */ (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;