Socket
Book a DemoInstallSign in
Socket

koa-write

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-write

Write to koa bodies in generator style

Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

koa-write

Write to koa bodies in generator style, respecting back pressure and throwing on closed sockets.

Example

var koa = require('koa');
var co = require('co');
var write = require('koa-write');
var PassThrough = require('stream').PassThrough;
var wait = require('co-wait');

var app = koa();
app.use(function(){
  var ctx = this;
  ctx.body = PassThrough();

  co(function*(){
    for (var i = 0; i < 100; i++) {
      yield write(ctx, Date.now().toString());
    }
  }, this.onerror);
});

API

write(ctx, chunk)

Write chunk to ctx.

Yields as soon as you're good to write more.

Throws when the socket is already closed, so it doesn't make sense to write more.

Why not just co-write?

co-write writes to streams and does the same http socket handling, however it expects to be passed an http response object for that to work. In the situation where you have stream.pipe(res) you'll want to write to stream, however end when res.socket ends.

Installation

$ npm install koa-write

License

MIT

FAQs

Package last updated on 14 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts