@braze/web-sdk 4.9.0 → 4.10.1

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.
Files changed (202) hide show
  1. package/index.d.ts +2296 -2235
  2. package/package.json +1 -1
  3. package/shared-lib/braze-shared-lib.js +1 -8
  4. package/shared-lib/encoding-utils.js +1 -12
  5. package/shared-lib/event-types.js +1 -34
  6. package/shared-lib/guid.js +1 -10
  7. package/shared-lib/indexed-db-adapter.js +1 -352
  8. package/shared-lib/logger.js +1 -38
  9. package/shared-lib/supported-options.js +1 -26
  10. package/shared-lib/types.js +1 -1
  11. package/src/Card/card-manager-factory.js +1 -14
  12. package/src/Card/card-manager.js +1 -78
  13. package/src/Card/display/card-display.js +1 -138
  14. package/src/Card/index.js +1 -6
  15. package/src/Card/log-card-click.js +1 -11
  16. package/src/Card/log-card-dismissal.js +1 -11
  17. package/src/Card/log-card-impressions.js +1 -13
  18. package/src/Card/log-content-card-click.js +1 -4
  19. package/src/Card/log-content-card-impressions.js +1 -4
  20. package/src/Card/models/banner.js +1 -31
  21. package/src/Card/models/captioned-image.js +1 -33
  22. package/src/Card/models/card.js +1 -196
  23. package/src/Card/models/classic-card.js +1 -32
  24. package/src/Card/models/control-card.js +1 -23
  25. package/src/Card/models/image-only.js +1 -32
  26. package/src/Card/models/index.js +1 -6
  27. package/src/Card/types.js +1 -1
  28. package/src/Card/util/card-factory.js +1 -104
  29. package/src/ContentCards/content-cards-provider-factory.js +1 -20
  30. package/src/ContentCards/content-cards-provider.js +1 -391
  31. package/src/ContentCards/content-cards.js +1 -24
  32. package/src/ContentCards/get-cached-content-cards.js +1 -5
  33. package/src/ContentCards/index.js +1 -8
  34. package/src/ContentCards/log-content-cards-displayed.js +1 -5
  35. package/src/ContentCards/request-content-cards-refresh.js +1 -5
  36. package/src/ContentCards/subscribe-to-content-cards-updates.js +1 -17
  37. package/src/ContentCards/types.js +1 -1
  38. package/src/ContentCards/ui/hide-content-cards.js +1 -8
  39. package/src/ContentCards/ui/show-content-cards.js +1 -77
  40. package/src/ContentCards/ui/toggle-content-cards.js +1 -8
  41. package/src/Core/add-sdk-metadata.js +1 -26
  42. package/src/Core/braze-sdk-metadata.js +1 -10
  43. package/src/Core/change-user.js +1 -17
  44. package/src/Core/destroy.js +1 -5
  45. package/src/Core/device-properties.js +1 -9
  46. package/src/Core/disable-sdk.js +1 -15
  47. package/src/Core/enable-sdk.js +1 -11
  48. package/src/Core/get-device-id.js +1 -11
  49. package/src/Core/get-user.js +1 -4
  50. package/src/Core/handle-braze-action.js +1 -87
  51. package/src/Core/index.js +1 -23
  52. package/src/Core/initialize.js +1 -4
  53. package/src/Core/is-disabled.js +1 -4
  54. package/src/Core/log-custom-event.js +1 -38
  55. package/src/Core/log-purchase.js +1 -245
  56. package/src/Core/open-session.js +1 -25
  57. package/src/Core/remove-all-subscriptions.js +1 -4
  58. package/src/Core/remove-subscription.js +1 -4
  59. package/src/Core/request-immediate-data-flush.js +1 -6
  60. package/src/Core/set-logger.js +1 -4
  61. package/src/Core/set-sdk-authentication-signature.js +1 -8
  62. package/src/Core/subscribe-to-sdk-authentication-failures.js +1 -6
  63. package/src/Core/toggle-logging.js +1 -4
  64. package/src/Core/types.js +1 -1
  65. package/src/Core/wipe-data.js +1 -16
  66. package/src/FeatureFlags/feature-flag-factory.js +1 -24
  67. package/src/FeatureFlags/feature-flag.js +1 -64
  68. package/src/FeatureFlags/feature-flags-provider-factory.js +1 -19
  69. package/src/FeatureFlags/feature-flags-provider.js +1 -145
  70. package/src/FeatureFlags/get-all-feature-flags.js +1 -12
  71. package/src/FeatureFlags/get-feature-flag.js +1 -12
  72. package/src/FeatureFlags/index.js +1 -6
  73. package/src/FeatureFlags/log-feature-flag-impression.js +1 -23
  74. package/src/FeatureFlags/refresh-feature-flags.js +1 -9
  75. package/src/FeatureFlags/subscribe-to-feature-flags-updates.js +1 -9
  76. package/src/FeatureFlags/types.js +1 -1
  77. package/src/Feed/feed-provider-factory.js +1 -18
  78. package/src/Feed/feed-provider.js +1 -90
  79. package/src/Feed/feed.js +1 -20
  80. package/src/Feed/get-cached-feed.js +1 -5
  81. package/src/Feed/index.js +1 -8
  82. package/src/Feed/log-feed-displayed.js +1 -7
  83. package/src/Feed/request-feed-refresh.js +1 -5
  84. package/src/Feed/subscribe-to-feed-updates.js +1 -5
  85. package/src/Feed/types.js +1 -1
  86. package/src/Feed/ui/hide-feed.js +1 -7
  87. package/src/Feed/ui/show-feed.js +1 -96
  88. package/src/Feed/ui/toggle-feed.js +1 -8
  89. package/src/InAppMessage/defer-in-app-message.js +1 -13
  90. package/src/InAppMessage/display/get-animation-effect.js +1 -19
  91. package/src/InAppMessage/display/html-message-to-html.js +1 -244
  92. package/src/InAppMessage/display/in-app-message-to-html.js +1 -188
  93. package/src/InAppMessage/display/modal-utils.js +1 -73
  94. package/src/InAppMessage/get-deferred-in-app-message.js +1 -5
  95. package/src/InAppMessage/in-app-message-factory.js +1 -166
  96. package/src/InAppMessage/in-app-message-manager-factory.js +1 -16
  97. package/src/InAppMessage/in-app-message-manager.js +1 -248
  98. package/src/InAppMessage/index.js +1 -16
  99. package/src/InAppMessage/log-in-app-message-button-click.js +1 -27
  100. package/src/InAppMessage/log-in-app-message-click.js +1 -19
  101. package/src/InAppMessage/log-in-app-message-html-click.js +1 -23
  102. package/src/InAppMessage/log-in-app-message-impression.js +1 -13
  103. package/src/InAppMessage/models/control-message.js +1 -11
  104. package/src/InAppMessage/models/full-screen-message.js +1 -105
  105. package/src/InAppMessage/models/html-message.js +1 -68
  106. package/src/InAppMessage/models/in-app-message-button.js +1 -47
  107. package/src/InAppMessage/models/in-app-message.js +1 -382
  108. package/src/InAppMessage/models/modal-message.js +1 -101
  109. package/src/InAppMessage/models/slide-up-message.js +1 -84
  110. package/src/InAppMessage/models/templated-in-app-message.js +1 -19
  111. package/src/InAppMessage/subscribe-to-in-app-message.js +1 -10
  112. package/src/InAppMessage/types.js +1 -1
  113. package/src/InAppMessage/ui/automatically-show-in-app-messages.js +1 -15
  114. package/src/InAppMessage/ui/show-in-app-message.js +1 -181
  115. package/src/InAppMessage/utils/in-app-message-utils.js +1 -1
  116. package/src/Push/index.js +1 -5
  117. package/src/Push/is-push-blocked.js +1 -5
  118. package/src/Push/is-push-permission-granted.js +1 -5
  119. package/src/Push/is-push-supported.js +1 -5
  120. package/src/Push/push-manager-factory.js +1 -30
  121. package/src/Push/push-manager.js +1 -387
  122. package/src/Push/request-push-permission.js +1 -9
  123. package/src/Push/types.js +1 -1
  124. package/src/Push/unregister-push.js +1 -5
  125. package/src/Push/utils/push-utils.js +1 -36
  126. package/src/User/index.js +1 -1
  127. package/src/User/types.js +1 -1
  128. package/src/User/user-manager.js +1 -73
  129. package/src/User/user.js +1 -301
  130. package/src/common/base-feed.js +1 -28
  131. package/src/common/base-provider.js +1 -6
  132. package/src/common/constants.js +1 -14
  133. package/src/common/event-logger.js +1 -31
  134. package/src/common/feed-display.js +1 -210
  135. package/src/common/translations.js +1 -148
  136. package/src/common/types.js +1 -1
  137. package/src/index.js +1 -9
  138. package/src/l10n/l10n-manager-factory.js +1 -22
  139. package/src/l10n/l10n-manager.js +1 -25
  140. package/src/l10n/types.js +1 -1
  141. package/src/managers/auth-manager.js +1 -51
  142. package/src/managers/braze-instance.js +1 -329
  143. package/src/managers/device-manager.js +1 -85
  144. package/src/managers/network-manager.js +1 -253
  145. package/src/managers/server-config-manager.js +1 -108
  146. package/src/managers/session-manager.js +1 -75
  147. package/src/managers/storage-manager-factory.js +1 -42
  148. package/src/managers/storage-manager.js +1 -419
  149. package/src/managers/subscription-manager.js +1 -28
  150. package/src/managers/types.js +1 -1
  151. package/src/models/backend-errors.js +1 -8
  152. package/src/models/braze-event.js +1 -47
  153. package/src/models/braze-sdk-metadata.js +1 -10
  154. package/src/models/device.js +1 -18
  155. package/src/models/identifier.js +1 -19
  156. package/src/models/push-token.js +1 -27
  157. package/src/models/request-result.js +1 -8
  158. package/src/models/server-config.js +1 -48
  159. package/src/models/types.js +1 -1
  160. package/src/request-controller.js +1 -307
  161. package/src/triggers/models/custom-event-data.js +1 -14
  162. package/src/triggers/models/custom-event-property-data.js +1 -21
  163. package/src/triggers/models/filter-set.js +1 -55
  164. package/src/triggers/models/filter.js +1 -102
  165. package/src/triggers/models/in-app-message-click-data.js +1 -25
  166. package/src/triggers/models/purchase-data.js +1 -14
  167. package/src/triggers/models/purchase-property-data.js +1 -21
  168. package/src/triggers/models/push-click-data.js +1 -15
  169. package/src/triggers/models/trigger-condition.js +1 -105
  170. package/src/triggers/models/trigger-events.js +1 -8
  171. package/src/triggers/models/trigger.js +1 -122
  172. package/src/triggers/triggers-provider-factory.js +1 -34
  173. package/src/triggers/triggers-provider.js +1 -326
  174. package/src/triggers/types.js +1 -1
  175. package/src/types.js +1 -1
  176. package/src/ui/js/attach-css.js +1 -13
  177. package/src/ui/js/feed-css.js +1 -12
  178. package/src/ui/js/iam-css.js +1 -12
  179. package/src/ui/js/index.js +1 -3
  180. package/src/ui/js/load-font-awesome.js +1 -13
  181. package/src/util/base-device-parser.js +1 -14
  182. package/src/util/braze-actions.js +1 -84
  183. package/src/util/browser-detector.js +1 -84
  184. package/src/util/client-hints-parser.js +1 -66
  185. package/src/util/code-utils.js +1 -100
  186. package/src/util/color-utils.js +1 -29
  187. package/src/util/component-utils.js +1 -26
  188. package/src/util/date-utils.js +1 -28
  189. package/src/util/deprecation-utils.js +1 -5
  190. package/src/util/device-constants.js +1 -14
  191. package/src/util/dom-utils.js +1 -91
  192. package/src/util/error-utils.js +1 -2
  193. package/src/util/key-codes.js +1 -1
  194. package/src/util/math.js +1 -5
  195. package/src/util/net.js +1 -71
  196. package/src/util/request-header-utils.js +1 -46
  197. package/src/util/string-utils.js +1 -22
  198. package/src/util/types.js +1 -1
  199. package/src/util/url-utils.js +1 -20
  200. package/src/util/user-agent-parser.js +1 -85
  201. package/src/util/validation-utils.js +1 -217
  202. package/src/util/window-utils.js +1 -34
@@ -1,387 +1 @@
1
- import { isArray as p, isEqual as ii } from "../util/code-utils.js";
2
- import ti from "../models/push-token.js";
3
- import r from "../../shared-lib/braze-shared-lib.js";
4
- import { STORAGE_KEYS as i } from "../managers/storage-manager.js";
5
- import { User } from "../User/index.js";
6
- import { WindowUtils as Z } from "../util/window-utils.js";
7
- import jt from "./utils/push-utils.js";
8
- import { getErrorMessage as ei } from "../util/error-utils.js";
9
- export default class ea {
10
- constructor(i, t, e, s, r, n, o, u, h, a) {
11
- (this.hn = i),
12
- (this.cn = t),
13
- (this.fn = e),
14
- (this.dn = r),
15
- (this.bn = n),
16
- (this.wt = o),
17
- (this.yn = u),
18
- (this.gn = h),
19
- (this.u = a),
20
- (this.hn = i),
21
- (this.cn = t),
22
- (this.fn = e),
23
- (this.wn = s + "/safari/" + t),
24
- (this.dn = r || "/service-worker.js"),
25
- (this.bn = n),
26
- (this.wt = o),
27
- (this.yn = u || !1),
28
- (this.gn = h || !1),
29
- (this.u = a),
30
- (this.vn = jt.kn()),
31
- (this.Pn = jt.Dn());
32
- }
33
- Sn() {
34
- return this.gn;
35
- }
36
- An(i, t, e, s, n) {
37
- i.unsubscribe()
38
- .then((i) => {
39
- i
40
- ? this.jn(t, e, s, n)
41
- : (r.j.error("Failed to unsubscribe device from push."),
42
- "function" == typeof n && n(!1));
43
- })
44
- .catch((i) => {
45
- r.j.error("Push unsubscription error: " + i),
46
- "function" == typeof n && n(!1);
47
- });
48
- }
49
- Un(i, t, e) {
50
- const s = ((i) => {
51
- if ("string" == typeof i) return i;
52
- if (0 !== i.endpoint.indexOf("https://android.googleapis.com/gcm/send"))
53
- return i.endpoint;
54
- let t = i.endpoint;
55
- const e = i;
56
- return (
57
- e.Wn &&
58
- -1 === i.endpoint.indexOf(e.Wn) &&
59
- (t = i.endpoint + "/" + e.Wn),
60
- t
61
- );
62
- })(i);
63
- let r = null,
64
- n = null;
65
- const o = i;
66
- if (null != o.getKey)
67
- try {
68
- const i = Array.from(new Uint8Array(o.getKey("p256dh"))),
69
- t = Array.from(new Uint8Array(o.getKey("auth")));
70
- (r = btoa(String.fromCharCode.apply(null, i))),
71
- (n = btoa(String.fromCharCode.apply(null, t)));
72
- } catch (i) {
73
- if ("invalid arguments" !== ei(i)) throw i;
74
- }
75
- const u = ((i) => {
76
- let t;
77
- return i.options &&
78
- (t = i.options.applicationServerKey) &&
79
- t.byteLength &&
80
- t.byteLength > 0
81
- ? btoa(String.fromCharCode.apply(null, Array.from(new Uint8Array(t))))
82
- .replace(/\+/g, "-")
83
- .replace(/\//g, "_")
84
- : null;
85
- })(o);
86
- this.hn && this.hn._n(s, t, r, n, u),
87
- s && "function" == typeof e && e(s, r, n);
88
- }
89
- xn() {
90
- this.hn && this.hn.Nn(!0);
91
- }
92
- Tn(i, t) {
93
- this.hn && this.hn.Nn(!1), r.j.info(i), "function" == typeof t && t(!1);
94
- }
95
- zn(i, t, e, s) {
96
- var n;
97
- if ("default" === t.permission)
98
- try {
99
- window.safari.pushNotification.requestPermission(
100
- this.wn,
101
- i,
102
- {
103
- api_key: this.cn,
104
- device_id:
105
- (null === (n = this.fn) || void 0 === n ? void 0 : n.ce().id) ||
106
- "",
107
- },
108
- (t) => {
109
- "granted" === t.permission &&
110
- this.hn &&
111
- this.hn.setPushNotificationSubscriptionType(
112
- User.NotificationSubscriptionTypes.OPTED_IN,
113
- ),
114
- this.zn(i, t, e, s);
115
- },
116
- );
117
- } catch (i) {
118
- this.Tn("Could not request permission for push: " + i, s);
119
- }
120
- else
121
- "denied" === t.permission
122
- ? this.Tn(
123
- "The user has blocked notifications from this site, or Safari push is not configured in the Braze dashboard.",
124
- s,
125
- )
126
- : "granted" === t.permission &&
127
- (r.j.info("Device successfully subscribed to push."),
128
- this.Un(t.deviceToken, new Date(), e));
129
- }
130
- requestPermission(i, t, e) {
131
- const s = (s) => {
132
- switch (s) {
133
- case "granted":
134
- return void ("function" == typeof i && i());
135
- case "default":
136
- return void ("function" == typeof t && t());
137
- case "denied":
138
- return void ("function" == typeof e && e());
139
- default:
140
- r.j.error("Received unexpected permission result " + s);
141
- }
142
- };
143
- let n = !1;
144
- const o = window.Notification.requestPermission((i) => {
145
- n && s(i);
146
- });
147
- o
148
- ? o.then((i) => {
149
- s(i);
150
- })
151
- : (n = !0);
152
- }
153
- jn(i, t, e, s) {
154
- const n = { userVisibleOnly: !0 };
155
- null != t && (n.applicationServerKey = t),
156
- i.pushManager
157
- .subscribe(n)
158
- .then((i) => {
159
- r.j.info("Device successfully subscribed to push."),
160
- this.Un(i, new Date(), e);
161
- })
162
- .catch((i) => {
163
- jt.isPushBlocked()
164
- ? (r.j.info("Permission for push notifications was denied."),
165
- "function" == typeof s && s(!1))
166
- : (r.j.error("Push subscription failed: " + i),
167
- "function" == typeof s && s(!0));
168
- });
169
- }
170
- In() {
171
- return this.yn
172
- ? navigator.serviceWorker.getRegistration(this.dn)
173
- : navigator.serviceWorker.register(this.dn).then(() =>
174
- navigator.serviceWorker.ready.then(
175
- (i) => (
176
- i &&
177
- "function" == typeof i.update &&
178
- i.update().catch((i) => {
179
- r.j.info("ServiceWorker update failed: " + i);
180
- }),
181
- i
182
- ),
183
- ),
184
- );
185
- }
186
- Vn(i) {
187
- this.yn ||
188
- (i.unregister(), r.j.info("Service worker successfully unregistered."));
189
- }
190
- subscribe(t, e) {
191
- if (!jt.isPushSupported())
192
- return r.j.info(ea.qn), void ("function" == typeof e && e(!1));
193
- if (this.vn) {
194
- if (!this.yn && null != window.location) {
195
- let i = this.dn;
196
- -1 === i.indexOf(window.location.host) &&
197
- (i = window.location.host + i),
198
- -1 === i.indexOf(window.location.protocol) &&
199
- (i = window.location.protocol + "//" + i);
200
- const t = i.substr(0, i.lastIndexOf("/") + 1);
201
- if (0 !== Z.Cn().indexOf(t))
202
- return (
203
- r.j.error(
204
- "Cannot subscribe to push from a path higher than the service worker location (tried to subscribe from " +
205
- window.location.pathname +
206
- " but service worker is at " +
207
- i +
208
- ")",
209
- ),
210
- void ("function" == typeof e && e(!0))
211
- );
212
- }
213
- if (jt.isPushBlocked())
214
- return void this.Tn(
215
- "Notifications from this site are blocked. This may be a temporary embargo or a permanent denial.",
216
- e,
217
- );
218
- if (this.wt && !this.wt.En() && 0 === this.wt.li())
219
- return (
220
- r.j.info(
221
- "Waiting for VAPID key from server config before subscribing to push.",
222
- ),
223
- void this.wt.Rn(() => {
224
- this.subscribe(t, e);
225
- })
226
- );
227
- const s = () => {
228
- r.j.info("Permission for push notifications was denied."),
229
- "function" == typeof e && e(!1);
230
- },
231
- n = () => {
232
- let i = "Permission for push notifications was ignored.";
233
- jt.isPushBlocked() &&
234
- (i +=
235
- " The browser has automatically blocked further permission requests for a period (probably 1 week)."),
236
- r.j.info(i),
237
- "function" == typeof e && e(!0);
238
- },
239
- o = jt.isPushPermissionGranted(),
240
- u = () => {
241
- !o &&
242
- this.hn &&
243
- this.hn.setPushNotificationSubscriptionType(
244
- User.NotificationSubscriptionTypes.OPTED_IN,
245
- ),
246
- this.In()
247
- .then((s) => {
248
- if (null == s)
249
- return (
250
- r.j.error(
251
- "No service worker registration. Set the `manageServiceWorkerExternally` initialization option to false or ensure that your service worker is registered before calling registerPush.",
252
- ),
253
- void ("function" == typeof e && e(!0))
254
- );
255
- s.pushManager
256
- .getSubscription()
257
- .then((n) => {
258
- let o = null;
259
- if (
260
- (this.wt &&
261
- null != this.wt.En() &&
262
- (o = r.On.Fn(this.wt.En())),
263
- n)
264
- ) {
265
- let u,
266
- h = null,
267
- a = null;
268
- if ((this.u && (u = this.u.v(i.k.Bn)), u && !p(u))) {
269
- let i;
270
- try {
271
- i = ti.Yn(u).Mn;
272
- } catch (t) {
273
- i = null;
274
- }
275
- null == i ||
276
- isNaN(i.getTime()) ||
277
- 0 === i.getTime() ||
278
- ((h = i),
279
- (a = new Date(h)),
280
- a.setMonth(h.getMonth() + 6));
281
- }
282
- null != o &&
283
- n.options &&
284
- n.options.applicationServerKey &&
285
- n.options.applicationServerKey.byteLength &&
286
- n.options.applicationServerKey.byteLength > 0 &&
287
- !ii(o, new Uint8Array(n.options.applicationServerKey))
288
- ? (n.options.applicationServerKey.byteLength > 12
289
- ? r.j.info(
290
- "Device was already subscribed to push using a different VAPID provider, creating new subscription.",
291
- )
292
- : r.j.info(
293
- "Attempting to upgrade a gcm_sender_id-based push registration to VAPID - depending on the browser this may or may not result in the same gcm_sender_id-based subscription.",
294
- ),
295
- this.An(n, s, o, t, e))
296
- : n.expirationTime &&
297
- new Date(n.expirationTime).valueOf() <=
298
- new Date().valueOf()
299
- ? (r.j.info(
300
- "Push subscription is expired, creating new subscription.",
301
- ),
302
- this.An(n, s, o, t, e))
303
- : u && p(u)
304
- ? this.An(n, s, o, t, e)
305
- : null == a
306
- ? (r.j.info(
307
- "No push subscription creation date found, creating new subscription.",
308
- ),
309
- this.An(n, s, o, t, e))
310
- : a.valueOf() <= new Date().valueOf()
311
- ? (r.j.info(
312
- "Push subscription older than 6 months, creating new subscription.",
313
- ),
314
- this.An(n, s, o, t, e))
315
- : (r.j.info(
316
- "Device already subscribed to push, sending existing subscription to backend.",
317
- ),
318
- this.Un(n, h, t));
319
- } else this.jn(s, o, t, e);
320
- })
321
- .catch((i) => {
322
- r.j.error(
323
- "Error checking current push subscriptions: " + i,
324
- );
325
- });
326
- })
327
- .catch((i) => {
328
- r.j.error("ServiceWorker registration failed: " + i);
329
- });
330
- };
331
- this.requestPermission(u, n, s);
332
- } else if (this.Pn) {
333
- if (null == this.bn || "" === this.bn)
334
- return (
335
- r.j.error(
336
- "You must supply the safariWebsitePushId initialization option in order to use registerPush on Safari",
337
- ),
338
- void ("function" == typeof e && e(!0))
339
- );
340
- const i = window.safari.pushNotification.permission(this.bn);
341
- this.zn(this.bn, i, t, e);
342
- }
343
- }
344
- unsubscribe(i, t) {
345
- if (!jt.isPushSupported())
346
- return r.j.info(ea.qn), void ("function" == typeof t && t());
347
- this.vn
348
- ? navigator.serviceWorker.getRegistration().then((e) => {
349
- e
350
- ? e.pushManager
351
- .getSubscription()
352
- .then((s) => {
353
- s &&
354
- (this.xn(),
355
- s
356
- .unsubscribe()
357
- .then((s) => {
358
- s
359
- ? (r.j.info(
360
- "Device successfully unsubscribed from push.",
361
- ),
362
- "function" == typeof i && i())
363
- : (r.j.error(
364
- "Failed to unsubscribe device from push.",
365
- ),
366
- "function" == typeof t && t()),
367
- this.Vn(e);
368
- })
369
- .catch((i) => {
370
- r.j.error("Push unsubscription error: " + i),
371
- "function" == typeof t && t();
372
- }));
373
- })
374
- .catch((i) => {
375
- r.j.error("Error unsubscribing from push: " + i),
376
- "function" == typeof t && t();
377
- })
378
- : (r.j.info("Device already unsubscribed from push."),
379
- "function" == typeof i && i());
380
- })
381
- : this.Pn &&
382
- (this.xn(),
383
- r.j.info("Device unsubscribed from push."),
384
- "function" == typeof i && i());
385
- }
386
- }
387
- ea.qn = "Push notifications are not supported in this browser.";
1
+ import{isArray as p,isEqual as ii}from"../util/code-utils.js";import ti from"../models/push-token.js";import r from"../../shared-lib/braze-shared-lib.js";import{STORAGE_KEYS as i}from"../managers/storage-manager.js";import{User}from"../User/index.js";import{WindowUtils as Z}from"../util/window-utils.js";import jt from"./utils/push-utils.js";import{getErrorMessage as ei}from"../util/error-utils.js";export default class ea{constructor(i,t,e,s,r,n,o,u,h,a){this.hn=i,this.cn=t,this.fn=e,this.dn=r,this.bn=n,this.wt=o,this.yn=u,this.gn=h,this.u=a,this.hn=i,this.cn=t,this.fn=e,this.wn=s+"/safari/"+t,this.dn=r||"/service-worker.js",this.bn=n,this.wt=o,this.yn=u||!1,this.gn=h||!1,this.u=a,this.vn=jt.kn(),this.Pn=jt.Dn()}Sn(){return this.gn}An(i,t,e,s,n){i.unsubscribe().then((i=>{i?this.jn(t,e,s,n):(r.j.error("Failed to unsubscribe device from push."),"function"==typeof n&&n(!1))})).catch((i=>{r.j.error("Push unsubscription error: "+i),"function"==typeof n&&n(!1)}))}Un(i,t,e){const s=(i=>{if("string"==typeof i)return i;if(0!==i.endpoint.indexOf("https://android.googleapis.com/gcm/send"))return i.endpoint;let t=i.endpoint;const e=i;return e.Wn&&-1===i.endpoint.indexOf(e.Wn)&&(t=i.endpoint+"/"+e.Wn),t})(i);let r=null,n=null;const o=i;if(null!=o.getKey)try{const i=Array.from(new Uint8Array(o.getKey("p256dh"))),t=Array.from(new Uint8Array(o.getKey("auth")));r=btoa(String.fromCharCode.apply(null,i)),n=btoa(String.fromCharCode.apply(null,t))}catch(i){if("invalid arguments"!==ei(i))throw i}const u=(i=>{let t;return i.options&&(t=i.options.applicationServerKey)&&t.byteLength&&t.byteLength>0?btoa(String.fromCharCode.apply(null,Array.from(new Uint8Array(t)))).replace(/\+/g,"-").replace(/\//g,"_"):null})(o);this.hn&&this.hn._n(s,t,r,n,u),s&&"function"==typeof e&&e(s,r,n)}xn(){this.hn&&this.hn.Nn(!0)}Tn(i,t){this.hn&&this.hn.Nn(!1),r.j.info(i),"function"==typeof t&&t(!1)}zn(i,t,e,s){var n;if("default"===t.permission)try{window.safari.pushNotification.requestPermission(this.wn,i,{api_key:this.cn,device_id:(null===(n=this.fn)||void 0===n?void 0:n.ce().id)||""},(t=>{"granted"===t.permission&&this.hn&&this.hn.setPushNotificationSubscriptionType(User.NotificationSubscriptionTypes.OPTED_IN),this.zn(i,t,e,s)}))}catch(i){this.Tn("Could not request permission for push: "+i,s)}else"denied"===t.permission?this.Tn("The user has blocked notifications from this site, or Safari push is not configured in the Braze dashboard.",s):"granted"===t.permission&&(r.j.info("Device successfully subscribed to push."),this.Un(t.deviceToken,new Date,e))}requestPermission(i,t,e){const s=s=>{switch(s){case"granted":return void("function"==typeof i&&i());case"default":return void("function"==typeof t&&t());case"denied":return void("function"==typeof e&&e());default:r.j.error("Received unexpected permission result "+s)}};let n=!1;const o=window.Notification.requestPermission((i=>{n&&s(i)}));o?o.then((i=>{s(i)})):n=!0}jn(i,t,e,s){const n={userVisibleOnly:!0};null!=t&&(n.applicationServerKey=t),i.pushManager.subscribe(n).then((i=>{r.j.info("Device successfully subscribed to push."),this.Un(i,new Date,e)})).catch((i=>{jt.isPushBlocked()?(r.j.info("Permission for push notifications was denied."),"function"==typeof s&&s(!1)):(r.j.error("Push subscription failed: "+i),"function"==typeof s&&s(!0))}))}In(){return this.yn?navigator.serviceWorker.getRegistration(this.dn):navigator.serviceWorker.register(this.dn).then((()=>navigator.serviceWorker.ready.then((i=>(i&&"function"==typeof i.update&&i.update().catch((i=>{r.j.info("ServiceWorker update failed: "+i)})),i)))))}Vn(i){this.yn||(i.unregister(),r.j.info("Service worker successfully unregistered."))}subscribe(t,e){if(!jt.isPushSupported())return r.j.info(ea.qn),void("function"==typeof e&&e(!1));if(this.vn){if(!this.yn&&null!=window.location){let i=this.dn;-1===i.indexOf(window.location.host)&&(i=window.location.host+i),-1===i.indexOf(window.location.protocol)&&(i=window.location.protocol+"//"+i);const t=i.substr(0,i.lastIndexOf("/")+1);if(0!==Z.Cn().indexOf(t))return r.j.error("Cannot subscribe to push from a path higher than the service worker location (tried to subscribe from "+window.location.pathname+" but service worker is at "+i+")"),void("function"==typeof e&&e(!0))}if(jt.isPushBlocked())return void this.Tn("Notifications from this site are blocked. This may be a temporary embargo or a permanent denial.",e);if(this.wt&&!this.wt.En()&&0===this.wt.li())return r.j.info("Waiting for VAPID key from server config before subscribing to push."),void this.wt.Rn((()=>{this.subscribe(t,e)}));const s=()=>{r.j.info("Permission for push notifications was denied."),"function"==typeof e&&e(!1)},n=()=>{let i="Permission for push notifications was ignored.";jt.isPushBlocked()&&(i+=" The browser has automatically blocked further permission requests for a period (probably 1 week)."),r.j.info(i),"function"==typeof e&&e(!0)},o=jt.isPushPermissionGranted(),u=()=>{!o&&this.hn&&this.hn.setPushNotificationSubscriptionType(User.NotificationSubscriptionTypes.OPTED_IN),this.In().then((s=>{if(null==s)return r.j.error("No service worker registration. Set the `manageServiceWorkerExternally` initialization option to false or ensure that your service worker is registered before calling registerPush."),void("function"==typeof e&&e(!0));s.pushManager.getSubscription().then((n=>{let o=null;if(this.wt&&null!=this.wt.En()&&(o=r.On.Fn(this.wt.En())),n){let u,h=null,a=null;if(this.u&&(u=this.u.v(i.k.Bn)),u&&!p(u)){let i;try{i=ti.Yn(u).Mn}catch(t){i=null}null==i||isNaN(i.getTime())||0===i.getTime()||(h=i,a=new Date(h),a.setMonth(h.getMonth()+6))}null!=o&&n.options&&n.options.applicationServerKey&&n.options.applicationServerKey.byteLength&&n.options.applicationServerKey.byteLength>0&&!ii(o,new Uint8Array(n.options.applicationServerKey))?(n.options.applicationServerKey.byteLength>12?r.j.info("Device was already subscribed to push using a different VAPID provider, creating new subscription."):r.j.info("Attempting to upgrade a gcm_sender_id-based push registration to VAPID - depending on the browser this may or may not result in the same gcm_sender_id-based subscription."),this.An(n,s,o,t,e)):n.expirationTime&&new Date(n.expirationTime).valueOf()<=(new Date).valueOf()?(r.j.info("Push subscription is expired, creating new subscription."),this.An(n,s,o,t,e)):u&&p(u)?this.An(n,s,o,t,e):null==a?(r.j.info("No push subscription creation date found, creating new subscription."),this.An(n,s,o,t,e)):a.valueOf()<=(new Date).valueOf()?(r.j.info("Push subscription older than 6 months, creating new subscription."),this.An(n,s,o,t,e)):(r.j.info("Device already subscribed to push, sending existing subscription to backend."),this.Un(n,h,t))}else this.jn(s,o,t,e)})).catch((i=>{r.j.error("Error checking current push subscriptions: "+i)}))})).catch((i=>{r.j.error("ServiceWorker registration failed: "+i)}))};this.requestPermission(u,n,s)}else if(this.Pn){if(null==this.bn||""===this.bn)return r.j.error("You must supply the safariWebsitePushId initialization option in order to use registerPush on Safari"),void("function"==typeof e&&e(!0));const i=window.safari.pushNotification.permission(this.bn);this.zn(this.bn,i,t,e)}}unsubscribe(i,t){if(!jt.isPushSupported())return r.j.info(ea.qn),void("function"==typeof t&&t());this.vn?navigator.serviceWorker.getRegistration().then((e=>{e?e.pushManager.getSubscription().then((s=>{s&&(this.xn(),s.unsubscribe().then((s=>{s?(r.j.info("Device successfully unsubscribed from push."),"function"==typeof i&&i()):(r.j.error("Failed to unsubscribe device from push."),"function"==typeof t&&t()),this.Vn(e)})).catch((i=>{r.j.error("Push unsubscription error: "+i),"function"==typeof t&&t()})))})).catch((i=>{r.j.error("Error unsubscribing from push: "+i),"function"==typeof t&&t()})):(r.j.info("Device already unsubscribed from push."),"function"==typeof i&&i())})):this.Pn&&(this.xn(),r.j.info("Device unsubscribed from push."),"function"==typeof i&&i())}}ea.qn="Push notifications are not supported in this browser.";
@@ -1,9 +1 @@
1
- import e from "../managers/braze-instance.js";
2
- import na from "./push-manager-factory.js";
3
- export function requestPushPermission(r, n) {
4
- if (e.rr())
5
- return na.m().subscribe((n, o, t) => {
6
- const s = e.cr();
7
- s && s.requestImmediateDataFlush(), "function" == typeof r && r(n, o, t);
8
- }, n);
9
- }
1
+ import e from"../managers/braze-instance.js";import na from"./push-manager-factory.js";export function requestPushPermission(r,n){if(e.rr())return na.m().subscribe(((n,o,t)=>{const s=e.cr();s&&s.requestImmediateDataFlush(),"function"==typeof r&&r(n,o,t)}),n)}
package/src/Push/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1,5 +1 @@
1
- import e from "../managers/braze-instance.js";
2
- import na from "./push-manager-factory.js";
3
- export function unregisterPush(r, n) {
4
- if (e.rr()) return na.m().unsubscribe(r, n);
5
- }
1
+ import e from"../managers/braze-instance.js";import na from"./push-manager-factory.js";export function unregisterPush(r,n){if(e.rr())return na.m().unsubscribe(r,n)}
@@ -1,36 +1 @@
1
- const jt = {
2
- kn: () =>
3
- "serviceWorker" in navigator &&
4
- "undefined" != typeof ServiceWorkerRegistration &&
5
- "showNotification" in ServiceWorkerRegistration.prototype &&
6
- "PushManager" in window,
7
- Dn: () =>
8
- "safari" in window &&
9
- "pushNotification" in window.safari &&
10
- "function" == typeof window.safari.pushNotification.permission &&
11
- "function" == typeof window.safari.pushNotification.requestPermission,
12
- isPushSupported: () => jt.kn() || jt.Dn(),
13
- isPushBlocked: () => {
14
- const i =
15
- jt.isPushSupported() &&
16
- "Notification" in window &&
17
- null != window.Notification &&
18
- null != window.Notification.permission &&
19
- "denied" === window.Notification.permission,
20
- n =
21
- jt.isPushSupported() &&
22
- (!("Notification" in window) || null == window.Notification);
23
- return i || n;
24
- },
25
- isPushPermissionGranted: () =>
26
- jt.isPushSupported() &&
27
- "Notification" in window &&
28
- null != window.Notification &&
29
- null != window.Notification.permission &&
30
- "granted" === window.Notification.permission,
31
- Gr: () =>
32
- !jt.isPushBlocked() &&
33
- jt.isPushSupported() &&
34
- !jt.isPushPermissionGranted(),
35
- };
36
- export default jt;
1
+ const jt={kn:()=>"serviceWorker"in navigator&&"undefined"!=typeof ServiceWorkerRegistration&&"showNotification"in ServiceWorkerRegistration.prototype&&"PushManager"in window,Dn:()=>"safari"in window&&"pushNotification"in window.safari&&"function"==typeof window.safari.pushNotification.permission&&"function"==typeof window.safari.pushNotification.requestPermission,isPushSupported:()=>jt.kn()||jt.Dn(),isPushBlocked:()=>{const i=jt.isPushSupported()&&"Notification"in window&&null!=window.Notification&&null!=window.Notification.permission&&"denied"===window.Notification.permission,n=jt.isPushSupported()&&(!("Notification"in window)||null==window.Notification);return i||n},isPushPermissionGranted:()=>jt.isPushSupported()&&"Notification"in window&&null!=window.Notification&&null!=window.Notification.permission&&"granted"===window.Notification.permission,Gr:()=>!jt.isPushBlocked()&&jt.isPushSupported()&&!jt.isPushPermissionGranted()};export default jt;
package/src/User/index.js CHANGED
@@ -1 +1 @@
1
- export { default as User } from "./user.js";
1
+ export{default as User}from"./user.js";
package/src/User/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1,73 +1 @@
1
- import _t from "../models/identifier.js";
2
- import { getByteLength as U } from "../util/string-utils.js";
3
- import ti from "../models/push-token.js";
4
- import r from "../../shared-lib/braze-shared-lib.js";
5
- import { STORAGE_KEYS as i } from "../managers/storage-manager.js";
6
- import { User } from "../User/index.js";
7
- export default class bt {
8
- constructor(t, s) {
9
- (this.wt = t), (this.u = s), (this.wt = t), (this.u = s);
10
- }
11
- getUserId() {
12
- const t = this.u.tu(i.eu.su);
13
- if (null == t) return null;
14
- let s = t.iu,
15
- e = U(s);
16
- if (e > User.lr) {
17
- for (; e > User.lr; ) (s = s.slice(0, s.length - 1)), (e = U(s));
18
- (t.iu = s), this.u.uu(i.eu.su, t);
19
- }
20
- return s;
21
- }
22
- ru(t) {
23
- const s = null == this.getUserId();
24
- this.u.uu(i.eu.su, new _t(t)), s && this.u.ou(t);
25
- }
26
- setCustomUserAttribute(t, s) {
27
- if (this.wt.hu(t))
28
- return (
29
- r.j.info('Custom Attribute "' + t + '" is blocklisted, ignoring.'), !1
30
- );
31
- const e = {};
32
- return (e[t] = s), this.nu(User.lu, e, !0);
33
- }
34
- nu(t, s, e = !1, i = !1) {
35
- const u = this.u.mu(this.getUserId(), t, s);
36
- let o = "",
37
- h = t,
38
- n = s;
39
- return (
40
- e &&
41
- ((o = " custom"),
42
- "object" == typeof s &&
43
- ((h = Object.keys(s)[0]),
44
- (n = s[h]),
45
- "object" == typeof n && (n = JSON.stringify(n, null, 2)))),
46
- !i && u && r.j.info(`Logged${o} attribute ${h} with value ${n}`),
47
- u
48
- );
49
- }
50
- _n(t, s, e, u, o) {
51
- this.nu("push_token", t, !1, !0),
52
- this.nu("custom_push_public_key", e, !1, !0),
53
- this.nu("custom_push_user_auth", u, !1, !0),
54
- this.nu("custom_push_vapid_public_key", o, !1, !0);
55
- const h = r.zt.Ft,
56
- n = new r.xt(h, r.j),
57
- l = new ti(t, s, e, u, o);
58
- this.u.D(i.k.Bn, l.ss()), n.setItem(h.Jt.cu, h.ie, !0);
59
- }
60
- Nn(t) {
61
- if (
62
- (this.nu("push_token", null, !1, !0),
63
- this.nu("custom_push_public_key", null, !1, !0),
64
- this.nu("custom_push_user_auth", null, !1, !0),
65
- this.nu("custom_push_vapid_public_key", null, !1, !0),
66
- t)
67
- ) {
68
- const t = r.zt.Ft,
69
- s = new r.xt(t, r.j);
70
- this.u.D(i.k.Bn, !1), s.setItem(t.Jt.cu, t.ie, !1);
71
- }
72
- }
73
- }
1
+ import _t from"../models/identifier.js";import{getByteLength as U}from"../util/string-utils.js";import ti from"../models/push-token.js";import r from"../../shared-lib/braze-shared-lib.js";import{STORAGE_KEYS as i}from"../managers/storage-manager.js";import{User}from"../User/index.js";export default class bt{constructor(t,s){this.wt=t,this.u=s,this.wt=t,this.u=s}getUserId(){const t=this.u.tu(i.eu.su);if(null==t)return null;let s=t.iu,e=U(s);if(e>User.lr){for(;e>User.lr;)s=s.slice(0,s.length-1),e=U(s);t.iu=s,this.u.uu(i.eu.su,t)}return s}ru(t){const s=null==this.getUserId();this.u.uu(i.eu.su,new _t(t)),s&&this.u.ou(t)}setCustomUserAttribute(t,s){if(this.wt.hu(t))return r.j.info('Custom Attribute "'+t+'" is blocklisted, ignoring.'),!1;const e={};return e[t]=s,this.nu(User.lu,e,!0)}nu(t,s,e=!1,i=!1){const u=this.u.mu(this.getUserId(),t,s);let o="",h=t,n=s;return e&&(o=" custom","object"==typeof s&&(h=Object.keys(s)[0],n=s[h],"object"==typeof n&&(n=JSON.stringify(n,null,2)))),!i&&u&&r.j.info(`Logged${o} attribute ${h} with value ${n}`),u}_n(t,s,e,u,o){this.nu("push_token",t,!1,!0),this.nu("custom_push_public_key",e,!1,!0),this.nu("custom_push_user_auth",u,!1,!0),this.nu("custom_push_vapid_public_key",o,!1,!0);const h=r.zt.Ft,n=new r.xt(h,r.j),l=new ti(t,s,e,u,o);this.u.D(i.k.Bn,l.ss()),n.setItem(h.Jt.cu,h.ie,!0)}Nn(t){if(this.nu("push_token",null,!1,!0),this.nu("custom_push_public_key",null,!1,!0),this.nu("custom_push_user_auth",null,!1,!0),this.nu("custom_push_vapid_public_key",null,!1,!0),t){const t=r.zt.Ft,s=new r.xt(t,r.j);this.u.D(i.k.Bn,!1),s.setItem(t.Jt.cu,t.ie,!1)}}}