diff options
author | isaacs <i@izs.me> | 2010-10-05 22:15:56 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-10-05 22:15:56 +0400 |
commit | 0841e0454ca472b96df927e239ec959d838c3f7d (patch) | |
tree | 51fb2c9635a5125fa4a050eb1a83271108dd95b1 | |
parent | 0cdded639c0cee72e4a76cf0ef93be6570ff692d (diff) |
Use a proper stream/pump rather than this kludgey manual stuff from long ago (since it's actually starting to fail now.)
-rw-r--r-- | lib/utils/registry/request.js | 45 |
1 files changed, 8 insertions, 37 deletions
diff --git a/lib/utils/registry/request.js b/lib/utils/registry/request.js index 0eca8d1d3..f827bb2bf 100644 --- a/lib/utils/registry/request.js +++ b/lib/utils/registry/request.js @@ -12,6 +12,7 @@ var npm = require("../../../npm") , ini = require("../ini") , Buffer = require("buffer").Buffer , fs = require("../graceful-fs") + , sys = require("sys") function request (method, where, what, cb) { log.verbose(where||"/", method) @@ -80,7 +81,10 @@ function request (method, where, what, cb) { var data = "" response.on("error", cb) response.on("clientError", cb) - response.on("data", function (chunk) { data += chunk }) + response.on("data", function (chunk) { + log.silly(chunk+"", "chunk") + data += chunk + }) response.on("end", function () { var parsed try { @@ -100,37 +104,7 @@ function request (method, where, what, cb) { }) }) if (what instanceof File) { - var size = 1024 - // , b = new Buffer(what.length) - , remaining = what.length - log.verbose(what.length, "bytes") - ;(function W () { - var b = new Buffer(size) - try { - var bytesRead = fs.readSync(what.fd, b, 0, b.length, null) - } catch (er) { - return log.er(cb, "Failure to read tarball")(er) - } - remaining -= bytesRead - if (bytesRead) { - return ( - request.write(bytesRead === b.length ? b : b.slice(0, bytesRead)) - ) ? W() - : request.on("drain", function DRAIN () { - request.removeListener("drain", DRAIN) - W() - }) - } - if (!remaining) { - request.end() - log.verbose(what.name, "written to uploading stream") - log.verbose("Not done yet! If it hangs/quits now, it didn't work.", "upload") - return - } - // wtf!? No bytes read, but also bytes remaining. - return cb(new Error("Some kind of weirdness reading the file")) - })() - return + return sys.pump(what.stream, request, cb) } else if (typeof what === "string" || Buffer.isBuffer(what)) { // just a json blob request.write(what) @@ -157,11 +131,8 @@ function File (name, cb) { if (er) return log.er(cb, "doesn't exist "+f.name)(er) log.silly(stat, "stat "+name) f.length = stat.size - fs.open(f.name, "r", 0666, function (er, fd) { - if (er) return log.er(cb, "Error opening "+f.name)(er) - f.fd = fd - cb(null, f) - }) + f.stream = fs.createReadStream(f.name) + cb(null, f) }) } |