async-writer
Advanced tools
Comparing version 1.3.9 to 1.4.0
@@ -23,2 +23,4 @@ /* | ||
*/ | ||
'use strict'; | ||
var AsyncWriter = require('./AsyncWriter'); | ||
@@ -25,0 +27,0 @@ |
@@ -16,7 +16,18 @@ /* | ||
*/ | ||
function StringBuilder() { | ||
'use strict'; | ||
function StringWriter(events) { | ||
this.str = ''; | ||
this.events = events; | ||
this.finished = false; | ||
} | ||
StringBuilder.prototype = { | ||
StringWriter.prototype = { | ||
end: function() { | ||
this.finished = true; | ||
if (this.events) { | ||
this.events.emit('finish'); | ||
} | ||
}, | ||
write: function(str) { | ||
@@ -74,6 +85,2 @@ this.str += str; | ||
return this._wrapped.once(event, callback); | ||
}, | ||
emit: function() { | ||
var wrappedStream = this._wrapped; | ||
return wrappedStream.emit.apply(wrappedStream); | ||
} | ||
@@ -90,37 +97,2 @@ }; | ||
function onProxy(asyncWriter, type, event, callback) { | ||
var global = asyncWriter.global; | ||
var events = asyncWriter._events; | ||
var async = asyncWriter._async; | ||
// Writable streams are only suppose to emit "finish" but | ||
// "through" streams (from the "through" module) only output | ||
// "end" event. Therefore, we need to normalize "end" and "finish" | ||
// events | ||
if (event === 'finish') { | ||
if (async.finished) { | ||
callback(); | ||
return asyncWriter; | ||
} | ||
var emitted = false; | ||
var onFinish = function() { | ||
if (emitted) { | ||
return; | ||
} | ||
emitted = true; | ||
callback(); | ||
}; | ||
events[type]('end', onFinish); | ||
events[type]('finish', onFinish); | ||
} else { | ||
events[type](event, callback); | ||
} | ||
return asyncWriter; | ||
} | ||
function Fragment(asyncWriter) { | ||
@@ -200,3 +172,3 @@ this.asyncWriter = asyncWriter; | ||
this.data = {}; | ||
this.global = this.attributes /* legacy */ = global || (global = {}); | ||
this.global = this.attributes /* legacy */ = (global || (global = {})); | ||
this._af = this._prevAF = this._parentAF = null; | ||
@@ -228,3 +200,3 @@ this._isSync = false; | ||
if (!writer) { | ||
writer = new StringBuilder(); | ||
writer = new StringWriter(this._events); | ||
} else if (buffer) { | ||
@@ -265,3 +237,3 @@ stream = writer; | ||
captureString: function (func, thisObj) { | ||
var sb = new StringBuilder(); | ||
var sb = new StringWriter(); | ||
this.swapWriter(sb, func, thisObj); | ||
@@ -302,3 +274,3 @@ return sb.toString(); | ||
var asyncOut = this.createNestedWriter(this.writer); | ||
var buffer = this.writer = new StringBuilder(); | ||
var buffer = this.writer = new StringWriter(); | ||
var asyncFragment = new AsyncFragment(asyncOut); | ||
@@ -381,7 +353,19 @@ var bufferedFragment = new BufferedFragment(this, buffer); | ||
on: function(event, callback) { | ||
return onProxy(this, 'on', event, callback); | ||
if (event === 'finish' && this.writer.finished) { | ||
callback(); | ||
return this; | ||
} | ||
this._events.on(event, callback); | ||
return this; | ||
}, | ||
once: function(event, callback) { | ||
return onProxy(this, 'once', event, callback); | ||
if (event === 'finish' && this.writer.finished) { | ||
callback(); | ||
return this; | ||
} | ||
this._events.once(event, callback); | ||
return this; | ||
}, | ||
@@ -388,0 +372,0 @@ |
{ | ||
"name": "async-writer", | ||
"description": "The async-writer module makes it possible to asynchronously write data to an output stream while still flushing out bytes in the correct order", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/raptorjs/async-writer.git" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test" | ||
}, | ||
"author": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"maintainers": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"dependencies": { | ||
"events": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.15.1", | ||
"chai": "~1.8.1", | ||
"through": "^2.3.4" | ||
}, | ||
"license": "Apache License v2.0", | ||
"bin": {}, | ||
"main": "lib/async-writer.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"version": "1.3.9" | ||
} | ||
"name": "async-writer", | ||
"description": "The async-writer module makes it possible to asynchronously write data to an output stream while still flushing out bytes in the correct order", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/raptorjs/async-writer.git" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test" | ||
}, | ||
"author": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"maintainers": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"dependencies": { | ||
"events": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"chai": "~1.8.1", | ||
"mocha": "~1.15.1", | ||
"through": "^2.3.4", | ||
"through2": "^0.6.5" | ||
}, | ||
"license": "Apache License v2.0", | ||
"bin": {}, | ||
"main": "lib/async-writer.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"version": "1.4.0" | ||
} |
@@ -9,2 +9,4 @@ 'use strict'; | ||
var fsReadOptions = { encoding: 'utf8' }; | ||
describe('async-writer' , function() { | ||
@@ -218,2 +220,34 @@ | ||
it('should support writing to a through2 stream', function(done) { | ||
var output = ''; | ||
var through2 = require('through2')( | ||
function write(data, encoding, callback) { | ||
output += data; | ||
callback(null, data); | ||
} | ||
); | ||
var errors = []; | ||
var out = require('../').create(through2) | ||
.on('error', function(e) { | ||
errors.push(e); | ||
}) | ||
.on('finish', function() { | ||
expect(errors.length).to.equal(0); | ||
expect(output).to.equal('123'); | ||
done(); | ||
}) | ||
.write('1'); | ||
var asyncOut = out.beginAsync(); | ||
setTimeout(function() { | ||
asyncOut.write('2'); | ||
asyncOut.end(); | ||
}, 10); | ||
out.write('3') | ||
.end(); | ||
}); | ||
it('should support writing to a through stream', function(done) { | ||
@@ -225,2 +259,3 @@ | ||
output += data; | ||
this.queue(data); | ||
} | ||
@@ -234,3 +269,3 @@ ); | ||
}) | ||
.on('finish', function() { | ||
.on('end', function() { | ||
expect(errors.length).to.equal(0); | ||
@@ -255,6 +290,6 @@ expect(output).to.equal('123'); | ||
var outFile = nodePath.join(__dirname, 'test.out'); | ||
var out = fs.createWriteStream(outFile, 'utf8'); | ||
var out = fs.createWriteStream(outFile, fsReadOptions); | ||
out.on('close', function() { | ||
var output = fs.readFileSync(outFile, 'utf8'); | ||
var output = fs.readFileSync(outFile, fsReadOptions); | ||
expect(errors.length).to.equal(0); | ||
@@ -292,3 +327,3 @@ expect(output).to.equal('123'); | ||
var helloReadStream = fs.createReadStream(nodePath.join(__dirname, 'hello.txt'), 'utf8'); | ||
var helloReadStream = fs.createReadStream(nodePath.join(__dirname, 'hello.txt'), fsReadOptions); | ||
helloReadStream.pipe(asyncOut); | ||
@@ -328,3 +363,3 @@ | ||
var helloReadStream = fs.createReadStream(nodePath.join(__dirname, 'hello.txt'), 'utf8'); | ||
var helloReadStream = fs.createReadStream(nodePath.join(__dirname, 'hello.txt'), fsReadOptions); | ||
helloReadStream.pipe(asyncOut); | ||
@@ -334,3 +369,3 @@ | ||
out.on('finish', function() { | ||
out.on('end', function() { | ||
expect(outStr).to.equal('1Hello World2'); | ||
@@ -337,0 +372,0 @@ done(); |
39615
1021
4