node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

koa-buffer-stream

Koa Buffer Stream Build Status

Buffer a response stream so you can do things like ETag, content-length calculations, or caching it in a database as a string.

Example

var fs = require('fs')
var app = require('koa')()
app.context(require('koa-buffer-stream'))
 
app.use(function (next) {
  return function* () {
    // An example stream 
    this.body = fs.createReadStream('file.txt')
 
    // Use HTTP caching for visitors only 
    if (!this.user) {
      yield this.bufferStream
      // Some custom async etag calculation function, not included 
      yield this.etag
      if (this.fresh)
        return this.status = 304
    }
  }
})

API

var app = require('koa')()
app.context(require('koa-buffer-stream'))

this.bufferStream

yield this.bufferStream

this.bufferStream is a function. When done, this.body will be a Buffer of the stream's contents.

License

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.