-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsourcemapper.ts
245 lines (221 loc) · 8.47 KB
/
sourcemapper.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
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Originally copied from cloud-debug-nodejs's sourcemapper.ts from
// https://github.com/googleapis/cloud-debug-nodejs/blob/7bdc2f1f62a3b45b7b53ea79f9444c8ed50e138b/src/agent/io/sourcemapper.ts
// Modified to map from generated code to source code, rather than from source
// code to generated code.
import * as fs from 'fs';
import * as path from 'path';
import * as sourceMap from 'source-map';
import * as scanner from '../../third_party/cloud-debug-nodejs/src/agent/io/scanner';
const pify = require('pify');
const pLimit = require('p-limit');
const readFile = pify(fs.readFile);
const CONCURRENCY = 10;
const MAP_EXT = '.map';
export interface MapInfoCompiled {
mapFileDir: string;
mapConsumer: sourceMap.RawSourceMap;
}
export interface GeneratedLocation {
file: string;
name?: string;
line: number;
column: number;
}
export interface SourceLocation {
file?: string;
name?: string;
line?: number;
column?: number;
}
/**
* @param {!Map} infoMap The map that maps input source files to
* SourceMapConsumer objects that are used to calculate mapping information
* @param {string} mapPath The path to the source map file to process. The
* path should be relative to the process's current working directory
* @private
*/
async function processSourceMap(
infoMap: Map<string, MapInfoCompiled>,
mapPath: string
): Promise<void> {
// this handles the case when the path is undefined, null, or
// the empty string
if (!mapPath || !mapPath.endsWith(MAP_EXT)) {
throw new Error(`The path "${mapPath}" does not specify a source map file`);
}
mapPath = path.normalize(mapPath);
let contents;
try {
contents = await readFile(mapPath, 'utf8');
} catch (e) {
throw new Error('Could not read source map file ' + mapPath + ': ' + e);
}
let consumer: sourceMap.RawSourceMap;
try {
// TODO: Determine how to reconsile the type conflict where `consumer`
// is constructed as a SourceMapConsumer but is used as a
// RawSourceMap.
// TODO: Resolve the cast of `contents as any` (This is needed because the
// type is expected to be of `RawSourceMap` but the existing
// working code uses a string.)
consumer = (await new sourceMap.SourceMapConsumer(
contents as {} as sourceMap.RawSourceMap
)) as {} as sourceMap.RawSourceMap;
} catch (e) {
throw new Error(
'An error occurred while reading the ' +
'sourceMap file ' +
mapPath +
': ' +
e
);
}
/*
* If the source map file defines a "file" attribute, use it as
* the output file where the path is relative to the directory
* containing the map file. Otherwise, use the name of the output
* file (with the .map extension removed) as the output file.
*/
const dir = path.dirname(mapPath);
const generatedBase = consumer.file
? consumer.file
: path.basename(mapPath, MAP_EXT);
const generatedPath = path.resolve(dir, generatedBase);
infoMap.set(generatedPath, {mapFileDir: dir, mapConsumer: consumer});
}
export class SourceMapper {
infoMap: Map<string, MapInfoCompiled>;
static async create(searchDirs: string[]): Promise<SourceMapper> {
const mapFiles: string[] = [];
for (const dir of searchDirs) {
try {
const mf = await getMapFiles(dir);
mf.forEach(mapFile => {
mapFiles.push(path.resolve(dir, mapFile));
});
} catch (e) {
throw new Error(`failed to get source maps from ${dir}: ${e}`);
}
}
return createFromMapFiles(mapFiles);
}
/**
* @param {Array.<string>} sourceMapPaths An array of paths to .map source map
* files that should be processed. The paths should be relative to the
* current process's current working directory
* @param {Logger} logger A logger that reports errors that occurred while
* processing the given source map files
* @constructor
*/
constructor() {
this.infoMap = new Map();
}
/**
* Used to get the information about the transpiled file from a given input
* source file provided there isn't any ambiguity with associating the input
* path to exactly one output transpiled file.
*
* @param inputPath The (possibly relative) path to the original source file.
* @return The `MapInfoCompiled` object that describes the transpiled file
* associated with the specified input path. `null` is returned if either
* zero files are associated with the input path or if more than one file
* could possibly be associated with the given input path.
*/
private getMappingInfo(inputPath: string): MapInfoCompiled | null {
if (this.infoMap.has(path.normalize(inputPath))) {
return this.infoMap.get(inputPath) as MapInfoCompiled;
}
return null;
}
/**
* Used to determine if the source file specified by the given path has
* a .map file and an output file associated with it.
*
* If there is no such mapping, it could be because the input file is not
* the input to a transpilation process or it is the input to a transpilation
* process but its corresponding .map file was not given to the constructor
* of this mapper.
*
* @param {string} inputPath The path to an input file that could
* possibly be the input to a transpilation process. The path should be
* relative to the process's current working directory.
*/
hasMappingInfo(inputPath: string): boolean {
return this.getMappingInfo(inputPath) !== null;
}
/**
* @param {string} inputPath The path to an input file that could possibly
* be the input to a transpilation process. The path should be relative to
* the process's current working directory
* @param {number} The line number in the input file where the line number is
* zero-based.
* @param {number} (Optional) The column number in the line of the file
* specified where the column number is zero-based.
* @return {Object} The object returned has a "file" attribute for the
* path of the output file associated with the given input file (where the
* path is relative to the process's current working directory),
* a "line" attribute of the line number in the output file associated with
* the given line number for the input file, and an optional "column" number
* of the column number of the output file associated with the given file
* and line information.
*
* If the given input file does not have mapping information associated
* with it then the input location is returned.
*/
mappingInfo(location: GeneratedLocation): SourceLocation {
const inputPath = path.normalize(location.file);
const entry = this.getMappingInfo(inputPath);
if (entry === null) {
return location;
}
const generatedPos = {line: location.line, column: location.column};
// TODO: Determine how to remove the explicit cast here.
const consumer: sourceMap.SourceMapConsumer =
entry.mapConsumer as {} as sourceMap.SourceMapConsumer;
const pos = consumer.originalPositionFor(generatedPos);
if (pos.source === null) {
return location;
}
return {
file: path.resolve(entry.mapFileDir, pos.source),
line: pos.line || undefined,
name: pos.name || location.name,
column: pos.column || undefined,
};
}
}
export async function createFromMapFiles(mapFiles: string[]): Promise<SourceMapper> {
const limit = pLimit(CONCURRENCY);
const mapper = new SourceMapper();
const promises: Array<Promise<void>> = mapFiles.map(mapPath =>
limit(() => processSourceMap(mapper.infoMap, mapPath))
);
try {
await Promise.all(promises);
} catch (err) {
throw new Error(
'An error occurred while processing the source map files' + err
);
}
return mapper;
}
async function getMapFiles(baseDir: string): Promise<string[]> {
const fileStats = await scanner.scan(false, baseDir, /.js.map$/);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
return mapFiles;
}