UNPKG

@4players/odin

Version:

A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects

122 lines (121 loc) 4.39 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.randomPointInUnitSphere = exports.isBlinkBrowser = exports.setupDefaultAudioContext = exports.uint8ArrayToValue = exports.valueToUint8Array = exports.parseJwt = exports.openStream = void 0; const stream_1 = require("./stream"); /** * Opens a new ODIN main/room stream and registers events handlers. * * @param url The URL to the main/room stream server * @param handler The handler for ODIN events * @returns The new steam */ function openStream(url, handler) { return __awaiter(this, void 0, void 0, function* () { const stream = new stream_1.OdinStream(url, handler); try { yield new Promise((resolve, reject) => { stream.addEventListener('open', resolve, { once: true }); stream.addEventListener('error', reject, { once: true }); }); } catch (e) { throw new Error(`Failed to connect to '${url}'`); } return stream; }); } exports.openStream = openStream; /** * Validates and parses a given JWT and returns its claims. * * @param token The JWT to parse * @returns The payload */ function parseJwt(token) { if (!/^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+(\.[A-Za-z0-9-_=]+)?$/.test(token)) { return {}; } const base64Url = token.split('.')[1]; const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); const payload = decodeURIComponent(atob(base64) .split('') .map(function (c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }) .join('')); return JSON.parse(payload); } exports.parseJwt = parseJwt; /** * Encodes a value or an object with the help of JSON.stringify to an Uint8Array. * * @param value Value or object to encode * @returns The Uint8Array encoded value */ function valueToUint8Array(value) { return new TextEncoder().encode(JSON.stringify(value)); } exports.valueToUint8Array = valueToUint8Array; /** * Decodes a Uint8Array. * * @param bytes Byte array to decode * @returns The decoded value or undefined on error */ function uint8ArrayToValue(bytes) { if (bytes.length === 0) return undefined; try { return JSON.parse(new TextDecoder().decode(bytes)); } catch (e) { return undefined; } } exports.uint8ArrayToValue = uint8ArrayToValue; /** * Creates new audio contexts for both audio input and output. * If the sample rate of the input device is 48 kHz, we can use the same audio context for both. * * @param input Current input audio context * @returns The new set of audio contexts */ function setupDefaultAudioContext(input) { const output = input.sampleRate === 48000 ? input : new AudioContext({ sampleRate: 48000 }); return { input, output }; } exports.setupDefaultAudioContext = setupDefaultAudioContext; /** * Tests the user agent against a valid pattern for Blink-based browsers. * * @returns True if we're using a Blink browser */ function isBlinkBrowser() { const pattern = /(apple)?webkit\/537\.36/i; return pattern.test(window.navigator.userAgent); } exports.isBlinkBrowser = isBlinkBrowser; /** * Generate a random point inside a unit sphere. * * @return The new position */ function randomPointInUnitSphere() { const theta = Math.random() * 2.0 * Math.PI; const phi = Math.random() * Math.PI; const r = Math.cbrt(Math.random()); const x = r * Math.sin(phi) * Math.cos(theta); const y = r * Math.sin(phi) * Math.sin(theta); const z = r * Math.cos(phi); return [x, y, z]; } exports.randomPointInUnitSphere = randomPointInUnitSphere;