sploders101 opened a new issue #274:
URL: https://github.com/apache/couchdb-nano/issues/274


   <!--- Provide a general summary of the issue in the Title above -->
   
   ## Expected Behavior
   <!--- If you're describing a bug, tell us what should happen -->
   <!--- If you're suggesting a change/improvement, tell us how it should work 
-->
   When passing a stream as an attachment to `db.attachments.insert(...)`, nano 
should stream the contents to the database regardless of the stream's 
implementation details.
   
   ## Current Behavior
   <!--- If describing a bug, tell us what happens instead of the expected 
behavior -->
   <!--- If suggesting a change/improvement, explain the difference from 
current behavior -->
   Nano uses a "poor man's deep clone" to create a copy of the request data 
(added in #256) before scrubbing the username and password for logging. When 
using a custom stream, this can create an issue where you are serializing a 
stream, which contains lots of circular references, and cannot be done.
   
   ## Possible Solution
   <!--- Not obligatory, but suggest a fix/reason for the bug, -->
   <!--- or ideas how to implement the addition or change -->
   Implement a better scrubbing strategy. Ex:
   ```typescript
   // scrub and log
   const scrubbedReq = {};
   Object.entries(([key, value]) => {
     if(key !== "auth") scrubbedReq[key] = value;
   });
   log(scrubbedReq);
   ```
   You don't need to clone the entire object. Deep cloning uses a lot of 
computing power for something that probably won't even end up happening. If you 
do a shallow clone, you can then omit the entire auth property (which might be 
safer if there is other authentication data in it). Data won't be modified 
during logging, so pass-by-reference (the issue deep cloning solves) shouldn't 
be an issue here.
   
   ## Steps to Reproduce (for bugs)
   <!--- Provide a link to a live example, or an unambiguous set of steps to -->
   <!--- reproduce this bug. Include code to reproduce, if relevant -->
   1. Create a readable stream (ex: `const myStream = fs.createReadStream(...)`)
   2. Create a circular reference (ex: `myStream.test = myStream`) (*many* 
stream implementations have these for a variety of reasons).
   3. Pass the req object into `db.attachments.insert(...)` as the data
   4. A circular reference error is emitted from JSON.stringify(...)
   
   ## Context
   <!--- How has this issue affected you? What are you trying to accomplish? -->
   <!--- Providing context helps us come up with a solution that is most useful 
in the real world -->
   I am trying to stream an upload via ExpressJS into CouchDB and need to 
either patch nano or use a `PassThrough` stream to omit circular references 
being passed to CouchDB. `JSON.stringify()` in most cases should not be used on 
objects that contain classes, because a great number of class implementations 
contain circular references.
   
   ## Your Environment
   <!--- Include as many relevant details about the environment you experienced 
the bug in -->
   * Version used: 9.0.3
   * Browser Name and version: N/A
   * Operating System and version (desktop or mobile): 11.4
   * Link to your project: Private Repo
   


-- 
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