Skip to content

juliangruber/koa-gen-body

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-gen-body

Experimenting with generator stream bodies for koa.

Until nodejs/node-v0.x-archive#7043 is resolved this has to be coupled to koa for otherwise there will be no end event.

For more rationale see koajs/koa#207 and koajs/koa#208.

Semantics

A readable generator stream is simply a generator function.

On each invokation it should return a String or Buffer of data, or a falsy value when there's nothing more to be read and the stream is done.

It may take an end argument, which when true tells the stream to clean up its underlying resources, like tcp connections or file descriptors.

To read from a generator stream:

var data;
while (data = yield genStream()) {
  console.log('data: %s', data);
}
console.log('done reading');

To read and abort:

console.log('data: %s', yield genStream());
console.log('data: %s', yield genStream());
console.log('last data: %s', yield genStream(true));
console.log('done reading');

Example

var koa = require('koa');
var genBody = require('koa-gen-body');
var wait = require('co-wait');

var app = koa();
app.use(genBody());
app.use(function*(){
  this.body = this.genBody(function*(end){
    if (end) return console.log('end');
    yield wait(1000);
    return Date.now() + '\n';
  });
});
app.listen(3000);

API

genBody()

ctx#genBody(fn)

Create a readable stream that pull data from generator function fn.

fn will be passed the end argument, which if true tells you to end what you're doing - useful for cleaning up underlying resources.

Installation

$ npm install koa-gen-body

License

MIT

About

Experimenting with generator stream bodies for koa

Resources

Stars

Watchers

Forks

Packages

No packages published