// FIXME: we need to specifically watch for the `error` and `end` events on the readable interface, to know when the transform stream has fully completed processing
@ -109,19 +111,15 @@ function fromTransform(stream) {
// request, destroy
letreadable=wireUpReadableInterface(stream,{
onEnd:()=>{
if(completionDefer!=null){
completionDefer.resolve();
}
debugTransform("Received end/close event from underlying stream");
},
onError:(error)=>{
if(completionDefer!=null){
completionDefer.reject(error);
}
onError:()=>{
debugTransform("Received error event from underlying stream");
}
});
// write, end, destroy
varwritable=wireUpWritableInterface(stream);
letwritable=wireUpWritableInterface(stream);
letconvertedStream={
_promistreamVersion:0,
@ -129,32 +127,36 @@ function fromTransform(stream) {
// NOTE: This logic exists at the start, not in the upstream EndOfStream handling code, because any number of buffer reads may be required before the wrapped Node stream can be closed
// NOTE: The push-buffer will automatically produce EndOfStream markers once the buffer has run out and the underlying stream has closed, so long as we're using the wireUpReadableInterface function
returnPromise.try(()=>{
returnreadable.request();
}).then((result)=>{
return[result];
});
}else{
returnPromise.try(()=>{
debugTransform("Doing upstream read...");
returnsource.read();
}).then((value)=>{
debugTransform("Writing upstream value to writable interface");
writable.write(value);
// This will quite possibly return an empty buffer, but that is fine; the `buffer` stream downstream from us will just keep reading (and therefore queueing up new items to be transformed) until it gets some results.
debugTransform("Consuming immediate buffer from readable interface");
returnreadable.consumeImmediateBuffer();
}).catch(isEndOfStream,(marker)=>{
// Wait for transform stream to drain fully, `error`/`end` event, and then return whatever buffer remains.
// FIXME: Error propagation logic is pretty shaky here. Verify that we don't end up with double error reports.
if(endHandled===false){
}).catch(isEndOfStream,()=>{
debugTransform("End of upstream reached");
endHandled=true;
debugTransform("Closing via writable interface");
writable.end();
returnPromise.try(()=>{
let{promise,defer}=createDefer();
completionDefer=defer;
returnpromise;
}).then(()=>{
returnreadable.consumeImmediateBuffer();
// Return nothing, let the next read call (and all of those after that) deal with either underlying stream completion or buffered results