Comparing version 0.1.6 to 0.1.7
@@ -40,6 +40,9 @@ var http = require('http') | ||
.on('error', function (err, req, res) { | ||
console.log('Proxy error:', err) | ||
}) | ||
.on('route:error', function (err, req, res) { | ||
console.log('Route error:', err) | ||
}) | ||
.on('replay:start', function (params, opts, req) { | ||
console.log('Replay request:', params, '=>', opts.target) | ||
console.log('Replay request:', params, '=>', opts.target, '=> URL:', req.url) | ||
}) | ||
@@ -46,0 +49,0 @@ .on('replay:error', function (err, req) { |
@@ -12,2 +12,4 @@ var http = require('http') | ||
proxy.get('/headers') | ||
.replay('http://localhost:3001') | ||
proxy.get('/status/:code') | ||
@@ -32,3 +34,3 @@ .replay('http://localhost:3002') | ||
// Replay server | ||
// Replay servers | ||
express() | ||
@@ -38,2 +40,8 @@ .use(function (req, res) { | ||
}) | ||
.listen(3001) | ||
express() | ||
.use(function (req, res) { | ||
res.sendStatus(503) | ||
}) | ||
.listen(3002) | ||
@@ -40,0 +48,0 @@ |
@@ -1,5 +0,5 @@ | ||
var _ = require('lodash') | ||
var rocky = require('..') | ||
const _ = require('lodash') | ||
const rocky = require('..') | ||
var PORT = 3000 | ||
const PORT = 3000 | ||
@@ -14,3 +14,3 @@ module.exports = function (config) { | ||
function createProxy(config) { | ||
var proxy = rocky(config) | ||
const proxy = rocky(config) | ||
@@ -34,3 +34,3 @@ if (config.forward) { | ||
function addRoutes(proxy, config) { | ||
var reservedKeys = ['forward', 'replay', 'target'] | ||
const reservedKeys = ['forward', 'replay', 'target'] | ||
@@ -37,0 +37,0 @@ var routes = Object.keys(config) |
@@ -1,2 +0,2 @@ | ||
var _ = require('lodash') | ||
const clone = require('lodash').clone | ||
@@ -11,3 +11,3 @@ module.exports = function routeHandler(rocky, route) { | ||
return function handler(req, res) { | ||
var opts = _.clone(rocky.opts) | ||
var opts = clone(rocky.opts) | ||
opts.replays = rocky.replays | ||
@@ -14,0 +14,0 @@ |
module.exports = function transformRequestBody(middleware) { | ||
return function (req, res, next) { | ||
if (req.method === 'GET') return next() | ||
if (req.method === 'GET' || req.method === 'HEAD') return next() | ||
@@ -5,0 +5,0 @@ // Pause the stream flow |
module.exports = function toPath(path, args) { | ||
return function (req, res, next) { | ||
var params = args || req.params || {} | ||
const params = args || req.params || {} | ||
@@ -5,0 +5,0 @@ Object.keys(params).forEach(function (param) { |
@@ -1,5 +0,5 @@ | ||
var http = require('http') | ||
var https = require('https') | ||
var parseUrl = require('url').parse | ||
var reqParams = require('http-proxy/lib/http-proxy/common').setupOutgoing | ||
const http = require('http') | ||
const https = require('https') | ||
const parseUrl = require('url').parse | ||
const reqParams = require('http-proxy/lib/http-proxy/common').setupOutgoing | ||
@@ -6,0 +6,0 @@ module.exports = function replay(req, route) { |
@@ -1,7 +0,7 @@ | ||
var router = require('router') | ||
var Route = require('./route') | ||
var createServer = require('./server') | ||
var routeHandler = require('./handler') | ||
var assign = require('lodash').assign | ||
var Emitter = require('events').EventEmitter | ||
const router = require('router') | ||
const Route = require('./route') | ||
const createServer = require('./server') | ||
const routeHandler = require('./handler') | ||
const assign = require('lodash').assign | ||
const Emitter = require('events').EventEmitter | ||
@@ -27,7 +27,3 @@ module.exports = Rocky | ||
Rocky.prototype.replay = function (url, opts) { | ||
if (typeof url === 'string') { | ||
url = { target: url } | ||
} | ||
this.replays.push(assign({}, url, opts)) | ||
return this | ||
return Route.prototype.replay.call(this, url, opts) | ||
} | ||
@@ -34,0 +30,0 @@ |
@@ -100,5 +100,10 @@ var http = require('http') | ||
if (err) { | ||
return route.proxy.emit('error', err, req, res) | ||
return route.proxy.emit('route:error', err, req, res) | ||
} | ||
// Ensure that we can forward/replay the request | ||
if (!opts.target && !opts.balance && !route.replays.length) { | ||
return cannotForwardError(route, req, res) | ||
} | ||
// Forward the request | ||
@@ -141,1 +146,8 @@ proxyRequest(req, res, route, opts) | ||
} | ||
function cannotForwardError(route, req, res) { | ||
var error = { 'message': 'Target URL was not defined for this route' } | ||
route.proxy.emit('route:error', null, req, res) | ||
res.writeHead(404, { 'Content-Type': 'application/json' }) | ||
return res.end(JSON.stringify(error)) | ||
} |
@@ -1,7 +0,7 @@ | ||
var http = require('http') | ||
var https = require('https') | ||
var version = require('../package.json').version | ||
const http = require('http') | ||
const https = require('https') | ||
const version = require('../package.json').version | ||
module.exports = function createServer(opts, router) { | ||
var handler = serverHandler(router) | ||
const handler = serverHandler(router) | ||
@@ -8,0 +8,0 @@ var server = opts.ssl |
{ | ||
"name": "rocky", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Pluggable and middleware-oriented full featured HTTP/S proxy router", | ||
@@ -30,3 +30,4 @@ "repository": "h2non/rocky", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha --timeout 2000 --reporter spec --ui tdd" | ||
"test": "./node_modules/.bin/mocha --timeout 2000 --reporter spec --ui tdd", | ||
"start": "./bin/rocky" | ||
}, | ||
@@ -33,0 +34,0 @@ "dependencies": { |
@@ -88,2 +88,19 @@ # rocky [![Build Status](https://api.travis-ci.org/h2non/rocky.svg?branch=master&style=flat)](https://travis-ci.org/h2non/rocky) [![Code Climate](https://codeclimate.com/github/h2non/rocky/badges/gpa.svg)](https://codeclimate.com/github/h2non/rocky) [![NPM](https://img.shields.io/npm/v/rocky.svg)](https://www.npmjs.org/package/rocky) ![Downloads](https://img.shields.io/npm/dm/rocky.svg) | ||
### Standalone binaries | ||
Powered by [nar](https://github.com/h2non/nar) | ||
- [linux-x64](https://github.com/h2non/rocky/releases/download/0.1.6/rocky-0.1.6-linux-x64.nar) | ||
- [darwin-x64](https://github.com/h2non/rocky/releases/download/0.1.6/rocky-0.1.6-darwin-x64.nar) | ||
##### Usage | ||
``` | ||
chmod +x rocky-0.1.6-linux-x64.nar | ||
``` | ||
``` | ||
./rocky-0.1.6-linux-x64.nar exec --port 3000 --config rocky.toml | ||
``` | ||
## Third-party middlewares | ||
@@ -143,4 +160,4 @@ | ||
- **target** `string` - <url string to be parsed with the url module | ||
- **replay** `array<string>` - Optional replay server URLs. You should call the `replay()` method | ||
- **balance** `array<url>` - Define the URLs to balance. You should call the `balance()` method | ||
- **replay** `array<string>` - Optional replay server URLs. Via API you should use the `replay()` method | ||
- **balance** `array<url>` - Define the URLs to balance. Via API you should use the `balance()` method | ||
- **forward** `string` - url string to be parsed with the url module | ||
@@ -160,2 +177,3 @@ - **agent** `object` - object to be passed to http(s).request. See node.js [`https`](https://nodejs.org/api/https.html#https_class_https_agent) docs | ||
- **auth** `boolean` - Basic authentication i.e. 'user:password' to compute an Authorization header. | ||
- **forwardHost** `boolean` - Forward target URL host while proxying. Default `false` | ||
- **hostRewrite** `boolean` - rewrites the location hostname on (301/302/307/308) redirects, Default: null. | ||
@@ -168,2 +186,4 @@ - **autoRewrite** `boolean` - rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false. | ||
Default configuration file name: `rocky.toml` | ||
The configuration file must be declared in [TOML](https://github.com/toml-lang/toml) language | ||
@@ -187,2 +207,6 @@ ```toml | ||
[/*] | ||
method = "GET" | ||
forward = "http://old.server" | ||
[/download/:file] | ||
@@ -192,5 +216,9 @@ method = "GET" | ||
[/*] | ||
[/photo/:name] | ||
method = "GET" | ||
forward = "http://old.server" | ||
[[replay]] | ||
target = "http://old.server" | ||
forwardHost = true | ||
[[replay]] | ||
target = "http://backup.server" | ||
``` | ||
@@ -505,2 +533,3 @@ | ||
- **error** `err, req, res` - Fired when the forward request fails | ||
- **route:error** `err, req, res` - Fired when cannot forward/replay the request or middleware error | ||
- **replay:start** `params, opts, req` - Fired before a replay request starts | ||
@@ -507,0 +536,0 @@ - **replay:error** `opts, err, req, res` - Fired when the replay request fails |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66794
1490
581