refactor(browser): charge source-map records before retention

This commit is contained in:
morluto
2026-10-09 06:32:10 +08:00
parent f7366a517f
commit e762e32afb
+28 -63
View File
@@ -470,35 +470,24 @@ const normalizeSourceMap = (
const sourceJsonBytes = new Map<string, number>(); const sourceJsonBytes = new Map<string, number>();
const nameJsonBytes = new Map<string, number>(); const nameJsonBytes = new Map<string, number>();
const originalSources: ParsedSourceMapItem["original_sources"] = []; const originalSources: ParsedSourceMapItem["original_sources"] = [];
let sourceBytes = 0;
for (const { leaf, resolvedSources } of decoded) { for (const { leaf, resolvedSources } of decoded) {
for (const [index, rawSource] of leaf.map.sources.entries()) { for (const [index, rawSource] of leaf.map.sources.entries()) {
if ((index & 255) === 0) check(); if ((index & 255) === 0) check();
const resolved = const resolved =
resolvedSources[index] ?? rawSource ?? "[unknown-source]"; resolvedSources[index] ?? rawSource ?? "[unknown-source]";
const sanitized = cachedSanitized(sourcesByRaw, resolved); const sanitized = cachedSanitized(sourcesByRaw, resolved);
sourceBytes += const content = leaf.map.sourcesContent?.[index] ?? null;
consumeExpandedBytes(
context.budget,
cachedJsonBytes(sourceJsonBytes, sanitized) + cachedJsonBytes(sourceJsonBytes, sanitized) +
sourceEntryOverheadBytes(); sourceEntryOverheadBytes() +
const content = leaf.map.sourcesContent?.[index] ?? null; (originalSources.length === 0 ? 0 : 1) +
if (content !== null) (content === null
sourceBytes += ? 0
jsonBytes(content) + : jsonBytes(content) +
artifactMetadataBytes(sourceMediaType(rawSource), content) - artifactMetadataBytes(sourceMediaType(rawSource), content) -
4; 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;
originalSources.push({ originalSources.push({
source: sanitized, source: sanitized,
artifact: 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"] = []; const mappings: ParsedSourceMapItem["mappings"] = [];
for (const { leaf, rows, resolvedSources } of decoded) for (const { leaf, rows, resolvedSources } of decoded)
for (const [line, row] of rows.entries()) { for (const [line, row] of rows.entries()) {
@@ -554,10 +510,24 @@ const normalizeSourceMap = (
const source = cachedSanitized(sourcesByRaw, resolved); const source = cachedSanitized(sourcesByRaw, resolved);
const name = const name =
segment.length === 5 ? (leaf.map.names[segment[4]] ?? null) : null; 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({ mappings.push({
generated_line: leaf.offset.line + line + 1, generated_line: generatedLine,
generated_column: generated_column: generatedColumn,
segment[0] + (line === 0 ? leaf.offset.column : 0),
source, source,
original_line: segment[2] + 1, original_line: segment[2] + 1,
original_column: segment[3], original_column: segment[3],
@@ -858,11 +828,6 @@ const sourceMapSuccessReservation = (request: WebSourceMapRequest): number => {
const sourceEntryOverheadBytes = (): number => const sourceEntryOverheadBytes = (): number =>
Buffer.byteLength('{"source":,"artifact":null}'); Buffer.byteLength('{"source":,"artifact":null}');
const originalSourcesCount = (
decoded: ReturnType<typeof decodeValidatedSourceMapLeaves>,
): number =>
decoded.reduce((count, { leaf }) => count + leaf.map.sources.length, 0);
const cachedJsonBytes = (cache: Map<string, number>, value: string): number => { const cachedJsonBytes = (cache: Map<string, number>, value: string): number => {
const cached = cache.get(value); const cached = cache.get(value);
if (cached !== undefined) return cached; if (cached !== undefined) return cached;