forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceMaps.ts
More file actions
562 lines (473 loc) · 16.1 KB
/
Copy pathsourceMaps.ts
File metadata and controls
562 lines (473 loc) · 16.1 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as Path from 'path';
import * as FS from 'fs';
import {SourceMapConsumer} from 'source-map';
import * as PathUtils from './pathUtilities';
import {NodeDebugSession} from './nodeDebug';
const util = require('../../node_modules/source-map/lib/util.js');
const pathNormalize = (process.platform === 'win32' || process.platform === 'darwin') ? path => path.toLowerCase() : path => path;
export interface MappingResult {
path: string; // absolute path
content?: string; // optional content of source (source inlined in source map)
line: number;
column: number;
}
export enum Bias {
GREATEST_LOWER_BOUND = 1,
LEAST_UPPER_BOUND = 2
}
export interface ISourceMaps {
/*
* Map source language path to generated path.
* Returns null if not found.
*/
MapPathFromSource(path: string): string;
/*
* Map generated path to source path.
* Returns null if not found.
*/
MapPathToSource(path: string, content: string): string[];
/*
* Map location in source language to location in generated code.
* line and column are 0 based.
*/
MapFromSource(path: string, line: number, column: number, bias?: Bias): MappingResult;
/*
* Map location in generated code to location in source language.
* line and column are 0 based.
*/
MapToSource(path: string, content: string, line: number, column: number, bias?: Bias): MappingResult;
/*
* Returns true if content contains a reference to a source map (or a data url with an inlined source map).
*/
HasSourceMap(content: string) : boolean;
}
export class SourceMaps implements ISourceMaps {
private static SOURCE_MAPPING_MATCHER = new RegExp('//[#@] ?sourceMappingURL=(.+)$');
private _session: NodeDebugSession;
private _allSourceMaps: { [id: string] : SourceMap; } = {}; // map file path -> SourceMap
private _generatedToSourceMaps: { [id: string] : SourceMap; } = {}; // generated file -> SourceMap
private _sourceToGeneratedMaps: { [id: string] : SourceMap; } = {}; // source file -> SourceMap
private _generatedCodeDirectory: string;
public constructor(session: NodeDebugSession, generatedCodeDirectory: string) {
this._session = session;
this._generatedCodeDirectory = generatedCodeDirectory;
}
public MapPathFromSource(pathToSource: string): string {
const map = this._findSourceToGeneratedMapping(pathToSource);
if (map) {
return map.generatedPath();
}
return null;
}
public MapPathToSource(pathToGenerated: string, content: string) : string[] {
const map = this._findGeneratedToSourceMapping(pathToGenerated, content);
if (map) {
return map.sources();
}
return null;
}
public MapFromSource(pathToSource: string, line: number, column: number, bias?: Bias): MappingResult {
const map = this._findSourceToGeneratedMapping(pathToSource);
if (map) {
line += 1; // source map impl is 1 based
const mr = map.generatedPositionFor(pathToSource, line, column, bias);
if (mr && typeof mr.line === 'number') {
return {
path: map.generatedPath(),
line: mr.line-1,
column: mr.column
};
}
}
return null;
}
public MapToSource(pathToGenerated: string, content: string, line: number, column: number, bias?: Bias): MappingResult {
const map = this._findGeneratedToSourceMapping(pathToGenerated, content);
if (map) {
line += 1; // source map impl is 1 based
const mr = map.originalPositionFor(line, column, bias);
if (mr && mr.source) {
return {
path: mr.source,
content: (<any>mr).content,
line: mr.line-1,
column: mr.column
};
}
}
return null;
}
public HasSourceMap(content: string) : boolean {
return this._findSourceMapUrlInFile(null, content) !== null;
}
//---- private -----------------------------------------------------------------------
/**
* Tries to find a SourceMap for the given source.
* This is difficult because the source does not contain any information about where
* the generated code or the source map is located.
* Our strategy is as follows:
* - search in all known source maps whether if refers to this source in the sources array.
* - ...
*/
private _findSourceToGeneratedMapping(pathToSource: string): SourceMap {
if (!pathToSource) {
return null;
}
const pathToSourceKey = pathNormalize(pathToSource);
// try to find in existing
if (pathToSourceKey in this._sourceToGeneratedMaps) {
return this._sourceToGeneratedMaps[pathToSourceKey];
}
// a reverse lookup: in all source maps try to find pathToSource in the sources array
for (let key in this._generatedToSourceMaps) {
const m = this._generatedToSourceMaps[key];
if (m.doesOriginateFrom(pathToSource)) {
this._sourceToGeneratedMaps[pathToSourceKey] = m;
return m;
}
}
// search for all map files in generatedCodeDirectory
if (this._generatedCodeDirectory) {
try {
let maps = FS.readdirSync(this._generatedCodeDirectory).filter(e => Path.extname(e.toLowerCase()) === '.map');
for (let map_name of maps) {
const map_path = Path.join(this._generatedCodeDirectory, map_name);
const m = this._loadSourceMap(map_path);
if (m && m.doesOriginateFrom(pathToSource)) {
this._log(`_findSourceToGeneratedMapping: found source map for source ${pathToSource} in outDir`);
this._sourceToGeneratedMaps[pathToSourceKey] = m;
return m;
}
}
}
catch (e) {
// ignore
}
}
// no map found
let pathToGenerated = pathToSource;
const ext = Path.extname(pathToSource);
if (ext !== '.js') {
// use heuristic: change extension to ".js" and find a map for it
const pos = pathToSource.lastIndexOf('.');
if (pos >= 0) {
pathToGenerated = pathToSource.substr(0, pos) + '.js';
}
}
let map = null;
// first look into the generated code directory
if (this._generatedCodeDirectory) {
let rest = PathUtils.makeRelative(this._generatedCodeDirectory, pathToGenerated);
while (rest) {
const path = Path.join(this._generatedCodeDirectory, rest);
map = this._findGeneratedToSourceMapping(path);
if (map) {
break;
}
rest = PathUtils.removeFirstSegment(rest);
}
}
// VSCode extension host support:
// we know that the plugin has an "out" directory next to the "src" directory
if (map === null) {
let srcSegment = Path.sep + 'src' + Path.sep;
if (pathToGenerated.indexOf(srcSegment) >= 0) {
const outSegment = Path.sep + 'out' + Path.sep;
map = this._findGeneratedToSourceMapping(pathToGenerated.replace(srcSegment, outSegment));
}
}
// if not found look in the same directory as the source
if (map === null && pathNormalize(pathToGenerated) !== pathToSourceKey) {
map = this._findGeneratedToSourceMapping(pathToGenerated);
}
if (map) {
this._sourceToGeneratedMaps[pathToSourceKey] = map;
return map;
}
// nothing found
return null;
}
/**
* Tries to find a SourceMap for the given path to a generated file.
* This is simple if the generated file has the 'sourceMappingURL' at the end.
* If not, we are using some heuristics...
*/
private _findGeneratedToSourceMapping(pathToGenerated: string, content?: string): SourceMap {
if (!pathToGenerated) {
return null;
}
const pathToGeneratedKey = pathNormalize(pathToGenerated);
if (pathToGeneratedKey in this._generatedToSourceMaps) {
return this._generatedToSourceMaps[pathToGeneratedKey];
}
// try to find a source map URL in the generated file
let map_path: string = null;
const uri = this._findSourceMapUrlInFile(pathToGenerated, content);
if (uri) {
// if uri is data url source map is inlined in generated file
if (uri.indexOf('data:application/json') >= 0) {
const pos = uri.lastIndexOf(',');
if (pos > 0) {
const data = uri.substr(pos+1);
try {
const buffer = new Buffer(data, 'base64');
const json = buffer.toString();
if (json) {
this._log(`_findGeneratedToSourceMapping: successfully read inlined source map in '${pathToGenerated}'`);
return this._registerSourceMap(new SourceMap(pathToGenerated, pathToGenerated, json));
}
}
catch (e) {
this._log(`_findGeneratedToSourceMapping: exception while processing data url '${e}'`);
}
}
} else {
map_path = uri;
}
}
// if path is relative make it absolute
if (map_path && !Path.isAbsolute(map_path)) {
map_path = PathUtils.makePathAbsolute(pathToGenerated, map_path);
}
if (!map_path || !FS.existsSync(map_path)) {
// try to find map file next to the generated source
map_path = pathToGenerated + '.map';
}
if (map_path && FS.existsSync(map_path)) {
const map = this._loadSourceMap(map_path, pathToGenerated);
if (map) {
return map;
}
}
return null;
}
/**
* Try to find the 'sourceMappingURL' in the file with the given path.
* Returns null if no source map url is found or if an error occured.
*/
private _findSourceMapUrlInFile(pathToGenerated: string, content: string): string {
try {
const contents = content || FS.readFileSync(pathToGenerated).toString();
const lines = contents.split('\n');
for (let line of lines) {
const matches = SourceMaps.SOURCE_MAPPING_MATCHER.exec(line);
if (matches && matches.length === 2) {
const uri = matches[1].trim();
if (pathToGenerated) {
this._log(`_findSourceMapUrlInFile: source map url found at end of generated file '${pathToGenerated}'`);
} else {
this._log(`_findSourceMapUrlInFile: source map url found at end of generated content`);
}
return uri;
}
}
} catch (e) {
// ignore exception
}
return null;
}
/**
* Loads source map from file system.
* If no generatedPath is given, the 'file' attribute of the source map is used.
*/
private _loadSourceMap(map_path: string, generatedPath?: string): SourceMap {
const mapPathKey = pathNormalize(map_path);
if (mapPathKey in this._allSourceMaps) {
return this._allSourceMaps[mapPathKey];
}
try {
const mp = Path.join(map_path);
const contents = FS.readFileSync(mp).toString();
const map = new SourceMap(mp, generatedPath, contents);
this._allSourceMaps[mapPathKey] = map;
this._registerSourceMap(map);
this._log(`_loadSourceMap: successfully loaded source map '${map_path}'`);
return map;
}
catch (e) {
this._log(`_loadSourceMap: loading source map '${map_path}' failed with exception: ${e}`);
}
return null;
}
private _registerSourceMap(map: SourceMap): SourceMap {
const gp = map.generatedPath();
if (gp) {
this._generatedToSourceMaps[pathNormalize(gp)] = map;
}
return map;
}
private _log(message: string): void {
this._session.log('sm', message);
}
}
class SourceMap {
private _sourcemapLocation: string; // the directory where this sourcemap lives
private _generatedFile: string; // the generated file to which this source map belongs to
private _sources: string[]; // the sources of the generated file (relative to sourceRoot)
private _sourceRoot: string; // the common prefix for the source (can be a URL)
private _smc: SourceMapConsumer; // the source map
public constructor(mapPath: string, generatedPath: string, json: string) {
this._sourcemapLocation = this.fixPath(Path.dirname(mapPath));
const sm = JSON.parse(json);
if (!generatedPath) {
let file = sm.file;
if (!PathUtils.isAbsolutePath(file)) {
generatedPath = PathUtils.makePathAbsolute(mapPath, file);
}
}
this._generatedFile = generatedPath;
// fix all paths for use with the source-map npm module.
sm.sourceRoot = this.fixPath(sm.sourceRoot, '');
for (let i = 0; i < sm.sources.length; i++) {
sm.sources[i] = this.fixPath(sm.sources[i]);
}
this._sourceRoot = sm.sourceRoot;
// use source-map utilities to normalize sources entries
this._sources = sm.sources
.map(util.normalize)
.map((source) => {
return this._sourceRoot && util.isAbsolute(this._sourceRoot) && util.isAbsolute(source)
? util.relative(this._sourceRoot, source)
: source;
});
try {
this._smc = new SourceMapConsumer(sm);
} catch (e) {
// ignore exception and leave _smc undefined
}
}
/**
* fix a path for use with the source-map npm module because:
* - source map sources are URLs, so even on Windows they should be using forward slashes.
* - the source-map library expects forward slashes and their relative path logic
* (specifically the "normalize" function) gives incorrect results when passing in backslashes.
* - paths starting with drive letters are not recognized as absolute by the source-map library.
*/
private fixPath(path: string, dflt?: string) : string {
if (path) {
path = path.replace(/\\/g, '/');
// if path starts with a drive letter convert path to a file url so that the source-map library can handle it
if (/^[a-zA-Z]\:\//.test(path)) {
// Windows drive letter must be prefixed with a slash
path = encodeURI('file:///' + path);
}
return path;
}
return dflt;
}
/**
* undo the fix
*/
private unfixPath(path: string) : string {
const prefix = 'file://';
if (path.indexOf(prefix) === 0) {
path = path.substr(prefix.length);
path = decodeURI(path);
if (/^\/[a-zA-Z]\:\//.test(path)) {
path = path.substr(1); // remove additional '/'
}
}
return path;
}
/*
* The generated file this source map belongs to.
*/
public generatedPath(): string {
return this._generatedFile;
}
public sources() : string[] {
return this._sources;
}
/*
* Returns true if this source map originates from the given source.
*/
public doesOriginateFrom(absPath: string): boolean {
return this.findSource(absPath) !== null;
}
/**
* returns the first entry from the sources array that matches the given absPath
* or null otherwise.
*/
private findSource(absPath: string): string {
// on Windows change back slashes to forward slashes because the source-map library requires this
if (process.platform === 'win32') {
absPath = absPath.replace(/\\/g, '/');
}
absPath = pathNormalize(absPath);
for (let name of this._sources) {
if (!util.isAbsolute(name)) {
name = util.join(this._sourceRoot, name);
}
let path = this.absolutePath(name);
path = pathNormalize(path);
if (absPath === path) {
return name;
}
}
return null;
}
/**
* Tries to make the given path absolute by prefixing it with the source maps location.
* Any url schemes are removed.
*/
private absolutePath(path: string): string {
if (!util.isAbsolute(path)) {
path = util.join(this._sourcemapLocation, path);
}
return this.unfixPath(path);
}
/*
* Finds the nearest source location for the given location in the generated file.
* Returns null if sourcemap is invalid.
*/
public originalPositionFor(line: number, column: number, bias: Bias): SourceMap.MappedPosition {
if (!this._smc) {
return null;
}
const needle = {
line: line,
column: column,
bias: bias || Bias.LEAST_UPPER_BOUND
};
const mp = this._smc.originalPositionFor(needle);
if (mp.source) {
// if source map has inlined source, return it
const src = this._smc.sourceContentFor(mp.source);
if (src) {
(<any>mp).content = src;
}
// map result back to absolute path
mp.source = this.absolutePath(mp.source);
// on Windows change forward slashes back to back slashes
if (process.platform === 'win32') {
mp.source = mp.source.replace(/\//g, '\\');
}
}
return mp;
}
/*
* Finds the nearest location in the generated file for the given source location.
* Returns null if sourcemap is invalid.
*/
public generatedPositionFor(absPath: string, line: number, column: number, bias: Bias): SourceMap.Position {
if (!this._smc) {
return null;
}
// make sure that we use an entry from the "sources" array that matches the passed absolute path
const source = this.findSource(absPath);
if (source) {
const needle = {
source: source,
line: line,
column: column,
bias: bias || Bias.LEAST_UPPER_BOUND
};
return this._smc.generatedPositionFor(needle);
}
return null;
}
}