Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 16, 2015 21:10
Show Gist options
  • Save creationix/5498108 to your computer and use it in GitHub Desktop.
Save creationix/5498108 to your computer and use it in GitHub Desktop.
Min Streams Interface Spec

Simple Streams Interface Spec

This spec describes a minimal stream interface meant for protocol implementors. Modules written to the interfaces in this spec can be used by a wide variety of project. The protocols will not need any extra dependencies themselves. It's just an interface to implement.

Simple Stream

A simple stream is just a function. It represents a pull-stream. It has the following signature:

// Implementing a stream.
function read(abort, callback) {
  // abort is a flag for signaling to the source that you're done with the stream.
  // if abort is truthy, and you're a filter, you usually want to pass it on the next time you call the source read.
  // if youe're a source, then stop reading and cleanup.  Call the callback with an end event when done.
    
  // To encode an item, have a falsy value for the first param and the item for the second.
  callback(null, item);
  
  // To encode the end of the stream, use the special `undefined` value for item.
  callback(null, undefined);
  // or simply
  callback();
  
  // If you wish to emit an error, pass a truthy value to the first parameter to the callback;
  callback(new Error("Oops, the monkey got out and ate all the banannas."));
});

Note: Dominic Tarr's pull-streams have a slightly different encoding for end events. Use an adapter for interop.

@creationix
Copy link
Author

@mikermcneil
Copy link

thanks!

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