Description
This is a low priority enhancement. But I do believe this is important in general to set a standard. That being said, the http server spans about... 8 different files with a collective 2661 lines (120+145+498+624+198+228+572+276) (some of which is repeated such as http and https). So I completely understand that this idea is going to be taken lightly if not ignored completely. However, seeing from the last TC meeting, you guys really are paying attention. And it warms my icy cold heart.
The problem
Nodejs already set the standard for using stream.Transform in servers, don't.
It can only be found in Crypto and zlib within the source
However, the nodejs implementers guide suggests using the transform for protocols. I'm not here to complain about hipocracy as much as I'm here to complain about how because node didn't lead by example others didn't follow and understand why it wasn't implemented that way.
Consequences
- tutorials don't teach using transforms
- The WebSocket Server almost all other WebSocket based servers require does not implement it
- A Proof of concept Stun Server also does not implement it
- A common multipart data parser doesn't use a transform
- Inability to pass these types of socket wrappers and others between processes
- people not seeing Transforms useful
Other solutions
- List all transforms that would be nice to have. This will give a hobby for anyone interested in it especially since almost every protocol is specified by w3 (I'm actually slightly interested after my recent work) (Also the most realistic)
- Specify that transformations for protocols aren't necessary and hardly ever used including within yours/nodes own framework so people don't believe whats on nodejs.org (That is kindof a joke)
- Giving the ability to send circular logic through child_process (That is kind of a joke)
Psuedocode
function onConnection(socket){
var res = new httpResponder();
var req = new httpRequestor();
res.pipe(socketconnection).pipe(req);
dostuff(req,res);
}
function dostuff(req,res){
req.on("error", res.send.bind(res,500));
req.on("head", function(head){
authenticateHead(req,res,function(err,bool){
if(err){
req.close();
res.send(500,err);
});
});
if(req.method == get){
this.dogetstuff(req).pipe(res);
}
});
req.on("body", function(body){
body.pipe(parser).pipe(this.dobodystuff(req)).pipe(res)
});
req.on("part", function(part){
part.pipe(handlePart).pipe(this.dopartstuff(req)).pipe(res)
});
}
function sendSocket(req,res,socket,child){
socket.pause();
child.send({req:req.export(),res:res.export()}, socket);
}
//Exports closes the stream, sends buffer, state and data the transform wants to send
function implementSocket(data,socket,next){
res = httpResponder.import(data.res);
req = httpRequestor.import(data.req,);
res.pipe(socket).pipe(req);
socket.resume();
next(req,res,socket);
}
//imports picks up on the state where it was left off, sets the buffer and sets the current data
Some related Lines
- https://github.com/iojs/io.js/blob/v0.12/lib/_http_server.js#L241 - Where its obvious node creates a Server
- https://github.com/iojs/io.js/blob/v0.12/lib/child_process.js#L427 - Where its checking the type of handle to send between processes