-
-
Notifications
You must be signed in to change notification settings - Fork 9
Fix device log bug #2
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use strict"; | ||
|
||
exports.DataEventName = "data"; | ||
exports.DeviceFoundEventName = "deviceFound"; | ||
exports.DeviceLostEventName = "deviceLost"; | ||
|
||
exports.DeviceEventEnum = { | ||
kDeviceFound: "deviceFound", | ||
kDeviceLost: "deviceLost", | ||
kDeviceTrusted: "deviceTrusted", | ||
kDeviceUnknown: "deviceUnknown" | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"use strict"; | ||
|
||
const EventEmitter = require("events"); | ||
const path = require("path"); | ||
const os = require("os"); | ||
const spawn = require("child_process").spawn; | ||
|
||
const bufferpack = require("bufferpack"); | ||
const Constants = require("./constants"); | ||
|
||
const HeaderSize = 4; | ||
|
||
class IOSDeviceLibStdioHandler extends EventEmitter { | ||
constructor() { | ||
super(); | ||
this._unfinishedMessage = new Buffer(0); | ||
} | ||
|
||
startReadingData() { | ||
this._chProc = spawn(path.join(__dirname, "bin", os.platform(), os.arch(), "ios-device-lib")); | ||
this._chProc.stdout.on("data", (data) => { | ||
this._unpackMessages(data); | ||
}); | ||
} | ||
|
||
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 DeviceEventEnum.kDeviceLost: | ||
this.emit(Constants.DeviceLostEventName, message); | ||
break; | ||
case DeviceEventEnum.kDeviceTrusted: | ||
this.emit(Constants.DeviceLostEventName, message); | ||
this.emit(Constants.DeviceFoundEventName, message); | ||
break; | ||
} | ||
} else { | ||
this.emit(Constants.DataEventName, message); | ||
} | ||
} | ||
|
||
_unpackMessages(data) { | ||
if (!this._unfinishedMessage.length && data.length >= HeaderSize) { | ||
// Get the message length header. | ||
const messageSizeBuffer = data.slice(0, HeaderSize); | ||
const messageLength = bufferpack.unpack(">i", messageSizeBuffer)[0]; | ||
|
||
if (messageLength > data.length - HeaderSize) { | ||
// Less than one message in the buffer. | ||
// Store the unfinished message untill the next call of the function. | ||
this._unfinishedMessage = data; | ||
} else if (data.length - 4 > messageLength) { | ||
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. why |
||
// More than one message in the buffer. | ||
const messageBuffer = this._getMessageFromBuffer(data, messageLength); | ||
|
||
// Get the rest of the message here. | ||
// Since our messages are not separated by whitespace or newlie | ||
// we do not need to add somethig when we slice the original buffer. | ||
const slicedBuffer = data.slice(messageBuffer.length + HeaderSize); | ||
this._distributeMessage(this._parseMessageFromBuffer(messageBuffer)); | ||
this._unpackMessages(slicedBuffer); | ||
} else { | ||
// One message in the buffer. | ||
const messageBuffer = this._getMessageFromBuffer(data, messageLength); | ||
this._distributeMessage(this._parseMessageFromBuffer(messageBuffer)); | ||
} | ||
} else if (this._unfinishedMessage.length && data.length >= HeaderSize) { | ||
// Append the new data to the unfinished message and try to unpack again. | ||
const concatenatedMessage = Buffer.concat([this._unfinishedMessage, data]); | ||
|
||
// Clear the unfinished message buffer. | ||
this._unfinishedMessage = new Buffer(0); | ||
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._unpackMessages(concatenatedMessage); | ||
} else { | ||
// While debugging the data here contains one character - \0, null or 0. | ||
// I think we get here when the Node inner buffer for the data of the data event | ||
// is filled with data and the last symbol is a part of the length header of the next message. | ||
// That's why we need this concat. | ||
// This code is tested with 10 000 000 messages in while loop. | ||
this._unfinishedMessage = Buffer.concat([this._unfinishedMessage, data]); | ||
} | ||
} | ||
|
||
_getMessageFromBuffer(buffer, messageLength) { | ||
return buffer.slice(HeaderSize, messageLength + HeaderSize); | ||
} | ||
|
||
_parseMessageFromBuffer(buffer) { | ||
return JSON.parse(buffer.toString().trim()); | ||
} | ||
} | ||
|
||
exports.IOSDeviceLibStdioHandler = IOSDeviceLibStdioHandler; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "ios-device-lib", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
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. breaking changes should increase the minor version when your version is 0.x.x |
||
"description": "", | ||
"types": "./typings/ios-device-lib.d.ts", | ||
"main": "index.js", | ||
|
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.
If we decide to lock printing after all can we place the mutex on the device log operations only so as to not slow down all the other operations 🤔
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.
I prefer using the mutex whenever we print on the terminal as this way we'll guarantee correct order of messages even if only part of them is sent to stdout's on data event