From 491829bd86611c66a80a461f2f9fba5853f20aaf Mon Sep 17 00:00:00 2001 From: Christopher Dwyer-Perkins Date: Wed, 22 Jan 2025 16:50:36 -0400 Subject: [PATCH] Added some more caching logic to the SourceMapManager --- src/managers/SourceMapManager.ts | 74 ++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/src/managers/SourceMapManager.ts b/src/managers/SourceMapManager.ts index 4523481b..96b68da5 100644 --- a/src/managers/SourceMapManager.ts +++ b/src/managers/SourceMapManager.ts @@ -47,6 +47,8 @@ export class SourceMapManager { } catch (e) { this.logger.error(`Error loading or parsing source map for '${sourceMapPath}'`, e); } + } else { + this.cache[key] = null; } } return this.cache[key]; @@ -88,38 +90,46 @@ export class SourceMapManager { let sourceMapPath = `${filePath}.map`; //if we have a source map, use it - if (await fsExtra.pathExists(sourceMapPath)) { - let parsedSourceMap = await this.getSourceMap(sourceMapPath); - if (parsedSourceMap) { - let position = await SourceMapConsumer.with(parsedSourceMap, null, (consumer) => { - return consumer.originalPositionFor({ - line: currentLineNumber, - column: currentColumnIndex, - bias: SourceMapConsumer.LEAST_UPPER_BOUND - }); - }); - if (position?.source) { - return { - columnIndex: position.column, - lineNumber: position.line, - filePath: position.source - }; - } - //if the sourcemap didn't find a valid mapped location, - //try to fallback to the first source referenced in the map - if (parsedSourceMap.sources?.[0]) { - return { - columnIndex: currentColumnIndex, - lineNumber: currentLineNumber, - filePath: parsedSourceMap.sources[0] - }; - } else { - return undefined; - } + let parsedSourceMap = await this.getSourceMap(sourceMapPath); + if (parsedSourceMap) { + const consumer = await this.getSourceMapConsumer(sourceMapPath, parsedSourceMap); + let position = consumer.originalPositionFor({ + line: currentLineNumber, + column: currentColumnIndex, + bias: SourceMapConsumer.LEAST_UPPER_BOUND + }); + if (position?.source) { + return { + columnIndex: position.column, + lineNumber: position.line, + filePath: position.source + }; + } + //if the sourcemap didn't find a valid mapped location, + //try to fallback to the first source referenced in the map + if (parsedSourceMap.sources?.[0]) { + return { + columnIndex: currentColumnIndex, + lineNumber: currentLineNumber, + filePath: parsedSourceMap.sources[0] + }; + } else { + return undefined; } } } + private sourceMapConsumerCache: Record = {}; + + private async getSourceMapConsumer(sourceMapPath: string, parsedSourceMap: RawSourceMap) { + let consumer = this.sourceMapConsumerCache[sourceMapPath]; + if (!consumer) { + consumer = await new SourceMapConsumer(parsedSourceMap); + this.sourceMapConsumerCache[sourceMapPath] = consumer; + } + return consumer; + } + /** * Given a source location, find the generated location using source maps */ @@ -159,4 +169,12 @@ export class SourceMapManager { })); return locations; } + + public destroy() { + // Clean up all source map consumers + for (let key in this.sourceMapConsumerCache) { + let consumer = this.sourceMapConsumerCache[key]; + consumer?.destroy?.(); + } + } }