Skip to content

Commit

Permalink
added a WritableStream interface again
Browse files Browse the repository at this point in the history
this time, it's implementing stream.Writable
  • Loading branch information
fb55 committed Mar 31, 2013
1 parent 70c6865 commit 4a7eb12
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/Stream.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
module.exports = Stream;

var Parser = require("./Parser.js");
var Parser = require("./WritableStream.js");

function Stream(options){
Parser.call(this, new cbs(this), options);
Parser.call(this, new Cbs(this), options);
}

require("util").inherits(Stream, Parser);

Stream.prototype.readable = true;

var cbs = function(scope){
function Cbs(scope){
this.scope = scope;
};
}

var EVENTS = require("../").EVENTS;

Object.keys(EVENTS).forEach(function(name){
if(EVENTS[name] === 0){
cbs.prototype["on" + name] = function(){
Cbs.prototype["on" + name] = function(){
this.scope.emit(name);
};
} else if(EVENTS[name] === 1){
cbs.prototype["on" + name] = function(a){
Cbs.prototype["on" + name] = function(a){
this.scope.emit(name, a);
};
} else if(EVENTS[name] === 2){
cbs.prototype["on" + name] = function(a, b){
Cbs.prototype["on" + name] = function(a, b){
this.scope.emit(name, a, b);
};
} else {
Expand Down
21 changes: 21 additions & 0 deletions lib/WritableStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = Stream;

var Parser = require("./Parser.js"),
WritableStream = require("stream").Writable || require("readable-stream").Writable;

function Stream(cbs, options){
var parser = this._parser = new Parser(cbs, options);

WritableStream.call(this, {decodeStrings: false});

this.once("finish", function(){
parser.end();
});
}

require("util").inherits(Stream, WritableStream);

WritableStream.prototype._write = function(chunk, encoding, cb){
this._parser.write(chunk);
cb();
};
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
return defineProp("Stream", require("./Stream.js"));
},
get WritableStream(){
return defineProp("WritableStream", this.Parser);
return defineProp("WritableStream", require("./WritableStream.js"));
},
get ProxyHandler(){
return defineProp("ProxyHandler", require("./ProxyHandler.js"));
Expand Down

0 comments on commit 4a7eb12

Please sign in to comment.