@@ -45,20 +45,19 @@ function fetchProxy(localhost: boolean): Promise<string> {
4545export interface DevToolsOptions {
4646 internet ?: boolean
4747 localhost ?: boolean
48+ tcp ?: boolean
4849
4950 bytecodeFile ?: string
5051 debugFile ?: string
5152}
5253
5354export async function devtools ( options : DevToolsOptions & CmdOptions ) {
54- const { internet, localhost, bytecodeFile, debugFile } = options
55+ const { internet, localhost, bytecodeFile, debugFile, tcp } = options
5556 const port = 8081
5657 const tcpPort = 8082
5758 const listenHost = internet ? undefined : "127.0.0.1"
5859
5960 log ( `starting dev tools at http://localhost:${ port } ` )
60- log ( ` websocket: ws://localhost:${ port } ` )
61- log ( ` tcpsocket: tcp://localhost:${ tcpPort } ` )
6261
6362 // download proxy sources
6463 const proxyHtml = await fetchProxy ( localhost )
@@ -71,9 +70,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
7170 ? ( ) => {
7271 const bytecode = readFileSync ( bytecodeFile )
7372 const dbg = debugFile ? readJSONSync ( debugFile ) : undefined
74- debug (
75- `refresh bytecode ${ prettySize ( bytecode . length ) } ...`
76- )
73+ debug ( `refresh bytecode ${ prettySize ( bytecode . length ) } ...` )
7774 const msg = JSON . stringify ( {
7875 type : "bytecode" ,
7976 channel : "devicescript" ,
@@ -84,6 +81,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
8481 }
8582 : undefined
8683
84+ log ( ` websocket: ws://localhost:${ port } ` )
8785 const server = http . createServer ( function ( req , res ) {
8886 const parsedUrl = url . parse ( req . url )
8987 const pathname = parsedUrl . pathname
@@ -121,31 +119,33 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
121119 client . on ( "error" , ( ev : Error ) => error ( ev ) )
122120 }
123121 } )
122+ server . listen ( port , listenHost )
124123
125- const tcpServer = net . createServer ( ( client : any ) => {
126- const sender = "tcp" + Math . random ( )
127- client [ SENDER_FIELD ] = sender
128- client . send = ( pkt0 : Buffer ) => {
129- const pkt = new Uint8Array ( pkt0 )
130- const b = new Uint8Array ( 1 + pkt . length )
131- b [ 0 ] = pkt . length
132- b . set ( pkt , 1 )
133- try {
134- client . write ( b )
135- } catch {
124+ if ( tcp ) {
125+ log ( ` tcpsocket: tcp://localhost:${ tcpPort } ` )
126+ const tcpServer = net . createServer ( ( client : any ) => {
127+ const sender = "tcp" + Math . random ( )
128+ client [ SENDER_FIELD ] = sender
129+ client . send = ( pkt0 : Buffer ) => {
130+ const pkt = new Uint8Array ( pkt0 )
131+ const b = new Uint8Array ( 1 + pkt . length )
132+ b [ 0 ] = pkt . length
133+ b . set ( pkt , 1 )
136134 try {
137- client . end ( )
138- } catch { } // eslint-disable-line no-empty
135+ client . write ( b )
136+ } catch {
137+ try {
138+ client . end ( )
139+ } catch { } // eslint-disable-line no-empty
140+ }
139141 }
140- }
141- clients . push ( client )
142- log ( `tcpclient: connected (${ sender } ${ clients . length } clients)` )
143- client . on ( "end" , ( ) => removeClient ( client ) )
144- client . on ( "error" , ( ev : Error ) => error ( ev ) )
145- } )
146-
147- server . listen ( port , listenHost )
148- tcpServer . listen ( tcpPort , listenHost )
142+ clients . push ( client )
143+ log ( `tcpclient: connected (${ sender } ${ clients . length } clients)` )
144+ client . on ( "end" , ( ) => removeClient ( client ) )
145+ client . on ( "error" , ( ev : Error ) => error ( ev ) )
146+ } )
147+ tcpServer . listen ( tcpPort , listenHost )
148+ }
149149
150150 if ( bytecodeFile ) {
151151 debug ( `watching ${ bytecodeFile } ...` )
0 commit comments