Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Last active Nov 20, 2019
Embed
What would you like to do?
Notes on Node streams

I have a real mental blockage around this stuff. Partly because the docs could probably be a little better, partly because it's changed over the years, probably partly because I'm dumb. Whatever. I'm going to try and document how to do stuff I occasionally need to do, so I can refer to it next time.

Return value of pipe

x.pipe(y) === y; // true
x.on(someEvent, callback) === x; // true

Creating a transform stream

const myTransform = new Transform({
  transform(chunk, encoding, callback) {
    callback(doSomethingWith(chunk));
  }
});

fs.createReadStream('somefile.txt').pipe(myTransform);

Flowing vs paused mode

Readable streams default to 'paused mode'. The data doesn't start flowing unless you

  • add a 'data' event handler
  • call stream.resume()
  • call stream.pipe(writable)

More information here. God, what a mess.

TODO what are the available events, does closing z in x.pipe(y).pipe(z) close x, etc

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