mirror of
https://github.com/openstf/stf
synced 2025-10-05 10:39:25 +02:00
Don't read new frames before the previous frame has been sent.
This commit is contained in:
parent
07fb3b530c
commit
feb77c4396
3 changed files with 97 additions and 48 deletions
|
@ -1,56 +1,66 @@
|
|||
var stream = require('stream')
|
||||
var util = require('util')
|
||||
|
||||
function FrameParser() {
|
||||
this.readFrameBytes = 0
|
||||
this.frameBodyLength = 0
|
||||
this.frameBody = new Buffer(0)
|
||||
stream.Transform.call(this)
|
||||
this.frameBody = null
|
||||
this.cursor = 0
|
||||
this.chunk = null
|
||||
}
|
||||
|
||||
util.inherits(FrameParser, stream.Transform)
|
||||
FrameParser.prototype.push = function(chunk) {
|
||||
if (this.chunk) {
|
||||
throw new Error('Must consume pending frames before pushing more chunks')
|
||||
}
|
||||
|
||||
FrameParser.prototype._transform = function(chunk, encoding, done) {
|
||||
var cursor, len, bytesLeft
|
||||
this.chunk = chunk
|
||||
}
|
||||
|
||||
for (cursor = 0, len = chunk.length; cursor < len;) {
|
||||
FrameParser.prototype.nextFrame = function() {
|
||||
if (!this.chunk) {
|
||||
return null
|
||||
}
|
||||
|
||||
for (var len = this.chunk.length; this.cursor < len;) {
|
||||
if (this.readFrameBytes < 4) {
|
||||
this.frameBodyLength +=
|
||||
(chunk[cursor] << (this.readFrameBytes * 8)) >>> 0
|
||||
cursor += 1
|
||||
(this.chunk[this.cursor] << (this.readFrameBytes * 8)) >>> 0
|
||||
this.cursor += 1
|
||||
this.readFrameBytes += 1
|
||||
}
|
||||
else {
|
||||
bytesLeft = len - cursor
|
||||
var bytesLeft = len - this.cursor
|
||||
|
||||
if (bytesLeft >= this.frameBodyLength) {
|
||||
this.frameBody = Buffer.concat([
|
||||
this.frameBody
|
||||
, chunk.slice(cursor, cursor + this.frameBodyLength)
|
||||
])
|
||||
var completeBody = this.frameBody
|
||||
? Buffer.concat([
|
||||
this.frameBody
|
||||
, this.chunk.slice(this.cursor, this.cursor + this.frameBodyLength)
|
||||
])
|
||||
: this.chunk.slice(this.cursor, this.cursor + this.frameBodyLength)
|
||||
|
||||
this.push(this.frameBody)
|
||||
|
||||
cursor += this.frameBodyLength
|
||||
this.cursor += this.frameBodyLength
|
||||
this.frameBodyLength = this.readFrameBytes = 0
|
||||
this.frameBody = new Buffer(0)
|
||||
this.frameBody = null
|
||||
|
||||
return completeBody
|
||||
}
|
||||
else {
|
||||
// @todo Consider/benchmark continuation frames to prevent
|
||||
// potential Buffer thrashing.
|
||||
this.frameBody = Buffer.concat([
|
||||
this.frameBody
|
||||
, chunk.slice(cursor, len)
|
||||
])
|
||||
this.frameBody = this.frameBody
|
||||
? Buffer.concat([this.frameBody, this.chunk.slice(this.cursor, len)])
|
||||
: this.chunk.slice(this.cursor, len)
|
||||
|
||||
this.frameBodyLength -= bytesLeft
|
||||
this.readFrameBytes += bytesLeft
|
||||
cursor = len
|
||||
this.cursor = len
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return done()
|
||||
this.cursor = 0
|
||||
this.chunk = null
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
module.exports = FrameParser
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue