-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathios-device-lib-stdio-handler.js
72 lines (61 loc) · 2.06 KB
/
ios-device-lib-stdio-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
const EventEmitter = require("events");
const path = require("path");
const os = require("os");
const spawn = require("child_process").spawn;
const Constants = require("./constants");
const MessageUnpackStream = require("./message-unpack-stream").MessageUnpackStream;
class IOSDeviceLibStdioHandler extends EventEmitter {
constructor(options) {
super();
this._showDebugInformation = options.debug;
}
startReadingData() {
this._chProc = spawn(path.join(__dirname, "bin", os.platform(), os.arch(), "ios-device-lib").replace("app.asar", "app.asar.unpacked"));
const stdinMessageUnpackStream = new MessageUnpackStream();
this._chProc.stdout.pipe(stdinMessageUnpackStream);
stdinMessageUnpackStream.on("data", (data) => {
this._distributeMessage(this._parseMessageFromBuffer(data));
});
if (this._showDebugInformation) {
const stderrMessageUnpackStream = new MessageUnpackStream();
this._chProc.stderr.pipe(stderrMessageUnpackStream);
stderrMessageUnpackStream.on("data", (data) => {
console.log("TRACE: ", data && data.toString());
});
}
}
writeData(data) {
this._chProc.stdin.write(data);
}
dispose(signal) {
this.removeAllListeners();
this._chProc.removeAllListeners();
this._chProc.kill(signal);
}
_distributeMessage(message) {
if (message.event) {
switch (message.event) {
case Constants.DeviceEventEnum.kDeviceFound:
this.emit(Constants.DeviceFoundEventName, message);
break;
case Constants.DeviceEventEnum.kDeviceUpdated:
this.emit(Constants.DeviceUpdatedEventName, message);
break;
case Constants.DeviceEventEnum.kDeviceLost:
this.emit(Constants.DeviceLostEventName, message);
break;
case Constants.DeviceEventEnum.kDeviceTrusted:
this.emit(Constants.DeviceLostEventName, message);
this.emit(Constants.DeviceFoundEventName, message);
break;
}
} else {
this.emit(Constants.DataEventName, message);
}
}
_parseMessageFromBuffer(buffer) {
return JSON.parse(buffer.toString().trim());
}
}
exports.IOSDeviceLibStdioHandler = IOSDeviceLibStdioHandler;