forked from drewzboto/grunt-connect-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect_proxy.js
More file actions
68 lines (65 loc) · 2.4 KB
/
connect_proxy.js
File metadata and controls
68 lines (65 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* grunt-connect-proxy
* https://github.com/drewzboto/grunt-connect-proxy
*
* Copyright (c) 2013 Drewz
* Licensed under the MIT license.
*/
'use strict';
var utils = require('../lib/utils');
var _ = require('lodash');
module.exports = function(grunt) {
grunt.registerTask('configureProxies', 'Configure any specified connect proxies.', function(config) {
// setup proxy
var httpProxy = require('http-proxy');
var proxyOption;
var proxyOptions = [];
var validateProxyConfig = function(proxyOption) {
if (_.isUndefined(proxyOption.host) || _.isUndefined(proxyOption.context)) {
grunt.log.error('Proxy missing host or context configuration');
return false;
}
if (proxyOption.https && proxyOption.port === 80) {
grunt.log.warn('Proxy for ' + proxyOption.context + ' is using https on port 80. Are you sure this is correct?');
}
return true;
};
utils.reset();
utils.log = grunt.log;
if (config) {
var connectOptions = grunt.config('connect.'+config) || [];
if (typeof connectOptions.appendProxies === 'undefined' || connectOptions.appendProxies) {
proxyOptions = proxyOptions.concat(grunt.config('connect.proxies') || []);
}
proxyOptions = proxyOptions.concat(connectOptions.proxies || []);
} else {
proxyOptions = proxyOptions.concat(grunt.config('connect.proxies') || []);
}
proxyOptions.forEach(function(proxy) {
proxyOption = _.defaults(proxy, {
port: 80,
https: false,
changeOrigin: false,
xforward: false,
rejectUnauthorized: false,
rules: [],
rewriteCookiePath: null
});
if (validateProxyConfig(proxyOption)) {
proxyOption.rules = utils.processRewrites(proxyOption.rewrite);
utils.registerProxy({
server: new httpProxy.HttpProxy({
target: proxyOption,
changeOrigin: proxyOption.changeOrigin,
enable : {
xforward: proxyOption.xforward // enables X-Forwarded-For
},
timeout: proxyOption.timeout
}),
config: proxyOption
});
grunt.log.writeln('Proxy created for: ' + proxyOption.context + ' to ' + proxyOption.host + ':' + proxyOption.port);
}
});
});
};