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

Commit 842690f

Browse files
committed
Add project detection and unit testing
1 parent 22e255a commit 842690f

File tree

3 files changed

+425
-80
lines changed

3 files changed

+425
-80
lines changed

src/agent/debuglet.ts

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class Debuglet extends EventEmitter {
189189
private v8debug: DebugApi | null;
190190
private running: boolean;
191191
private project: string | null;
192-
private controller: Controller;
192+
private controller: Controller | null;
193193
private completedBreakpointMap: {[key: string]: boolean};
194194

195195
// breakpointFetchedTimestamp represents the last timestamp when
@@ -255,17 +255,7 @@ export class Debuglet extends EventEmitter {
255255
level: Debuglet.logLevelToName(this.config.logLevel),
256256
});
257257

258-
/** @private {DebugletApi} */
259-
if (config.useFirebase) {
260-
// TODO: This shouldn't be on the critical path.
261-
const firebaseDb = FirebaseController.initialize({
262-
keyPath: config.firebaseKeyPath,
263-
databaseUrl: config.firebaseDbUrl,
264-
});
265-
this.controller = new FirebaseController(firebaseDb);
266-
} else {
267-
this.controller = new OnePlatformController(this.debug, this.config);
268-
}
258+
this.controller = null;
269259

270260
/** @private {Debuggee} */
271261
this.debuggee = null;
@@ -434,8 +424,19 @@ export class Debuglet extends EventEmitter {
434424

435425
let project: string;
436426
if (this.config.useFirebase) {
437-
// Project id was discovered during initialization of the app.
438-
project = (this.controller as FirebaseController).getProjectId();
427+
try {
428+
const firebaseDb = await FirebaseController.initialize({
429+
keyPath: this.config.firebaseKeyPath,
430+
databaseUrl: this.config.firebaseDbUrl,
431+
projectId: this.config.projectId,
432+
});
433+
this.controller = new FirebaseController(firebaseDb);
434+
project = (this.controller as FirebaseController).getProjectId();
435+
} catch (err) {
436+
this.logger.error('Unable to connect to Firebase: ' + err.message);
437+
this.emit('initError', err);
438+
return;
439+
}
439440
} else {
440441
try {
441442
project = await this.debug.authClient.getProjectId();
@@ -446,6 +447,7 @@ export class Debuglet extends EventEmitter {
446447
this.emit('initError', err);
447448
return;
448449
}
450+
this.controller = new OnePlatformController(this.debug, this.config);
449451
}
450452

451453
if (
@@ -723,6 +725,7 @@ export class Debuglet extends EventEmitter {
723725
}
724726

725727
setTimeout(() => {
728+
assert(that.controller);
726729
if (!that.running) {
727730
onError(new Error('Debuglet not running'));
728731
return;
@@ -773,8 +776,7 @@ export class Debuglet extends EventEmitter {
773776
}
774777

775778
startListeningForBreakpoints_(): void {
776-
// eslint-disable-next-line @typescript-eslint/no-this-alias
777-
const that = this;
779+
assert(this.controller);
778780
// TODO: Handle the case where this.debuggee is null or not properly registered.
779781
this.controller.subscribeToBreakpoints(
780782
this.debuggee!,
@@ -785,11 +787,11 @@ export class Debuglet extends EventEmitter {
785787
const delay =
786788
err.name === 'RegistrationExpiredError'
787789
? 0
788-
: that.config.internal.registerDelayOnFetcherErrorSec;
789-
that.scheduleRegistration_(delay);
790+
: this.config.internal.registerDelayOnFetcherErrorSec;
791+
this.scheduleRegistration_(delay);
790792
}
791793

792-
that.updateActiveBreakpoints_(breakpoints);
794+
this.updateActiveBreakpoints_(breakpoints);
793795
}
794796
);
795797
}
@@ -814,30 +816,28 @@ export class Debuglet extends EventEmitter {
814816
* @private
815817
*/
816818
updateActiveBreakpoints_(breakpoints: stackdriver.Breakpoint[]): void {
817-
// eslint-disable-next-line @typescript-eslint/no-this-alias
818-
const that = this;
819819
const updatedBreakpointMap = this.convertBreakpointListToMap_(breakpoints);
820820

821821
if (breakpoints.length) {
822-
that.logger.info(
822+
this.logger.info(
823823
formatBreakpoints('Server breakpoints: ', updatedBreakpointMap)
824824
);
825825
}
826826
breakpoints.forEach((breakpoint: stackdriver.Breakpoint) => {
827827
// TODO: Address the case when `breakpoint.id` is `undefined`.
828828
if (
829-
!that.completedBreakpointMap[breakpoint.id as string] &&
830-
!that.activeBreakpointMap[breakpoint.id as string]
829+
!this.completedBreakpointMap[breakpoint.id as string] &&
830+
!this.activeBreakpointMap[breakpoint.id as string]
831831
) {
832832
// New breakpoint
833-
that.addBreakpoint_(breakpoint, err => {
833+
this.addBreakpoint_(breakpoint, err => {
834834
if (err) {
835-
that.completeBreakpoint_(breakpoint, false);
835+
this.completeBreakpoint_(breakpoint, false);
836836
}
837837
});
838838

839839
// Schedule the expiry of server breakpoints.
840-
that.scheduleBreakpointExpiry_(breakpoint);
840+
this.scheduleBreakpointExpiry_(breakpoint);
841841
}
842842
});
843843

@@ -850,7 +850,7 @@ export class Debuglet extends EventEmitter {
850850
// field. It is possible that breakpoint.id is always
851851
// undefined!
852852
// TODO: Make sure the use of `that` here is correct.
853-
delete that.completedBreakpointMap[(breakpoint as {} as {id: number}).id];
853+
delete this.completedBreakpointMap[(breakpoint as {} as {id: number}).id];
854854
});
855855

856856
// Remove active breakpoints that the server no longer care about.
@@ -906,14 +906,11 @@ export class Debuglet extends EventEmitter {
906906
breakpoint: stackdriver.Breakpoint,
907907
cb: (ob: Error | string) => void
908908
): void {
909-
// eslint-disable-next-line @typescript-eslint/no-this-alias
910-
const that = this;
911-
912909
if (
913-
!that.config.allowExpressions &&
910+
!this.config.allowExpressions &&
914911
(breakpoint.condition || breakpoint.expressions)
915912
) {
916-
that.logger.error(ALLOW_EXPRESSIONS_MESSAGE);
913+
this.logger.error(ALLOW_EXPRESSIONS_MESSAGE);
917914
breakpoint.status = new StatusMessage(
918915
StatusMessage.UNSPECIFIED,
919916
ALLOW_EXPRESSIONS_MESSAGE,
@@ -927,7 +924,7 @@ export class Debuglet extends EventEmitter {
927924

928925
if (utils.satisfies(process.version, '5.2 || <4')) {
929926
const message = NODE_VERSION_MESSAGE;
930-
that.logger.error(message);
927+
this.logger.error(message);
931928
breakpoint.status = new StatusMessage(
932929
StatusMessage.UNSPECIFIED,
933930
message,
@@ -939,41 +936,41 @@ export class Debuglet extends EventEmitter {
939936
return;
940937
}
941938

942-
// TODO: Address the case when `that.v8debug` is `null`.
943-
(that.v8debug as DebugApi).set(breakpoint, err1 => {
939+
// TODO: Address the case when `this.v8debug` is `null`.
940+
(this.v8debug as DebugApi).set(breakpoint, err1 => {
944941
if (err1) {
945942
cb(err1);
946943
return;
947944
}
948-
that.logger.info('\tsuccessfully added breakpoint ' + breakpoint.id);
945+
this.logger.info('\tsuccessfully added breakpoint ' + breakpoint.id);
949946
// TODO: Address the case when `breakpoint.id` is `undefined`.
950-
that.activeBreakpointMap[breakpoint.id as string] = breakpoint;
947+
this.activeBreakpointMap[breakpoint.id as string] = breakpoint;
951948

952949
if (breakpoint.action === 'LOG') {
953-
// TODO: Address the case when `that.v8debug` is `null`.
954-
(that.v8debug as DebugApi).log(
950+
// TODO: Address the case when `this.v8debug` is `null`.
951+
(this.v8debug as DebugApi).log(
955952
breakpoint,
956953
(fmt: string, exprs: string[]) => {
957-
that.config.log.logFunction(
954+
this.config.log.logFunction(
958955
`LOGPOINT: ${Debuglet.format(fmt, exprs)}`
959956
);
960957
},
961958
() => {
962959
// TODO: Address the case when `breakpoint.id` is `undefined`.
963-
return that.completedBreakpointMap[breakpoint.id as string];
960+
return this.completedBreakpointMap[breakpoint.id as string];
964961
}
965962
);
966963
} else {
967-
// TODO: Address the case when `that.v8debug` is `null`.
968-
(that.v8debug as DebugApi).wait(breakpoint, err2 => {
964+
// TODO: Address the case when `this.v8debug` is `null`.
965+
(this.v8debug as DebugApi).wait(breakpoint, err2 => {
969966
if (err2) {
970-
that.logger.error(err2);
967+
this.logger.error(err2);
971968
cb(err2);
972969
return;
973970
}
974971

975-
that.logger.info('Breakpoint hit!: ' + breakpoint.id);
976-
that.completeBreakpoint_(breakpoint);
972+
this.logger.info('Breakpoint hit!: ' + breakpoint.id);
973+
this.completeBreakpoint_(breakpoint);
977974
});
978975
}
979976
});
@@ -989,21 +986,24 @@ export class Debuglet extends EventEmitter {
989986
breakpoint: stackdriver.Breakpoint,
990987
deleteFromV8 = true
991988
): void {
992-
// eslint-disable-next-line @typescript-eslint/no-this-alias
993-
const that = this;
989+
assert(this.controller);
994990

995-
that.logger.info('\tupdating breakpoint data on server', breakpoint.id);
996-
that.controller.updateBreakpoint(
997-
// TODO: Address the case when `that.debuggee` is `null`.
998-
that.debuggee as Debuggee,
991+
this.logger.info('\tupdating breakpoint data on server', breakpoint.id);
992+
this.controller.updateBreakpoint(
993+
// TODO: Address the case when `this.debuggee` is `null`.
994+
this.debuggee as Debuggee,
999995
breakpoint,
1000996
(err /*, body*/) => {
1001997
if (err) {
1002-
that.logger.error('Unable to complete breakpoint on server', err);
1003-
} else {
998+
this.logger.error('Unable to complete breakpoint on server', err);
999+
return;
1000+
}
1001+
// The Firebase controller will remove the breakpoint during the update
1002+
// by removing it from the database.
1003+
if (!this.config.useFirebase) {
10041004
// TODO: Address the case when `breakpoint.id` is `undefined`.
1005-
that.completedBreakpointMap[breakpoint.id as string] = true;
1006-
that.removeBreakpoint_(breakpoint, deleteFromV8);
1005+
this.completedBreakpointMap[breakpoint.id as string] = true;
1006+
this.removeBreakpoint_(breakpoint, deleteFromV8);
10071007
}
10081008
}
10091009
);
@@ -1015,16 +1015,15 @@ export class Debuglet extends EventEmitter {
10151015
* @private
10161016
*/
10171017
rejectBreakpoint_(breakpoint: stackdriver.Breakpoint): void {
1018-
// eslint-disable-next-line @typescript-eslint/no-this-alias
1019-
const that = this;
1018+
assert(this.controller);
10201019

1021-
// TODO: Address the case when `that.debuggee` is `null`.
1022-
that.controller.updateBreakpoint(
1023-
that.debuggee as Debuggee,
1020+
// TODO: Address the case when `this.debuggee` is `null`.
1021+
this.controller.updateBreakpoint(
1022+
this.debuggee as Debuggee,
10241023
breakpoint,
10251024
(err /*, body*/) => {
10261025
if (err) {
1027-
that.logger.error('Unable to complete breakpoint on server', err);
1026+
this.logger.error('Unable to complete breakpoint on server', err);
10281027
}
10291028
}
10301029
);
@@ -1039,23 +1038,20 @@ export class Debuglet extends EventEmitter {
10391038
* @private
10401039
*/
10411040
scheduleBreakpointExpiry_(breakpoint: stackdriver.Breakpoint): void {
1042-
// eslint-disable-next-line @typescript-eslint/no-this-alias
1043-
const that = this;
1044-
10451041
const now = Date.now() / 1000;
10461042
const createdTime = breakpoint.createdTime
10471043
? Number(breakpoint.createdTime.seconds)
10481044
: now;
1049-
const expiryTime = createdTime + that.config.breakpointExpirationSec;
1045+
const expiryTime = createdTime + this.config.breakpointExpirationSec;
10501046

10511047
setTimeout(() => {
1052-
that.logger.info('Expiring breakpoint ' + breakpoint.id);
1048+
this.logger.info('Expiring breakpoint ' + breakpoint.id);
10531049
breakpoint.status = {
10541050
description: {format: 'The snapshot has expired'},
10551051
isError: true,
10561052
refersTo: StatusMessage.BREAKPOINT_AGE,
10571053
};
1058-
that.completeBreakpoint_(breakpoint);
1054+
this.completeBreakpoint_(breakpoint);
10591055
}, (expiryTime - now) * 1000).unref();
10601056
}
10611057

@@ -1066,6 +1062,7 @@ export class Debuglet extends EventEmitter {
10661062
* pending operations.
10671063
*/
10681064
stop(): void {
1065+
assert(this.controller);
10691066
assert.ok(this.running, 'stop can only be called on a running agent');
10701067
this.logger.debug('Stopping Debuglet');
10711068
this.running = false;

0 commit comments

Comments
 (0)