Files
stf/lib/units/device/plugins/forward/util/writer.js
Denis Barbaron 878171ec24 fix deprecated warnings on Buffer class (#580)
Signed-off-by: Denis barbaron <denis.barbaron@orange.com>

Signed-off-by: Denis barbaron <denis.barbaron@orange.com>
2022-09-16 16:07:49 +02:00

46 lines
905 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(fullChunk, encoding, done) {
var chunk = fullChunk
var header, length
do {
length = Math.min(MAX_PACKET_SIZE, chunk.length)
header = Buffer.alloc(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 = Buffer.alloc(HEADER_SIZE)
header.writeUInt16LE(this._target, 0)
header.writeUInt16LE(0, 2)
this.push(header)
done()
}
module.exports = ForwardWriter