-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsourceMapTransformer.ts
252 lines (217 loc) · 11.5 KB
/
sourceMapTransformer.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as path from 'path';
import * as fs from 'fs';
import {DebugProtocol} from 'vscode-debugprotocol';
import {ISourceMaps, SourceMaps} from './sourceMaps';
import {ISetBreakpointsArgs, IDebugTransformer, ILaunchRequestArgs, IAttachRequestArgs, ISetBreakpointsResponseBody, IStackTraceResponseBody} from '../../webkit/WebKitAdapterInterfaces';
import * as utils from '../../webkit/utilities';
interface IPendingBreakpoint {
resolve: () => void;
reject: (e: Error) => void;
args: ISetBreakpointsArgs;
requestSeq: number;
}
/**
* If sourcemaps are enabled, converts from source files on the client side to runtime files on the target side
*/
export class SourceMapTransformer implements IDebugTransformer {
private _sourceMaps: ISourceMaps;
private _requestSeqToSetBreakpointsArgs: Map<number, ISetBreakpointsArgs>;
private _allRuntimeScriptPaths: Set<string>;
private _pendingBreakpointsByPath = new Map<string, IPendingBreakpoint>();
private _webRoot: string;
private _authoredPathsToMappedBPLines: Map<string, number[]>;
private _authoredPathsToMappedBPCols: Map<string, number[]>;
public launch(args: ILaunchRequestArgs): void {
this.init(args);
}
public attach(args: IAttachRequestArgs): void {
this.init(args);
}
private init(args: ILaunchRequestArgs | IAttachRequestArgs): void {
if (args.sourceMaps) {
this._webRoot = utils.getAppRoot(args);
this._sourceMaps = new SourceMaps(this._webRoot);
this._requestSeqToSetBreakpointsArgs = new Map<number, ISetBreakpointsArgs>();
this._allRuntimeScriptPaths = new Set<string>();
this._authoredPathsToMappedBPLines = new Map<string, number[]>();
this._authoredPathsToMappedBPCols = new Map<string, number[]>();
}
}
public clearTargetContext(): void {
this._allRuntimeScriptPaths = new Set<string>();
}
/**
* Apply sourcemapping to the setBreakpoints request path/lines
*/
public setBreakpoints(args: ISetBreakpointsArgs, requestSeq: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (this._sourceMaps && args.source.path && path.extname(args.source.path) !== ".js") {
const argsPath = args.source.path;
const mappedPath = this._sourceMaps.MapPathFromSource(argsPath);
if (mappedPath) {
utils.Logger.log(`SourceMaps.setBP: Mapped ${argsPath} to ${mappedPath}`);
args.authoredPath = argsPath;
args.source.path = mappedPath;
// DebugProtocol doesn't send cols, but they need to be added from sourcemaps
const mappedCols = [];
const mappedLines = args.lines.map((line, i) => {
const mapped = this._sourceMaps.MapFromSource(argsPath, line, /*column=*/0);
if (mapped) {
utils.Logger.log(`SourceMaps.setBP: Mapped ${argsPath}:${line}:0 to ${mappedPath}:${mapped.line}:${mapped.column}`);
mappedCols[i] = mapped.column;
return mapped.line;
} else {
utils.Logger.log(`SourceMaps.setBP: Mapped ${argsPath} but not line ${line}, column 0`);
mappedCols[i] = 0;
return line;
}
});
this._authoredPathsToMappedBPLines.set(argsPath, mappedLines);
this._authoredPathsToMappedBPCols.set(argsPath, mappedCols);
// Include BPs from other files that map to the same file. Ensure the current file's breakpoints go first
args.lines = mappedLines;
args.cols = mappedCols;
this._sourceMaps.AllMappedSources(mappedPath).forEach(sourcePath => {
if (sourcePath === argsPath) {
return;
}
const sourceBPLines = this._authoredPathsToMappedBPLines.get(sourcePath);
const sourceBPCols = this._authoredPathsToMappedBPCols.get(sourcePath);
if (sourceBPLines && sourceBPCols) {
// Don't modify the cached array
args.lines = args.lines.concat(sourceBPLines);
args.cols = args.cols.concat(sourceBPCols);
}
});
} else if (this._allRuntimeScriptPaths.has(argsPath)) {
// It's a generated file which is loaded
utils.Logger.log(`SourceMaps.setBP: SourceMaps are enabled but ${argsPath} is a runtime script`);
} else {
// Source (or generated) file which is not loaded, need to wait
utils.Logger.log(`SourceMaps.setBP: ${argsPath} can't be resolved to a loaded script.`);
this._pendingBreakpointsByPath.set(argsPath, { resolve, reject, args, requestSeq });
return;
}
this._requestSeqToSetBreakpointsArgs.set(requestSeq, JSON.parse(JSON.stringify(args)));
resolve();
} else {
resolve();
}
});
}
/**
* Apply sourcemapping back to authored files from the response
*/
public setBreakpointsResponse(response: ISetBreakpointsResponseBody, requestSeq: number): void {
if (this._sourceMaps && this._requestSeqToSetBreakpointsArgs.has(requestSeq)) {
const args = this._requestSeqToSetBreakpointsArgs.get(requestSeq);
if (args.authoredPath) {
const sourceBPLines = this._authoredPathsToMappedBPLines.get(args.authoredPath);
if (sourceBPLines) {
// authoredPath is set, so the file was mapped to source.
// Remove breakpoints from files that map to the same file, and map back to source.
response.breakpoints = response.breakpoints.filter((_, i) => i < sourceBPLines.length);
response.breakpoints.forEach((bp, i) => {
const mapped = this._sourceMaps.MapToSource(args.source.path, args.lines[i], args.cols[i]);
if (mapped) {
utils.Logger.log(`SourceMaps.setBP: Mapped ${args.source.path}:${bp.line}:${bp.column} to ${mapped.path}:${mapped.line}`);
bp.line = mapped.line;
} else {
utils.Logger.log(`SourceMaps.setBP: Can't map ${args.source.path}:${bp.line}:${bp.column}, keeping the line number as-is.`);
}
this._requestSeqToSetBreakpointsArgs.delete(requestSeq);
});
}
}
}
// Cleanup column, which is passed in here in case it's needed for sourcemaps, but isn't actually
// part of the DebugProtocol
response.breakpoints.forEach(bp => {
delete bp.column;
});
}
/**
* Apply sourcemapping to the stacktrace response
*/
public stackTraceResponse(response: IStackTraceResponseBody): void {
if (this._sourceMaps) {
response.stackFrames.forEach(stackFrame => {
const mapped = this._sourceMaps.MapToSource(stackFrame.source.path, stackFrame.line, stackFrame.column);
if (mapped && utils.existsSync(mapped.path)) {
// Script was mapped to a valid path
stackFrame.source.path = utils.canonicalizeUrl(mapped.path);
stackFrame.source.sourceReference = 0;
stackFrame.source.name = path.basename(mapped.path);
stackFrame.line = mapped.line;
stackFrame.column = mapped.column;
} else if (utils.existsSync(stackFrame.source.path)) {
// Script could not be mapped, but does exist on disk. Keep it and clear the sourceReference.
stackFrame.source.sourceReference = 0;
} else {
// Script could not be mapped and doesn't exist on disk. Clear the path, use sourceReference.
stackFrame.source.path = undefined;
}
});
} else {
response.stackFrames.forEach(stackFrame => {
// PathTransformer needs to leave the frame in an unfinished state because it doesn't know whether sourcemaps are enabled
if (stackFrame.source.path && stackFrame.source.sourceReference) {
stackFrame.source.path = undefined;
}
});
}
}
public scriptParsed(event: DebugProtocol.Event): void {
if (this._sourceMaps) {
this._allRuntimeScriptPaths.add(event.body.scriptUrl);
let sourceMapUrlValue = event.body.sourceMapURL;
if (!sourceMapUrlValue) {
sourceMapUrlValue = this._sourceMaps.FindSourceMapUrlInFile(event.body.scriptUrl);
}
if (!sourceMapUrlValue || sourceMapUrlValue === "") {
this.resolvePendingBreakpoints(event.body.scriptUrl);
return;
}
this._sourceMaps.ProcessNewSourceMap(event.body.scriptUrl, sourceMapUrlValue).then(() => {
const sources = this._sourceMaps.AllMappedSources(event.body.scriptUrl);
if (sources) {
utils.Logger.log(`SourceMaps.scriptParsed: ${event.body.scriptUrl} was just loaded and has mapped sources: ${JSON.stringify(sources)}`);
sources.forEach(this.resolvePendingBreakpoints, this);
}
});
}
}
// private getSourceMappingFile(filePathOrSourceMapValue: string): string {
// let result = filePathOrSourceMapValue;
// if (!fs.existsSync(filePathOrSourceMapValue)) {
// return result;
// }
// let fileContents = fs.readFileSync(filePathOrSourceMapValue, 'utf8');
// var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)";
// // Matches /* ... */ comments
// var blockCommentRegex = new RegExp("/\\*" + baseRegex + "\\s*\\*/");
// // Matches // .... comments
// var commentRegex = new RegExp("//" + baseRegex + "($|\n|\r\n?)");
// let match = fileContents.match(commentRegex);
// if (!match) {
// match = fileContents.match(blockCommentRegex);
// }
// if (match) {
// result = match[1];
// }
// return result;
// }
private resolvePendingBreakpoints(sourcePath: string): void {
// If there's a setBreakpoints request waiting on this script, go through setBreakpoints again
if (this._pendingBreakpointsByPath.has(sourcePath)) {
utils.Logger.log(`SourceMaps.scriptParsed: Resolving pending breakpoints for ${sourcePath}`);
const pendingBreakpoint = this._pendingBreakpointsByPath.get(sourcePath);
this._pendingBreakpointsByPath.delete(sourcePath);
this.setBreakpoints(pendingBreakpoint.args, pendingBreakpoint.requestSeq)
.then(pendingBreakpoint.resolve, pendingBreakpoint.reject);
}
}
}