mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
45 lines
878 B
JavaScript
45 lines
878 B
JavaScript
var util = require('util')
|
|
var stream = require('stream')
|
|
|
|
var HEADER_SIZE = 4
|
|
var MAX_PACKET_SIZE = 0xFFFF
|
|
|
|
function ForwardWriter(target) {
|
|
stream.Transform.call(this)
|
|
this._target = target
|
|
}
|
|
|
|
util.inherits(ForwardWriter, stream.Transform)
|
|
|
|
ForwardWriter.prototype._transform = function(chunk, encoding, done) {
|
|
var header
|
|
, length
|
|
|
|
do {
|
|
length = Math.min(MAX_PACKET_SIZE, chunk.length)
|
|
|
|
header = new Buffer(HEADER_SIZE)
|
|
header.writeUInt16LE(this._target, 0)
|
|
header.writeUInt16LE(length, 2)
|
|
|
|
this.push(header)
|
|
this.push(chunk.slice(0, length))
|
|
|
|
chunk = chunk.slice(length)
|
|
}
|
|
while (chunk.length)
|
|
|
|
done()
|
|
}
|
|
|
|
ForwardWriter.prototype._flush = function(done) {
|
|
var header = new Buffer(HEADER_SIZE)
|
|
header.writeUInt16LE(this._target, 0)
|
|
header.writeUInt16LE(0, 2)
|
|
|
|
this.push(header)
|
|
|
|
done()
|
|
}
|
|
|
|
module.exports = ForwardWriter
|