@braze/service-worker 4.0.2 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/service-worker.js +8 -8
package/package.json
CHANGED
package/service-worker.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Braze Web SDK v4.0.
|
|
2
|
+
* Braze Web SDK v4.0.5
|
|
3
3
|
* (c) Braze, Inc. 2022 - http://braze.com
|
|
4
4
|
* License available at https://github.com/braze-inc/braze-web-sdk/blob/master/LICENSE
|
|
5
|
-
* Compiled on 2022-
|
|
5
|
+
* Compiled on 2022-07-01
|
|
6
6
|
*/
|
|
7
7
|
'use strict';const k={P:function(a){var b="=".repeat((4-a.length%4)%4);a=(a+b).replace(/\-/g,"+").replace(/_/g,"/");a=atob(a);b=new Uint8Array(a.length);for(let d=0;d<a.length;++d)b[d]=a.charCodeAt(d);return b}};const l={H:()=>{const a=b=>{const d=(Math.random().toString(16)+"000000000").substr(2,8);return b?"-"+d.substr(0,4)+"-"+d.substr(4,4):d};return a()+a(!0)+a(!0)+a()}};function q(a){if("indexedDB"in a.G)return a.G.indexedDB}function r(a){try{if(null==q(a))return!1;q(a).open("Braze IndexedDB Support Test");if("undefined"!==typeof window){const b=window.qa||window.pa||window.sa;if(b&&b.O&&b.O.id)return a.j.info("Not using IndexedDB for storage because we are running inside an extension"),!1}return!0}catch(b){return a.j.info("Not using IndexedDB for storage due to following error: "+b),!1}}
|
|
8
8
|
function t(a,b,d){const f=q(a).open(a.h.l,a.h.VERSION);if(null==f)return"function"===typeof d&&d(),!1;f.onupgradeneeded=e=>{a.j.info("Upgrading indexedDB "+a.h.l+" to v"+a.h.VERSION+"...");e=e.target.result;for(const c in a.h.g)a.h.g.hasOwnProperty(c)&&!e.objectStoreNames.contains(a.h.g[c])&&e.createObjectStore(a.h.g[c])};f.onsuccess=e=>{const c=e.target.result;c.onversionchange=()=>{c.close();"function"===typeof d&&d();a.j.error("Needed to close the database unexpectedly because of an upgrade in another tab")};
|
|
@@ -11,12 +11,12 @@ function w(a,b,d,f){r(a)?t(a,e=>{e.objectStoreNames.contains(b)?(e=e.transaction
|
|
|
11
11
|
f):"function"===typeof f&&f()}
|
|
12
12
|
class x{constructor(a,b){this.G="undefined"===typeof window?self:window;this.h=a;this.j=b}setItem(a,b,d,f,e){if(!r(this))return"function"===typeof e&&e(),!1;const c=this;return t(this,g=>{g.objectStoreNames.contains(a)?(g=g.transaction([a],"readwrite").objectStore(a).put(d,b),g.onerror=()=>{c.j.error("Could not store object "+b+" in "+a+" on indexedDB "+c.h.l);"function"===typeof e&&e()},g.onsuccess=()=>{"function"===typeof f&&f()}):(c.j.error("Could not store object "+b+" in "+a+" on indexedDB "+
|
|
13
13
|
c.h.l+" - "+a+" is not a valid objectStore"),"function"===typeof e&&e())},e)}getItem(a,b,d){if(!r(this))return!1;const f=this;return t(this,e=>{e.objectStoreNames.contains(a)?(e=e.transaction([a],"readonly").objectStore(a).get(b),e.onerror=()=>{f.j.error("Could not retrieve object "+b+" in "+a+" on indexedDB "+f.h.l)},e.onsuccess=c=>{c=c.target.result;null!=c&&d(c)}):f.j.error("Could not retrieve object "+b+" in "+a+" on indexedDB "+f.h.l+" - "+a+" is not a valid objectStore")})}clearData(){if(!r(this))return!1;
|
|
14
|
-
const a=[];for(const d in this.h.g)this.h.g.hasOwnProperty(d)&&this.h.g[d]!==this.h.g.C&&a.push(this.h.g[d]);const b=this;return t(this,function(d){d=d.transaction(a,"readwrite");for(let f=0;f<a.length;f++)d.objectStore(a[f]).clear().onerror=function(){b.j.error("Could not clear "+this.source.name+" on indexedDB "+b.h.l)};d.onerror=function(){b.j.error("Could not clear object stores on indexedDB "+b.h.l)}})}};const y={B:function(a){if(void 0!==a||void 0===y.o)y.o=!!a;y.F||(y.F=!0)},ra:function(){y.F=!1;y.o=void 0;y.j=void 0},ta:function(a){"function"!==typeof a?y.info("Ignoring setLogger call since logger is not a function"):(y.B(),y.j=a)},ua:function(){y.B();y.o?(console.log("Disabling Braze logging"),y.o=!1):(console.log("Enabled Braze logging"),y.o=!0)},info:function(a){y.o&&(a="Braze: "+a,null!=y.j?y.j(a):console.log(a))},warn:function(a){y.o&&(a="Braze SDK Warning: "+a+" (v4.0.
|
|
15
|
-
y.j?y.j(a):console.warn(a))},error:function(a){y.o&&(a="Braze SDK Error: "+a+" (v4.0.
|
|
14
|
+
const a=[];for(const d in this.h.g)this.h.g.hasOwnProperty(d)&&this.h.g[d]!==this.h.g.C&&a.push(this.h.g[d]);const b=this;return t(this,function(d){d=d.transaction(a,"readwrite");for(let f=0;f<a.length;f++)d.objectStore(a[f]).clear().onerror=function(){b.j.error("Could not clear "+this.source.name+" on indexedDB "+b.h.l)};d.onerror=function(){b.j.error("Could not clear object stores on indexedDB "+b.h.l)}})}};const y={B:function(a){if(void 0!==a||void 0===y.o)y.o=!!a;y.F||(y.F=!0)},ra:function(){y.F=!1;y.o=void 0;y.j=void 0},ta:function(a){"function"!==typeof a?y.info("Ignoring setLogger call since logger is not a function"):(y.B(),y.j=a)},ua:function(){y.B();y.o?(console.log("Disabling Braze logging"),y.o=!1):(console.log("Enabled Braze logging"),y.o=!0)},info:function(a){y.o&&(a="Braze: "+a,null!=y.j?y.j(a):console.log(a))},warn:function(a){y.o&&(a="Braze SDK Warning: "+a+" (v4.0.5)",null!=
|
|
15
|
+
y.j?y.j(a):console.warn(a))},error:function(a){y.o&&(a="Braze SDK Error: "+a+" (v4.0.5)",null!=y.j?y.j(a):console.error(a))}};var z={CustomEvent:"ce",ga:"p",M:"pc",L:"ca",ia:"i",ha:"ie",W:"cci",X:"ccic",U:"ccc",V:"ccd",na:"ss",ma:"se",fa:"si",da:"sc",ca:"sbc",ea:"sfe",Y:"iec",la:"lr",R:"uae",T:"ci",S:"cc",ja:"lcaa",ka:"lcar",$:"inc",Z:"add",aa:"rem",ba:"set",oa:"sgu"},A=x,B={m:{l:"AppboyServiceWorkerAsyncStorage",VERSION:6,g:{s:"data",K:"pushClicks",D:"pushSubscribed",A:"fallbackDevice",I:"cardUpdates",C:"optOut",J:"pendingData",N:"sdkAuthenticationSignature"},v:1}},E=y;function F(){return new Promise(function(a,b){const d=B.m;w(new A(d,E),d.g.C,b,a)})};function G(){return new Promise(a=>{const b=B.m;w(new A(b,E),b.g.N,(d,f)=>{a(f)},()=>{a(null)})})}function H(a,b,d){E.info(`${b} due to SDK Authentication failure with error code ${d.error_code}. The data will be logged on the user's next session start.`);b=B.m;(new A(b,E)).setItem(b.g.J,l.H(),a)};function I(a,b,d){return new Promise(function(f,e){const c={};c.time=Math.floor((new Date).valueOf()/1E3);c.device_id=d;c.api_key=a;c.sdk_version="4.0.5";c.sdk_flavor="amp";c.respond_with={config:{config_time:0}};fetch(b+"/data/",{method:"POST",headers:{"Content-type":"application/json","X-Braze-Api-Key":a},body:JSON.stringify(c)}).then(function(g){g.ok||E.error("Unable to get config: "+g.status);return g.json()}).then(function(g){g.error&&(E.error("Unable to get config: "+g.error),e());
|
|
16
16
|
g.auth_error&&(E.error("Unable to get config due to authentication error. SDK Authentication does not support AMP pages."),e());g.error||g.auth_error||(g={userVisibleOnly:!0,applicationServerKey:k.P(g.config.vapid_public_key)},f(g))}).catch(function(g){E.error("Unable to get config: "+g);e()})})}
|
|
17
|
-
function J(a,b,d,f,e,c,g,h,m){return G().then(n=>new Promise(function(C,D){const u={};u.device_id=d;u.api_key=a;u.sdk_version="4.0.
|
|
17
|
+
function J(a,b,d,f,e,c,g,h,m){return G().then(n=>new Promise(function(C,D){const u={};u.device_id=d;u.api_key=a;u.sdk_version="4.0.5";null!=c&&(u.sdk_flavor=c);var v=null;let K=null,L=null;e&&(L=e.endpoint,e.getKey&&(v=btoa(String.fromCharCode.apply(null,new Uint8Array(e.getKey("p256dh")))),K=btoa(String.fromCharCode.apply(null,new Uint8Array(e.getKey("auth"))))));u.time=Math.floor((new Date).valueOf()/1E3);u.attributes=[{user_id:f,push_token:L,custom_push_public_key:v,custom_push_user_auth:K}];
|
|
18
18
|
v={"Content-type":"application/json","X-Braze-Api-Key":u.api_key};n&&m&&(v["X-Braze-Auth-Signature"]=n);fetch(b+"/data/",{method:"POST",headers:v,body:JSON.stringify(u)}).then(function(p){p.ok||E.error(h+" "+p.status);return p.json()}).then(function(p){p.error&&(E.error(h+" "+p.error),D());p.auth_error&&(H(u,h,p.auth_error),D());p.error||p.auth_error||(E.info(g),C())}).catch(function(p){E.error(h+" "+p);D()})}))}
|
|
19
|
-
function M(a,b){return F().then(function(){return G()}).then(function(d){const f=B.m;w(new A(f,E),f.g.s,function(e,c){e=Math.floor((new Date).valueOf()/1E3);const g=c.data;g.time=e;a.time=e;a.user_id=c.userId;g.events=[a];g.sdk_version="4.0.
|
|
19
|
+
function M(a,b){return F().then(function(){return G()}).then(function(d){const f=B.m;w(new A(f,E),f.g.s,function(e,c){e=Math.floor((new Date).valueOf()/1E3);const g=c.data;g.time=e;a.time=e;a.user_id=c.userId;g.events=[a];g.sdk_version="4.0.5";e={"Content-Type":"application/json","X-Braze-Api-Key":g.api_key};d&&c.sdkAuthEnabled&&(e["X-Braze-Auth-Signature"]=d);fetch(c.baseUrl+"/data/",{method:"POST",headers:e,body:JSON.stringify(g)}).then(function(h){h.ok||E.error("Unable to log "+b+": "+
|
|
20
20
|
h.status);return h.json()}).then(function(h){h.error&&E.error("Unable to log "+b+":",h.error);h.auth_error&&H(g,"Unable to log "+b,h.auth_error);h.error||h.auth_error||E.info("Successfully logged "+b);return Promise.resolve()}).catch(function(h){E.error("Unable to log "+b+":",h);return Promise.resolve()})})}).catch(function(){return Promise.reject("Not sending data to Braze backend due to opt-out.")})};function N(){const a=self.location.search.match(/apiKey=([^&]+)/i);if(a)return a[1];E.error("Missing API key in query params.");return null}function O(){const a=self.location.search.match(/baseUrl=([^&]+)/i);if(a)return a[1];E.error("Missing base URL in query params.");return null};function P(a,b){self.clients.matchAll().then(function(d){for(let f=0;f<d.length;f++)d[f].postMessage({command:a,payload:b})})}
|
|
21
21
|
function Q(a,b,d,f,e){return I(a,b,d).then(function(c){return self.registration.pushManager.subscribe(c)}).then(function(c){P("amp-web-push-subscribe",null);return J(a,b,d,f,c,"amp","Successfully sent AMP push subscription to Braze backend.","Unable to send AMP push subscription to Braze backend.",e)}).catch(function(){E.error("Failed to subscribe for AMP push.");return Promise.reject()})}
|
|
22
22
|
function R(){self.registration.pushManager.getSubscription().then(function(a){return a?self.registration.pushManager.permissionState(a.options):null}).then(function(a){P("amp-web-push-subscription-state","granted"===a)})}
|
|
@@ -24,9 +24,9 @@ function S(){const a=B.m,b=new A(a,E);return(new Promise(function(d,f){w(b,a.g.s
|
|
|
24
24
|
f){b.setItem(a.g.D,a.v,!0,d,f)})})}
|
|
25
25
|
function T(){return self.registration.pushManager.getSubscription().then(function(a){return a.unsubscribe()}).then(function(){P("amp-web-push-unsubscribe",null);const a=B.m,b=new A(a,E);return(new Promise(function(d,f){w(b,a.g.s,function(e,c){J(c.data.api_key,c.baseUrl,c.data.device_id,c.userId,null,"amp","Successfully sent AMP push unsubscription to Braze backend.","Unable to send AMP push unsubscription to Braze backend.",c.sdkAuthEnabled).then(function(){d()}).catch(function(){f()})},function(){w(b,
|
|
26
26
|
a.g.A,function(e,c){e=N();const g=O();J(e,g,c,null,null,"amp","Successfully sent AMP push unsubscription to Braze backend.","Unable to send AMP push unsubscription to Braze backend.",null).then(function(){d()}).catch(function(){f()})},function(){E.error("No device found during unsubscription.");f()})})})).then(function(){return new Promise(function(d,f){b.setItem(a.g.D,a.v,!1,d,f)})})}).catch(function(){E.error("Failed to unsubscribe for AMP push.");return Promise.reject()})};function U(a,b){a.waitUntil(b.catch(function(d){d&&E.info(d)}))};E.B(!0);
|
|
27
|
-
function V(a){if(null==a||0===Object.keys(a).length)return Promise.reject("Server has no pending push message for this registration. Ignoring push event.");const b=a.t,d=a.a,f=a.i,e=a.img,c={url:a.u,ab_ids:{cid:a.cid},extra:a.e},g=a.ri;a.ab_push_fetch_test_triggers_key&&(E.info("Service worker 4.0.
|
|
27
|
+
function V(a){if(null==a||0===Object.keys(a).length)return Promise.reject("Server has no pending push message for this registration. Ignoring push event.");const b=a.t,d=a.a,f=a.i,e=a.img,c={url:a.u,ab_ids:{cid:a.cid},extra:a.e},g=a.ri;a.ab_push_fetch_test_triggers_key&&(E.info("Service worker 4.0.5 found trigger fetch key in push payload."),c.fetchTriggers=!0);var h=a.ab_cd;if(null!=h){var m=B.m;(new A(m,E)).setItem(m.g.I,(new Date).valueOf(),{userId:a.ab_cd_uid,card:h})}a=a.pab||[];h=
|
|
28
28
|
{};for(m=0;m<a.length;m++)if(null!=a[m]&&null!=a[m].action){let n;switch(a[m].a){case "ab_none":n=null;break;case "ab_uri":if(n=a[m].u,null==n||""===n)n="/"}h[a[m].action]=n}c.actionTargets=h;E.info("Displaying push notification!");return self.registration.showNotification(b,{body:d,icon:f,image:e,data:c,actions:a,requireInteraction:g}).catch(function(n){E.info(n)})}self.addEventListener("install",function(a){a.waitUntil(self.skipWaiting())});self.addEventListener("activate",function(){return self.clients.claim()});
|
|
29
|
-
self.addEventListener("push",function(a){E.info("Service worker 4.0.
|
|
29
|
+
self.addEventListener("push",function(a){E.info("Service worker 4.0.5 received push");null!=a.data&&null!=a.data.json?U(a,V(a.data.json())):U(a,new Promise(function(b,d){const f=B.m;w(new A(f,E),f.g.s,function(e,c){const g=c.data;F().then(function(){return fetch(c.baseUrl+"/web_push/",{method:"POST",headers:{"Content-type":"application/json","X-Braze-Api-Key":g.api_key},body:JSON.stringify(g)})}).then(function(h){return h.ok?h.json():(E.error("Unable to retrieve push payload from server: "+
|
|
30
30
|
h.status),Promise.reject())}).then(function(h){E.info("Retrieved push payload from server");b(V(h))}).catch(function(h){d("Unable to retrieve push payload from server or user has opt-out: "+h)})})}))});
|
|
31
31
|
self.addEventListener("notificationclick",function(a){if(a&&a.notification&&(a.notification.close(),null!=Notification&&Notification.prototype.hasOwnProperty("data")&&a.notification.data&&a.notification.data.ab_ids)){var b=null!=a.action&&""!==a.action;var d=b?M({name:z.L,data:{cid:a.notification.data.ab_ids.cid,a:a.action}},"push button click"):M({name:z.M,data:{cid:a.notification.data.ab_ids.cid}},"push click");if(!b){const g={lastClick:(new Date).valueOf(),trackingString:a.notification.data.ab_ids.cid};
|
|
32
32
|
a.notification.data.fetchTriggers&&(g.fetchTriggers=!0);const h=B.m,m=new A(h,E);var f=d.then(function(){return new Promise(function(n,C){m.setItem(h.g.K,h.v,g,n,C)})}).catch(function(){E.info("Not storing push click due to no click event being created.");return Promise.resolve()})}if(b)var e=a.notification.data.actionTargets[a.action];else if(e=a.notification.data.url,null==e||""===e)e="/";var c;null!=e&&""!==e&&(c=clients.matchAll({type:"window"}).then(function(){if(clients.openWindow)return clients.openWindow(e)}));
|