Comparing version 0.2.0-beta.1 to 0.2.0
@@ -6,4 +6,5 @@ module.exports = { | ||
headers: require('./headers'), | ||
redirect: require('./redirect'), | ||
requestBody: require('./request-body'), | ||
responseBody: require('./response-body') | ||
} |
@@ -81,4 +81,4 @@ const router = require('router') | ||
Rocky.prototype.useFor = Route.prototype.useFor | ||
Rocky.prototype.useReplay = Route.prototype.useReplay | ||
Rocky.prototype.useForward = Route.prototype.useForward | ||
Rocky.prototype.useReplay = Route.prototype.useReplay | ||
@@ -105,2 +105,3 @@ Rocky.prototype.route = function (method, path) { | ||
// Expose rocky via the middleware | ||
req.rocky = { | ||
@@ -107,0 +108,0 @@ options: assign({}, rocky.opts), |
@@ -103,4 +103,9 @@ const midware = require('midware') | ||
Route.prototype.reply = function (code, headers, body) { | ||
this.use(middleware.reply.apply(null, arguments)) | ||
this.use(middleware.reply(code, headers, body)) | ||
return this | ||
} | ||
Route.prototype.redirect = function (url) { | ||
this.use(middleware.redirect(url)) | ||
return this | ||
} |
{ | ||
"name": "rocky", | ||
"version": "0.2.0-beta.1", | ||
"version": "0.2.0", | ||
"description": "Pluggable, versatile and middleware-oriented full featured HTTP/S proxy with traffic replay and intercept", | ||
@@ -5,0 +5,0 @@ "repository": "h2non/rocky", |
109
README.md
@@ -5,3 +5,5 @@ # 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) | ||
**Pluggable**, **full featured** and **middleware-oriented** **HTTP/S proxy** with versatile **routing** layer, **traffic interceptor and replay** to multiple backends, **built-in balancer**, **hierarchical configuration** and [more](#features). Built for [node.js](http://nodejs.org)/[io.js](https://iojs.org). Compatible with [connect](https://github.com/senchalabs/connect)/[express](http://expressjs.com). | ||
**Pluggable**, **full featured** and **middleware-oriented** **HTTP/S proxy** with versatile **routing** layer, **traffic interceptor and replay** to multiple backends, **built-in balancer**, **hierarchical configuration** and [more](#features). | ||
Built for [node.js](http://nodejs.org)/[io.js](https://iojs.org). | ||
Compatible with [connect](https://github.com/senchalabs/connect)/[express](http://expressjs.com). | ||
@@ -27,3 +29,5 @@ `rocky` can be fluently used [programmatically](#programmatic-api) or via [command-line](#command-line) interface. | ||
- [Middleware layer](#middleware-layer) | ||
- [Hierarchies](#hierarchies) | ||
- [Types of middleware](#types-of-middleware) | ||
- [Middleware flow](#middleware-flow) | ||
- [Middleware API](#middleware-api) | ||
@@ -67,3 +71,3 @@ - [Third-party middleware](#third-party-middleware) | ||
- As HTTP load balancer with zero-downtime | ||
- As HTTP API proxy gateway | ||
- As HTTP API gateway | ||
- As SSL terminator proxy | ||
@@ -112,8 +116,8 @@ - For A/B testing | ||
`rocky` was designed with versatility in mind, with a small core and clean codebase, and very focused on extensibility providing multiple layers of extensibility, such as middleware, which could be considered as well like a kind of hooks in some way. | ||
`rocky` design is driven by keeping versatility and extensibility in mind. The main goal is to keep it with a small core and codebase with just the proper responsability and built-in features, but highly opened to extensibility. | ||
Middleware layer is probably the core and more useful feature of `rocky`. | ||
so it can work as a standalone HTTP proxy or integrated in your existent `node.js` backend, powered by express/connect or a raw http server. | ||
The significant difference betweet the middleware layer and an event bus, which is very common in asynchronous programming, is the control flow capability. Via middleware you can completely rely on a consistent control flow when handling some data, continuing or stoping it accordingly. | ||
This approach allow you to plug in intermedia jobs with custom logic beetwen stages of the HTTP traffic flow live cycle. | ||
`rocky` will take care of HTTP routing, discerning traffic and forwarding/replaying it accordingly to your desired new backend. | ||
### Stability | ||
@@ -124,3 +128,4 @@ | ||
Version `0.2.x` introduces significant improvements, a more consistent API and handy features to extend rocky via its multiple middleware layers. This version is more focused on stability and production focused, however it's only recommended to use it in non-hostile environments. | ||
Version `0.2.x` introduced significant improvements such as a more consistent API and a new hierarchical middleware layer. | ||
This version is focused on stability and production use, however it's only recommended to use it in non-hostile environments for now. | ||
@@ -130,3 +135,3 @@ ### Versions | ||
- [**0.1.x**](https://github.com/h2non/rocky/tree/v0.1.x) `beta` - First version. Initially released at `25.06.2015`. | ||
- [**0.2.x**](https://github.com/h2non/rocky/tree/master) `beta` - Currently under development. | ||
- [**0.2.x**](https://github.com/h2non/rocky/tree/master) `beta` - Released at `07.07.2015`. | ||
@@ -165,2 +170,9 @@ ### How does it work? | ||
### Hierarchies | ||
`rocky` supports multiple middleware hierarchies: | ||
- **global** - Dispached on every incoming request matched by the router | ||
- **route** - Dispached only per route scope | ||
### Types of middleware | ||
@@ -174,20 +186,41 @@ | ||
Supported types of middleware are: | ||
**Supported types of middleware**: | ||
- **global** `.use([path], function (req, res, next))` - Dispachted on every matched route for both forward and replay phases. | ||
- **forward** `.useForward(function (req, res, next))` - Dispached before forwarding a request. | ||
- **replay** `.useReplay(function (req, res, next))` - Dispached before starting each replay request. | ||
- **param** `.useParam(param, function (req, res, next))` - Dispached on every matched param on route path. Only available as global middleware. | ||
##### router | ||
- **Scope**: `global` | ||
- **Description**: Dispatched on every matched route. | ||
- **Notation**: `.use([path], function (req, res, next))` | ||
##### forward | ||
- **Scope**: `global`, `route` | ||
- **Description**: Dispached before forwarding an incoming request. | ||
- **Notation**: `.useForward(function (req, res, next))` | ||
##### replay | ||
- **Scope**: `global`, `route` | ||
- **Description**: Dispached before starting each replay request. | ||
- **Notation**: `.useReplay(function (req, res, next))` | ||
##### param | ||
- **Scope**: `global` | ||
- **Description**: Dispached on every matched param on any route. | ||
- **Notation**: `.useParam(function (req, res, next))` | ||
### Middleware flow | ||
The following diagram explains the request flow and how the different middleware layers are involved in it: | ||
``` | ||
↓ ( Incoming request ) ↓ | ||
↓ || ↓ | ||
↓ ||| ↓ | ||
↓ ---------------- ↓ | ||
↓ | Router | ↓ --> Match a route, dispatching its middleware if required | ||
↓ ---------------- ↓ | ||
↓ ||| ↓ | ||
↓ --------------------- ↓ | ||
↓ [ Global middleware ] ↓ --> Dispatch on every incoming request | ||
↓ | Global middleware | ↓ --> Dispatch on every incoming request (Global) | ||
↓ --------------------- ↓ | ||
↓ || ↓ | ||
↓ ----------------- ↓ | ||
↓ | Route handler | ↓ --> Match a configured route | ||
↓ ----------------- ↓ | ||
↓ ||| ↓ | ||
@@ -197,3 +230,3 @@ ↓ / \ ↓ | ||
↓ / \ ↓ | ||
↓ [ Forward ] [ Replay ] ↓ --> Dispatch both middleware in separated flows | ||
↓ [ Forward ] [ Replay ] ↓ --> Dispatch both middleware in separated flows (Global, Route) | ||
↓ \ / ↓ | ||
@@ -212,2 +245,25 @@ ↓ \ / ↓ | ||
`rocky` exposes as a sort of inversion of control in every `http.ClientRequest` object the following fields: | ||
- **req.rocky** `object` | ||
- **.options** `object` - Expose the [configuration](#configuration) options for the current request. | ||
- **.proxy** `Rocky` - Expose the rocky instance. Use only for hacking purposes! | ||
- **.route** `Route` - Expose the current running route. Only available in `route` type middleware | ||
This provides you way to extend or modify specific values from the middleware layer without having side-effects, | ||
for instance replacing the server target URL, like in the following example: | ||
```js | ||
rocky() | ||
.get('/users/:name') | ||
.forward('http://old.server.net') | ||
.use(function (req, res, next) { | ||
if (req.param.name === 'admin') { | ||
// Overwrite the target URL only for this user | ||
req.rocky.options.target = 'http://new.server.net' | ||
} | ||
next() | ||
}) | ||
``` | ||
### Third-party middleware | ||
@@ -582,2 +638,3 @@ | ||
Define a set of URLs to balance between with a simple round-robin like scheduler. | ||
`urls` param must be an array of strings. | ||
@@ -595,8 +652,12 @@ #### route#reply(status, [ headers, body ]) | ||
Define or overwrite request headers | ||
Define or overwrite request headers for the current route. | ||
#### route#host(host) | ||
Overwrite the `Host` header value when forward the request | ||
Overwrite the `Host` header value when forward the request. | ||
#### route#redirect(url) | ||
Redirect the incoming request for the current route. | ||
#### route#transformRequestBody(middleware, [ filter ]) | ||
@@ -772,2 +833,6 @@ | ||
#### rocky.middleware.redirect(url) | ||
Shortcut method to redirect the current request. | ||
### rocky.httpProxy | ||
@@ -774,0 +839,0 @@ |
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
106167
56
2347
841