Skip to content

Instantly share code, notes, and snippets.

@ollym
Created September 8, 2011 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollym/1205094 to your computer and use it in GitHub Desktop.
Save ollym/1205094 to your computer and use it in GitHub Desktop.
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