Skip to content

Instantly share code, notes, and snippets.

@blubbll
Created January 4, 2020 21:35
Show Gist options
  • Save blubbll/a3e76214f2177b192218a70b206f9bc9 to your computer and use it in GitHub Desktop.
Save blubbll/a3e76214f2177b192218a70b206f9bc9 to your computer and use it in GitHub Desktop.
/**!checkes6 by Blubbll*/
var es6 = void 0 !== Array.from && void 0 !== Array.of && void 0 !== Math.acosh && void 0 !== Math.hypot && void 0 !== Math.imul && void 0 !== Number.isInteger && void 0 !== Number.isNaN && void 0 !== Number.EPSILON && void 0 !== Object.assign && void 0 !== Promise && void 0 !== Proxy && void 0 !== Map && void 0 !== Set && void 0 !== WeakMap && void 0 !== WeakSet && void 0 !== Symbol;
window.WebSocket && es6 &&
! function(e) {
if ("object" == typeof exports && "undefined" != typeof module) module.exports = e();
else if ("function" == typeof define && define.amd) define([], e);
else {
("undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : this).OneWebSocket = e()
}
}(function() {
return function() {
return function e(t, n, r) {
function i(s, u) {
if (!n[s]) {
if (!t[s]) {
var c = "function" == typeof require && require;
if (!u && c) return c(s, !0);
if (o) return o(s, !0);
var a = new Error("Cannot find module '" + s + "'");
throw a.code = "MODULE_NOT_FOUND", a
}
var f = n[s] = {
exports: {}
};
t[s][0].call(f.exports, function(e) {
return i(t[s][1][e] || e)
}, f, f.exports, e, t, n, r)
}
return n[s].exports
}
for (var o = "function" == typeof require && require, s = 0; s < r.length; s++) i(r[s]);
return i
}
}()({
1: [function(e, t, n) {
"use strict";
var r = function() {
function e(e, t) {
for (var n = 0; n < t.length; n++) {
var r = t[n];
r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
}
}
return function(t, n, r) {
return n && e(t.prototype, n), r && e(t, r), t
}
}();
var i = e("events").EventEmitter,
o = e("ws"),
s = "function" == typeof o ? o : window.WebSocket,
u = function(e) {
function t(e, n) {
! function(e, t) {
if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}(this, t);
var r = function(e, t) {
if (!e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
return !t || "object" != typeof t && "function" != typeof t ? e : t
}(this, (t.__proto__ || Object.getPrototypeOf(t)).call(this));
return r._validateUrl(e), r._url = e, r._options = function(e) {
var t = {
autoReconnect: !1 !== e.autoReconnect,
maxReconnectAttempts: 1 / 0
};
Number.isInteger(e.maxReconnectAttempts) && (t.maxReconnectAttempts = e.maxReconnectAttempts);
return Object.assign(t, e)
}(n || {}), r._socket = null, r._isConnected = !1, r._isCleanUpMode = !1, r._isDestroyed = !1, r._isReconnecting = !0, r._reconnectTimer = null, r._retryAttempts = 0, r._openSocket(), r
}
return function(e, t) {
if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function, not " + typeof t);
e.prototype = Object.create(t && t.prototype, {
constructor: {
value: e,
enumerable: !1,
writable: !0,
configurable: !0
}
}), t && (Object.setPrototypeOf ? Object.setPrototypeOf(e, t) : e.__proto__ = t)
}(t, i), r(t, [{
key: "destroy",
value: function() {
this._isDestroyed = !0, this._cleanUp()
}
}, {
key: "send",
value: function(e) {
if (this._isDestroyed) throw new Error("cannot call send after the socket has been destroyed");
this._socket.send(e)
}
}, {
key: "_validateUrl",
value: function(e) {
if ("string" != typeof e) throw new Error("WebSocket url must be of type string");
if (!(e.startsWith("ws://") || e.startsWith("wss://"))) throw new Error("WebSocket url must begin with either 'ws://' or 'wss://'")
}
}, {
key: "_openSocket",
value: function() {
this._isCleanUpMode = !1, this._socket = "function" == typeof o ? new s(this._url, this._options) : new s(this._url), this._socket.binaryType = "arraybuffer", this._addWebSocketHandlers()
}
}, {
key: "_addWebSocketHandlers",
value: function() {
var e = this;
e._socket.onopen = function(t) {
e._onOpen(t)
}, e._socket.onmessage = function(t) {
e._onMessage(t)
}, e._socket.onclose = function(t) {
e._onClose(t)
}, e._socket.onerror = function(t) {
e._onError(t)
}
}
}, {
key: "_onOpen",
value: function(e) {
this._isConnected || this._isCleanUpMode || this._isDestroyed || (this._isConnected = !0, this._isReconnecting && (this._isReconnecting = !1, this._retryAttempts = 0), this.emit("connect", e))
}
}, {
key: "_onMessage",
value: function(e) {
this._isCleanUpMode || this._isDestroyed || this.emit("data", e.data)
}
}, {
key: "_onClose",
value: function(e) {
this._isCleanUpMode || this._isDestroyed || (this._cleanUp(), this.emit("disconnect"), this._options.autoReconnect ? this._startReconnectTimer() : this._isDestroyed = !0)
}
}, {
key: "_onError",
value: function(e) {
if (!this._isCleanUpMode && !this._isDestroyed) {
this._cleanUp();
var t = new Error('WebSocket connection to "' + this._url + '" failed');
this.emit("warning", t), this._options.autoReconnect ? this._startReconnectTimer() : this._isDestroyed = !0
}
}
}, {
key: "_startReconnectTimer",
value: function() {
var e = this;
if (!this._isDestroyed) {
this._isReconnecting = !0, clearTimeout(this._reconnectTimer);
var t = this._calculateBackoff();
this._reconnectTimer = setTimeout(function() {
e._retryAttempts < e._options.maxReconnectAttempts ? (e._retryAttempts++, e._openSocket()) : e.destroy()
}, t)
}
}
}, {
key: "_calculateBackoff",
value: function() {
var e, t = 2e4;
return this._retryAttempts < 5 && (t = 1e3 * Math.pow(2, this._retryAttempts)), e = t, Math.floor(Math.random() * (e + 1))
}
}, {
key: "_cleanUp",
value: function() {
if (this._isConnected = !1, this._isCleanUpMode = !0, clearTimeout(this._reconnectTimer), this._socket) {
var e = this._socket,
t = function() {
e.onclose = null
};
if (e.readyState === s.CLOSED) t();
else try {
e.onclose = t, e.close()
} catch (e) {
t()
}
e.onopen = null, e.onmessage = null, e.onerror = function() {}
}
this._socket = null
}
}, {
key: "binaryType",
get: function() {
return "arraybuffer"
}
}, {
key: "bufferedAmount",
get: function() {
return this._socket ? this._socket.bufferedAmount : 0
}
}, {
key: "isConnected",
get: function() {
return this._isConnected
}
}, {
key: "isDestroyed",
get: function() {
return this._isDestroyed
}
}, {
key: "readyState",
get: function() {
return this._socket ? this._socket.readyState : s.CLOSED
}
}, {
key: "url",
get: function() {
return this._url
}
}], [{
key: "CLOSED",
get: function() {
return s.CLOSED
}
}, {
key: "CLOSING",
get: function() {
return s.CLOSING
}
}, {
key: "CONNECTING",
get: function() {
return s.CONNECTING
}
}, {
key: "OPEN",
get: function() {
return s.OPEN
}
}, {
key: "IS_WEBSOCKET_SUPPORTED",
get: function() {
return null != s
}
}]), t
}();
t.exports = u
}, {
events: 3,
ws: 2
}],
2: [function(e, t, n) {}, {}],
3: [function(e, t, n) {
var r = Object.create || function(e) {
var t = function() {};
return t.prototype = e, new t
},
i = Object.keys || function(e) {
var t = [];
for (var n in e) Object.prototype.hasOwnProperty.call(e, n) && t.push(n);
return n
},
o = Function.prototype.bind || function(e) {
var t = this;
return function() {
return t.apply(e, arguments)
}
};
function s() {
this._events && Object.prototype.hasOwnProperty.call(this, "_events") || (this._events = r(null), this._eventsCount = 0), this._maxListeners = this._maxListeners || void 0
}
t.exports = s, s.EventEmitter = s, s.prototype._events = void 0, s.prototype._maxListeners = void 0;
var u, c = 10;
try {
var a = {};
Object.defineProperty && Object.defineProperty(a, "x", {
value: 0
}), u = 0 === a.x
} catch (e) {
u = !1
}
function f(e) {
return void 0 === e._maxListeners ? s.defaultMaxListeners : e._maxListeners
}
function l(e, t, n, i) {
var o, s, u;
if ("function" != typeof n) throw new TypeError('"listener" argument must be a function');
if ((s = e._events) ? (s.newListener && (e.emit("newListener", t, n.listener ? n.listener : n), s = e._events), u = s[t]) : (s = e._events = r(null), e._eventsCount = 0), u) {
if ("function" == typeof u ? u = s[t] = i ? [n, u] : [u, n] : i ? u.unshift(n) : u.push(n), !u.warned && (o = f(e)) && o > 0 && u.length > o) {
u.warned = !0;
var c = new Error("Possible EventEmitter memory leak detected. " + u.length + ' "' + String(t) + '" listeners added. Use emitter.setMaxListeners() to increase limit.');
c.name = "MaxListenersExceededWarning", c.emitter = e, c.type = t, c.count = u.length, "object" == typeof console && console.warn && console.warn("%s: %s", c.name, c.message)
}
} else u = s[t] = n, ++e._eventsCount;
return e
}
function h() {
if (!this.fired) switch (this.target.removeListener(this.type, this.wrapFn), this.fired = !0, arguments.length) {
case 0:
return this.listener.call(this.target);
case 1:
return this.listener.call(this.target, arguments[0]);
case 2:
return this.listener.call(this.target, arguments[0], arguments[1]);
case 3:
return this.listener.call(this.target, arguments[0], arguments[1], arguments[2]);
default:
for (var e = new Array(arguments.length), t = 0; t < e.length; ++t) e[t] = arguments[t];
this.listener.apply(this.target, e)
}
}
function p(e, t, n) {
var r = {
fired: !1,
wrapFn: void 0,
target: e,
type: t,
listener: n
},
i = o.call(h, r);
return i.listener = n, r.wrapFn = i, i
}
function y(e, t, n) {
var r = e._events;
if (!r) return [];
var i = r[t];
return i ? "function" == typeof i ? n ? [i.listener || i] : [i] : n ? function(e) {
for (var t = new Array(e.length), n = 0; n < t.length; ++n) t[n] = e[n].listener || e[n];
return t
}(i) : v(i, i.length) : []
}
function _(e) {
var t = this._events;
if (t) {
var n = t[e];
if ("function" == typeof n) return 1;
if (n) return n.length
}
return 0
}
function v(e, t) {
for (var n = new Array(t), r = 0; r < t; ++r) n[r] = e[r];
return n
}
u ? Object.defineProperty(s, "defaultMaxListeners", {
enumerable: !0,
get: function() {
return c
},
set: function(e) {
if ("number" != typeof e || e < 0 || e != e) throw new TypeError('"defaultMaxListeners" must be a positive number');
c = e
}
}) : s.defaultMaxListeners = c, s.prototype.setMaxListeners = function(e) {
if ("number" != typeof e || e < 0 || isNaN(e)) throw new TypeError('"n" argument must be a positive number');
return this._maxListeners = e, this
}, s.prototype.getMaxListeners = function() {
return f(this)
}, s.prototype.emit = function(e) {
var t, n, r, i, o, s, u = "error" === e;
if (s = this._events) u = u && null == s.error;
else if (!u) return !1;
if (u) {
if (arguments.length > 1 && (t = arguments[1]), t instanceof Error) throw t;
var c = new Error('Unhandled "error" event. (' + t + ")");
throw c.context = t, c
}
if (!(n = s[e])) return !1;
var a = "function" == typeof n;
switch (r = arguments.length) {
case 1:
! function(e, t, n) {
if (t) e.call(n);
else
for (var r = e.length, i = v(e, r), o = 0; o < r; ++o) i[o].call(n)
}(n, a, this);
break;
case 2:
! function(e, t, n, r) {
if (t) e.call(n, r);
else
for (var i = e.length, o = v(e, i), s = 0; s < i; ++s) o[s].call(n, r)
}(n, a, this, arguments[1]);
break;
case 3:
! function(e, t, n, r, i) {
if (t) e.call(n, r, i);
else
for (var o = e.length, s = v(e, o), u = 0; u < o; ++u) s[u].call(n, r, i)
}(n, a, this, arguments[1], arguments[2]);
break;
case 4:
! function(e, t, n, r, i, o) {
if (t) e.call(n, r, i, o);
else
for (var s = e.length, u = v(e, s), c = 0; c < s; ++c) u[c].call(n, r, i, o)
}(n, a, this, arguments[1], arguments[2], arguments[3]);
break;
default:
for (i = new Array(r - 1), o = 1; o < r; o++) i[o - 1] = arguments[o];
! function(e, t, n, r) {
if (t) e.apply(n, r);
else
for (var i = e.length, o = v(e, i), s = 0; s < i; ++s) o[s].apply(n, r)
}(n, a, this, i)
}
return !0
}, s.prototype.addListener = function(e, t) {
return l(this, e, t, !1)
}, s.prototype.on = s.prototype.addListener, s.prototype.prependListener = function(e, t) {
return l(this, e, t, !0)
}, s.prototype.once = function(e, t) {
if ("function" != typeof t) throw new TypeError('"listener" argument must be a function');
return this.on(e, p(this, e, t)), this
}, s.prototype.prependOnceListener = function(e, t) {
if ("function" != typeof t) throw new TypeError('"listener" argument must be a function');
return this.prependListener(e, p(this, e, t)), this
}, s.prototype.removeListener = function(e, t) {
var n, i, o, s, u;
if ("function" != typeof t) throw new TypeError('"listener" argument must be a function');
if (!(i = this._events)) return this;
if (!(n = i[e])) return this;
if (n === t || n.listener === t) 0 == --this._eventsCount ? this._events = r(null) : (delete i[e], i.removeListener && this.emit("removeListener", e, n.listener || t));
else if ("function" != typeof n) {
for (o = -1, s = n.length - 1; s >= 0; s--)
if (n[s] === t || n[s].listener === t) {
u = n[s].listener, o = s;
break
} if (o < 0) return this;
0 === o ? n.shift() : function(e, t) {
for (var n = t, r = n + 1, i = e.length; r < i; n += 1, r += 1) e[n] = e[r];
e.pop()
}(n, o), 1 === n.length && (i[e] = n[0]), i.removeListener && this.emit("removeListener", e, u || t)
}
return this
}, s.prototype.removeAllListeners = function(e) {
var t, n, o;
if (!(n = this._events)) return this;
if (!n.removeListener) return 0 === arguments.length ? (this._events = r(null), this._eventsCount = 0) : n[e] && (0 == --this._eventsCount ? this._events = r(null) : delete n[e]), this;
if (0 === arguments.length) {
var s, u = i(n);
for (o = 0; o < u.length; ++o) "removeListener" !== (s = u[o]) && this.removeAllListeners(s);
return this.removeAllListeners("removeListener"), this._events = r(null), this._eventsCount = 0, this
}
if ("function" == typeof(t = n[e])) this.removeListener(e, t);
else if (t)
for (o = t.length - 1; o >= 0; o--) this.removeListener(e, t[o]);
return this
}, s.prototype.listeners = function(e) {
return y(this, e, !0)
}, s.prototype.rawListeners = function(e) {
return y(this, e, !1)
}, s.listenerCount = function(e, t) {
return "function" == typeof e.listenerCount ? e.listenerCount(t) : _.call(e, t)
}, s.prototype.listenerCount = _, s.prototype.eventNames = function() {
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : []
}
}, {}]
}, {}, [1])(1)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment