0

I am using gulp and would like to create a function that can be piped into that passes on the input to the output and writes a single line to the console.

For example:

   this.src('/foo')
        .pipe(myFunction)
        .pipe(...)

Can someone help me understand what myFunction needs to look like?

The following code, stops the stream in its tracks. How can I ensure the stream data is passed onwards?

function myFunction(obj) {
  var stream = new Stream.Transform({objectMode: true});

  stream._transform = function () {
    console.log('foo bar bam baz');
  };

  return stream;
}

1 Answer 1

2

Transformation stream is meant - unsuprisingly - for transformation of data. An example of this is zlib stream, which compresses the data written to it, and the compressed bytes can be read out from the same stream.

You are creating a transformation stream that doesn't transform anything - that's quite ok. But you still need to pass the "transformed value" (in this case the same value) along. In it's current form, your stream only consumes what's put in. The _transform function recieves 3 arguments: data, encoding and callback, which has to be called for example like this:

function myFunction(obj) {
    var stream = new Stream.Transform({objectMode: true});

    stream._transform = function (data, encoding, callback) {
        console.log('foo bar bam baz');
        callback(null, data);
    };

    return stream;
}

Source: https://nodejs.org/api/stream.html#stream_transform_transform_chunk_encoding_callback

Excellent guide on Node streams: https://github.com/substack/stream-handbook

Sign up to request clarification or add additional context in comments.

2 Comments

Also, _transform gets called every time a new chunk is recieved, so you'll have to work around that, if you only want one line in your console.
Is there a word to describe these stream steps that are piped together?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.