@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
468 lines (467 loc) • 12.9 kB
JavaScript
import Ie from "../models/braze-event.js";
import _t from "../models/identifier.js";
import {
isArray as g,
keys as D,
validateValueIsFromEnum as ta,
values as Wt,
} from "../util/code-utils.js";
import { logger as E, EventTypes as p } from "../../shared-lib/index.js";
import { User } from "../User/index.js";
import ro from "../util/browser-detector.js";
import { getErrorMessage as ai } from "../util/error-utils.js";
export const STORAGE_KEYS = {
Ou: {
Cu: "ab.storage.userId",
Wh: "ab.storage.deviceId",
um: "ab.storage.sessionId",
},
Tt: {
_c: "ab.test",
QE: "ab.storage.events",
ZE: "ab.storage.attributes",
$E: "ab.storage.attributes.anonymous_user",
Ba: "ab.storage.device",
Ka: "ab.storage.sdk_metadata",
Pa: "ab.storage.session_id_for_cached_metadata",
Uu: "ab.storage.pushToken",
tS: "ab.storage.cardImpressions",
tc: "ab.storage.serverConfig",
eS: "ab.storage.triggers",
sS: "ab.storage.triggers.ts",
dm: "ab.storage.messagingSessionStart",
Ci: "ab.storage.cc",
gi: "ab.storage.ccLastFullSync",
ji: "ab.storage.ccLastCardUpdated",
Ml: "ab.storage.globalRateLimitCurrentTokenCount",
Ll: "ab.storage.dynamicRateLimitCurrentTokenCount",
ns: "ab.storage.ccClicks",
hs: "ab.storage.ccImpressions",
us: "ab.storage.ccDismissals",
rS: "ab.storage.lastDisplayedTriggerTimesById",
oS: "ab.storage.lastDisplayedTriggerTime",
nS: "ab.storage.triggerFireInstancesById",
fh: "ab.storage.signature",
aS: "ab.storage.brazeSyncRetryCount",
ki: "ab.storage.sdkVersion",
Fo: "ab.storage.ff",
jo: "ab.storage.ffImpressions",
qo: "ab.storage.ffLastRefreshAt",
Do: "ab.storage.ff.sessionId",
iS: "ab.storage.lastReqToEndpoint",
ES: "ab.storage.requestAttempts",
pa: "ab.storage.deferredIam",
Cl: "ab.storage.lastSdkReq",
SS: "ab.storage.alias",
St: "ab.storage.banners",
Wt: "ab.storage.banners.impressions",
Mt: "ab.storage.banners.dismissals",
_t: "ab.storage.banners.sessionId",
Gt: "ab.storage.banners.lastRequestedTime",
Ke: "ab.storage.dust.mite",
Qe: "ab.storage.dust.host",
Ve: "ab.storage.dust.auth",
Xe: "ab.storage.dust.expiration",
},
ce: "ab.optOut",
};
export default class ne {
constructor(t, e) {
(this.lS = t), (this._S = e), (this.lS = t), (this._S = e);
}
hl(t) {
const e = D(STORAGE_KEYS.Ou),
s = new ne.le(t);
for (const t of e) s.remove(STORAGE_KEYS.Ou[t]);
}
Iu(t, e) {
let s = null;
null != e && e instanceof _t && (s = e.qt()), this.lS.store(t, s);
}
uS(t) {
const e = this.$u(t);
null != e && ((e.pm = new Date().valueOf()), this.Iu(t, e));
}
$u(t) {
const e = this.lS.wr(t),
s = ((t) => {
let e;
try {
e = JSON.parse(t);
} catch (t) {
e = null;
}
return e;
})(e);
let r;
if (s) (r = _t._u(s) || null), r && this.Iu(t, r);
else {
const s = _t.cS(e);
(r = _t._u(s) || null), s !== e && r && this.Iu(t, r);
}
return r;
}
jm(t) {
this.lS.remove(t);
}
ll() {
const t = D(STORAGE_KEYS.Ou);
let e;
for (const s of t)
(e = this.$u(STORAGE_KEYS.Ou[s])),
null != e && this.Iu(STORAGE_KEYS.Ou[s], e);
}
ol(t) {
let e;
if (null == t || 0 === t.length) return !1;
e = g(t) ? t : [t];
let s = this._S.wr(STORAGE_KEYS.Tt.QE);
(null != s && g(s)) || (s = []);
for (let t = 0; t < e.length; t++) s.push(e[t].qt());
return this._S.store(STORAGE_KEYS.Tt.QE, s);
}
gm(t) {
return null != t && this.ol([t]);
}
TS() {
let t = this._S.wr(STORAGE_KEYS.Tt.QE);
this._S.remove(STORAGE_KEYS.Tt.QE), null == t && (t = []);
const e = [];
let s = !1,
r = null;
if (g(t))
for (let s = 0; s < t.length; s++)
Ie.hS(t[s]) ? e.push(Ie._u(t[s])) : (r = s);
else s = !0;
if (s || null != r) {
let o = "Stored events could not be deserialized as Events";
s &&
(o += ", was " + Object.prototype.toString.call(t) + " not an array"),
null != r &&
(o += ", value at index " + r + " does not look like an event"),
(o +=
", serialized values were of type " +
typeof t +
": " +
JSON.stringify(t)),
e.push(new Ie(null, p.Ya, new Date().valueOf(), null, { e: o }));
}
return e;
}
It(t, e) {
return (
!!ta(
STORAGE_KEYS.Tt,
t,
"StorageManager cannot store object.",
"STORAGE_KEYS.OBJECTS",
) && this._S.store(t, e)
);
}
Rt(t) {
return (
!!ta(
STORAGE_KEYS.Tt,
t,
"StorageManager cannot retrieve object.",
"STORAGE_KEYS.OBJECTS",
) && this._S.wr(t)
);
}
Qt(t) {
return (
!!ta(
STORAGE_KEYS.Tt,
t,
"StorageManager cannot remove object.",
"STORAGE_KEYS.OBJECTS",
) && (this._S.remove(t), !0)
);
}
clearData() {
const t = D(STORAGE_KEYS.Ou),
e = D(STORAGE_KEYS.Tt);
for (let e = 0; e < t.length; e++) {
const s = t[e];
this.lS.remove(STORAGE_KEYS.Ou[s]);
}
for (let t = 0; t < e.length; t++) {
const s = e[t];
this._S.remove(STORAGE_KEYS.Tt[s]);
}
}
AS(t) {
return t || STORAGE_KEYS.Tt.$E;
}
vl(t) {
let e = this._S.wr(STORAGE_KEYS.Tt.ZE);
null == e && (e = {});
const s = this.AS(t[User.Sa]),
r = e[s];
for (const o in t)
o !== User.Sa &&
(null == e[s] || (r && null == r[o])) &&
this.Eu(t[User.Sa], o, t[o]);
}
Eu(t, e, s) {
let r = this._S.wr(STORAGE_KEYS.Tt.ZE);
null == r && (r = {});
const o = this.AS(t);
let n = r[o];
if (
(null == n && ((n = {}), null != t && (n[User.Sa] = t)), e === User.Bu)
) {
null == n[e] && (n[e] = {});
for (const t in s) n[e][t] = s[t];
} else n[e] = s;
return (r[o] = n), this._S.store(STORAGE_KEYS.Tt.ZE, r);
}
gS() {
const t = this._S.wr(STORAGE_KEYS.Tt.ZE);
this._S.remove(STORAGE_KEYS.Tt.ZE);
const e = [];
for (const s in t) null != t[s] && e.push(t[s]);
return e;
}
Lu(t) {
const e = this._S.wr(STORAGE_KEYS.Tt.ZE);
if (null != e) {
const s = this.AS(null),
r = e[s];
null != r &&
((e[s] = void 0),
this._S.store(STORAGE_KEYS.Tt.ZE, e),
(r[User.Sa] = t),
this.vl(r));
}
const s = this.$u(STORAGE_KEYS.Ou.um);
let r = null;
null != s && (r = s.Tu);
const o = this.TS();
if (null != o)
for (let e = 0; e < o.length; e++) {
const s = o[e];
null == s.userId && s.sessionId == r && (s.userId = t), this.gm(s);
}
}
RS() {
return this._S.dS;
}
}
(ne.wc = class {
constructor(t) {
(this.tu = t), (this.tu = t), (this.dS = ro.IS() ? 3 : 10);
}
bS(t) {
return t + "." + this.tu;
}
store(t, e) {
const s = { v: e };
try {
return localStorage.setItem(this.bS(t), JSON.stringify(s)), !0;
} catch (t) {
return E.info("Storage failure: " + ai(t)), !1;
}
}
wr(t) {
try {
let e = null;
const s = localStorage.getItem(this.bS(t));
return null != s && (e = JSON.parse(s)), null == e ? null : e.v;
} catch (t) {
return E.info("Storage retrieval failure: " + ai(t)), null;
}
}
remove(t) {
try {
localStorage.removeItem(this.bS(t));
} catch (t) {
return E.info("Storage removal failure: " + ai(t)), !1;
}
}
}),
(ne.Rc = class {
constructor() {
(this.fS = {}), (this.mS = 5242880), (this.dS = 3);
}
store(t, e) {
const s = { value: e },
r = this.KS(e);
return r > this.mS
? (E.info(
"Storage failure: object is ≈" +
r +
" bytes which is greater than the max of " +
this.mS,
),
!1)
: ((this.fS[t] = s), !0);
}
KS(t) {
const e = [],
s = [t];
let r = 0;
for (; s.length; ) {
const t = s.pop();
if ("boolean" == typeof t) r += 4;
else if ("string" == typeof t) r += 2 * t.length;
else if ("number" == typeof t) r += 8;
else if ("object" == typeof t && -1 === e.indexOf(t)) {
let r, o;
e.push(t);
for (const e in t) (o = t), (r = e), s.push(o[r]);
}
}
return r;
}
wr(t) {
const e = this.fS[t];
return null == e ? null : e.value;
}
remove(t) {
this.fS[t] = null;
}
}),
(ne.le = class {
constructor(t, e, s) {
(this.tu = t), (this.YS = e), (this.tu = t), (this.NS = this.DS());
const r = "number" == typeof s && s > 0 ? s : 400;
(this.GS = 24 * r * 60), (this.CS = {}), (this.YS = !!e);
}
bS(t) {
return null != this.tu ? t + "." + this.tu : t;
}
DS() {
let t = 0,
e = document.location.hostname;
const s = e.split("."),
r = "ab._gd";
for (; t < s.length - 1 && -1 === document.cookie.indexOf(r + "=" + r); )
t++,
(e = "." + s.slice(-1 - t).join(".")),
(document.cookie = r + "=" + r + ";domain=" + e + ";");
return (
(document.cookie =
r + "=;expires=" + new Date(0).toUTCString() + ";domain=" + e + ";"),
e
);
}
me() {
const t = new Date();
return t.setTime(t.getTime() + 60 * this.GS * 1e3), t.getFullYear();
}
MS() {
const t = Wt(STORAGE_KEYS.Ou),
e = document.cookie.split(";");
for (let s = 0; s < e.length; s++) {
let r = e[s];
for (; " " === r.charAt(0); ) r = r.substring(1);
let o = !1;
for (let e = 0; e < t.length; e++)
if (0 === r.indexOf(t[e])) {
o = !0;
break;
}
if (o) {
const t = r.split("=")[0];
-1 === t.indexOf("." + this.tu) && this.pS(t);
}
}
}
store(t, e) {
this.MS();
const s = this.bS(t),
r = new Date();
r.setTime(r.getTime() + 60 * this.GS * 1e3);
const o = "expires=" + r.toUTCString(),
n = "domain=" + this.NS;
let a;
a = this.YS ? e : encodeURIComponent(e);
const i = s + "=" + a + ";" + o + ";" + n + ";path=/";
if (i.length >= 4093)
return (
E.info(
"Storage failure: string is " +
i.length +
" chars which is too large to store as a cookie.",
),
(this.CS[s] = !0),
this.pS(s),
!1
);
try {
document.cookie = i;
} catch (t) {
return E.info("Storage failure: " + ai(t)), (this.CS[s] = !0), !1;
}
const S = this.YS ? String(a) : decodeURIComponent(a);
return this.vS(s) !== S
? (E.info(
`Storage failure: unable to verify cookie write for "${s}". Falling back to other storage.`,
),
(this.CS[s] = !0),
this.pS(s),
!1)
: (delete this.CS[s], !0);
}
wr(t) {
const e = this.bS(t);
return this.CS[e] ? null : this.vS(e);
}
vS(t) {
const e = [],
s = t + "=",
r = document.cookie.split(";");
for (let o = 0; o < r.length; o++) {
let n = r[o];
for (; " " === n.charAt(0); ) n = n.substring(1);
if (0 === n.indexOf(s))
try {
let t;
(t = this.YS
? n.substring(s.length, n.length)
: decodeURIComponent(n.substring(s.length, n.length))),
e.push(t);
} catch (e) {
return (
E.info("Storage retrieval failure: " + ai(e)), this.pS(t), null
);
}
}
return e.length > 0 ? e[e.length - 1] : null;
}
remove(t) {
this.pS(this.bS(t));
}
pS(t) {
const e = t + "=;expires=" + new Date(0).toUTCString();
(document.cookie = e), (document.cookie = e + ";path=/");
const s = e + ";domain=" + this.NS;
(document.cookie = s), (document.cookie = s + ";path=/");
}
}),
(ne.Ec = class {
constructor(t, e, s, r) {
(this.tu = t),
(this.US = []),
e && this.US.push(new ne.le(t, void 0, r)),
s && this.US.push(new ne.wc(t)),
this.US.push(new ne.Rc());
}
store(t, e) {
let s = !0;
for (let r = 0; r < this.US.length; r++) s = this.US[r].store(t, e) && s;
return s;
}
wr(t) {
for (let e = 0; e < this.US.length; e++) {
const s = this.US[e].wr(t);
if (null != s) return s;
}
return null;
}
remove(t) {
new ne.le(this.tu).remove(t);
for (let e = 0; e < this.US.length; e++) this.US[e].remove(t);
}
});