a zip code crypto-currency system good for red ONLY

draft75.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. var Base = require('./base'),
  3. util = require('util');
  4. var Draft75 = function(request, url, options) {
  5. Base.apply(this, arguments);
  6. this._stage = 0;
  7. this.version = 'hixie-75';
  8. this._headers.set('Upgrade', 'WebSocket');
  9. this._headers.set('Connection', 'Upgrade');
  10. this._headers.set('WebSocket-Origin', this._request.headers.origin);
  11. this._headers.set('WebSocket-Location', this.url);
  12. };
  13. util.inherits(Draft75, Base);
  14. var instance = {
  15. close: function() {
  16. if (this.readyState === 3) return false;
  17. this.readyState = 3;
  18. this.emit('close', new Base.CloseEvent(null, null));
  19. return true;
  20. },
  21. parse: function(chunk) {
  22. if (this.readyState > 1) return;
  23. this._reader.put(chunk);
  24. this._reader.eachByte(function(octet) {
  25. var message;
  26. switch (this._stage) {
  27. case -1:
  28. this._body.push(octet);
  29. this._sendHandshakeBody();
  30. break;
  31. case 0:
  32. this._parseLeadingByte(octet);
  33. break;
  34. case 1:
  35. this._length = (octet & 0x7F) + 128 * this._length;
  36. if (this._closing && this._length === 0) {
  37. return this.close();
  38. }
  39. else if ((octet & 0x80) !== 0x80) {
  40. if (this._length === 0) {
  41. this._stage = 0;
  42. }
  43. else {
  44. this._skipped = 0;
  45. this._stage = 2;
  46. }
  47. }
  48. break;
  49. case 2:
  50. if (octet === 0xFF) {
  51. this._stage = 0;
  52. message = new Buffer(this._buffer).toString('utf8', 0, this._buffer.length);
  53. this.emit('message', new Base.MessageEvent(message));
  54. }
  55. else {
  56. if (this._length) {
  57. this._skipped += 1;
  58. if (this._skipped === this._length)
  59. this._stage = 0;
  60. } else {
  61. this._buffer.push(octet);
  62. if (this._buffer.length > this._maxLength) return this.close();
  63. }
  64. }
  65. break;
  66. }
  67. }, this);
  68. },
  69. frame: function(buffer) {
  70. if (this.readyState === 0) return this._queue([buffer]);
  71. if (this.readyState > 1) return false;
  72. if (typeof buffer !== 'string') buffer = buffer.toString();
  73. var payload = new Buffer(buffer, 'utf8'),
  74. frame = new Buffer(payload.length + 2);
  75. frame[0] = 0x00;
  76. frame[payload.length + 1] = 0xFF;
  77. payload.copy(frame, 1);
  78. this._write(frame);
  79. return true;
  80. },
  81. _handshakeResponse: function() {
  82. var start = 'HTTP/1.1 101 Web Socket Protocol Handshake',
  83. headers = [start, this._headers.toString(), ''];
  84. return new Buffer(headers.join('\r\n'), 'utf8');
  85. },
  86. _parseLeadingByte: function(octet) {
  87. if ((octet & 0x80) === 0x80) {
  88. this._length = 0;
  89. this._stage = 1;
  90. } else {
  91. delete this._length;
  92. delete this._skipped;
  93. this._buffer = [];
  94. this._stage = 2;
  95. }
  96. }
  97. };
  98. for (var key in instance)
  99. Draft75.prototype[key] = instance[key];
  100. module.exports = Draft75;