CachedSource.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. const CACHE_KEY_EMPTY = "{}";
  74. const CACHE_KEY_COLUMNS_FALSE = '{"columns":false}';
  75. const CACHE_KEY_COLUMNS_TRUE = '{"columns":true}';
  76. /**
  77. * Fast-path replacement for `JSON.stringify(options)` when used as a cache
  78. * key. MapOptions / streamChunks Options are both small boolean-only shapes
  79. * and the overwhelmingly common shapes (`undefined`, `{}`, `{columns}`) can
  80. * be keyed without calling `JSON.stringify`, which dominates short-circuit
  81. * cache lookups. Falls back to `JSON.stringify` for any other shape so keys
  82. * remain compatible with previously cached `BufferedMaps` entries.
  83. * @param {undefined | MapOptions | Options} options options
  84. * @returns {string} cache key
  85. */
  86. const getCacheKey = (options) => {
  87. if (!options) return CACHE_KEY_EMPTY;
  88. const { columns } = options;
  89. if (
  90. /** @type {Options} */ (options).source === undefined &&
  91. /** @type {Options} */ (options).finalSource === undefined &&
  92. /** @type {MapOptions} */ (options).module === undefined
  93. ) {
  94. if (columns === undefined) return CACHE_KEY_EMPTY;
  95. return columns ? CACHE_KEY_COLUMNS_TRUE : CACHE_KEY_COLUMNS_FALSE;
  96. }
  97. return JSON.stringify(options);
  98. };
  99. /**
  100. * @typedef {object} CachedData
  101. * @property {boolean=} source source
  102. * @property {Buffer} buffer buffer
  103. * @property {number=} size size
  104. * @property {BufferedMaps} maps maps
  105. * @property {(string | Buffer)[]=} hash hash
  106. */
  107. class CachedSource extends Source {
  108. /**
  109. * @param {Source | (() => Source)} source source
  110. * @param {CachedData=} cachedData cached data
  111. */
  112. constructor(source, cachedData) {
  113. super();
  114. /**
  115. * @private
  116. * @type {Source | (() => Source)}
  117. */
  118. this._source = source;
  119. /**
  120. * @private
  121. * @type {undefined | string}
  122. */
  123. this._cachedSource = undefined;
  124. // Split on `cachedData` once instead of re-evaluating the ternary for
  125. // every field. Under the interpreter (and CodSpeed's simulation) each
  126. // ternary is a separate branch; consolidating cuts the per-instance
  127. // branch count roughly in half.
  128. if (cachedData) {
  129. /**
  130. * @private
  131. * @type {boolean | undefined}
  132. */
  133. this._cachedSourceType = cachedData.source;
  134. /**
  135. * @private
  136. * @type {Buffer | undefined}
  137. */
  138. this._cachedBuffer = cachedData.buffer;
  139. /**
  140. * @private
  141. * @type {number | undefined}
  142. */
  143. this._cachedSize = cachedData.size;
  144. /**
  145. * @private
  146. * @type {BufferedMaps}
  147. */
  148. this._cachedMaps = cachedData.maps;
  149. /**
  150. * @private
  151. * @type {(string | Buffer)[] | undefined}
  152. */
  153. this._cachedHashUpdate = cachedData.hash;
  154. } else {
  155. this._cachedSourceType = undefined;
  156. this._cachedBuffer = undefined;
  157. this._cachedSize = undefined;
  158. this._cachedMaps = new Map();
  159. this._cachedHashUpdate = undefined;
  160. }
  161. }
  162. /**
  163. * @returns {CachedData} cached data
  164. */
  165. getCachedData() {
  166. /** @type {BufferedMaps} */
  167. const bufferedMaps = new Map();
  168. for (const pair of this._cachedMaps) {
  169. const [, cacheEntry] = pair;
  170. if (cacheEntry.bufferedMap === undefined) {
  171. cacheEntry.bufferedMap = mapToBufferedMap(
  172. this._getMapFromCacheEntry(cacheEntry),
  173. );
  174. }
  175. bufferedMaps.set(pair[0], {
  176. map: undefined,
  177. bufferedMap: cacheEntry.bufferedMap,
  178. });
  179. }
  180. return {
  181. // We don't want to cache strings
  182. // So if we have a caches sources
  183. // create a buffer from it and only store
  184. // if it was a Buffer or string
  185. buffer: this._cachedSource
  186. ? this.buffer()
  187. : /** @type {Buffer} */ (this._cachedBuffer),
  188. source:
  189. this._cachedSourceType !== undefined
  190. ? this._cachedSourceType
  191. : typeof this._cachedSource === "string"
  192. ? true
  193. : Buffer.isBuffer(this._cachedSource)
  194. ? false
  195. : undefined,
  196. size: this._cachedSize,
  197. maps: bufferedMaps,
  198. hash: this._cachedHashUpdate,
  199. };
  200. }
  201. originalLazy() {
  202. return this._source;
  203. }
  204. original() {
  205. if (typeof this._source === "function") this._source = this._source();
  206. return this._source;
  207. }
  208. /**
  209. * @returns {SourceValue} source
  210. */
  211. source() {
  212. // Fully inlined _getCachedSource: both warm- and cold-cache paths skip
  213. // the prototype method lookup / stack frame the interpreter would
  214. // otherwise pay on every call.
  215. if (this._cachedSource !== undefined) return this._cachedSource;
  216. const cachedBuffer = this._cachedBuffer;
  217. const cachedSourceType = this._cachedSourceType;
  218. if (cachedBuffer !== undefined && cachedSourceType !== undefined) {
  219. const value = cachedSourceType
  220. ? cachedBuffer.toString("utf8")
  221. : cachedBuffer;
  222. if (isDualStringBufferCachingEnabled()) {
  223. this._cachedSource = /** @type {string} */ (value);
  224. }
  225. return /** @type {string} */ (value);
  226. }
  227. return (this._cachedSource =
  228. /** @type {string} */
  229. (this.original().source()));
  230. }
  231. /**
  232. * @private
  233. * @param {BufferEntry} cacheEntry cache entry
  234. * @returns {null | RawSourceMap} raw source map
  235. */
  236. _getMapFromCacheEntry(cacheEntry) {
  237. if (cacheEntry.map !== undefined) {
  238. return cacheEntry.map;
  239. } else if (cacheEntry.bufferedMap !== undefined) {
  240. return (cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap));
  241. }
  242. return null;
  243. }
  244. /**
  245. * @private
  246. * @returns {undefined | string} cached source
  247. */
  248. _getCachedSource() {
  249. if (this._cachedSource !== undefined) return this._cachedSource;
  250. if (this._cachedBuffer && this._cachedSourceType !== undefined) {
  251. const value = this._cachedSourceType
  252. ? this._cachedBuffer.toString("utf8")
  253. : this._cachedBuffer;
  254. if (isDualStringBufferCachingEnabled()) {
  255. this._cachedSource = /** @type {string} */ (value);
  256. }
  257. return /** @type {string} */ (value);
  258. }
  259. }
  260. /**
  261. * @returns {Buffer} buffer
  262. */
  263. buffer() {
  264. if (this._cachedBuffer !== undefined) return this._cachedBuffer;
  265. if (this._cachedBuffers !== undefined) {
  266. return (this._cachedBuffer = Buffer.concat(this._cachedBuffers));
  267. }
  268. if (this._cachedSource !== undefined) {
  269. const value = Buffer.isBuffer(this._cachedSource)
  270. ? this._cachedSource
  271. : Buffer.from(this._cachedSource, "utf8");
  272. if (isDualStringBufferCachingEnabled()) {
  273. this._cachedBuffer = value;
  274. }
  275. return value;
  276. }
  277. if (typeof this.original().buffer === "function") {
  278. return (this._cachedBuffer = this.original().buffer());
  279. }
  280. const bufferOrString = this.source();
  281. if (Buffer.isBuffer(bufferOrString)) {
  282. return (this._cachedBuffer = bufferOrString);
  283. }
  284. const value = Buffer.from(bufferOrString, "utf8");
  285. if (isDualStringBufferCachingEnabled()) {
  286. this._cachedBuffer = value;
  287. }
  288. return value;
  289. }
  290. /**
  291. * @returns {Buffer[]} buffers
  292. */
  293. buffers() {
  294. if (this._cachedBuffers !== undefined) return this._cachedBuffers;
  295. if (this._cachedBuffer !== undefined) {
  296. return (this._cachedBuffers = [this._cachedBuffer]);
  297. }
  298. const original = this.original();
  299. if (typeof original.buffers === "function") {
  300. return (this._cachedBuffers = original.buffers());
  301. }
  302. return (this._cachedBuffers = [this.buffer()]);
  303. }
  304. /**
  305. * @returns {number} size
  306. */
  307. size() {
  308. if (this._cachedSize !== undefined) return this._cachedSize;
  309. if (this._cachedBuffer !== undefined) {
  310. return (this._cachedSize = this._cachedBuffer.length);
  311. }
  312. const source = this._getCachedSource();
  313. if (source !== undefined) {
  314. return (this._cachedSize = Buffer.byteLength(source));
  315. }
  316. return (this._cachedSize = this.original().size());
  317. }
  318. /**
  319. * @param {MapOptions=} options map options
  320. * @returns {SourceAndMap} source and map
  321. */
  322. sourceAndMap(options) {
  323. const key = getCacheKey(options);
  324. const cacheEntry = this._cachedMaps.get(key);
  325. // Look for a cached map
  326. if (cacheEntry !== undefined) {
  327. // We have a cached map in some representation
  328. const map = this._getMapFromCacheEntry(cacheEntry);
  329. // Either get the cached source or compute it
  330. return { source: this.source(), map };
  331. }
  332. // Look for a cached source
  333. let source = this._getCachedSource();
  334. // Compute the map
  335. let map;
  336. if (source !== undefined) {
  337. map = this.original().map(options);
  338. } else {
  339. // Compute the source and map together.
  340. const sourceAndMap = this.original().sourceAndMap(options);
  341. source = /** @type {string} */ (sourceAndMap.source);
  342. map = sourceAndMap.map;
  343. this._cachedSource = source;
  344. }
  345. this._cachedMaps.set(key, {
  346. map,
  347. bufferedMap: undefined,
  348. });
  349. return { source, map };
  350. }
  351. /**
  352. * @param {Options} options options
  353. * @param {OnChunk} onChunk called for each chunk of code
  354. * @param {OnSource} onSource called for each source
  355. * @param {OnName} onName called for each name
  356. * @returns {GeneratedSourceInfo} generated source info
  357. */
  358. streamChunks(options, onChunk, onSource, onName) {
  359. const key = getCacheKey(options);
  360. if (
  361. this._cachedMaps.has(key) &&
  362. (this._cachedBuffer !== undefined || this._cachedSource !== undefined)
  363. ) {
  364. const { source, map } = this.sourceAndMap(options);
  365. if (map) {
  366. return streamChunksOfSourceMap(
  367. /** @type {string} */
  368. (source),
  369. map,
  370. onChunk,
  371. onSource,
  372. onName,
  373. Boolean(options && options.finalSource),
  374. true,
  375. );
  376. }
  377. return streamChunksOfRawSource(
  378. /** @type {string} */
  379. (source),
  380. onChunk,
  381. onSource,
  382. onName,
  383. Boolean(options && options.finalSource),
  384. );
  385. }
  386. const sourceAndMap = streamAndGetSourceAndMap(
  387. this.original(),
  388. options,
  389. onChunk,
  390. onSource,
  391. onName,
  392. );
  393. this._cachedSource = sourceAndMap.source;
  394. this._cachedMaps.set(key, {
  395. map: /** @type {RawSourceMap} */ (sourceAndMap.map),
  396. bufferedMap: undefined,
  397. });
  398. return sourceAndMap.result;
  399. }
  400. /**
  401. * @param {MapOptions=} options map options
  402. * @returns {RawSourceMap | null} map
  403. */
  404. map(options) {
  405. const key = getCacheKey(options);
  406. const cacheEntry = this._cachedMaps.get(key);
  407. if (cacheEntry !== undefined) {
  408. return this._getMapFromCacheEntry(cacheEntry);
  409. }
  410. const map = this.original().map(options);
  411. this._cachedMaps.set(key, {
  412. map,
  413. bufferedMap: undefined,
  414. });
  415. return map;
  416. }
  417. /**
  418. * @param {HashLike} hash hash
  419. * @returns {void}
  420. */
  421. updateHash(hash) {
  422. if (this._cachedHashUpdate !== undefined) {
  423. for (const item of this._cachedHashUpdate) hash.update(item);
  424. return;
  425. }
  426. /** @type {(string | Buffer)[]} */
  427. const update = [];
  428. /** @type {string | undefined} */
  429. let currentString;
  430. const tracker = {
  431. /**
  432. * @param {string | Buffer} item item
  433. * @returns {void}
  434. */
  435. update: (item) => {
  436. if (typeof item === "string" && item.length < 10240) {
  437. if (currentString === undefined) {
  438. currentString = item;
  439. } else {
  440. currentString += item;
  441. if (currentString.length > 102400) {
  442. update.push(Buffer.from(currentString));
  443. currentString = undefined;
  444. }
  445. }
  446. } else {
  447. if (currentString !== undefined) {
  448. update.push(Buffer.from(currentString));
  449. currentString = undefined;
  450. }
  451. update.push(item);
  452. }
  453. },
  454. };
  455. this.original().updateHash(/** @type {HashLike} */ (tracker));
  456. if (currentString !== undefined) {
  457. update.push(Buffer.from(currentString));
  458. }
  459. for (const item of update) hash.update(item);
  460. this._cachedHashUpdate = update;
  461. }
  462. }
  463. module.exports = CachedSource;