| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | Object.defineProperty(exports, "__esModule", { |
| 4 | value: true |
| 5 | }); |
| 6 | |
| 7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
| 8 | |
| 9 | var _events = require('events'); |
| 10 | |
| 11 | var _events2 = _interopRequireDefault(_events); |
| 12 | |
| 13 | var _fayeWebsocket = require('faye-websocket'); |
| 14 | |
| 15 | var _fayeWebsocket2 = _interopRequireDefault(_fayeWebsocket); |
| 16 | |
| 17 | var _objectAssign = require('object-assign'); |
| 18 | |
| 19 | var _objectAssign2 = _interopRequireDefault(_objectAssign); |
| 20 | |
| 21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 22 | |
| 23 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
| 24 | |
| 25 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } |
| 26 | |
| 27 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } |
| 28 | |
| 29 | var debug = require('debug')('tinylr:client'); |
| 30 | |
| 31 | var idCounter = 0; |
| 32 | |
| 33 | var Client = function (_events$EventEmitter) { |
| 34 | _inherits(Client, _events$EventEmitter); |
| 35 | |
| 36 | function Client(req, socket, head) { |
| 37 | var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; |
| 38 | |
| 39 | _classCallCheck(this, Client); |
| 40 | |
| 41 | var _this = _possibleConstructorReturn(this, (Client.__proto__ || Object.getPrototypeOf(Client)).call(this)); |
| 42 | |
| 43 | _this.options = options; |
| 44 | _this.ws = new _fayeWebsocket2.default(req, socket, head); |
| 45 | _this.ws.onmessage = _this.message.bind(_this); |
| 46 | _this.ws.onclose = _this.close.bind(_this); |
| 47 | _this.id = _this.uniqueId('ws'); |
| 48 | return _this; |
| 49 | } |
| 50 | |
| 51 | _createClass(Client, [{ |
| 52 | key: 'message', |
| 53 | value: function message(event) { |
| 54 | var data = this.data(event); |
| 55 | if (this[data.command]) return this[data.command](data); |
| 56 | } |
| 57 | }, { |
| 58 | key: 'close', |
| 59 | value: function close(event) { |
| 60 | if (this.ws) { |
| 61 | this.ws.close(); |
| 62 | this.ws = null; |
| 63 | } |
| 64 | |
| 65 | this.emit('end', event); |
| 66 | } |
| 67 | |
| 68 | // Commands |
| 69 | |
| 70 | }, { |
| 71 | key: 'hello', |
| 72 | value: function hello() { |
| 73 | this.send({ |
| 74 | command: 'hello', |
| 75 | protocols: ['http://livereload.com/protocols/official-7'], |
| 76 | serverName: 'tiny-lr' |
| 77 | }); |
| 78 | } |
| 79 | }, { |
| 80 | key: 'info', |
| 81 | value: function info(data) { |
| 82 | if (data) { |
| 83 | debug('Info', data); |
| 84 | this.emit('info', (0, _objectAssign2.default)({}, data, { id: this.id })); |
| 85 | this.plugins = data.plugins; |
| 86 | this.url = data.url; |
| 87 | } |
| 88 | |
| 89 | return (0, _objectAssign2.default)({}, data || {}, { id: this.id, url: this.url }); |
| 90 | } |
| 91 | |
| 92 | // Server commands |
| 93 | |
| 94 | }, { |
| 95 | key: 'reload', |
| 96 | value: function reload(files) { |
| 97 | files.forEach(function (file) { |
| 98 | this.send({ |
| 99 | command: 'reload', |
| 100 | path: file, |
| 101 | liveCSS: this.options.liveCSS !== false, |
| 102 | reloadMissingCSS: this.options.reloadMissingCSS !== false, |
| 103 | liveImg: this.options.liveImg !== false |
| 104 | }); |
| 105 | }, this); |
| 106 | } |
| 107 | }, { |
| 108 | key: 'alert', |
| 109 | value: function alert(message) { |
| 110 | this.send({ |
| 111 | command: 'alert', |
| 112 | message: message |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | // Utilities |
| 117 | |
| 118 | }, { |
| 119 | key: 'data', |
| 120 | value: function data(event) { |
| 121 | var data = {}; |
| 122 | try { |
| 123 | data = JSON.parse(event.data); |
| 124 | } catch (e) {} |
| 125 | return data; |
| 126 | } |
| 127 | }, { |
| 128 | key: 'send', |
| 129 | value: function send(data) { |
| 130 | if (!this.ws) return; |
| 131 | this.ws.send(JSON.stringify(data)); |
| 132 | } |
| 133 | }, { |
| 134 | key: 'uniqueId', |
| 135 | value: function uniqueId(prefix) { |
| 136 | var id = idCounter++; |
| 137 | return prefix ? prefix + id : id; |
| 138 | } |
| 139 | }]); |
| 140 | |
| 141 | return Client; |
| 142 | }(_events2.default.EventEmitter); |
| 143 | |
| 144 | exports.default = Client; |
| 145 | module.exports = exports['default']; |