|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as Platform from '../../../core/platform/platform.js'; |
| 6 | +// eslint-disable-next-line rulesdir/no-imports-in-directory |
| 7 | +import type * as SDK from '../../../core/sdk/sdk.js'; |
| 8 | +import type * as Protocol from '../../../generated/protocol.js'; |
| 9 | +import * as Types from '../types/types.js'; |
| 10 | + |
| 11 | +export interface ScriptsData { |
| 12 | + /** Note: this is only populated when the "Enhanced Traces" feature is enabled. */ |
| 13 | + scripts: Map<Protocol.Runtime.ScriptId, {}>; |
| 14 | +} |
| 15 | + |
| 16 | +interface Script { |
| 17 | + scriptId: Protocol.Runtime.ScriptId; |
| 18 | + url?: string; |
| 19 | + content?: string; |
| 20 | + sourceMapUrl?: string; |
| 21 | + sourceMap?: SDK.SourceMap.SourceMapV3; |
| 22 | +} |
| 23 | + |
| 24 | +const scriptById = new Map<Protocol.Runtime.ScriptId, Script>(); |
| 25 | + |
| 26 | +export function reset(): void { |
| 27 | + scriptById.clear(); |
| 28 | +} |
| 29 | + |
| 30 | +export function handleEvent(event: Types.Events.Event): void { |
| 31 | + if (Types.Events.isScriptRundownEvent(event)) { |
| 32 | + const {scriptId, url, sourceMapUrl} = event.args.data; |
| 33 | + const script = Platform.MapUtilities.getWithDefault(scriptById, scriptId, () => ({scriptId} as Script)); |
| 34 | + script.url = url; |
| 35 | + // Ignore nonsense values, which is what this was when initially implemented. |
| 36 | + // TODO(cjamcl): https://g-issues.chromium.org/issues/337909145#comment15 |
| 37 | + if (sourceMapUrl && sourceMapUrl !== url && sourceMapUrl.endsWith('.json')) { |
| 38 | + script.sourceMapUrl = sourceMapUrl; |
| 39 | + } |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (Types.Events.isScriptSourceRundownEvent(event)) { |
| 44 | + const {scriptId, sourceText} = event.args.data; |
| 45 | + const script = Platform.MapUtilities.getWithDefault(scriptById, scriptId, () => ({scriptId} as Script)); |
| 46 | + script.content = sourceText; |
| 47 | + return; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export async function finalize(options: Types.Configuration.ParseOptions): Promise<void> { |
| 52 | + if (!options.resolveSourceMap) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const promises = []; |
| 57 | + for (const script of scriptById.values()) { |
| 58 | + if (script.sourceMapUrl) { |
| 59 | + promises.push(options.resolveSourceMap(script.sourceMapUrl).then(sourceMap => { |
| 60 | + script.sourceMap = sourceMap; |
| 61 | + })); |
| 62 | + } |
| 63 | + } |
| 64 | + await Promise.all(promises); |
| 65 | +} |
| 66 | + |
| 67 | +export function data(): ScriptsData { |
| 68 | + return { |
| 69 | + scripts: scriptById, |
| 70 | + }; |
| 71 | +} |
0 commit comments