Permalink
Browse files

Add proxy support

Edited slightly by isaacs for style
  • Loading branch information...
1 parent 05dcec1 commit c42fe61a0d491344c829254b3a1e49e501de93c0 @cliffano cliffano committed with isaacs Jul 29, 2010
Showing with 33 additions and 5 deletions.
  1. +9 −0 doc/config.md
  2. +7 −2 lib/utils/fetch.js
  3. +7 −3 lib/utils/registry/request.js
  4. +10 −0 man/config.1
View
@@ -109,3 +109,12 @@ If you ask npm to install a package and don't tell it a specific version, then
it will install the specified tag.
Note: this has no effect on the npm-tag(1) command.
+
+### proxy
+
+If proxy is available, then npm will fetch the modules from the registry via
+the proxy server.
+
+Example:
+
+ proxy = http://proxy-server:8080
View
@@ -9,6 +9,7 @@ var http = require("http")
, get = require("./get")
, set = require("./set")
, log = require("./log")
+ , npm = require("../../npm")
, Buffer = require("buffer").Buffer
module.exports = fetch
@@ -33,9 +34,13 @@ function fetchAndWrite (remote, fd, headers, maxRedirects, redirects, cb) {
remote = url.parse(remote)
set(headers, "host", remote.hostname)
remote.path = remote.pathname+(remote.search||"")+(remote.hash||"")
- var https = (remote.protocol === "https:")
+ var proxyConfig = npm.config.get("proxy")
+ , proxy = (proxyConfig) ? url.parse(proxyConfig) : null
+ , https = (proxy || remote).protocol === "https:"
+ , port = (proxy || remote).port || (https ? 443 : 80)
+ , hostname = (proxy || remote).hostname
http
- .createClient(remote.port || (https ? 443 : 80), remote.hostname, https)
+ .createClient(port, hostname, https)
.request( "GET"
, (remote.pathname||"/")+(remote.search||"")+(remote.hash||"")
, headers
@@ -25,7 +25,12 @@ function request (method, where, what, cb) {
|| method === "DELETE"
where = url.resolve(npm.config.get("registry"), where)
var u = url.parse(where)
- , https = u.protocol === "https:"
+ , proxyConfig = npm.config.get("proxy")
+ , proxy = (proxyConfig) ? url.parse(proxyConfig) : null
+ , https = (proxy || u).protocol === "https:"
+ , port = (proxy || u).port || (https ? 443 : 80)
+ , hostname = (proxy || u).hostname
+ , client = http.createClient(port, hostname, https)
, auth = authRequired && npm.config.get("_auth")
if (authRequired && !auth) {
@@ -54,8 +59,7 @@ function request (method, where, what, cb) {
headers["content-length"] = 0
}
- var client = http.createClient(u.port || (https ? 443 : 80), u.hostname, https)
- , request = client.request(method, u.pathname, headers)
+ var request = client.request(method, u.pathname, headers)
request.on("error", cb)
request.on("response", function (response) {
// if (response.statusCode !== 200) return cb(new Error(
View
@@ -140,3 +140,13 @@ If you ask npm to install a package and don\'t tell it a specific version, then
.
.P
Note: this has no effect on the npm\-tag(1) command\.
+.
+.SS "proxy"
+If proxy is available, then npm will fetch the modules from the registry via
+the proxy server\.
+.
+.P
+Example:
+.
+.P
+proxy = http://proxy-server:8080

0 comments on commit c42fe61

Please sign in to comment.