index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. "use strict";
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. 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; }; }();
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. 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; }
  6. 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; }
  7. /* This header is placed at the beginning of the output file and defines the
  8. special `__require`, `__getFilename`, and `__getDirname` functions.
  9. */
  10. (function () {
  11. /* __modules is an Array of functions; each function is a module added
  12. to the project */
  13. var __modules = {},
  14. /* __modulesCache is an Array of cached modules, much like
  15. `require.cache`. Once a module is executed, it is cached. */
  16. __modulesCache = {},
  17. /* __moduleIsCached - an Array of booleans, `true` if module is cached. */
  18. __moduleIsCached = {};
  19. /* If the module with the specified `uid` is cached, return it;
  20. otherwise, execute and cache it first. */
  21. function __require(uid, parentUid) {
  22. if (!__moduleIsCached[uid]) {
  23. // Populate the cache initially with an empty `exports` Object
  24. __modulesCache[uid] = { "exports": {}, "loaded": false };
  25. __moduleIsCached[uid] = true;
  26. if (uid === 0 && typeof require === "function") {
  27. require.main = __modulesCache[0];
  28. } else {
  29. __modulesCache[uid].parent = __modulesCache[parentUid];
  30. }
  31. /* Note: if this module requires itself, or if its depenedencies
  32. require it, they will only see an empty Object for now */
  33. // Now load the module
  34. __modules[uid].call(this, __modulesCache[uid], __modulesCache[uid].exports);
  35. __modulesCache[uid].loaded = true;
  36. }
  37. return __modulesCache[uid].exports;
  38. }
  39. /* This function is the replacement for all `__filename` references within a
  40. project file. The idea is to return the correct `__filename` as if the
  41. file was not concatenated at all. Therefore, we should return the
  42. filename relative to the output file's path.
  43. `path` is the path relative to the output file's path at the time the
  44. project file was concatenated and added to the output file.
  45. */
  46. function __getFilename(path) {
  47. return require("path").resolve(__dirname + "/" + path);
  48. }
  49. /* Same deal as __getFilename.
  50. `path` is the path relative to the output file's path at the time the
  51. project file was concatenated and added to the output file.
  52. */
  53. function __getDirname(path) {
  54. return require("path").resolve(__dirname + "/" + path + "/../");
  55. }
  56. /********** End of header **********/
  57. /********** Start module 0: /home/blake/Repositories/ws-wrapper/lib/wrapper.js **********/
  58. __modules[0] = function (module, exports) {
  59. "use strict";
  60. var WebSocketChannel = __require(1, 0);
  61. var WebSocketWrapper = function (_WebSocketChannel) {
  62. _inherits(WebSocketWrapper, _WebSocketChannel);
  63. function WebSocketWrapper(socket, options) {
  64. _classCallCheck(this, WebSocketWrapper);
  65. var _this = _possibleConstructorReturn(this, (WebSocketWrapper.__proto__ || Object.getPrototypeOf(WebSocketWrapper)).call(this));
  66. _this._wrapper = _this;
  67. options = options || {};
  68. if (typeof options.debug === "function") {
  69. _this._debug = options.debug;
  70. } else if (options.debug === true) {
  71. _this._debug = console.log.bind(console);
  72. } else {
  73. _this._debug = function () {}; // no-op
  74. }
  75. if (typeof options.errorToJSON !== "function") {
  76. _this._errorToJSON = function (err) {
  77. if (typeof window === "undefined") {
  78. return JSON.stringify({ "message": err.message });
  79. } else {
  80. return JSON.stringify(err, Object.getOwnPropertyNames(err));
  81. }
  82. };
  83. } else {
  84. _this._errorToJSON = options.errorToJSON;
  85. }
  86. if (options.requestTimeout > 0) _this._requestTimeout = options.requestTimeout | 0;
  87. _this._opened = false;
  88. _this._pendingSend = [];
  89. _this._lastRequestId = 0;
  90. /* Object of pending requests; keys are the request ID, values are
  91. Objects containing `resolve` and `reject` functions used to
  92. resolve the request's Promise. */
  93. _this._pendingRequests = {};
  94. /* Object of WebSocketChannels (except `this` associated with this
  95. WebSocket); keys are the channel name. */
  96. _this._channels = {};
  97. _this._data = {};
  98. _this._socket = null;
  99. if (socket && socket.constructor) {
  100. _this.bind(socket);
  101. }
  102. return _this;
  103. }
  104. _createClass(WebSocketWrapper, [{
  105. key: "bind",
  106. value: function bind(socket) {
  107. var _this2 = this;
  108. if (this._socket) {
  109. var s = this._socket;
  110. s.onopen = s.onmessage = s.onerror = s.onclose = null;
  111. }
  112. this._socket = socket;
  113. socket.onopen = function (event) {
  114. _this2._opened = true;
  115. _this2._debug("socket: onopen");
  116. for (var i = 0; i < _this2._pendingSend.length; i++) {
  117. if (_this2.isConnected) {
  118. _this2._debug("wrapper: Sending pending message:", _this2._pendingSend[i]);
  119. try {
  120. _this2._socket.send(_this2._pendingSend[i]);
  121. } catch (e) {
  122. _this2._pendingSend = _this2._pendingSend.slice(i - 1);
  123. throw e;
  124. }
  125. } else {
  126. break;
  127. }
  128. }
  129. _this2._pendingSend = _this2._pendingSend.slice(i);
  130. _this2.emit("open", event);
  131. };
  132. socket.onmessage = function (event) {
  133. _this2._debug("socket: onmessage", event.data);
  134. _this2.emit("message", event, event.data);
  135. _this2._onMessage(event.data);
  136. };
  137. socket.onerror = function (event) {
  138. _this2._debug("socket: onerror", event);
  139. _this2.emit("error", event);
  140. };
  141. socket.onclose = function (event) {
  142. var opened = _this2._opened;
  143. _this2._opened = false;
  144. _this2._debug("socket: onclose", event);
  145. _this2.emit("close", event, opened);
  146. _this2.emit("disconnect", event, opened);
  147. };
  148. if (this.isConnected) {
  149. socket.onopen();
  150. }
  151. return this;
  152. }
  153. }, {
  154. key: "abort",
  155. value: function abort() {
  156. for (var id in this._pendingRequests) {
  157. this._pendingRequests[id].reject(new Error("Request was aborted"));
  158. }
  159. this._pendingRequests = {};
  160. this._pendingSend = [];
  161. return this;
  162. }
  163. }, {
  164. key: "of",
  165. value: function of(namespace) {
  166. if (namespace == null) {
  167. return this;
  168. }
  169. if (!this._channels[namespace]) {
  170. this._channels[namespace] = new WebSocketChannel(namespace, this);
  171. }
  172. return this._channels[namespace];
  173. }
  174. }, {
  175. key: "send",
  176. value: function send(data, ignoreMaxQueueSize) {
  177. if (this.isConnected) {
  178. this._debug("wrapper: Sending message:", data);
  179. this._socket.send(data);
  180. } else if (ignoreMaxQueueSize || this._pendingSend.length < WebSocketWrapper.MAX_SEND_QUEUE_SIZE) {
  181. this._debug("wrapper: Queuing message:", data);
  182. this._pendingSend.push(data);
  183. } else {
  184. throw new Error("WebSocket is not connected and send queue is full");
  185. }
  186. return this;
  187. }
  188. }, {
  189. key: "disconnect",
  190. value: function disconnect() {
  191. if (this._socket) this._socket.close.apply(this._socket, arguments);
  192. return this;
  193. }
  194. }, {
  195. key: "_onMessage",
  196. value: function _onMessage(msg) {
  197. try {
  198. msg = JSON.parse(msg);
  199. if (msg["ws-wrapper"] === false) return;
  200. if (msg.a) {
  201. var argsArray = [];
  202. for (var i in msg.a) {
  203. argsArray[i] = msg.a[i];
  204. }
  205. msg.a = argsArray;
  206. }
  207. /* If `msg` does not have an `a` Array with at least 1 element,
  208. ignore the message because it is not a valid event/request */
  209. if (msg.a instanceof Array && msg.a.length >= 1 && (msg.c || WebSocketChannel.NO_WRAP_EVENTS.indexOf(msg.a[0]) < 0)) {
  210. var event = {
  211. "name": msg.a.shift(),
  212. "args": msg.a,
  213. "requestId": msg.i
  214. };
  215. var channel = msg.c == null ? this : this._channels[msg.c];
  216. if (!channel) {
  217. if (msg.i >= 0) {
  218. this._sendReject(msg.i, new Error("Channel '" + msg.c + "' does not exist"));
  219. }
  220. this._debug("wrapper: Event '" + event.name + "' ignored " + ("because channel '" + msg.c + "' does not exist."));
  221. } else if (channel._emitter.emit(event.name, event)) {
  222. this._debug("wrapper: Event '" + event.name + "' sent to " + "event listener");
  223. } else {
  224. if (msg.i >= 0) {
  225. this._sendReject(msg.i, new Error("No event listener for '" + event.name + "'" + (msg.c ? " on channel '" + msg.c + "'" : "")));
  226. }
  227. this._debug("wrapper: Event '" + event.name + "' had no " + "event listener");
  228. }
  229. } else if (this._pendingRequests[msg.i]) {
  230. this._debug("wrapper: Processing response for request", msg.i);
  231. if (msg.e !== undefined) {
  232. var err = msg.e;
  233. if (msg._ && err) {
  234. err = new Error(err.message);
  235. for (var key in msg.e) {
  236. err[key] = msg.e[key];
  237. }
  238. }
  239. this._pendingRequests[msg.i].reject(err);
  240. } else {
  241. this._pendingRequests[msg.i].resolve(msg.d);
  242. }
  243. clearTimeout(this._pendingRequests[msg.i].timer);
  244. delete this._pendingRequests[msg.i];
  245. }
  246. } catch (e) {
  247. /* Note: It's also possible for uncaught exceptions from event
  248. handlers to end up here. */
  249. }
  250. }
  251. /* The following methods are called by a WebSocketChannel to send data
  252. to the Socket. */
  253. }, {
  254. key: "_sendEvent",
  255. value: function _sendEvent(channel, eventName, args, isRequest) {
  256. var _this3 = this;
  257. var data = { "a": args };
  258. if (channel != null) {
  259. data.c = channel;
  260. }
  261. var request;
  262. if (isRequest) {
  263. /* Unless we send petabytes of data using the same socket,
  264. we won't worry about `_lastRequestId` getting too big. */
  265. data.i = ++this._lastRequestId;
  266. request = new Promise(function (resolve, reject) {
  267. var pendReq = _this3._pendingRequests[data.i] = {
  268. "resolve": resolve,
  269. "reject": reject
  270. };
  271. if (_this3._requestTimeout > 0) {
  272. pendReq.timer = setTimeout(function () {
  273. reject(new Error("Request timed out"));
  274. delete _this3._pendingRequests[data.i];
  275. }, _this3._requestTimeout);
  276. }
  277. });
  278. }
  279. this.send(JSON.stringify(data));
  280. return request;
  281. }
  282. }, {
  283. key: "_sendResolve",
  284. value: function _sendResolve(id, data) {
  285. this.send(JSON.stringify({
  286. "i": id,
  287. "d": data
  288. }), true /* ignore max queue length */);
  289. }
  290. }, {
  291. key: "_sendReject",
  292. value: function _sendReject(id, err) {
  293. var isError = err instanceof Error;
  294. if (isError) {
  295. err = JSON.parse(this._errorToJSON(err));
  296. }
  297. this.send(JSON.stringify({
  298. "i": id,
  299. "e": err,
  300. "_": isError ? 1 : undefined
  301. }), true /* ignore max queue length */);
  302. }
  303. }, {
  304. key: "get",
  305. value: function get(key) {
  306. return this._data[key];
  307. }
  308. }, {
  309. key: "set",
  310. value: function set(key, value) {
  311. this._data[key] = value;
  312. return this;
  313. }
  314. }, {
  315. key: "socket",
  316. get: function get() {
  317. return this._socket;
  318. },
  319. set: function set(socket) {
  320. this.bind(socket);
  321. }
  322. }, {
  323. key: "isConnecting",
  324. get: function get() {
  325. return this._socket && this._socket.readyState === this._socket.constructor.CONNECTING;
  326. }
  327. }, {
  328. key: "isConnected",
  329. get: function get() {
  330. return this._socket && this._socket.readyState === this._socket.constructor.OPEN;
  331. }
  332. }]);
  333. return WebSocketWrapper;
  334. }(WebSocketChannel);
  335. /* Maximum number of items in the send queue. If a user tries to send more
  336. messages than this number while a WebSocket is not connected, errors will
  337. be thrown. */
  338. WebSocketWrapper.MAX_SEND_QUEUE_SIZE = 10;
  339. module.exports = WebSocketWrapper;
  340. return module.exports;
  341. };
  342. /********** End of module 0: /home/blake/Repositories/ws-wrapper/lib/wrapper.js **********/
  343. /********** Start module 1: /home/blake/Repositories/ws-wrapper/lib/channel.js **********/
  344. __modules[1] = function (module, exports) {
  345. "use strict";
  346. var EventEmitter = __require(2, 1).EventEmitter;
  347. /* A WebSocketChannel exposes an EventEmitter-like API for sending and handling
  348. events or requests over the channel through the attached WebSocketWrapper.
  349. `var channel = new WebSocketChannel(name, socketWrapper);`
  350. - `name` - the namespace for the channel
  351. - `socketWrapper` - the WebSocketWrapper instance to which data should
  352. be sent
  353. */
  354. var WebSocketChannel = function () {
  355. function WebSocketChannel(name, socketWrapper) {
  356. _classCallCheck(this, WebSocketChannel);
  357. this._name = name;
  358. this._wrapper = socketWrapper;
  359. this._emitter = new EventEmitter();
  360. this._wrappedListeners = new WeakMap();
  361. }
  362. _createClass(WebSocketChannel, [{
  363. key: "on",
  364. /* Expose EventEmitter-like API
  365. When `eventName` is one of the `NO_WRAP_EVENTS`, the event handlers
  366. are left untouched, and the emitted events are just sent to the
  367. EventEmitter; otherwise, event listeners are wrapped to process the
  368. incoming request and the emitted events are sent to the WebSocketWrapper
  369. to be serialized and sent over the WebSocket. */
  370. value: function on(eventName, listener) {
  371. if (this._name == null && WebSocketChannel.NO_WRAP_EVENTS.indexOf(eventName) >= 0)
  372. /* Note: The following is equivalent to:
  373. `this._emitter.on(eventName, listener.bind(this));`
  374. But thanks to eventemitter3, the following is a touch faster. */
  375. this._emitter.on(eventName, listener, this);else this._emitter.on(eventName, this._wrapListener(listener));
  376. return this;
  377. }
  378. }, {
  379. key: "once",
  380. value: function once(eventName, listener) {
  381. if (this._name == null && WebSocketChannel.NO_WRAP_EVENTS.indexOf(eventName) >= 0) this._emitter.once(eventName, listener, this);else this._emitter.once(eventName, this._wrapListener(listener));
  382. return this;
  383. }
  384. }, {
  385. key: "removeListener",
  386. value: function removeListener(eventName, listener) {
  387. if (this._name == null && WebSocketChannel.NO_WRAP_EVENTS.indexOf(eventName) >= 0) this._emitter.removeListener(eventName, listener);else this._emitter.removeListener(eventName, this._wrappedListeners.get(listener));
  388. return this;
  389. }
  390. }, {
  391. key: "removeAllListeners",
  392. value: function removeAllListeners(eventName) {
  393. this._emitter.removeAllListeners(eventName);
  394. return this;
  395. }
  396. }, {
  397. key: "eventNames",
  398. value: function eventNames() {
  399. return this._emitter.eventNames();
  400. }
  401. }, {
  402. key: "listeners",
  403. value: function listeners(eventName) {
  404. if (this._name == null && WebSocketChannel.NO_WRAP_EVENTS.indexOf(eventName) >= 0) return this._emitter.listeners(eventName);else {
  405. return this._emitter.listeners(eventName).map(function (wrapper) {
  406. return wrapper._original;
  407. });
  408. }
  409. }
  410. /* The following `emit` and `request` methods will serialize and send the
  411. event over the WebSocket using the WebSocketWrapper. */
  412. }, {
  413. key: "emit",
  414. value: function emit(eventName) {
  415. if (this._name == null && WebSocketChannel.NO_WRAP_EVENTS.indexOf(eventName) >= 0) return this._emitter.emit.apply(this._emitter, arguments);else return this._wrapper._sendEvent(this._name, eventName, arguments);
  416. }
  417. /* Temporarily set the request timeout for the next request. */
  418. }, {
  419. key: "timeout",
  420. value: function timeout(tempTimeout) {
  421. this._tempTimeout = tempTimeout;
  422. return this;
  423. }
  424. }, {
  425. key: "request",
  426. value: function request(eventName) {
  427. var oldTimeout = this._wrapper._requestTimeout;
  428. if (this._tempTimeout !== undefined) {
  429. this._wrapper._requestTimeout = this._tempTimeout;
  430. delete this._tempTimeout;
  431. }
  432. var ret = this._wrapper._sendEvent(this._name, eventName, arguments, true);
  433. this._wrapper._requestTimeout = oldTimeout;
  434. return ret;
  435. }
  436. }, {
  437. key: "_wrapListener",
  438. value: function _wrapListener(listener) {
  439. if (typeof listener !== "function") {
  440. throw new TypeError("\"listener\" argument must be a function");
  441. }
  442. var wrapped = this._wrappedListeners.get(listener);
  443. if (!wrapped) {
  444. wrapped = function channelListenerWrapper(event) {
  445. var _this4 = this;
  446. /* This function is called when an event is emitted on this
  447. WebSocketChannel's `_emitter` when the WebSocketWrapper
  448. receives an incoming message for this channel. If this
  449. event is a request, special processing is needed to
  450. send the response back over the socket. Below we use
  451. the return value from the original `listener` to
  452. determine what response should be sent back.
  453. `this` refers to the WebSocketChannel instance
  454. `event` has the following properties:
  455. - `name`
  456. - `args`
  457. - `requestId`
  458. */
  459. try {
  460. var returnVal = listener.apply(this, event.args);
  461. } catch (err) {
  462. if (event.requestId >= 0) {
  463. /* If event listener throws, pass that Error back
  464. as a response to the request */
  465. this._wrapper._sendReject(event.requestId, err);
  466. }
  467. throw err;
  468. }
  469. if (returnVal instanceof Promise) {
  470. /* If event listener returns a Promise, respond once
  471. the Promise resolves */
  472. returnVal.then(function (data) {
  473. if (event.requestId >= 0) {
  474. _this4._wrapper._sendResolve(event.requestId, data);
  475. }
  476. }).catch(function (err) {
  477. if (event.requestId >= 0) {
  478. _this4._wrapper._sendReject(event.requestId, err);
  479. }
  480. });
  481. } else if (event.requestId >= 0) {
  482. /* Otherwise, assume that the `returnVal` is what
  483. should be passed back as the response */
  484. this._wrapper._sendResolve(event.requestId, returnVal);
  485. }
  486. }.bind(this); // Bind the channel to the `channelListenerWrapper`
  487. wrapped._original = listener;
  488. this._wrappedListeners.set(listener, wrapped);
  489. }
  490. return wrapped;
  491. }
  492. }, {
  493. key: "get",
  494. value: function get(key) {
  495. return this._wrapper.get(key);
  496. }
  497. }, {
  498. key: "set",
  499. value: function set(key, value) {
  500. this._wrapper.set(key, value);
  501. return this;
  502. }
  503. }, {
  504. key: "name",
  505. get: function get() {
  506. return this._name;
  507. },
  508. set: function set(name) {
  509. throw new Error("Setting the channel name is not allowed");
  510. }
  511. }]);
  512. return WebSocketChannel;
  513. }();
  514. WebSocketChannel.prototype.addListener = WebSocketChannel.prototype.on;
  515. WebSocketChannel.prototype.off = WebSocketChannel.prototype.removeListener;
  516. WebSocketChannel.NO_WRAP_EVENTS = ["open", "message", "error", "close", "disconnect"];
  517. module.exports = WebSocketChannel;
  518. return module.exports;
  519. };
  520. /********** End of module 1: /home/blake/Repositories/ws-wrapper/lib/channel.js **********/
  521. /********** Start module 2: /home/blake/Repositories/ws-wrapper/node_modules/eventemitter3/index.js **********/
  522. __modules[2] = function (module, exports) {
  523. 'use strict';
  524. var has = Object.prototype.hasOwnProperty,
  525. prefix = '~';
  526. /**
  527. * Constructor to create a storage for our `EE` objects.
  528. * An `Events` instance is a plain object whose properties are event names.
  529. *
  530. * @constructor
  531. * @api private
  532. */
  533. function Events() {}
  534. if (Object.create) {
  535. Events.prototype = Object.create(null);
  536. if (!new Events().__proto__) prefix = false;
  537. }
  538. /**
  539. * Representation of a single event listener.
  540. *
  541. * @param {Function} fn The listener function.
  542. * @param {Mixed} context The context to invoke the listener with.
  543. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  544. * @constructor
  545. * @api private
  546. */
  547. function EE(fn, context, once) {
  548. this.fn = fn;
  549. this.context = context;
  550. this.once = once || false;
  551. }
  552. /**
  553. * Minimal `EventEmitter` interface that is molded against the Node.js
  554. * `EventEmitter` interface.
  555. *
  556. * @constructor
  557. * @api public
  558. */
  559. function EventEmitter() {
  560. this._events = new Events();
  561. this._eventsCount = 0;
  562. }
  563. /**
  564. * Return an array listing the events for which the emitter has registered
  565. * listeners.
  566. *
  567. * @returns {Array}
  568. * @api public
  569. */
  570. EventEmitter.prototype.eventNames = function eventNames() {
  571. var names = [],
  572. events,
  573. name;
  574. if (this._eventsCount === 0) return names;
  575. for (name in events = this._events) {
  576. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  577. }
  578. if (Object.getOwnPropertySymbols) {
  579. return names.concat(Object.getOwnPropertySymbols(events));
  580. }
  581. return names;
  582. };
  583. /**
  584. * Return the listeners registered for a given event.
  585. *
  586. * @param {String|Symbol} event The event name.
  587. * @param {Boolean} exists Only check if there are listeners.
  588. * @returns {Array|Boolean}
  589. * @api public
  590. */
  591. EventEmitter.prototype.listeners = function listeners(event, exists) {
  592. var evt = prefix ? prefix + event : event,
  593. available = this._events[evt];
  594. if (exists) return !!available;
  595. if (!available) return [];
  596. if (available.fn) return [available.fn];
  597. for (var i = 0, l = available.length, ee = new Array(l); i < l; i++) {
  598. ee[i] = available[i].fn;
  599. }
  600. return ee;
  601. };
  602. /**
  603. * Calls each of the listeners registered for a given event.
  604. *
  605. * @param {String|Symbol} event The event name.
  606. * @returns {Boolean} `true` if the event had listeners, else `false`.
  607. * @api public
  608. */
  609. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  610. var evt = prefix ? prefix + event : event;
  611. if (!this._events[evt]) return false;
  612. var listeners = this._events[evt],
  613. len = arguments.length,
  614. args,
  615. i;
  616. if (listeners.fn) {
  617. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  618. switch (len) {
  619. case 1:
  620. return listeners.fn.call(listeners.context), true;
  621. case 2:
  622. return listeners.fn.call(listeners.context, a1), true;
  623. case 3:
  624. return listeners.fn.call(listeners.context, a1, a2), true;
  625. case 4:
  626. return listeners.fn.call(listeners.context, a1, a2, a3), true;
  627. case 5:
  628. return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  629. case 6:
  630. return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  631. }
  632. for (i = 1, args = new Array(len - 1); i < len; i++) {
  633. args[i - 1] = arguments[i];
  634. }
  635. listeners.fn.apply(listeners.context, args);
  636. } else {
  637. var length = listeners.length,
  638. j;
  639. for (i = 0; i < length; i++) {
  640. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  641. switch (len) {
  642. case 1:
  643. listeners[i].fn.call(listeners[i].context);break;
  644. case 2:
  645. listeners[i].fn.call(listeners[i].context, a1);break;
  646. case 3:
  647. listeners[i].fn.call(listeners[i].context, a1, a2);break;
  648. case 4:
  649. listeners[i].fn.call(listeners[i].context, a1, a2, a3);break;
  650. default:
  651. if (!args) for (j = 1, args = new Array(len - 1); j < len; j++) {
  652. args[j - 1] = arguments[j];
  653. }
  654. listeners[i].fn.apply(listeners[i].context, args);
  655. }
  656. }
  657. }
  658. return true;
  659. };
  660. /**
  661. * Add a listener for a given event.
  662. *
  663. * @param {String|Symbol} event The event name.
  664. * @param {Function} fn The listener function.
  665. * @param {Mixed} [context=this] The context to invoke the listener with.
  666. * @returns {EventEmitter} `this`.
  667. * @api public
  668. */
  669. EventEmitter.prototype.on = function on(event, fn, context) {
  670. var listener = new EE(fn, context || this),
  671. evt = prefix ? prefix + event : event;
  672. if (!this._events[evt]) this._events[evt] = listener, this._eventsCount++;else if (!this._events[evt].fn) this._events[evt].push(listener);else this._events[evt] = [this._events[evt], listener];
  673. return this;
  674. };
  675. /**
  676. * Add a one-time listener for a given event.
  677. *
  678. * @param {String|Symbol} event The event name.
  679. * @param {Function} fn The listener function.
  680. * @param {Mixed} [context=this] The context to invoke the listener with.
  681. * @returns {EventEmitter} `this`.
  682. * @api public
  683. */
  684. EventEmitter.prototype.once = function once(event, fn, context) {
  685. var listener = new EE(fn, context || this, true),
  686. evt = prefix ? prefix + event : event;
  687. if (!this._events[evt]) this._events[evt] = listener, this._eventsCount++;else if (!this._events[evt].fn) this._events[evt].push(listener);else this._events[evt] = [this._events[evt], listener];
  688. return this;
  689. };
  690. /**
  691. * Remove the listeners of a given event.
  692. *
  693. * @param {String|Symbol} event The event name.
  694. * @param {Function} fn Only remove the listeners that match this function.
  695. * @param {Mixed} context Only remove the listeners that have this context.
  696. * @param {Boolean} once Only remove one-time listeners.
  697. * @returns {EventEmitter} `this`.
  698. * @api public
  699. */
  700. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  701. var evt = prefix ? prefix + event : event;
  702. if (!this._events[evt]) return this;
  703. if (!fn) {
  704. if (--this._eventsCount === 0) this._events = new Events();else delete this._events[evt];
  705. return this;
  706. }
  707. var listeners = this._events[evt];
  708. if (listeners.fn) {
  709. if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
  710. if (--this._eventsCount === 0) this._events = new Events();else delete this._events[evt];
  711. }
  712. } else {
  713. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  714. if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
  715. events.push(listeners[i]);
  716. }
  717. }
  718. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;else if (--this._eventsCount === 0) this._events = new Events();else delete this._events[evt];
  719. }
  720. return this;
  721. };
  722. /**
  723. * Remove all listeners, or those of the specified event.
  724. *
  725. * @param {String|Symbol} [event] The event name.
  726. * @returns {EventEmitter} `this`.
  727. * @api public
  728. */
  729. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  730. var evt;
  731. if (event) {
  732. evt = prefix ? prefix + event : event;
  733. if (this._events[evt]) {
  734. if (--this._eventsCount === 0) this._events = new Events();else delete this._events[evt];
  735. }
  736. } else {
  737. this._events = new Events();
  738. this._eventsCount = 0;
  739. }
  740. return this;
  741. };
  742. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  743. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  744. EventEmitter.prototype.setMaxListeners = function setMaxListeners() {
  745. return this;
  746. };
  747. EventEmitter.prefixed = prefix;
  748. EventEmitter.EventEmitter = EventEmitter;
  749. if ('undefined' !== typeof module) {
  750. module.exports = EventEmitter;
  751. }
  752. return module.exports;
  753. };
  754. /********** End of module 2: /home/blake/Repositories/ws-wrapper/node_modules/eventemitter3/index.js **********/
  755. /********** Footer **********/
  756. if ((typeof module === "undefined" ? "undefined" : _typeof(module)) === "object") module.exports = __require(0);else return __require(0);
  757. })();
  758. /********** End of footer **********/