-
Notifications
You must be signed in to change notification settings - Fork 16
/
ws-jsgi.js
36 lines (34 loc) · 936 Bytes
/
ws-jsgi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var when = require("./promise").when,
NodeRequest = require("./jsgi-node").Request;
module.exports = function(socketServer, jsgiApp){
socketServer.on("connection", function(socket){
var req = socket.upgradeReq;
req.setTimeout(0);
function Request(){}
Request.prototype = new NodeRequest(req);
function Headers(){}
Headers.prototype = Request.prototype.headers;
socket.on("message", function(data){
var request = new Request();
request.body = [data];
request.headers = new Headers();
when(jsgiApp(request), function(response){
when(response.body, function(body){
var chunks = [],
done = false;
when(body.forEach(function (chunk) {
chunks.push(chunk);
}), function () {
done = true;
});
socket.stream(function (err, send) {
if (!err && chunks.length) {
send(chunks.join(''), done);
chunks = [];
}
});
});
});
});
});
};