Skip to content
Browse files

fix fetching of /-/all

  • Loading branch information...
1 parent 56d435e commit b78aa86cb2decb1d1b85410ed3b91d5ff967c1a4 @othiym23 othiym23 committed May 22, 2014
Showing with 37 additions and 11 deletions.
  1. +20 −10 lib/get.js
  2. +1 −1 lib/request.js
  3. +16 −0 test/get-all.js
View
30 lib/get.js
@@ -8,7 +8,15 @@ var fs = require("graceful-fs")
, chownr = require("chownr")
, url = require("url")
-// function get (uri, timeout, nofollow, staleOk, cb) {
+/**
+ * options:
+ *
+ * timeout: request timeouts
+ * follow: follow redirects
+ * staleOk: stale results are OK
+ * stat: results of checking for cached metadata
+ * data: the cached metadata
+ */
function get (uri, options, cb) {
assert(uri, "must have URL to fetch")
assert(cb, "must have callback")
@@ -20,7 +28,7 @@ function get (uri, options, cb) {
// It uses timestamp-based caching and partial updates,
// because it is a monster.
if (url.parse(uri).pathname === "/-/all") {
- return requestAll.call(this, cache, cb)
+ return requestAll.call(this, uri, cache, cb)
}
// If the GET is part of a write operation (PUT or DELETE), then
@@ -43,37 +51,39 @@ function get (uri, options, cb) {
}.bind(this))
}
-function requestAll (cache, cb) {
+function requestAll (uri, cache, cb) {
+ this.log.info("get", cache)
mkdir(path.dirname(cache), function (er) {
if (er) return cb(er)
fs.readFile(cache, function (er, data) {
- if (er) return requestAll_.call(this, 0, {}, cache, cb)
+ if (er) return requestAll_.call(this, uri, 0, {}, cache, cb)
try {
data = JSON.parse(data)
} catch (ex) {
fs.writeFile(cache, "{}", function (er) {
if (er) return cb(new Error("Broken cache."))
- return requestAll_.call(this, 0, {}, cache, cb)
+ return requestAll_.call(this, uri, 0, {}, cache, cb)
}.bind(this))
}
var t = +data._updated || 0
- requestAll_.call(this, t, data, cache, cb)
+ requestAll_.call(this, uri, t, data, cache, cb)
}.bind(this))
}.bind(this))
}
-function requestAll_ (c, data, cache, cb) {
+function requestAll_ (uri, c, data, cache, cb) {
// use the cache and update in the background if it's not too old
if (Date.now() - c < 60000) {
cb(null, data)
cb = function () {}
}
- var uri = "/-/all/since?stale=update_after&startkey=" + c
-
if (c === 0) {
this.log.warn("", "Building the local index for the first time, please be patient")
- uri = "/-/all"
+ uri = url.resolve(uri, "/-/all")
+ }
+ else {
+ uri = url.resolve(uri, "/-/all/since?stale=update_after&startkey=" + c)
}
this.request('GET', uri, null, function (er, updates, _, res) {
View
2 lib/request.js
@@ -41,7 +41,7 @@ function regRequest (method, uri, options, cb_) {
this.log.verbose("request", "where is", where)
var registry = url.format(parsed)
- this.log.verbose("request", "registry is", registry)
+ this.log.verbose("request", "registry", registry)
if (!this.sessionToken) {
this.sessionToken = crypto.randomBytes(8).toString("hex")
View
16 test/get-all.js
@@ -0,0 +1,16 @@
+var tap = require("tap")
+
+var server = require("./lib/server.js")
+var common = require("./lib/common.js")
+var client = common.freshClient()
+
+tap.test("basic request", function (t) {
+ server.expect("/-/all", function (req, res) {
+ res.json([])
+ })
+
+ client.get("http://localhost:1337/-/all", null, function (er) {
+ t.notOk(er, "no error")
+ t.end()
+ })
+})

0 comments on commit b78aa86

Please sign in to comment.
Something went wrong with that request. Please try again.