CachedSource.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. /**
  89. * @private
  90. * @type {Source | (() => Source)}
  91. */
  92. this._source = source;
  93. /**
  94. * @private
  95. * @type {boolean | undefined}
  96. */
  97. this._cachedSourceType = cachedData ? cachedData.source : undefined;
  98. /**
  99. * @private
  100. * @type {undefined | string}
  101. */
  102. this._cachedSource = undefined;
  103. /**
  104. * @private
  105. * @type {Buffer | undefined}
  106. */
  107. this._cachedBuffer = cachedData ? cachedData.buffer : undefined;
  108. /**
  109. * @private
  110. * @type {number | undefined}
  111. */
  112. this._cachedSize = cachedData ? cachedData.size : undefined;
  113. /**
  114. * @private
  115. * @type {BufferedMaps}
  116. */
  117. this._cachedMaps = cachedData ? cachedData.maps : new Map();
  118. /**
  119. * @private
  120. * @type {(string | Buffer)[] | undefined}
  121. */
  122. this._cachedHashUpdate = cachedData ? cachedData.hash : undefined;
  123. }
  124. /**
  125. * @returns {CachedData} cached data
  126. */
  127. getCachedData() {
  128. /** @type {BufferedMaps} */
  129. const bufferedMaps = new Map();
  130. for (const pair of this._cachedMaps) {
  131. const [, cacheEntry] = pair;
  132. if (cacheEntry.bufferedMap === undefined) {
  133. cacheEntry.bufferedMap = mapToBufferedMap(
  134. this._getMapFromCacheEntry(cacheEntry),
  135. );
  136. }
  137. bufferedMaps.set(pair[0], {
  138. map: undefined,
  139. bufferedMap: cacheEntry.bufferedMap,
  140. });
  141. }
  142. return {
  143. // We don't want to cache strings
  144. // So if we have a caches sources
  145. // create a buffer from it and only store
  146. // if it was a Buffer or string
  147. buffer: this._cachedSource
  148. ? this.buffer()
  149. : /** @type {Buffer} */ (this._cachedBuffer),
  150. source:
  151. this._cachedSourceType !== undefined
  152. ? this._cachedSourceType
  153. : typeof this._cachedSource === "string"
  154. ? true
  155. : Buffer.isBuffer(this._cachedSource)
  156. ? false
  157. : undefined,
  158. size: this._cachedSize,
  159. maps: bufferedMaps,
  160. hash: this._cachedHashUpdate,
  161. };
  162. }
  163. originalLazy() {
  164. return this._source;
  165. }
  166. original() {
  167. if (typeof this._source === "function") this._source = this._source();
  168. return this._source;
  169. }
  170. /**
  171. * @returns {SourceValue} source
  172. */
  173. source() {
  174. const source = this._getCachedSource();
  175. if (source !== undefined) return source;
  176. return (this._cachedSource =
  177. /** @type {string} */
  178. (this.original().source()));
  179. }
  180. /**
  181. * @private
  182. * @param {BufferEntry} cacheEntry cache entry
  183. * @returns {null | RawSourceMap} raw source map
  184. */
  185. _getMapFromCacheEntry(cacheEntry) {
  186. if (cacheEntry.map !== undefined) {
  187. return cacheEntry.map;
  188. } else if (cacheEntry.bufferedMap !== undefined) {
  189. return (cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap));
  190. }
  191. return null;
  192. }
  193. /**
  194. * @private
  195. * @returns {undefined | string} cached source
  196. */
  197. _getCachedSource() {
  198. if (this._cachedSource !== undefined) return this._cachedSource;
  199. if (this._cachedBuffer && this._cachedSourceType !== undefined) {
  200. const value = this._cachedSourceType
  201. ? this._cachedBuffer.toString("utf8")
  202. : this._cachedBuffer;
  203. if (isDualStringBufferCachingEnabled()) {
  204. this._cachedSource = /** @type {string} */ (value);
  205. }
  206. return /** @type {string} */ (value);
  207. }
  208. }
  209. /**
  210. * @returns {Buffer} buffer
  211. */
  212. buffer() {
  213. if (this._cachedBuffer !== undefined) return this._cachedBuffer;
  214. if (this._cachedSource !== undefined) {
  215. const value = Buffer.isBuffer(this._cachedSource)
  216. ? this._cachedSource
  217. : Buffer.from(this._cachedSource, "utf8");
  218. if (isDualStringBufferCachingEnabled()) {
  219. this._cachedBuffer = value;
  220. }
  221. return value;
  222. }
  223. if (typeof this.original().buffer === "function") {
  224. return (this._cachedBuffer = this.original().buffer());
  225. }
  226. const bufferOrString = this.source();
  227. if (Buffer.isBuffer(bufferOrString)) {
  228. return (this._cachedBuffer = bufferOrString);
  229. }
  230. const value = Buffer.from(bufferOrString, "utf8");
  231. if (isDualStringBufferCachingEnabled()) {
  232. this._cachedBuffer = value;
  233. }
  234. return value;
  235. }
  236. /**
  237. * @returns {number} size
  238. */
  239. size() {
  240. if (this._cachedSize !== undefined) return this._cachedSize;
  241. if (this._cachedBuffer !== undefined) {
  242. return (this._cachedSize = this._cachedBuffer.length);
  243. }
  244. const source = this._getCachedSource();
  245. if (source !== undefined) {
  246. return (this._cachedSize = Buffer.byteLength(source));
  247. }
  248. return (this._cachedSize = this.original().size());
  249. }
  250. /**
  251. * @param {MapOptions=} options map options
  252. * @returns {SourceAndMap} source and map
  253. */
  254. sourceAndMap(options) {
  255. const key = options ? JSON.stringify(options) : "{}";
  256. const cacheEntry = this._cachedMaps.get(key);
  257. // Look for a cached map
  258. if (cacheEntry !== undefined) {
  259. // We have a cached map in some representation
  260. const map = this._getMapFromCacheEntry(cacheEntry);
  261. // Either get the cached source or compute it
  262. return { source: this.source(), map };
  263. }
  264. // Look for a cached source
  265. let source = this._getCachedSource();
  266. // Compute the map
  267. let map;
  268. if (source !== undefined) {
  269. map = this.original().map(options);
  270. } else {
  271. // Compute the source and map together.
  272. const sourceAndMap = this.original().sourceAndMap(options);
  273. source = /** @type {string} */ (sourceAndMap.source);
  274. map = sourceAndMap.map;
  275. this._cachedSource = source;
  276. }
  277. this._cachedMaps.set(key, {
  278. map,
  279. bufferedMap: undefined,
  280. });
  281. return { source, map };
  282. }
  283. /**
  284. * @param {Options} options options
  285. * @param {OnChunk} onChunk called for each chunk of code
  286. * @param {OnSource} onSource called for each source
  287. * @param {OnName} onName called for each name
  288. * @returns {GeneratedSourceInfo} generated source info
  289. */
  290. streamChunks(options, onChunk, onSource, onName) {
  291. const key = options ? JSON.stringify(options) : "{}";
  292. if (
  293. this._cachedMaps.has(key) &&
  294. (this._cachedBuffer !== undefined || this._cachedSource !== undefined)
  295. ) {
  296. const { source, map } = this.sourceAndMap(options);
  297. if (map) {
  298. return streamChunksOfSourceMap(
  299. /** @type {string} */
  300. (source),
  301. map,
  302. onChunk,
  303. onSource,
  304. onName,
  305. Boolean(options && options.finalSource),
  306. true,
  307. );
  308. }
  309. return streamChunksOfRawSource(
  310. /** @type {string} */
  311. (source),
  312. onChunk,
  313. onSource,
  314. onName,
  315. Boolean(options && options.finalSource),
  316. );
  317. }
  318. const sourceAndMap = streamAndGetSourceAndMap(
  319. this.original(),
  320. options,
  321. onChunk,
  322. onSource,
  323. onName,
  324. );
  325. this._cachedSource = sourceAndMap.source;
  326. this._cachedMaps.set(key, {
  327. map: /** @type {RawSourceMap} */ (sourceAndMap.map),
  328. bufferedMap: undefined,
  329. });
  330. return sourceAndMap.result;
  331. }
  332. /**
  333. * @param {MapOptions=} options map options
  334. * @returns {RawSourceMap | null} map
  335. */
  336. map(options) {
  337. const key = options ? JSON.stringify(options) : "{}";
  338. const cacheEntry = this._cachedMaps.get(key);
  339. if (cacheEntry !== undefined) {
  340. return this._getMapFromCacheEntry(cacheEntry);
  341. }
  342. const map = this.original().map(options);
  343. this._cachedMaps.set(key, {
  344. map,
  345. bufferedMap: undefined,
  346. });
  347. return map;
  348. }
  349. /**
  350. * @param {HashLike} hash hash
  351. * @returns {void}
  352. */
  353. updateHash(hash) {
  354. if (this._cachedHashUpdate !== undefined) {
  355. for (const item of this._cachedHashUpdate) hash.update(item);
  356. return;
  357. }
  358. /** @type {(string | Buffer)[]} */
  359. const update = [];
  360. /** @type {string | undefined} */
  361. let currentString;
  362. const tracker = {
  363. /**
  364. * @param {string | Buffer} item item
  365. * @returns {void}
  366. */
  367. update: (item) => {
  368. if (typeof item === "string" && item.length < 10240) {
  369. if (currentString === undefined) {
  370. currentString = item;
  371. } else {
  372. currentString += item;
  373. if (currentString.length > 102400) {
  374. update.push(Buffer.from(currentString));
  375. currentString = undefined;
  376. }
  377. }
  378. } else {
  379. if (currentString !== undefined) {
  380. update.push(Buffer.from(currentString));
  381. currentString = undefined;
  382. }
  383. update.push(item);
  384. }
  385. },
  386. };
  387. this.original().updateHash(/** @type {HashLike} */ (tracker));
  388. if (currentString !== undefined) {
  389. update.push(Buffer.from(currentString));
  390. }
  391. for (const item of update) hash.update(item);
  392. this._cachedHashUpdate = update;
  393. }
  394. }
  395. module.exports = CachedSource;