Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Last active November 20, 2019 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rich-Harris/07bbf2d52b0ff264ef4a870d82737be4 to your computer and use it in GitHub Desktop.
Save Rich-Harris/07bbf2d52b0ff264ef4a870d82737be4 to your computer and use it in GitHub Desktop.
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