From 2d3c6991755bcc1bac5a1122aca784beca920f17 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Wed, 5 Jan 2022 12:44:29 -0800 Subject: [PATCH 1/4] Upgrade idb in installations --- packages/installations-compat/package.json | 2 +- packages/installations/package.json | 2 +- .../installations/src/helpers/idb-manager.ts | 22 +++++++++---------- packages/messaging/package.json | 2 +- yarn.lock | 8 +++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/installations-compat/package.json b/packages/installations-compat/package.json index 158c698691d..8725c245e21 100644 --- a/packages/installations-compat/package.json +++ b/packages/installations-compat/package.json @@ -61,7 +61,7 @@ "@firebase/installations-types": "0.4.0", "@firebase/util": "1.4.3", "@firebase/component": "0.5.10", - "idb": "3.0.2", + "idb": "7.0.0", "tslib": "^2.1.0" } } \ No newline at end of file diff --git a/packages/installations/package.json b/packages/installations/package.json index 3af4639ff58..c8c7a962375 100644 --- a/packages/installations/package.json +++ b/packages/installations/package.json @@ -64,7 +64,7 @@ "dependencies": { "@firebase/util": "1.4.3", "@firebase/component": "0.5.10", - "idb": "3.0.2", + "idb": "7.0.0", "tslib": "^2.1.0" } } \ No newline at end of file diff --git a/packages/installations/src/helpers/idb-manager.ts b/packages/installations/src/helpers/idb-manager.ts index bc30563fa06..1233bb27b58 100644 --- a/packages/installations/src/helpers/idb-manager.ts +++ b/packages/installations/src/helpers/idb-manager.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { DB, openDb } from 'idb'; +import { IDBPDatabase, openDB } from 'idb'; import { AppConfig } from '../interfaces/installation-impl'; import { InstallationEntry } from '../interfaces/installation-entry'; import { getKey } from '../util/get-key'; @@ -25,20 +25,20 @@ const DATABASE_NAME = 'firebase-installations-database'; const DATABASE_VERSION = 1; const OBJECT_STORE_NAME = 'firebase-installations-store'; -let dbPromise: Promise | null = null; -function getDbPromise(): Promise { +let dbPromise: Promise | null = null; +function getDbPromise(): Promise { if (!dbPromise) { - dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => { + dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {upgrade: (db, oldVersion) => { // We don't use 'break' in this switch statement, the fall-through // behavior is what we want, because if there are multiple versions between // the old version and the current version, we want ALL the migrations // that correspond to those versions to run, not only the last one. // eslint-disable-next-line default-case - switch (upgradeDB.oldVersion) { + switch (oldVersion) { case 0: - upgradeDB.createObjectStore(OBJECT_STORE_NAME); + db.createObjectStore(OBJECT_STORE_NAME); } - }); + }}); } return dbPromise; } @@ -66,7 +66,7 @@ export async function set( const objectStore = tx.objectStore(OBJECT_STORE_NAME); const oldValue = await objectStore.get(key); await objectStore.put(value, key); - await tx.complete; + await tx.done; if (!oldValue || oldValue.fid !== value.fid) { fidChanged(appConfig, value.fid); @@ -81,7 +81,7 @@ export async function remove(appConfig: AppConfig): Promise { const db = await getDbPromise(); const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite'); await tx.objectStore(OBJECT_STORE_NAME).delete(key); - await tx.complete; + await tx.done; } /** @@ -106,7 +106,7 @@ export async function update( } else { await store.put(newValue, key); } - await tx.complete; + await tx.done; if (newValue && (!oldValue || oldValue.fid !== newValue.fid)) { fidChanged(appConfig, newValue.fid); @@ -119,5 +119,5 @@ export async function clear(): Promise { const db = await getDbPromise(); const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite'); await tx.objectStore(OBJECT_STORE_NAME).clear(); - await tx.complete; + await tx.done; } diff --git a/packages/messaging/package.json b/packages/messaging/package.json index 56bbbcef088..37b60521745 100644 --- a/packages/messaging/package.json +++ b/packages/messaging/package.json @@ -51,7 +51,7 @@ "@firebase/messaging-interop-types": "0.1.0", "@firebase/util": "1.4.3", "@firebase/component": "0.5.10", - "idb": "3.0.2", + "idb": "7.0.0", "tslib": "^2.1.0" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index fbcc82f6634..885224b7670 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8972,10 +8972,10 @@ iconv-lite@0.6.3, iconv-lite@^0.6.2: dependencies: safer-buffer ">= 2.1.2 < 3.0.0" -idb@3.0.2: - version "3.0.2" - resolved "https://registry.npmjs.org/idb/-/idb-3.0.2.tgz#c8e9122d5ddd40f13b60ae665e4862f8b13fa384" - integrity sha512-+FLa/0sTXqyux0o6C+i2lOR0VoS60LU/jzUo5xjfY6+7sEEgy4Gz1O7yFBXvjd7N0NyIGWIRg8DcQSLEG+VSPw== +idb@7.0.0: + version "7.0.0" + resolved "https://registry.npmjs.org/idb/-/idb-7.0.0.tgz#f349b418c128f625961147a7d6b0e4b526fd34ed" + integrity sha512-jSx0WOY9Nj+QzP6wX5e7g64jqh8ExtDs/IAuOrOEZCD/h6+0HqyrKsDMfdJc0hqhSvh0LsrwqrkDn+EtjjzSRA== ieee754@^1.1.13, ieee754@^1.1.4: version "1.2.1" From 789053d69eed28432c6e7631fefa563f873d3851 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Wed, 5 Jan 2022 12:52:58 -0800 Subject: [PATCH 2/4] Upgrade idb in messaging --- .../installations/src/helpers/idb-manager.ts | 22 +-- .../src/helpers/migrate-old-database.test.ts | 30 ++-- .../src/helpers/migrate-old-database.ts | 134 +++++++++--------- .../messaging/src/internals/idb-manager.ts | 30 ++-- packages/messaging/src/testing/setup.ts | 4 +- 5 files changed, 114 insertions(+), 106 deletions(-) diff --git a/packages/installations/src/helpers/idb-manager.ts b/packages/installations/src/helpers/idb-manager.ts index 1233bb27b58..df9fe3a22cd 100644 --- a/packages/installations/src/helpers/idb-manager.ts +++ b/packages/installations/src/helpers/idb-manager.ts @@ -28,17 +28,19 @@ const OBJECT_STORE_NAME = 'firebase-installations-store'; let dbPromise: Promise | null = null; function getDbPromise(): Promise { if (!dbPromise) { - dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {upgrade: (db, oldVersion) => { - // We don't use 'break' in this switch statement, the fall-through - // behavior is what we want, because if there are multiple versions between - // the old version and the current version, we want ALL the migrations - // that correspond to those versions to run, not only the last one. - // eslint-disable-next-line default-case - switch (oldVersion) { - case 0: - db.createObjectStore(OBJECT_STORE_NAME); + dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, { + upgrade: (db, oldVersion) => { + // We don't use 'break' in this switch statement, the fall-through + // behavior is what we want, because if there are multiple versions between + // the old version and the current version, we want ALL the migrations + // that correspond to those versions to run, not only the last one. + // eslint-disable-next-line default-case + switch (oldVersion) { + case 0: + db.createObjectStore(OBJECT_STORE_NAME); + } } - }}); + }); } return dbPromise; } diff --git a/packages/messaging/src/helpers/migrate-old-database.test.ts b/packages/messaging/src/helpers/migrate-old-database.test.ts index 020295ca2fd..038297a4c24 100644 --- a/packages/messaging/src/helpers/migrate-old-database.test.ts +++ b/packages/messaging/src/helpers/migrate-old-database.test.ts @@ -28,7 +28,7 @@ import { FakePushSubscription } from '../testing/fakes/service-worker'; import { base64ToArray } from './array-base64-translator'; import { expect } from 'chai'; import { getFakeTokenDetails } from '../testing/fakes/token-details'; -import { openDb } from 'idb'; +import { openDB } from 'idb'; describe('migrateOldDb', () => { it("does nothing if old DB didn't exist", async () => { @@ -179,25 +179,27 @@ describe('migrateOldDb', () => { }); async function put(version: number, value: object): Promise { - const db = await openDb('fcm_token_details_db', version, upgradeDb => { - if (upgradeDb.oldVersion === 0) { - const objectStore = upgradeDb.createObjectStore( - 'fcm_token_object_Store', - { - keyPath: 'swScope' - } - ); - objectStore.createIndex('fcmSenderId', 'fcmSenderId', { - unique: false - }); - objectStore.createIndex('fcmToken', 'fcmToken', { unique: true }); + const db = await openDB('fcm_token_details_db', version, { + upgrade: (upgradeDb, oldVersion) => { + if (oldVersion === 0) { + const objectStore = upgradeDb.createObjectStore( + 'fcm_token_object_Store', + { + keyPath: 'swScope' + } + ); + objectStore.createIndex('fcmSenderId', 'fcmSenderId', { + unique: false + }); + objectStore.createIndex('fcmToken', 'fcmToken', { unique: true }); + } } }); try { const tx = db.transaction('fcm_token_object_Store', 'readwrite'); await tx.objectStore('fcm_token_object_Store').put(value); - await tx.complete; + await tx.done; } finally { db.close(); } diff --git a/packages/messaging/src/helpers/migrate-old-database.ts b/packages/messaging/src/helpers/migrate-old-database.ts index f7a5977502e..2a3429bbe82 100644 --- a/packages/messaging/src/helpers/migrate-old-database.ts +++ b/packages/messaging/src/helpers/migrate-old-database.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { deleteDb, openDb } from 'idb'; +import { deleteDB, openDB } from 'idb'; import { TokenDetails } from '../interfaces/token-details'; import { arrayToBase64 } from './array-base64-translator'; @@ -88,83 +88,85 @@ export async function migrateOldDatabase( let tokenDetails: TokenDetails | null = null; - const db = await openDb(OLD_DB_NAME, OLD_DB_VERSION, async db => { - if (db.oldVersion < 2) { - // Database too old, skip migration. - return; - } - - if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) { - // Database did not exist. Nothing to do. - return; - } - - const objectStore = db.transaction.objectStore(OLD_OBJECT_STORE_NAME); - const value = await objectStore.index('fcmSenderId').get(senderId); - await objectStore.clear(); + const db = await openDB(OLD_DB_NAME, OLD_DB_VERSION, { + upgrade: async (db, oldVersion, _newVersion, transaction) => { + if (oldVersion < 2) { + // Database too old, skip migration. + return; + } - if (!value) { - // No entry in the database, nothing to migrate. - return; - } + if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) { + // Database did not exist. Nothing to do. + return; + } - if (db.oldVersion === 2) { - const oldDetails = value as V2TokenDetails; + const objectStore = transaction.objectStore(OLD_OBJECT_STORE_NAME); + const value = await objectStore.index('fcmSenderId').get(senderId); + await objectStore.clear(); - if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) { + if (!value) { + // No entry in the database, nothing to migrate. return; } - tokenDetails = { - token: oldDetails.fcmToken, - createTime: oldDetails.createTime ?? Date.now(), - subscriptionOptions: { - auth: oldDetails.auth, - p256dh: oldDetails.p256dh, - endpoint: oldDetails.endpoint, - swScope: oldDetails.swScope, - vapidKey: - typeof oldDetails.vapidKey === 'string' - ? oldDetails.vapidKey - : arrayToBase64(oldDetails.vapidKey) - } - }; - } else if (db.oldVersion === 3) { - const oldDetails = value as V3TokenDetails; - - tokenDetails = { - token: oldDetails.fcmToken, - createTime: oldDetails.createTime, - subscriptionOptions: { - auth: arrayToBase64(oldDetails.auth), - p256dh: arrayToBase64(oldDetails.p256dh), - endpoint: oldDetails.endpoint, - swScope: oldDetails.swScope, - vapidKey: arrayToBase64(oldDetails.vapidKey) - } - }; - } else if (db.oldVersion === 4) { - const oldDetails = value as V4TokenDetails; - - tokenDetails = { - token: oldDetails.fcmToken, - createTime: oldDetails.createTime, - subscriptionOptions: { - auth: arrayToBase64(oldDetails.auth), - p256dh: arrayToBase64(oldDetails.p256dh), - endpoint: oldDetails.endpoint, - swScope: oldDetails.swScope, - vapidKey: arrayToBase64(oldDetails.vapidKey) + if (oldVersion === 2) { + const oldDetails = value as V2TokenDetails; + + if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) { + return; } - }; + + tokenDetails = { + token: oldDetails.fcmToken, + createTime: oldDetails.createTime ?? Date.now(), + subscriptionOptions: { + auth: oldDetails.auth, + p256dh: oldDetails.p256dh, + endpoint: oldDetails.endpoint, + swScope: oldDetails.swScope, + vapidKey: + typeof oldDetails.vapidKey === 'string' + ? oldDetails.vapidKey + : arrayToBase64(oldDetails.vapidKey) + } + }; + } else if (oldVersion === 3) { + const oldDetails = value as V3TokenDetails; + + tokenDetails = { + token: oldDetails.fcmToken, + createTime: oldDetails.createTime, + subscriptionOptions: { + auth: arrayToBase64(oldDetails.auth), + p256dh: arrayToBase64(oldDetails.p256dh), + endpoint: oldDetails.endpoint, + swScope: oldDetails.swScope, + vapidKey: arrayToBase64(oldDetails.vapidKey) + } + }; + } else if (oldVersion === 4) { + const oldDetails = value as V4TokenDetails; + + tokenDetails = { + token: oldDetails.fcmToken, + createTime: oldDetails.createTime, + subscriptionOptions: { + auth: arrayToBase64(oldDetails.auth), + p256dh: arrayToBase64(oldDetails.p256dh), + endpoint: oldDetails.endpoint, + swScope: oldDetails.swScope, + vapidKey: arrayToBase64(oldDetails.vapidKey) + } + }; + } } }); db.close(); // Delete all old databases. - await deleteDb(OLD_DB_NAME); - await deleteDb('fcm_vapid_details_db'); - await deleteDb('undefined'); + await deleteDB(OLD_DB_NAME); + await deleteDB('fcm_vapid_details_db'); + await deleteDB('undefined'); return checkTokenDetails(tokenDetails) ? tokenDetails : null; } diff --git a/packages/messaging/src/internals/idb-manager.ts b/packages/messaging/src/internals/idb-manager.ts index 4ddebf5ae96..fb66cdc1c5c 100644 --- a/packages/messaging/src/internals/idb-manager.ts +++ b/packages/messaging/src/internals/idb-manager.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { DB, deleteDb, openDb } from 'idb'; +import { IDBPDatabase, deleteDB, openDB } from 'idb'; import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies'; import { TokenDetails } from '../interfaces/token-details'; @@ -26,17 +26,19 @@ export const DATABASE_NAME = 'firebase-messaging-database'; const DATABASE_VERSION = 1; const OBJECT_STORE_NAME = 'firebase-messaging-store'; -let dbPromise: Promise | null = null; -function getDbPromise(): Promise { +let dbPromise: Promise | null = null; +function getDbPromise(): Promise { if (!dbPromise) { - dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDb => { - // We don't use 'break' in this switch statement, the fall-through behavior is what we want, - // because if there are multiple versions between the old version and the current version, we - // want ALL the migrations that correspond to those versions to run, not only the last one. - // eslint-disable-next-line default-case - switch (upgradeDb.oldVersion) { - case 0: - upgradeDb.createObjectStore(OBJECT_STORE_NAME); + dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, { + upgrade: (upgradeDb, oldVersion) => { + // We don't use 'break' in this switch statement, the fall-through behavior is what we want, + // because if there are multiple versions between the old version and the current version, we + // want ALL the migrations that correspond to those versions to run, not only the last one. + // eslint-disable-next-line default-case + switch (oldVersion) { + case 0: + upgradeDb.createObjectStore(OBJECT_STORE_NAME); + } } }); } @@ -77,7 +79,7 @@ export async function dbSet( const db = await getDbPromise(); const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite'); await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key); - await tx.complete; + await tx.done; return tokenDetails; } @@ -89,14 +91,14 @@ export async function dbRemove( const db = await getDbPromise(); const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite'); await tx.objectStore(OBJECT_STORE_NAME).delete(key); - await tx.complete; + await tx.done; } /** Deletes the DB. Useful for tests. */ export async function dbDelete(): Promise { if (dbPromise) { (await dbPromise).close(); - await deleteDb(DATABASE_NAME); + await deleteDB(DATABASE_NAME); dbPromise = null; } } diff --git a/packages/messaging/src/testing/setup.ts b/packages/messaging/src/testing/setup.ts index eb4802a11a1..69b47f3de81 100644 --- a/packages/messaging/src/testing/setup.ts +++ b/packages/messaging/src/testing/setup.ts @@ -19,7 +19,7 @@ import chaiAsPromised from 'chai-as-promised'; import sinonChai from 'sinon-chai'; import { dbDelete } from '../internals/idb-manager'; -import { deleteDb } from 'idb'; +import { deleteDB } from 'idb'; import { restore } from 'sinon'; import { use } from 'chai'; @@ -29,5 +29,5 @@ use(sinonChai); afterEach(async () => { restore(); await dbDelete(); - await deleteDb('fcm_token_details_db'); + await deleteDB('fcm_token_details_db'); }); From c99c86022d2570b9aeab9b3049f23543d2775ea4 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Wed, 5 Jan 2022 12:56:29 -0800 Subject: [PATCH 3/4] Remove from renovate ignore list --- renovate.json | 1 - 1 file changed, 1 deletion(-) diff --git a/renovate.json b/renovate.json index fbb19f98e44..077027c0187 100644 --- a/renovate.json +++ b/renovate.json @@ -17,7 +17,6 @@ "protractor", "long", "rollup-plugin-copy-assets", - "idb", "whatwg-fetch", "typedoc", "@microsoft/tsdoc" From 8f26cc68cb9b35c4a39c012da5b3e38c23a84f4e Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Tue, 11 Jan 2022 12:12:53 -0800 Subject: [PATCH 4/4] Add changeset! --- .changeset/quiet-years-sleep.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/quiet-years-sleep.md diff --git a/.changeset/quiet-years-sleep.md b/.changeset/quiet-years-sleep.md new file mode 100644 index 00000000000..acb855bb937 --- /dev/null +++ b/.changeset/quiet-years-sleep.md @@ -0,0 +1,7 @@ +--- +'@firebase/installations': patch +'@firebase/installations-compat': patch +'@firebase/messaging': patch +--- + +Update `idb` dependency from 3.0.2 to 7.0.0.