-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
119 lines (103 loc) · 3.2 KB
/
index.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
var net = require('net');
var tls = require('tls');
var http = require('http');
var https = require('https');
var ws = require('ws');
var url = require('url');
var wsStream = require('websocket-stream');
var async = require('async');
var enableDestroy = require('server-destroy');
function createServer(clientHandler) {
return net.createServer(function(client){
clientHandler(client);
});
}
function createSecureServer(sslOptions, clientHandler){
return tls.createServer(sslOptions, function(client){
clientHandler(client);
});
}
function attachServer(server, clientHandler) {
(new ws.Server({
server: server
})).on('connection', function(ws) {
clientHandler(wsStream(ws));
});
return server;
}
function createWebSocketServer(clientHandler){
var server = http.createServer();
attachServer(server, clientHandler);
return server;
}
function createSecureWebSocketServer(sslOptions, clientHandler){
var server = https.createServer(sslOptions);
attachServer(server, clientHandler);
return server;
}
module.exports = function(serverConfig, sharedConfig, clientStreamHandler){
if(typeof sharedConfig == 'function') {
clientStreamHandler = sharedConfig;
sharedConfig = {};
}
if(typeof sharedConfig != 'object') {
sharedConfig = {};
}
var servers = {};
Object.keys(serverConfig).forEach(function(id) {
var config = serverConfig[id];
if(typeof config == 'string') {
var c = url.parse(config);
config = {
protocol: c.protocol.replace(/:$/, ''),
port: c.port,
host: c.hostname
};
}
config.host = config.host || sharedConfig.host || 'localhost';
config.ssl = config.ssl || sharedConfig.ssl;
var server;
if(config.attach) {
server = attachServer(config.attach, clientStreamHandler);
server._css_exclude = true;
} else if(config.protocol == 'tcp') {
server = createServer(clientStreamHandler);
} else if(config.protocol == 'ssl') {
server = createSecureServer(config.ssl, clientStreamHandler);
} else if(config.protocol == 'ws') {
server = createWebSocketServer(clientStreamHandler);
} else if(config.protocol == 'wss') {
server = createSecureWebSocketServer(config.ssl, clientStreamHandler);
}
server._css_host = config.host;
server._css_port = config.port;
servers[id] = server;
});
return {
servers: servers,
listen: function(callback){
async.mapSeries(Object.keys(servers), function(id, cb){
var server = servers[id];
if(server._css_exclude) return cb();
server.listen(server._css_port, server._css_host, function(){
enableDestroy(server);
return cb();
});
}, callback || function(){});
},
close: function(callback){
async.mapSeries(Object.keys(servers), function(id, cb){
var server = servers[id];
if(server._css_exclude) return cb();
server.close(cb);
}, callback || function(){});
},
destroy: function(callback){
async.mapSeries(Object.keys(servers), function(id, cb){
var server = servers[id];
if(server._css_exclude) return cb();
server.destroy(cb);
}, callback || function(){});
}
};
};