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.
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.
An example of an echo server in working code https://github.com/creationix/moonslice-node/blob/master/testserver.js#L4