Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit e38854e

Browse files
fix: index only .js.map source map files (#371)
1 parent c75494c commit e38854e

File tree

8 files changed

+269
-240
lines changed

8 files changed

+269
-240
lines changed

package-lock.json

Lines changed: 221 additions & 228 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/agent/debuglet.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ class IsReadyImpl implements IsReady {
156156
}
157157
}
158158

159+
export interface FindFilesResult {
160+
jsStats: scanner.ScanStats;
161+
mapFiles: string[];
162+
hash?: string;
163+
}
164+
159165
export class Debuglet extends EventEmitter {
160166
private debug: Debug;
161167
private v8debug: DebugApi|null;
@@ -266,6 +272,14 @@ export class Debuglet extends EventEmitter {
266272
return extend(true, {}, defaultConfig, config, envConfig);
267273
}
268274

275+
static async findFiles(shouldHash: boolean, baseDir: string):
276+
Promise<FindFilesResult> {
277+
const fileStats = await scanner.scan(shouldHash, baseDir, /.js$|.js.map$/);
278+
const jsStats = fileStats.selectStats(/.js$/);
279+
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
280+
return {jsStats, mapFiles, hash: fileStats.hash};
281+
}
282+
269283
/**
270284
* Starts the Debuglet. It is important that this is as quick as possible
271285
* as it is on the critical path of application startup.
@@ -289,19 +303,16 @@ export class Debuglet extends EventEmitter {
289303
id = 'GAE-' + process.env.GAE_MINOR_VERSION;
290304
}
291305

292-
let fileStats: scanner.ScanResults;
306+
let findResults: FindFilesResult;
293307
try {
294-
fileStats =
295-
await scanner.scan(!id, that.config.workingDirectory, /.js$|.map$/);
308+
findResults = await Debuglet.findFiles(!id, that.config.workingDirectory);
296309
} catch (err) {
297310
that.logger.error('Error scanning the filesystem.', err);
298311
that.emit('initError', err);
299312
return;
300313
}
301314

302-
const jsStats = fileStats.selectStats(/.js$/);
303-
const mapFiles = fileStats.selectFiles(/.map$/, process.cwd());
304-
SourceMapper.create(mapFiles, async (err3, sourcemapper) => {
315+
SourceMapper.create(findResults.mapFiles, async (err3, sourcemapper) => {
305316
if (err3) {
306317
that.logger.error('Error processing the sourcemaps.', err3);
307318
that.emit('initError', err3);
@@ -311,9 +322,10 @@ export class Debuglet extends EventEmitter {
311322
// At this point err3 being falsy implies sourcemapper is defined
312323
const mapper = sourcemapper as SourceMapper.SourceMapper;
313324

314-
that.v8debug = debugapi.create(that.logger, that.config, jsStats, mapper);
325+
that.v8debug = debugapi.create(
326+
that.logger, that.config, findResults.jsStats, mapper);
315327

316-
id = id || fileStats.hash;
328+
id = id || findResults.hash;
317329

318330
that.logger.info('Unique ID for this Application: ' + id);
319331

test/fixtures/sourcemaps/css-file.css

Whitespace-only changes.

test/fixtures/sourcemaps/css-map-file.css.map

Whitespace-only changes.

test/fixtures/sourcemaps/js-file.js

Whitespace-only changes.

test/fixtures/sourcemaps/js-map-file.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test-debuglet.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ describe('CachedPromise', () => {
9393
});
9494

9595
describe('Debuglet', () => {
96+
describe('findFiles', () => {
97+
const SOURCEMAP_DIR = path.join(__dirname, 'fixtures', 'sourcemaps');
98+
99+
it('throws an error for an invalid directory', async () => {
100+
let err: Error|null = null;
101+
try {
102+
await Debuglet.findFiles(false, path.join(SOURCEMAP_DIR, '!INVALID!'));
103+
} catch (e) {
104+
err = e;
105+
}
106+
assert.ok(err);
107+
});
108+
109+
it('finds the correct sourcemaps files', async () => {
110+
const searchResults = await Debuglet.findFiles(false, SOURCEMAP_DIR);
111+
assert(searchResults.jsStats);
112+
assert.strictEqual(Object.keys(searchResults.jsStats).length, 1);
113+
assert(searchResults.jsStats[path.join(SOURCEMAP_DIR, 'js-file.js')]);
114+
assert.strictEqual(searchResults.mapFiles.length, 1);
115+
assert(searchResults.mapFiles[0].endsWith('js-map-file.js.map'));
116+
});
117+
});
118+
96119
describe('runningOnGCP', () => {
97120
// TODO: Make this more precise.
98121
let savedLookup: Function;

test/test-v8debugapi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ describe('debugapi selection', () => {
123123
};
124124
it('should use the correct debugapi and have appropriate warning', (done) => {
125125
let api: DebugApi;
126-
scanner.scan(true, config.workingDirectory as string, /.js$|.map$/)
126+
scanner.scan(true, config.workingDirectory, /.js$|.js.map$/)
127127
.then((fileStats) => {
128128
const jsStats = fileStats.selectStats(/.js$/);
129-
const mapFiles = fileStats.selectFiles(/.map$/, process.cwd());
129+
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
130130
SourceMapper.create(mapFiles, (err, mapper) => {
131131
assert(!err);
132132
// TODO: Handle the case when mapper is undefined.
@@ -166,10 +166,10 @@ describe('v8debugapi', () => {
166166

167167
beforeEach((done) => {
168168
if (!api) {
169-
scanner.scan(true, config.workingDirectory, /.js$|.map$/)
169+
scanner.scan(true, config.workingDirectory, /.js$|.js.map$/)
170170
.then((fileStats) => {
171171
const jsStats = fileStats.selectStats(/.js$/);
172-
const mapFiles = fileStats.selectFiles(/.map$/, process.cwd());
172+
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
173173
SourceMapper.create(mapFiles, (err1, mapper) => {
174174
assert(!err1);
175175

0 commit comments

Comments
 (0)