mirror of
https://github.com/openstf/stf
synced 2025-10-05 10:39:25 +02:00
Add a varint-delimiting stream as well.
This commit is contained in:
parent
1c611232f5
commit
514fc3554d
2 changed files with 113 additions and 44 deletions
|
@ -1,7 +1,7 @@
|
|||
var util = require('util')
|
||||
var stream = require('stream')
|
||||
|
||||
function MessageStream() {
|
||||
function DelimitedStream() {
|
||||
stream.Transform.call(this)
|
||||
this._length = 0
|
||||
this._lengthIndex = 0
|
||||
|
@ -9,9 +9,9 @@ function MessageStream() {
|
|||
this._buffer = new Buffer(0)
|
||||
}
|
||||
|
||||
util.inherits(MessageStream, stream.Transform)
|
||||
util.inherits(DelimitedStream, stream.Transform)
|
||||
|
||||
MessageStream.prototype._transform = function(chunk, encoding, done) {
|
||||
DelimitedStream.prototype._transform = function(chunk, encoding, done) {
|
||||
this._buffer = Buffer.concat([this._buffer, chunk])
|
||||
|
||||
var lo = 0
|
||||
|
@ -49,4 +49,34 @@ MessageStream.prototype._transform = function(chunk, encoding, done) {
|
|||
done()
|
||||
}
|
||||
|
||||
module.exports = MessageStream
|
||||
module.exports.DelimitedStream = DelimitedStream
|
||||
|
||||
function DelimitingStream() {
|
||||
stream.Transform.call(this)
|
||||
}
|
||||
|
||||
util.inherits(DelimitingStream, stream.Transform)
|
||||
|
||||
DelimitingStream.prototype._transform = function(chunk, encoding, done) {
|
||||
var length = chunk.length
|
||||
var lengthBytes = []
|
||||
var more = true
|
||||
|
||||
while (more) {
|
||||
if (length > 0x7f) {
|
||||
lengthBytes.push((1 << 7) + (length & 0x7f))
|
||||
length >>= 7
|
||||
}
|
||||
else {
|
||||
lengthBytes.push(length)
|
||||
more = false
|
||||
}
|
||||
}
|
||||
|
||||
this.push(new Buffer(lengthBytes))
|
||||
this.push(chunk)
|
||||
|
||||
done()
|
||||
}
|
||||
|
||||
module.exports.DelimitingStream = DelimitingStream
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue