1.0.8 • Published 7 years ago

writsy v1.0.8

Weekly downloads
12
License
MIT
Repository
github
Last release
7 years ago

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

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago