Skip to content

Commit 306a973

Browse files
Merge pull request #932 from NativeScript/KristinaKoeva/RemoveSocket
Remove Inspector WebSocket
2 parents ff0ec8f + 630e0d8 commit 306a973

File tree

1 file changed

+68
-20
lines changed

1 file changed

+68
-20
lines changed

lib/services/ios-debug-service.ts

+68-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as stream from "stream";
99
import * as path from "path";
1010
import Future = require("fibers/future");
1111
import semver = require("semver");
12+
import temp = require("temp");
1213

1314
module notification {
1415
function formatNotification(bundleId: string, notification: string) {
@@ -118,15 +119,14 @@ class IOSDebugService implements IDebugService {
118119
let emulatorPackage = this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait();
119120

120121
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();
123123
}).future<void>()();
124124
}
125125

126126
private emulatorStart(): IFuture<void> {
127127
return (() => {
128-
createWebSocketProxy(this.$logger, (callback) => connectEventually(() => net.connect(InspectorBackendPort), callback));
129-
this.executeOpenDebuggerClient().wait();
128+
this.wireDebuggerClient(() => net.connect(InspectorBackendPort)).wait();
129+
130130
let projectId = this.$projectData.projectId;
131131
let attachRequestMessage = notification.attachRequest(projectId);
132132

@@ -160,8 +160,7 @@ class IOSDebugService implements IDebugService {
160160
this.$errors.failWithoutHelp("Timeout waiting for NativeScript debugger.");
161161
}
162162

163-
createWebSocketProxy(this.$logger, (callback) => connectEventually(() => iosDevice.connectToPort(InspectorBackendPort), callback));
164-
this.executeOpenDebuggerClient().wait();
163+
this.wireDebuggerClient(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
165164
deploy.wait();
166165
}).future<void>()()).wait();
167166
}).future<void>()();
@@ -202,37 +201,43 @@ class IOSDebugService implements IDebugService {
202201
} catch (e) {
203202
this.$errors.failWithoutHelp(`The application ${projectId} timed out when performing the NativeScript debugger handshake.`);
204203
}
205-
this.readyForAttachAction(iosDevice).wait();
204+
this.wireDebuggerClient(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
206205
break;
207206
case readyForAttach:
208-
this.readyForAttachAction(iosDevice).wait();
207+
this.wireDebuggerClient(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
209208
break;
210209
}
211210
}).future<void>()()).wait();
212211
}).future<void>()();
213212
}
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+
}
214223

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>()();
218226
}
219227

220-
public executeOpenDebuggerClient(): IFuture<void> {
228+
public executeOpenDebuggerClient(fileDescriptor: string): IFuture<void> {
221229
if (this.$options.client) {
222-
return this.openDebuggingClient();
230+
return this.openDebuggingClient(fileDescriptor);
223231
} else {
224232
return (() => {
225233
this.$logger.info("Supressing debugging client.");
226234
}).future<void>()();
227235
}
228236
}
229237

230-
private openDebuggingClient(): IFuture<void> {
238+
private openDebuggingClient(fileDescriptor: string): IFuture<void> {
231239
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();
236241
let inspectorPath = this.getInspectorPath(frameworkVersion).wait();
237242
let inspectorSourceLocation = path.join(inspectorPath, "Safari/Main.html");
238243
let cmd: string = null;
@@ -244,12 +249,20 @@ class IOSDebugService implements IDebugService {
244249
if(!this.$fs.exists(inspectorApplicationPath).wait()) {
245250
this.$fs.unzip(path.join(inspectorPath, "NativeScript Inspector.zip"), inspectorPath).wait();
246251
}
247-
cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}'`;
252+
cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}' '${fileDescriptor}'`;
248253
}
249254

250255
this.$childProcess.exec(cmd).wait();
251256
}).future<void>()();
252257
}
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+
}
253266

254267
private getInspectorPath(frameworkVersion: string): IFuture<string> {
255268
return (() => {
@@ -277,7 +290,42 @@ class IOSDebugService implements IDebugService {
277290
}
278291
$injector.register("iOSDebugService", IOSDebugService);
279292

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 {
281329
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
282330
let localPort = 8080;
283331

0 commit comments

Comments
 (0)