1414 * limitations under the License.
1515 */
1616
17- import { Common } from '../types/common-types' ;
17+ import { Common } from '../types/common-types' ;
1818const common : Common = require ( '@google-cloud/common' ) ;
1919
2020import * as crypto from 'crypto' ;
2121import { EventEmitter } from 'events' ;
2222import * as extend from 'extend' ;
2323import * as fs from 'fs' ;
2424
25- import { GcpMetadata } from '../types/gcp-metadata-types' ;
25+ import { GcpMetadata } from '../types/gcp-metadata-types' ;
2626const metadata : GcpMetadata = require ( 'gcp-metadata' ) ;
2727
2828import * as _ from 'lodash' ;
@@ -219,16 +219,20 @@ export class Debuglet extends EventEmitter {
219219 // `null`.
220220 scanner . scan (
221221 ! id , that . config_ . workingDirectory as string , / .j s $ | .m a p $ / ,
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 ( / .j s $ / ) ;
231- const mapFiles = ( fileStats as scanner . ScanResults ) . selectFiles ( / .m a p $ / , process . cwd ( ) ) ;
232+ const jsStats =
233+ ( fileStats as scanner . ScanResults ) . selectStats ( / .j s $ / ) ;
234+ const mapFiles = ( fileStats as scanner . ScanResults )
235+ . selectFiles ( / .m a p $ / , 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 ) {
0 commit comments