Skip to content

Commit f2110d7

Browse files
committed
Remove Inspector WebSocket
Inspector websocket crashes when the message is too big so use a tcp socket instead
1 parent c9b9d3d commit f2110d7

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.wireDebuggerClinet(() => 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.wireDebuggerClinet(() => 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.wireDebuggerClinet(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
165164
deploy.wait();
166165
}).future<void>()()).wait();
167166
}).future<void>()();
@@ -202,37 +201,44 @@ 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.wireDebuggerClinet(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
206205
break;
207206
case readyForAttach:
208-
this.readyForAttachAction(iosDevice).wait();
207+
this.wireDebuggerClinet(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
209208
break;
210209
}
211210
}).future<void>()()).wait();
212211
}).future<void>()();
213212
}
213+
214+
private wireDebuggerClinet(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 = temp.path({ suffix: ".sock" });
220+
createTcpSocketProxy(socketFileLocation, this.$logger, (callback) => connectEventually(factory, callback));
221+
} else {
222+
createWebSocketProxy(this.$logger, (callback) => connectEventually(factory, callback));
223+
}
214224

215-
private readyForAttachAction(iosDevice: iOSDevice.IOSDevice): IFuture<void> {
216-
createWebSocketProxy(this.$logger, (callback) => connectEventually(() => iosDevice.connectToPort(InspectorBackendPort), callback));
217-
return this.executeOpenDebuggerClient();
225+
this.executeOpenDebuggerClient(socketFileLocation).wait();
226+
}).future<void>()();
218227
}
219228

220-
public executeOpenDebuggerClient(): IFuture<void> {
229+
public executeOpenDebuggerClient(fileDescriptor: string): IFuture<void> {
221230
if (this.$options.client) {
222-
return this.openDebuggingClient();
231+
return this.openDebuggingClient(fileDescriptor);
223232
} else {
224233
return (() => {
225234
this.$logger.info("Supressing debugging client.");
226235
}).future<void>()();
227236
}
228237
}
229238

230-
private openDebuggingClient(): IFuture<void> {
239+
private openDebuggingClient(fileDescriptor: string): IFuture<void> {
231240
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-
241+
let frameworkVersion = this.getProjectFrameworkVersion().wait();
236242
let inspectorPath = this.getInspectorPath(frameworkVersion).wait();
237243
let inspectorSourceLocation = path.join(inspectorPath, "Safari/Main.html");
238244
let cmd: string = null;
@@ -244,12 +250,20 @@ class IOSDebugService implements IDebugService {
244250
if(!this.$fs.exists(inspectorApplicationPath).wait()) {
245251
this.$fs.unzip(path.join(inspectorPath, "NativeScript Inspector.zip"), inspectorPath).wait();
246252
}
247-
cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}'`;
253+
cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}' '${fileDescriptor}'`;
248254
}
249255

250256
this.$childProcess.exec(cmd).wait();
251257
}).future<void>()();
252258
}
259+
260+
private getProjectFrameworkVersion(): IFuture<string> {
261+
return (() => {
262+
this.$projectDataService.initialize(this.$projectData.projectDir);
263+
let platformData = this.$platformsData.getPlatformData(this.platform);
264+
return this.$projectDataService.getValue(platformData.frameworkPackageName).wait().version;
265+
}).future<string>()();
266+
}
253267

254268
private getInspectorPath(frameworkVersion: string): IFuture<string> {
255269
return (() => {
@@ -277,7 +291,41 @@ class IOSDebugService implements IDebugService {
277291
}
278292
$injector.register("iOSDebugService", IOSDebugService);
279293

280-
function createWebSocketProxy($logger: ILogger, socketFactory: (handler: (_socket: net.Socket) => void) => void): ws.Server {
294+
function createTcpSocketProxy(socketFileLocation: string, $logger: ILogger, socketFactory: (handler: (socket: net.Socket) => void) => void): net.Server {
295+
$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
296+
297+
var server = net.createServer({
298+
allowHalfOpen: true
299+
});
300+
301+
server.listen(socketFileLocation);
302+
303+
server.on("connection", (frontendSocket: net.Socket) => {
304+
$logger.info("Frontend client connected.");
305+
306+
frontendSocket.on("end", function() {
307+
$logger.info('Frontend socket closed!');
308+
process.exit(0);
309+
});
310+
311+
socketFactory((backendSocket) => {
312+
$logger.info("Backend socket created.");
313+
314+
backendSocket.on("end", () => {
315+
$logger.info("Backend socket closed!");
316+
process.exit(0);
317+
});
318+
319+
backendSocket.pipe(frontendSocket);
320+
frontendSocket.pipe(backendSocket);
321+
frontendSocket.resume();
322+
});
323+
});
324+
325+
return server;
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)