Merge pull request #99 from guyyt-msft/user/guyomtov/optionalRemoveSourcesContent

add optional removeSourcesContent param
This commit is contained in:
Alex Reitbort 2023-07-23 18:17:39 +03:00 коммит произвёл GitHub
Родитель 377d4373c9 dff82ea699
Коммит cfea69483d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 52 добавлений и 17 удалений

Просмотреть файл

@ -16,10 +16,10 @@ namespace SourcemapToolkit.CallstackDeminifier
private readonly ISourceMapProvider _sourceMapProvider;
private readonly KeyValueCache<string, SourceMap> _sourceMapCache;
public SourceMapStore(ISourceMapProvider sourceMapProvider)
public SourceMapStore(ISourceMapProvider sourceMapProvider, bool removeSourcesContent)
{
_sourceMapProvider = sourceMapProvider;
_sourceMapParser = new SourceMapParser();
_sourceMapParser = new SourceMapParser(removeSourcesContent);
_sourceMapCache = new KeyValueCache<string, SourceMap>(sourceCodeUrl => _sourceMapParser.ParseSourceMap(_sourceMapProvider.GetSourceMapContentsForCallstackUrl(sourceCodeUrl)));
}

Просмотреть файл

@ -28,9 +28,10 @@ namespace SourcemapToolkit.CallstackDeminifier
/// </summary>
/// <param name="sourceMapProvider">Consumers of the API should implement this interface, which provides the source map for a given JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param>
/// <param name="generatedCodeProvider">Consumers of the API should implement this interface, which provides the contents of a JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param>
public static StackTraceDeminifier GetStackTraceDeminfier(ISourceMapProvider sourceMapProvider, ISourceCodeProvider generatedCodeProvider)
/// <param name="removeSourcesContent">Optional parameter that will remove "SourcesContent" data from the loaded source maps, which will use less memory for the cached map files</param>
public static StackTraceDeminifier GetStackTraceDeminfier(ISourceMapProvider sourceMapProvider, ISourceCodeProvider generatedCodeProvider, bool removeSourcesContent = false)
{
return GetStackTraceDeminfier(sourceMapProvider, generatedCodeProvider, new StackTraceParser());
return GetStackTraceDeminfier(sourceMapProvider, generatedCodeProvider, new StackTraceParser(), removeSourcesContent);
}
/// <summary>
@ -39,11 +40,12 @@ namespace SourcemapToolkit.CallstackDeminifier
/// <param name="sourceMapProvider">Consumers of the API should implement this interface, which provides the source map for a given JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param>
/// <param name="generatedCodeProvider">Consumers of the API should implement this interface, which provides the contents of a JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param>
/// <param name="stackTraceParser">Consumers of the API should implement this interface, which provides a parser for the stacktrace. Throws ArgumentNullException if the parameter is set to null.</param>
public static StackTraceDeminifier GetStackTraceDeminfier(ISourceMapProvider sourceMapProvider, ISourceCodeProvider generatedCodeProvider, IStackTraceParser stackTraceParser)
/// <param name="removeSourcesContent">Optional parameter that will remove "SourcesContent" data from the loaded source maps, which will use less memory for the cached map files</param>
public static StackTraceDeminifier GetStackTraceDeminfier(ISourceMapProvider sourceMapProvider, ISourceCodeProvider generatedCodeProvider, IStackTraceParser stackTraceParser, bool removeSourcesContent = false)
{
ValidateArguments(sourceMapProvider, generatedCodeProvider, stackTraceParser);
ISourceMapStore sourceMapStore = new SourceMapStore(sourceMapProvider);
ISourceMapStore sourceMapStore = new SourceMapStore(sourceMapProvider, removeSourcesContent);
IStackFrameDeminifier stackFrameDeminifier = new StackFrameDeminifier(sourceMapStore,
new FunctionMapStore(generatedCodeProvider, sourceMapStore.GetSourceMapForUrl), new FunctionMapConsumer());
@ -55,9 +57,10 @@ namespace SourcemapToolkit.CallstackDeminifier
/// StackTrace deminifiers created with this method will keep source maps cached, and thus use significantly more memory during runtime than the ones generated with GetMethodNameOnlyStackTraceDeminfier.
/// </summary>
/// <param name="sourceMapProvider">Consumers of the API should implement this interface, which provides the source map for a given JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param>
public static StackTraceDeminifier GetMapOnlyStackTraceDeminfier(ISourceMapProvider sourceMapProvider)
/// <param name="removeSourcesContent">Optional parameter that will remove "SourcesContent" data from the loaded source maps, which will use less memory for the cached map files</param>
public static StackTraceDeminifier GetMapOnlyStackTraceDeminfier(ISourceMapProvider sourceMapProvider, bool removeSourcesContent = false)
{
return GetMapOnlyStackTraceDeminfier(sourceMapProvider, new StackTraceParser());
return GetMapOnlyStackTraceDeminfier(sourceMapProvider, new StackTraceParser(), removeSourcesContent);
}
/// <summary>
@ -66,7 +69,8 @@ namespace SourcemapToolkit.CallstackDeminifier
/// </summary>
/// <param name="sourceMapProvider">Consumers of the API should implement this interface, which provides the source map for a given JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param>
/// <param name="stackTraceParser">Consumers of the API should implement this interface, which provides a parser for the stacktrace. Throws ArgumentNullException if the parameter is set to null.</param>
public static StackTraceDeminifier GetMapOnlyStackTraceDeminfier(ISourceMapProvider sourceMapProvider, IStackTraceParser stackTraceParser)
/// <param name="removeSourcesContent">Optional parameter that will remove "SourcesContent" data from the loaded source maps, which will use less memory for the cached map files</param>
public static StackTraceDeminifier GetMapOnlyStackTraceDeminfier(ISourceMapProvider sourceMapProvider, IStackTraceParser stackTraceParser, bool removeSourcesContent = false)
{
if (sourceMapProvider == null)
{
@ -78,7 +82,7 @@ namespace SourcemapToolkit.CallstackDeminifier
throw new ArgumentNullException(nameof(stackTraceParser));
}
ISourceMapStore sourceMapStore = new SourceMapStore(sourceMapProvider);
ISourceMapStore sourceMapStore = new SourceMapStore(sourceMapProvider, removeSourcesContent);
IStackFrameDeminifier stackFrameDeminifier = new StackFrameDeminifier(sourceMapStore);
return new StackTraceDeminifier(stackFrameDeminifier, stackTraceParser);
@ -93,7 +97,7 @@ namespace SourcemapToolkit.CallstackDeminifier
{
return GetMethodNameOnlyStackTraceDeminfier(sourceMapProvider, generatedCodeProvider, new StackTraceParser());
}
/// <summary>
/// Creates a StackTraceDeminifier that only deminifies the method names. StackTrace deminifiers created with this method will use significantly less memory during runtime than the
/// </summary>
@ -103,7 +107,7 @@ namespace SourcemapToolkit.CallstackDeminifier
public static StackTraceDeminifier GetMethodNameOnlyStackTraceDeminfier(ISourceMapProvider sourceMapProvider, ISourceCodeProvider generatedCodeProvider, IStackTraceParser stackTraceParser)
{
ValidateArguments(sourceMapProvider, generatedCodeProvider, stackTraceParser);
SourceMapParser sourceMapParser = new SourceMapParser();
IStackFrameDeminifier stackFrameDeminifier = new MethodNameStackFrameDeminifier(new FunctionMapStore(generatedCodeProvider, (url) => sourceMapParser.ParseSourceMap(sourceMapProvider.GetSourceMapContentsForCallstackUrl(url))), new FunctionMapConsumer());

Просмотреть файл

@ -8,10 +8,12 @@ namespace SourcemapToolkit.SourcemapParser
public class SourceMapParser
{
private readonly MappingsListParser _mappingsListParser;
private readonly bool _removeSourcesContent;
public SourceMapParser()
public SourceMapParser(bool removeSourcesContent = false)
{
_mappingsListParser = new MappingsListParser();
_removeSourcesContent = removeSourcesContent;
}
/// <summary>
@ -36,7 +38,15 @@ namespace SourcemapToolkit.SourcemapParser
RemoveExtraSpaceFromList(parsedMappings);
RemoveExtraSpaceFromList(deserializedSourceMap.Sources);
RemoveExtraSpaceFromList(deserializedSourceMap.Names);
RemoveExtraSpaceFromList(deserializedSourceMap.SourcesContent);
if (_removeSourcesContent && deserializedSourceMap.SourcesContent != null)
{
deserializedSourceMap.SourcesContent.Clear();
}
else
{
RemoveExtraSpaceFromList(deserializedSourceMap.SourcesContent);
}
SourceMap result = new SourceMap(
version: deserializedSourceMap.Version,

Просмотреть файл

@ -20,7 +20,7 @@ namespace SourcemapToolkit.CallstackDeminifier.UnitTests
ISourceCodeProvider sourceCodeProvider = MockRepository.GenerateStrictMock<ISourceCodeProvider>();
sourceCodeProvider.Stub(x => x.GetSourceCode("http://localhost:11323/closurecrashcauser.minified.js")).Return(UnitTestUtils.StreamReaderFromString(GeneratedCodeString));
return StackTraceDeminfierFactory.GetStackTraceDeminfier(sourceMapProvider, sourceCodeProvider);
return StackTraceDeminfierFactory.GetStackTraceDeminfier(sourceMapProvider, sourceCodeProvider, removeSourcesContent: true);
}
private void ValidateDeminifyStackTraceResults(DeminifyStackTraceResult results)

Просмотреть файл

@ -19,7 +19,7 @@ namespace SourcemapToolkit.CallstackDeminifier.UnitTests
ISourceCodeProvider sourceCodeProvider = MockRepository.GenerateStrictMock<ISourceCodeProvider>();
sourceCodeProvider.Stub(x => x.GetSourceCode("http://localhost:11323/crashcauser.min.js")).Return(UnitTestUtils.StreamReaderFromString(GeneratedCodeString));
return StackTraceDeminfierFactory.GetStackTraceDeminfier(sourceMapProvider, sourceCodeProvider);
return StackTraceDeminfierFactory.GetStackTraceDeminfier(sourceMapProvider, sourceCodeProvider, removeSourcesContent: true);
}
private static void ValidateDeminifyStackTraceResults(DeminifyStackTraceResult results)

Просмотреть файл

@ -40,5 +40,26 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
Assert.Equal("CommonStrings", output.Names[0]);
Assert.Equal("afrikaans", output.Names[1]);
}
[Fact]
public void ParseSourceMap_SimpleSourceMapWithRemovedSourcesContent_CorrectlyParsed()
{
// Arrange
SourceMapParser sourceMapParser = new SourceMapParser(removeSourcesContent: true);
string input = "{ \"version\":3, \"file\":\"CommonIntl\", \"lineCount\":65, \"mappings\":\"AACAA,aAAA,CAAc\", \"sources\":[\"input/CommonIntl.js\"], \"names\":[\"CommonStrings\",\"afrikaans\"], \"sourcescontent\":[\"CommonStrings.afrikaans(test)...\"]}";
// Act
SourceMap output = sourceMapParser.ParseSourceMap(UnitTestUtils.StreamReaderFromString(input));
// Assert
Assert.Equal(3, output.Version);
Assert.Equal("CommonIntl", output.File);
Assert.Equal("AACAA,aAAA,CAAc", output.Mappings);
Assert.Single(output.Sources);
Assert.Equal("input/CommonIntl.js", output.Sources[0]);
Assert.Equal(2, output.Names.Count);
Assert.Equal("CommonStrings", output.Names[0]);
Assert.Equal("afrikaans", output.Names[1]);
}
}
}