Skip to content

Commit

Permalink
refactor maybe* methods to be more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
othiym23 committed May 8, 2014
1 parent ff35ed6 commit 235002d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 75 deletions.
80 changes: 38 additions & 42 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function read (name, ver, forceBypass, cb) {
var jsonFile = path.join(npm.cache, name, ver, "package", "package.json")
function c (er, data) {
if (data) deprCheck(data)

return cb(er, data)
}

Expand All @@ -140,7 +141,9 @@ function read (name, ver, forceBypass, cb) {
er = needVersion(er, data)
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
if (er) return addNamed(name, ver, null, c)

deprCheck(data)

c(er, data)
})
}
Expand Down Expand Up @@ -251,15 +254,27 @@ function add (args, cb) {
// in that case, we will not have a protocol now, but if we
// split and check, we will.
if (!name && !p.protocol) {
if (spec.indexOf("/") !== -1 ||
process.platform === "win32" && spec.indexOf("\\") !== -1) {
return maybeFile(spec, p, cb)
} else if (spec.indexOf("@") !== -1) {
return maybeAt(spec, cb)
return maybeFile(spec, cb)
}
else {
switch (p.protocol) {
case "http:":
case "https:":
return addRemoteTarball(spec, null, name, "", cb)

default:
if (isGitUrl(p)) return addRemoteGit(spec, p, false, cb)

// if we have a name and a spec, then try name@spec
if (name) {
addNamed(name, spec, null, cb)
}
// if not, then try just spec (which may try name@"" if not found)
else {
addLocal(spec, "", cb)
}
}
}

add_(name, spec, p, cb)
}

function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) {
Expand All @@ -284,26 +299,6 @@ function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) {
})
}

function add_ (name, spec, p, cb) {
switch (p.protocol) {
case "http:":
case "https:":
return addRemoteTarball(spec, null, name, "", cb)

default:
if (isGitUrl(p))
return addRemoteGit(spec, p, false, cb)

// if we have a name and a spec, then try name@spec
// if not, then try just spec (which may try name@"" if not found)
if (name) {
addNamed(name, spec, null, cb)
} else {
addLocal(spec, "", cb)
}
}
}

function afterAdd (arg, cb) { return function (er, data) {
if (er || !data || !data.name || !data.version) {
return cb(er, data)
Expand All @@ -319,29 +314,30 @@ function afterAdd (arg, cb) { return function (er, data) {
})
}}

function maybeFile (spec, p, cb) {
function maybeFile (spec, cb) {
// split [email protected] only if name is a valid package name,
// don't split in case of "./[email protected]/" (local path)
fs.stat(spec, function (er) {
if (!er) {
// definitely a local thing
addLocal(spec, "", cb)
} else if (er && spec.indexOf("@") !== -1) {
// bar@baz/loofa
maybeAt(spec, cb)
} else {
// Already know it's not a url, so must be local
addLocal(spec, "", cb)
return addLocal(spec, "", cb)
}

maybeAt(spec, cb)
})
}

function maybeAt (spec, cb) {
var tmp = spec.split("@")

// split [email protected] only if name is a valid package name,
// don't split in case of "./[email protected]/" (local path)
var name = tmp.shift()
spec = tmp.join("@")
return add([name, spec], cb)
if (spec.indexOf("@") !== -1) {
var tmp = spec.split("@")

var name = tmp.shift()
spec = tmp.join("@")
add([name, spec], cb)
} else {
// already know it's not a url, so must be local
addLocal(spec, "", cb)
}
}

function needName(er, data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/add-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function addLocal (p, name, cb_) {
// might be username/project
// in that case, try it as a github url.
if (p.split("/").length === 2) {
return maybeGithub(p, name, er, cb)
return maybeGithub(p, er, cb)
}
return cb(er)
}
Expand Down
31 changes: 2 additions & 29 deletions lib/cache/add-named.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var path = require("path")
, locker = require("../utils/locker.js")
, lock = locker.lock
, unlock = locker.unlock
, maybeGithub = require("./maybe-github.js")
, addRemoteTarball = require("./add-remote-tarball.js")
, addRemoteGit = require("./add-remote-git.js")


module.exports = addNamed
Expand Down Expand Up @@ -62,7 +62,7 @@ function addNameTag (name, tag, data, cb_) {
// might be username/project
// in that case, try it as a github url.
if (er && tag.split("/").length === 2) {
return maybeGithub(tag, name, er, cb_)
return maybeGithub(tag, er, cb_)
}
return cb_(er, data)
}
Expand Down Expand Up @@ -239,33 +239,6 @@ function addNameRange (name, range, data, cb) {
}
}

function maybeGithub (p, name, er, cb) {
var u = "git://github.com/" + p
, up = url.parse(u)
log.info("maybeGithub", "Attempting %s from %s", p, u)

return addRemoteGit(u, up, true, function (er2, data) {
if (er2) {
var upriv = "git+ssh://[email protected]:" + p
, uppriv = url.parse(upriv)

log.info("maybeGithub", "Attempting %s from %s", p, upriv)

return addRemoteGit(upriv, uppriv, false, function (er3, data) {
if (er3) return cb(er)
success(upriv, data)
})
}
success(u, data)
})

function success (u, data) {
data._from = u
data._fromGithub = true
return cb(null, data)
}
}

function installTargetsError (requested, data) {
var targets = Object.keys(data["dist-tags"]).filter(function (f) {
return (data.versions || {}).hasOwnProperty(f)
Expand Down
11 changes: 8 additions & 3 deletions lib/cache/maybe-github.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict';

var log = require("npmlog")
, url = require("url")
var url = require("url")
, assert = require("assert")
, log = require("npmlog")
, addRemoteGit = require("./add-remote-git.js")

module.exports = function maybeGithub (p, name, er, cb) {
module.exports = function maybeGithub (p, er, cb) {
assert(typeof p === "string", "must pass package name")
assert(er instanceof Error, "must include error")
assert(typeof cb === "function", "must pass callback")

var u = "git://github.com/" + p
, up = url.parse(u)
log.info("maybeGithub", "Attempting %s from %s", p, u)
Expand Down
76 changes: 76 additions & 0 deletions test/tap/maybe-github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require("../common-tap.js")
var test = require("tap").test
var npm = require("../../lib/npm.js")

// this is the narrowest way to replace a function in the module cache
var found = true
var remoteGitPath = require.resolve('../../lib/cache/add-remote-git.js')
require("module")._cache[remoteGitPath] = {
id: remoteGitPath,
exports: function stub(_, error, __, cb) {
if (found) {
cb(null, {})
}
else {
cb(error)
}
}
}

// only load maybeGithub now, so it gets the stub from cache
var maybeGithub = require("../../lib/cache/maybe-github.js")

test("should throw with no parameters", function (t) {
t.plan(1)

t.throws(function () {
maybeGithub();
}, "throws when called without parameters")
})

test("should throw with wrong parameter types", function (t) {
t.plan(3)

t.throws(function () {
maybeGithub({}, new Error(), function () {})
}, "expects only a package name")

t.throws(function () {
maybeGithub("npm/xxx-noexist", null, function () {})
}, "expects to be called after a previous check already failed")

t.throws(function () {
maybeGithub("npm/xxx-noexist", new Error(), "ham")
}, "is always async")
})

test("should find an existing package on Github", function (t) {
found = true
npm.load({}, function (error) {
t.notOk(error, "bootstrapping succeeds")
t.doesNotThrow(function () {
maybeGithub("npm/npm", new Error("not on filesystem"), function (error, data) {
t.notOk(error, "no issues in looking things up")
t.ok(data, "received metadata from Github")
t.end()
})
})
})
})

test("shouldn't find a nonexistent package on Github", function (t) {
found = false
npm.load({}, function () {
t.doesNotThrow(function () {
maybeGithub("npm/xxx-noexist", new Error("not on filesystem"), function (error, data) {
t.equal(
error.message,
"not on filesystem",
"passed through original error message"
)
t.notOk(data, "didn't pass any metadata")
t.end()
})
})
})
})

0 comments on commit 235002d

Please sign in to comment.