A stream with a header! Yay!
headed-stream lets you put a length-prefixed header on a stream, for whatever weird purposes you have. I wanted to send a little bit of metadata along with a connection from a proxy; I don't know what you want to do. I'm not here to judge.
[X(4)][Y(X)][Z...]
X: Header length (4 bytes)
Y: Header data (X bytes)
Z: Content (any following data)
var HeadedStream = require("headed-stream"),
net = require("net");
net.createServer(function(socket) {
var hs = new HeadedStream();
hs.on("header", function(header) {
console.log(header);
});
// remaining data will be piped back to the socket, making this an echo server
socket.pipe(hs).pipe(socket);
}).listen(3000);
var s = new HeadedStream();
The HeadedStream
object is a duplex stream, so it implements all the regular
streamy things you'd expect. The only addition is the "header" event that it
emits once it's collected your headers for you.
s.on("header", function(header) {
console.log(header);
});
3-clause BSD. A copy is included with the source.
- GitHub (deoxxa)
- Twitter (@deoxxa)
- Email (deoxxa@fknsrs.biz)