jbgtmartin opened a new issue, #366:
URL: https://github.com/apache/couchdb-nano/issues/366

   ### Describe the bug
   
   When a CouchDB-compatible server (or proxy) responds with `Content-Type: 
application/json; charset=utf-8` instead of the bare `application/json`, nano 
v11 returns a raw `Buffer` instead of a parsed JavaScript object. This is a 
silent failure: callers that access properties like `.rows` on the result get 
`undefined` with no error thrown.
   
   ### To reproduce
   
   Set up any HTTP proxy in front of CouchDB that adds `; charset=utf-8` to the 
`Content-Type` response header (common behaviour in Node.js HTTP frameworks 
such as Express). Then call any nano method that expects a JSON response body, 
e.g.:
   
   ```js
   const result = await db.view('myDesignDoc', 'myView', { include_docs: true 
});
   console.log(result);      // <Buffer 7b 22 74 6f 74 61 6c ...>
   console.log(result.rows); // undefined — silently processes nothing
   ```
   
   ### Expected behavior
   
   `result` should be a parsed JavaScript object `{ total_rows: N, offset: N, 
rows: [...] }` regardless of whether the server sends `Content-Type: 
application/json` or `Content-Type: application/json; charset=utf-8`.
   
   ### Root cause
   
   In `lib/nano.js`, the JSON parsing branch uses strict equality:
   
   ```js
   // lib/nano.js, line 151
   } else if (contentType === 'application/json') {
     retval = await response.json()
   ```
   
   `'application/json; charset=utf-8' === 'application/json'` is `false`, so 
execution falls through to the `else` branch which returns a `Buffer`. A fix 
would be to use `startsWith` instead:
   
   ```js
   } else if (contentType?.startsWith('application/json')) {
     retval = await response.json()
   ```
   
   From what I understand this is a regression from nano v10, which used the 
`request` library with `json: true`. That option forced JSON parsing regardless 
of the `Content-Type` header, so the charset suffix was never an issue.
   
   ### Environment
   
   - nano version: 11.0.4
   - Node.js version: 22


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to