apache / nano

Nano is now part of Apache CouchDB. Repo moved to https://GitHub.com/apache/couchdb-nano

Home Page:https://github.com/apache/couchdb-nano

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

db.follow breaks when couchdb is at a prefix

gr2m opened this issue · comments

var couch = nano({
    url: 'http://app.com/_couchdb',
    parseUrl: false
  });
var db = couch.use('mydb')
db.follow({since: 'now'}).follow()
// throws error, because it requests `http://app.com/mydb` instead of  `http://app.com/_couchdb/mydb`

Problem is in the followDb function at https://github.com/dscape/nano/blob/master/lib/nano.js#L308

url.resolve('http://app.com/_couchdb', encodeURIComponent('mydb'));
// returns http://app.com/mydb

A simple workaround would be to assure that couchdb url always ends with a /

url.resolve('http://app.com/_couchdb/', encodeURIComponent('mydb'));
// returns http://app.com/_couchdb/mydb

When I introduced prefix support (and the parseUrl flag) I have thought about that nano might modify the url to always contain a trailing /. But I was afraid it might break some code which relies on nano not changing the url, for example when accessing nano.config, which I sometimes do.

Anyway, I think at this point we have a reason and should take the responsibility for this break.

@dscape what is your opinion on that?

note that not only follow is broken. I've updated the according code at all places in my pull request

yes, I know 👍

I think we should 🚢 it and npm version major it just in case ?

(we never used versioning as marketing so might as well respect semver in case something case break production code?)

OK. But #269 does not break anything IMHO so I gonna just release it as 6.1.3.

Damn: ERROR: Coverage for branches (99.39%) does not meet global threshold (100%)

Seems like a test is missing

@gr2m wanna take a look?

Yeah I know, I wanted to make sure that I didn't miss understand anything. I'd love to add the test, but I'll be next 3 weeks traveling. Feel free to do it yourself, if it's still open, I promise to do it when I'm back

Fixed with 10d857b

@gr2m could you please be so nice to add the test?

I tried this

it('resolves db URL correctly for http://app.com/_couchdb', function(assert) {
  var nano = require('../../../lib/nano');

  var couch = nano({
    url: 'http://app.com/_couchdb',
    parseUrl: false,
    request: function(options) {
      assert.equal(options.uri, 'http://app.com/_couchdb/mydb/mydoc', 'should get doc at prefixed path');
      assert.end();
    }
  });
  couch.use('mydb').get('mydoc')
});

Which definitely goes into the if block of the added code here:

function urlResolveFix(couchUrl, dbName) {
  if (/[^\/]$/.test(couchUrl)) {
    couchUrl += '/';
  }
  return u.resolve(couchUrl, dbName);
}

But it still says coverage is below 100%, and I don't understand why that is.

Ah, I think the problem is that it /[^\/]$/.test(couchUrl) always returns true right now, and we miss a test where it would return false, that's why Statements coverage is at 100%, but Branches at 99.39%. I got it now :)

What I don't get though is why /[^\/]$/.test(couchUrl) is always true. Because in my initial issue I clearly had a case where it the missing / at the end is causing the problem

okay 🎉 I got it, PR incoming ...

Released as nano@6.1.3.

Thanks a lot!