Created
September 8, 2011 23:32
-
-
Save ollym/1205094 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Stream(host, port, mode) { ... } | |
Stream.new = function(host, port, mode) { return new Stream(host, port, mode); } | |
Stream.prototype = Object.create(Object.prototype, { | |
readable: { get: function() { ... }, | |
writable: true, enumerable: false, configurable: true }, | |
writable: { get: function() { ... }, | |
writable: true, enumerable: false, configurable: true }, | |
read: { value: function(callback) { ... }, | |
writable: true, enumerable: false, configurable: true }, | |
write: { value: function(data) { ... }, | |
writable: true, enumerable: false, configurable: true }, | |
pipe: { value: function(strm) { | |
if ( ! (strm instanceof Stream)) | |
throw new TypeError('Stream.pipe must only be given a valid stream as its first parameter.') | |
this.read(strm.write.bind(strm)); | |
}, writable: true, enumerable: false, configurable: true } | |
}); | |
function MemoryStream(capacity) { ... | |
Object.defineProperty(this, 'buffer', { | |
value: new Buffer(capacity), configurable: true | |
}); | |
} | |
MemoryStream.new = function(cap) { return new MemoryStream(cap); } | |
MemoryStream.prototype = Object.create(Stream.prototype, { | |
capacity: { get: function() { return this.buffer.capacity } }, | |
size: { get: function() { return this.buffer.size } }, | |
buffer: { value: new Buffer(), configurable: true }, | |
readable: { get: function() { return this.size > 0 } }, | |
writable: { get: function() { return this.capacity > this.size } }, | |
read: { value: function() { | |
return buffer.empty(); | |
}, writable: true, enumerable: false, configurable: true }, | |
write: { value: function(data) { | |
buffer.push(data); | |
}, writable: true, enumerable: false, configurable: true }, | |
}, writable: true, enumerable: false, configurable: true } | |
); | |
var strm = Stream.new('127.0.0.1', 8080), | |
mstrm = MemoryStream.new(1024); | |
mstrm.pipe(strm); | |
mstrm.write('HelloWorld!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment