From 0b8739f5a6a79f65d39dfd21a10420bf984acc68 Mon Sep 17 00:00:00 2001 From: guybedford Date: Tue, 14 Oct 2014 23:46:47 +0200 Subject: [PATCH] locate hook and registry separation completion --- lib/core.js | 13 +++++++------ lib/endpoint.js | 4 +++- lib/package.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/lib/core.js b/lib/core.js index ab3605110..7d3bf0f68 100644 --- a/lib/core.js +++ b/lib/core.js @@ -261,10 +261,15 @@ var install = exports.install = function(name, target, options) { // to be sure we're not overriding something important var oldDep, oldMap, oldVersions; - return Promise.resolve(target) + + return pkg.locate(target, options.override) // if not a primary install, and we have something that satisfies this already, then use that - .then(function() { + // otherwise do the full lookup + .then(function(located) { + target = located.target; + options.override = located.override; + if (options.primary) return; @@ -299,10 +304,6 @@ var install = exports.install = function(name, target, options) { .then(function(_lookup) { lookup = _lookup; - // allow the lookup to provide an override - if (lookup.override && !options.override) - options.override = lookup.override; - if (!options.primary) return; diff --git a/lib/endpoint.js b/lib/endpoint.js index c52a79642..927a87efd 100644 --- a/lib/endpoint.js +++ b/lib/endpoint.js @@ -47,8 +47,10 @@ process.on('exit', function() { METHODS parse (name) -> { package, path } + locate (packageName, versionRange, override) + -> { found: true/false, [override] } / { redirect: 'new:package', [override] } lookup (packageName) - -> { notfound: true } / { redirect: 'new:package' } / { versions: {...} } + -> { notfound: true } / { versions: {...} } download (packageName, version, hash, dir) -> Promise pjson getPackageConfig (packageName, version, hash), optional diff --git a/lib/package.js b/lib/package.js index 0d17ff1dd..fa0343041 100644 --- a/lib/package.js +++ b/lib/package.js @@ -64,6 +64,13 @@ Package.prototype.setVersion = function(version) { this.exactPackage = this.package + v; this.exactName = this.name + v; } +Package.prototype.setEndpoint = function(endpoint) { + if (this.endpoint) + throw 'Endpoint already set.'; + this.endpoint = endpoint; + this.exactName = endpoint + ':' + this.exactName; + this.name = endpoint + ':' + this.name; +} var link = require('./link'); @@ -87,6 +94,39 @@ function extend(a, b) { var _pkg = module.exports; + +// given a name like 'jquery', 'github:repo/thatwasmoved' +// add the default registry endpoint to the name +// so we now have 'jspm:jquery', 'github:repo/thatwasmoved' +// then run the locate hook (if provided) of the endpoint +// following redirects until the locate hook converges +// getting 'github:components/jquery' and 'github:repo/redirected' +// at this point, we have the final name for the target +// return value is { target, override } +exports.locate = function(target, override) { + if (!target.endpoint) + target.setEndpoint(config.globalConfig.registry); + + var endpoint = ep.load(target.endpoint); + + if (!endpoint.locate) + return Promise.resolve({ target: target, override: override }); + + return Promise.resolve(endpoint.locate(target.package, target.version, override)) + .then(function(located) { + if (located.redirect) { + var newTarget = new Package(located.redirect); + newTarget.setVersion(target.version); + return _pkg.locate(newTarget, located.override || override); + } + if (!located.found) + throw 'Repo `' + target.name + '` not found!'; + if (located.found) + return { target: target, override: located.override || override }; + throw 'Invalid endpoint locate response for %' + target.endpoint + '%'; + }); +} + // lookup the latest suitable version for this package range // if the range is a tag or prerelease, then it is just that // if the range is compatible with a prerelease, then it is just that @@ -110,11 +150,12 @@ exports.lookup = function(pkg) { return lookupCache[pkg.package] = Promise.resolve(ep.load(pkg.endpoint).lookup(pkg.package)); }) .then(function(lookup) { - if (lookup.redirect) - return exports.lookup(new Package(lookup.redirect)); if (lookup.notfound) throw 'Repo `' + pkg.package + '` not found!'; + if (!lookup.versions) + throw 'Invalid endpoint lookup response for %' + pkg.endpoint + '%'; + return _pkg.getVersionMatch(pkg, lookup.versions); }) // return the lookup @@ -124,8 +165,6 @@ exports.lookup = function(pkg) { var lookup = new Package(pkg.name + '@' + lookupObj.version); lookup.hash = lookupObj.hash; - if (lookupObj.override) - lookup.override = lookupObj.override; return lookup; }); }