pax_global_header00006660000000000000000000000064130006552160014510gustar00rootroot0000000000000052 comment=d35a4071dacbcc60fc40d798fa58fc425cba3efc flush-write-stream-1.0.2/000077500000000000000000000000001300065521600152525ustar00rootroot00000000000000flush-write-stream-1.0.2/.gitignore000066400000000000000000000000411300065521600172350ustar00rootroot00000000000000node_modules bundle.js test.html flush-write-stream-1.0.2/.travis.yml000066400000000000000000000001051300065521600173570ustar00rootroot00000000000000language: node_js node_js: - '0.10' - '0.12' - '4.0' - '5.0' flush-write-stream-1.0.2/LICENSE000066400000000000000000000020671300065521600162640ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Mathias Buus 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. flush-write-stream-1.0.2/README.md000066400000000000000000000021731300065521600165340ustar00rootroot00000000000000# flush-write-stream A write stream constructor that supports a flush function that is called before `finish` is emitted ``` npm install flush-write-stream ``` [![build status](http://img.shields.io/travis/mafintosh/flush-write-stream.svg?style=flat)](http://travis-ci.org/mafintosh/flush-write-stream) ## Usage ``` js var writer = require('flush-write-stream') var ws = writer(write, flush) ws.on('finish', function () { console.log('finished') }) ws.write('hello') ws.write('world') ws.end() function write (data, enc, cb) { // i am your normal ._write method console.log('writing', data.toString()) cb() } function flush (cb) { // i am called before finish is emitted setTimeout(cb, 1000) // wait 1 sec } ``` If you run the above it will produce the following output ``` writing hello writing world (nothing happens for 1 sec) finished ``` ## API #### `var ws = writer([options], write, [flush])` Create a new writable stream. Options are forwarded to the stream constructor. #### `var ws = writer.obj([options], write, [flush])` Same as the above except `objectMode` is set to `true` per default. ## License MIT flush-write-stream-1.0.2/example.js000066400000000000000000000006061300065521600172450ustar00rootroot00000000000000var writer = require('./') var ws = writer(write, flush) ws.on('finish', function () { console.log('finished') }) ws.write('hello') ws.write('world') ws.end() function write (data, enc, cb) { // i am your normal ._write method console.log('writing', data.toString()) cb() } function flush (cb) { // i am called before finish is emitted setTimeout(cb, 1000) // wait 1 sec } flush-write-stream-1.0.2/index.js000066400000000000000000000026701300065521600167240ustar00rootroot00000000000000var stream = require('readable-stream') var inherits = require('inherits') var SIGNAL_FLUSH = new Buffer([0]) module.exports = WriteStream function WriteStream (opts, write, flush) { if (!(this instanceof WriteStream)) return new WriteStream(opts, write, flush) if (typeof opts === 'function') { flush = write write = opts opts = {} } stream.Writable.call(this, opts) this.destroyed = false this._worker = write || null this._flush = flush || null } inherits(WriteStream, stream.Writable) WriteStream.obj = function (opts, worker, flush) { if (typeof opts === 'function') return WriteStream.obj(null, opts, worker) if (!opts) opts = {} opts.objectMode = true return new WriteStream(opts, worker, flush) } WriteStream.prototype._write = function (data, enc, cb) { if (SIGNAL_FLUSH === data) this._flush(cb) else this._worker(data, enc, cb) } WriteStream.prototype.end = function (data, enc, cb) { if (!this._flush) return stream.Writable.prototype.end.apply(this, arguments) if (typeof data === 'function') return this.end(null, null, data) if (typeof enc === 'function') return this.end(data, null, enc) if (data) this.write(data) if (!this._writableState.ending) this.write(SIGNAL_FLUSH) return stream.Writable.prototype.end.call(this, cb) } WriteStream.prototype.destroy = function (err) { if (this.destroyed) return this.destroyed = true if (err) this.emit('error', err) this.emit('close') } flush-write-stream-1.0.2/package.json000066400000000000000000000012661300065521600175450ustar00rootroot00000000000000{ "name": "flush-write-stream", "version": "1.0.2", "description": "A write stream constructor that supports a flush function that is called before finish is emitted", "main": "index.js", "dependencies": { "inherits": "^2.0.1", "readable-stream": "^2.0.4" }, "devDependencies": { "tape": "^4.2.2" }, "scripts": { "test": "tape test.js" }, "repository": { "type": "git", "url": "https://github.com/mafintosh/flush-write-stream.git" }, "author": "Mathias Buus (@mafintosh)", "license": "MIT", "bugs": { "url": "https://github.com/mafintosh/flush-write-stream/issues" }, "homepage": "https://github.com/mafintosh/flush-write-stream" } flush-write-stream-1.0.2/test.js000066400000000000000000000030741300065521600165730ustar00rootroot00000000000000var tape = require('tape') var writer = require('./') tape('is a write stream', function (t) { var expected = ['hello', 'world', 'verden'] var ws = writer.obj(write) ws.write('hello') ws.write('world') ws.write('verden') ws.end(function () { t.same(expected.length, 0) t.end() }) function write (data, enc, cb) { t.same(data, expected.shift()) cb() } }) tape('is flushable', function (t) { var expected = ['hello', 'world', 'verden'] var flushed = false var ws = writer.obj(write, flush) ws.write('hello') ws.write('world') ws.write('verden') ws.end(function () { t.same(expected.length, 0) t.ok(flushed, 'was flushed') t.end() }) function write (data, enc, cb) { t.same(data, expected.shift()) cb() } function flush (cb) { flushed = true process.nextTick(cb) } }) tape('can pass options', function (t) { var expected = ['hello', 'world', 'verden'] var flushed = false var ws = writer({objectMode: true}, write, flush) ws.write('hello') ws.write('world') ws.write('verden') ws.end(function () { t.same(expected.length, 0) t.ok(flushed, 'was flushed') t.end() }) function write (data, enc, cb) { t.same(data, expected.shift()) cb() } function flush (cb) { flushed = true process.nextTick(cb) } }) tape('emits error on destroy', function (t) { var expected = new Error() var ws = writer({objectMode: true}, function () {}) ws.on('error', function (err) { t.equal(err, expected) }) ws.on('close', t.end) ws.destroy(expected) })