diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2015-03-25 22:41:44 +0300 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2015-03-25 22:41:44 +0300 |
commit | f35aa933e136228a89e3fcfdebe8c7cc4f1e7c00 (patch) | |
tree | 3207206d4ebd5c8d5c1986f215acd961292f6df6 /lib | |
parent | c910399ecfd8db49fe4496dd26887765a8aed20f (diff) |
search: request correct URLs (fixes #7443)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cache/update-index.js | 124 | ||||
-rw-r--r-- | lib/search.js | 30 |
2 files changed, 76 insertions, 78 deletions
diff --git a/lib/cache/update-index.js b/lib/cache/update-index.js index 2955f6a14..ab1ede120 100644 --- a/lib/cache/update-index.js +++ b/lib/cache/update-index.js @@ -1,82 +1,86 @@ module.exports = updateIndex -var fs = require("graceful-fs") - , assert = require("assert") - , path = require("path") - , mkdir = require("mkdirp") - , chownr = require("chownr") - , url = require("url") - , npm = require("../npm.js") - , log = require("npmlog") - , cacheFile = require("npm-cache-filename") - , getCacheStat = require("./get-stat.js") +var fs = require('graceful-fs') +var assert = require('assert') +var path = require('path') +var mkdir = require('mkdirp') +var chownr = require('chownr') +var npm = require('../npm.js') +var log = require('npmlog') +var cacheFile = require('npm-cache-filename') +var getCacheStat = require('./get-stat.js') +var mapToRegistry = require('../utils/map-to-registry.js') /* /-/all is special. * It uses timestamp-based caching and partial updates, * because it is a monster. */ -function updateIndex (uri, params, cb) { - assert(typeof uri === "string", "must pass registry URI to updateIndex") - assert(params && typeof params === "object", "must pass params to updateIndex") - assert(typeof cb === "function", "must pass callback to updateIndex") - - var parsed = url.parse(uri) - assert( - parsed.protocol === "http:" || parsed.protocol === "https:", - "must have a URL that starts with http: or https:" - ) - - var cacheBase = cacheFile(npm.config.get("cache"))(uri) - var cachePath = path.join(cacheBase, ".cache.json") - log.info("updateIndex", cachePath) - - getCacheStat(function (er, st) { +function updateIndex (staleness, cb) { + assert(typeof cb === 'function', 'must pass callback to updateIndex') + + mapToRegistry('-/all', npm.config, function (er, uri, auth) { if (er) return cb(er) - mkdir(cacheBase, function (er, made) { - if (er) return cb(er) + var params = { + timeout: staleness, + follow: true, + staleOk: true, + auth: auth + } + var cacheBase = cacheFile(npm.config.get('cache'))(uri) + var cachePath = path.join(cacheBase, '.cache.json') + log.info('updateIndex', cachePath) - fs.readFile(cachePath, function (er, data) { - if (er) return updateIndex_(uri, params, 0, {}, cachePath, cb) + getCacheStat(function (er, st) { + if (er) return cb(er) - try { - data = JSON.parse(data) - } - catch (ex) { - fs.writeFile(cachePath, "{}", function (er) { - if (er) return cb(new Error("Broken cache.")) + mkdir(cacheBase, function (er, made) { + if (er) return cb(er) - return updateIndex_(uri, params, 0, {}, cachePath, cb) + fs.readFile(cachePath, function (er, data) { + if (er) { + log.warn('', 'Building the local index for the first time, please be patient') + return updateIndex_(uri, params, {}, cachePath, cb) + } + + chownr(made || cachePath, st.uid, st.gid, function (er) { + if (er) return cb(er) + + try { + data = JSON.parse(data) + } catch (ex) { + fs.writeFile(cachePath, '{}', function (er) { + if (er) return cb(new Error('Broken cache.')) + + log.warn('', 'Building the local index for the first time, please be patient') + return updateIndex_(uri, params, {}, cachePath, cb) + }) + } + + var t = +data._updated || 0 + // use the cache and update in the background if it's not too old + if (Date.now() - t < 60000) { + cb(null, data) + cb = function () {} + } + + if (t === 0) { + log.warn('', 'Building the local index for the first time, please be patient') + } else { + log.verbose('updateIndex', 'Cached search data present with timestamp', t) + uri += '/since?stale=update_after&startkey=' + t + } + updateIndex_(uri, params, data, cachePath, cb) }) - } - var t = +data._updated || 0 - chownr(made || cachePath, st.uid, st.gid, function (er) { - if (er) return cb(er) - - updateIndex_(uri, params, t, data, cachePath, cb) }) }) }) }) } -function updateIndex_ (uri, params, t, data, cachePath, cb) { - // use the cache and update in the background if it's not too old - if (Date.now() - t < 60000) { - cb(null, data) - cb = function () {} - } - - var full - if (t === 0) { - log.warn("", "Building the local index for the first time, please be patient") - full = url.resolve(uri, "/-/all") - } - else { - full = url.resolve(uri, "/-/all/since?stale=update_after&startkey=" + t) - } - - npm.registry.request(full, params, function (er, updates, _, res) { +function updateIndex_ (all, params, data, cachePath, cb) { + log.silly('update-index', 'fetching', all) + npm.registry.request(all, params, function (er, updates, _, res) { if (er) return cb(er, data) var headers = res.headers diff --git a/lib/search.js b/lib/search.js index ad3f312e5..840bc2f6b 100644 --- a/lib/search.js +++ b/lib/search.js @@ -3,7 +3,6 @@ module.exports = exports = search var npm = require("./npm.js") , columnify = require("columnify") - , mapToRegistry = require("./utils/map-to-registry.js") , updateIndex = require("./cache/update-index.js") search.usage = "npm search [some search terms ...]" @@ -35,19 +34,24 @@ function search (args, silent, staleness, cb) { if (typeof cb !== "function") cb = silent, silent = false var searchopts = npm.config.get("searchopts") - , searchexclude = npm.config.get("searchexclude") + var searchexclude = npm.config.get("searchexclude") + if (typeof searchopts !== "string") searchopts = "" searchopts = searchopts.split(/\s+/) - if (typeof searchexclude === "string") { - searchexclude = searchexclude.split(/\s+/) - } else searchexclude = [] var opts = searchopts.concat(args).map(function (s) { return s.toLowerCase() }).filter(function (s) { return s }) + + if (typeof searchexclude === "string") { + searchexclude = searchexclude.split(/\s+/) + } else { + searchexclude = [] + } searchexclude = searchexclude.map(function (s) { return s.toLowerCase() }) - getFilteredData( staleness, opts, searchexclude, function (er, data) { + + getFilteredData(staleness, opts, searchexclude, function (er, data) { // now data is the list of data that we want to show. // prettify and print it, and then provide the raw // data to the cb. @@ -58,19 +62,9 @@ function search (args, silent, staleness, cb) { } function getFilteredData (staleness, args, notArgs, cb) { - mapToRegistry("-/all", npm.config, function (er, uri, auth) { + updateIndex(staleness, function (er, data) { if (er) return cb(er) - - var params = { - timeout : staleness, - follow : true, - staleOk : true, - auth : auth - } - updateIndex(uri, params, function (er, data) { - if (er) return cb(er) - return cb(null, filter(data, args, notArgs)) - }) + return cb(null, filter(data, args, notArgs)) }) } |