|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const EventEmitter = require("events"); |
| 4 | +const path = require("path"); |
| 5 | +const os = require("os"); |
| 6 | +const spawn = require("child_process").spawn; |
| 7 | + |
| 8 | +const bufferpack = require("bufferpack"); |
| 9 | +const Constants = require("./constants"); |
| 10 | + |
| 11 | +const HeaderSize = 4; |
| 12 | + |
| 13 | +class IOSDeviceLibStdioHandler extends EventEmitter { |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + this._unfinishedMessage = Buffer.alloc(0); |
| 17 | + } |
| 18 | + |
| 19 | + startReadingData() { |
| 20 | + this._chProc = spawn(path.join(__dirname, "bin", os.platform(), os.arch(), "ios-device-lib")); |
| 21 | + this._chProc.stdout.on("data", (data) => { |
| 22 | + this._unpackMessages(data); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + writeData(data) { |
| 27 | + this._chProc.stdin.write(data); |
| 28 | + } |
| 29 | + |
| 30 | + dispose(signal) { |
| 31 | + this.removeAllListeners(); |
| 32 | + this._chProc.removeAllListeners(); |
| 33 | + this._chProc.kill(signal); |
| 34 | + } |
| 35 | + |
| 36 | + _distributeMessage(message) { |
| 37 | + if (message.event) { |
| 38 | + switch (message.event) { |
| 39 | + case Constants.DeviceEventEnum.kDeviceFound: |
| 40 | + this.emit(Constants.DeviceFoundEventName, message); |
| 41 | + break; |
| 42 | + case DeviceEventEnum.kDeviceLost: |
| 43 | + this.emit(Constants.DeviceLostEventName, message); |
| 44 | + break; |
| 45 | + case DeviceEventEnum.kDeviceTrusted: |
| 46 | + this.emit(Constants.DeviceLostEventName, message); |
| 47 | + this.emit(Constants.DeviceFoundEventName, message); |
| 48 | + break; |
| 49 | + } |
| 50 | + } else { |
| 51 | + this.emit(Constants.DataEventName, message); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + _unpackMessages(data) { |
| 56 | + if (!this._unfinishedMessage.length && data.length >= HeaderSize) { |
| 57 | + // Get the message length header. |
| 58 | + const messageSizeBuffer = data.slice(0, HeaderSize); |
| 59 | + const messageLength = bufferpack.unpack(">i", messageSizeBuffer)[0]; |
| 60 | + const dataLengthWithoutHeader = data.length - HeaderSize; |
| 61 | + |
| 62 | + if (messageLength > dataLengthWithoutHeader) { |
| 63 | + // Less than one message in the buffer. |
| 64 | + // Store the unfinished message untill the next call of the function. |
| 65 | + this._unfinishedMessage = data; |
| 66 | + } else if (dataLengthWithoutHeader > messageLength) { |
| 67 | + // More than one message in the buffer. |
| 68 | + const messageBuffer = this._getMessageFromBuffer(data, messageLength); |
| 69 | + |
| 70 | + // Get the rest of the message here. |
| 71 | + // Since our messages are not separated by whitespace or newlie |
| 72 | + // we do not need to add somethig when we slice the original buffer. |
| 73 | + const slicedBuffer = data.slice(messageBuffer.length + HeaderSize); |
| 74 | + this._distributeMessage(this._parseMessageFromBuffer(messageBuffer)); |
| 75 | + this._unpackMessages(slicedBuffer); |
| 76 | + } else { |
| 77 | + // One message in the buffer. |
| 78 | + const messageBuffer = this._getMessageFromBuffer(data, messageLength); |
| 79 | + this._distributeMessage(this._parseMessageFromBuffer(messageBuffer)); |
| 80 | + } |
| 81 | + } else if (this._unfinishedMessage.length && data.length >= HeaderSize) { |
| 82 | + // Append the new data to the unfinished message and try to unpack again. |
| 83 | + const concatenatedMessage = Buffer.concat([this._unfinishedMessage, data]); |
| 84 | + |
| 85 | + // Clear the unfinished message buffer. |
| 86 | + this._unfinishedMessage = Buffer.alloc(0); |
| 87 | + this._unpackMessages(concatenatedMessage); |
| 88 | + } else { |
| 89 | + // While debugging the data here contains one character - \0, null or 0. |
| 90 | + // I think we get here when the Node inner buffer for the data of the data event |
| 91 | + // is filled with data and the last symbol is a part of the length header of the next message. |
| 92 | + // That's why we need this concat. |
| 93 | + // This code is tested with 10 000 000 messages in while loop. |
| 94 | + this._unfinishedMessage = Buffer.concat([this._unfinishedMessage, data]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + _getMessageFromBuffer(buffer, messageLength) { |
| 99 | + return buffer.slice(HeaderSize, messageLength + HeaderSize); |
| 100 | + } |
| 101 | + |
| 102 | + _parseMessageFromBuffer(buffer) { |
| 103 | + return JSON.parse(buffer.toString().trim()); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +exports.IOSDeviceLibStdioHandler = IOSDeviceLibStdioHandler; |
0 commit comments