@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 C,
validateValueIsFromEnum as ta,
values as Wt,
} from "../util/code-utils.js";
import { logger as b, EventTypes as p } from "../../shared-lib/index.js";
import { User } from "../User/index.js";
import ro from "../util/browser-detector.js";
import { getErrorMessage as hi } from "../util/error-utils.js";
export const STORAGE_KEYS = {
Ou: {
Cu: "ab.storage.userId",
Wh: "ab.storage.deviceId",
um: "ab.storage.sessionId",
},
It: {
ac: "ab.test",
ZE: "ab.storage.events",
$E: "ab.storage.attributes",
tS: "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",
eS: "ab.storage.cardImpressions",
sd: "ab.storage.serverConfig",
sS: "ab.storage.triggers",
rS: "ab.storage.triggers.ts",
dm: "ab.storage.messagingSessionStart",
bi: "ab.storage.cc",
ji: "ab.storage.ccLastFullSync",
yi: "ab.storage.ccLastCardUpdated",
Ml: "ab.storage.globalRateLimitCurrentTokenCount",
Ll: "ab.storage.dynamicRateLimitCurrentTokenCount",
Zt: "ab.storage.ccClicks",
ps: "ab.storage.ccImpressions",
fs: "ab.storage.ccDismissals",
oS: "ab.storage.lastDisplayedTriggerTimesById",
nS: "ab.storage.lastDisplayedTriggerTime",
aS: "ab.storage.triggerFireInstancesById",
fh: "ab.storage.signature",
iS: "ab.storage.brazeSyncRetryCount",
zi: "ab.storage.sdkVersion",
Fo: "ab.storage.ff",
jo: "ab.storage.ffImpressions",
qo: "ab.storage.ffLastRefreshAt",
Do: "ab.storage.ff.sessionId",
ES: "ab.storage.lastReqToEndpoint",
SS: "ab.storage.requestAttempts",
pa: "ab.storage.deferredIam",
Bl: "ab.storage.lastSdkReq",
lS: "ab.storage.alias",
Tt: "ab.storage.banners",
At: "ab.storage.banners.impressions",
Xt: "ab.storage.banners.dismissals",
kt: "ab.storage.banners.sessionId",
Ht: "ab.storage.banners.lastRequestedTime",
Xe: "ab.storage.dust.mite",
Ye: "ab.storage.dust.host",
Ze: "ab.storage.dust.auth",
sn: "ab.storage.dust.expiration",
},
ce: "ab.optOut",
};
export default class ne {
constructor(t, e) {
(this._S = t), (this.uS = e), (this._S = t), (this.uS = e);
}
hl(t) {
const e = C(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._S.store(t, s);
}
cS(t) {
const e = this.$u(t);
null != e && ((e.pm = new Date().valueOf()), this.Iu(t, e));
}
$u(t) {
const e = this._S.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.TS(e);
(r = _t._u(s) || null), s !== e && r && this.Iu(t, r);
}
return r;
}
jm(t) {
this._S.remove(t);
}
ll() {
const t = C(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.uS.wr(STORAGE_KEYS.It.ZE);
(null != s && g(s)) || (s = []);
for (let t = 0; t < e.length; t++) s.push(e[t].qt());
return this.uS.store(STORAGE_KEYS.It.ZE, s);
}
gm(t) {
return null != t && this.ol([t]);
}
hS() {
let t = this.uS.wr(STORAGE_KEYS.It.ZE);
this.uS.remove(STORAGE_KEYS.It.ZE), null == t && (t = []);
const e = [];
let s = !1,
r = null;
if (g(t))
for (let s = 0; s < t.length; s++)
Ie.AS(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;
}
Pt(t, e) {
return (
!!ta(
STORAGE_KEYS.It,
t,
"StorageManager cannot store object.",
"STORAGE_KEYS.OBJECTS",
) && this.uS.store(t, e)
);
}
St(t) {
return (
!!ta(
STORAGE_KEYS.It,
t,
"StorageManager cannot retrieve object.",
"STORAGE_KEYS.OBJECTS",
) && this.uS.wr(t)
);
}
Vt(t) {
return (
!!ta(
STORAGE_KEYS.It,
t,
"StorageManager cannot remove object.",
"STORAGE_KEYS.OBJECTS",
) && (this.uS.remove(t), !0)
);
}
clearData() {
const t = C(STORAGE_KEYS.Ou),
e = C(STORAGE_KEYS.It);
for (let e = 0; e < t.length; e++) {
const s = t[e];
this._S.remove(STORAGE_KEYS.Ou[s]);
}
for (let t = 0; t < e.length; t++) {
const s = e[t];
this.uS.remove(STORAGE_KEYS.It[s]);
}
}
gS(t) {
return t || STORAGE_KEYS.It.tS;
}
vl(t) {
let e = this.uS.wr(STORAGE_KEYS.It.$E);
null == e && (e = {});
const s = this.gS(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.uS.wr(STORAGE_KEYS.It.$E);
null == r && (r = {});
const o = this.gS(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.uS.store(STORAGE_KEYS.It.$E, r);
}
RS() {
const t = this.uS.wr(STORAGE_KEYS.It.$E);
this.uS.remove(STORAGE_KEYS.It.$E);
const e = [];
for (const s in t) null != t[s] && e.push(t[s]);
return e;
}
Lu(t) {
const e = this.uS.wr(STORAGE_KEYS.It.$E);
if (null != e) {
const s = this.gS(null),
r = e[s];
null != r &&
((e[s] = void 0),
this.uS.store(STORAGE_KEYS.It.$E, 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.hS();
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);
}
}
dS() {
return this.uS.IS;
}
}
(ne.ec = class {
constructor(t) {
(this.tu = t), (this.tu = t), (this.IS = ro.bS() ? 3 : 10);
}
fS(t) {
return t + "." + this.tu;
}
store(t, e) {
const s = { v: e };
try {
return localStorage.setItem(this.fS(t), JSON.stringify(s)), !0;
} catch (t) {
return b.info("Storage failure: " + hi(t)), !1;
}
}
wr(t) {
try {
let e = null;
const s = localStorage.getItem(this.fS(t));
return null != s && (e = JSON.parse(s)), null == e ? null : e.v;
} catch (t) {
return b.info("Storage retrieval failure: " + hi(t)), null;
}
}
remove(t) {
try {
localStorage.removeItem(this.fS(t));
} catch (t) {
return b.info("Storage removal failure: " + hi(t)), !1;
}
}
}),
(ne.rc = class {
constructor() {
(this.mS = {}), (this.KS = 5242880), (this.IS = 3);
}
store(t, e) {
const s = { value: e },
r = this.YS(e);
return r > this.KS
? (b.info(
"Storage failure: object is ≈" +
r +
" bytes which is greater than the max of " +
this.KS,
),
!1)
: ((this.mS[t] = s), !0);
}
YS(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.mS[t];
return null == e ? null : e.value;
}
remove(t) {
this.mS[t] = null;
}
}),
(ne.le = class {
constructor(t, e, s) {
(this.tu = t), (this.NS = e), (this.tu = t), (this.DS = this.GS());
const r = "number" == typeof s && s > 0 ? s : 400;
(this.CS = 24 * r * 60), (this.MS = {}), (this.NS = !!e);
}
fS(t) {
return null != this.tu ? t + "." + this.tu : t;
}
GS() {
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.CS * 1e3), t.getFullYear();
}
pS() {
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.vS(t);
}
}
}
store(t, e) {
this.pS();
const s = this.fS(t),
r = new Date();
r.setTime(r.getTime() + 60 * this.CS * 1e3);
const o = "expires=" + r.toUTCString(),
n = "domain=" + this.DS;
let a;
a = this.NS ? e : encodeURIComponent(e);
const i = s + "=" + a + ";" + o + ";" + n + ";path=/";
if (i.length >= 4093)
return (
b.info(
"Storage failure: string is " +
i.length +
" chars which is too large to store as a cookie.",
),
(this.MS[s] = !0),
this.vS(s),
!1
);
try {
document.cookie = i;
} catch (t) {
return b.info("Storage failure: " + hi(t)), (this.MS[s] = !0), !1;
}
const E = this.NS ? String(a) : decodeURIComponent(a);
return this.US(s) !== E
? (b.info(
`Storage failure: unable to verify cookie write for "${s}". Falling back to other storage.`,
),
(this.MS[s] = !0),
this.vS(s),
!1)
: (delete this.MS[s], !0);
}
wr(t) {
const e = this.fS(t);
return this.MS[e] ? null : this.US(e);
}
US(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.NS
? n.substring(s.length, n.length)
: decodeURIComponent(n.substring(s.length, n.length))),
e.push(t);
} catch (e) {
return (
b.info("Storage retrieval failure: " + hi(e)), this.vS(t), null
);
}
}
return e.length > 0 ? e[e.length - 1] : null;
}
remove(t) {
this.vS(this.fS(t));
}
vS(t) {
const e = t + "=;expires=" + new Date(0).toUTCString();
(document.cookie = e), (document.cookie = e + ";path=/");
const s = e + ";domain=" + this.DS;
(document.cookie = s), (document.cookie = s + ";path=/");
}
}),
(ne.tc = class {
constructor(t, e, s, r) {
(this.tu = t),
(this.yS = []),
e && this.yS.push(new ne.le(t, void 0, r)),
s && this.yS.push(new ne.ec(t)),
this.yS.push(new ne.rc());
}
store(t, e) {
let s = !0;
for (let r = 0; r < this.yS.length; r++) s = this.yS[r].store(t, e) && s;
return s;
}
wr(t) {
for (let e = 0; e < this.yS.length; e++) {
const s = this.yS[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.yS.length; e++) this.yS[e].remove(t);
}
});