Guest User

Untitled

a guest
Jan 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1.  
  2. function Stream(host, port, mode) { ... }
  3. Stream.new = function(host, port, mode) { return new Stream(host, port, mode); }
  4. Stream.prototype = Object.create(Object.prototype, {
  5.  
  6. readable: { get: function() { ... },
  7. writable: true, enumerable: false, configurable: true },
  8.  
  9. writable: { get: function() { ... },
  10. writable: true, enumerable: false, configurable: true },
  11.  
  12. read: { value: function(callback) { ... },
  13. writable: true, enumerable: false, configurable: true },
  14.  
  15. write: { value: function(data) { ... },
  16. writable: true, enumerable: false, configurable: true },
  17.  
  18. pipe: { value: function(strm) {
  19.  
  20. if ( ! (strm instanceof Stream))
  21. throw new TypeError('Stream.pipe must only be given a valid stream as its first parameter.')
  22.  
  23. this.read(strm.write.bind(strm));
  24.  
  25. }, writable: true, enumerable: false, configurable: true }
  26. });
  27.  
  28. function MemoryStream(capacity) { ...
  29.  
  30. Object.defineProperty(this, 'buffer', {
  31. value: new Buffer(capacity), configurable: true
  32. });
  33. }
  34. MemoryStream.new = function(cap) { return new MemoryStream(cap); }
  35. MemoryStream.prototype = Object.create(Stream.prototype, {
  36.  
  37. capacity: { get: function() { return this.buffer.capacity } },
  38. size: { get: function() { return this.buffer.size } },
  39.  
  40. buffer: { value: new Buffer(), configurable: true },
  41.  
  42. readable: { get: function() { return this.size > 0 } },
  43. writable: { get: function() { return this.capacity > this.size } },
  44.  
  45. read: { value: function() {
  46.  
  47. return buffer.empty();
  48.  
  49. }, writable: true, enumerable: false, configurable: true },
  50.  
  51. write: { value: function(data) {
  52.  
  53. buffer.push(data);
  54.  
  55. }, writable: true, enumerable: false, configurable: true },
  56.  
  57. }, writable: true, enumerable: false, configurable: true }
  58. );
  59.  
  60. var strm = Stream.new('127.0.0.1', 8080),
  61. mstrm = MemoryStream.new(1024);
  62.  
  63. mstrm.pipe(strm);
  64. mstrm.write('HelloWorld!');
Add Comment
Please, Sign In to add comment