From e762e32afbba3fd32532fd42a3f4fdcd05252ad3 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 9 Oct 2026 06:29:11 +0800 Subject: [PATCH] refactor(browser): charge source-map records before retention --- src/browser/WebSourceMapFetcher.ts | 91 +++++++++--------------------- 1 file changed, 28 insertions(+), 63 deletions(-) diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts index 5464fcc0f8..f5657a77b2 100644 --- a/src/browser/WebSourceMapFetcher.ts +++ b/src/browser/WebSourceMapFetcher.ts @@ -470,35 +470,24 @@ const normalizeSourceMap = ( const sourceJsonBytes = new Map(); const nameJsonBytes = new Map(); const originalSources: ParsedSourceMapItem["original_sources"] = []; - let sourceBytes = 0; for (const { leaf, resolvedSources } of decoded) { for (const [index, rawSource] of leaf.map.sources.entries()) { if ((index & 255) === 0) check(); const resolved = resolvedSources[index] ?? rawSource ?? "[unknown-source]"; const sanitized = cachedSanitized(sourcesByRaw, resolved); - sourceBytes += + const content = leaf.map.sourcesContent?.[index] ?? null; + consumeExpandedBytes( + context.budget, cachedJsonBytes(sourceJsonBytes, sanitized) + - sourceEntryOverheadBytes(); - const content = leaf.map.sourcesContent?.[index] ?? null; - if (content !== null) - sourceBytes += - jsonBytes(content) + - artifactMetadataBytes(sourceMediaType(rawSource), content) - - 4; - } - } - consumeExpandedBytes( - context.budget, - sourceBytes + originalSourcesCount(decoded), - ); - for (const { leaf, resolvedSources } of decoded) { - for (const [index, rawSource] of leaf.map.sources.entries()) { - if ((index & 255) === 0) check(); - const resolved = - resolvedSources[index] ?? rawSource ?? "[unknown-source]"; - const sanitized = cachedSanitized(sourcesByRaw, resolved); - const content = leaf.map.sourcesContent?.[index] ?? null; + sourceEntryOverheadBytes() + + (originalSources.length === 0 ? 0 : 1) + + (content === null + ? 0 + : jsonBytes(content) + + artifactMetadataBytes(sourceMediaType(rawSource), content) - + 4), + ); originalSources.push({ source: sanitized, artifact: @@ -508,39 +497,6 @@ const normalizeSourceMap = ( }); } } - let mappingBytes = 0; - let mappingCount = 0; - for (const { leaf, rows, resolvedSources } of decoded) - for (const [line, row] of rows.entries()) { - if ((line & 255) === 0) check(); - for (const segment of row) { - if (!isBeforeSourceMapLeafStop(leaf, line, segment[0])) continue; - if (segment.length === 1) continue; - const rawSource = leaf.map.sources[segment[1]]; - const source = cachedSanitized( - sourcesByRaw, - resolvedSources[segment[1]] ?? rawSource ?? "[unknown-source]", - ); - const name = - segment.length === 5 ? (leaf.map.names[segment[4]] ?? null) : null; - mappingBytes += - cachedJsonBytes(sourceJsonBytes, source) + - (name === null ? 4 : cachedJsonBytes(nameJsonBytes, name)) + - mappingRowBytes( - leaf.offset.line + line + 1, - segment[0] + (line === 0 ? leaf.offset.column : 0), - segment[2] + 1, - segment[3], - ); - mappingCount += 1; - if ( - mappingBytes > - WEB_SOURCE_MAP_LIMITS.outputBytes - context.budget.expandedBytes - ) - throw expandedOutputFailure(); - } - } - consumeExpandedBytes(context.budget, mappingBytes + mappingCount); const mappings: ParsedSourceMapItem["mappings"] = []; for (const { leaf, rows, resolvedSources } of decoded) for (const [line, row] of rows.entries()) { @@ -554,10 +510,24 @@ const normalizeSourceMap = ( const source = cachedSanitized(sourcesByRaw, resolved); const name = segment.length === 5 ? (leaf.map.names[segment[4]] ?? null) : null; + const generatedLine = leaf.offset.line + line + 1; + const generatedColumn = + segment[0] + (line === 0 ? leaf.offset.column : 0); + consumeExpandedBytes( + context.budget, + cachedJsonBytes(sourceJsonBytes, source) + + (name === null ? 4 : cachedJsonBytes(nameJsonBytes, name)) + + mappingRowBytes( + generatedLine, + generatedColumn, + segment[2] + 1, + segment[3], + ) + + (mappings.length === 0 ? 0 : 1), + ); mappings.push({ - generated_line: leaf.offset.line + line + 1, - generated_column: - segment[0] + (line === 0 ? leaf.offset.column : 0), + generated_line: generatedLine, + generated_column: generatedColumn, source, original_line: segment[2] + 1, original_column: segment[3], @@ -858,11 +828,6 @@ const sourceMapSuccessReservation = (request: WebSourceMapRequest): number => { const sourceEntryOverheadBytes = (): number => Buffer.byteLength('{"source":,"artifact":null}'); -const originalSourcesCount = ( - decoded: ReturnType, -): number => - decoded.reduce((count, { leaf }) => count + leaf.map.sources.length, 0); - const cachedJsonBytes = (cache: Map, value: string): number => { const cached = cache.get(value); if (cached !== undefined) return cached;