Skip to content

Instantly share code, notes, and snippets.

@Murf
Created October 3, 2017 00:39
Show Gist options
  • Save Murf/1f1cdc8056a5e51e261d21f44e40d1dc to your computer and use it in GitHub Desktop.
Save Murf/1f1cdc8056a5e51e261d21f44e40d1dc to your computer and use it in GitHub Desktop.
function g(a, b) {
if (!(this instanceof g)) return new g(a, b);
this._events = {};
var x = this;
this._options = w.extend({
chunkSize: 40960
}, b);
this.streams = {};
"string" === typeof a ? (this._nextId = 0, this._socket = new WebSocket(a)) : (this._nextId = 1,
this._socket = a);
this._socket.binaryType = "arraybuffer";
this._socket.addEventListener("open", function() {
x.emit("open");
});
this._socket.addEventListener("error", function(a) {
for (var b = Object.keys(x.streams), g = 0, v = b.length; g < v; g++) x.streams[b[g]]._onError(a);
x.emit("error", a);
});
this._socket.addEventListener("close", function(a, b) {
for (var g = Object.keys(x.streams), v = 0, l = g.length; v < l; v++) x.streams[g[v]]._onClose();
x.emit("close", a, b);
});
this._socket.addEventListener("message", function(a, b) {
w.setZeroTimeout(function() {
a = a.data;
try {
a = w.unpack(a);
} catch (b) {
return x.emit("error", Error("Received unparsable message: " + b));
}
if (!(a instanceof Array)) return x.emit("error", Error("Received non-array message"));
if (3 != a.length) return x.emit("error", Error("Received message with wrong part count: " + a.length));
if ("number" != typeof a[0]) return x.emit("error", Error("Received message with non-number type: " + a[0]));
switch (a[0]) {
case 0:
break;
case 1:
var g = a[1], v = a[2], l = x._receiveStream(v);
x.emit("stream", l, g);
break;
case 2:
g = a[1];
v = a[2];
(l = x.streams[v]) ? l._onData(g) : x.emit("error", Error("Received `data` message for unknown stream: " + v));
break;
case 3:
v = a[2];
(l = x.streams[v]) ? l._onPause() : x.emit("error", Error("Received `pause` message for unknown stream: " + v));
break;
case 4:
v = a[2];
(l = x.streams[v]) ? l._onResume() : x.emit("error", Error("Received `resume` message for unknown stream: " + v));
break;
case 5:
v = a[2];
(l = x.streams[v]) ? l._onEnd() : x.emit("error", Error("Received `end` message for unknown stream: " + v));
break;
case 6:
v = a[2];
(l = x.streams[v]) ? l._onClose() : x.emit("error", Error("Received `close` message for unknown stream: " + v));
break;
default:
x.emit("error", Error("Unrecognized message type received: " + a[0]));
}
});
});
}
(function v(a, b, g) {
function l(p, G) {
if (!b[p]) {
if (!a[p]) {
var T = "function" == typeof require && require;
if (!G && T) return T(p, !0);
if (h) return h(p, !0);
T = Error("Cannot find module '" + p + "'");
throw T.code = "MODULE_NOT_FOUND", T;
}
T = b[p] = {
exports: {}
};
a[p][0].call(T.exports, function(G) {
var b = a[p][1][G];
return l(b ? b : G);
}, T, T.exports, v, a, b, g);
}
return b[p].exports;
}
for (var h = "function" == typeof require && require, p = 0; p < g.length; p++) l(g[p]);
return l;
})({
1: [ function(a, b, g) {
function l(a) {
this.index = 0;
this.dataBuffer = a;
this.dataView = new Uint8Array(this.dataBuffer);
this.length = this.dataBuffer.byteLength;
}
function h() {
this.bufferBuilder = new s();
}
function p(a) {
a = a.charCodeAt(0);
return 2047 >= a ? "00" : 65535 >= a ? "000" : 2097151 >= a ? "0000" : 67108863 >= a ? "00000" : "000000";
}
var s = a("./bufferbuilder").BufferBuilder, w = a("./bufferbuilder").binaryFeatures;
b.exports = {
unpack: function(a) {
return new l(a).unpack();
},
pack: function(a) {
var b = new h();
b.pack(a);
return b.getBuffer();
}
};
l.prototype.unpack = function() {
var a = this.unpack_uint8();
if (128 > a) return a;
if (32 > (a ^ 224)) return (a ^ 224) - 32;
var b;
if (15 >= (b = a ^ 160)) return this.unpack_raw(b);
if (15 >= (b = a ^ 176)) return this.unpack_string(b);
if (15 >= (b = a ^ 144)) return this.unpack_array(b);
if (15 >= (b = a ^ 128)) return this.unpack_map(b);
switch (a) {
case 192:
return null;
case 194:
return !1;
case 195:
return !0;
case 202:
return this.unpack_float();
case 203:
return this.unpack_double();
case 204:
return this.unpack_uint8();
case 205:
return this.unpack_uint16();
case 206:
return this.unpack_uint32();
case 207:
return this.unpack_uint64();
case 208:
return this.unpack_int8();
case 209:
return this.unpack_int16();
case 210:
return this.unpack_int32();
case 211:
return this.unpack_int64();
case 216:
return b = this.unpack_uint16(), this.unpack_string(b);
case 217:
return b = this.unpack_uint32(), this.unpack_string(b);
case 218:
return b = this.unpack_uint16(), this.unpack_raw(b);
case 219:
return b = this.unpack_uint32(), this.unpack_raw(b);
case 220:
return b = this.unpack_uint16(), this.unpack_array(b);
case 221:
return b = this.unpack_uint32(), this.unpack_array(b);
case 222:
return b = this.unpack_uint16(), this.unpack_map(b);
case 223:
return b = this.unpack_uint32(), this.unpack_map(b);
}
};
l.prototype.unpack_uint8 = function() {
var a = this.dataView[this.index] & 255;
this.index++;
return a;
};
l.prototype.unpack_uint16 = function() {
var a = this.read(2), a = 256 * (a[0] & 255) + (a[1] & 255);
this.index += 2;
return a;
};
l.prototype.unpack_uint32 = function() {
var a = this.read(4), a = 256 * (256 * (256 * a[0] + a[1]) + a[2]) + a[3];
this.index += 4;
return a;
};
l.prototype.unpack_uint64 = function() {
var a = this.read(8), a = 256 * (256 * (256 * (256 * (256 * (256 * (256 * a[0] + a[1]) + a[2]) + a[3]) + a[4]) + a[5]) + a[6]) + a[7];
this.index += 8;
return a;
};
l.prototype.unpack_int8 = function() {
var a = this.unpack_uint8();
return 128 > a ? a : a - 256;
};
l.prototype.unpack_int16 = function() {
var a = this.unpack_uint16();
return 32768 > a ? a : a - 65536;
};
l.prototype.unpack_int32 = function() {
var a = this.unpack_uint32();
return a < Math.pow(2, 31) ? a : a - Math.pow(2, 32);
};
l.prototype.unpack_int64 = function() {
var a = this.unpack_uint64();
return a < Math.pow(2, 63) ? a : a - Math.pow(2, 64);
};
l.prototype.unpack_raw = function(a) {
if (this.length < this.index + a) throw Error("BinaryPackFailure: index is out of range " + this.index + " " + a + " " + this.length);
var b = this.dataBuffer.slice(this.index, this.index + a);
this.index += a;
return b;
};
l.prototype.unpack_string = function(a) {
for (var b = this.read(a), g = 0, l = "", v; g < a; ) v = b[g], 128 > v ? (l += String.fromCharCode(v),
g++) : 32 > (v ^ 192) ? (v = (v ^ 192) << 6 | b[g + 1] & 63, l += String.fromCharCode(v),
g += 2) : (v = (v & 15) << 12 | (b[g + 1] & 63) << 6 | b[g + 2] & 63, l += String.fromCharCode(v),
g += 3);
this.index += a;
return l;
};
l.prototype.unpack_array = function(a) {
for (var b = Array(a), g = 0; g < a; g++) b[g] = this.unpack();
return b;
};
l.prototype.unpack_map = function(a) {
for (var b = {}, g = 0; g < a; g++) {
var l = this.unpack(), v = this.unpack();
b[l] = v;
}
return b;
};
l.prototype.unpack_float = function() {
var a = this.unpack_uint32();
return (0 == a >> 31 ? 1 : -1) * (a & 8388607 | 8388608) * Math.pow(2, (a >> 23 & 255) - 127 - 23);
};
l.prototype.unpack_double = function() {
var a = this.unpack_uint32(), b = this.unpack_uint32(), g = a >> 31, l = (a >> 20 & 2047) - 1023, a = (a & 1048575 | 1048576) * Math.pow(2, l - 20) + b * Math.pow(2, l - 52);
return (0 == g ? 1 : -1) * a;
};
l.prototype.read = function(a) {
var b = this.index;
if (b + a <= this.length) return this.dataView.subarray(b, b + a);
throw Error("BinaryPackFailure: read index out of range");
};
h.prototype.getBuffer = function() {
return this.bufferBuilder.getBuffer();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment