Skip to content

Instantly share code, notes, and snippets.

@oxUnd
Created July 9, 2014 00:37
Show Gist options
  • Save oxUnd/d4465813f1c1a16b7cb8 to your computer and use it in GitHub Desktop.
Save oxUnd/d4465813f1c1a16b7cb8 to your computer and use it in GitHub Desktop.
readable.js
var _ = require('util');
var Readable = require('stream').Readable;
function MyReader (options) {
if (!(this instanceof MyReader)) {
return new MyReader(options);
}
Readable.call(this, options);
this.idx = 0;
this.buffer = [
"a",
"b",
"c",
"d",
"e",
"f"
];
}
_.inherits(MyReader, Readable);
MyReader.prototype._read = function (n) {
var len = this.buffer.length;
if (this.idx < len - 1) {
this.push(this.buffer[this.idx]);
this.idx++;
} else {
this.push(null);
}
}
MyReader().pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment