protocol.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, error, 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, error, 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);