@@ -16,20 +16,18 @@ Array.prototype.peekLast = function() {
1616
1717let connection ;
1818
19- function createTabs ( tabs ) {
20-
19+ function createTabs ( tabs , { type, clientType} ) {
2120 return tabs
2221 . filter ( tab => {
23- const isPage = tab . type == "page" ;
24- return isPage ;
22+ return tab . type == type ;
2523 } )
2624 . map ( tab => {
2725 return Tab ( {
2826 title : tab . title ,
2927 url : tab . url ,
3028 id : tab . id ,
3129 tab,
32- browser : "chrome"
30+ clientType
3331 } ) ;
3432 } ) ;
3533}
@@ -41,48 +39,83 @@ function connectClient() {
4139 return Promise . resolve ( createTabs ( [ ] ) )
4240 }
4341
44- const webSocketPort = getValue ( "chrome.webSocketPort" ) ;
45- const url = `http://localhost:${ webSocketPort } /json/list` ;
42+ const port = getValue ( "chrome.port" ) ;
43+ const host = getValue ( "chrome.host" ) ;
44+ const url = `http://${ host } :${ port } /json/list` ;
45+
4646 networkRequest ( url ) . then ( res => {
47- deferred . resolve ( createTabs ( JSON . parse ( res . content ) ) )
48- } ) . catch ( ( ) => deferred . reject ( ) ) ;
47+ deferred . resolve ( createTabs (
48+ JSON . parse ( res . content ) ,
49+ { clientType : "chrome" , type : "page" }
50+ ) )
51+ } ) . catch ( err => deferred . reject ( ) ) ;
4952
5053 return deferred . promise ;
5154}
5255
53- function connectTab ( tab ) {
54- return connect ( tab . webSocketDebuggerUrl ) . then ( conn => {
55- connection = conn ;
56+
57+ function connectNodeClient ( ) {
58+ const deferred = defer ( ) ;
59+
60+ if ( ! getValue ( "node.debug" ) ) {
61+ return Promise . resolve ( createTabs ( [ ] ) )
62+ }
63+
64+ const host = getValue ( "node.host" ) ;
65+ const port = getValue ( "node.port" ) ;
66+ const url = `http://${ host } :${ port } /json/list` ;
67+
68+ networkRequest ( url ) . then ( res => {
69+ deferred . resolve ( createTabs (
70+ JSON . parse ( res . content ) ,
71+ { clientType : "node" , type : "node" }
72+ ) )
73+ } ) . catch ( err => {
74+ console . log ( err ) ;
75+ deferred . reject ( ) ;
5676 } ) ;
77+
78+ return deferred . promise ;
5779}
5880
59- function connectNode ( url ) {
60- return connect ( url ) . then ( conn => {
61- connection = conn ;
62- } ) ;
81+ function connectTab ( tab ) {
82+ return connect ( tab . webSocketDebuggerUrl , { type : "browser" } )
83+ . then ( conn => { connection = conn } ) ;
84+ }
85+
86+ function connectNode ( tab ) {
87+ return connect ( tab . webSocketDebuggerUrl , { type : "node" } )
88+ . then ( conn => { connection = conn } ) ;
6389}
6490
65- function initPage ( actions ) {
91+ function initPage ( actions , { clientType } ) {
6692 const agents = connection . _agents ;
6793
68- setupCommands ( { agents : agents } ) ;
69- setupEvents ( { actions, agents } )
94+ setupCommands ( { agents, clientType } ) ;
95+ setupEvents ( { actions, agents, clientType } )
7096
7197 agents . Debugger . enable ( ) ;
7298 agents . Debugger . setPauseOnExceptions ( "none" ) ;
7399 agents . Debugger . setAsyncCallStackDepth ( 0 ) ;
74100
75101 agents . Runtime . enable ( ) ;
76- agents . Runtime . run ( ) ;
77102
78- agents . Page . enable ( ) ;
103+ if ( clientType == "node" ) {
104+ agents . Runtime . runIfWaitingForDebugger ( )
105+ }
106+
107+ if ( clientType == "chrome" ) {
108+ agents . Page . enable ( ) ;
109+ }
110+
79111
80112 connection . registerDispatcher ( "Debugger" , clientEvents ) ;
81113 connection . registerDispatcher ( "Page" , pageEvents ) ;
82114}
83115
84116module . exports = {
85117 connectClient,
118+ connectNodeClient,
86119 clientCommands,
87120 connectNode,
88121 connectTab,
0 commit comments