cloudant / nodejs-cloudant

Cloudant Node.js client library
Apache License 2.0
255 stars 90 forks source link

'Promises' plugin stringifies (and so corrupts) binary attachment bodies #347

Closed alex-thewsey-ibm closed 6 years ago

alex-thewsey-ibm commented 6 years ago

Bug Description

Because the Cloudant Promises plugin always stringifies response body (unlike the underlying Nano relax() method), Non-UTF-able binary responses (e.g. PDF attachment data) are converted to Strings and corrupted in the process.

It is still possible to access the underlying Buffer data by supplying a callback (bypassing the plugin handler).

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

// Initialise Cloudant with 'promises' plugin
// Create 'db' with document 'docId', file attachment 'attId' (e.g. PDF)

db.attachment.get(docId, attId)
  .then(data => {
    console.log(data.constructor.name); // "String"
  });

db.attachment.get(docId, attId, (err, data) => {
  if (!err) {
    console.log(data.constructor.name); // "Buffer"
  }
})

2. What you expected to happen

Cloudant Promises plugin behaves consistently with the underlying nano relax() and getAtt methods: getAtt internally sets request parameter dontParse: true for attachment requests, and this param is respected by the underlying request/response agent.

3. What actually happened

Cloudant's own request handler promisification JSON.stringify()s all responses, damaging non-UTFable data

Environment details

emlaver commented 6 years ago

Thanks for reporting. The PR for updating to the latest nano version will fix this: https://github.com/cloudant/nodejs-cloudant/pull/342.