UNPKG

@tidal-music/player

Version:
86 lines (85 loc) 2.67 kB
function y() { let s = [], o, a, p, l; const r = /* @__PURE__ */ new Map(); function d() { return ("10000000-1000-4000-8000" + -1e11).replace(/[018]/g, (n) => { const e = crypto.getRandomValues(new Uint8Array(1))[0]; return (n ^ (e ?? 0) & 15 >> n / 4).toString(16); }); } async function f(n, e) { if (r.has(e)) return r.get(e); r.clear(); const t = await (await fetch(n + "/sessions", { headers: new Headers({ Authorization: "Bearer " + e }), // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - It is a thing. importance: "low" })).json(); return r.set(e, t), t; } setInterval(async () => { if (s.length > 0 && l) { const n = { batchId: d(), events: [...s] }; try { const e = new Headers(); if (o && e.append("authorization", "Bearer " + o), a && e.append("x-tidal-token", a), e.append("content-type", "application/json; boundary=player-sdk"), (await fetch(l, { body: JSON.stringify(n), headers: e, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - It is a thing. importance: "low", method: "POST" })).ok) s.forEach((t) => { "streamingSessionId" in t.payload && postMessage({ command: "cleanUp", eventName: t.name, streamingSessionId: t.payload.streamingSessionId }); }), s = []; else throw new Error("Response not ok"); } catch (e) { console.warn(e); } } }, 15e3), onmessage = async (n) => { const e = JSON.parse(n.data); l = e.eventUrl; const i = e.type === "play_log_open" ? void 0 : await f(e.apiUrl, e.accessToken); e.accessToken && (o = e.accessToken), !o && e.type !== "play_log_open" && console.trace( "An accessToken is missing. Make sure to send at least one commit-payload with it defined (not needed for play_log_open events)." ), e.clientId && (a = e.clientId), e.clientPlatform && (p = e.clientPlatform), e.events.forEach((c) => { const u = { client: { platform: p, token: a, version: e.appVersion }, ..."extras" in c && { extras: c.extras }, group: e.type, name: c.name, payload: c.payload, ts: e.ts, user: { accessToken: o ?? "", clientId: i?.client.id ?? 0, id: i?.userId ?? 0 }, uuid: d(), version: 2 }; s.push(u); }); }; } export { y as beacon };