irc-framework
Version:
A better IRC framework for node.js
193 lines (188 loc) • 9.09 kB
JavaScript
'use strict';
/**
* Websocket transport
*/
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.symbol.to-primitive.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.date.to-primitive.js");
require("core-js/modules/es.number.constructor.js");
require("core-js/modules/es.object.create.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-prototype-of.js");
require("core-js/modules/es.reflect.construct.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.date.to-json.js");
require("core-js/modules/es.function.bind.js");
require("core-js/modules/es.object.set-prototype-of.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/web.timers.js");
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var EventEmitter = require('eventemitter3');
module.exports = /*#__PURE__*/function (_EventEmitter) {
function Connection(options) {
var _this;
_classCallCheck(this, Connection);
_this = _callSuper(this, Connection);
_this.options = options || {};
_this.socket = null;
_this.connected = false;
_this.last_socket_error = null;
_this.encoding = 'utf8';
_this.incoming_buffer = '';
_this.protocol_fallback = false;
// JSON does not allow undefined and websocket protocol does not allow falsy
// if the protocol is falsy then the user intends no protocol, so set to undefined
_this.protocol = options.websocket_protocol ? options.websocket_protocol : undefined;
return _this;
}
_inherits(Connection, _EventEmitter);
return _createClass(Connection, [{
key: "isConnected",
value: function isConnected() {
return this.connected;
}
}, {
key: "writeLine",
value: function writeLine(line, cb) {
this.debugOut('writeLine() socket=' + (this.socket ? 'yes' : 'no') + ' connected=' + this.connected);
if (this.socket && this.connected) {
this.socket.send(line);
}
// Websocket.send() does not support callbacks
// call the callback in the next tick instead
if (cb) {
setTimeout(cb, 0);
}
}
}, {
key: "debugOut",
value: function debugOut(out) {
this.emit('debug', out);
}
}, {
key: "connect",
value: function connect() {
var that = this;
var options = this.options;
var socket = null;
var ws_addr = '';
this.debugOut('Connection.connect()');
this.disposeSocket();
this.requested_disconnect = false;
// Build the websocket address. eg. ws://ws.rizon.net:8080
ws_addr += options.tls || options.ssl ? 'wss://' : 'ws://';
ws_addr += options.host;
ws_addr += options.port ? ':' + options.port : '';
ws_addr += options.path ? options.path : '';
socket = this.socket = new WebSocket(ws_addr, this.protocol);
socket.onopen = function () {
that.onSocketFullyConnected();
};
socket.onclose = function (event) {
that.onSocketClose(event);
};
socket.onmessage = function (event) {
that.onSocketMessage(event.data);
};
socket.onerror = function (err) {
that.debugOut('socketError() ' + err.message);
that.last_socket_error = err;
};
}
// Called when the socket is connected and ready to start sending/receiving data.
}, {
key: "onSocketFullyConnected",
value: function onSocketFullyConnected() {
this.debugOut('socketFullyConnected()');
this.last_socket_error = null;
this.connected = true;
this.emit('open');
}
}, {
key: "onSocketClose",
value: function onSocketClose(event) {
var possible_protocol_error = !this.connected && event.code === 1006;
if (possible_protocol_error && !this.protocol_fallback && this.protocol !== undefined) {
// First connection attempt failed possibly due to mismatched protocol,
// retry the connection with undefined protocol
// After this attempt, normal reconnect functions apply which will
// reconstruct this websocket, resetting these variables
this.debugOut('socketClose() possible protocol error, retrying with no protocol');
this.protocol_fallback = true;
this.protocol = undefined;
this.connect();
return;
}
this.debugOut('socketClose()');
this.connected = false;
this.emit('close', this.last_socket_error ? this.last_socket_error : false);
}
}, {
key: "onSocketMessage",
value: function onSocketMessage(data) {
if (typeof data !== 'string') {
this.last_socket_error = new Error('Websocket received unexpected binary data, closing the connection');
this.debugOut('socketData() ' + this.last_socket_error.message);
this.close();
return;
}
this.debugOut('socketData() ' + JSON.stringify(data));
var that = this;
var lines = null;
this.incoming_buffer += data + '\n';
lines = this.incoming_buffer.split('\n');
if (lines[lines.length - 1] !== '') {
this.incoming_buffer = lines.pop();
} else {
lines.pop();
this.incoming_buffer = '';
}
lines.forEach(function (line) {
that.emit('line', line);
});
}
}, {
key: "disposeSocket",
value: function disposeSocket() {
this.debugOut('Connection.disposeSocket() connected=' + this.connected);
if (this.socket && this.connected) {
this.socket.close();
}
if (this.socket) {
this.socket.onopen = null;
this.socket.onclose = null;
this.socket.onmessage = null;
this.socket.onerror = null;
this.socket = null;
}
}
}, {
key: "close",
value: function close() {
if (this.socket && this.connected) {
this.socket.close();
}
}
}, {
key: "setEncoding",
value: function setEncoding(encoding) {}
}]);
}(EventEmitter);