summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-registry-client/lib/request.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/npm-registry-client/lib/request.js')
-rw-r--r--deps/npm/node_modules/npm-registry-client/lib/request.js196
1 files changed, 91 insertions, 105 deletions
diff --git a/deps/npm/node_modules/npm-registry-client/lib/request.js b/deps/npm/node_modules/npm-registry-client/lib/request.js
index 910fe0131..fa969ac22 100644
--- a/deps/npm/node_modules/npm-registry-client/lib/request.js
+++ b/deps/npm/node_modules/npm-registry-client/lib/request.js
@@ -1,105 +1,72 @@
-var assert = require("assert")
- , url = require("url")
- , zlib = require("zlib")
- , Stream = require("stream").Stream
-
-var rm = require("rimraf")
- , request = require("request")
- , once = require("once")
-
module.exports = regRequest
// npm: means
// 1. https
// 2. send authorization
// 3. content-type is 'application/json' -- metadata
-function regRequest (method, uri, options, cb_) {
- assert(uri, "must pass resource to load")
- assert(cb_, "must pass callback")
+//
+var assert = require("assert")
+ , url = require("url")
+ , zlib = require("zlib")
+ , Stream = require("stream").Stream
- options = options || {}
+var request = require("request")
+ , once = require("once")
- var parsed = url.parse(uri)
- var where = parsed.pathname
- var what = options.body
- var follow = (typeof options.follow === "boolean" ? options.follow : true)
- this.log.verbose("request", "on initialization, where is", where)
-
- if (parsed.search) {
- where = where + parsed.search
- parsed.search = ""
- }
- parsed.pathname = "/"
- this.log.verbose("request", "after pass 1, where is", where)
+function regRequest (uri, params, cb_) {
+ assert(typeof uri === "string", "must pass uri to request")
+ assert(params && typeof params === "object", "must pass params to request")
+ assert(typeof cb_ === "function", "must pass callback to request")
+
+ params.method = params.method || "GET"
+ this.log.verbose("request", "uri", uri)
// Since there are multiple places where an error could occur,
// don't let the cb be called more than once.
var cb = once(cb_)
- if (where.match(/^\/?favicon.ico/)) {
+ if (uri.match(/^\/?favicon.ico/)) {
return cb(new Error("favicon.ico isn't a package, it's a picture."))
}
- var adduserChange = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)\/-rev/
- , isUserChange = where.match(adduserChange)
- , adduserNew = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)$/
- , isNewUser = where.match(adduserNew)
- , registry = url.format(parsed)
- , alwaysAuth = this.conf.getCredentialsByURI(registry).alwaysAuth
- , isDelete = method === "DELETE"
- , isWrite = what || isDelete
+ var adduserChange = /\/?-\/user\/org\.couchdb\.user:([^/]+)\/-rev/
+ , isUserChange = uri.match(adduserChange)
+ , adduserNew = /\/?-\/user\/org\.couchdb\.user:([^/?]+)$/
+ , isNewUser = uri.match(adduserNew)
+ , alwaysAuth = params.auth && params.auth.alwaysAuth
+ , isDelete = params.method === "DELETE"
+ , isWrite = params.body || isDelete
if (isUserChange && !isWrite) {
return cb(new Error("trying to change user document without writing(?!)"))
}
- // resolve to a full url on the registry
- if (!where.match(/^https?:\/\//)) {
- this.log.verbose("request", "url raw", where)
-
- var q = where.split("?")
- where = q.shift()
- q = q.join("?")
-
- if (where.charAt(0) !== "/") where = "/" + where
- where = "." + where.split("/").map(function (p) {
- p = p.trim()
- if (p.match(/^org.couchdb.user/)) {
- return p.replace(/\//g, encodeURIComponent("/"))
- }
- return p
- }).join("/")
- if (q) where += "?" + q
-
- this.log.verbose("request", "resolving registry", [registry, where])
- where = url.resolve(registry, where)
- this.log.verbose("request", "after pass 2, where is", where)
- }
-
- var authed
// new users can *not* use auth, because they don't *have* auth yet
- if (isNewUser) {
+ if (isUserChange) {
+ this.log.verbose("request", "updating existing user; sending authorization")
+ params.authed = true
+ }
+ else if (isNewUser) {
this.log.verbose("request", "new user, so can't send auth")
- authed = false
+ params.authed = false
}
else if (alwaysAuth) {
this.log.verbose("request", "always-auth set; sending authorization")
- authed = true
+ params.authed = true
}
else if (isWrite) {
this.log.verbose("request", "sending authorization for write operation")
- authed = true
+ params.authed = true
}
else {
// most of the time we don't want to auth
this.log.verbose("request", "no auth needed")
- authed = false
+ params.authed = false
}
var self = this
this.attempt(function (operation) {
- makeRequest.call(self, method, where, what, options.etag, follow, authed
- , function (er, parsed, raw, response) {
+ makeRequest.call(self, uri, params, function (er, parsed, raw, response) {
if (!er || (er.message && er.message.match(/^SSL Error/))) {
if (er)
er.code = "ESSL"
@@ -127,51 +94,64 @@ function regRequest (method, uri, options, cb_) {
})
}
-function makeRequest (method, where, what, etag, follow, authed, cb_) {
+function makeRequest (uri, params, cb_) {
var cb = once(cb_)
- var parsed = url.parse(where)
+ var parsed = url.parse(uri)
var headers = {}
// metadata should be compressed
headers["accept-encoding"] = "gzip"
- var er = this.authify(authed, parsed, headers)
+ var er = this.authify(params.authed, parsed, headers, params.auth)
if (er) return cb_(er)
var opts = this.initialize(
parsed,
- method,
+ params.method,
"application/json",
headers
)
- opts.followRedirect = follow
+ opts.followRedirect = (typeof params.follow === "boolean" ? params.follow : true)
opts.encoding = null // tell request let body be Buffer instance
- if (etag) {
- this.log.verbose("etag", etag)
- headers[method === "GET" ? "if-none-match" : "if-match"] = etag
+ if (params.etag) {
+ this.log.verbose("etag", params.etag)
+ headers[params.method === "GET" ? "if-none-match" : "if-match"] = params.etag
+ }
+
+ if (params.lastModified && params.method === "GET") {
+ this.log.verbose("lastModified", params.lastModified)
+ headers["if-modified-since"] = params.lastModified;
}
- // figure out wth "what" is
- if (what) {
- if (Buffer.isBuffer(what) || typeof what === "string") {
- opts.body = what
+ // figure out wth body is
+ if (params.body) {
+ if (Buffer.isBuffer(params.body)) {
+ opts.body = params.body
headers["content-type"] = "application/json"
- headers["content-length"] = Buffer.byteLength(what)
- } else if (what instanceof Stream) {
+ headers["content-length"] = params.body.length
+ }
+ else if (typeof params.body === "string") {
+ opts.body = params.body
+ headers["content-type"] = "application/json"
+ headers["content-length"] = Buffer.byteLength(params.body)
+ }
+ else if (params.body instanceof Stream) {
headers["content-type"] = "application/octet-stream"
- if (what.size) headers["content-length"] = what.size
- } else {
- delete what._etag
- opts.json = what
+ if (params.body.size) headers["content-length"] = params.body.size
+ }
+ else {
+ delete params.body._etag
+ delete params.body._lastModified
+ opts.json = params.body
}
}
- this.log.http("request", method, parsed.href || "/")
+ this.log.http("request", params.method, parsed.href || "/")
- var done = requestDone.call(this, method, where, cb)
+ var done = requestDone.call(this, params.method, uri, cb)
var req = request(opts, decodeResponseBody(done))
req.on("error", cb)
@@ -179,8 +159,8 @@ function makeRequest (method, where, what, etag, follow, authed, cb_) {
s.on("error", cb)
})
- if (what && (what instanceof Stream)) {
- what.pipe(req)
+ if (params.body && (params.body instanceof Stream)) {
+ params.body.pipe(req)
}
}
@@ -194,7 +174,9 @@ function decodeResponseBody(cb) {
response.socket.destroy()
}
- if (response.headers["content-encoding"] !== "gzip") return cb(er, response, data)
+ if (response.headers["content-encoding"] !== "gzip") {
+ return cb(er, response, data)
+ }
zlib.gunzip(data, function (er, buf) {
if (er) return cb(er, response, data)
@@ -210,16 +192,14 @@ function requestDone (method, where, cb) {
if (er) return cb(er)
var urlObj = url.parse(where)
- if (urlObj.auth)
- urlObj.auth = "***"
+ if (urlObj.auth) urlObj.auth = "***"
this.log.http(response.statusCode, url.format(urlObj))
- var parsed
-
if (Buffer.isBuffer(data)) {
data = data.toString()
}
+ var parsed
if (data && typeof data === "string" && response.statusCode !== 304) {
try {
parsed = JSON.parse(data)
@@ -246,7 +226,13 @@ function requestDone (method, where, cb) {
parsed._etag = response.headers.etag
}
- if (parsed && parsed.error && response.statusCode >= 400) {
+ if (parsed && response.headers['last-modified']) {
+ parsed._lastModified = response.headers['last-modified']
+ }
+
+ // for the search endpoint, the "error" property can be an object
+ if (parsed && parsed.error && typeof parsed.error !== "object" ||
+ response.statusCode >= 400) {
var w = url.parse(where).pathname.substr(1)
var name
if (!w.match(/^-/)) {
@@ -254,24 +240,24 @@ function requestDone (method, where, cb) {
name = w[w.indexOf("_rewrite") + 1]
}
- if (name && parsed.error === "not_found") {
+ if (!parsed.error) {
+ er = new Error(
+ "Registry returned " + response.statusCode +
+ " for " + method +
+ " on " + where
+ )
+ }
+ else if (name && parsed.error === "not_found") {
er = new Error("404 Not Found: " + name)
- } else {
+ }
+ else {
er = new Error(
- parsed.error + " " + (parsed.reason || "") + ": " + w)
+ parsed.error + " " + (parsed.reason || "") + ": " + w
+ )
}
if (name) er.pkgid = name
er.statusCode = response.statusCode
er.code = "E" + er.statusCode
-
- } 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
- // from thinking that it didn't work when it did.
- // Note that failure is an acceptable option here, since the
- // only result will be a stale cache for some helper commands.
- rm(this.cacheFile(where), function() {})
}
return cb(er, parsed, data, response)
}.bind(this)