1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 10:19:30 +02:00
OpenSTF/lib/wire/messagestream.js
2014-04-01 11:50:16 +09:00

52 lines
1.2 KiB
JavaScript

var util = require('util')
var stream = require('stream')
function MessageStream() {
stream.Transform.call(this)
this._length = 0
this._lengthIndex = 0
this._readingLength = true
this._buffer = new Buffer(0)
}
util.inherits(MessageStream, stream.Transform)
MessageStream.prototype._transform = function(chunk, encoding, done) {
this._buffer = Buffer.concat([this._buffer, chunk])
var lo = 0
var hi = this._buffer.length
while (lo < hi) {
if (this._readingLength) {
while (lo < hi) {
var byte = this._buffer[lo++]
if (byte & (1 << 7)) {
this._length += (byte & 0x7f) << (7 * this._lengthIndex)
this._lengthIndex += 1
this._readingLength = true
}
else {
this._length += (byte & 0x7f) << (7 * this._lengthIndex)
this._lengthIndex = 0
this._readingLength = false
break
}
}
}
if (!this._readingLength && lo + this._length <= hi) {
this.push(chunk.slice(lo, lo + this._length))
lo += this._length
this._length = 0
this._readingLength = true
}
else {
break
}
}
done()
}
module.exports = MessageStream