-
-
Notifications
You must be signed in to change notification settings - Fork 197
Send page reload message to iOS runtime #1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2268bfc
4ef739e
ac77f74
ee3be6f
8e72b52
541d5cb
78cbba1
dd3f71b
c8df28b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
+24 −8 | declarations.d.ts | |
+6 −3 | definitions/mobile.d.ts | |
+20 −5 | helpers.ts | |
+12 −2 | mobile/android/android-device-file-system.ts | |
+2 −6 | mobile/android/android-device.ts | |
+15 −9 | mobile/ios/ios-application-manager.ts | |
+104 −22 | mobile/ios/ios-core.ts | |
+4 −0 | mobile/ios/ios-device-file-system.ts | |
+46 −19 | mobile/ios/ios-emulator-services.ts | |
+2 −2 | mobile/ios/ios-proxy-services.ts | |
+77 −96 | services/usb-livesync-service-base.ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
///<reference path="../../.d.ts"/> | ||
"use strict"; | ||
|
||
export class IOSNotification implements IiOSNotification { | ||
private static WAIT_FOR_DEBUG_NOTIFICATION_NAME = "WaitForDebugger"; | ||
private static ATTACH_REQUEST_NOTIFICATION_NAME = "AttachRequest"; | ||
private static APP_LAUNCHING_NOTIFICATION_NAME = "AppLaunching"; | ||
private static READY_FOR_ATTACH_NOTIFICATION_NAME = "ReadyForAttach"; | ||
private static ATTACH_AVAILABILITY_QUERY_NOTIFICATION_NAME = "AttachAvailabilityQuery"; | ||
private static ALREADY_CONNECTED_NOTIFICATION_NAME = "AlreadyConnected"; | ||
private static ATTACH_AVAILABLE_NOTIFICATION_NAME = "AttachAvailable"; | ||
|
||
constructor(private $projectData: IProjectData) { } | ||
|
||
public get waitForDebug() { | ||
return this.formatNotification(IOSNotification.WAIT_FOR_DEBUG_NOTIFICATION_NAME); | ||
} | ||
|
||
public get attachRequest(): string { | ||
return this.formatNotification(IOSNotification.ATTACH_REQUEST_NOTIFICATION_NAME); | ||
} | ||
|
||
public get appLaunching(): string { | ||
return this.formatNotification(IOSNotification.APP_LAUNCHING_NOTIFICATION_NAME); | ||
} | ||
|
||
public get readyForAttach(): string { | ||
return this.formatNotification(IOSNotification.READY_FOR_ATTACH_NOTIFICATION_NAME); | ||
} | ||
|
||
public get attachAvailabilityQuery() { | ||
return this.formatNotification(IOSNotification.ATTACH_AVAILABILITY_QUERY_NOTIFICATION_NAME); | ||
} | ||
|
||
public get alreadyConnected() { | ||
return this.formatNotification(IOSNotification.ALREADY_CONNECTED_NOTIFICATION_NAME); | ||
} | ||
|
||
public get attachAvailable() { | ||
return this.formatNotification(IOSNotification.ATTACH_AVAILABLE_NOTIFICATION_NAME); | ||
} | ||
|
||
private formatNotification(notification: string) { | ||
return `${this.$projectData.projectId}:NativeScript.Debug.${notification}`; | ||
} | ||
} | ||
$injector.register("iOSNotification", IOSNotification); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
///<reference path="../../.d.ts"/> | ||
"use strict"; | ||
|
||
import * as stream from "stream"; | ||
|
||
export class PacketStream extends stream.Transform { | ||
private buffer: Buffer; | ||
private offset: number; | ||
|
||
constructor(opts?: stream.TransformOptions) { | ||
super(opts); | ||
} | ||
|
||
public _transform(packet: any, encoding: string, done: Function): void { | ||
while (packet.length > 0) { | ||
if (!this.buffer) { | ||
// read length | ||
let length = packet.readInt32BE(0); | ||
this.buffer = new Buffer(length); | ||
this.offset = 0; | ||
packet = packet.slice(4); | ||
} | ||
|
||
packet.copy(this.buffer, this.offset); | ||
let copied = Math.min(this.buffer.length - this.offset, packet.length); | ||
this.offset += copied; | ||
packet = packet.slice(copied); | ||
|
||
if (this.offset === this.buffer.length) { | ||
this.push(this.buffer); | ||
this.buffer = undefined; | ||
} | ||
} | ||
done(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
///<reference path="../../.d.ts"/> | ||
"use strict"; | ||
|
||
import { PacketStream } from "./packet-stream"; | ||
import * as net from "net"; | ||
import * as semver from "semver"; | ||
import * as ws from "ws"; | ||
import temp = require("temp"); | ||
import * as helpers from "../../common/helpers"; | ||
|
||
export class SocketProxyFactory implements ISocketProxyFactory { | ||
constructor(private $logger: ILogger, | ||
private $projectData: IProjectData, | ||
private $projectDataService: IProjectDataService) { } | ||
|
||
public createSocketProxy(factory: () => net.Socket): IFuture<any> { | ||
return (() => { | ||
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback); | ||
|
||
this.$projectDataService.initialize(this.$projectData.projectDir); | ||
let frameworkVersion = this.$projectDataService.getValue("tns-ios").wait().version; | ||
let result: any; | ||
|
||
if(semver.gte(frameworkVersion, "1.4.0")) { | ||
result = this.createTcpSocketProxy(socketFactory); | ||
} else { | ||
result = this.createWebSocketProxy(socketFactory); | ||
} | ||
|
||
return result; | ||
}).future<any>()(); | ||
} | ||
|
||
private createWebSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): ws.Server { | ||
// NOTE: We will try to provide command line options to select ports, at least on the localhost. | ||
let localPort = 8080; | ||
|
||
this.$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n"); | ||
|
||
// NB: When the inspector frontend connects we might not have connected to the inspector backend yet. | ||
// That's why we use the verifyClient callback of the websocket server to stall the upgrade request until we connect. | ||
// We store the socket that connects us to the device in the upgrade request object itself and later on retrieve it | ||
// in the connection callback. | ||
|
||
let server = ws.createServer(<any>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you really need the cast? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, due to some bugs in .ws.d files 😿 |
||
port: localPort, | ||
verifyClient: (info: any, callback: Function) => { | ||
this.$logger.info("Frontend client connected."); | ||
socketFactory((_socket: any) => { | ||
this.$logger.info("Backend socket created."); | ||
info.req["__deviceSocket"] = _socket; | ||
callback(true); | ||
}); | ||
} | ||
}); | ||
server.on("connection", (webSocket) => { | ||
let encoding = "utf16le"; | ||
|
||
let deviceSocket: net.Socket = (<any>webSocket.upgradeReq)["__deviceSocket"]; | ||
let packets = new PacketStream(); | ||
deviceSocket.pipe(packets); | ||
|
||
packets.on("data", (buffer: Buffer) => { | ||
webSocket.send(buffer.toString(encoding)); | ||
}); | ||
|
||
webSocket.on("message", (message, flags) => { | ||
let length = Buffer.byteLength(message, encoding); | ||
let payload = new Buffer(length + 4); | ||
payload.writeInt32BE(length, 0); | ||
payload.write(message, 4, length, encoding); | ||
deviceSocket.write(payload); | ||
}); | ||
|
||
deviceSocket.on("end", () => { | ||
this.$logger.info("Backend socket closed!"); | ||
process.exit(0); | ||
}); | ||
|
||
webSocket.on("close", () => { | ||
this.$logger.info('Frontend socket closed!'); | ||
process.exit(0); | ||
}); | ||
|
||
}); | ||
|
||
this.$logger.info("Opened localhost " + localPort); | ||
return server; | ||
} | ||
|
||
private createTcpSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): string { | ||
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n"); | ||
|
||
let server = net.createServer({ | ||
allowHalfOpen: true | ||
}); | ||
|
||
server.on("connection", (frontendSocket: net.Socket) => { | ||
this.$logger.info("Frontend client connected."); | ||
|
||
frontendSocket.on("end", () => { | ||
this.$logger.info('Frontend socket closed!'); | ||
process.exit(0); | ||
}); | ||
|
||
socketFactory((backendSocket: net.Socket) => { | ||
this.$logger.info("Backend socket created."); | ||
|
||
backendSocket.on("end", () => { | ||
this.$logger.info("Backend socket closed!"); | ||
process.exit(0); | ||
}); | ||
|
||
backendSocket.pipe(frontendSocket); | ||
frontendSocket.pipe(backendSocket); | ||
frontendSocket.resume(); | ||
}); | ||
}); | ||
|
||
let socketFileLocation = temp.path({ suffix: ".sock" }); | ||
server.listen(socketFileLocation); | ||
|
||
return socketFileLocation; | ||
} | ||
} | ||
$injector.register("socketProxyFactory", SocketProxyFactory); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
///<reference path="../../.d.ts"/> | ||
"use strict"; | ||
|
||
import * as helpers from "../../common/helpers"; | ||
import * as iOSProxyServices from "../../common/mobile/ios/ios-proxy-services"; | ||
|
||
export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor { | ||
constructor(private $errors: IErrors, | ||
private $injector: IInjector, | ||
private $iOSNotification: IiOSNotification, | ||
private $iOSNotificationService: IiOSNotificationService, | ||
private $logger: ILogger, | ||
private $projectData: IProjectData, | ||
private $socketProxyFactory: ISocketProxyFactory) { } | ||
|
||
public executeAttachRequest(device: Mobile.IiOSDevice, timeout: number): IFuture<void> { | ||
return (() => { | ||
let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector); | ||
|
||
let [alreadyConnected, readyForAttach, attachAvailable] = [this.$iOSNotification.alreadyConnected, this.$iOSNotification.readyForAttach, this.$iOSNotification.attachAvailable] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice destructing usage. |
||
.map((notification) => this.$iOSNotificationService.awaitNotification(npc, notification, timeout)); | ||
|
||
npc.postNotificationAndAttachForData(this.$iOSNotification.attachAvailabilityQuery); | ||
|
||
let receivedNotification: IFuture<string>; | ||
try { | ||
receivedNotification = helpers.whenAny(alreadyConnected, readyForAttach, attachAvailable).wait(); | ||
} catch (e) { | ||
this.$errors.failWithoutHelp(`The application ${this.$projectData.projectId} does not appear to be running on ${device.deviceInfo.displayName} or is not built with debugging enabled.`); | ||
} | ||
|
||
switch (receivedNotification) { | ||
case alreadyConnected: | ||
this.$errors.failWithoutHelp("A client is already connected."); | ||
break; | ||
case attachAvailable: | ||
this.executeAttachAvailable(npc, timeout).wait(); | ||
break; | ||
case readyForAttach: | ||
break; | ||
} | ||
}).future<void>()(); | ||
} | ||
|
||
public executeLaunchRequest(device: Mobile.IiOSDevice, timeout: number, readyForAttachTimeout: number): IFuture<void> { | ||
return (() => { | ||
let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector); | ||
|
||
try { | ||
this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.appLaunching, timeout).wait(); | ||
process.nextTick(() => { | ||
npc.postNotificationAndAttachForData(this.$iOSNotification.waitForDebug ); | ||
npc.postNotificationAndAttachForData(this.$iOSNotification.attachRequest); | ||
}); | ||
|
||
this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.readyForAttach, readyForAttachTimeout).wait(); | ||
} catch(e) { | ||
this.$logger.trace(`Timeout error: ${e}`); | ||
this.$errors.failWithoutHelp("Timeout waiting for response from NativeScript runtime."); | ||
} | ||
}).future<void>()(); | ||
} | ||
|
||
private executeAttachAvailable(npc: Mobile.INotificationProxyClient, timeout: number): IFuture<void> { | ||
return (() => { | ||
process.nextTick(() => npc.postNotificationAndAttachForData(this.$iOSNotification.attachRequest)); | ||
try { | ||
this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.readyForAttach, timeout).wait(); | ||
} catch (e) { | ||
this.$errors.failWithoutHelp(`The application ${this.$projectData.projectId} timed out when performing the socket handshake.`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error will be shown for every thrown Error, is this expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
} | ||
}).future<void>()(); | ||
} | ||
} | ||
$injector.register("iOSSocketRequestExecutor", IOSSocketRequestExecutor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a need for an interface for this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can safely be just a holder for constants, or even exported variables that can be imported via destructing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I use the interface:
https://github.com/NativeScript/nativescript-cli/pull/1107/files#diff-ec63df112e054b09c6c8a97ec07ecb6aR10,
https://github.com/NativeScript/nativescript-cli/pull/1107/files#diff-3ea8e9be6fce663fc286a4462786a297R31
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I miss the part where we use the
$projectData
.