Events for fs createReadStream do not always trigger

I have fs filestream used to upload files that are dynamically created. I receive this stream and pass it to my response object after setting the appropriate headers.

I also set up a couple of stream events so that if an error or stream ends there, it deletes the generated files from the file system.

I had a problem when sometimes, when the download was not correctly initialized or finished, some hanging files may remain in the file system. I believe this is due to events that I am connecting to the stream.

My code is:

stream = fs.createReadStream( fileName, {bufferSize: 64*1024})

had_error = false;
stream.on 'error', (err) ->
  had_error = err
stream.on 'close', ->
  if had_error
    console.log(had_error)

  fs.unlink fileName, (error) ->
    if error
      console.log("DELETE ERROR")
      console.log error

  fs.unlink dataFileName, (error) ->
    if error
      console.log("DELETE ERROR")
      console.log error

API "close" . "", , : Indicates that no more 'data' events will happen. If the stream is also writable, it may be possible to continue writing.

, "end", , , , , http, .

"" , ?

cronjob (bleh).

0
1

. , response , , . - , .

0

Source: https://habr.com/ru/post/1528108/


All Articles