client.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. 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; }; }();
  6. var _events = require('events');
  7. var _events2 = _interopRequireDefault(_events);
  8. var _fayeWebsocket = require('faye-websocket');
  9. var _fayeWebsocket2 = _interopRequireDefault(_fayeWebsocket);
  10. var _objectAssign = require('object-assign');
  11. var _objectAssign2 = _interopRequireDefault(_objectAssign);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. 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; }
  15. 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; }
  16. var debug = require('debug')('tinylr:client');
  17. var idCounter = 0;
  18. var Client = function (_events$EventEmitter) {
  19. _inherits(Client, _events$EventEmitter);
  20. function Client(req, socket, head) {
  21. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  22. _classCallCheck(this, Client);
  23. var _this = _possibleConstructorReturn(this, (Client.__proto__ || Object.getPrototypeOf(Client)).call(this));
  24. _this.options = options;
  25. _this.ws = new _fayeWebsocket2.default(req, socket, head);
  26. _this.ws.onmessage = _this.message.bind(_this);
  27. _this.ws.onclose = _this.close.bind(_this);
  28. _this.id = _this.uniqueId('ws');
  29. return _this;
  30. }
  31. _createClass(Client, [{
  32. key: 'message',
  33. value: function message(event) {
  34. var data = this.data(event);
  35. if (this[data.command]) return this[data.command](data);
  36. }
  37. }, {
  38. key: 'close',
  39. value: function close(event) {
  40. if (this.ws) {
  41. this.ws.close();
  42. this.ws = null;
  43. }
  44. this.emit('end', event);
  45. }
  46. // Commands
  47. }, {
  48. key: 'hello',
  49. value: function hello() {
  50. this.send({
  51. command: 'hello',
  52. protocols: ['http://livereload.com/protocols/official-7'],
  53. serverName: 'tiny-lr'
  54. });
  55. }
  56. }, {
  57. key: 'info',
  58. value: function info(data) {
  59. if (data) {
  60. debug('Info', data);
  61. this.emit('info', (0, _objectAssign2.default)({}, data, { id: this.id }));
  62. this.plugins = data.plugins;
  63. this.url = data.url;
  64. }
  65. return (0, _objectAssign2.default)({}, data || {}, { id: this.id, url: this.url });
  66. }
  67. // Server commands
  68. }, {
  69. key: 'reload',
  70. value: function reload(files) {
  71. files.forEach(function (file) {
  72. this.send({
  73. command: 'reload',
  74. path: file,
  75. liveCSS: this.options.liveCSS !== false,
  76. reloadMissingCSS: this.options.reloadMissingCSS !== false,
  77. liveImg: this.options.liveImg !== false
  78. });
  79. }, this);
  80. }
  81. }, {
  82. key: 'alert',
  83. value: function alert(message) {
  84. this.send({
  85. command: 'alert',
  86. message: message
  87. });
  88. }
  89. // Utilities
  90. }, {
  91. key: 'data',
  92. value: function data(event) {
  93. var data = {};
  94. try {
  95. data = JSON.parse(event.data);
  96. } catch (e) {}
  97. return data;
  98. }
  99. }, {
  100. key: 'send',
  101. value: function send(data) {
  102. if (!this.ws) return;
  103. this.ws.send(JSON.stringify(data));
  104. }
  105. }, {
  106. key: 'uniqueId',
  107. value: function uniqueId(prefix) {
  108. var id = idCounter++;
  109. return prefix ? prefix + id : id;
  110. }
  111. }]);
  112. return Client;
  113. }(_events2.default.EventEmitter);
  114. exports.default = Client;
  115. module.exports = exports['default'];