Skip to content

Instantly share code, notes, and snippets.

@trentm
Created June 28, 2012 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trentm/3013392 to your computer and use it in GitHub Desktop.
Save trentm/3013392 to your computer and use it in GitHub Desktop.
better fetch
Index: /Users/trentm/tm/npm/lib/utils/fetch.js
index b489c21..6042abc 100644
--- a/lib/utils/fetch.js
+++ b/lib/utils/fetch.js
@@ -25,17 +25,31 @@ 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 (fstr._ERROR) return
+ if (calledback) return
+ calledback = true
cb(fstr._ERROR = er)
})
fstr.on("open", function () {
- makeRequest(remote, fstr, headers)
+ var req = makeRequest(remote, fstr, headers)
+ req.on("response", function (res) {
+ log.http(res.statusCode, remote)
+ response = res
+ })
})
fstr.on("close", function () {
- if (fstr._ERROR) return
- cb()
+ if (calledback) return
+ calledback = true
+ 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)
+ }
})
}
@@ -64,8 +78,6 @@ function makeRequest (remote, fstr, headers) {
req.on("error", function (er) {
fstr.emit("error", er)
})
- req.on("response", function (res) {
- log.http(res.statusCode, remote.href)
- })
req.pipe(fstr)
+ return req;
}
$ touch proxy.js && sleep 1 && rm -rf tmp/cache node_modules/bunyan && node ~/tm/npm/cli.js --loglevel=info --cache=`pwd`/tmp/cache --registry=http://localhost:8000/ install bunyan@0.6.8npm info it worked if it ends with ok
npm info using npm@1.1.32
npm info using node@v0.6.19
npm info retry registry request attempt 1 at 12:48:28
npm http GET http://localhost:8000/bunyan/0.6.8
npm http 200 http://localhost:8000/bunyan/0.6.8
npm info retry fetch attempt 1 at 12:48:38
npm http GET http://localhost:8000/bunyan/-/bunyan-0.6.8-BOGUS.tgz
npm http 404 http://localhost:8000/bunyan/-/bunyan-0.6.8-BOGUS.tgz
npm ERR! fetch failed http://localhost:8000/bunyan/-/bunyan-0.6.8-BOGUS.tgz
npm ERR! Error: 404 Not Found
npm ERR! at [object Object].<anonymous> (/Users/trentm/tm/npm/lib/utils/fetch.js:47:16)
npm ERR! at [object Object].emit (events.js:88:20)
npm ERR! at fs.js:1319:12
npm ERR! at Object.oncomplete (/Users/trentm/tm/npm/node_modules/graceful-fs/graceful-fs.js:94:5)
npm ERR! [Error: 404 Not Found]
npm ERR! You may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-@googlegroups.com>
npm ERR! System Darwin 10.8.0
npm ERR! command "node" "/Users/trentm/tm/npm/cli.js" "--loglevel=info" "--cache=/Users/trentm/tm/npm-registry-proxy/tmp/cache" "--registry=http://localhost:8000/" "install" "bunyan@0.6.8"
npm ERR! cwd /Users/trentm/tm/npm-registry-proxy
npm ERR! node -v v0.6.19
npm ERR! npm -v 1.1.32
npm ERR! message 404 Not Found
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/trentm/tm/npm-registry-proxy/npm-debug.log
npm ERR! not ok code undefined
npm ERR! not ok code 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment