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

Commit 8d044a2

Browse files
Format the code (#294)
PR-URL: #294
1 parent 8202cf0 commit 8d044a2

File tree

8 files changed

+263
-190
lines changed

8 files changed

+263
-190
lines changed

src.ts/agent/debuglet.ts

Lines changed: 101 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Common } from '../types/common-types';
17+
import {Common} from '../types/common-types';
1818
const common: Common = require('@google-cloud/common');
1919

2020
import * as crypto from 'crypto';
2121
import {EventEmitter} from 'events';
2222
import * as extend from 'extend';
2323
import * as fs from 'fs';
2424

25-
import { GcpMetadata } from '../types/gcp-metadata-types';
25+
import {GcpMetadata} from '../types/gcp-metadata-types';
2626
const metadata: GcpMetadata = require('gcp-metadata');
2727

2828
import * as _ from 'lodash';
@@ -219,16 +219,20 @@ export class Debuglet extends EventEmitter {
219219
// `null`.
220220
scanner.scan(
221221
!id, that.config_.workingDirectory as string, /.js$|.map$/,
222-
function(err2: Error|null, fileStats?: scanner.ScanResults, hash?: string) {
222+
function(
223+
err2: Error|null, fileStats?: scanner.ScanResults,
224+
hash?: string) {
223225
if (err2) {
224226
that.logger_.error('Error scanning the filesystem.', err2);
225227
that.emit('initError', err2);
226228
return;
227229
}
228230

229231
// TODO: Handle the case where `fileStats` is `undefined`.
230-
const jsStats = (fileStats as scanner.ScanResults).selectStats(/.js$/);
231-
const mapFiles = (fileStats as scanner.ScanResults).selectFiles(/.map$/, process.cwd());
232+
const jsStats =
233+
(fileStats as scanner.ScanResults).selectStats(/.js$/);
234+
const mapFiles = (fileStats as scanner.ScanResults)
235+
.selectFiles(/.map$/, process.cwd());
232236
SourceMapper.create(mapFiles, function(err3, mapper) {
233237
if (err3) {
234238
that.logger_.error(
@@ -239,13 +243,16 @@ export class Debuglet extends EventEmitter {
239243

240244
that.v8debug_ = v8debugapi.create(
241245
// TODO: Handle the case where `mapper` is `undefined`.
242-
that.logger_, that.config_, jsStats, mapper as SourceMapper.SourceMapper);
246+
that.logger_, that.config_, jsStats,
247+
mapper as SourceMapper.SourceMapper);
243248

244249
id = id || hash;
245250

246251
that.logger_.info('Unique ID for this Application: ' + id);
247252

248-
that.getProjectId_(function(err4: Error|null, project: string|undefined, onGCP?: boolean) {
253+
that.getProjectId_(function(
254+
err4: Error|null, project: string|undefined,
255+
onGCP?: boolean) {
249256
if (err4) {
250257
that.logger_.error(
251258
'Unable to discover projectId. Please provide ' +
@@ -277,12 +284,14 @@ export class Debuglet extends EventEmitter {
277284
// TODO: Address the case where `project` is `undefined`.
278285
that.project_ = project as string;
279286
that.debuggee_ = Debuglet.createDebuggee(
280-
// TODO: Address the case when `project` is `undefined`.
287+
// TODO: Address the case when `project` is
288+
// `undefined`.
281289
// TODO: Address the case when `id` is `undefined`.
282290
project as string, id as string,
283291
that.config_.serviceContext,
284292
// TODO: Handle the case where `onGCP` is `undefined`.
285-
sourceContext, that.config_.description, null, onGCP as boolean);
293+
sourceContext, that.config_.description, null,
294+
onGCP as boolean);
286295
that.scheduleRegistration_(0 /* immediately */);
287296
that.emit('started');
288297
});
@@ -297,9 +306,11 @@ export class Debuglet extends EventEmitter {
297306
*/
298307
// TODO: Determine the type of sourceContext
299308
static createDebuggee(
300-
projectId: string, uid: string,
301-
serviceContext:
302-
{service: string|null, version: string|null, minorVersion_: string|null},
309+
projectId: string, uid: string, serviceContext: {
310+
service: string | null,
311+
version: string|null,
312+
minorVersion_: string|null
313+
},
303314
sourceContext: {[key: string]: string}, description: string|null,
304315
errorMessage: string|null, onGCP: boolean): Debuggee {
305316
const cwd = process.cwd();
@@ -379,21 +390,23 @@ export class Debuglet extends EventEmitter {
379390
// TODO: change this to getProjectId in the future.
380391
// TODO: Determine if it is expected that the second argument (which was
381392
// named `response`) is not used.
382-
metadata.project('project-id', function(err: Error, _: http.ServerResponse, metadataProject: string) {
383-
// We should get an error if we are not on GCP.
384-
const onGCP = !err;
385-
386-
// We perfer to use the locally available projectId as that is least
387-
// surprising to users.
388-
const project = that.debug_.options.projectId ||
389-
process.env.GCLOUD_PROJECT || metadataProject;
390-
391-
// We if don't have a projectId by now, we fail with an error.
392-
if (!project) {
393-
return callback(err);
394-
}
395-
return callback(null, project, onGCP);
396-
});
393+
metadata.project(
394+
'project-id',
395+
function(err: Error, _: http.ServerResponse, metadataProject: string) {
396+
// We should get an error if we are not on GCP.
397+
const onGCP = !err;
398+
399+
// We perfer to use the locally available projectId as that is least
400+
// surprising to users.
401+
const project = that.debug_.options.projectId ||
402+
process.env.GCLOUD_PROJECT || metadataProject;
403+
404+
// We if don't have a projectId by now, we fail with an error.
405+
if (!project) {
406+
return callback(err);
407+
}
408+
return callback(null, project, onGCP);
409+
});
397410
}
398411

399412
getSourceContext_(
@@ -437,34 +450,41 @@ export class Debuglet extends EventEmitter {
437450
}
438451

439452
// TODO: Handle the case when `that.debuggee_` is null.
440-
that.debugletApi_.register(that.debuggee_ as Debuggee, function(err: Error|null, result?: { debuggee: Debuggee; }) {
441-
if (err) {
442-
onError(err);
443-
return;
444-
}
453+
that.debugletApi_.register(
454+
that.debuggee_ as Debuggee,
455+
function(err: Error|null, result?: {debuggee: Debuggee;}) {
456+
if (err) {
457+
onError(err);
458+
return;
459+
}
445460

446-
// TODO: It appears that the Debuggee class never has an `isDisabled`
447-
// field set. Determine if this is a bug or if the following
448-
// code is not needed.
449-
// TODO: Handle the case when `result` is undefined.
450-
if ((result as { debuggee: Debuggee}).debuggee.isDisabled) {
451-
// Server has disabled this debuggee / debug agent.
452-
onError(new Error('Disabled by the server'));
453-
that.emit('remotelyDisabled');
454-
return;
455-
}
461+
// TODO: It appears that the Debuggee class never has an
462+
// `isDisabled`
463+
// field set. Determine if this is a bug or if the following
464+
// code is not needed.
465+
// TODO: Handle the case when `result` is undefined.
466+
if ((result as {debuggee: Debuggee}).debuggee.isDisabled) {
467+
// Server has disabled this debuggee / debug agent.
468+
onError(new Error('Disabled by the server'));
469+
that.emit('remotelyDisabled');
470+
return;
471+
}
456472

457-
// TODO: Handle the case when `result` is undefined.
458-
that.logger_.info('Registered as debuggee:', (result as {debuggee: Debuggee}).debuggee.id);
459-
// TODO: Handle the case when `that.debuggee_` is null.
460-
// TODO: Handle the case when `result` is undefined.
461-
(that.debuggee_ as Debuggee).id = (result as {debuggee: Debuggee}).debuggee.id;
462-
// TODO: Handle the case when `result` is undefined.
463-
that.emit('registered', (result as {debuggee: Debuggee}).debuggee.id);
464-
if (!that.fetcherActive_) {
465-
that.scheduleBreakpointFetch_(0);
466-
}
467-
});
473+
// TODO: Handle the case when `result` is undefined.
474+
that.logger_.info(
475+
'Registered as debuggee:',
476+
(result as {debuggee: Debuggee}).debuggee.id);
477+
// TODO: Handle the case when `that.debuggee_` is null.
478+
// TODO: Handle the case when `result` is undefined.
479+
(that.debuggee_ as Debuggee).id =
480+
(result as {debuggee: Debuggee}).debuggee.id;
481+
// TODO: Handle the case when `result` is undefined.
482+
that.emit(
483+
'registered', (result as {debuggee: Debuggee}).debuggee.id);
484+
if (!that.fetcherActive_) {
485+
that.scheduleBreakpointFetch_(0);
486+
}
487+
});
468488
}, seconds * 1000).unref();
469489
}
470490

@@ -510,25 +530,28 @@ export class Debuglet extends EventEmitter {
510530

511531
default:
512532
// TODO: Address the case where `response` is `undefined`.
513-
that.logger_.info('\t' + (response as http.ServerResponse).statusCode + ' completed.');
533+
that.logger_.info(
534+
'\t' + (response as http.ServerResponse).statusCode +
535+
' completed.');
514536
if (body.wait_expired) {
515537
that.logger_.info('\tLong poll completed.');
516538
that.scheduleBreakpointFetch_(0 /*immediately*/);
517539
return;
518540
}
519-
const bps = (body.breakpoints || []).filter(function(bp: Breakpoint) {
520-
const action = bp.action || 'CAPTURE';
521-
if (action !== 'CAPTURE' && action !== 'LOG') {
522-
that.logger_.warn(
523-
'Found breakpoint with invalid action:', action);
524-
bp.status = new StatusMessage(
525-
StatusMessage.UNSPECIFIED, BREAKPOINT_ACTION_MESSAGE,
526-
true);
527-
that.rejectBreakpoint_(bp);
528-
return false;
529-
}
530-
return true;
531-
});
541+
const bps =
542+
(body.breakpoints || []).filter(function(bp: Breakpoint) {
543+
const action = bp.action || 'CAPTURE';
544+
if (action !== 'CAPTURE' && action !== 'LOG') {
545+
that.logger_.warn(
546+
'Found breakpoint with invalid action:', action);
547+
bp.status = new StatusMessage(
548+
StatusMessage.UNSPECIFIED,
549+
BREAKPOINT_ACTION_MESSAGE, true);
550+
that.rejectBreakpoint_(bp);
551+
return false;
552+
}
553+
return true;
554+
});
532555
that.updateActiveBreakpoints_(bps);
533556
if (Object.keys(that.activeBreakpointMap_).length) {
534557
that.logger_.info(formatBreakpoints(
@@ -596,7 +619,7 @@ export class Debuglet extends EventEmitter {
596619
*/
597620
convertBreakpointListToMap_(breakpointList: Breakpoint[]):
598621
{[key: string]: Breakpoint} {
599-
const map: { [id: string]: Breakpoint } = {};
622+
const map: {[id: string]: Breakpoint} = {};
600623
breakpointList.forEach(function(breakpoint) {
601624
// TODO: Address the case when `breakpoint.id` is `undefined`.
602625
map[breakpoint.id as string] = breakpoint;
@@ -660,15 +683,16 @@ export class Debuglet extends EventEmitter {
660683

661684
if (breakpoint.action === 'LOG') {
662685
// TODO: Address the case when `that.v8debug_` is `null`.
663-
(that.v8debug_ as V8DebugApi).log(
664-
breakpoint,
665-
function(fmt: string, exprs: string[]) {
666-
console.log('LOGPOINT:', Debuglet.format(fmt, exprs));
667-
},
668-
function() {
669-
// TODO: Address the case when `breakpoint.id` is `undefined`.
670-
return that.completedBreakpointMap_[breakpoint.id as string];
671-
});
686+
(that.v8debug_ as V8DebugApi)
687+
.log(
688+
breakpoint,
689+
function(fmt: string, exprs: string[]) {
690+
console.log('LOGPOINT:', Debuglet.format(fmt, exprs));
691+
},
692+
function() {
693+
// TODO: Address the case when `breakpoint.id` is `undefined`.
694+
return that.completedBreakpointMap_[breakpoint.id as string];
695+
});
672696
} else {
673697
// TODO: Address the case when `that.v8debug_` is `null`.
674698
(that.v8debug_ as V8DebugApi).wait(breakpoint, function(err2) {

src.ts/agent/scanner.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ScanResultsImpl implements ScanResults {
7676
* to determine if the scan results for that filename
7777
* should be included in the returned results.
7878
*/
79-
selectStats(regex: RegExp): ScanStats | {} {
79+
selectStats(regex: RegExp): ScanStats|{} {
8080
return _.pickBy(this.stats_, function(_, key) {
8181
return regex.test(key);
8282
});
@@ -148,31 +148,34 @@ function computeStats(
148148
const hashes: Array<string|undefined> = [];
149149
const statistics: ScanStats = {};
150150
fileList.forEach(function(filename) {
151-
stats(filename, shouldHash, function(err: Error, fileStats: FileStats|undefined) {
152-
if (err) {
153-
callback(err);
154-
return;
155-
}
151+
stats(
152+
filename, shouldHash,
153+
function(err: Error, fileStats: FileStats|undefined) {
154+
if (err) {
155+
callback(err);
156+
return;
157+
}
156158

157-
pending--;
158-
if (shouldHash) {
159-
// TODO: Address the case when `fileStats` is `undefined`
160-
hashes.push((fileStats as FileStats).hash);
161-
}
162-
statistics[filename] = fileStats;
159+
pending--;
160+
if (shouldHash) {
161+
// TODO: Address the case when `fileStats` is `undefined`
162+
hashes.push((fileStats as FileStats).hash);
163+
}
164+
statistics[filename] = fileStats;
163165

164-
if (pending === 0) {
165-
let hash;
166-
if (shouldHash) {
167-
// Sort the hashes to get a deterministic order as the files may not
168-
// be in the same order each time we scan the disk.
169-
const buffer = hashes.sort().join();
170-
const sha1 = crypto.createHash('sha1').update(buffer).digest('hex');
171-
hash = 'SHA1-' + sha1;
172-
}
173-
callback(null, new ScanResultsImpl(statistics), hash);
174-
}
175-
});
166+
if (pending === 0) {
167+
let hash;
168+
if (shouldHash) {
169+
// Sort the hashes to get a deterministic order as the files may
170+
// not be in the same order each time we scan the disk.
171+
const buffer = hashes.sort().join();
172+
const sha1 =
173+
crypto.createHash('sha1').update(buffer).digest('hex');
174+
hash = 'SHA1-' + sha1;
175+
}
176+
callback(null, new ScanResultsImpl(statistics), hash);
177+
}
178+
});
176179
});
177180
}
178181

@@ -256,7 +259,7 @@ function stats(
256259
});
257260
byLine.on('end', function() {
258261
// TODO: Address the case where `d` is `undefined`.
259-
let d: string | undefined;
262+
let d: string|undefined;
260263
if (shouldHash) {
261264
d = shasum.digest('hex');
262265
}

src.ts/agent/sourcemapper.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ function processSourcemap(
8686
// TODO: Determine how to reconsile the type conflict where `consumer`
8787
// is constructed as a SourceMapConsumer but is used as a
8888
// RawSourceMap.
89-
consumer = new sourceMap.SourceMapConsumer(data) as any as sourceMap.RawSourceMap;
89+
consumer = new sourceMap.SourceMapConsumer(data) as any as
90+
sourceMap.RawSourceMap;
9091
} catch (e) {
9192
return callback(new Error(
9293
'An error occurred while reading the ' +
@@ -200,7 +201,8 @@ export class SourceMapper {
200201
};
201202

202203
// TODO: Determine how to remove the explicit cast here.
203-
const consumer: sourceMap.SourceMapConsumer = entry.mapConsumer as any as sourceMap.SourceMapConsumer;
204+
const consumer: sourceMap.SourceMapConsumer =
205+
entry.mapConsumer as any as sourceMap.SourceMapConsumer;
204206
const allPos = consumer.allGeneratedPositionsFor(sourcePos);
205207
/*
206208
* Based on testing, it appears that the following code is needed to
@@ -212,7 +214,9 @@ export class SourceMapper {
212214
const mappedPos: sourceMap.Position = allPos && allPos.length > 0 ?
213215
Array.prototype.reduce.call(
214216
allPos,
215-
function(accumulator: sourceMap.Position, value: sourceMap.Position /*, index, arr*/) {
217+
function(
218+
accumulator: sourceMap.Position,
219+
value: sourceMap.Position /*, index, arr*/) {
216220
return value.line < accumulator.line ? value : accumulator;
217221
}) :
218222
consumer.generatedPositionFor(sourcePos);
@@ -225,9 +229,10 @@ export class SourceMapper {
225229
// TODO: The `sourceMap.Position` type definition has a `column`
226230
// attribute and not a `col` attribute. Determine if the type
227231
// definition or this code is correct.
228-
column: (mappedPos as any).col // SourceMapConsumer uses zero-based column
229-
// numbers which is the same as the
230-
// expected output
232+
column:
233+
(mappedPos as any).col // SourceMapConsumer uses zero-based column
234+
// numbers which is the same as the
235+
// expected output
231236
};
232237
}
233238
}

0 commit comments

Comments
 (0)