UNPKG

@mothepro/fancy-p2p

Version:

A quick and efficient way to form p2p groups in the browser

83 lines 3.51 kB
import { MessageType } from './builders.js'; import HashableSet from './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... */ export function parseClientIds(offset, data) { const ids = new HashableSet; for (let i = offset; i < data.byteLength; i += 2 /* SHORT */) ids.add(data.getUint16(i, true)); return ids; } /** Helper to get name that server assigns. */ export 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}`); } /** Helper to get status of a Client joining. */ export 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}`); } /** Helper to get status of a Client leaving. */ export 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}`); } /** Helper to get status of a Client leaving. */ export 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}`); } /** Helper to get status of a Group finalization. */ export 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}`); } export function parseSdp(data) { if (data.byteLength > 2 /* SHORT */ + 1 /* CHAR */ && data.getUint8(1 /* CHAR */) in MessageType) return { from: data.getUint16(0, true), sdp: { type: MessageType[data.getUint8(2 /* SHORT */)], sdp: decoder.decode(data.buffer.slice(2 /* SHORT */ + 1 /* CHAR */)), } }; throw Error(`Expected a SDP message, but got ${data}`); } export 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}`); } //# sourceMappingURL=parsers.js.map