Skip to content

Commit 53dea92

Browse files
glynnbirdGlynn Bird
andauthored
always return a Buffer from attachment.get. Fixes issue #373 (#374)
Co-authored-by: Glynn Bird <glynnbird@apache.org>
1 parent 260862b commit 53dea92

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎lib/nano.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ module.exports = exports = function dbScope (cfg) {
148148
// when doing head requests, we return the response headers, not the response body
149149
if (req.method === 'head') {
150150
retval = Object.fromEntries(response.headers)
151-
} else if (contentType && contentType.startsWith('application/json')) {
151+
} else if (contentType && contentType.startsWith('application/json') && !opts.dontParse) {
152152
try {
153153
retval = await response.json()
154154
} catch {

‎package-lock.json‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "Apache-2.0",
55
"homepage": "http://github.com/apache/couchdb-nano",
66
"repository": "http://github.com/apache/couchdb-nano",
7-
"version": "11.0.6",
7+
"version": "11.0.7",
88
"author": "Apache CouchDB <dev@couchdb.apache.org> (http://couchdb.apache.org)",
99
"keywords": [
1010
"couchdb",

‎test/attachment.get.test.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ test('should be able to get an attachment with opts - GET /db/id/attname - db.at
4343
mockAgent.assertNoPendingInterceptors()
4444
})
4545

46+
test('should be able to get a JSON attachment as a buffer - GET /db/id/attname - db.attachment.get', async () => {
47+
// mocks
48+
const json = JSON.stringify({ foo: 'bar' })
49+
mockPool
50+
.intercept({ path: '/db/id/att.json' })
51+
.reply(200, json, { headers: { 'content-type': 'application/json' } })
52+
53+
// test GET /db/id/attname
54+
const db = nano.db.use('db')
55+
const p = await db.attachment.get('id', 'att.json')
56+
assert(Buffer.isBuffer(p), 'JSON attachment should be a buffer')
57+
assert.equal(p.toString(), json)
58+
mockAgent.assertNoPendingInterceptors()
59+
})
60+
4661
test('should detect missing parameters - db.attachment.get', async () => {
4762
const db = nano.db.use('db')
4863
await assert.rejects(db.attachment.get(), { message: 'Invalid parameters' })

0 commit comments

Comments
 (0)