Skip to content

Ability to set highWaterMark on transform stream? #8855

Closed

Description

@olalonde
  • Version: v6.6.0
  • Platform: OS X El Capitan
  • Subsystem: Stream

I noticed that transform streams buffer up to 16kb (the default high watermark for read/write streams) even when they are not piping anywhere. I looked through the code/docs and it seems this value can't be set by implementers. As I'm writing a library which relies on this behaviour (transform buffering even when not piping to anywhere), I'd like to make sure it's not going to change and also was wondering if it would be possible for the Transform constructor to accept the highWaterMark option as well.

Activity

  1. added
    questionIssues asking questions about Node.js.
    streamIssues and PRs related to Node.js streams.
    on Sep 30, 2016
  2. mscdex commented on Sep 30, 2016

    @mscdex
    Contributor

    All of the constructors can already accept options, including highWaterMark. For example:

    var Transform = require('stream').Transform;
    var inherits = require('util').inherits;
    
    function MyTransform(opts) {
      if (typeof opts !== 'object' || opts === null)
        opts = {};
      Transform.call(this, { highWaterMark: opts.highWaterMark });
      // Or just: `Transform.call(this, opts);`
    
      // Other initialization ...
    }
    inherits(MyTransform, Transform);
    
    MyTransform.prototype._transform = function(chunk, enc, cb) {
      // ...
    };
  3. olalonde commented on Sep 30, 2016

    @olalonde
    ContributorAuthor

    Ok, thanks. Maybe I'm misunderstanding something then. I just tested the following code, which outputs 16384 instead of the expected 100 (highWaterMark) to the console:

    var Transform = require('stream').Transform
    var RandomStream = require('common-streams').RandomStream
    
    class CustomTransform extends Transform {
      constructor() {
        super({
          highWaterMark: 100,
        })
      }
    
      _transform(chunk, enc, cb) {
        console.log(chunk.length)
        cb(null, chunk)
      }
    }
    
    const rand = new RandomStream(10 * 1024 * 1024)
    
    rand.pipe(new CustomTransform())

    Is highWaterMark a minimum which can possibly be exceeded?

  4. mscdex commented on Sep 30, 2016

    @mscdex
    Contributor

    highWaterMark is just an indicator to communicate to upstream that no more data should be written/pushed. Obviously you can write more data and it will just continue to be buffered in memory. highWaterMark does not dictate the size of chunks passed to _transform()/_write().

  5. olalonde commented on Sep 30, 2016

    @olalonde
    ContributorAuthor

    Ah ok, so it's more of a signal to upstream but not enforced, thanks.

  6. kennylbj commented on Sep 4, 2020

    @kennylbj

    How can I set the chunk size in _transform function?

  7. bbrzoska commented on Jul 18, 2024

    @bbrzoska

    If you're here because your @aws-sdk/lib-storage Upload code started failing after upgrading node from v18 to v22, and have this line:

    Readable.toWeb(stream)

    then try changing it to:

    Readable.from(stream)

    Which fixes the problem for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues asking questions about Node.js.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions