UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

401 lines (400 loc) 11.1 kB
import Xt from "../models/backend-errors.js"; import ue from "../models/braze-event.js"; import { convertMsToSeconds as P, convertSecondsToMs as Zt, } from "../util/date-utils.js"; import c from "../common/event-logger.js"; import { isArray as z, isEqual as ii } from "../util/code-utils.js"; import { logger as E, EventTypes as m } from "../../shared-lib/index.js"; import { STORAGE_KEYS as s } from "./storage-manager.js"; import h from "../util/request-header-utils.js"; import { LAST_REQUEST_TO_ENDPOINT_MS_AGO_DEFAULT as he, MAX_RETRY_COUNT_PER_REQUEST as ce, } from "../common/constants.js"; import { getAlias as ve } from "./utils.js"; import { readResponseHeaders as Re } from "../util/net.js"; export default class Mt { constructor(t, e, i, s, r, n, o, a, h, u, l, c) { (this.Dn = t), (this.j = e), (this.La = i), (this.Cs = s), (this.C = r), (this.h = n), (this.Pn = o), (this.Ua = a), (this.Ra = h), (this.Pa = u), (this.appVersion = l), (this.Ru = c), (this.qu = (t) => (null == t ? "" : `${t} `)), (this.Dn = t), (this.j = e), (this.La = i), (this.Cs = s), (this.C = r), (this.h = n), (this.Pn = o), (this.Ua = a), (this.Ra = h), (this.Pa = u), (this.appVersion = l), (this.Ru = c), (this.Du = ["npm"]), (this.Tu = {}); } $(t, e = !1, i = !1) { const r = this.Dn.fe(!i), n = r.pn(), o = this.j.lt(s.ct.ac); ii(o, n) || (t.device = n), (t.api_key = this.Pn), (t.time = P(new Date().valueOf(), !0)); const a = this.j.lt(s.ct.ku) || [], h = this.j.lt(s.ct.wu) || ""; this.Du.length > 0 && (!ii(a, this.Du) || h !== this.C.bt()) && (t.sdk_metadata = this.Du), (t.sdk_version = this.Ra), this.Pa && (t.sdk_flavor = this.Pa), (t.app_version = this.appVersion), (t.app_version_code = this.Ru), (t.device_id = r.id); const u = this.Cs.getUserId(); if ((e && null !== u && (t.user_id = u), !u && !this.La.wh())) { const e = ve(this.j); e && (t.alias = e); } return t; } Y(t, e, i) { const s = e.auth_error, r = e.error; if (!s && !r) return !0; if (s) { let e; this.La.Hh(); const r = { errorCode: s.error_code }; for (const t of i) z(t) && "X-Braze-Auth-Signature" === t[0] && (r.signature = t[1]); t.respond_with && t.respond_with.user_id ? (r.userId = t.respond_with.user_id) : t.user_id && (r.userId = t.user_id); const n = s.reason; return ( n ? ((r.reason = n), (e = `due to ${n}`)) : (e = `with error code ${s.error_code}.`), this.La.wh() || (e += ' Please use the "enableSdkAuthentication" initialization option to enable authentication.'), E.error(`SDK Authentication failed ${e}`), this.Bu(t.events || [], t.attributes || []), this.La.Bh(r), !1 ); } if (r) { let i, s = r; switch (s) { case Xt.zu: return ( (i = "Received successful response with empty body."), c.rt(m.ju, { e: i }), E.info(i), !1 ); case Xt.Mu: return ( (i = "Received successful response with invalid JSON"), c.rt(m.ju, { e: i + ": " + e.response }), E.info(i), !1 ); case Xt.$u: s = `The API key "${t.api_key}" is invalid for the baseUrl ${this.Ua}`; break; case Xt.Xu: s = "Sorry, we are not currently accepting your requests. If you think this is in error, please contact us."; break; case Xt.Lu: s = "No device identifier. Please contact support@braze.com"; } E.error("Backend error: " + s); } return !1; } Fu(t, e, i) { return !!((t && 0 !== t.length) || (e && 0 !== e.length) || i); } Ku(t, e, i, s = !1) { const r = [], n = (t) => t || "", o = n(this.Cs.getUserId()); let a = this.cn(t); const u = [], l = []; let c, d = null; if (e.length > 0) { const t = []; for (const i of e) { if (((c = i.pn()), this.La.wh())) { if (o && !c.user_id) { d || (d = {}), d.events || (d.events = []), d.events.push(c); continue; } if (n(c.user_id) !== o) { l.push(c); continue; } } t.push(c); } t.length > 0 && (a.events = t); } if (i.length > 0) { const t = []; for (const e of i) e && (this.La.wh() && n(e.user_id) !== o ? u.push(e) : t.push(e)); t.length > 0 && (a.attributes = t); } if ((this.Bu(l, u), (a = this.$(a, !1, s)), d)) { d = this.$(d, !1, s); const t = { requestData: d, headers: this.A(d, h.H.Yi) }; r.push(t); } if (a && !this.Fu(a.events, a.attributes, t)) return d ? r : null; const f = { requestData: a, headers: this.A(a, h.H.Yi) }; return r.push(f), r; } Bu(t, e) { if (t) { const e = []; for (const i of t) { const t = ue.fromJson(i); (t.time = Zt(t.time)), e.push(t); } this.j.Go(e); } if (e) for (const t of e) this.j.Pu(t); } _(t, e) { let i = "HTTP error "; null != t && (i += t + " "), (i += e), E.error(i); } xu(t) { return c.rt(m.Hu, { n: t }); } cn(t, e) { const i = {}; t && (i.triggers = !0); const s = null != e ? e : this.Cs.getUserId(); if ((s && (i.user_id = s), !i.user_id && !this.La.wh())) { const t = ve(this.j); t && (i.alias = t); } return (i.config = { config_time: this.h.Ft() }), { respond_with: i }; } Ou(t) { const e = new Date().valueOf(); let i = he.toString(); const s = h.Gu(this.j, t); if (-1 !== s) { i = (e - s).toString(); } return i; } A(t, e, i = !1) { const s = [["X-Braze-Api-Key", this.Pn]], r = this.Ou(e); s.push(["X-Braze-Last-Req-Ms-Ago", r]); const n = h._u(this.j, e).toString(); s.push(["X-Braze-Req-Attempt", n]); let o = !1; if ( (null != t.respond_with && t.respond_with.triggers && (s.push(["X-Braze-TriggersRequest", "true"]), (o = !0)), e === h.H.Ci) ) { s.push(["X-Braze-ContentCardsRequest", "true"]); let t = h._u(this.j, h.H.Ci); (t && !i) || ((t = 1), h.Wu(this.j, h.H.Ci, t)); const e = Math.max(0, t - 1); s.push(["BRAZE-SYNC-RETRY-COUNT", e.toString()]), (o = !0); } if ( (e === h.H.ke && (s.push(["X-Braze-FeatureFlagsRequest", "true"]), (o = !0)), o && s.push(["X-Braze-DataRequest", "true"]), this.La.wh()) ) { const t = this.La.jh(); null != t && s.push(["X-Braze-Auth-Signature", t]); } return s; } Yu(t, e, i, s) { if (this.Tu[s]) return; const r = window.setTimeout(() => { E.info(`Retrying rate limited ${this.qu(s)}SDK request.`), this.J(e, i, s); }, t); this.Tu[s] = r; } fo() { for (const t in this.Tu) { const e = this.Tu[t]; window.clearTimeout(e); } this.Tu = {}; } J(t, e, i, r) { if (!this.Ju(i)) return ( E.info(`${this.qu(i)}SDK request being rate limited.`), void ("function" == typeof r && r()) ); const n = this.Qu(); if (!n.Vu) return ( this.Yu(n.Zu, t, e, i), void E.info( `${this.qu( i, )}SDK request being rate limited. Request will be retried in ${Math.trunc( n.Zu / 1e3, )} seconds.`, ) ); this.j.ft(s.ct.tl, new Date().valueOf()); const o = t.device; o && o.os_version instanceof Promise ? o.os_version.then((i) => { (t.device.os_version = i), e(n.el); }) : e(n.el); } il(t) { const e = t ? Re(t) : null; if (!e || !e["retry-after"]) return null; const i = e["retry-after"]; if (isNaN(i) && !isNaN(Date.parse(i))) return { type: "date", value: Date.parse(i) }; if (!isNaN(parseFloat(i.toString()))) return { type: "timestamp", value: 1e3 * parseFloat(i.toString()) }; { const t = "Received unexpected value for retry-after header in /sync response"; c.rt(m.ju, { e: t + ": " + i }); } return null; } st(t, e, i, s, r, n) { if (h._u(this.j, i) >= ce) return; let o; n = n || 0; const a = this.il(t); r(); const u = (t) => { const r = window.setTimeout(() => { e(); }, t); s(r), h.sl(this.j, i); }; if (a && !isNaN(a.value)) { switch (a.type) { case "date": (o = a.value - new Date().getTime() + n), o < 0 && e(); break; case "timestamp": o = a.value + n; } u(o); } else n ? u(n) : h.fi(this.j, i); } rl(t) { var e; null === (e = this.j) || void 0 === e || e.ft(s.ct.ol, t); } al(t, e) { let i = this.hl(); null == i && (i = {}), (i[t] = e), this.j.ft(s.ct.ul, i); } cl() { var t; return null === (t = this.j) || void 0 === t ? void 0 : t.lt(s.ct.ol); } hl() { var t; return null === (t = this.j) || void 0 === t ? void 0 : t.lt(s.ct.ul); } dl(t, e, i, s, r = "") { let n; if (r) { const t = this.hl(); n = null == t || isNaN(t[r]) ? e : t[r]; } else (n = this.cl()), (null == n || isNaN(n)) && (n = e); const o = (t - s) / 1e3; return (n = Math.min(n + o / i, e)), n; } fl(t, e) { return Math.max(0, (1 - t) * e * 1e3); } ml(t, e = "") { var i, r, n, o, a; const u = { Vu: !0, el: -1, Zu: 0 }; if ((null == t && (t = !0), !t && !e)) return u; let l, c, d = null; if (t) d = null === (i = this.j) || void 0 === i ? void 0 : i.lt(s.ct.tl); else { const t = h.vl(this.j); if (null == t || null == t[e]) return u; d = t[e]; } if (null == d || isNaN(d)) return u; if ( (t ? ((l = (null === (r = this.h) || void 0 === r ? void 0 : r.Rl()) || -1), (c = (null === (n = this.h) || void 0 === n ? void 0 : n.gl()) || -1)) : ((l = (null === (o = this.h) || void 0 === o ? void 0 : o.bl(e)) || -1), (c = (null === (a = this.h) || void 0 === a ? void 0 : a.ql(e)) || -1)), -1 === l || -1 === c) ) return u; const f = new Date().valueOf(); let m = this.dl(f, l, c, d, e); return m < 1 ? ((u.Vu = !1), (u.Zu = this.fl(m, c)), u) : ((m = Math.trunc(m) - 1), (u.el = m), t ? this.rl(m) : this.al(e, m), u); } Qu() { return this.ml(!0); } Ju(t) { const e = this.ml(!1, t); return !(e && !e.Vu); } Z() { this.La.Z(); } V() { return this.Ua; } addSdkMetadata(t) { for (const e of t) -1 === this.Du.indexOf(e) && this.Du.push(e); } }