Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions protocol.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@

module.exports = (handleMessage) => {

process.stdin.on('readable', () => {
var input = []
var chunk
while (chunk = process.stdin.read()) {
input.push(chunk)
}
input = Buffer.concat(input)

var msgLen = input.readUInt32LE(0)
var dataLen = msgLen + 4

if (input.length >= dataLen) {
var content = input.slice(4, dataLen)
var json = JSON.parse(content.toString())
handleMessage(json)
}
})
var msgLen = 0,
dataLen = 0,
input = []

function sendMessage (msg) {
function sendMessage(msg) {
var buffer = Buffer.from(JSON.stringify(msg))

var header = Buffer.alloc(4)
Expand All @@ -29,10 +14,34 @@ module.exports = (handleMessage) => {
process.stdout.write(data)
}

process.stdin.on('readable', () => {
var chunk
while (chunk = process.stdin.read()) {
// Set message value length once
if (msgLen === 0 && dataLen === 0) {
msgLen = chunk.readUInt32LE(0)
chunk = chunk.subarray(4)
}
// Store accrued message length read
dataLen += chunk.length
input.push(chunk)
if (dataLen === msgLen) {
// Send accrued message from client back to client
handleMessage(JSON.parse(Buffer.concat(input).toString()))
// Reset dynamic variables after sending accrued read message to client
msgLen = 0,
dataLen = 0,
input = []
}
}
})

process.on('uncaughtException', (err) => {
sendMessage({error: err.toString()})
sendMessage({
error: err.toString()
})
})

return sendMessage

}