web-push-notifications 3.44.6 → 3.44.8

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 (224) hide show
  1. package/core/Pushwoosh.d.ts +199 -0
  2. package/core/Pushwoosh.types.d.ts +213 -0
  3. package/core/Safari.types.d.ts +17 -0
  4. package/core/constants.d.ts +41 -0
  5. package/core/events.types.d.ts +55 -0
  6. package/core/functions.d.ts +5 -0
  7. package/core/legacyEventsMap.d.ts +5 -0
  8. package/core/logger.d.ts +14 -0
  9. package/core/modules/EventBus/EventBus.d.ts +10 -0
  10. package/core/storage.d.ts +27 -0
  11. package/helpers/logger.d.ts +11 -0
  12. package/helpers/pwlogger/Logger.constants.d.ts +21 -0
  13. package/helpers/pwlogger/Logger.d.ts +37 -0
  14. package/helpers/pwlogger/Logger.types.d.ts +44 -0
  15. package/helpers/pwlogger/handlers/handler-console/handler-console.d.ts +2 -0
  16. package/{src/helpers/unescape.ts → helpers/unescape.d.ts} +1 -18
  17. package/models/InboxMessages.d.ts +82 -0
  18. package/models/InboxMessages.types.d.ts +83 -0
  19. package/models/NotificationPayload.d.ts +39 -0
  20. package/models/NotificationPayload.types.d.ts +63 -0
  21. package/modules/Api/Api.d.ts +45 -0
  22. package/modules/Api/Api.types.d.ts +7 -0
  23. package/modules/ApiClient/ApiClient.d.ts +29 -0
  24. package/modules/ApiClient/ApiClient.types.d.ts +188 -0
  25. package/modules/Data/Data.d.ts +76 -0
  26. package/modules/DateModule.d.ts +27 -0
  27. package/modules/InboxMessagesPublic.d.ts +64 -0
  28. package/modules/PlatformChecker/PlatformChecker.d.ts +45 -0
  29. package/modules/PlatformChecker/PlatformChecker.types.d.ts +5 -0
  30. package/modules/storage/Storage.d.ts +63 -0
  31. package/{src/modules/storage/Storage.types.ts → modules/storage/Storage.types.d.ts} +7 -25
  32. package/modules/storage/Store.d.ts +22 -0
  33. package/modules/storage/migrations/26-11-2018.d.ts +2 -0
  34. package/modules/storage/migrations/MigrationExecutor.d.ts +9 -0
  35. package/modules/storage/migrations/Migrations.d.ts +15 -0
  36. package/modules/storage/migrations/constants.d.ts +6 -0
  37. package/modules/storage/migrations/helpers.d.ts +7 -0
  38. package/modules/storage/migrations/initial.d.ts +2 -0
  39. package/modules/storage/version.d.ts +2 -0
  40. package/npm.js +2 -0
  41. package/npm.js.map +1 -0
  42. package/package.json +1 -1
  43. package/service-worker.d.ts +1 -0
  44. package/service-worker.js +2 -0
  45. package/service-worker.js.map +1 -0
  46. package/services/PushService/PushService.types.d.ts +64 -0
  47. package/services/PushService/drivers/PushServiceDefault/PushServiceDefault.d.ts +29 -0
  48. package/{src/services/PushService/drivers/PushServiceDefault/PushServiceDefault.types.ts → services/PushService/drivers/PushServiceDefault/PushServiceDefault.types.d.ts} +1 -1
  49. package/services/PushService/drivers/PushServiceSafari/PushServiceSafari.d.ts +21 -0
  50. package/services/PushService/drivers/PushServiceSafari/PushServiceSafari.types.d.ts +4 -0
  51. package/widget-inbox.js +2 -0
  52. package/widget-inbox.js.map +1 -0
  53. package/widget-subscribe-popup.js +2 -0
  54. package/widget-subscribe-popup.js.map +1 -0
  55. package/widget-subscription-button.js +2 -0
  56. package/widget-subscription-button.js.map +1 -0
  57. package/widget-subscription-prompt.d.ts +2 -0
  58. package/widget-subscription-prompt.js +2 -0
  59. package/widget-subscription-prompt.js.map +1 -0
  60. package/widgets/Inbox/InboxWidget.d.ts +49 -0
  61. package/widgets/Inbox/constants.d.ts +7 -0
  62. package/widgets/Inbox/helpers.d.ts +4 -0
  63. package/widgets/Inbox/inbox_widget.types.d.ts +34 -0
  64. package/widgets/Inbox/widgetTemplates.d.ts +4 -0
  65. package/widgets/SubscribePopup/SubscribePopup.d.ts +20 -0
  66. package/widgets/SubscribePopup/constants.d.ts +6 -0
  67. package/widgets/SubscribePopup/helpers.d.ts +1 -0
  68. package/widgets/SubscribePopup/popupTemplates.d.ts +9 -0
  69. package/widgets/SubscribePopup/types/subscribe-popup.d.ts +43 -0
  70. package/widgets/SubscriptionButton/bell.d.ts +2 -0
  71. package/widgets/SubscriptionButton/constants.d.ts +7 -0
  72. package/widgets/SubscriptionButton/index.d.ts +90 -0
  73. package/widgets/SubscriptionButton/positioning.d.ts +11 -0
  74. package/widgets/SubscriptionButton/subscribe_widget.types.d.ts +35 -0
  75. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.constants.d.ts +1 -0
  76. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.d.ts +14 -0
  77. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.helpers.d.ts +3 -0
  78. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.types.d.ts +19 -0
  79. package/widgets/SubscriptionPrompt/constants.d.ts +21 -0
  80. package/widgets/SubscriptionPrompt/helpers.d.ts +5 -0
  81. package/worker/global.d.ts +16 -0
  82. package/worker/notification.d.ts +9 -0
  83. package/worker/worker.types.d.ts +4 -0
  84. package/.editorconfig +0 -11
  85. package/.gitlab-ci.yml +0 -193
  86. package/babel.config.js +0 -7
  87. package/ci/cdn/Dockerfile +0 -12
  88. package/ci/dev/Dockerfile +0 -30
  89. package/ci/dev/rootfs/entrypoint.sh +0 -18
  90. package/ci/dev/rootfs/entrypoint.sh.d/nginx.sh +0 -6
  91. package/ci/dev/rootfs/entrypoint.sh.d/supervisor.sh +0 -5
  92. package/ci/dev/rootfs/etc/nginx/_real_ip.conf +0 -2
  93. package/ci/dev/rootfs/etc/nginx/conf.d/default.conf +0 -20
  94. package/ci/dev/rootfs/etc/supervisor.d/nginx.ini +0 -11
  95. package/ci/github/Dockerfile +0 -59
  96. package/ci/github/release-zip.js +0 -61
  97. package/ci/npm/Dockerfile +0 -22
  98. package/config/config.js +0 -24
  99. package/config/configBuilder.js +0 -126
  100. package/config/helpers.js +0 -9
  101. package/config/index.js +0 -1
  102. package/develop/README.md +0 -42
  103. package/develop/favicon.png +0 -0
  104. package/develop/index.html +0 -511
  105. package/eslint.config.mjs +0 -114
  106. package/public/index.d.ts +0 -337
  107. package/scripts/zip.js +0 -26
  108. package/src/core/Pushwoosh.ts +0 -768
  109. package/src/core/Pushwoosh.types.ts +0 -254
  110. package/src/core/Safari.types.ts +0 -26
  111. package/src/core/constants.ts +0 -58
  112. package/src/core/events.types.ts +0 -46
  113. package/src/core/functions.ts +0 -33
  114. package/src/core/legacyEventsMap.ts +0 -64
  115. package/src/core/logger.ts +0 -64
  116. package/src/core/modules/EventBus/EventBus.ts +0 -66
  117. package/src/core/storage.ts +0 -254
  118. package/src/helpers/logger.ts +0 -81
  119. package/src/helpers/pwlogger/Logger.constants.ts +0 -31
  120. package/src/helpers/pwlogger/Logger.ts +0 -218
  121. package/src/helpers/pwlogger/Logger.types.ts +0 -66
  122. package/src/helpers/pwlogger/handlers/handler-console/handler-console.ts +0 -40
  123. package/src/models/InboxMessages.ts +0 -202
  124. package/src/models/InboxMessages.types.ts +0 -111
  125. package/src/models/NotificationPayload.ts +0 -216
  126. package/src/models/NotificationPayload.types.ts +0 -65
  127. package/src/modules/Api/Api.ts +0 -386
  128. package/src/modules/Api/Api.types.ts +0 -7
  129. package/src/modules/ApiClient/ApiClient.ts +0 -153
  130. package/src/modules/ApiClient/ApiClient.types.ts +0 -222
  131. package/src/modules/Data/Data.ts +0 -345
  132. package/src/modules/DateModule.ts +0 -53
  133. package/src/modules/InboxMessagesPublic.ts +0 -222
  134. package/src/modules/PlatformChecker/PlatformChecker.ts +0 -170
  135. package/src/modules/PlatformChecker/PlatformChecker.types.ts +0 -5
  136. package/src/modules/storage/Storage.ts +0 -164
  137. package/src/modules/storage/Store.ts +0 -104
  138. package/src/modules/storage/migrations/26-11-2018.ts +0 -25
  139. package/src/modules/storage/migrations/MigrationExecutor.ts +0 -31
  140. package/src/modules/storage/migrations/Migrations.ts +0 -41
  141. package/src/modules/storage/migrations/constants.ts +0 -8
  142. package/src/modules/storage/migrations/helpers.ts +0 -16
  143. package/src/modules/storage/migrations/initial.ts +0 -47
  144. package/src/modules/storage/version.ts +0 -2
  145. package/src/pushwoosh-web-notifications.ts +0 -47
  146. package/src/pushwoosh-widget-inbox.ts +0 -8
  147. package/src/pushwoosh-widget-subscribe-popup.ts +0 -9
  148. package/src/pushwoosh-widget-subscription-button.ts +0 -8
  149. package/src/pushwoosh-widget-subscription-prompt.ts +0 -6
  150. package/src/service-worker.ts +0 -455
  151. package/src/services/PushService/PushService.types.ts +0 -74
  152. package/src/services/PushService/drivers/PushServiceDefault/PushServiceDefault.ts +0 -235
  153. package/src/services/PushService/drivers/PushServiceSafari/PushServiceSafari.ts +0 -125
  154. package/src/services/PushService/drivers/PushServiceSafari/PushServiceSafari.types.ts +0 -4
  155. package/src/widget-subscription-prompt.ts +0 -33
  156. package/src/widgets/Inbox/InboxWidget.ts +0 -564
  157. package/src/widgets/Inbox/constants.ts +0 -49
  158. package/src/widgets/Inbox/css/inboxWidgetStyle.css +0 -274
  159. package/src/widgets/Inbox/helpers.ts +0 -63
  160. package/src/widgets/Inbox/inbox.d.ts +0 -9
  161. package/src/widgets/Inbox/inbox_widget.types.ts +0 -41
  162. package/src/widgets/Inbox/widgetTemplates.ts +0 -55
  163. package/src/widgets/SubscribePopup/SubscribePopup.ts +0 -241
  164. package/src/widgets/SubscribePopup/constants.ts +0 -66
  165. package/src/widgets/SubscribePopup/helpers.ts +0 -11
  166. package/src/widgets/SubscribePopup/popupTemplates.ts +0 -24
  167. package/src/widgets/SubscribePopup/styles/popup.css +0 -226
  168. package/src/widgets/SubscribePopup/types/subscribe-popup.ts +0 -68
  169. package/src/widgets/SubscriptionButton/assets/css/main.css +0 -205
  170. package/src/widgets/SubscriptionButton/bell.ts +0 -67
  171. package/src/widgets/SubscriptionButton/constants.ts +0 -28
  172. package/src/widgets/SubscriptionButton/index.ts +0 -377
  173. package/src/widgets/SubscriptionButton/positioning.ts +0 -165
  174. package/src/widgets/SubscriptionButton/subscribe_widget.types.ts +0 -53
  175. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.constants.ts +0 -1
  176. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.helpers.ts +0 -110
  177. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.ts +0 -102
  178. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.types.ts +0 -23
  179. package/src/widgets/SubscriptionPrompt/constants.ts +0 -22
  180. package/src/widgets/SubscriptionPrompt/helpers.ts +0 -42
  181. package/src/widgets/widgets.d.ts +0 -4
  182. package/src/worker/global.ts +0 -36
  183. package/src/worker/notification.ts +0 -34
  184. package/src/worker/worker.types.ts +0 -4
  185. package/test/__helpers__/apiHelpers.ts +0 -22
  186. package/test/__helpers__/keyValueHelpers.ts +0 -15
  187. package/test/__helpers__/platformHelpers.ts +0 -54
  188. package/test/__helpers__/sinonHelpers.ts +0 -7
  189. package/test/__helpers__/storageHelpers.ts +0 -56
  190. package/test/__mocks__/apiRequests.ts +0 -26
  191. package/test/__mocks__/idbMock.ts +0 -12
  192. package/test/__mocks__/idbObjectStoreMock.ts +0 -38
  193. package/test/__mocks__/inboxMessages.ts +0 -292
  194. package/test/__mocks__/models/inboxModel.ts +0 -71
  195. package/test/__mocks__/modules/apiClientModule.ts +0 -18
  196. package/test/__mocks__/modules/dateModule.ts +0 -34
  197. package/test/__mocks__/modules/inboxParamsModule.ts +0 -21
  198. package/test/__mocks__/modules/paramsBuilder.ts +0 -12
  199. package/test/__mocks__/modules/paramsModule.ts +0 -35
  200. package/test/__mocks__/modules/payloadBuilderModule.ts +0 -15
  201. package/test/__mocks__/modules/storageModule.ts +0 -58
  202. package/test/__mocks__/navigator.ts +0 -38
  203. package/test/__mocks__/notification.ts +0 -84
  204. package/test/__mocks__/pushwoosh.ts +0 -12
  205. package/test/__mocks__/userAgents +0 -8
  206. package/test/functions.test.ts +0 -22
  207. package/test/ignore-html.js +0 -6
  208. package/test/mocha.opts +0 -6
  209. package/test/modules/DateModule/unit.test.ts +0 -80
  210. package/test/modules/storage/Storage/unit.test.ts +0 -180
  211. package/test/modules/storage/Store/unit.test.ts +0 -192
  212. package/testRegister.js +0 -24
  213. package/tsconfig.json +0 -31
  214. package/webpack.config.js +0 -163
  215. /package/{src/core/modules/EventBus/index.ts → core/modules/EventBus/index.d.ts} +0 -0
  216. /package/{src/helpers/pwlogger/index.ts → helpers/pwlogger/index.d.ts} +0 -0
  217. /package/{src/modules/PlatformChecker/index.ts → modules/PlatformChecker/index.d.ts} +0 -0
  218. /package/{src/npm.ts → npm.d.ts} +0 -0
  219. /package/{src/services/PushService/PushService.ts → services/PushService/PushService.d.ts} +0 -0
  220. /package/{src/widget-inbox.ts → widget-inbox.d.ts} +0 -0
  221. /package/{src/widget-subscribe-popup.ts → widget-subscribe-popup.d.ts} +0 -0
  222. /package/{src/widget-subscription-button.ts → widget-subscription-button.d.ts} +0 -0
  223. /package/{src/widgets/Inbox/index.ts → widgets/Inbox/index.d.ts} +0 -0
  224. /package/{src/widgets/SubscribePopup/index.ts → widgets/SubscribePopup/index.d.ts} +0 -0
@@ -1,31 +0,0 @@
1
- import Migrations from './Migrations';
2
- import { type TMigrationType } from '../Storage.types';
3
-
4
- export default class MigrationExecutor {
5
- db: IDBDatabase;
6
- migrationsBuilder: Migrations;
7
-
8
- constructor(
9
- db: IDBDatabase,
10
- migrationsBuilder: Migrations = new Migrations(),
11
- ) {
12
- this.db = db;
13
- this.migrationsBuilder = migrationsBuilder;
14
- }
15
-
16
- applyMigrations() {
17
- // apply initial migrations
18
- this.applyMigrationsPack(this.migrationsBuilder.initial);
19
-
20
- // apply migrations, sorted by date
21
- this.migrationsBuilder.dateSorted.forEach((migrationsPack) => {
22
- this.applyMigrationsPack(migrationsPack);
23
- });
24
- }
25
-
26
- applyMigrationsPack(migrationsPack: Array<TMigrationType>) {
27
- migrationsPack.forEach((migration) => {
28
- migration(this.db);
29
- });
30
- }
31
- }
@@ -1,41 +0,0 @@
1
- import { default as migrations26_11_2018 } from './26-11-2018';
2
- import { default as migrationsInitial } from './initial';
3
- import DateModule from '../../DateModule';
4
- import { type TMigrationsObjectType, type TMigrationType } from '../Storage.types';
5
-
6
- export default class Migrations {
7
- migrations: TMigrationsObjectType;
8
- dateModule: DateModule;
9
-
10
- constructor(dateModule: DateModule = new DateModule()) {
11
- this.migrations = {
12
- // initial migrations
13
- initial: migrationsInitial,
14
-
15
- // migrations for 2018-11-26
16
- '2018/11/26': migrations26_11_2018,
17
- };
18
- this.dateModule = dateModule;
19
- }
20
-
21
- /**
22
- * Initial migration pack
23
- */
24
- get initial(): Array<TMigrationType> {
25
- return this.migrations.initial;
26
- }
27
-
28
- /**
29
- * Return array of migrations packs sorted by date
30
- */
31
- get dateSorted(): Array<Array<TMigrationType>> {
32
- return Object.keys(this.migrations)
33
- .filter((key) => key !== 'initial') // remove initial migrations
34
- .sort((a, b) => { // sort migrations by date YYYY/MM/DD
35
- const dateA = new DateModule(new Date(a));
36
- const dateB = new DateModule(new Date(b));
37
- return dateA.getTimestamp() - dateB.getTimestamp();
38
- })
39
- .map((key) => this.migrations[key]); // Array of migrations packs, sorted by date
40
- }
41
- }
@@ -1,8 +0,0 @@
1
- import { type TInboxMessagesStoreName, type TKeyValueStoreName, type TMainLogStoreName, type TMessageLogStoreName } from '../Storage.types';
2
-
3
- export const STORE_NAME_KEY_VALUE: TKeyValueStoreName = 'keyValue';
4
- export const STORE_NAME_MESSAGE_LOG: TMessageLogStoreName = 'messages';
5
- export const STORE_NAME_MAIN_LOG: TMainLogStoreName = 'log';
6
- export const STORE_NAME_INBOX_MESSAGES: TInboxMessagesStoreName = 'inboxMessages';
7
-
8
- export const KEY_PATH_BASE_INCREMENT = 'id';
@@ -1,16 +0,0 @@
1
- import { type TMigrationType, type TSdkStoreName } from '../Storage.types';
2
-
3
- /**
4
- * Check existed store, before create
5
- * @param name
6
- * @param storeCreator
7
- */
8
- export function storeCreatorDecorator(name: TSdkStoreName, storeCreator: TMigrationType): TMigrationType {
9
- return function (database: IDBDatabase) {
10
- if (database.objectStoreNames.contains(name)) {
11
- return;
12
- }
13
-
14
- storeCreator(database);
15
- };
16
- }
@@ -1,47 +0,0 @@
1
- import {
2
- STORE_NAME_KEY_VALUE,
3
- STORE_NAME_MAIN_LOG,
4
- STORE_NAME_MESSAGE_LOG,
5
- KEY_PATH_BASE_INCREMENT,
6
- } from './constants';
7
- import { storeCreatorDecorator } from './helpers';
8
-
9
- /**
10
- * Create keyValue store migration
11
- * @param database
12
- */
13
- function createKeyValueStore(database: IDBDatabase) {
14
- database.createObjectStore(STORE_NAME_KEY_VALUE, { keyPath: 'key' });
15
- }
16
-
17
- /**
18
- * Create log store migration
19
- * @param database
20
- */
21
- function createLogStore(database: IDBDatabase) {
22
- const logStore = database.createObjectStore(
23
- STORE_NAME_MAIN_LOG,
24
- { keyPath: KEY_PATH_BASE_INCREMENT, autoIncrement: true },
25
- );
26
- logStore.createIndex('environment', 'environment', { unique: false });
27
- logStore.createIndex('date', 'date', { unique: false });
28
- logStore.createIndex('type', 'type', { unique: false });
29
- }
30
-
31
- /**
32
- * Create message log store migration
33
- * @param database
34
- */
35
- function createMessageLogStore(database: IDBDatabase) {
36
- const messagesStore = database.createObjectStore(
37
- STORE_NAME_MESSAGE_LOG,
38
- { keyPath: KEY_PATH_BASE_INCREMENT, autoIncrement: true },
39
- );
40
- messagesStore.createIndex('date', 'date', { unique: false });
41
- }
42
-
43
- export default [
44
- storeCreatorDecorator(STORE_NAME_KEY_VALUE, createKeyValueStore),
45
- storeCreatorDecorator(STORE_NAME_MAIN_LOG, createLogStore),
46
- storeCreatorDecorator(STORE_NAME_MESSAGE_LOG, createMessageLogStore),
47
- ];
@@ -1,2 +0,0 @@
1
- // indexed DB version
2
- export default 7;
@@ -1,47 +0,0 @@
1
- import { getGlobal } from './core/functions';
2
- import { Pushwoosh } from './core/Pushwoosh';
3
-
4
- declare const __SDK_PATH__: string;
5
- function addWidget(widgetName: string) {
6
- const src = `${__SDK_PATH__}pushwoosh-widget-${widgetName}${process.env.NODE_ENV === 'development' ? '.uncompress' : ''}.js`;
7
- const script = document.createElement('script');
8
- script.src = src;
9
- script.async = true;
10
- script.type = 'text/javascript';
11
- document.head.appendChild(script);
12
- }
13
-
14
- function main() {
15
- const global: any = getGlobal();
16
- const PW = new Pushwoosh();
17
- const commands = Array.isArray(global.Pushwoosh) ? global.Pushwoosh as any[] : [];
18
-
19
- global.Pushwoosh = PW;
20
- commands.forEach((command) => PW.push(command));
21
-
22
- PW.push(() => {
23
- const { initParams } = PW;
24
-
25
- if (PW.driver.checkIsPermissionDefault()) {
26
- addWidget('subscription-prompt');
27
- }
28
-
29
- if (initParams.subscribeWidget?.enable) {
30
- addWidget('subscription-button');
31
- }
32
-
33
- if (initParams.subscribePopup?.enable) {
34
- addWidget('subscribe-popup');
35
- }
36
-
37
- if (initParams.inboxWidget?.enable) {
38
- addWidget('inbox');
39
- }
40
- });
41
- }
42
-
43
- if (document.readyState === 'complete') {
44
- main();
45
- } else {
46
- window.addEventListener('load', main);
47
- }
@@ -1,8 +0,0 @@
1
- import { type Pushwoosh } from './core/Pushwoosh';
2
- import { PWInboxWidget } from './widgets/Inbox';
3
-
4
- const globalPW: Pushwoosh = (globalThis as any).Pushwoosh;
5
-
6
- globalPW.push(() => {
7
- new PWInboxWidget(globalPW);
8
- });
@@ -1,9 +0,0 @@
1
- import { type Pushwoosh } from './core/Pushwoosh';
2
- import { PWSubscribePopupWidget } from './widgets/SubscribePopup';
3
-
4
- const globalPW: Pushwoosh = (globalThis as any).Pushwoosh;
5
-
6
- globalPW.push(() => {
7
- const popup = new PWSubscribePopupWidget(globalPW);
8
- popup.initPopup();
9
- });
@@ -1,8 +0,0 @@
1
- import { type Pushwoosh } from './core/Pushwoosh';
2
- import { PWSubscriptionButtonWidget } from './widgets/SubscriptionButton';
3
-
4
- const globalPW: Pushwoosh = (globalThis as any).Pushwoosh;
5
-
6
- globalPW.push(() => {
7
- new PWSubscriptionButtonWidget(globalPW);
8
- });
@@ -1,6 +0,0 @@
1
- import { type Pushwoosh } from './core/Pushwoosh';
2
- import { createWidget } from './widget-subscription-prompt';
3
-
4
- const globalPW: Pushwoosh = (globalThis as any).Pushwoosh;
5
-
6
- globalPW.push(() => createWidget(globalPW));
@@ -1,455 +0,0 @@
1
- import {
2
- PERIOD_GOAL_EVENT,
3
- LEGACY_EVENT_ON_PUSH_DELIVERY,
4
- LEGACY_EVENT_ON_NOTIFICATION_CLICK,
5
- LEGACY_EVENT_ON_NOTIFICATION_CLOSE,
6
- LEGACY_EVENT_ON_PUT_NEW_MESSAGE_TO_INBOX_STORE,
7
- } from './core/constants';
8
- import { getVersion, parseSerializedNotificationParams } from './core/functions';
9
- import { Logger } from './core/logger';
10
- import { EventBus } from './core/modules/EventBus';
11
- import { type INotificationOptions, type IPWBroadcastClientsParams, type IWorkerPushwooshGlobal } from './core/Pushwoosh.types';
12
- import { message as messagesLog } from './core/storage';
13
- import { sendFatalLogToRemoteServer } from './helpers/logger';
14
- import InboxMessages from './models/InboxMessages';
15
- import { type TInboxMessageStatusOpen } from './models/InboxMessages.types';
16
- import NotificationPayload from './models/NotificationPayload';
17
- import { type INotificationButton } from './models/NotificationPayload.types';
18
- import { Api } from './modules/Api/Api';
19
- import { Data } from './modules/Data/Data';
20
- import InboxMessagesPublic from './modules/InboxMessagesPublic';
21
- import { PushServiceDefault } from './services/PushService/drivers/PushServiceDefault/PushServiceDefault';
22
- import WorkerPushwooshGlobal from './worker/global';
23
- import PushwooshNotification from './worker/notification';
24
- import { type PushSubscriptionChangeEvent } from './worker/worker.types';
25
-
26
- declare const self: ServiceWorkerGlobalScope & {
27
- Pushwoosh: IWorkerPushwooshGlobal;
28
- };
29
-
30
- const eventBus = new EventBus();
31
- const data = new Data();
32
- const api = new Api(eventBus, data);
33
-
34
- const Pushwoosh = self.Pushwoosh = new WorkerPushwooshGlobal(eventBus, data, api);
35
- const clickedNotifications: string[] = [];
36
-
37
- self.addEventListener('install', onInstallEventHandler);
38
-
39
- self.addEventListener('activate', onActivateEventHandler);
40
-
41
- self.addEventListener('push', onPushEventHandler);
42
-
43
- self.addEventListener('notificationclick', onClickNotificationEventHandler);
44
-
45
- self.addEventListener('notificationclose', onCloseNotificationEventHandler);
46
-
47
- self.addEventListener('pushsubscriptionchange', onPushSubscriptionChange);
48
-
49
- /**
50
- * On install SW event handler
51
- * Update indexedDB SW version and skip waiting stage
52
- * @param event
53
- */
54
- function onInstallEventHandler(event: ExtendableEvent): void {
55
- async function onInstall(): Promise<void> {
56
- await Promise.all([
57
- Pushwoosh.data.setServiceWorkerVersion(getVersion()),
58
- Logger.write('info', 'install'),
59
- ]);
60
-
61
- // PUSH-21674 - not auto closing push if push receive when chrome is closed
62
- await self.skipWaiting();
63
- }
64
-
65
- return event.waitUntil(
66
- onInstall()
67
- .catch(onInstallFailure),
68
- );
69
- }
70
-
71
- /**
72
- * On activate SW event handler
73
- * Do nothing, only write log
74
- * @param event
75
- */
76
- function onActivateEventHandler(event: ExtendableEvent) {
77
- async function onActivate(): Promise<void> {
78
- await Promise.all([
79
- Logger.write('info', 'activate'),
80
- ]);
81
-
82
- await self.clients.claim();
83
- }
84
-
85
- event.waitUntil(
86
- onActivate()
87
- .catch(onActivateFailure),
88
- );
89
- }
90
-
91
- /**
92
- * On push SW event handler
93
- * @param event
94
- */
95
- function onPushEventHandler(event: PushEvent): void {
96
- async function onPush(event: PushEvent): Promise<void> {
97
- // wake up SW on all pages
98
- await self.clients.claim();
99
-
100
- // get payload
101
- const payload = await event.data!.json();
102
-
103
- // create notification payload
104
- const notificationPayload = new NotificationPayload(payload);
105
-
106
- // get notification options
107
- const notificationOptions = await notificationPayload.getNotificationOptionsPayload();
108
-
109
- // get notification show options
110
- const notificationShowOptions = await notificationPayload.getShowNotificationOptions();
111
-
112
- // get message hash
113
- const messageHash = notificationPayload.messageHash;
114
-
115
- // logging in indexedDB;
116
- await Logger.write('info', JSON.stringify(notificationOptions), 'onPush');
117
-
118
- // show notification instance
119
- const notification = new PushwooshNotification(
120
- notificationShowOptions,
121
- notificationPayload.body,
122
- await notificationPayload.getTitle(),
123
- );
124
-
125
- // Call receive push listeners
126
- const callbacks = Pushwoosh.getListeners('onPush');
127
-
128
- await callbacks.reduce((pr, fun) => pr.then(() => fun(notification)), Promise.resolve());
129
-
130
- // Execute receive push actions
131
- const onPushActions = [
132
- notification.show(), // Show notification
133
- messagesLog.add({ // Put message to messages store
134
- payload: payload,
135
- parsedPayload: notificationOptions,
136
- showOptions: notificationShowOptions,
137
- }),
138
- broadcastClients({ type: LEGACY_EVENT_ON_PUSH_DELIVERY, payload: notificationOptions }), // post message to window clients
139
- ];
140
-
141
- // Send delivery statistic
142
- if (messageHash) {
143
- onPushActions.push(
144
- Pushwoosh.initApi().then(() => Pushwoosh.api.messageDeliveryEvent(messageHash, true, notificationOptions.metaData)),
145
- );
146
- }
147
-
148
- // Inbox message actions
149
- if (notificationPayload.inboxId !== '') {
150
- const inboxMessages = new InboxMessages(Pushwoosh.eventBus, Pushwoosh.data, Pushwoosh.api);
151
- const inboxMessagesPublic = new InboxMessagesPublic(Pushwoosh.data, Pushwoosh.api, inboxMessages);
152
- const inboxMessagePayload = await notificationPayload.getInboxMessage();
153
-
154
- const payload = await inboxMessagesPublic.publicMessageBuilder(inboxMessagePayload);
155
- onPushActions.push(
156
- inboxMessages.putMessage(inboxMessagePayload), // put message to inboxMessages store
157
- broadcastClients({ // post message to window clients
158
- type: LEGACY_EVENT_ON_PUT_NEW_MESSAGE_TO_INBOX_STORE,
159
- payload,
160
- }),
161
- );
162
- }
163
-
164
- await Promise.all(onPushActions);
165
- }
166
-
167
- event.waitUntil(
168
- onPush(event)
169
- .catch((error) => onPushFailure(error, event)),
170
- );
171
- }
172
-
173
- /**
174
- * On click notification event handler
175
- * @param event
176
- */
177
- function onClickNotificationEventHandler(event: NotificationEvent): void {
178
- async function onClickNotification(event: NotificationEvent) {
179
- await self.clients.claim();
180
-
181
- // get notification options
182
- const notificationOptions = await parseNotificationEvent(event);
183
-
184
- const {
185
- messageHash,
186
- metaData,
187
- url,
188
- code,
189
- inboxId,
190
- } = notificationOptions;
191
-
192
- if (code) {
193
- clickedNotifications.push(code);
194
- }
195
-
196
- if (inboxId !== '') {
197
- const inboxMessages = new InboxMessages(Pushwoosh.eventBus, Pushwoosh.data, Pushwoosh.api);
198
-
199
- const message = await inboxMessages.getMessage(inboxId);
200
- (<TInboxMessageStatusOpen>message.status) = 3;
201
- await inboxMessages.putMessage(message);
202
- }
203
-
204
- event.notification.close();
205
-
206
- const message = { type: LEGACY_EVENT_ON_NOTIFICATION_CLICK, payload: notificationOptions };
207
-
208
- if (url) {
209
- event.waitUntil(
210
- self.clients.matchAll({ type: 'window' })
211
- .then((windowClients) => {
212
- return focusWindow(windowClients as any, url);
213
- })
214
- .then((isFocusOnNewWindowClient) => {
215
- if (isFocusOnNewWindowClient) {
216
- Pushwoosh.data.setDelayedEvent(message);
217
- }
218
- }),
219
- );
220
- }
221
-
222
- return Promise.all([
223
- Pushwoosh.initApi().then(() => Pushwoosh.api.pushStat(messageHash, true, metaData)),
224
- Pushwoosh.data.setLastOpenMessage({
225
- url,
226
- messageHash,
227
- expiry: Date.now() + PERIOD_GOAL_EVENT,
228
- }),
229
- broadcastClients(message),
230
- ]);
231
- }
232
-
233
- event.waitUntil(
234
- onClickNotification(event)
235
- .catch(onClickNotificationFailure),
236
- );
237
- }
238
-
239
- /**
240
- * On close notification event handler
241
- * @param event
242
- */
243
- function onCloseNotificationEventHandler(event: NotificationEvent) {
244
- async function closeNotification(event: NotificationEvent): Promise<void> {
245
- await self.clients.claim();
246
-
247
- const notificationOptions = await parseNotificationEvent(event);
248
- const { code } = notificationOptions;
249
-
250
- event.notification.close();
251
-
252
- if (!code) {
253
- return;
254
- }
255
-
256
- const index = clickedNotifications.indexOf(code);
257
- if (index >= 0) {
258
- clickedNotifications.splice(index, 1);
259
- } else {
260
- return broadcastClients({ type: LEGACY_EVENT_ON_NOTIFICATION_CLOSE, payload: notificationOptions });
261
- }
262
- }
263
-
264
- event.waitUntil(
265
- closeNotification(event)
266
- .catch(closeNotificationFailure),
267
- );
268
- }
269
-
270
- async function onPushSubscriptionChange(event: PushSubscriptionChangeEvent): Promise<void> {
271
- async function changePushSubscription(event: PushSubscriptionChangeEvent): Promise<void> {
272
- let subscription = event.newSubscription;
273
- if (!subscription) {
274
- subscription = await self.registration.pushManager.getSubscription();
275
- if (!subscription) {
276
- return;
277
- }
278
- }
279
-
280
- const pushService = new PushServiceDefault(api, data, {});
281
- await pushService.subscribe(subscription);
282
- }
283
-
284
- event.waitUntil(
285
- changePushSubscription(event)
286
- .catch(pushSubscriptionChangeFailure),
287
- );
288
- }
289
-
290
- /**
291
- * Post message to all Window Clients
292
- * @param msg
293
- */
294
- async function broadcastClients(msg: IPWBroadcastClientsParams) {
295
- const clients = await self.clients.matchAll();
296
- clients.forEach((client) => client.postMessage(msg));
297
- }
298
-
299
- async function focusWindow(windowClients: Array<WindowClient>, url: string): Promise<boolean> {
300
- // if opened url without location, use service worker origin
301
- const {
302
- hostname: openedHostname,
303
- pathname: openedPathname,
304
- } = new URL(url, self.location.origin);
305
-
306
- const clientWithSameAddress = windowClients.find((windowClient) => {
307
- const {
308
- hostname: windowClientHostname,
309
- pathname: windowClientPathname,
310
- } = new URL(windowClient.url);
311
-
312
- // opened url is equal with current window client url if hostname and pathname are equals
313
- return windowClientHostname === openedHostname && windowClientPathname === openedPathname;
314
- });
315
- if (clientWithSameAddress) {
316
- await clientWithSameAddress.focus();
317
- return false; // focus on exist window client
318
- }
319
-
320
- // if not window client with opened url we must open new window and focus it
321
- await self.clients.openWindow(url)
322
- .then((newClientWindow) => {
323
- // if new client window url does not match
324
- // of service worker domain then new client window is null
325
- if (!newClientWindow) {
326
- return;
327
- }
328
-
329
- newClientWindow.focus();
330
- });
331
- return true; // focus on new window client
332
- }
333
-
334
- async function parseNotificationEvent(event: NotificationEvent): Promise<INotificationOptions> {
335
- const { notification = {} as Notification } = event;
336
- const { data: notificationData } = notification;
337
- const notificationTag = parseSerializedNotificationParams(notification.tag, {});
338
-
339
- let url = '';
340
-
341
- if (event.action && Array.isArray(notificationData.buttons)) {
342
- const button = notificationData.buttons.find((button: INotificationButton) => button.action === event.action) || {};
343
- url = button.url;
344
- } else {
345
- url = notificationTag.url;
346
- }
347
-
348
- return {
349
- title: notification.title,
350
- body: notification.body,
351
- icon: notification.icon,
352
-
353
- buttons: notificationData.buttons,
354
- image: notificationData.image,
355
- code: notificationData.code,
356
- campaignCode: notificationData.campaignCode,
357
- inboxId: notificationData.inboxId,
358
-
359
- messageHash: notificationTag.messageHash,
360
- customData: notificationTag.customData,
361
- metaData: notificationTag.metaData,
362
- openUrl: notificationTag.url,
363
-
364
- tag: notification.tag,
365
- url,
366
- };
367
- }
368
-
369
- async function onInstallFailure(error: Error | string): Promise<void> {
370
- const applicationCode = await Pushwoosh.data.getApplicationCode();
371
- const workerVersion = await Pushwoosh.data.getServiceWorkerVersion();
372
-
373
- await sendFatalLogToRemoteServer({
374
- message: 'Error in onInstallEventHandler',
375
- code: 'FATAL-SW-001',
376
- error,
377
- applicationCode,
378
- workerVersion,
379
- });
380
- }
381
-
382
- async function onActivateFailure(error: Error | string): Promise<void> {
383
- const applicationCode = await Pushwoosh.data.getApplicationCode();
384
- const workerVersion = await Pushwoosh.data.getServiceWorkerVersion();
385
-
386
- await sendFatalLogToRemoteServer({
387
- message: 'Error in onActivateEventHandler',
388
- code: 'FATAL-SW-002',
389
- error,
390
- applicationCode,
391
- workerVersion,
392
- });
393
- }
394
-
395
- async function onPushFailure(error: Error | string, event: PushEvent): Promise<void> {
396
- const applicationCode = await Pushwoosh.data.getApplicationCode();
397
- const workerVersion = await Pushwoosh.data.getServiceWorkerVersion();
398
-
399
- await sendFatalLogToRemoteServer({
400
- message: 'Error in onPushEventHandler',
401
- code: 'FATAL-SW-003',
402
- error,
403
- applicationCode,
404
- workerVersion,
405
- });
406
-
407
- if (!(error instanceof Error)) {
408
- error = new Error(error);
409
- }
410
-
411
- return messagesLog.add({
412
- error: `${error}`,
413
- stack: error.stack,
414
- payload: event.data && event.data.text(),
415
- });
416
- }
417
-
418
- async function onClickNotificationFailure(error: Error | string): Promise<void> {
419
- const applicationCode = await Pushwoosh.data.getApplicationCode();
420
- const workerVersion = await Pushwoosh.data.getServiceWorkerVersion();
421
-
422
- await sendFatalLogToRemoteServer({
423
- message: 'Error in onNotificationClickEventHandler',
424
- code: 'FATAL-SW-004',
425
- error,
426
- applicationCode,
427
- workerVersion,
428
- });
429
- }
430
-
431
- async function closeNotificationFailure(error: Error | string): Promise<void> {
432
- const applicationCode = await Pushwoosh.data.getApplicationCode();
433
- const workerVersion = await Pushwoosh.data.getServiceWorkerVersion();
434
-
435
- await sendFatalLogToRemoteServer({
436
- message: 'Error in onNotificationCloseEventHandler',
437
- code: 'FATAL-SW-005',
438
- error,
439
- applicationCode,
440
- workerVersion,
441
- });
442
- }
443
-
444
- async function pushSubscriptionChangeFailure(error: Error | string): Promise<void> {
445
- const applicationCode = await Pushwoosh.data.getApplicationCode();
446
- const workerVersion = await Pushwoosh.data.getServiceWorkerVersion();
447
-
448
- await sendFatalLogToRemoteServer({
449
- message: 'Error in onPushSubscriptionChange',
450
- code: 'FATAL-SW-006',
451
- error,
452
- applicationCode,
453
- workerVersion,
454
- });
455
- }