11
11
12
12
import type {
13
13
DebuggerRequest ,
14
- DebuggerResponse ,
15
14
GetScriptSourceRequest ,
16
15
GetScriptSourceResponse ,
17
16
MessageFromDevice ,
@@ -21,6 +20,7 @@ import type {
21
20
} from './types' ;
22
21
23
22
import * as fs from 'fs' ;
23
+ import * as http from 'http' ;
24
24
import * as path from 'path' ;
25
25
import WS from 'ws' ;
26
26
@@ -177,14 +177,13 @@ class Device {
177
177
socket.on('message', (message: string) => {
178
178
debug ( '(Debugger) -> (Proxy) (Device): ' + message ) ;
179
179
const debuggerRequest = JSON . parse ( message ) ;
180
- const interceptedResponse = this . _interceptMessageFromDebugger (
180
+ const handled = this . _interceptMessageFromDebugger (
181
181
debuggerRequest ,
182
182
debuggerInfo ,
183
+ socket ,
183
184
) ;
184
185
185
- if ( interceptedResponse ) {
186
- socket . send ( JSON . stringify ( interceptedResponse ) ) ;
187
- } else {
186
+ if ( ! handled ) {
188
187
this . _sendMessageToDevice ( {
189
188
event : 'wrappedEvent' ,
190
189
payload : {
@@ -437,21 +436,21 @@ class Device {
437
436
}
438
437
}
439
438
440
- // Allows to make changes in incoming messages from debugger.
439
+ // Allows to make changes in incoming messages from debugger. Returns a boolean
440
+ // indicating whether the message has been handled locally (i.e. does not need
441
+ // to be forwarded to the target).
441
442
_interceptMessageFromDebugger (
442
443
req : DebuggerRequest ,
443
444
debuggerInfo : DebuggerInfo ,
444
- ) : ? DebuggerResponse {
445
- let response = null ;
445
+ socket : typeof WS ,
446
+ ) : boolean {
446
447
if ( req . method === 'Debugger.setBreakpointByUrl' ) {
447
448
this . _processDebuggerSetBreakpointByUrl ( req , debuggerInfo ) ;
448
449
} else if ( req . method === 'Debugger.getScriptSource' ) {
449
- response = {
450
- id : req . id ,
451
- result : this . _processDebuggerGetScriptSource ( req ) ,
452
- } ;
450
+ this . _processDebuggerGetScriptSource ( req , socket ) ;
451
+ return true ;
453
452
}
454
- return response ;
453
+ return false ;
455
454
}
456
455
457
456
_processDebuggerSetBreakpointByUrl (
@@ -488,26 +487,59 @@ class Device {
488
487
489
488
_processDebuggerGetScriptSource (
490
489
req : GetScriptSourceRequest ,
491
- ) : GetScriptSourceResponse {
490
+ socket : typeof WS ,
491
+ ) {
492
492
let scriptSource = `Source for script with id '${ req . params . scriptId } ' was not found.` ;
493
493
494
+ const sendResponse = ( ) => {
495
+ const result : GetScriptSourceResponse = { scriptSource} ;
496
+ socket . send ( JSON . stringify ( { id : req . id , result} ) ) ;
497
+ } ;
498
+
494
499
const pathToSource = this . _scriptIdToSourcePathMapping . get (
495
500
req . params . scriptId ,
496
501
) ;
497
502
if ( pathToSource ) {
503
+ let pathIsURL = false ;
498
504
try {
499
- scriptSource = fs . readFileSync (
500
- path . resolve ( this . _projectRoot , pathToSource ) ,
501
- 'utf8' ,
502
- ) ;
503
- } catch ( err ) {
504
- scriptSource = err . message ;
505
+ pathIsURL = new URL ( pathToSource ) . hostname == 'localhost' ;
506
+ } catch { }
507
+
508
+ if ( pathIsURL ) {
509
+ http
510
+ . get ( pathToSource , httpResponse => {
511
+ const { statusCode} = httpResponse ;
512
+ if ( statusCode == 200 ) {
513
+ httpResponse . setEncoding ( 'utf8' ) ;
514
+ scriptSource = '' ;
515
+ httpResponse . on ( 'data' , body => {
516
+ scriptSource += body ;
517
+ } ) ;
518
+ httpResponse . on ( 'end' , ( ) => {
519
+ sendResponse ( ) ;
520
+ } ) ;
521
+ } else {
522
+ scriptSource = `Fetching ${ pathToSource } returned status ${ statusCode } ` ;
523
+ sendResponse ( ) ;
524
+ httpResponse . resume ( ) ;
525
+ }
526
+ } )
527
+ . on ( 'error' , e => {
528
+ scriptSource = `Fetching ${ pathToSource } failed with error ${ e . message } ` ;
529
+ sendResponse ( ) ;
530
+ } ) ;
531
+ } else {
532
+ try {
533
+ scriptSource = fs . readFileSync (
534
+ path . resolve ( this . _projectRoot , pathToSource ) ,
535
+ 'utf8' ,
536
+ ) ;
537
+ } catch ( err ) {
538
+ scriptSource = err . message ;
539
+ }
540
+ sendResponse ( ) ;
505
541
}
506
542
}
507
-
508
- return {
509
- scriptSource,
510
- } ;
511
543
}
512
544
513
545
_mapToDevicePageId ( pageId : string ) : string {
0 commit comments