Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

transform streams: unpipe or ignore future writes after .push(null) #6694

@jonathanong

Description

@jonathanong

ran into the following issue. i'm creating a transform stream that "truncates" the source stream. here's an example:

util.inherits(Truncate, Stream.Transform)

function Truncate() {
  this.count = 0
}

Truncate.prototype._transform = function (doc, enc, cb) {
  if (this.count++ < 10) {
    this.push(doc)
    cb()
  } else {
    this.push(null)
    cb()
  }
}

ideally, once this.push(null) is called, the transform stream should be considered "finished" and ._transform() shouldn't be called anymore. however, this isn't the case as ._transform() will continued to be called, and you'll get cannot push after EOF due to additional .push(null)s. to compensate, you'll have to keep track using some sort of this.ended flag.

implementation might by iffy though if you're piping. not sure if the source stream should unpipe on dest.on('end') or just continue writing and have the transform stream discard the data (which is what you'd do right now, anyways). i'm also not sure if and when the finish event would occur in this case. would also be nice if there were consistent stream.destroy() semantics.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions