writsy

1.0.8 • Public • Published

writsy

Write stream wrapper that supports async initialization and flush function.

Build Status

npm install writsy

var ws = writsy([opts])

var ws = writsy.obj([opts])

Wraps a new writable stream (or object stream) by passing init callback function. Supports optional flush function that is called before 'finish' emitted.

var writsy = require('writsy')
...
 
var ws = writsy({
  init (cb) {
    // async initialization, error handling
    mkdirp('/tmp/foo/', (err) => {
      if (err) return cb(err)
      cb(null, fs.createWriteStream('/tmp/foo/bar.txt'))
    })
  },
  flush (cb) {
    // flush before finish
    fs.rename('/tmp/foo/bar.txt', './dest.txt', cb)
  }
})
 
fs.createReadStream('loremipsum.txt').pipe(ws)

Or class inheritance by setting _init and _flush methods:

var Writsy = require('writsy')
 
class Writer extends Writsy {
  constructor (opts) {
    super(opts)
  }
  _init (cb) {
    // async initialization, error handling
    mkdirp('/tmp/foo/', (err) => {
      if (err) return cb(err)
      cb(null, fs.createWriteStream('/tmp/foo/bar.txt'))
    })
  }
  _flush (cb) {
    // flush before finish
    fs.rename('/tmp/foo/bar.txt', './dest.txt', cb)
  }
}
 
fs.createReadStream('loremipsum.txt').pipe(new Writer())

License

MIT

Dependencies (2)

Dev Dependencies (5)

Package Sidebar

Install

npm i writsy

Weekly Downloads

0

Version

1.0.8

License

MIT

Last publish

Collaborators

  • cshum