UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

397 lines (396 loc) 11.1 kB
import Xt from "../models/backend-errors.js"; import ue from "../models/braze-event.js"; import { convertMsToSeconds as X, convertSecondsToMs as Yt, } from "../util/date-utils.js"; import c from "../common/event-logger.js"; import { isArray as D, isEqual as ii } from "../util/code-utils.js"; import { logger as N, EventTypes as d } 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 Zt, MAX_RETRY_COUNT_PER_REQUEST as he, } from "../common/constants.js"; import { getAlias as ce } from "./utils.js"; import { readResponseHeaders as ve } from "../util/net.js"; export default class Mt { constructor(t, e, i, s, r, n, o, a, h, l, u, c) { (this.un = t), (this.C = e), (this.Na = i), (this.Cs = s), (this.T = r), (this.B = n), (this.on = o), (this.Oa = a), (this.Ia = h), (this.Sa = l), (this.appVersion = u), (this.tl = c), (this.el = (t) => (null == t ? "" : `${t} `)), (this.un = t), (this.C = e), (this.Na = i), (this.Cs = s), (this.T = r), (this.B = n), (this.on = o), (this.Oa = a), (this.Ia = h), (this.Sa = l), (this.appVersion = u), (this.tl = c), (this.il = ["npm"]), (this.sl = []); } H(t, e = !1, i = !1) { const r = this.un.ve(!i), n = r.Tr(), o = this.C.ft(s.gt.Za); ii(o, n) || (t.device = n), (t.api_key = this.on), (t.time = X(new Date().valueOf(), !0)); const a = this.C.ft(s.gt.rl) || [], h = this.C.ft(s.gt.ol) || ""; this.il.length > 0 && (!ii(a, this.il) || h !== this.T.St()) && (t.sdk_metadata = this.il), (t.sdk_version = this.Ia), this.Sa && (t.sdk_flavor = this.Sa), (t.app_version = this.appVersion), (t.app_version_code = this.tl), (t.device_id = r.id); const l = this.Cs.getUserId(); if ((e && null !== l && (t.user_id = l), !l && !this.Na.wh())) { const e = ce(this.C); e && (t.alias = e); } return t; } st(t, e, i) { const s = e.auth_error, r = e.error; if (!s && !r) return !0; if (s) { let e; this.Na.Hh(); const r = { errorCode: s.error_code }; for (const t of i) D(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.Na.wh() || (e += ' Please use the "enableSdkAuthentication" initialization option to enable authentication.'), N.error(`SDK Authentication failed ${e}`), this.al(t.events || [], t.attributes || []), this.Na.Bh(r), !1 ); } if (r) { let i, s = r; switch (s) { case Xt.hl: return ( (i = "Received successful response with empty body."), c.ut(d.ul, { e: i }), N.info(i), !1 ); case Xt.cl: return ( (i = "Received successful response with invalid JSON"), c.ut(d.ul, { e: i + ": " + e.response }), N.info(i), !1 ); case Xt.dl: s = `The API key "${t.api_key}" is invalid for the baseUrl ${this.Oa}`; break; case Xt.fl: s = "Sorry, we are not currently accepting your requests. If you think this is in error, please contact us."; break; case Xt.ml: s = "No device identifier. Please contact support@braze.com"; } N.error("Backend error: " + s); } return !1; } vl(t, e, i) { return !!((t && 0 !== t.length) || (e && 0 !== e.length) || i); } Rl(t, e, i, s = !1) { const r = [], n = (t) => t || "", o = n(this.Cs.getUserId()); let a = this.Ar(t); const l = [], u = []; let c, d = null; if (e.length > 0) { const t = []; for (const i of e) { if (((c = i.Tr()), this.Na.wh())) { if (o && !c.user_id) { d || (d = {}), d.events || (d.events = []), d.events.push(c); continue; } if (n(c.user_id) !== o) { u.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.Na.wh() && n(e.user_id) !== o ? l.push(e) : t.push(e)); t.length > 0 && (a.attributes = t); } if ((this.al(u, l), (a = this.H(a, !1, s)), d)) { d = this.H(d, !1, s); const t = { requestData: d, headers: this.J(d, h.O.gl) }; r.push(t); } if (a && !this.vl(a.events, a.attributes, t)) return d ? r : null; const f = { requestData: a, headers: this.J(a, h.O.gl) }; return r.push(f), r; } al(t, e) { if (t) { const e = []; for (const i of t) { const t = ue.fromJson(i); (t.time = Yt(t.time)), e.push(t); } this.C.Wo(e); } if (e) for (const t of e) this.C.bl(t); } et(t, e) { let i = "HTTP error "; null != t && (i += t + " "), (i += e), N.error(i); } ql(t) { return c.ut(d.Al, { n: t }); } Ar(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.Na.wh())) { const t = ce(this.C); t && (i.alias = t); } return (i.config = { config_time: this.B.Et() }), { respond_with: i }; } Dl(t) { const e = new Date().valueOf(); let i = Zt.toString(); const s = h.Tl(this.C, t); if (-1 !== s) { i = (e - s).toString(); } return i; } J(t, e, i = !1) { const s = [["X-Braze-Api-Key", this.on]], r = this.Dl(e); s.push(["X-Braze-Last-Req-Ms-Ago", r]); const n = h.kl(this.C, 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.O.vi) ) { s.push(["X-Braze-ContentCardsRequest", "true"]); let t = h.kl(this.C, h.O.vi); (t && !i) || ((t = 1), h.yl(this.C, h.O.vi, t)); const e = Math.max(0, t - 1); s.push(["BRAZE-SYNC-RETRY-COUNT", e.toString()]), (o = !0); } if ( (e === h.O.Le && (s.push(["X-Braze-FeatureFlagsRequest", "true"]), (o = !0)), o && s.push(["X-Braze-DataRequest", "true"]), this.Na.wh()) ) { const t = this.Na.jh(); null != t && s.push(["X-Braze-Auth-Signature", t]); } return s; } wl(t, e, i, s) { const r = window.setTimeout(() => { N.info(`Retrying rate limited ${this.el(s)}SDK request.`), this.V(e, i, s); }, t); this.sl.push(r); } fo() { for (const t of this.sl) window.clearTimeout(t); this.sl = []; } V(t, e, i, r) { if (!this.Nl(i)) return ( N.info(`${this.el(i)}SDK request being rate limited.`), void ("function" == typeof r && r()) ); const n = this.Cl(); if (!n.Sl) return ( N.info( `${this.el( i, )}SDK request being rate limited. Request will be retried in ${Math.trunc( n.Bl / 1e3, )} seconds.`, ), void this.wl(n.Bl, t, e, i) ); this.C.Bt(s.gt.zl, 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.jl); }) : e(n.jl); } Ml(t) { const e = t ? ve(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.ut(d.ul, { e: t + ": " + i }); } return null; } rt(t, e, i, s, r, n) { if (h.kl(this.C, i) >= he) return; let o; n = n || 0; const a = this.Ml(t); r(); const l = (t) => { const r = window.setTimeout(() => { e(); }, t); s(r), h.$l(this.C, 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; } l(o); } else n ? l(n) : h.bi(this.C, i); } Xl(t) { var e; null === (e = this.C) || void 0 === e || e.Bt(s.gt.Ll, t); } El(t, e) { let i = this.Fl(); null == i && (i = {}), (i[t] = e), this.C.Bt(s.gt.Kl, i); } Il() { var t; return null === (t = this.C) || void 0 === t ? void 0 : t.ft(s.gt.Ll); } Fl() { var t; return null === (t = this.C) || void 0 === t ? void 0 : t.ft(s.gt.Kl); } Pl(t, e, i, s, r = "") { let n; if (r) { const t = this.Fl(); n = null == t || isNaN(t[r]) ? e : t[r]; } else (n = this.Il()), (null == n || isNaN(n)) && (n = e); const o = (t - s) / 1e3; return (n = Math.min(n + o / i, e)), n; } Ul(t, e) { return Math.max(0, (1 - t) * e * 1e3); } xl(t, e = "") { var i, r, n, o, a; const l = { Sl: !0, jl: -1, Bl: 0 }; if ((null == t && (t = !0), !t && !e)) return l; let u, c, d = null; if (t) d = null === (i = this.C) || void 0 === i ? void 0 : i.ft(s.gt.zl); else { const t = h.Hl(this.C); if (null == t || null == t[e]) return l; d = t[e]; } if (null == d || isNaN(d)) return l; if ( (t ? ((u = (null === (r = this.B) || void 0 === r ? void 0 : r.Ol()) || -1), (c = (null === (n = this.B) || void 0 === n ? void 0 : n.Gl()) || -1)) : ((u = (null === (o = this.B) || void 0 === o ? void 0 : o._l(e)) || -1), (c = (null === (a = this.B) || void 0 === a ? void 0 : a.Wl(e)) || -1)), -1 === u || -1 === c) ) return l; const f = new Date().valueOf(); let m = this.Pl(f, u, c, d, e); return m < 1 ? ((l.Sl = !1), (l.Bl = this.Ul(m, c)), l) : ((m = Math.trunc(m) - 1), (l.jl = m), t ? this.Xl(m) : this.El(e, m), l); } Cl() { return this.xl(!0); } Nl(t) { const e = this.xl(!1, t); return !(e && !e.Sl); } it() { this.Na.it(); } Z() { return this.Oa; } addSdkMetadata(t) { for (const e of t) -1 === this.il.indexOf(e) && this.il.push(e); } }