Permalink
Browse files

Use npm-registry-client dep

  • Loading branch information...
1 parent b194c5e commit 82fd860e65c7ddbba0ad562908f9f4a402d3ca65 @isaacs isaacs committed Jun 7, 2012
View
@@ -1,10 +1,10 @@
module.exports = adduser
-var registry = require("./utils/npm-registry-client/index.js")
- , ini = require("./utils/ini.js")
+var ini = require("./utils/ini.js")
, log = require("npmlog")
, npm = require("./npm.js")
+ , registry = npm.registry
@Raynos

Raynos Oct 1, 2012

Contributor

npm.registry is only a valid value after npm.load is called.

So code like

var search = require("npm/lib/search")
    , npm = require("npm")

npm.load(function () { npm.search(...) })

fails

@isaacs

isaacs Oct 2, 2012

Owner

Yeah. Don't do that.

@Raynos

Raynos Oct 2, 2012

Contributor

@isaacs the point is requiring is blocking and should be done at startup time. Yet I can't require this file at startup time.

That's a massive WTF

@leecookson

leecookson Oct 2, 2012

The npm object that is ready to use is passed to the npm.load callback:
function (err, npm) { npm.commands.search(...) }
Also, the commands within npm are lazy-loaded into the "npm,commands" object. Digging into a package like "npm/lib/search" is generally a bad idea. I've integrated a lot of npm functionality into a command line/release tool and use all of the npm functionality through npm.commands.*

@isaacs

isaacs Oct 3, 2012

Owner

@Raynos Get out of npm's underwear! Run npm.load(fn) and only use its commands via npm.commands.whatever after it's loaded its stuff.

@Raynos

Raynos Oct 3, 2012

Contributor

@isaacs it was kind of documented. Maybe I should have read it and done it that way.

@isaacs

isaacs Oct 3, 2012

Owner

@Raynos In your defense, it's not exactly a typical pattern, or documented very well.

If you're digging into npm's guts, it's a good sign that some part of it should be gutted. In an ideal world, no program would ever list npm as a dependency.

, read = require("read")
, promiseChain = require("./utils/promise-chain.js")
, crypto
@@ -39,6 +39,9 @@ function adduser (args, cb) {
if (changed) npm.config.del("_auth")
registry.adduser(u.u, u.p, u.e, function (er) {
if (er) return cb(er)
+ registry.username = u.u
+ registry.password = u.p
+ registry.email = u.e
ini.set("username", u.u, "user")
ini.set("_password", u.p, "user")
ini.set("email", u.e, "user")
View
@@ -3,22 +3,22 @@ module.exports = bugs
bugs.usage = "npm bugs <pkgname>"
+var exec = require("./utils/exec.js")
+ , npm = require("./npm.js")
+ , registry = npm.registry
+ , log = require("npmlog")
+
bugs.completion = function (opts, cb) {
if (opts.conf.argv.remain.length > 2) return cb()
- registry.get("/-/short", null, 60000, function (er, list) {
+ registry.get("/-/short", 60000, function (er, list) {
return cb(null, list || [])
})
}
-var exec = require("./utils/exec.js")
- , registry = require("./utils/npm-registry-client/index.js")
- , npm = require("./npm.js")
- , log = require("npmlog")
-
function bugs (args, cb) {
if (!args.length) return cb(bugs.usage)
var n = args[0].split("@").shift()
- registry.get(n, "latest", 3600, function (er, d) {
+ registry.get(n + "/latest", 3600, function (er, d) {
if (er) return cb(er)
var bugs = d.bugs
, repo = d.repository || d.repositories
View
@@ -13,7 +13,7 @@ Adding a url:
2. goto folder(2)
adding a name@version:
-1. registry.get(name, version)
+1. registry.get(name/version)
2. if response isn't 304, add url(dist.tarball)
adding a name@range:
@@ -38,7 +38,7 @@ var mkdir = require("mkdirp")
, fs = require("graceful-fs")
, rm = require("rimraf")
, readJson = require("./utils/read-json.js")
- , registry = require("./utils/npm-registry-client/index.js")
+ , registry = npm.registry
, log = require("npmlog")
, path = require("path")
, output
@@ -520,7 +520,7 @@ function addNameVersion (name, ver, data, cb) {
response = null
return next()
}
- registry.get(name, ver, function (er, d, json, resp) {
+ registry.get(name + "/" + ver, function (er, d, json, resp) {
if (er) return cb(er)
data = d
response = resp
View
@@ -18,9 +18,9 @@ deprecate.completion = function (opts, cb) {
})
}
-var registry = require("./utils/npm-registry-client/index.js")
- , semver = require("semver")
+var semver = require("semver")
, npm = require("./npm.js")
+ , registry = npm.registry
function deprecate (args, cb) {
var pkg = args[0]
@@ -42,6 +42,6 @@ function deprecate (args, cb) {
data.versions[v].deprecated = msg
})
// now update the doc on the registry
- registry.request.PUT(data._id, data, cb)
+ registry.request('PUT', data._id, data, cb)
})
}
View
@@ -5,20 +5,20 @@ docs.usage = "npm docs <pkgname>"
docs.completion = function (opts, cb) {
if (opts.conf.argv.remain.length > 2) return cb()
- registry.get("/-/short", null, 60000, function (er, list) {
+ registry.get("/-/short", 60000, function (er, list) {
return cb(null, list || [])
})
}
var exec = require("./utils/exec.js")
- , registry = require("./utils/npm-registry-client/index.js")
, npm = require("./npm.js")
+ , registry = npm.registry
, log = require("npmlog")
function docs (args, cb) {
if (!args.length) return cb(docs.usage)
var n = args[0].split("@").shift()
- registry.get(n, "latest", 3600, function (er, d) {
+ registry.get(n + "/latest", 3600, function (er, d) {
if (er) return cb(er)
var homepage = d.homepage
, repo = d.repository || d.repositories
View
@@ -30,7 +30,7 @@ install.completion = function (opts, cb) {
// if it has a slash, then it's gotta be a folder
// if it starts with https?://, then just give up, because it's a url
// for now, not yet implemented.
- var registry = require("./utils/npm-registry-client/index.js")
+ var registry = npm.registry
registry.get("/-/short", function (er, pkgs) {
if (er) return cb()
if (!opts.partialWord) return cb(null, pkgs)
View
@@ -29,6 +29,7 @@ var EventEmitter = require("events").EventEmitter
, mkdirp = require("mkdirp")
, slide = require("slide")
, chain = slide.chain
+ , RegClient = require("npm-registry-client")
// /usr/local is often a read-only fs, which is not
// well handled by node or mkdirp. Just double-check
@@ -276,9 +277,25 @@ function load (npm, conf, cb) {
}
log.resume()
- //console.error("back from config lookup", er && er.stack)
if (er) return cb(er)
+ // at this point the configs are all set.
+ // go ahead and spin up the registry client.
+ npm.registry = new RegClient(
+ { registry: npm.config.get("registry")
+ , cache: npm.config.get("cache")
+ , auth: npm.config.get("_auth")
+ , alwaysAuth: npm.config.get("always-auth")
+ , email: npm.config.get("email")
+ , tag: npm.config.get("tag")
+ , ca: npm.config.get("ca")
+ , strictSSL: npm.config.get("strict-ssl")
+ , userAgent: npm.config.get("user-agent")
+ , E404: npm.E404
+ , EPUBLISHCONFLICT: npm.EPUBLISHCONFLICT
+ , log: log
+ })
+
var umask = parseInt(conf.umask, 8)
npm.modes = { exec: 0777 & (~umask)
, file: 0666 & (~umask)
View
@@ -64,12 +64,10 @@ owner.completion = function (opts, cb) {
}
}
-var registry = require("./utils/npm-registry-client/index.js")
- , get = registry.request.GET
- , put = registry.request.PUT
+var npm = require("./npm.js")
+ , registry = npm.registry
, log = require("npmlog")
, output
- , npm = require("./npm.js")
function owner (args, cb) {
var action = args.shift()
@@ -83,7 +81,7 @@ function owner (args, cb) {
function ls (pkg, cb) {
if (!pkg) return cb(owner.usage)
- get(pkg, function (er, data) {
+ registry.get(pkg, function (er, data) {
var msg = ""
if (er) {
log.error("owner ls", "Couldn't get owner data", pkg)
@@ -147,7 +145,7 @@ function rm (user, pkg, cb) {
function mutate (pkg, user, mutation, cb) {
if (user) {
- get("/-/user/org.couchdb.user:"+user, mutate_)
+ registry.get("/-/user/org.couchdb.user:"+user, mutate_)
} else {
mutate_(null, null)
}
@@ -162,7 +160,7 @@ function mutate (pkg, user, mutation, cb) {
}
if (u) u = { "name" : u.name, "email" : u.email }
- get("/"+pkg, function (er, data) {
+ registry.get(pkg, function (er, data) {
if (er) {
log.error("owner mutate", "Error getting package data for %s", pkg)
return cb(er)
@@ -174,7 +172,9 @@ function mutate (pkg, user, mutation, cb) {
, _rev : data._rev
, maintainers : m
}
- put("/"+pkg+"/-rev/"+data._rev, data, function (er, data) {
+ registry.request("PUT"
+ , pkg+"/-rev/"+data._rev, data
+ , function (er, data) {
if (!er && data.error) er = new Error(
"Failed to update package metadata: "+JSON.stringify(data))
if (er) {
View
@@ -2,7 +2,7 @@
module.exports = publish
var npm = require("./npm.js")
- , registry = require("./utils/npm-registry-client/index.js")
+ , registry = npm.registry
, log = require("npmlog")
, tar = require("./utils/tar.js")
, sha = require("./utils/sha.js")
@@ -151,9 +151,12 @@ function regPublish (data, prebuilt, isRetry, arg, cachedir, cb) {
// check to see if there's a README.md in there.
var readme = path.resolve(cachedir, "README.md")
+ , tarball = cachedir + ".tgz"
+
fs.readFile(readme, function (er, readme) {
// ignore error. it's an optional feature
- registry.publish(data, prebuilt, readme, function (er) {
+
+ registry.publish(data, tarball, readme, function (er) {
if (er && er.errno === npm.EPUBLISHCONFLICT
&& npm.config.get("force") && !isRetry) {
log.warn("publish", "Forced publish over "+data._id)
View
@@ -2,7 +2,7 @@
module.exports = exports = search
var npm = require("./npm.js")
- , registry = require("./utils/npm-registry-client/index.js")
+ , registry = npm.registry
, semver = require("semver")
, output
@@ -59,7 +59,7 @@ function search (args, silent, staleness, cb_) {
}
function getFilteredData (staleness, args, notArgs, cb) {
- registry.get( "/-/all", null, staleness, false
+ registry.get( "/-/all", staleness, false
, true, function (er, data) {
if (er) return cb(er)
return cb(null, filter(data, args, notArgs))
View
@@ -2,7 +2,7 @@
module.exports = star
var npm = require("./npm.js")
- , registry = require("./utils/npm-registry-client/index.js")
+ , registry = npm.registry
, log = require("npmlog")
, asyncMap = require("slide").asyncMap
, output = require("./utils/output.js")
@@ -11,7 +11,7 @@ star.usage = "npm star <package> [pkg, pkg, ...]\n"
+ "npm unstar <package> [pkg, pkg, ...]"
star.completion = function (opts, cb) {
- registry.get("/-/short", null, 60000, function (er, list) {
+ registry.get("/-/short", 60000, function (er, list) {
return cb(null, list || [])
})
}
View
@@ -6,12 +6,13 @@ tag.usage = "npm tag <project>@<version> [<tag>]"
tag.completion = require("./unpublish.js").completion
var npm = require("./npm.js")
+ , registry = npm.registry
function tag (args, cb) {
var thing = (args.shift() || "").split("@")
, project = thing.shift()
, version = thing.join("@")
, t = args.shift() || npm.config.get("tag")
if (!project || !version || !t) return cb("Usage:\n"+tag.usage)
- require("./utils/npm-registry-client/index.js").tag(project, version, t, cb)
+ registry.tag(project, version, t, cb)
}
View
@@ -1,9 +1,9 @@
module.exports = unpublish
-var registry = require("./utils/npm-registry-client/index.js")
- , log = require("npmlog")
+var log = require("npmlog")
, npm = require("./npm.js")
+ , registry = npm.registry
, readJson = require("./utils/read-json.js")
, path = require("path")
, output = require("./utils/output.js")
@@ -1,7 +1,8 @@
module.exports = remotePackages
-var registry = require("../npm-registry-client/index.js")
+var npm = require("../../npm.js")
+ , registry = npm.registry
, containsSingleMatch = require("./contains-single-match.js")
, getCompletions = require("./get-completions.js")
@@ -25,7 +26,7 @@ function remotePackages (args, index, doVersion, doTag
if (name === undefined) name = ""
if (name.indexOf("/") !== -1) return cb(null, [])
// use up-to 1 hour stale cache. not super urgent.
- registry.get("/", null, 3600, function (er, d) {
+ registry.get("/", 3600, function (er, d) {
if (er) return cb(er)
var remoteList = Object.keys(d)
, found = remoteList.indexOf(name)
@@ -1,7 +1,8 @@
module.exports = users
-var registry = require("../npm-registry-client/index.js")
+var npm = require("../../npm")
+ , registry = npm.registry
, containsSingleMatch = require("./contains-single-match.js")
, getCompletions = require("./get-completions.js")
, log = require("npmlog")
@@ -11,7 +12,7 @@ function users (args, index, cb) {
if (name === undefined) name = ""
// use up-to 1 day stale cache. doesn't change much
log.warn("users completion", "About to fetch")
- registry.get("/-/users", null, 24*60*60, function (er, d) {
+ registry.get("/-/users", 24*60*60, function (er, d) {
log.warn("userdata", d)
log.warn("name", name)
if (er) return cb(er)
Oops, something went wrong.

0 comments on commit 82fd860

Please sign in to comment.