Skip to content

Added error events #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@ You can easily add a `pass` (stages) into both the pipelines (XXX: ADD API).

In addition, every stage emits a corresponding event so introspection during the process is always available.

#### Setup a basic stand-alone proxy server

var http = require('http'),
caronte = require('caronte');
//
// Create your proxy server
//
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);

//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

#### Setup a stand-alone proxy server with custom server logic

``` js
var http = require('http'),
caronte = require('caronte');

//
// Create a proxy server with custom application logic
//
var proxy = caronte.createProxyServer({});

var server = require('http').createServer(function(req, res) {
proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});

console.log("listening on port 5050")
server.listen(5050);
```

### Contributing and Issues

* Search on Google/Github
Expand All @@ -53,6 +90,17 @@ In addition, every stage emits a corresponding event so introspection during the
* Commit to your local branch (which must be different from `master`)
* Submit your Pull Request (be sure to include tests and update documentation)

### Options

`caronte.createProxyServer` supports the following options:

* **target**: url string to be parsed with the url module
* **forward**: url string to be parsed with the url module
* **ssl**: object to be passed to https.createServer()
* **ws**: true/false, if you want to proxy websockets
* **xfwd**: true/false, adds x-forward headers
* **maxSock**: maximum number of sockets

### Test

```
Expand Down
15 changes: 15 additions & 0 deletions examples/stand-alone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var http = require('http'),
caronte = require('caronte');
//
// Create your proxy server
//
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);

//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
17 changes: 14 additions & 3 deletions lib/caronte/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var caronte = exports,
web = require('./passes/web-incoming');
extend = require('util')._extend,
parse_url = require('url').parse,
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');

caronte.createWebProxy = createRightProxy('web');
Expand Down Expand Up @@ -41,7 +43,11 @@ function createRightProxy(type) {
!(args[cntr] instanceof Buffer) &&
args[cntr] !== res
) {
options = args[cntr];
//Copy global options
options = extend({}, options);
//Overwrite with request options
extend(options, args[cntr]);

cntr--;
}

Expand All @@ -51,7 +57,12 @@ function createRightProxy(type) {

options.ee.emit(ev + 'begin', req, res);


['target', 'forward'].forEach(
function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
});

passes.some(function(pass) {
var evnt = ev + pass.name.toLowerCase() + ':';

Expand Down
8 changes: 8 additions & 0 deletions lib/caronte/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ function stream(req, res, options) {
common.setupOutgoing(options.ssl || {}, options, req)
);

proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:web:';
if (options.ee.listeners(ev + 'error').length == 0){
throw err;
}
options.ee.emit(ev + 'error', err, req, res);
});

req.pipe(proxyReq);

proxyReq.on('response', function(proxyRes) {
Expand Down
7 changes: 7 additions & 0 deletions lib/caronte/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ function stream(req, socket, options, head) {
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
);
proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:ws:';
if (options.ee.listeners(ev + 'error').length == 0){
throw err;
}
options.ee.emit(ev + 'error', err, req, res);
});

proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
common.setupSocket(proxySocket);
Expand Down