Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a way to detect end of stream on custom Writable Streams implementing _write #2994

Closed
GeorgeBailey opened this issue Sep 22, 2015 · 10 comments
Labels
feature request Issues that request new features to be added to Node.js. stream Issues and PRs related to the stream subsystem.

Comments

@GeorgeBailey
Copy link

It seems that _write should be called with null as the data, when the end of stream is reached.

The problem I am having is that I need to add additional data to the end of the stream, and the finish event should not be emitted until after that is completed. I need a way to control this, so I'm thinking a new _end function with a callback, or perhaps a callback on _write(null) would do the trick.

@GeorgeBailey
Copy link
Author

Actually, I'm surprised this was not already in place. (or is it?)

@brendanashworth brendanashworth added stream Issues and PRs related to the stream subsystem. feature request Issues that request new features to be added to Node.js. labels Sep 22, 2015
@jhamhader
Copy link
Contributor

Why not call writableStream.end('last data', cb)?

@brendanashworth
Copy link
Contributor

I think the prefinish event (which may be undocumented) would fit your requirements. Maybe try that for a spin?

@vkurchatkin
Copy link
Contributor

I think the prefinish event (which may be undocumented) would fit your requirements. Maybe try that for a spin?

It's not public. Take a look at this PR: #2314

@brendanashworth
Copy link
Contributor

@vkurchatkin ah, thanks for pointing me to that. @GeorgeBailey they might be interested in hearing your use case.

@GeorgeBailey
Copy link
Author

they might be interested in hearing your use case.

@brendanashworth, I am piping an http request and response over stdio to isolate various webapps in a process tree. This way an individual webapp can be killed without interrupting the whole server. I need to call res.end() in the parent process after res.end() in the child process is called.

( I realize a more common approach to process isolation is to set up localhost HTTP servers and listen on various TCP ports; using a proxy. However, for our environment we determined the stdio approach was more advantageous. )

I can certainly see other cases where detecting end of stream would be appropriate. (i.e. an encoding stream that has to add trailing data to the end of a compressed file format)

@GeorgeBailey
Copy link
Author

Why not call writableStream.end('last data', cb)?

@jhamhader, There needs to be a way to distinguish data from end of stream. The easiest way to do this is just overriding the write and end properties to begin with.

The method you suggest is less efficient as it requires parsing the stream for a pre-determined string. (similar to a mime id)

My understanding is that Writable Streams are intended to provide a more robust solution via an override to _write.

@Trott
Copy link
Member

Trott commented May 24, 2016

Did anything ever come of this? If not, is anything likely to come of it? (Basically, trying to figure out if this should be closed or if something could happen to get things moving, as it's been dormant for 7 months...)

@olalonde
Copy link
Contributor

olalonde commented Sep 8, 2016

Also need this... Use case is to convert http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property to a sync call that returns a write stream (I'm writing an adapter and have to conform to a stream returning interface which I have no control over).

// aws-sdk, callback api
s3.upload({ Key: 'somekey', Body: readStream }, (err) => {
  // etc.
})
const { PassThrough } = require('stream') 
const createWriteStream = (key) => {
  const through = new PassThrough()
  s3.upload({ Key: key, Body: through }, (err) => {
    // etc.
  })
  return through
}

The above doesn't cut it because the finish event is fired on through before the s3.upload callback is called. There doesn't seem to be any way to have through wait for the callback before emitting finish with the current stream implementer API.

@mcollina
Copy link
Member

mcollina commented Jun 6, 2017

This is solved by #12828 and the new _final  method that has been added to Node 8.0.0. Please have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

7 participants