-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathsource-map-symbolication.ts
More file actions
141 lines (131 loc) · 4.85 KB
/
Copy pathsource-map-symbolication.ts
File metadata and controls
141 lines (131 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { getRawProfileSharedData } from 'firefox-profiler/selectors';
import { applySourceMapSymbolicationResponse } from 'firefox-profiler/profile-logic/source-map-symbolication';
import type {
WorkerInput,
WorkerOutput,
} from 'firefox-profiler/profile-logic/source-map-worker-types';
import type { IndexIntoSourceTable, ThunkAction } from 'firefox-profiler/types';
import type { RawSourceMap } from 'source-map';
import { assertExhaustiveCheck } from 'firefox-profiler/utils/types';
/**
* Run source map symbolication using previously-fetched source maps from Redux
* state. Offloads source parsing and source-map lookups to a dedicated Web
* Worker so the main thread stays responsive.
*
* Reads the current profile from Redux state at dispatch time. Callers must
* ensure native symbolication has already committed its changes before
* dispatching this action, so that JS symbolication builds on the final state.
*
* `compiledSources` maps bundle IndexIntoSourceTable values to compiled source
* text (fetched alongside the source maps). Used for scope-tree-based function
* name resolution.
*/
export function doSourceMapSymbolication(
resolvedSourceMaps: Map<IndexIntoSourceTable, RawSourceMap>,
compiledSources: Map<IndexIntoSourceTable, string>
): ThunkAction<Promise<void>> {
return async (dispatch, getState) => {
if (resolvedSourceMaps.size === 0) {
return;
}
const shared = getRawProfileSharedData(getState());
const input: WorkerInput = {
resolvedSourceMaps,
compiledSources,
funcTable: shared.funcTable,
frameTable: shared.frameTable,
sourceLocationTable: shared.sourceLocationTable,
sources: shared.sources,
stringArray: shared.stringArray,
};
dispatch({ type: 'START_SOURCE_MAP_SYMBOLICATION' });
const result = await _runSourceMapWorker(input);
switch (result.type) {
case 'success': {
// Apply against the current shared state (not the snapshot the worker
// received), so concurrent worker runs compose instead of stomping
// each other's results.
const currentShared = getRawProfileSharedData(getState());
const applied = applySourceMapSymbolicationResponse(
currentShared,
result.response
);
if (applied === null) {
dispatch({ type: 'SOURCE_MAP_SYMBOLICATION_FAILED' });
break;
}
dispatch({
type: 'BULK_SOURCE_MAP_SYMBOLICATION',
newFuncTable: applied.newFuncTable,
newFrameTable: applied.newFrameTable,
newSourceLocationTable: applied.newSourceLocationTable,
newSources: applied.newSources,
newStringArray: applied.newStringArray,
});
break;
}
case 'error':
console.warn('Source map worker error:', result.message);
dispatch({ type: 'SOURCE_MAP_SYMBOLICATION_FAILED' });
break;
case 'no-op':
dispatch({ type: 'SOURCE_MAP_SYMBOLICATION_FAILED' });
break;
default:
throw assertExhaustiveCheck(result);
}
};
}
/**
* Spawn a one-shot source map worker, send it the input, and return the
* output. The worker is terminated once a response is received or an error
* occurs. Uses the same on-demand spawn pattern as gz.browser.ts.
*
* Debugging tip: to step through symbolication from the page DevTools, paste
* the snippet below over this function body. It runs the same core on the
* main thread (blocking, debug only). The worker no longer mutates its
* input, so no defensive cloning is needed here.
*
* const { runSourceMapSymbolicationCore } = await import(
* 'firefox-profiler/profile-logic/source-map-symbolication'
* );
* const wasmUrl = new URL('/mappings.wasm', window.location.href).href;
* return runSourceMapSymbolicationCore(input, wasmUrl);
*/
function _runSourceMapWorker(input: WorkerInput): Promise<WorkerOutput> {
return new Promise((resolve) => {
const reportError = (err: unknown): void => {
resolve({
type: 'error',
message: err instanceof Error ? err.message : String(err),
});
};
let worker: Worker;
try {
worker = new Worker(SOURCE_MAP_WORKER_PATH);
} catch (err) {
reportError(err);
return;
}
worker.onmessage = (e: MessageEvent<WorkerOutput>) => {
resolve(e.data);
worker.terminate();
};
worker.onerror = (e: ErrorEvent) => {
resolve({
type: 'error',
message: e.error?.message ?? e.message ?? 'Source map worker failed',
});
worker.terminate();
};
try {
worker.postMessage(input);
} catch (err) {
reportError(err);
worker.terminate();
}
});
}