@@ -9,6 +9,7 @@ import * as stream from "stream";
9
9
import * as path from "path" ;
10
10
import Future = require( "fibers/future" ) ;
11
11
import semver = require( "semver" ) ;
12
+ import temp = require( "temp" ) ;
12
13
13
14
module notification {
14
15
function formatNotification ( bundleId : string , notification : string ) {
@@ -118,15 +119,14 @@ class IOSDebugService implements IDebugService {
118
119
let emulatorPackage = this . $platformService . getLatestApplicationPackageForEmulator ( platformData ) . wait ( ) ;
119
120
120
121
this . $iOSEmulatorServices . startEmulator ( emulatorPackage . packageName , { args : "--nativescript-debug-brk" } ) . wait ( ) ;
121
- createWebSocketProxy ( this . $logger , ( callback ) => connectEventually ( ( ) => net . connect ( InspectorBackendPort ) , callback ) ) ;
122
- this . executeOpenDebuggerClient ( ) . wait ( ) ;
122
+ this . wireDebuggerClient ( ( ) => net . connect ( InspectorBackendPort ) ) . wait ( ) ;
123
123
} ) . future < void > ( ) ( ) ;
124
124
}
125
125
126
126
private emulatorStart ( ) : IFuture < void > {
127
127
return ( ( ) => {
128
- createWebSocketProxy ( this . $logger , ( callback ) => connectEventually ( ( ) => net . connect ( InspectorBackendPort ) , callback ) ) ;
129
- this . executeOpenDebuggerClient ( ) . wait ( ) ;
128
+ this . wireDebuggerClient ( ( ) => net . connect ( InspectorBackendPort ) ) . wait ( ) ;
129
+
130
130
let projectId = this . $projectData . projectId ;
131
131
let attachRequestMessage = notification . attachRequest ( projectId ) ;
132
132
@@ -160,8 +160,7 @@ class IOSDebugService implements IDebugService {
160
160
this . $errors . failWithoutHelp ( "Timeout waiting for NativeScript debugger." ) ;
161
161
}
162
162
163
- createWebSocketProxy ( this . $logger , ( callback ) => connectEventually ( ( ) => iosDevice . connectToPort ( InspectorBackendPort ) , callback ) ) ;
164
- this . executeOpenDebuggerClient ( ) . wait ( ) ;
163
+ this . wireDebuggerClient ( ( ) => iosDevice . connectToPort ( InspectorBackendPort ) ) . wait ( ) ;
165
164
deploy . wait ( ) ;
166
165
} ) . future < void > ( ) ( ) ) . wait ( ) ;
167
166
} ) . future < void > ( ) ( ) ;
@@ -202,37 +201,43 @@ class IOSDebugService implements IDebugService {
202
201
} catch ( e ) {
203
202
this . $errors . failWithoutHelp ( `The application ${ projectId } timed out when performing the NativeScript debugger handshake.` ) ;
204
203
}
205
- this . readyForAttachAction ( iosDevice ) . wait ( ) ;
204
+ this . wireDebuggerClient ( ( ) => iosDevice . connectToPort ( InspectorBackendPort ) ) . wait ( ) ;
206
205
break ;
207
206
case readyForAttach :
208
- this . readyForAttachAction ( iosDevice ) . wait ( ) ;
207
+ this . wireDebuggerClient ( ( ) => iosDevice . connectToPort ( InspectorBackendPort ) ) . wait ( ) ;
209
208
break ;
210
209
}
211
210
} ) . future < void > ( ) ( ) ) . wait ( ) ;
212
211
} ) . future < void > ( ) ( ) ;
213
212
}
213
+
214
+ private wireDebuggerClient ( factory : ( ) => net . Socket ) : IFuture < void > {
215
+ return ( ( ) => {
216
+ let frameworkVersion = this . getProjectFrameworkVersion ( ) . wait ( ) ;
217
+ let socketFileLocation = "" ;
218
+ if ( semver . gte ( frameworkVersion , "1.4.0" ) ) {
219
+ socketFileLocation = createTcpSocketProxy ( this . $logger , ( callback ) => connectEventually ( factory , callback ) ) ;
220
+ } else {
221
+ createWebSocketProxy ( this . $logger , ( callback ) => connectEventually ( factory , callback ) ) ;
222
+ }
214
223
215
- private readyForAttachAction ( iosDevice : iOSDevice . IOSDevice ) : IFuture < void > {
216
- createWebSocketProxy ( this . $logger , ( callback ) => connectEventually ( ( ) => iosDevice . connectToPort ( InspectorBackendPort ) , callback ) ) ;
217
- return this . executeOpenDebuggerClient ( ) ;
224
+ this . executeOpenDebuggerClient ( socketFileLocation ) . wait ( ) ;
225
+ } ) . future < void > ( ) ( ) ;
218
226
}
219
227
220
- public executeOpenDebuggerClient ( ) : IFuture < void > {
228
+ public executeOpenDebuggerClient ( fileDescriptor : string ) : IFuture < void > {
221
229
if ( this . $options . client ) {
222
- return this . openDebuggingClient ( ) ;
230
+ return this . openDebuggingClient ( fileDescriptor ) ;
223
231
} else {
224
232
return ( ( ) => {
225
233
this . $logger . info ( "Supressing debugging client." ) ;
226
234
} ) . future < void > ( ) ( ) ;
227
235
}
228
236
}
229
237
230
- private openDebuggingClient ( ) : IFuture < void > {
238
+ private openDebuggingClient ( fileDescriptor : string ) : IFuture < void > {
231
239
return ( ( ) => {
232
- this . $projectDataService . initialize ( this . $projectData . projectDir ) ;
233
- let platformData = this . $platformsData . getPlatformData ( this . platform ) ;
234
- let frameworkVersion = this . $projectDataService . getValue ( platformData . frameworkPackageName ) . wait ( ) . version ;
235
-
240
+ let frameworkVersion = this . getProjectFrameworkVersion ( ) . wait ( ) ;
236
241
let inspectorPath = this . getInspectorPath ( frameworkVersion ) . wait ( ) ;
237
242
let inspectorSourceLocation = path . join ( inspectorPath , "Safari/Main.html" ) ;
238
243
let cmd : string = null ;
@@ -244,12 +249,20 @@ class IOSDebugService implements IDebugService {
244
249
if ( ! this . $fs . exists ( inspectorApplicationPath ) . wait ( ) ) {
245
250
this . $fs . unzip ( path . join ( inspectorPath , "NativeScript Inspector.zip" ) , inspectorPath ) . wait ( ) ;
246
251
}
247
- cmd = `open -a '${ inspectorApplicationPath } ' --args '${ inspectorSourceLocation } ' '${ this . $projectData . projectName } '` ;
252
+ cmd = `open -a '${ inspectorApplicationPath } ' --args '${ inspectorSourceLocation } ' '${ this . $projectData . projectName } ' ' ${ fileDescriptor } ' ` ;
248
253
}
249
254
250
255
this . $childProcess . exec ( cmd ) . wait ( ) ;
251
256
} ) . future < void > ( ) ( ) ;
252
257
}
258
+
259
+ private getProjectFrameworkVersion ( ) : IFuture < string > {
260
+ return ( ( ) => {
261
+ this . $projectDataService . initialize ( this . $projectData . projectDir ) ;
262
+ let platformData = this . $platformsData . getPlatformData ( this . platform ) ;
263
+ return this . $projectDataService . getValue ( platformData . frameworkPackageName ) . wait ( ) . version ;
264
+ } ) . future < string > ( ) ( ) ;
265
+ }
253
266
254
267
private getInspectorPath ( frameworkVersion : string ) : IFuture < string > {
255
268
return ( ( ) => {
@@ -277,7 +290,42 @@ class IOSDebugService implements IDebugService {
277
290
}
278
291
$injector . register ( "iOSDebugService" , IOSDebugService ) ;
279
292
280
- function createWebSocketProxy ( $logger : ILogger , socketFactory : ( handler : ( _socket : net . Socket ) => void ) => void ) : ws . Server {
293
+ function createTcpSocketProxy ( $logger : ILogger , socketFactory : ( handler : ( socket : net . Socket ) => void ) => void ) : string {
294
+ $logger . info ( "\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n" ) ;
295
+
296
+ let server = net . createServer ( {
297
+ allowHalfOpen : true
298
+ } ) ;
299
+
300
+ server . on ( "connection" , ( frontendSocket : net . Socket ) => {
301
+ $logger . info ( "Frontend client connected." ) ;
302
+
303
+ frontendSocket . on ( "end" , function ( ) {
304
+ $logger . info ( 'Frontend socket closed!' ) ;
305
+ process . exit ( 0 ) ;
306
+ } ) ;
307
+
308
+ socketFactory ( ( backendSocket ) => {
309
+ $logger . info ( "Backend socket created." ) ;
310
+
311
+ backendSocket . on ( "end" , ( ) => {
312
+ $logger . info ( "Backend socket closed!" ) ;
313
+ process . exit ( 0 ) ;
314
+ } ) ;
315
+
316
+ backendSocket . pipe ( frontendSocket ) ;
317
+ frontendSocket . pipe ( backendSocket ) ;
318
+ frontendSocket . resume ( ) ;
319
+ } ) ;
320
+ } ) ;
321
+
322
+ let socketFileLocation = temp . path ( { suffix : ".sock" } ) ;
323
+ server . listen ( socketFileLocation ) ;
324
+
325
+ return socketFileLocation ;
326
+ }
327
+
328
+ function createWebSocketProxy ( $logger : ILogger , socketFactory : ( handler : ( socket : net . Socket ) => void ) => void ) : ws . Server {
281
329
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
282
330
let localPort = 8080 ;
283
331
0 commit comments