Check out a free preview of the full Networking and Streams course

The "Using through2" Lesson is part of the full, Networking and Streams course featured in this preview video. Here's what you'd learn in this lesson:

James reviews the differences between through2 and native npm for using streams.

Preview
Close

Transcript from the "Using through2" Lesson

[00:00:00]
>> So we looked a little bit about how the through2 userland package compares to the transform stream that's part of core. Just use whatever version you like better. It doesn't matter very much. So if you do use core, then you have to pass in a, you can pass in a function that's gonna be acting on each chunk of data as it comes in.

[00:00:26]
And you can also pass in a function that will get called when the stream is finished at the end, which is called flush is the name of the key. And if you call through2, then you can pass these in just as positional arguments, you don't have to provide an object.

[00:00:47]
So here's what the through2 interface looks like, you provide a right and an end function. And optionally at the beginning, you can provide an options object to control some of the sort of obscure options that are available like object mode and also things like the high watermark. So the signature of these things is we already saw the right function, you have the buffer, the encoding, which you can mostly ignore and the next function.

[00:01:14]
One thing that I didn't cover yet is that so sometimes, most of the time, you'll wanna do something like call the next function with the data that you have, but not every piece of data might correspond to a piece of output. So what you can use instead in those cases is you can call next separately without any data, and you can call this.push to sort of provide data to the stream.

[00:01:46]
So for example, maybe if you're writing something like a gzip compression, you have to buffer things up into this set buffer size. So if you only got 100 bytes of data, then you don't really have enough to fill up your buffer yet. So you might not have any data to push right away.

[00:02:04]
So you might just call next a couple of times until you have enough data, for example. But these are interchangeable. So how this would work is, So these two things do the same thing.
>> So it will push it to the next widget to the output of that.

[00:02:28]
>> Yes, yeah, push provides output. And there's a special case for the push method. If you push null, that's the same as sending an end signal and that's also true for next. So if you provide null as the data portion of the next callback, that's like you're telling downstream consumers that okay, this stream is finished.

[00:02:51]
It's sort of if you hit Ctrl D on the input of a program that's reading from the keyboard on standardin, that's a similar idea. Another handy thing is if you don't give through any arguments, it sets some defaults. They're pretty convenient. So if you need a pass through stream for whatever reason, something that's just gonna take the input and copy it to the output side.

[00:03:17]
And occasionally, you need something like this, just don't give through any arguments. Or if you only want something that's going to hook into the end portion of through, you can pass null or undefined as the first argument for right.
>> Wouldn't it just do that without through?
>> For a transform stream, yeah.

[00:03:45]
So an example for when you might wanna do that is if you need to use an API that has a stream shaped hole and you need to fit a stream into it and you just want it to kind of just pass through. So this crops up now and then.

[00:04:00]
It's kind of hard to articulate when that happens exactly without a really contrived example, but it does happen.
>> Okay.
>> Actually, pretty commonly. So just something to be aware of. I also see that there's a question for best practices for errors. So yeah, I have some stuff about errors a bit later and dealing with clean up properly.

[00:04:22]
There's some good modules for that, like a pump and pump of fire pretty good.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now