Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PouchDB wrongly flags + as illegal character for database name #4314

Closed
colinskow opened this issue Sep 11, 2015 · 5 comments
Closed

PouchDB wrongly flags + as illegal character for database name #4314

colinskow opened this issue Sep 11, 2015 · 5 comments
Labels
enhancement Feature request

Comments

@colinskow
Copy link
Member

new PouchDB('http://localhost:5984/my+database')
  .catch(function(err) {
    console.log(err);
  });

Responds...

{ [illegal_database_name: Database encountered an unknown error]
  status: 400,
  name: 'illegal_database_name',
  message: 'Database encountered an unknown error',
  error: true,
  reason: 'Name: \'my database\'. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed. Must begin with a letter.' }
@willholley
Copy link
Member

This error is just passed through from CouchDB in this instance. It will work if you percent-encode the +:

new PouchDB('http://localhost:5984/my%2Bdatabase')
.catch(function(err) {
   console.log(err);
});

@daleharvey daleharvey added the bug Confirmed bug label Sep 16, 2015
@daleharvey
Copy link
Member

I am fairly sure we purposefully dont url encode the database name, but I cant quite think of why right now

@daleharvey daleharvey added enhancement Feature request and removed bug Confirmed bug labels Sep 16, 2015
@daleharvey
Copy link
Member

Nope, cant think of any reason to not do this

@daleharvey
Copy link
Member

fixed in 94c7276

@lupomontero
Copy link
Contributor

This breaks things when a database name contains a /, which is valid as far as CouchDB is concerned (ie: user databases in Hoodie are named user/randomid).

Before this change we could work with database names like this, but not now. Consider a CouchDB server being served in a subdirectory (ie: http://foo.com/_couch/), and a database called user/bar, so the URL would be http://foo.com/_couch/user%2Fbar.

// pre 5.1.0 this worked, now it gets double encoded
var db = new PouchDB('http://foo.com/_couch/user%2Fbar');

// now, without encoding PouchDB will think `bar` is the database name
var db = new PouchDB('http://foo.com/_couch/user/bar');

This is because PouchDB is splitting the url using / as the separator, and then encoding the last part. From source:

  // Split the path part of the URI into parts using '/' as the delimiter
  // after removing any leading '/' and any trailing '/'
  var parts = uri.path.replace(/(^\/|\/$)/g, '').split('/');

  // Store the first part as the database name and remove it from the parts
  // array
  uri.db = encodeURIComponent(parts.pop());

A possible solution would be to check for the presence of % in db name before it is encoded with encodeURIComponent() as suggested by @colinskow here. Something like:

  uri.db = parts.pop();
  if (uri.db.indexOf('%') === -1) {
    uri.db = encodeURIComponent(uri.db);
  }

I will send PR shortly.

cc/ @daleharvey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request
Projects
None yet
Development

No branches or pull requests

4 participants