Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Report 404 errors properly with non-vhosted registries
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 29, 2011
1 parent f451082 commit 8923b80
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/utils/npm-registry-client/request.js
Expand Up @@ -81,10 +81,12 @@ function request (method, where, what, etag, nofollow, cb_) {
"Cannot insert data into the registry without authorization\n"
+ "See: npm-adduser(1)"))
}

if (auth && !secure && !warnedAuth) {
warnedAuth = true
log.warn("Sending authorization over insecure channel.")
}

var headers = { "accept" : "application/json" }
if (auth) headers.authorization = "Basic " + auth
if (what) {
Expand All @@ -101,6 +103,7 @@ function request (method, where, what, etag, nofollow, cb_) {
} else {
headers["content-length"] = 0
}

if (etag) {
log.verbose(etag, "etag")
headers[method === "GET" ? "if-none-match" : "if-match"] = etag
Expand Down Expand Up @@ -132,14 +135,14 @@ function request (method, where, what, etag, nofollow, cb_) {

var req = (opts.secure ? https : http).request(opts, function (response) {
log.verbose(where, "response")
// if (response.statusCode !== 200) return cb(new Error(
// "Status code " + response.statusCode + " from PUT "+where))

var data = ""
response.on("error", log.er(cb, "response error from "+where))
response.on("data", function (chunk) {
log.silly(chunk+"", "chunk")
data += chunk
})

global.newloctimeout = global.newloctimeout || 0
response.on("end", function () {
if (!nofollow
Expand All @@ -157,6 +160,7 @@ function request (method, where, what, etag, nofollow, cb_) {
log.er(cb, "Failed to fetch "+newLoc))
}, 1000*(global.newloctimeout ++))
}

var parsed
if (response.statusCode !== 304) {
try {
Expand All @@ -168,14 +172,17 @@ function request (method, where, what, etag, nofollow, cb_) {
return cb(ex, null, data, response)
}
}

var er = null
if (parsed && response.headers.etag) {
parsed._etag = response.headers.etag
}

if (parsed && parsed.error) {
var w = url.parse(where).pathname.substr(1)
, name = w.split("/")[0]
if (parsed.error === "not_found") {
w = w.split("/")
name = w[w.indexOf("_rewrite") + 1]
er = new Error("404 Not Found: "+name)
er.errno = npm.E404
er.pkgid = name
Expand All @@ -184,6 +191,7 @@ function request (method, where, what, etag, nofollow, cb_) {
parsed.error + " " + (parsed.reason || "") + ": " + w)
}
} else if (method !== "HEAD" && method !== "GET") {

// invalidate cache
// This is irrelevant for commands that do etag caching, but
// ls and view also have a timed cache, so this keeps the user
Expand Down

0 comments on commit 8923b80

Please sign in to comment.