@mothepro/fancy-p2p
Version:
A quick and efficient way to form p2p groups in the browser
97 lines • 4.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseFallback = exports.parseSdp = exports.parseGroupFinalize = exports.parseGroupChange = exports.parseClientLeave = exports.parseClientJoin = exports.parseYourName = exports.parseClientIds = void 0;
const builders_js_1 = require("./builders.js");
const HashableSet_js_1 = __importDefault(require("./HashableSet.js"));
/* Parse ArrayBuffers to sent from server to us. */
const decoder = new TextDecoder;
/**
* Helper to gets a list of `ClientID`s from a buffer at an offset.
*
* Unfortunately, Buffer -> UInt16Array is not WAI.
* Also, do not rely on the underlying ArrayBuffer `data.buffer`, socket may modify it...
*/
function parseClientIds(offset, data) {
const ids = new HashableSet_js_1.default;
for (let i = offset; i < data.byteLength; i += 2 /* SHORT */)
ids.add(data.getUint16(i, true));
return ids;
}
exports.parseClientIds = parseClientIds;
/** Helper to get name that server assigns. */
function parseYourName(data) {
if (data.getUint8(0) == 5 /* YOUR_NAME */ && data.byteLength > 1 /* CHAR */)
return decoder.decode(data.buffer.slice(1 /* CHAR */));
throw Error(`Expected a your name message, but got ${data}`);
}
exports.parseYourName = parseYourName;
/** Helper to get status of a Client joining. */
function parseClientJoin(data) {
if (data.getUint8(0) == 1 /* CLIENT_JOIN */
&& data.byteLength > 1 /* CHAR */ + 2 /* SHORT */)
return {
id: data.getUint16(1 /* CHAR */, true),
name: decoder.decode(data.buffer.slice(1 /* CHAR */ + 2 /* SHORT */)),
};
throw Error(`Expected a client join message, but got ${data}`);
}
exports.parseClientJoin = parseClientJoin;
/** Helper to get status of a Client leaving. */
function parseClientLeave(data) {
if (data.getUint8(0) == 0 /* CLIENT_LEAVE */
&& data.byteLength == 1 /* CHAR */ + 2 /* SHORT */)
return data.getUint16(1 /* CHAR */, true);
throw Error(`Expected a client leave message, but got ${data}`);
}
exports.parseClientLeave = parseClientLeave;
/** Helper to get status of a Client leaving. */
function parseGroupChange(data) {
if ((data.getUint8(0) == 3 /* GROUP_REJECT */ || data.getUint8(0) == 2 /* GROUP_REQUEST */)
&& data.byteLength >= 1 /* CHAR */ + 2 /* SHORT */
&& data.byteLength % 2 /* SHORT */ == 1 /* CHAR */)
return {
approve: data.getUint8(0) == 2 /* GROUP_REQUEST */,
actor: data.getUint16(1 /* CHAR */, true),
members: parseClientIds(1 /* CHAR */, data),
};
throw Error(`Expected a group join or leave message, but got ${data}`);
}
exports.parseGroupChange = parseGroupChange;
/** Helper to get status of a Group finalization. */
function parseGroupFinalize(data) {
if (data.getUint8(0) == 4 /* GROUP_FINAL */
&& data.byteLength >= 1 /* CHAR */ + 4 /* INT */)
return {
code: data.getInt32(1 /* CHAR */, true),
cmp: data.getUint16(1 /* CHAR */ + 4 /* INT */, true),
members: parseClientIds(1 /* CHAR */ + 4 /* INT */ + 2 /* SHORT */, data),
};
throw Error(`Expected a group finalize message, but got ${data}`);
}
exports.parseGroupFinalize = parseGroupFinalize;
function parseSdp(data) {
if (data.byteLength > 2 /* SHORT */ + 1 /* CHAR */
&& data.getUint8(1 /* CHAR */) in builders_js_1.MessageType)
return {
from: data.getUint16(0, true),
sdp: {
type: builders_js_1.MessageType[data.getUint8(2 /* SHORT */)],
sdp: decoder.decode(data.buffer.slice(2 /* SHORT */ + 1 /* CHAR */)),
}
};
throw Error(`Expected a SDP message, but got ${data}`);
}
exports.parseSdp = parseSdp;
function parseFallback(data) {
if (data.byteLength > 2 /* SHORT */)
return {
from: data.getUint16(0, true),
data: data.buffer.slice(2 /* SHORT */),
};
throw Error(`Expected a fallback message, but got ${data}`);
}
exports.parseFallback = parseFallback;
//# sourceMappingURL=parsers.js.map