zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 365 forks source link

Need to close stream process? #277

Closed threepears closed 6 years ago

threepears commented 6 years ago

I'm experiencing something very strange and can't find a solution, so thought I'd check to see if I was missing something.

I have an interface where users can set parameters and then download data files from our system. I set this up to stream from the database request back through the API to the front end, and was using an older version of json2csv on the front end to synchronously catch the resulting data and make it into a file for download. However, it was having issues with bigger files, and I decided to upgrade my json2csv to the current version and implement the Transform streaming in my API. It is setup like this (a little differently from your docs for brevity, but I think I did this right):

import json2csv from 'json2csv'
const Json2csvTransform = new json2csv.Transform

// code ...

return inputStream
      .pipe(JSONStream.stringify())
      .pipe(Json2csvTransform)
      .pipe(res)
      .on('error', e => next(`Error: ${e}`))
      .on('finish', () => console.log("Streaming of file is now complete."))

However, when I added the ".pipe(Json2csvTransform)" to my streaming setup, I ran into an issue. Files now download more quickly, and I don't have an issue with larger files anymore, which is great ... but I can no longer make another file request immediately after I finish downloading a file. My logs tell me that the process runs, but no data returns ... until a few moments after everything completes, when my logging of data in my pipe finally returns the result I was expecting (by now, the code has all run with no results, so no file is created). This registers as a failure for the user on the front-end, however. Or, if I wait a few minutes, then I can successfully download again, but just not after one download completes. File size and amount of data has no effect on this phenomenon.

If I comment out the ".pipe(json2csv)", everything goes back to normal and I can download files one after the other. It's almost as if there's some part of the process that is not closing after the first file successfully downloads, and the error is only happening when I enable json2csv. Has anyone come across something like this before, or have any idea what the cause might be?

I'm using the latest version of json2csv and am working in Node 8.9.1 and Chrome 65.

knownasilya commented 6 years ago

Have you tried creating a new Transform instance for every request?

threepears commented 6 years ago

I am, apparently, a complete idiot. I had the Transform instance being created outside the request code. Pulling it down a few lines eliminated the issue. Thank you SO much!