Add noproxy configuration #2873

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+161 −3
Split
View
@@ -483,6 +483,13 @@ Any "%s" in the message will be replaced with the version number.
The node version to use when checking package's "engines" hash.
+### noproxy
+
+* Default: `NO_PROXY` or `no_proxy` environment variable, or "null"
+* Type: String
+
+Hostnames which won't use proxy when being requested.
+
### npat
* Default: false
View
@@ -73,9 +73,13 @@ function makeRequest (remote, fstr, headers) {
"Auth required and none provided. Please run 'npm adduser'"))
}
- var proxy
- if (remote.protocol !== "https:" || !(proxy = npm.config.get("https-proxy"))) {
- proxy = npm.config.get("proxy")
+ var proxy = null
+ var noproxy = npm.config.get("noproxy") ?
+ npm.config.get("noproxy").replace(/\s/g, "").split(",") : []
+ if(noproxy.indexOf(remote.hostname) === -1) {
+ if (remote.protocol !== "https:" || !(proxy = npm.config.get("https-proxy"))) {
+ proxy = npm.config.get("proxy")
+ }
}
var opts = { url: remote
@@ -0,0 +1,17 @@
+Test noproxy configuration.
+
+
+We create a server and a proxy.
+Server listens on localhost:80.
+Proxy listens on localhost:8080.
+The proxy redirects all requests to /proxy on the server.
+On the server, /proxy sends back proxy.tar.gz .
+When server is directly requested, it answers with noproxy.tar.gz .
+
+
+So we perform 2 tests, both with proxy equal to "http://localhost:8080".
+-A test is performed with noproxy equal to "null". In this case, npm
+ installs the package named "proxy.tar.gz" because the proxy is used.
+-In the other test, noproxy equal "localhost, example.com".
+ Since localhost is part of noproxy, request is made directly
+ to the server and proxy is ignored.
@@ -0,0 +1 @@
+Package for npm-test-noproxy.
@@ -0,0 +1,6 @@
+{
+ "author": "vvision",
+ "name": "test-noproxy-test",
+ "description": "Dummy package for noproxy configuration tests",
+ "version": "10.10.10"
+}
Binary file not shown.
@@ -0,0 +1,7 @@
+{ "name":"npm-test-noproxy"
+, "version" : "1.0.0"
+, "scripts" :
+ { "install" : "node test.js"
+ , "test" : "node test.js"
+ }
+}
@@ -0,0 +1 @@
+Package for npm-test-noproxy.
@@ -0,0 +1,6 @@
+{
+ "author": "vvision",
+ "name": "test-proxy-test",
+ "description": "Dummy package for noproxy configuration tests",
+ "version": "7.7.7"
+}
Binary file not shown.
@@ -0,0 +1,109 @@
+var npm = require("npm")
+ , npmconf = require("npmconf")
+ , assert = require("assert")
+ , path = require('path')
+ , request = require('request')
+ , fs = require('graceful-fs')
+ , http = require('http')
+ , nopt = require('nopt')
+ , log = require("npmlog")
+
+ , configDefs = npmconf.defs
+ , shorthands = configDefs.shorthands
+ , types = configDefs.types
+ , conf = nopt(types, shorthands)
+ //We create a server and a proxy (see README).
+ , server = http.createServer(function(req, res){
+ res.statusCode = 200
+ if(req.url == '/proxy') {
+ // opens file in read, with a stream
+ fs.createReadStream(__dirname+'/proxy.tar.gz').on('error', function(err){
+ // something goes wrong while reading
+ log.warn(err)
+ res.statusCode = 404
+ res.end()
+ }).pipe(res)
+ // read data are piped into the response
+ } else {
+ fs.createReadStream(__dirname+'/noproxy.tar.gz').on('error', function(err){
+ log.warn(err)
+ res.statusCode = 404
+ res.end()
+ }).pipe(res)
+ }
+ })
+ , proxy = http.createServer(function (req, res) {
+ res.statusCode = 200
+ var url = 'http://localhost:80/proxy'
+ var x = request(url)
+ req.pipe(x)
+ x.pipe(res)
+ })
+
+npm.load(conf, function (er, npm) {
+ if(er) log.warn(er)
+
+ //Set proxy configuration, launch server and proxy
+ var initialize = function (cb) {
+ npm.config.set('proxy', 'http://localhost:8080')
+ server.listen(80, 'localhost', function () {
+ proxy.listen(8080, 'localhost', cb)
+ })
+ }
+
+ //Tests
+ initialize(function () {
+ assert.equal('http://localhost:8080', npm.config.get('proxy'))
+ //Checking the route for server and proxy
+ request.get("http://localhost:80/test", function (err, res, body) {
+ assert.equal(res.statusCode, 200)
+ request.get("http://localhost:80/proxy", function (err, res2, body) {
+ assert.equal(res2.statusCode, 200)
+ request.get("http://localhost:8080/test", function (err, res3, body) {
+ assert.equal(res3.statusCode, 200)
+ makeNoProxyTest(function () {
+ makeProxyTest(function () {
+ closeServer(server)
+ closeServer(proxy)
+ })
+ })
+ })
+ })
+ })
+ })
+
+ //Use npm with noproxy
+ var makeNoProxyTest = function (cb) {
+ //Set noproxy configuration
+ npm.config.set('noproxy', 'localhost , example.com')
+ assert.equal('localhost , example.com', npm.config.get('noproxy'))
+ //Install from localhost:80/test without proxy
+ npm.commands.install(['http://localhost:80/test'], function () {
+ //Let's check if the package directory exists in node_modules
+ fs.exists('../test-noproxy-test', function (exists) {
+ assert.equal(exists, true)
+ cb()
+ })
+ })
+ }
+
+ //Use npm with proxy
+ var makeProxyTest = function (cb) {
+ //Set proxy configuration
+ npm.config.set('noproxy', 'null')
+ assert.equal('null', npm.config.get('noproxy'))
+ //Install from localhost:80/test with proxy
+ npm.commands.install(['http://localhost:80/test'], function () {
+ //Let's check if the package directory exists in node_modules
+ fs.exists('../test-proxy-test', function (exists) {
+ assert.equal(exists, true)
+ cb()
+ })
+ })
+ }
+
+ var closeServer = function (s) {
+ s.close()
+ }
+
+})