diff options
author | isaacs <i@izs.me> | 2013-03-20 17:49:57 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-03-20 17:49:57 -0700 |
commit | 3dd7938c03389371ce1d142a8558a98b450ca2ee (patch) | |
tree | be7bf56caefeaec9b6a872fc903963b8fba6e48b /deps/npm/lib/utils/fetch.js | |
parent | 855caa82aaef5c6e6dd244b5d9df314994cb23eb (diff) | |
download | node-new-3dd7938c03389371ce1d142a8558a98b450ca2ee.tar.gz |
npm: upgrade to 1.2.15
Diffstat (limited to 'deps/npm/lib/utils/fetch.js')
-rw-r--r-- | deps/npm/lib/utils/fetch.js | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/deps/npm/lib/utils/fetch.js b/deps/npm/lib/utils/fetch.js index b988520660..526b836a2c 100644 --- a/deps/npm/lib/utils/fetch.js +++ b/deps/npm/lib/utils/fetch.js @@ -11,11 +11,13 @@ var request = require("request") , mkdir = require("mkdirp") , chownr = require("chownr") , regHost + , once = require("once") module.exports = fetch function fetch (remote, local, headers, cb) { if (typeof cb !== "function") cb = headers, headers = {} + cb = once(cb) log.verbose("fetch", "to=", local) mkdir(path.dirname(local), function (er, made) { if (er) return cb(er) @@ -26,30 +28,36 @@ function fetch (remote, local, headers, cb) { function fetch_ (remote, local, headers, cb) { var fstr = fs.createWriteStream(local, { mode : npm.modes.file }) var response = null - var calledback = false + fstr.on("error", function (er) { - fs.close(fstr.fd, function () {}) - if (calledback) return - calledback = true - cb(fstr._ERROR = er) + cb(er) + fstr.close() }) - fstr.on("open", function () { - var req = makeRequest(remote, fstr, headers) - req.on("response", function (res) { - log.http(res.statusCode, remote) - response = res - }) + + var req = makeRequest(remote, fstr, headers) + req.on("response", function (res) { + log.http(res.statusCode, remote) + response = res + response.resume() + // Work around bug in node v0.10.0 where the CryptoStream + // gets stuck and never starts reading again. + if (process.version === "v0.10.0") { + response.resume = function (orig) { return function() { + var ret = orig.apply(response, arguments) + if (response.socket.encrypted) + response.socket.encrypted.read(0) + return ret + }}(response.resume) + } }) + fstr.on("close", function () { - if (calledback) return - calledback = true + var er if (response && response.statusCode && response.statusCode >= 400) { - var er = new Error(response.statusCode + " " - + require("http").STATUS_CODES[response.statusCode]) - cb(fstr._ERROR = er, response) - } else { - cb(null, response) + er = new Error(response.statusCode + " " + + require("http").STATUS_CODES[response.statusCode]) } + cb(er, response) }) } @@ -73,6 +81,7 @@ function makeRequest (remote, fstr, headers) { var opts = { url: remote , proxy: proxy , strictSSL: npm.config.get("strict-ssl") + , rejectUnauthorized: npm.config.get("strict-ssl") , ca: remote.host === regHost ? npm.config.get("ca") : undefined , headers: { "user-agent": npm.config.get("user-agent") }} var req = request(opts) @@ -80,5 +89,5 @@ function makeRequest (remote, fstr, headers) { fstr.emit("error", er) }) req.pipe(fstr) - return req; + return req } |