Socket
Socket
Sign inDemoInstall

http-proxy-middleware

Package Overview
Dependencies
41
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.7.0 to 0.8.0

examples/websocket/index.html

10

examples/websocket/index.js

@@ -11,3 +11,2 @@ /**

target: 'http://echo.websocket.org',
// target: 'ws://echo.websocket.org', // alternative way to provide target with ws:// protocol
// pathRewrite: {

@@ -23,8 +22,11 @@ // '^/websocket' : '/socket', // rewrite path.

var app = express();
app.use('/', express.static(__dirname)); // demo page
app.use(proxy); // add the proxy to express
app.listen(3000);
var server = app.listen(3000);
server.on('upgrade', proxy.upgrade); // optional: upgrade externally
console.log('listening on port 3000');
console.log('try:');
console.log(' http://localhost:3000 for a demo');
console.log(' ws://localhost:3000 requests will be proxied to ws://echo.websocket.org');

@@ -34,8 +36,8 @@

* Example:
* Open http://localhost:3000 in WebSocket compatible browser. // don't mind the 404 page...
* Open http://localhost:3000 in WebSocket compatible browser.
* In browser console:
* 1. `var socket = new WebSocket('ws://localhost:3000');` // create new WebSocket
* 2. `socket.onmessage = function (msg) {console.log(msg)};` // listen to socket messages
* 3. `socket.send('hello world')`; // send message
* 3. `socket.send('hello world');` // send message
* > {data: "hello world"} // server should echo back your message.
**/

28

index.js

@@ -45,2 +45,9 @@ var _ = require('lodash');

// https://github.com/chimurai/http-proxy-middleware/issues/19
// expose function to upgrade externally
middleware.upgrade = function (req, socket, head) {
handleUpgrade(req, socket, head);
isWsUpgradeListened = true;
};
return middleware;

@@ -67,7 +74,7 @@

if (proxyOptions.ws === true) {
catchUpgradeRequest(req);
catchUpgradeRequest(req.connection.server);
}
}
function catchUpgradeRequest (req) {
function catchUpgradeRequest (server) {
// make sure only 1 handle listens to server's upgrade request.

@@ -78,13 +85,14 @@ if (isWsUpgradeListened === true) {

server.on('upgrade', handleUpgrade);
isWsUpgradeListened = true;
}
req.connection.server.on('upgrade', function (req, socket, head) {
if (contextMatcher.match(config.context, req.url)) {
if (pathRewriter) {
req.url = pathRewriter(req.url);
}
proxy.ws(req, socket, head);
console.log('[HPM] Upgrading to WebSocket');
function handleUpgrade (req, socket, head) {
if (contextMatcher.match(config.context, req.url)) {
if (pathRewriter) {
req.url = pathRewriter(req.url);
}
});
proxy.ws(req, socket, head);
console.log('[HPM] Upgrading to WebSocket');
}
}

@@ -91,0 +99,0 @@

@@ -21,4 +21,9 @@ var _ = require('lodash');

config.context = oUrl.pathname;
config.context = oUrl.pathname || '/';
config.options = _.assign(config.options, {target:target}, opts);
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
config.options.ws = true;
}
} else {

@@ -25,0 +30,0 @@ config.context = context;

{
"name": "http-proxy-middleware",
"version": "0.7.0",
"version": "0.8.0",
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",

@@ -38,5 +38,5 @@ "main": "index.js",

"express": "^4.13.3",
"istanbul": "^0.3.18",
"istanbul": "^0.3.19",
"istanbul-coveralls": "^1.0.3",
"mocha": "^2.2.5",
"mocha": "^2.3.0",
"mocha-lcov-reporter": "0.0.2",

@@ -46,8 +46,7 @@ "ws": "^0.8.0"

"dependencies": {
"http-proxy": "^1.11.1",
"http-proxy": "^1.11.2",
"is-glob": "^2.0.0",
"lodash": "^3.10.1",
"micromatch": "^2.2.0",
"url": "^0.10.3"
"micromatch": "^2.2.0"
}
}

@@ -25,6 +25,2 @@ # http-proxy-middleware

// 'proxy' is now ready to be used in a server.
// shorthand syntax for the example above:
// proxyMiddleware('http://www.example.org/api');
```

@@ -37,6 +33,12 @@ * **context**: matches provided context against request-urls' path.

``` javascript
// shorthand syntax for the example above:
var proxy = proxyMiddleware('http://www.example.org/api');
```
More about the [shorthand configuration](#shorthand).
### Example
A simple example with express server.
An example with express server.
```javascript

@@ -102,3 +104,3 @@ // include dependencies

### Shorthand
Use the shorthand syntax for simple use cases. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.

@@ -118,2 +120,28 @@ ```javascript

### WebSocket
```javascript
// verbose api
proxyMiddleware('/', {target:'http://echo.websocket.org', ws: true});
// shorthand
proxyMiddleware('http://echo.websocket.org', {ws:true});
// shorter shorthand
proxyMiddleware('ws://echo.websocket.org');
```
#### External WebSocket upgrade
In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http `upgrade` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http `upgrade` event manually.
```javascript
var proxy = proxyMiddleware('ws://echo.websocket.org', {changeOrigin:true});
var app = express();
app.use(proxy);
var server = app.listen(3000);
server.on('upgrade', proxy.upgrade); // <-- subscribe to http 'upgrade'
```
### Options

@@ -128,3 +156,3 @@ * **option.pathRewrite**: object, rewrite target's url path. Object-keys will be used as _RegExp_ to match paths.

* **option.proxyTable**: object, re-target `option.target` based on the request header `host` parameter. `host` can be used in conjunction with `path`. The order of the configuration matters.
* **option.proxyTable**: object, re-target `option.target` based on the request header `host` parameter. `host` can be used in conjunction with `path`. Only one instance of the proxy will be used. The order of the configuration matters.
```javascript

@@ -198,5 +226,5 @@ {

Or just explore the proxy examples' sources:
* `examples/connect` - [connect proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js)
* `examples/express` - [express proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js)
* `examples/browser-sync` - [browser-sync proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js)
* `examples/connect` - [connect proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js)
* `examples/express` - [express proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js)
* `examples/browser-sync` - [browser-sync proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js)
* `examples/websocket` - [websocket proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js) with express

@@ -225,2 +253,3 @@

### Changelog
* [v0.8.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.8.0) - support external websocket upgrade, fixed websocket shorthand
* [v0.7.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.7.0) - support shorthand syntax, fixed express/connect mounting

@@ -227,0 +256,0 @@ * [v0.6.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.6.0) - support proxyTable

@@ -58,2 +58,30 @@ var expect = require('chai').expect;

describe('shorthand api for websocket url', function () {
beforeEach(function () {
result = configFactory.createConfig('ws://www.example.org:8000');
});
it('should return on config object with context', function () {
expect(result.context).to.equal('/');
});
it('should return on options with ws = true', function () {
expect(result.options.ws).to.equal(true);
});
});
describe('shorthand api for secure websocket url', function () {
beforeEach(function () {
result = configFactory.createConfig('wss://www.example.org:8000');
});
it('should return on config object with context', function () {
expect(result.context).to.equal('/');
});
it('should return on options with ws = true', function () {
expect(result.options.ws).to.equal(true);
});
});
describe('shorthand api with globbing', function () {

@@ -60,0 +88,0 @@ beforeEach(function () {

@@ -8,17 +8,19 @@ var expect = require('chai').expect;

describe('option.ws - WebSocket proxy', function () {
describe('WebSocket proxy', function () {
var proxyServer, ws, wss;
var targetHeaders;
var responseMessage;
var proxy;
beforeEach(function () {
proxyServer = createServer(3000, proxyMiddleware('/', {
target:'http://localhost:8000',
ws: true,
pathRewrite: {
'^/socket' : ''
}
}));
proxy = proxyMiddleware('/', {
target:'http://localhost:8000',
ws: true,
pathRewrite: {'^/socket' : ''}
});
proxyServer = createServer(3000, proxy);
wss = new WebSocketServer({ port: 8000 });
wss.on('connection', function connection(ws) {

@@ -31,18 +33,20 @@ ws.on('message', function incoming(message) {

beforeEach(function (done) {
// need to make a normal http request,
// so http-proxy-middleware can catch the upgrade request
http.get('http://localhost:3000/', function () {
// do a second http request to make
// sure only 1 listener subscribes to upgrade request
describe('option.ws', function () {
beforeEach(function (done) {
// need to make a normal http request,
// so http-proxy-middleware can catch the upgrade request
http.get('http://localhost:3000/', function () {
ws = new WebSocket('ws://localhost:3000/socket');
// do a second http request to make
// sure only 1 listener subscribes to upgrade request
http.get('http://localhost:3000/', function () {
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
ws.on('open', function open() {
ws.send('foobar');
});
});

@@ -52,4 +56,57 @@ });

it('should proxy to path', function () {
expect(responseMessage).to.equal('foobar');
});
});
describe('option.ws with external server "upgrade"', function () {
beforeEach(function (done) {
proxyServer.on('upgrade', proxy.upgrade);
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
});
});
it('should proxy to path', function () {
expect(responseMessage).to.equal('foobar');
});
});
describe('option.ws with external server "upgrade" and shorthand usage', function () {
beforeEach(function () {
proxyServer.close();
// override
proxy = proxyMiddleware('ws://localhost:8000', {pathRewrite: {'^/socket' : ''}});
proxyServer = createServer(3000, proxy);
});
beforeEach(function (done) {
proxyServer.on('upgrade', proxy.upgrade);
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
});
});
it('should proxy to path', function () {
expect(responseMessage).to.equal('foobar');
});
});
afterEach(function () {

@@ -61,5 +118,2 @@ proxyServer.close();

it('should proxy to path', function () {
expect(responseMessage).to.equal('foobar');
});
});

@@ -66,0 +120,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc