Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

# Logs
logs
*.log
Expand Down
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- 0.10
- 0.8
before_install:
- npm install npm@v1.4-latest -g
matrix:
fast_finish: true
allow_failures:
- node_js: "0.11"
21 changes: 21 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# create-stream-server is an OPEN Open Source Project

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

## Rules

There are a few basic ground-rules for contributors:

1. **No `--force` pushes** or modifying the Git history in any way.
1. **Non-master branches** ought to be used for ongoing work.
1. **External API changes and significant modifications** ought to be subject to an **internal pull-request** to solicit feedback from other contributors.
1. Internal pull-requests to solicit feedback are *encouraged* for any other non-trivial contribution but left to the discretion of the contributor.
1. Contributors should attempt to adhere to the prevailing code-style.

## Releases

Declaring formal releases remains the prerogative of the project maintainer.

## Changes to this arrangement

This is an experiment and feedback is welcome! This document may also be subject to pull-requests or changes by contributors where you believe you have something valuable to add or change.
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# The MIT License (MIT)

## Copyright (c) 2014-2015 create-stream-server contributors

*mqtt-packet contributors listed at <https://github.com/mqttjs/create-stream-server#contributors>*

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# create-stream-server
# create-stream-server&nbsp;&nbsp;[![Build Status](https://travis-ci.org/mqttjs/create-stream-server.png)](https://travis-ci.org/mqttjs/create-stream-server)

**create multiple stream servers easily**

## Example

```js
var css = require('create-stream-server');

var servers = css({
s1: 'tcp://localhost:8080',
s2: 'ssl://0.0.0.0:80',
s3: {
protocol: 'wss',
host: 'localhost',
port: 8888,
ssl: {
key: fs.readFileSync('./wss_server.key'),
cert: fs.readFileSync('./wss_server.crt')
}
}
}, {
ssl: {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
}
}, function(clientStream, server){
// handle the connected client as a stream
});

// to start
servers.listen(function(){
console.log('launched!');
});

// after some time
servers.close(function(){
console.log('done!');
});

// to release all resources
servers.destroy(function(){
console.log('all gone!');
});
```

## Contributing

create-stream-server is an **OPEN Open Source Project**. This means that:

> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the [CONTRIBUTING.md](https://github.com/mqttjs/create-stream-server/blob/master/CONTRIBUTING.md) file for more details.

### Contributors

create-stream-server is only possible due to the excellent work of the following contributors:

<table><tbody>
<tr><th align="left">Joël Gähwiler</th><td><a href="https://github.com/256dpi">GitHub/256dpi</a></td><td><a href="http://twitter.com/256dpi">Twitter/@256dpi</a></td></tr>
<tr><th align="left">Matteo Collina</th><td><a href="https://github.com/mcollina">GitHub/mcollina</a></td><td><a href="http://twitter.com/matteocollina">Twitter/@matteocollina</a></td></tr>
</tbody></table>

### License

MIT
110 changes: 110 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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));
});
}

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.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];
server.listen(server._css_port, server._css_host, function(){
enableDestroy(server);
cb();
});
}, callback || function(){});
},
close: function(callback){
async.mapSeries(Object.keys(servers), function(id, cb){
servers[id].close(cb);
}, callback || function(){});
},
destroy: function(callback){
async.mapSeries(Object.keys(servers), function(id, cb){
servers[id].destroy(cb);
}, callback || function(){});
}
};
};
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "create-stream-server",
"version": "0.0.1",
"description": "create multiple stream servers easily",
"main": "index.js",
"contributors": [
"Joël Gähwiler <joel.gaehwiler@gmail.com> (https://github.com/256dpi)",
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)"
],
"scripts": {
"test": "./node_modules/.bin/mocha --reporter list *.js"
},
"repository": {
"type": "git",
"url": "https://github.com/mqttjs/create-stream-server.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/mqttjs/create-stream-server/issues"
},
"homepage": "https://github.com/mqttjs/create-stream-server",
"dependencies": {
"async": "0.9.0",
"server-destroy": "1.0.0",
"websocket-stream": "1.3.2",
"ws": "0.7.0"
},
"devDependencies": {
"mocha": "2.1.0"
}
}
19 changes: 19 additions & 0 deletions support/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDLDCCAhQCCQD0OFi+lx3bpjANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJD
SDEPMA0GA1UECBMGWnVyaWNoMQ8wDQYDVQQHEwZadXJpY2gxEjAQBgNVBAoTCXNo
aWZ0ci5pbzETMBEGA1UEAxMKbWVrb25nLmRldjAeFw0xNDEyMjgxMTMzNTdaFw0x
NTEyMjgxMTMzNTdaMFgxCzAJBgNVBAYTAkNIMQ8wDQYDVQQIEwZadXJpY2gxDzAN
BgNVBAcTBlp1cmljaDESMBAGA1UEChMJc2hpZnRyLmlvMRMwEQYDVQQDEwptZWtv
bmcuZGV2MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyqZScnDkk3lc
h9MG00RlpD+U0jvGDGGykHi19J0bUZY+GmwCXHEdHqE2GuJDWEQcaB5RVBU5UOBd
KqBcQ5AhU8HWoL3txO9+Z+E7JzchDHPhabBtwTNDD5TKcL9XcrYd3Al7FpGARsDd
H8U3AC4i6srtUZb37lILQeD628qw5gL2OWL4noOr9Eszek63zURZhQQZ4aZqTi3W
2EU4brYSEy1ZW0WhkSYGm8gpeCC8f8gmufPFyalMwVPXLjNVJnD9EdWVyhmtiEYG
Q5iJweFaiQwIuFvYC5jxeVAji86UFV/pE5qZ/ffZHgRMBwdoNh8TZiFMV/psRv6C
6JuKBeBauwIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQCgQTneRLx35T1XcEbKvqZq
DGZnB+cVH7D5VGUGwOlWhVrxxRfurR3Yrcb4mUCqu6EjUrWXhauczsAdoXqrrzRY
YyR9CbarAm6f80xj+aSINBm/lLZJ/GJd7XhrOUTbM3VIIHUGF3MbAP7a16t2aYsf
IDa2U1GXllJfMBWyFbn53AH6Lf85fLbcmg47K1ffx+4Baw+IzIsVGuvzHG73TCA9
R1/ygrIDABAXw/919zOe2vsF+BKIWZiDIeTPVxzM4M8gTuoky2ecHBBjVyAS2yXS
73mi7srYyxs+7iCmixn2csZUpSnrYmJE61CDPgMtmQSt3cXnyldq6gDzefiCnZdR
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions support/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEoAIBAAKCAQEAyqZScnDkk3lch9MG00RlpD+U0jvGDGGykHi19J0bUZY+GmwC
XHEdHqE2GuJDWEQcaB5RVBU5UOBdKqBcQ5AhU8HWoL3txO9+Z+E7JzchDHPhabBt
wTNDD5TKcL9XcrYd3Al7FpGARsDdH8U3AC4i6srtUZb37lILQeD628qw5gL2OWL4
noOr9Eszek63zURZhQQZ4aZqTi3W2EU4brYSEy1ZW0Wh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