@firebase/messaging
Version:
This is the Firebase Cloud Messaging component of the Firebase JS SDK.
1 lines • 153 kB
Source Map (JSON)
{"version":3,"file":"index.sw.cjs","sources":["../src/util/constants.ts","../src/interfaces/internal-message-payload.ts","../src/helpers/array-base64-translator.ts","../src/helpers/migrate-old-database.ts","../src/util/errors.ts","../src/internals/idb-manager.ts","../src/internals/requests.ts","../src/internals/token-manager.ts","../src/helpers/updateVapidKey.ts","../src/internals/register-fid.ts","../src/helpers/fid-change-registration.ts","../src/helpers/externalizePayload.ts","../src/helpers/is-console-message.ts","../src/helpers/sleep.ts","../src/helpers/logToFirelog.ts","../src/listeners/sw-listeners.ts","../src/helpers/extract-app-config.ts","../src/messaging-service.ts","../src/helpers/register.ts","../src/api/isSupported.ts","../src/api/onBackgroundMessage.ts","../src/api/onRegistered.ts","../src/api/onUnregistered.ts","../src/api/setDeliveryMetricsExportedToBigQueryEnabled.ts","../src/api.ts","../src/index.sw.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport const DEFAULT_SW_PATH = '/firebase-messaging-sw.js';\nexport const DEFAULT_SW_SCOPE = '/firebase-cloud-messaging-push-scope';\n\nexport const DEFAULT_VAPID_KEY =\n 'BDOU99-h67HcA6JeFXHbSNMu7e2yNNu3RzoMj8TM4W88jITfq7ZmPvIM1Iv-4_l2LxQcYwhqby2xGpWwzjfAnG4';\n\nexport const ENDPOINT = 'https://fcmregistrations.googleapis.com/v1';\n\n/** Key of FCM Payload in Notification's data field. */\nexport const FCM_MSG = 'FCM_MSG';\n\nexport const CONSOLE_CAMPAIGN_ID = 'google.c.a.c_id';\nexport const CONSOLE_CAMPAIGN_NAME = 'google.c.a.c_l';\nexport const CONSOLE_CAMPAIGN_TIME = 'google.c.a.ts';\n/** Set to '1' if Analytics is enabled for the campaign */\nexport const CONSOLE_CAMPAIGN_ANALYTICS_ENABLED = 'google.c.a.e';\nexport const TAG = 'FirebaseMessaging: ';\nexport const MAX_NUMBER_OF_EVENTS_PER_LOG_REQUEST = 1000;\nexport const MAX_RETRIES = 3;\nexport const LOG_INTERVAL_IN_MS = 86400000; //24 hour\nexport const DEFAULT_BACKOFF_TIME_MS = 5000;\nexport const DEFAULT_REGISTRATION_TIMEOUT = 10000;\n\n// FCM log source name registered at Firelog: 'FCM_CLIENT_EVENT_LOGGING'. It uniquely identifies\n// FCM's logging configuration.\nexport const FCM_LOG_SOURCE = 1249;\n\n// Defined as in proto/messaging_event.proto. Neglecting fields that are supported.\nexport const SDK_PLATFORM_WEB = 3;\nexport const EVENT_MESSAGE_DELIVERED = 1;\n\nexport enum MessageType {\n DATA_MESSAGE = 1,\n DISPLAY_NOTIFICATION = 3\n}\n","/**\n * @license\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License. You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under the License\n * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n * or implied. See the License for the specific language governing permissions and limitations under\n * the License.\n */\n\nimport {\n CONSOLE_CAMPAIGN_ANALYTICS_ENABLED,\n CONSOLE_CAMPAIGN_ID,\n CONSOLE_CAMPAIGN_NAME,\n CONSOLE_CAMPAIGN_TIME\n} from '../util/constants';\n\nexport interface MessagePayloadInternal {\n notification?: NotificationPayloadInternal;\n data?: unknown;\n fcmOptions?: FcmOptionsInternal;\n messageType?: MessageType;\n isFirebaseMessaging?: boolean;\n from: string;\n fcmMessageId: string;\n productId: number;\n // eslint-disable-next-line camelcase\n collapse_key: string;\n}\n\n// https://developer.mozilla.org/en-US/docs/Web/API/Notification/actions\ninterface NotificationAction {\n action: string;\n icon?: string;\n title: string;\n}\n\n/**\n * This interface defines experimental properties of NotificationOptions, that are not part of\n * the interface in the generated DOM types at https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/179bdd84a944933a3103f29c2274c9f5a857b693/baselines/dom.generated.d.ts#L1012\n * https://developer.mozilla.org/en-US/docs/Web/API/Notification\n */\ninterface NotificationOptionsExperimental extends NotificationOptions {\n readonly maxActions?: number;\n readonly actions?: NotificationAction[];\n readonly image?: string;\n readonly renotify?: boolean;\n readonly timestamp?: EpochTimeStamp;\n readonly vibrate?: VibratePattern;\n}\n\nexport interface NotificationPayloadInternal\n extends NotificationOptionsExperimental {\n title: string;\n // Supported in the Legacy Send API.\n // See:https://firebase.google.com/docs/cloud-messaging/xmpp-server-ref.\n // eslint-disable-next-line camelcase\n click_action?: string;\n icon?: string;\n}\n\n// Defined in\n// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions. Note\n// that the keys are sent to the clients in snake cases which we need to convert to camel so it can\n// be exposed as a type to match the Firebase API convention.\nexport interface FcmOptionsInternal {\n link?: string;\n\n // eslint-disable-next-line camelcase\n analytics_label?: string;\n}\n\nexport enum MessageType {\n PUSH_RECEIVED = 'push-received',\n NOTIFICATION_CLICKED = 'notification-clicked',\n FID_REGISTERED = 'fid-registered'\n}\n\n/** Additional data of a message sent from the FN Console. */\nexport interface ConsoleMessageData {\n [CONSOLE_CAMPAIGN_ID]: string;\n [CONSOLE_CAMPAIGN_TIME]: string;\n [CONSOLE_CAMPAIGN_NAME]?: string;\n [CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]?: '1';\n}\n\nexport interface FidRegisteredPayload {\n isFirebaseMessaging: boolean;\n messageType: MessageType.FID_REGISTERED;\n fid: string;\n}\n","/**\n * @license\n * Copyright 2017 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport function arrayToBase64(array: Uint8Array | ArrayBuffer): string {\n const uint8Array = new Uint8Array(array);\n const base64String = btoa(String.fromCharCode(...uint8Array));\n return base64String.replace(/=/g, '').replace(/\\+/g, '-').replace(/\\//g, '_');\n}\n\nexport function base64ToArray(base64String: string): Uint8Array {\n const padding = '='.repeat((4 - (base64String.length % 4)) % 4);\n const base64 = (base64String + padding)\n .replace(/\\-/g, '+')\n .replace(/_/g, '/');\n\n const rawData = atob(base64);\n const outputArray = new Uint8Array(rawData.length);\n\n for (let i = 0; i < rawData.length; ++i) {\n outputArray[i] = rawData.charCodeAt(i);\n }\n return outputArray;\n}\n","/**\n * @license\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { deleteDB, openDB } from 'idb';\n\nimport { TokenDetails } from '../interfaces/registration-details';\nimport { arrayToBase64 } from './array-base64-translator';\n\n// https://github.com/firebase/firebase-js-sdk/blob/7857c212f944a2a9eb421fd4cb7370181bc034b5/packages/messaging/src/interfaces/token-details.ts\nexport interface V2TokenDetails {\n fcmToken: string;\n swScope: string;\n vapidKey: string | Uint8Array;\n subscription: PushSubscription;\n fcmSenderId: string;\n fcmPushSet: string;\n createTime?: number;\n endpoint?: string;\n auth?: string;\n p256dh?: string;\n}\n\n// https://github.com/firebase/firebase-js-sdk/blob/6b5b15ce4ea3df5df5df8a8b33a4e41e249c7715/packages/messaging/src/interfaces/registration-details.ts\nexport interface V3TokenDetails {\n fcmToken: string;\n swScope: string;\n vapidKey: Uint8Array;\n fcmSenderId: string;\n fcmPushSet: string;\n endpoint: string;\n auth: ArrayBuffer;\n p256dh: ArrayBuffer;\n createTime: number;\n}\n\n// https://github.com/firebase/firebase-js-sdk/blob/9567dba664732f681fa7fe60f5b7032bb1daf4c9/packages/messaging/src/interfaces/registration-details.ts\nexport interface V4TokenDetails {\n fcmToken: string;\n swScope: string;\n vapidKey: Uint8Array;\n fcmSenderId: string;\n endpoint: string;\n auth: ArrayBufferLike;\n p256dh: ArrayBufferLike;\n createTime: number;\n}\n\nconst OLD_DB_NAME = 'fcm_token_details_db';\n/**\n * The last DB version of 'fcm_token_details_db' was 4. This is one higher, so that the upgrade\n * callback is called for all versions of the old DB.\n */\nconst OLD_DB_VERSION = 5;\nconst OLD_OBJECT_STORE_NAME = 'fcm_token_object_Store';\n\nexport async function migrateOldDatabase(\n senderId: string\n): Promise<TokenDetails | null> {\n if ('databases' in indexedDB) {\n // indexedDb.databases() is an IndexedDB v3 API and does not exist in all browsers. TODO: Remove\n // typecast when it lands in TS types.\n const databases = await (\n indexedDB as {\n databases(): Promise<Array<{ name: string; version: number }>>;\n }\n ).databases();\n const dbNames = databases.map(db => db.name);\n\n if (!dbNames.includes(OLD_DB_NAME)) {\n // old DB didn't exist, no need to open.\n return null;\n }\n }\n\n let tokenDetails: TokenDetails | null = null;\n\n const db = await openDB(OLD_DB_NAME, OLD_DB_VERSION, {\n upgrade: async (db, oldVersion, newVersion, upgradeTransaction) => {\n if (oldVersion < 2) {\n // Database too old, skip migration.\n return;\n }\n\n if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {\n // Database did not exist. Nothing to do.\n return;\n }\n\n const objectStore = upgradeTransaction.objectStore(OLD_OBJECT_STORE_NAME);\n const value = await objectStore.index('fcmSenderId').get(senderId);\n await objectStore.clear();\n\n if (!value) {\n // No entry in the database, nothing to migrate.\n return;\n }\n\n if (oldVersion === 2) {\n const oldDetails = value as V2TokenDetails;\n\n if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {\n return;\n }\n\n tokenDetails = {\n token: oldDetails.fcmToken,\n createTime: oldDetails.createTime ?? Date.now(),\n subscriptionOptions: {\n auth: oldDetails.auth,\n p256dh: oldDetails.p256dh,\n endpoint: oldDetails.endpoint,\n swScope: oldDetails.swScope,\n vapidKey:\n typeof oldDetails.vapidKey === 'string'\n ? oldDetails.vapidKey\n : arrayToBase64(oldDetails.vapidKey)\n }\n };\n } else if (oldVersion === 3) {\n const oldDetails = value as V3TokenDetails;\n\n tokenDetails = {\n token: oldDetails.fcmToken,\n createTime: oldDetails.createTime,\n subscriptionOptions: {\n auth: arrayToBase64(oldDetails.auth),\n p256dh: arrayToBase64(oldDetails.p256dh),\n endpoint: oldDetails.endpoint,\n swScope: oldDetails.swScope,\n vapidKey: arrayToBase64(oldDetails.vapidKey)\n }\n };\n } else if (oldVersion === 4) {\n const oldDetails = value as V4TokenDetails;\n\n tokenDetails = {\n token: oldDetails.fcmToken,\n createTime: oldDetails.createTime,\n subscriptionOptions: {\n auth: arrayToBase64(oldDetails.auth),\n p256dh: arrayToBase64(oldDetails.p256dh),\n endpoint: oldDetails.endpoint,\n swScope: oldDetails.swScope,\n vapidKey: arrayToBase64(oldDetails.vapidKey)\n }\n };\n }\n }\n });\n db.close();\n\n // Delete all old databases.\n await deleteDB(OLD_DB_NAME);\n await deleteDB('fcm_vapid_details_db');\n await deleteDB('undefined');\n\n return checkTokenDetails(tokenDetails) ? tokenDetails : null;\n}\n\nfunction checkTokenDetails(\n tokenDetails: TokenDetails | null\n): tokenDetails is TokenDetails {\n if (!tokenDetails || !tokenDetails.subscriptionOptions) {\n return false;\n }\n const { subscriptionOptions } = tokenDetails;\n return (\n typeof tokenDetails.createTime === 'number' &&\n tokenDetails.createTime > 0 &&\n typeof tokenDetails.token === 'string' &&\n tokenDetails.token.length > 0 &&\n typeof subscriptionOptions.auth === 'string' &&\n subscriptionOptions.auth.length > 0 &&\n typeof subscriptionOptions.p256dh === 'string' &&\n subscriptionOptions.p256dh.length > 0 &&\n typeof subscriptionOptions.endpoint === 'string' &&\n subscriptionOptions.endpoint.length > 0 &&\n typeof subscriptionOptions.swScope === 'string' &&\n subscriptionOptions.swScope.length > 0 &&\n typeof subscriptionOptions.vapidKey === 'string' &&\n subscriptionOptions.vapidKey.length > 0\n );\n}\n","/**\n * @license\n * Copyright 2017 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ErrorFactory, ErrorMap } from '@firebase/util';\n\nexport const enum ErrorCode {\n MISSING_APP_CONFIG_VALUES = 'missing-app-config-values',\n AVAILABLE_IN_WINDOW = 'only-available-in-window',\n AVAILABLE_IN_SW = 'only-available-in-sw',\n PERMISSION_DEFAULT = 'permission-default',\n PERMISSION_BLOCKED = 'permission-blocked',\n UNSUPPORTED_BROWSER = 'unsupported-browser',\n INDEXED_DB_UNSUPPORTED = 'indexed-db-unsupported',\n FAILED_DEFAULT_REGISTRATION = 'failed-service-worker-registration',\n TOKEN_SUBSCRIBE_FAILED = 'token-subscribe-failed',\n TOKEN_SUBSCRIBE_NO_TOKEN = 'token-subscribe-no-token',\n FID_REGISTRATION_FAILED = 'fid-registration-failed',\n FID_UNREGISTER_FAILED = 'fid-unregister-failed',\n FID_REGISTRATION_IDB_SCHEMA_UNAVAILABLE = 'fid-registration-idb-schema-unavailable',\n TOKEN_UNSUBSCRIBE_FAILED = 'token-unsubscribe-failed',\n TOKEN_UPDATE_FAILED = 'token-update-failed',\n TOKEN_UPDATE_NO_TOKEN = 'token-update-no-token',\n INVALID_BG_HANDLER = 'invalid-bg-handler',\n USE_SW_AFTER_GET_TOKEN = 'use-sw-after-get-token',\n INVALID_SW_REGISTRATION = 'invalid-sw-registration',\n USE_VAPID_KEY_AFTER_GET_TOKEN = 'use-vapid-key-after-get-token',\n INVALID_VAPID_KEY = 'invalid-vapid-key',\n INVALID_ON_REGISTERED_HANDLER = 'invalid-on-registered-handler'\n}\n\nexport const ERROR_MAP: ErrorMap<ErrorCode> = {\n [ErrorCode.MISSING_APP_CONFIG_VALUES]:\n 'Missing App configuration value: \"{$valueName}\"',\n [ErrorCode.AVAILABLE_IN_WINDOW]:\n 'This method is available in a Window context.',\n [ErrorCode.AVAILABLE_IN_SW]:\n 'This method is available in a service worker context.',\n [ErrorCode.PERMISSION_DEFAULT]:\n 'The notification permission was not granted and dismissed instead.',\n [ErrorCode.PERMISSION_BLOCKED]:\n 'The notification permission was not granted and blocked instead.',\n [ErrorCode.UNSUPPORTED_BROWSER]:\n \"This browser doesn't support the API's required to use the Firebase SDK.\",\n [ErrorCode.INDEXED_DB_UNSUPPORTED]:\n \"This browser doesn't support indexedDb.open() (ex. Safari iFrame, Firefox Private Browsing, etc)\",\n [ErrorCode.FAILED_DEFAULT_REGISTRATION]:\n 'We are unable to register the default service worker. {$browserErrorMessage}',\n [ErrorCode.TOKEN_SUBSCRIBE_FAILED]:\n 'A problem occurred while subscribing the user to FCM: {$errorInfo}',\n [ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN]:\n 'FCM returned no token when subscribing the user to push.',\n [ErrorCode.FID_REGISTRATION_FAILED]:\n 'A problem occurred while creating an FCM registration via FID: {$errorInfo}',\n [ErrorCode.FID_UNREGISTER_FAILED]:\n 'A problem occurred while unregistering the FCM registration via FID: {$errorInfo}',\n [ErrorCode.FID_REGISTRATION_IDB_SCHEMA_UNAVAILABLE]:\n 'Unable to read or persist FID registration metadata because the messaging ' +\n 'IndexedDB schema is unavailable (for example, the database could not be ' +\n 'upgraded to the latest version).',\n [ErrorCode.TOKEN_UNSUBSCRIBE_FAILED]:\n 'A problem occurred while unsubscribing the ' +\n 'user from FCM: {$errorInfo}',\n [ErrorCode.TOKEN_UPDATE_FAILED]:\n 'A problem occurred while updating the user from FCM: {$errorInfo}',\n [ErrorCode.TOKEN_UPDATE_NO_TOKEN]:\n 'FCM returned no token when updating the user to push.',\n [ErrorCode.USE_SW_AFTER_GET_TOKEN]:\n 'The useServiceWorker() method may only be called once and must be ' +\n 'called before calling getToken() to ensure your service worker is used.',\n [ErrorCode.INVALID_SW_REGISTRATION]:\n 'The input to useServiceWorker() must be a ServiceWorkerRegistration.',\n [ErrorCode.INVALID_BG_HANDLER]:\n 'The input to setBackgroundMessageHandler() must be a function.',\n [ErrorCode.INVALID_VAPID_KEY]: 'The public VAPID key must be a string.',\n [ErrorCode.USE_VAPID_KEY_AFTER_GET_TOKEN]:\n 'The usePublicVapidKey() method may only be called once and must be ' +\n 'called before calling getToken() to ensure your VAPID key is used.',\n [ErrorCode.INVALID_ON_REGISTERED_HANDLER]:\n 'No onRegistered callback handler was provided or registered. Implement onRegistered() before register().'\n};\n\ninterface ErrorParams {\n [ErrorCode.MISSING_APP_CONFIG_VALUES]: {\n valueName: string;\n };\n [ErrorCode.FAILED_DEFAULT_REGISTRATION]: { browserErrorMessage: string };\n [ErrorCode.TOKEN_SUBSCRIBE_FAILED]: { errorInfo: string };\n [ErrorCode.FID_REGISTRATION_FAILED]: { errorInfo: string };\n [ErrorCode.FID_UNREGISTER_FAILED]: { errorInfo: string };\n [ErrorCode.TOKEN_UNSUBSCRIBE_FAILED]: { errorInfo: string };\n [ErrorCode.TOKEN_UPDATE_FAILED]: { errorInfo: string };\n}\n\nexport const ERROR_FACTORY = new ErrorFactory<ErrorCode, ErrorParams>(\n 'messaging',\n 'Messaging',\n ERROR_MAP\n);\n","/**\n * @license\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { deleteDB, openDB } from 'idb';\nimport type { DBSchema, IDBPDatabase, OpenDBCallbacks } from 'idb';\n\nimport { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';\nimport { TokenDetails } from '../interfaces/registration-details';\nimport { migrateOldDatabase } from '../helpers/migrate-old-database';\nimport { ERROR_FACTORY, ErrorCode } from '../util/errors';\n\nexport const DATABASE_NAME = 'firebase-messaging-database';\nconst DATABASE_VERSION = 2;\nconst TOKEN_OBJECT_STORE_NAME = 'firebase-messaging-store';\nconst FID_REGISTRATION_OBJECT_STORE_NAME =\n 'firebase-messaging-fid-registration-store';\n\ninterface MessagingDB extends DBSchema {\n 'firebase-messaging-store': {\n key: string;\n value: TokenDetails;\n };\n 'firebase-messaging-fid-registration-store': {\n key: string;\n value: FidRegistrationDetails;\n };\n}\n\nexport interface FidRegistrationDetails {\n fid: string;\n lastRegisterTime: number;\n vapidKey?: string;\n}\n\ninterface IdbImpl {\n openDB: typeof openDB;\n deleteDB: typeof deleteDB;\n}\n\nconst defaultIdb: IdbImpl = { openDB, deleteDB };\nlet idbImpl: IdbImpl = defaultIdb;\n\n// Exported for tests.\nexport function _setIdbForTests(impl: IdbImpl): void {\n idbImpl = impl;\n}\n\nexport function _resetIdbForTests(): void {\n idbImpl = defaultIdb;\n}\n\n// Open v2, but fall back to v1 if upgrade/open fails. Cache as `unknown` and guard store access.\nlet dbPromise: Promise<IDBPDatabase<unknown>> | null = null;\n\nfunction migrateMessagingDb(\n upgradeDb: IDBPDatabase<unknown>,\n oldVersion: number,\n targetSchemaVersion: 1 | 2\n): void {\n // Intentional fall-through for v2: run all intermediate migrations.\n // eslint-disable-next-line default-case\n switch (oldVersion) {\n case 0:\n upgradeDb.createObjectStore(TOKEN_OBJECT_STORE_NAME);\n if (targetSchemaVersion === 1) {\n break;\n }\n // fall through\n case 1:\n if (targetSchemaVersion === 2) {\n upgradeDb.createObjectStore(FID_REGISTRATION_OBJECT_STORE_NAME);\n }\n }\n}\n\nfunction createOpenDbOptions(\n targetSchemaVersion: 1 | 2\n): OpenDBCallbacks<unknown> {\n return {\n upgrade: (upgradeDb: IDBPDatabase<unknown>, oldVersion: number) => {\n migrateMessagingDb(upgradeDb, oldVersion, targetSchemaVersion);\n },\n blocked: () => {\n /* no-op */\n },\n blocking: (\n _currentVersion: number,\n _blockedVersion: number | null,\n event: IDBVersionChangeEvent\n ) => {\n dbPromise = null;\n (event.target as IDBDatabase | null)?.close();\n },\n terminated: () => {\n dbPromise = null;\n }\n };\n}\n\nfunction getDbPromise(): Promise<IDBPDatabase<MessagingDB>> {\n if (!dbPromise) {\n const openLatest = idbImpl.openDB(\n DATABASE_NAME,\n DATABASE_VERSION,\n createOpenDbOptions(2)\n );\n\n // Assign synchronously to avoid concurrent openDB() calls.\n dbPromise = (openLatest as unknown as Promise<IDBPDatabase<unknown>>).catch(\n () =>\n idbImpl.openDB(\n DATABASE_NAME,\n DATABASE_VERSION - 1,\n createOpenDbOptions(1)\n ) as unknown as Promise<IDBPDatabase<unknown>>\n );\n }\n return dbPromise as Promise<IDBPDatabase<MessagingDB>>;\n}\n\nfunction hasObjectStore(db: IDBPDatabase<unknown>, storeName: string): boolean {\n return db.objectStoreNames.contains(storeName);\n}\n\nfunction assertFidRegistrationObjectStore(db: IDBPDatabase<MessagingDB>): void {\n if (\n !hasObjectStore(\n db as unknown as IDBPDatabase<unknown>,\n FID_REGISTRATION_OBJECT_STORE_NAME\n )\n ) {\n throw ERROR_FACTORY.create(\n ErrorCode.FID_REGISTRATION_IDB_SCHEMA_UNAVAILABLE\n );\n }\n}\n\nexport async function dbGet(\n firebaseDependencies: FirebaseInternalDependencies\n): Promise<TokenDetails | undefined> {\n const key = getKey(firebaseDependencies);\n const db = await getDbPromise();\n const tokenDetails = (await db\n .transaction(TOKEN_OBJECT_STORE_NAME)\n .objectStore(TOKEN_OBJECT_STORE_NAME)\n .get(key)) as TokenDetails;\n\n if (tokenDetails) {\n return tokenDetails;\n } else {\n const oldTokenDetails = await migrateOldDatabase(\n firebaseDependencies.appConfig.senderId\n );\n if (oldTokenDetails) {\n await dbSet(firebaseDependencies, oldTokenDetails);\n return oldTokenDetails;\n }\n }\n}\n\nexport async function dbSet(\n firebaseDependencies: FirebaseInternalDependencies,\n tokenDetails: TokenDetails\n): Promise<TokenDetails> {\n const key = getKey(firebaseDependencies);\n const db = await getDbPromise();\n\n const stores: Array<\n typeof TOKEN_OBJECT_STORE_NAME | typeof FID_REGISTRATION_OBJECT_STORE_NAME\n > = [TOKEN_OBJECT_STORE_NAME];\n const hasFidStore = hasObjectStore(\n db as unknown as IDBPDatabase<unknown>,\n FID_REGISTRATION_OBJECT_STORE_NAME\n );\n if (hasFidStore) {\n stores.push(FID_REGISTRATION_OBJECT_STORE_NAME);\n }\n\n const tx = db.transaction(stores, 'readwrite');\n await tx.objectStore(TOKEN_OBJECT_STORE_NAME).put(tokenDetails, key);\n if (hasFidStore) {\n await tx.objectStore(FID_REGISTRATION_OBJECT_STORE_NAME).delete(key);\n }\n await tx.done;\n\n return tokenDetails;\n}\n\nexport async function dbRemove(\n firebaseDependencies: FirebaseInternalDependencies\n): Promise<void> {\n const key = getKey(firebaseDependencies);\n const db = await getDbPromise();\n const tx = db.transaction(TOKEN_OBJECT_STORE_NAME, 'readwrite');\n await tx.objectStore(TOKEN_OBJECT_STORE_NAME).delete(key);\n await tx.done;\n}\n\nexport async function dbGetFidRegistration(\n firebaseDependencies: FirebaseInternalDependencies\n): Promise<FidRegistrationDetails | undefined> {\n const key = getKey(firebaseDependencies);\n const db = await getDbPromise();\n assertFidRegistrationObjectStore(db);\n return (await db\n .transaction(FID_REGISTRATION_OBJECT_STORE_NAME)\n .objectStore(FID_REGISTRATION_OBJECT_STORE_NAME)\n .get(key)) as FidRegistrationDetails | undefined;\n}\n\nexport async function dbSetFidRegistration(\n firebaseDependencies: FirebaseInternalDependencies,\n details: FidRegistrationDetails\n): Promise<FidRegistrationDetails> {\n const key = getKey(firebaseDependencies);\n const db = await getDbPromise();\n assertFidRegistrationObjectStore(db);\n\n const tx = db.transaction(\n [TOKEN_OBJECT_STORE_NAME, FID_REGISTRATION_OBJECT_STORE_NAME],\n 'readwrite'\n );\n await tx.objectStore(FID_REGISTRATION_OBJECT_STORE_NAME).put(details, key);\n await tx.objectStore(TOKEN_OBJECT_STORE_NAME).delete(key);\n await tx.done;\n\n return details;\n}\n\nexport async function dbRemoveFidRegistration(\n firebaseDependencies: FirebaseInternalDependencies\n): Promise<void> {\n const key = getKey(firebaseDependencies);\n const db = await getDbPromise();\n assertFidRegistrationObjectStore(db);\n const tx = db.transaction(FID_REGISTRATION_OBJECT_STORE_NAME, 'readwrite');\n await tx.objectStore(FID_REGISTRATION_OBJECT_STORE_NAME).delete(key);\n await tx.done;\n}\n\n/** Deletes the DB. Useful for tests. */\nexport async function dbDelete(): Promise<void> {\n const promise = dbPromise;\n dbPromise = null;\n\n try {\n if (promise) {\n (await promise).close();\n }\n } catch {\n // Ignore open failures; deleting the DB is the recovery mechanism.\n } finally {\n await idbImpl.deleteDB(DATABASE_NAME);\n }\n}\n\nfunction getKey({ appConfig }: FirebaseInternalDependencies): string {\n return appConfig.appId;\n}\n","/**\n * @license\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DEFAULT_VAPID_KEY, ENDPOINT } from '../util/constants';\nimport { ERROR_FACTORY, ErrorCode } from '../util/errors';\nimport {\n SubscriptionOptions,\n TokenDetails\n} from '../interfaces/registration-details';\n\nimport { AppConfig } from '../interfaces/app-config';\nimport { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';\nimport { version as fcmSdkVersion } from '../../package.json';\n\n/** Max attempts (initial fetch + retries) when CreateRegistration `fetch()` throws. */\nexport const FID_REGISTRATION_FETCH_MAX_ATTEMPTS = 3;\n\n/** Base delay in ms; backoff is `BASE * 2^attempt` after each failed attempt. */\nexport const FID_REGISTRATION_FETCH_BASE_BACKOFF_MS = 1000;\n\nexport interface ApiResponse {\n token?: string;\n /**\n * CreateRegistration resource name, e.g. `projects/{projectId}/registrations/{fid}`.\n */\n name?: string;\n error?: { message: string };\n}\n\nexport interface ApiRequestBody {\n // eslint-disable-next-line camelcase\n fcm_sdk_version?: string;\n web: {\n /**\n * Client identifier for the registration: the site host (e.g. `www.example.com`) when the\n * service worker scope is a URL, otherwise the app name.\n */\n origin: string;\n endpoint: string;\n p256dh: string;\n auth: string;\n applicationPubKey?: string;\n };\n}\n\nexport async function requestGetToken(\n firebaseDependencies: FirebaseInternalDependencies,\n subscriptionOptions: SubscriptionOptions\n): Promise<string> {\n const headers = await getHeaders(firebaseDependencies);\n const body = getBody(\n subscriptionOptions,\n firebaseDependencies.appConfig.appName,\n /* includeSdkVersion= */ false\n );\n\n const subscribeOptions = {\n method: 'POST',\n headers,\n body: JSON.stringify(body)\n };\n\n let responseData: ApiResponse;\n try {\n const response = await fetch(\n getEndpoint(firebaseDependencies.appConfig),\n subscribeOptions\n );\n responseData = await response.json();\n } catch (err) {\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_SUBSCRIBE_FAILED, {\n errorInfo: (err as Error)?.toString()\n });\n }\n\n if (responseData.error) {\n const message = responseData.error.message;\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_SUBSCRIBE_FAILED, {\n errorInfo: message\n });\n }\n\n if (!responseData.token) {\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN);\n }\n\n return responseData.token;\n}\n\n/**\n * Creates (or refreshes) an FCM Web registration via CreateRegistration.\n *\n * This is used by the FID-based register path, where we don't require the returned FCM token, but\n * we do require a non-empty `name` (echoing the Firebase Installation ID) in the success response body.\n */\nexport interface CreateRegistrationResult {\n /** Firebase Installation ID parsed from the CreateRegistration response `name` field. */\n responseFid: string;\n}\n\nexport async function requestCreateRegistration(\n firebaseDependencies: FirebaseInternalDependencies,\n subscriptionOptions: SubscriptionOptions\n): Promise<CreateRegistrationResult> {\n const headers = await getHeaders(firebaseDependencies);\n const body = getBody(\n subscriptionOptions,\n firebaseDependencies.appConfig.appName,\n /* includeSdkVersion= */ true\n );\n\n const subscribeOptions = {\n method: 'POST',\n headers,\n body: JSON.stringify(body)\n };\n\n let response: Response;\n try {\n response = await fetchWithExponentialRetry(\n () =>\n fetch(getEndpoint(firebaseDependencies.appConfig), subscribeOptions),\n FID_REGISTRATION_FETCH_MAX_ATTEMPTS,\n FID_REGISTRATION_FETCH_BASE_BACKOFF_MS\n );\n } catch (err) {\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo: (err as Error)?.toString()\n });\n }\n\n if (response.ok) {\n const responseFid = await parseCreateRegistrationSuccessFid(response);\n return { responseFid };\n }\n\n // `fetch()` succeeded, but the backend returned a non-2xx response.\n // Best-effort parse the body to extract `error.message`, but always fail with\n // `FID_REGISTRATION_FAILED` to keep the error surface uniform.\n // Best-effort extraction of error details; the main signal is response.ok / status.\n let responseData: ApiResponse;\n try {\n responseData = (await response.json()) as ApiResponse;\n } catch (err) {\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo: response.statusText\n });\n }\n const message = responseData.error?.message ?? response.statusText;\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo: message\n });\n}\n\n/**\n * Deletes an FCM Web registration via DeleteRegistration using the Firebase Installation ID (FID).\n */\nexport async function requestDeleteRegistration(\n firebaseDependencies: FirebaseInternalDependencies,\n fid: string\n): Promise<void> {\n const headers = await getHeaders(firebaseDependencies);\n\n const options: RequestInit = {\n method: 'DELETE',\n headers\n };\n\n let response: Response;\n try {\n response = await fetch(\n `${getEndpoint(firebaseDependencies.appConfig)}/${fid}`,\n options\n );\n } catch (err) {\n throw ERROR_FACTORY.create(ErrorCode.FID_UNREGISTER_FAILED, {\n errorInfo: (err as Error)?.toString()\n });\n }\n\n if (response.ok) {\n return;\n }\n\n // Best-effort parse error details; surface uniform error code.\n try {\n const responseData = (await response.json()) as ApiResponse;\n const message = responseData.error?.message ?? response.statusText;\n throw message;\n } catch (err) {\n // If parsing failed, fall back to status text.\n throw ERROR_FACTORY.create(ErrorCode.FID_UNREGISTER_FAILED, {\n errorInfo:\n (typeof err === 'string' && err) ||\n response.statusText ||\n (err as Error)?.toString()\n });\n }\n}\n\n/**\n * Parses a successful CreateRegistration body. The backend must return JSON with a non-empty\n * string `name`: a resource name `projects/{projectId}/registrations/{fid}`\n */\nasync function parseCreateRegistrationSuccessFid(\n response: Response\n): Promise<string> {\n const text = await response.text();\n if (!text.trim()) {\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo: 'CreateRegistration succeeded but response body is empty'\n });\n }\n let data: ApiResponse;\n try {\n data = JSON.parse(text) as ApiResponse;\n } catch {\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo:\n 'CreateRegistration succeeded but response body is not valid JSON'\n });\n }\n const name = data.name;\n if (typeof name !== 'string' || name.length === 0) {\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo:\n 'CreateRegistration succeeded but response did not include a non-empty name'\n });\n }\n return parseFidFromRegistrationResourceName(name);\n}\n\nconst REGISTRATIONS_NAME_SEGMENT = '/registrations/';\n\n/** Extracts the Firebase Installation ID from CreateRegistration `name` (resource path). */\nfunction parseFidFromRegistrationResourceName(name: string): string {\n const segmentIndex = name.indexOf(REGISTRATIONS_NAME_SEGMENT);\n if (segmentIndex !== -1) {\n const fid = name.slice(segmentIndex + REGISTRATIONS_NAME_SEGMENT.length);\n if (fid.length > 0) {\n return fid;\n }\n }\n throw ERROR_FACTORY.create(ErrorCode.FID_REGISTRATION_FAILED, {\n errorInfo:\n 'CreateRegistration succeeded but response name is not a valid registration resource name'\n });\n}\n\nexport async function requestUpdateToken(\n firebaseDependencies: FirebaseInternalDependencies,\n tokenDetails: TokenDetails\n): Promise<string> {\n const headers = await getHeaders(firebaseDependencies);\n const body = getBody(\n tokenDetails.subscriptionOptions!,\n firebaseDependencies.appConfig.appName,\n /* includeSdkVersion= */ false\n );\n\n const updateOptions = {\n method: 'PATCH',\n headers,\n body: JSON.stringify(body)\n };\n\n let responseData: ApiResponse;\n try {\n const response = await fetch(\n `${getEndpoint(firebaseDependencies.appConfig)}/${tokenDetails.token}`,\n updateOptions\n );\n responseData = await response.json();\n } catch (err) {\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_UPDATE_FAILED, {\n errorInfo: (err as Error)?.toString()\n });\n }\n\n if (responseData.error) {\n const message = responseData.error.message;\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_UPDATE_FAILED, {\n errorInfo: message\n });\n }\n\n if (!responseData.token) {\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_UPDATE_NO_TOKEN);\n }\n\n return responseData.token;\n}\n\nexport async function requestDeleteToken(\n firebaseDependencies: FirebaseInternalDependencies,\n token: string\n): Promise<void> {\n const headers = await getHeaders(firebaseDependencies);\n\n const unsubscribeOptions = {\n method: 'DELETE',\n headers\n };\n\n try {\n const response = await fetch(\n `${getEndpoint(firebaseDependencies.appConfig)}/${token}`,\n unsubscribeOptions\n );\n const responseData: ApiResponse = await response.json();\n if (responseData.error) {\n const message = responseData.error.message;\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_UNSUBSCRIBE_FAILED, {\n errorInfo: message\n });\n }\n } catch (err) {\n throw ERROR_FACTORY.create(ErrorCode.TOKEN_UNSUBSCRIBE_FAILED, {\n errorInfo: (err as Error)?.toString()\n });\n }\n}\n\n/**\n * Re-runs `operation` when it throws, with exponential backoff between attempts.\n * Rethrows the last error if all attempts fail.\n */\nasync function fetchWithExponentialRetry(\n operation: () => Promise<Response>,\n maxAttempts: number,\n baseBackoffMs: number\n): Promise<Response> {\n let lastError: unknown;\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n try {\n return await operation();\n } catch (err) {\n lastError = err;\n if (attempt < maxAttempts - 1) {\n const delayMs = baseBackoffMs * Math.pow(2, attempt);\n await new Promise<void>(resolve => setTimeout(resolve, delayMs));\n }\n }\n }\n throw lastError;\n}\n\nfunction getEndpoint({ projectId }: AppConfig): string {\n return `${ENDPOINT}/projects/${projectId!}/registrations`;\n}\n\nasync function getHeaders({\n appConfig,\n installations\n}: FirebaseInternalDependencies): Promise<Headers> {\n const authToken = await installations.getToken();\n\n return new Headers({\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'x-goog-api-key': appConfig.apiKey!,\n 'x-goog-firebase-installations-auth': `FIS ${authToken}`\n });\n}\n\n/**\n * Hostname for the registering web client (e.g. `www.example.com`), or the app name\n * (`appNameFallback`) when the scope cannot be resolved (e.g. some test environments).\n */\nexport function getRegistrationOrigin(\n swScope: string,\n appNameFallback: string\n): string {\n try {\n if (/^[a-zA-Z][a-zA-Z\\d+\\-.]*:/.test(swScope)) {\n return new URL(swScope).host;\n }\n } catch {\n // Fall through to relative-scope handling.\n }\n try {\n if (typeof self !== 'undefined' && self.location?.href) {\n return new URL(swScope, self.location.origin).host;\n }\n } catch {\n // Fall through.\n }\n if (typeof self !== 'undefined' && self.location?.host) {\n return self.location.host;\n }\n return appNameFallback;\n}\n\nfunction getBody(\n { p256dh, auth, endpoint, vapidKey, swScope }: SubscriptionOptions,\n appNameFallback: string,\n includeSdkVersion: boolean\n): ApiRequestBody {\n const body: ApiRequestBody = {\n web: {\n origin: getRegistrationOrigin(swScope, appNameFallback),\n endpoint,\n auth,\n p256dh\n }\n };\n\n if (includeSdkVersion) {\n // eslint-disable-next-line camelcase\n body.fcm_sdk_version = fcmSdkVersion;\n }\n\n if (vapidKey !== DEFAULT_VAPID_KEY) {\n body.web.applicationPubKey = vapidKey;\n }\n\n return body;\n}\n","/**\n * @license\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n SubscriptionOptions,\n TokenDetails\n} from '../interfaces/registration-details';\nimport {\n arrayToBase64,\n base64ToArray\n} from '../helpers/array-base64-translator';\nimport {\n dbGet,\n dbGetFidRegistration,\n dbRemove,\n dbRemoveFidRegistration,\n dbSet\n} from './idb-manager';\nimport {\n requestDeleteRegistration,\n requestDeleteToken,\n requestGetToken,\n requestUpdateToken\n} from './requests';\n\nimport { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';\nimport { MessagingService } from '../messaging-service';\n\n// UpdateRegistration will be called once every week.\nconst TOKEN_EXPIRATION_MS = 7 * 24 * 60 * 60 * 1000; // 7 days\n\nexport async function getTokenInternal(\n messaging: MessagingService\n): Promise<string> {\n const pushSubscription = await getPushSubscription(\n messaging.swRegistration!,\n messaging.vapidKey!\n );\n\n const subscriptionOptions: SubscriptionOptions = {\n vapidKey: messaging.vapidKey!,\n swScope: messaging.swRegistration!.scope,\n endpoint: pushSubscription.endpoint,\n auth: arrayToBase64(pushSubscription.getKey('auth')!),\n p256dh: arrayToBase64(pushSubscription.getKey('p256dh')!)\n };\n\n const tokenDetails = await dbGet(messaging.firebaseDependencies);\n if (!tokenDetails) {\n // No token, get a new one.\n return getNewToken(messaging.firebaseDependencies, subscriptionOptions);\n } else if (\n !isTokenValid(tokenDetails.subscriptionOptions!, subscriptionOptions)\n ) {\n // Invalid token, get a new one.\n try {\n await requestDeleteToken(\n messaging.firebaseDependencies!,\n tokenDetails.token\n );\n } catch (e) {\n // Suppress errors because of #2364\n console.warn(e);\n }\n\n return getNewToken(messaging.firebaseDependencies!, subscriptionOptions);\n } else if (Date.now() >= tokenDetails.createTime + TOKEN_EXPIRATION_MS) {\n // Weekly token refresh\n return updateToken(messaging, {\n token: tokenDetails.token,\n createTime: Date.now(),\n subscriptionOptions\n });\n } else {\n // Valid token, nothing to do.\n return tokenDetails.token;\n }\n}\n\n/**\n * Legacy getToken() path: there is a token row in IndexedDB. Revoke it with FCM, drop the row, and\n * clear any leftover FID registration metadata (apps may mix APIs).\n */\nasync function revokeLegacyFcmTokenAndClearCaches(\n messaging: MessagingService,\n tokenDetails: TokenDetails\n): Promise<void> {\n await requestDeleteToken(messaging.firebaseDependencies, tokenDetails.token);\n await dbRemove(messaging.firebaseDependencies);\n await removeFidRegistrationBestEffort(messaging.firebaseDependencies);\n}\n\n/**\n * No legacy token row: the client may only have FID-based registration (register() flow). If so,\n * delete that registration on the server, always scrub local FID metadata, then surface\n * onUnregistered when we actually had an FID.\n */\nasync function revokeFidRegistrationIfStored(\n messaging: MessagingService\n): Promise<void> {\n const stored = await dbGetFidRegistration(\n messaging.firebaseDependencies\n ).catch(() => undefined);\n const fid = stored?.fid;\n\n if (fid) {\n await requestDeleteRegistration(messaging.firebaseDependencies, fid);\n }\n\n await removeFidRegistrationBestEffort(messaging.firebaseDependencies);\n\n if (fid) {\n notifyOnUnregistered(messaging, fid);\n }\n}\n\n/**\n * Revokes the app's FCM registration: legacy token (getToken/deleteToken) and/or FID-based\n * registration (register/unregister), clears local caches, notifies onUnregistered when a stored\n * FID existed, then unsubscribes the push subscription when present.\n */\nexport async function revokeRegistrationInternal(\n messaging: MessagingService\n): Promise<boolean> {\n const tokenDetails = await dbGet(messaging.firebaseDependencies);\n if (tokenDetails) {\n await revokeLegacyFcmTokenAndClearCaches(messaging, tokenDetails);\n } else {\n await revokeFidRegistrationIfStored(messaging);\n }\n\n // Unsubscribe from the push subscription.\n const pushSubscription =\n await messaging.swRegistration!.pushManager.getSubscription();\n if (pushSubscription) {\n return pushSubscription.unsubscribe();\n }\n\n // If there's no SW, consider it a success.\n return true;\n}\n\nasync function updateToken(\n messaging: MessagingService,\n tokenDetails: TokenDetails\n): Promise<string> {\n try {\n const updatedToken = await requestUpdateToken(\n messaging.firebaseDependencies,\n tokenDetails\n );\n\n const updatedTokenDetails: TokenDetails = {\n ...tokenDetails,\n token: updatedToken,\n createTime: Date.now()\n };\n\n await dbSet(messaging.firebaseDependencies, updatedTokenDetails);\n return updatedToken;\n } catch (e) {\n throw e;\n }\n}\n\nasync function getNewToken(\n firebaseDependencies: FirebaseInternalDependencies,\n subscriptionOptions: SubscriptionOptions\n): Promise<string> {\n const token = await requestGetToken(\n firebaseDependencies,\n subscriptionOptions\n );\n const tokenDetails: TokenDetails = {\n token,\n createTime: Date.now(),\n subscriptionOptions\n };\n await dbSet(firebaseDependencies, tokenDetails);\n return tokenDetails.token;\n}\n\n/**\n * Gets a PushSubscription for the current user.\n */\nasync function getPushSubscription(\n swRegistration: ServiceWorkerRegistration,\n vapidKey: string\n): Promise<PushSubscription> {\n const subscription = await swRegistration.pushManager.getSubscription();\n if (subscription) {\n return subscription;\n }\n\n return swRegistration.pushManager.subscribe({\n userVisibleOnly: true,\n // Chrome <= 75 doesn't support base64-encoded VAPID key. For backward compatibility, VAPID key\n // submitted to pushManager#subscribe must be of type Uint8Array.\n applicationServerKey: base64ToArray(vapidKey)\n });\n}\n\n/**\n * Checks if the saved tokenDetails object matches the configuration provided.\n */\nfunction isTokenValid(\n dbOptions: SubscriptionOptions,\n currentOptions: SubscriptionOptions\n): boolean {\n const isVapidKeyEqual = currentOptions.vapidKey === dbOptions.vapidKey;\n const isEndpointEqual = currentOptions.endpoint === dbOptions.endpoint;\n const isAuthEqual = currentOptions.auth === dbOptions.auth;\n const isP256dhEqual = currentOptions.p256dh === dbOptions.p256dh;\n\n return isVapidKeyEqual && isEndpointEqual && isAuthEqual && isP256dhEqual;\n}\n\n/** Clears FID registration metadata; apps may mix legacy getToken() with FID register/unregister. */\nasync function removeFidRegistrationBestEffort(\n firebaseDependencies: FirebaseInternalDependencies\n): Promise<void> {\n try {\n await dbRemoveFidRegistration(firebaseDependencies);\n } catch {\n // Ignore.\n }\n}\n\nexport function notifyOnRegistered(\n messaging: MessagingService,\n fid: string\n): void {\n const handler = messaging.onRegisteredHandler;\n if (!handler) {\n return;\n }\n if (typeof handler === 'function') {\n handler(fid);\n } else {\n handler.next(fid);\n }\n}\n\nexport function notifyOnUnregistered(\n messaging: MessagingService,\n fid: string\n): void {\n const handler = messaging.onUnregisteredHandler;\n if (!handler) {\n return;\n }\n if (typeof handler === 'function') {\n handler(fid);\n } else {\n handler.next(fid);\n }\n}\n","/**\n * @license\n * Copyright 2020 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DEFAULT_VAPID_KEY } from '../util/constants';\nimport { MessagingService } from '../messaging-service';\n\nexport async function updateVapidKey(\n messaging: MessagingService,\n vapidKey?: string | undefined\n): Promise<void> {\n if (!!vapidKey) {\n messaging.vapidKey = vapidKey;\n } else if (!messaging.vapidKey) {\n messaging.vapidKey = DEFAULT_VAPID_KEY;\n }\n}\n","/**\n * @license\n * Copyright 2020 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a c