Skip to content

pipeline + generator - Premature close #33792

@urugator

Description

@urugator

Version: 14.4.0
Platform: 64-bit (Windows)
Subsystem: stream

pipeline throws Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close when:

  1. There is a destination function and transformer function throws from for await block .
// Expecting: Error: transformer
// Got: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close 
Stream.pipeline(
  Stream.Readable.from(['a', 'b', 'c']),
  async function* (readable) {    
    for await (const chunk of readable) {
      // If this line is moved before or after the `for await` a correct error is thrown
      throw new Error('transformer');
    }
  },
  // If destination function is removed a correct error is thrown
  async function (readable) {
    let result = '';
    for await (const chunk of readable) {
      result += chunk;
    }
    return result;
  },
  (error, val) => error ? console.error(error) : console.log(val)
)
  1. The destination function throws from for await block
// Expecting: Error: destination
// Got: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close 
Stream.pipeline(
  Stream.Readable.from(['a', 'b', 'c']),
  async function (readable) {
    let result = '';
    for await (const chunk of readable) {
      // If this line is moved before or after the `for await` a correct error is thrown
      throw new Error('destination');	
      result += chunk;      	
    }
    return result;
  },
  (error, val) => error ? console.error(error) : console.log(val)
)
  1. The transformer or destination returns from for await block
// Expecting: Pipeline resolved with the value returned from destination and unfinished streams being silently destroyed
// Got: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close 
Stream.pipeline(
  Stream.Readable.from(['a', 'b', 'c']),
  async function (readable) {
    for await (const chunk of readable) {
      // If this line is moved BEFORE `for await` - callback is NOT called AT ALL (node simply exits if it has nothing else to do)
      return 'foo';
    }
  },
  (error, val) => error ? console.error(error) : console.log(val)
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to the stream subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions