a zip code crypto-currency system good for red ONLY

protocol.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function() {
  2. var PROTOCOL_6, PROTOCOL_7, Parser, ProtocolError,
  3. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  4. exports.PROTOCOL_6 = PROTOCOL_6 = 'http://livereload.com/protocols/official-6';
  5. exports.PROTOCOL_7 = PROTOCOL_7 = 'http://livereload.com/protocols/official-7';
  6. exports.ProtocolError = ProtocolError = (function() {
  7. function ProtocolError(reason, data) {
  8. this.message = "LiveReload protocol error (" + reason + ") after receiving data: \"" + data + "\".";
  9. }
  10. return ProtocolError;
  11. })();
  12. exports.Parser = Parser = (function() {
  13. function Parser(handlers) {
  14. this.handlers = handlers;
  15. this.reset();
  16. }
  17. Parser.prototype.reset = function() {
  18. return this.protocol = null;
  19. };
  20. Parser.prototype.process = function(data) {
  21. var command, e, message, options, _ref;
  22. try {
  23. if (this.protocol == null) {
  24. if (data.match(/^!!ver:([\d.]+)$/)) {
  25. this.protocol = 6;
  26. } else if (message = this._parseMessage(data, ['hello'])) {
  27. if (!message.protocols.length) {
  28. throw new ProtocolError("no protocols specified in handshake message");
  29. } else if (__indexOf.call(message.protocols, PROTOCOL_7) >= 0) {
  30. this.protocol = 7;
  31. } else if (__indexOf.call(message.protocols, PROTOCOL_6) >= 0) {
  32. this.protocol = 6;
  33. } else {
  34. throw new ProtocolError("no supported protocols found");
  35. }
  36. }
  37. return this.handlers.connected(this.protocol);
  38. } else if (this.protocol === 6) {
  39. message = JSON.parse(data);
  40. if (!message.length) {
  41. throw new ProtocolError("protocol 6 messages must be arrays");
  42. }
  43. command = message[0], options = message[1];
  44. if (command !== 'refresh') {
  45. throw new ProtocolError("unknown protocol 6 command");
  46. }
  47. return this.handlers.message({
  48. command: 'reload',
  49. path: options.path,
  50. liveCSS: (_ref = options.apply_css_live) != null ? _ref : true
  51. });
  52. } else {
  53. message = this._parseMessage(data, ['reload', 'alert']);
  54. return this.handlers.message(message);
  55. }
  56. } catch (_error) {
  57. e = _error;
  58. if (e instanceof ProtocolError) {
  59. return this.handlers.error(e);
  60. } else {
  61. throw e;
  62. }
  63. }
  64. };
  65. Parser.prototype._parseMessage = function(data, validCommands) {
  66. var e, message, _ref;
  67. try {
  68. message = JSON.parse(data);
  69. } catch (_error) {
  70. e = _error;
  71. throw new ProtocolError('unparsable JSON', data);
  72. }
  73. if (!message.command) {
  74. throw new ProtocolError('missing "command" key', data);
  75. }
  76. if (_ref = message.command, __indexOf.call(validCommands, _ref) < 0) {
  77. throw new ProtocolError("invalid command '" + message.command + "', only valid commands are: " + (validCommands.join(', ')) + ")", data);
  78. }
  79. return message;
  80. };
  81. return Parser;
  82. })();
  83. }).call(this);