File tree 2 files changed +20
-8
lines changed
2 files changed +20
-8
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,22 @@ import inherits from 'inherits';
6
6
import { btoa } from 'pouchdb-binary-utils' ;
7
7
8
8
function getBaseUrl ( db ) {
9
+ // Parse database url
10
+ let url ;
9
11
if ( typeof db . getUrl === 'function' ) { // pouchdb pre-6.0.0
10
- return urlParse ( db . getUrl ( ) ) . origin ;
11
- } else if ( db . __opts && db . __opts . prefix ) { // PouchDB.defaults
12
- return db . __opts . prefix ;
12
+ url = urlParse ( db . getUrl ( ) ) ;
13
13
} else { // pouchdb post-6.0.0
14
- return urlParse ( db . name ) . origin ;
14
+ // Use PouchDB.defaults' prefix, if any
15
+ let prefix = db . __opts && db . __opts . prefix ? db . __opts . prefix + '/' : '' ;
16
+ url = urlParse ( prefix + db . name ) ;
15
17
}
18
+
19
+ // Compute parent path for databases not hosted on domain root (see #215)
20
+ let path = url . pathname ;
21
+ path = path . substr ( - 1 , 1 ) === '/' ? path . substr ( 0 , - 1 ) : path ;
22
+ let parentPath = path . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ;
23
+
24
+ return url . origin + parentPath ;
16
25
}
17
26
18
27
function getConfigUrl ( db , nodeName ) {
Original file line number Diff line number Diff line change @@ -4,15 +4,12 @@ var PouchDB = require('pouchdb-memory');
4
4
var Authentication = require ( '../lib' ) ;
5
5
PouchDB . plugin ( Authentication ) ;
6
6
7
- var utils = require ( './test-utils' ) ;
8
7
var chai = require ( 'chai' ) ;
9
8
chai . should ( ) ;
10
9
11
- var serverHost = utils . getConfig ( ) . serverHost ;
12
-
13
10
describe ( 'urls' , function ( ) {
14
11
15
- var hostUrl = serverHost ;
12
+ var hostUrl = 'http://example.com' ;
16
13
var dbName = 'testdb' ;
17
14
var dbUrl = hostUrl + '/' + dbName ;
18
15
@@ -47,4 +44,10 @@ describe('urls', function () {
47
44
var usersUrl = db . getUsersDatabaseUrl ( ) ;
48
45
usersUrl . should . equal ( hostUrl + '/_users' ) ;
49
46
} ) ;
47
+
48
+ it ( 'Correct users database url for proxied database urls (issue-215)' , function ( ) {
49
+ var db = new PouchDB ( hostUrl + '/db/' + dbName ) ;
50
+ var usersUrl = db . getUsersDatabaseUrl ( ) ;
51
+ usersUrl . should . equal ( hostUrl + '/db/_users' ) ;
52
+ } ) ;
50
53
} ) ;
You can’t perform that action at this time.
0 commit comments