Skip to content

Commit cd650be

Browse files
authored
Properly escape partition ID's (fixes #283) (#284)
This is a breaking change, for reasons descibed in that bug report.
1 parent 2d4bd71 commit cd650be

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

lib/nano.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ module.exports = exports = function dbScope (cfg) {
10361036

10371037
return relax({
10381038
db: dbName,
1039-
path: '_partition/' + partitionKey
1039+
path: '_partition/' + encodeURIComponent(partitionKey)
10401040
}, callback)
10411041
}
10421042

@@ -1047,15 +1047,15 @@ module.exports = exports = function dbScope (cfg) {
10471047
}
10481048
return relax({
10491049
db: dbName,
1050-
path: '_partition/' + partitionKey + '/_all_docs',
1050+
path: '_partition/' + encodeURIComponent(partitionKey) + '/_all_docs',
10511051
qs: opts
10521052
}, callback)
10531053
}
10541054

10551055
function partitionedListAsStream (partitionKey, qs) {
10561056
return relax({
10571057
db: dbName,
1058-
path: '_partition/' + partitionKey + '/_all_docs',
1058+
path: '_partition/' + encodeURIComponent(partitionKey) + '/_all_docs',
10591059
qs: qs,
10601060
stream: true
10611061
})
@@ -1068,7 +1068,7 @@ module.exports = exports = function dbScope (cfg) {
10681068

10691069
return relax({
10701070
db: dbName,
1071-
path: '_partition/' + partition + '/_find',
1071+
path: '_partition/' + encodeURIComponent(partition) + '/_find',
10721072
method: 'POST',
10731073
body: query
10741074
}, callback)
@@ -1077,7 +1077,7 @@ module.exports = exports = function dbScope (cfg) {
10771077
function partitionedFindAsStream (partition, query) {
10781078
return relax({
10791079
db: dbName,
1080-
path: '_partition/' + partition + '/_find',
1080+
path: '_partition/' + encodeURIComponent(partition) + '/_find',
10811081
method: 'POST',
10821082
body: query,
10831083
stream: true
@@ -1090,15 +1090,15 @@ module.exports = exports = function dbScope (cfg) {
10901090
}
10911091
return relax({
10921092
db: dbName,
1093-
path: '_partition/' + partition + '/_design/' + ddoc + '/_search/' + searchName,
1093+
path: '_partition/' + encodeURIComponent(partition) + '/_design/' + ddoc + '/_search/' + searchName,
10941094
qs: opts
10951095
}, callback)
10961096
}
10971097

10981098
function partitionedSearchAsStream (partition, ddoc, searchName, opts) {
10991099
return relax({
11001100
db: dbName,
1101-
path: '_partition/' + partition + '/_design/' + ddoc + '/_search/' + searchName,
1101+
path: '_partition/' + encodeURIComponent(partition) + '/_design/' + ddoc + '/_search/' + searchName,
11021102
qs: opts,
11031103
stream: true
11041104
})
@@ -1110,15 +1110,15 @@ module.exports = exports = function dbScope (cfg) {
11101110
}
11111111
return relax({
11121112
db: dbName,
1113-
path: '_partition/' + partition + '/_design/' + ddoc + '/_view/' + viewName,
1113+
path: '_partition/' + encodeURIComponent(partition) + '/_design/' + ddoc + '/_view/' + viewName,
11141114
qs: opts
11151115
}, callback)
11161116
}
11171117

11181118
function partitionedViewAsStream (partition, ddoc, viewName, opts) {
11191119
return relax({
11201120
db: dbName,
1121-
path: '_partition/' + partition + '/_design/' + ddoc + '/_view/' + viewName,
1121+
path: '_partition/' + encodeURIComponent(partition) + '/_design/' + ddoc + '/_view/' + viewName,
11221122
qs: opts,
11231123
stream: true
11241124
})

test/partition.list.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ test('should be able to list partition docs (callback) - GET /db/_partition/_all
121121
})
122122
})
123123

124+
test('should escape unusual characters - GET /db/_partition/a+b/_all_docs - db.partitionedList', async () => {
125+
// mocks
126+
const scope = nock(COUCH_URL)
127+
.get('/db/_partition/a%2Bb/_all_docs')
128+
.reply(200, response)
129+
130+
// test GET /db/_partition/_all_docs
131+
return new Promise((resolve, reject) => {
132+
db.partitionedList('a+b', (err, data) => {
133+
expect(err).toBeNull()
134+
expect(data).toStrictEqual(response)
135+
expect(scope.isDone()).toBe(true)
136+
resolve()
137+
})
138+
})
139+
})
140+
124141
test('should handle missing database - GET /db/_partition/_all_docs - db.partitionedList', async () => {
125142
// mocks
126143
const scope = nock(COUCH_URL)

0 commit comments

Comments
 (0)