grunt-connect-proxy
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -22,7 +22,7 @@ /* | ||
'lib/*.js', | ||
'<%= nodeunit.tests %>', | ||
'<%= nodeunit.tests %>' | ||
], | ||
options: { | ||
jshintrc: '.jshintrc', | ||
}, | ||
jshintrc: '.jshintrc' | ||
} | ||
}, | ||
@@ -32,3 +32,3 @@ | ||
clean: { | ||
tests: ['tmp'], | ||
tests: ['tmp'] | ||
}, | ||
@@ -59,3 +59,8 @@ | ||
'^/full': '/anothercontext' | ||
} | ||
}, | ||
timeout: 5000, | ||
headers: { | ||
"X-Proxied-Header": "added" | ||
}, | ||
ws: true | ||
}, | ||
@@ -83,3 +88,3 @@ { | ||
{ | ||
host: 'www.missingcontext.com', | ||
host: 'www.missingcontext.com' | ||
}, | ||
@@ -89,3 +94,3 @@ { | ||
host: 'www.defaults.com' | ||
}, | ||
} | ||
], | ||
@@ -110,2 +115,20 @@ server2: { | ||
] | ||
}, | ||
request: { | ||
options: { | ||
middleware: function (connect, options) { | ||
return [require('./lib/utils').proxyRequest]; | ||
} | ||
}, | ||
proxies: [ | ||
{ | ||
context: '/request', | ||
host: 'localhost', | ||
port: 8080, | ||
changeOrigin: true, | ||
headers: { | ||
"x-proxied-header": "added" | ||
} | ||
} | ||
] | ||
} | ||
@@ -116,6 +139,8 @@ }, | ||
nodeunit: { | ||
tests: ['test/connect_proxy_test.js'], | ||
tests: 'test/connect_proxy_test.js', | ||
server2: 'test/server2_proxy_test.js', | ||
server3: 'test/server3_proxy_test.js' | ||
}, | ||
server3: 'test/server3_proxy_test.js', | ||
utils: 'test/utils_test.js', | ||
request: 'test/request_test.js' | ||
} | ||
@@ -128,2 +153,3 @@ }); | ||
// These plugins provide necessary tasks. | ||
grunt.loadNpmTasks('grunt-contrib-connect'); | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
@@ -137,2 +163,3 @@ grunt.loadNpmTasks('grunt-contrib-clean'); | ||
'clean', | ||
'nodeunit:utils', | ||
'configureProxies', | ||
@@ -144,2 +171,5 @@ 'nodeunit:tests', | ||
'nodeunit:server3', | ||
'configureProxies:request', | ||
'connect:request', | ||
'nodeunit:request' | ||
]); | ||
@@ -151,3 +181,3 @@ | ||
'configureProxies:server2', | ||
'nodeunit:server2', | ||
'nodeunit:server2' | ||
]); | ||
@@ -154,0 +184,0 @@ |
@@ -59,18 +59,54 @@ 'use strict'; | ||
utils.matchContext = function(context, url) { | ||
var positiveContexts, negativeContexts, positiveMatch, negativeMatch; | ||
var contexts = context; | ||
var matched = false; | ||
if (!_.isArray(contexts)) { | ||
contexts = [contexts]; | ||
} | ||
contexts.forEach(function(testContext) { | ||
if (url.lastIndexOf(testContext, 0) === 0) { | ||
matched = true; | ||
positiveContexts = _.filter(contexts, function(c){return c.charAt(0) !== '!';}); | ||
negativeContexts = _.filter(contexts, function(c){return c.charAt(0) === '!';}); | ||
// Remove the '!' character from the contexts | ||
negativeContexts = _.map(negativeContexts, function(c){return c.slice(1);}); | ||
negativeMatch = _.find(negativeContexts, function(c){return url.lastIndexOf(c, 0) === 0;}); | ||
// If any context negates this url, it must not be proxied. | ||
if (negativeMatch) { | ||
return false; | ||
} | ||
positiveMatch = _.find(positiveContexts, function(c){return url.lastIndexOf(c, 0) === 0;}); | ||
// If there is any positive match, lets proxy this url. | ||
return positiveMatch != null; | ||
}; | ||
function onUpgrade(req, socket, head) { | ||
var proxied = false; | ||
proxies.forEach(function(proxy) { | ||
if (!proxied && req && proxy.config.ws && utils.matchContext(proxy.config.context, req.url)) { | ||
if (proxy.config.rules.length) { | ||
proxy.config.rules.forEach(rewrite(req)); | ||
} | ||
proxy.server.proxyWebSocketRequest(req, socket, head); | ||
proxied = true; | ||
var source = req.url; | ||
var target = (proxy.server.target.https ? 'wss://' : 'ws://') + proxy.server.target.host + ':' + proxy.server.target.port + req.url; | ||
utils.log.verbose.writeln('[WS] Proxied request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2)); | ||
} | ||
}); | ||
return matched; | ||
}; | ||
} | ||
//Listen for the update event,onces. grunt-contrib-connect doesnt expose the server object, so bind after the first req | ||
function enableWebsocket(server) { | ||
if (!server.proxyWs) { | ||
server.proxyWs = true; | ||
utils.log.verbose.writeln('[WS] Catching upgrade event...'); | ||
server.on('upgrade', onUpgrade); | ||
} | ||
} | ||
utils.proxyRequest = function (req, res, next) { | ||
var proxied = false; | ||
enableWebsocket(req.connection.server); | ||
proxies.forEach(function(proxy) { | ||
@@ -81,2 +117,8 @@ if (!proxied && req && utils.matchContext(proxy.config.context, req.url)) { | ||
} | ||
// Add headers present in the config object | ||
if (proxy.config.headers != null) { | ||
_.forOwn(proxy.config.headers, function(value, key) { | ||
req.headers[key] = value; | ||
}); | ||
} | ||
proxy.server.proxyRequest(req, res); | ||
@@ -83,0 +125,0 @@ // proxying twice would cause the writing to a response header that is already sent. Bad config! |
{ | ||
"name": "grunt-connect-proxy", | ||
"description": "Provides a http proxy as middleware for grunt connect.", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"homepage": "https://github.com/drewzboto/grunt-connect-proxy", | ||
@@ -38,3 +38,4 @@ "author": { | ||
"grunt-contrib-nodeunit": "~0.1.2", | ||
"grunt": "~0.4.1" | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-connect": "~0.5.0" | ||
}, | ||
@@ -45,4 +46,8 @@ "peerDependencies": { | ||
"keywords": [ | ||
"gruntplugin", "proxy", "connect", "http", "grunt" | ||
"gruntplugin", | ||
"proxy", | ||
"connect", | ||
"http", | ||
"grunt" | ||
] | ||
} |
# grunt-connect-proxy | ||
> Provides a http proxy as middleware for the grunt-contrib-connect plugin. | ||
> Provides a http proxy as middleware for the grunt-contrib-connect plugin. | ||
@@ -30,2 +30,3 @@ ## Getting Started | ||
connect: { | ||
server: { | ||
options: { | ||
@@ -42,6 +43,10 @@ port: 9000, | ||
changeOrigin: false, | ||
xforward: false | ||
xforward: false, | ||
headers: { | ||
"x-custom-added-header": value | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}) | ||
@@ -52,6 +57,2 @@ ``` | ||
##### With Livereload | ||
Expose the proxy function to use in the middleware, at the top of the Gruntfile: | ||
```js | ||
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest; | ||
``` | ||
@@ -63,6 +64,20 @@ Add the middleware call from the connect option middleware hook | ||
options: { | ||
middleware: function (connect) { | ||
return [ | ||
proxySnippet | ||
]; | ||
middleware: function (connect, options) { | ||
var middlewares = []; | ||
var directory = options.directory || options.base[options.base.length - 1]; | ||
if (!Array.isArray(options.base)) { | ||
options.base = [options.base]; | ||
} | ||
// Setup the proxy | ||
middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest); | ||
options.base.forEach(function(base) { | ||
// Serve static files. | ||
middlewares.push(connect.static(base)); | ||
}); | ||
// Make directory browse-able. | ||
middlewares.push(connect.directory(directory)); | ||
return middlewares; | ||
} | ||
@@ -88,14 +103,16 @@ } | ||
middleware: function (connect, options) { | ||
var config = [ // Serve static files. | ||
connect.static(options.base), | ||
// Make empty directories browsable. | ||
connect.directory(options.base) | ||
]; | ||
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest; | ||
config.unshift(proxy); | ||
return config; | ||
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest; | ||
return [ | ||
// Include the proxy first | ||
proxy, | ||
// Serve static files. | ||
connect.static(options.base), | ||
// Make empty directories browsable. | ||
connect.directory(options.base) | ||
]; | ||
} | ||
} | ||
}, | ||
proxies: [ /* as defined above */ ] | ||
} | ||
// ... | ||
} | ||
``` | ||
@@ -110,3 +127,3 @@ | ||
'compass:server', | ||
'configureProxies', | ||
'configureProxies:server', | ||
'livereload-start', | ||
@@ -120,2 +137,3 @@ 'connect:livereload', | ||
**IMPORTANT**: You must specify the connect target in the `configureProxies` task. | ||
@@ -130,3 +148,3 @@ ### Options | ||
Multiple contexts can be matched for the same proxy rule via an array such as: | ||
context: ['/api', 'otherapi'] | ||
context: ['/api', 'otherapi'] | ||
@@ -142,3 +160,3 @@ #### options.host | ||
The port to proxy to. | ||
The port to proxy to. | ||
@@ -157,3 +175,3 @@ #### options.https | ||
#### options.rejectUnauthorized: | ||
#### options.rejectUnauthorized: | ||
Type: `Boolean` | ||
@@ -164,3 +182,3 @@ Default: false | ||
#### options.xforward: | ||
#### options.xforward: | ||
Type: `Boolean` | ||
@@ -198,2 +216,18 @@ Default: false | ||
#### options.timeout | ||
Type: `Number` | ||
The connection timeout in milliseconds. The default timeout is 2 minutes (120000 ms). | ||
#### options.headers | ||
Type: `Object` | ||
A map of headers to be added to proxied requests. | ||
#### options.ws | ||
Type: `Boolean` | ||
Default: false | ||
Set to true to proxy websockets. | ||
## Contributing | ||
@@ -254,1 +288,2 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). | ||
* 0.1.6 Add xforward option, added support for context arrays, added debug logging | ||
* 0.1.7 Added WebSocket support (thanks for @killfill), Headers support (thanks to @gadr), various docs fixed |
@@ -48,3 +48,4 @@ /* | ||
rejectUnauthorized: false, | ||
rules: [] | ||
rules: [], | ||
ws: false | ||
}); | ||
@@ -59,3 +60,4 @@ if (validateProxyConfig(proxyOption)) { | ||
xforward: proxyOption.xforward // enables X-Forwarded-For | ||
} | ||
}, | ||
timeout: proxyOption.timeout | ||
}), | ||
@@ -62,0 +64,0 @@ config: proxyOption |
@@ -32,3 +32,3 @@ 'use strict'; | ||
default_options: function(test) { | ||
test.expect(9); | ||
test.expect(10); | ||
var proxies = utils.proxies(); | ||
@@ -45,2 +45,3 @@ | ||
test.equal(proxies[0].config.xforward, false, 'should have default xforward'); | ||
test.equal(proxies[0].config.ws, false, 'should have default ws to false'); | ||
@@ -50,3 +51,3 @@ test.done(); | ||
full_options: function(test) { | ||
test.expect(12); | ||
test.expect(13); | ||
var proxies = utils.proxies(); | ||
@@ -63,2 +64,3 @@ | ||
test.equal(proxies[1].config.xforward, true, 'should have xforward set from config'); | ||
test.equal(proxies[1].config.ws, true, 'should have ws set from config'); | ||
test.deepEqual(proxies[1].config.rewrite, { '^/full': '/anothercontext' }, 'should have rewrite set from config'); | ||
@@ -65,0 +67,0 @@ test.equal(proxies[1].config.rules.length, 1, 'rules array should have an item'); |
@@ -9,3 +9,3 @@ var utils = require("../lib/utils.js"); | ||
proxy_options_test: function(test) { | ||
test.expect(10); | ||
test.expect(11); | ||
var proxies = utils.proxies(); | ||
@@ -22,2 +22,3 @@ | ||
test.equal(proxies[0].config.changeOrigin, false, 'should have default change origin'); | ||
test.equal(proxies[0].config.ws, false, 'should have default ws to false'); | ||
test.equal(proxies[0].config.rules.length, 0, 'rules array should have zero items'); | ||
@@ -24,0 +25,0 @@ |
@@ -9,3 +9,3 @@ var utils = require("../lib/utils.js"); | ||
proxy_options_test: function(test) { | ||
test.expect(8); | ||
test.expect(9); | ||
var proxies = utils.proxies(); | ||
@@ -20,2 +20,3 @@ | ||
test.equal(proxies[0].config.changeOrigin, true, 'should have change origin from task'); | ||
test.equal(proxies[0].config.ws, false, 'should have ws default to false'); | ||
test.equal(proxies[0].config.rules.length, 0, 'rules array should have zero items'); | ||
@@ -22,0 +23,0 @@ |
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
Network access
Supply chain riskThis module accesses the network.
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
40321
15
823
277
5
3