-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
137 lines (117 loc) · 3.17 KB
/
server.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var http = require('http');
var spdy = require('spdy');
var argo = require('argo');
var titan = require('titan');
var WebSocketServer = require('ws').Server;
var SpdyAgent = require('./spdy_agent');
var WebSocket = require('./web_socket');
var server = spdy.createServer({
connection: {
windowSize: 1024 * 1024, // Server's window size
autoSpdy31: false
},
spdy: {
plain: true,
ssl: false
}
});
var agent = null;
var pingInterval = null;
var topics = 0;
var cloud = argo()
.use(titan)
.allow({
methods: ['DELETE', 'PUT', 'PATCH', 'POST'],
origins: ['*'],
headers: ['accept', 'content-type'],
maxAge: '432000'
})
.use(function(handle) {
handle('request', function(env, next) {
if (!agent) {
console.log('agent missing')
env.response.statusCode = 502;
return next(env);
}
var req = env.request;
var res = env.response;
var opts = {
method: req.method,
headers: req.headers,
path: req.url,
agent: agent,
pipe: true
};
console.log('req', req.url)
var request = http.request(opts, function(response) {
Object.keys(response.headers).forEach(function(header) {
res.setHeader(header, response.headers[header]);
});
res.statusCode = response.statusCode;
env.response.body = response;
next(env);
}).on('error', function(err) {
env.response.statusCode = 502;
return next(env);
});
request.on('push', function(stream) {
var encoding = stream.headers['x-event-encoding'] || 'json';
var length = Number(stream.headers['content-length']);
console.log(stream.headers);
var data = Buffer.alloc(length);
var idx = 0;
var d = null;
stream.on('readable', function() {
while (d = stream.read()) {
for (var i=0; i<d.length;i++) {
data[idx++] = d[i];
}
};
});
stream.on('error', function(err) {
console.log('stream error:', err)
})
stream.on('end', function() {
var json = JSON.parse(data.toString());
console.log(json);
stream.destroy();
});
})
if (req.body) {
request.end(req.body);
} else {
req.pipe(request);
}
});
});
cloud = cloud.build();
server.on('request', cloud.run);
//spdyServer.on('request', cloud.run);
var wss = new WebSocketServer({ server: server });
wss.on('connection', function(ws) {
ws._socket.removeAllListeners('data'); // Remove WebSocket data handler.
ws.on('error', function(err) {
console.log(err)
})
agent = spdy.createAgent(SpdyAgent, {
port: 80,
socket: ws._socket,
spdy: {
plain: true,
ssl: false,
protocol: 'h2',
}
});
clearInterval(pingInterval);
agent.once('_connect', function() {
pingInterval = setInterval(function() {
agent._spdyState.connection.ping();
}, 500);
});
agent.maxSockets = 150;
agent.on('error', function(err) {
console.error('agent error:', err);
agent.close();
});
});
server.listen(process.argv[2]);