Skip to content

Instantly share code, notes, and snippets.

var Readable = require('stream').Readable;
var Writable = require('stream').Writable;
var readable = Readable();
readable._read = function(){
setTimeout(function(){
readable.push(''+Date.now());
}, 100);
}
var http = require('http');
var Readable = require('stream').Readable;
http.createServer(function(req, res){
var stream = Readable();
stream._read = function(){
console.log('_read', Date.now());
if (stream.closed) return stream.push(null);
@juliangruber
juliangruber / coros.js
Last active August 29, 2015 13:56 — forked from tj/coros.rb
function producer(){
return function*(){
return yield io.read();
};
}
function prepend(str, fn){
return function*(){
return str + yield fn();
@juliangruber
juliangruber / gist:8819271
Last active August 29, 2015 13:56
co-stream pipe example
function pipe(){
var streams = [].slice.call(arguments);
return streams.slice(1).reduce(function(acc, stream){
return stream(acc);
}, streams[0]);
}
// usage:
var read = pipe(a(), b(), c(), d());
function readable(){
function*(end){
if (end) return; // clean up etc
var data = createData();
return data != null
? data
: end;
}
}
function values (ary) {
var i = 0;
return function (end, cb) {
if (i < ary.length) cb(true);
else cb(null, ary[i++])
}
}
function sync (read) {
read(null, function next (err, data) {
function values (ary) {
var i = 0;
return function*(end) {
if (end || i < ary.length) return null;
return ary[i++];
}
}
function sink (read) {
return function*(end){
{"foo":"bar","num":3,"nes":{"ted":["is",1]}}
var genBody = require('koa-gen-body');
var fromStream = require('co-from-stream');
var sse = require('co-sse');
var fs = require('fs');
app.use(genBody());
app.use(function*(){
this.body = this.genBody(sse(fromStream(fs.createReadStream('path'))));
});
var genBody = require('koa-gen-body');
var fs = require('co-fs');
var sse = require('co-sse');
app.use(genBody());
app.use(function*(){
this.body = this.genBody(sse(fs.createReadStream('path')));
});