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; };
5 exports.PROTOCOL_6 = PROTOCOL_6 = 'http://livereload.com/protocols/official-6';
7 exports.PROTOCOL_7 = PROTOCOL_7 = 'http://livereload.com/protocols/official-7';
9 exports.ProtocolError = ProtocolError = (function() {
10 function ProtocolError(reason, data) {
11 this.message = "LiveReload protocol error (" + reason + ") after receiving data: \"" + data + "\".";
18 exports.Parser = Parser = (function() {
19 function Parser(handlers) {
20 this.handlers = handlers;
24 Parser.prototype.reset = function() {
25 return this.protocol = null;
28 Parser.prototype.process = function(data) {
29 var command, e, message, options, _ref;
31 if (this.protocol == null) {
32 if (data.match(/^!!ver:([\d.]+)$/)) {
34 } else if (message = this._parseMessage(data, ['hello'])) {
35 if (!message.protocols.length) {
36 throw new ProtocolError("no protocols specified in handshake message");
37 } else if (__indexOf.call(message.protocols, PROTOCOL_7) >= 0) {
39 } else if (__indexOf.call(message.protocols, PROTOCOL_6) >= 0) {
42 throw new ProtocolError("no supported protocols found");
45 return this.handlers.connected(this.protocol);
46 } else if (this.protocol === 6) {
47 message = JSON.parse(data);
48 if (!message.length) {
49 throw new ProtocolError("protocol 6 messages must be arrays");
51 command = message[0], options = message[1];
52 if (command !== 'refresh') {
53 throw new ProtocolError("unknown protocol 6 command");
55 return this.handlers.message({
58 liveCSS: (_ref = options.apply_css_live) != null ? _ref : true
61 message = this._parseMessage(data, ['reload', 'alert']);
62 return this.handlers.message(message);
66 if (e instanceof ProtocolError) {
67 return this.handlers.error(e);
74 Parser.prototype._parseMessage = function(data, validCommands) {
77 message = JSON.parse(data);
80 throw new ProtocolError('unparsable JSON', data);
82 if (!message.command) {
83 throw new ProtocolError('missing "command" key', data);
85 if (_ref = message.command, __indexOf.call(validCommands, _ref) < 0) {
86 throw new ProtocolError("invalid command '" + message.command + "', only valid commands are: " + (validCommands.join(', ')) + ")", data);