Skip to content

Commit 2b455ea

Browse files
authored
Merge 8f26cc6 into c271d16
2 parents c271d16 + 8f26cc6 commit 2b455ea

File tree

11 files changed

+134
-120
lines changed

11 files changed

+134
-120
lines changed

.changeset/quiet-years-sleep.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@firebase/installations': patch
3+
'@firebase/installations-compat': patch
4+
'@firebase/messaging': patch
5+
---
6+
7+
Update `idb` dependency from 3.0.2 to 7.0.0.

packages/installations-compat/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"@firebase/installations-types": "0.4.0",
6262
"@firebase/util": "1.4.3",
6363
"@firebase/component": "0.5.10",
64-
"idb": "3.0.2",
64+
"idb": "7.0.0",
6565
"tslib": "^2.1.0"
6666
}
6767
}

packages/installations/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"dependencies": {
6565
"@firebase/util": "1.4.3",
6666
"@firebase/component": "0.5.10",
67-
"idb": "3.0.2",
67+
"idb": "7.0.0",
6868
"tslib": "^2.1.0"
6969
}
7070
}

packages/installations/src/helpers/idb-manager.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DB, openDb } from 'idb';
18+
import { IDBPDatabase, openDB } from 'idb';
1919
import { AppConfig } from '../interfaces/installation-impl';
2020
import { InstallationEntry } from '../interfaces/installation-entry';
2121
import { getKey } from '../util/get-key';
@@ -25,18 +25,20 @@ const DATABASE_NAME = 'firebase-installations-database';
2525
const DATABASE_VERSION = 1;
2626
const OBJECT_STORE_NAME = 'firebase-installations-store';
2727

28-
let dbPromise: Promise<DB> | null = null;
29-
function getDbPromise(): Promise<DB> {
28+
let dbPromise: Promise<IDBPDatabase> | null = null;
29+
function getDbPromise(): Promise<IDBPDatabase> {
3030
if (!dbPromise) {
31-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
32-
// We don't use 'break' in this switch statement, the fall-through
33-
// behavior is what we want, because if there are multiple versions between
34-
// the old version and the current version, we want ALL the migrations
35-
// that correspond to those versions to run, not only the last one.
36-
// eslint-disable-next-line default-case
37-
switch (upgradeDB.oldVersion) {
38-
case 0:
39-
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
31+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {
32+
upgrade: (db, oldVersion) => {
33+
// We don't use 'break' in this switch statement, the fall-through
34+
// behavior is what we want, because if there are multiple versions between
35+
// the old version and the current version, we want ALL the migrations
36+
// that correspond to those versions to run, not only the last one.
37+
// eslint-disable-next-line default-case
38+
switch (oldVersion) {
39+
case 0:
40+
db.createObjectStore(OBJECT_STORE_NAME);
41+
}
4042
}
4143
});
4244
}
@@ -66,7 +68,7 @@ export async function set<ValueType extends InstallationEntry>(
6668
const objectStore = tx.objectStore(OBJECT_STORE_NAME);
6769
const oldValue = await objectStore.get(key);
6870
await objectStore.put(value, key);
69-
await tx.complete;
71+
await tx.done;
7072

7173
if (!oldValue || oldValue.fid !== value.fid) {
7274
fidChanged(appConfig, value.fid);
@@ -81,7 +83,7 @@ export async function remove(appConfig: AppConfig): Promise<void> {
8183
const db = await getDbPromise();
8284
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
8385
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
84-
await tx.complete;
86+
await tx.done;
8587
}
8688

8789
/**
@@ -106,7 +108,7 @@ export async function update<ValueType extends InstallationEntry | undefined>(
106108
} else {
107109
await store.put(newValue, key);
108110
}
109-
await tx.complete;
111+
await tx.done;
110112

111113
if (newValue && (!oldValue || oldValue.fid !== newValue.fid)) {
112114
fidChanged(appConfig, newValue.fid);
@@ -119,5 +121,5 @@ export async function clear(): Promise<void> {
119121
const db = await getDbPromise();
120122
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
121123
await tx.objectStore(OBJECT_STORE_NAME).clear();
122-
await tx.complete;
124+
await tx.done;
123125
}

packages/messaging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"@firebase/messaging-interop-types": "0.1.0",
5252
"@firebase/util": "1.4.3",
5353
"@firebase/component": "0.5.10",
54-
"idb": "3.0.2",
54+
"idb": "7.0.0",
5555
"tslib": "^2.1.0"
5656
},
5757
"devDependencies": {

packages/messaging/src/helpers/migrate-old-database.test.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { FakePushSubscription } from '../testing/fakes/service-worker';
2828
import { base64ToArray } from './array-base64-translator';
2929
import { expect } from 'chai';
3030
import { getFakeTokenDetails } from '../testing/fakes/token-details';
31-
import { openDb } from 'idb';
31+
import { openDB } from 'idb';
3232

3333
describe('migrateOldDb', () => {
3434
it("does nothing if old DB didn't exist", async () => {
@@ -179,25 +179,27 @@ describe('migrateOldDb', () => {
179179
});
180180

181181
async function put(version: number, value: object): Promise<void> {
182-
const db = await openDb('fcm_token_details_db', version, upgradeDb => {
183-
if (upgradeDb.oldVersion === 0) {
184-
const objectStore = upgradeDb.createObjectStore(
185-
'fcm_token_object_Store',
186-
{
187-
keyPath: 'swScope'
188-
}
189-
);
190-
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
191-
unique: false
192-
});
193-
objectStore.createIndex('fcmToken', 'fcmToken', { unique: true });
182+
const db = await openDB('fcm_token_details_db', version, {
183+
upgrade: (upgradeDb, oldVersion) => {
184+
if (oldVersion === 0) {
185+
const objectStore = upgradeDb.createObjectStore(
186+
'fcm_token_object_Store',
187+
{
188+
keyPath: 'swScope'
189+
}
190+
);
191+
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
192+
unique: false
193+
});
194+
objectStore.createIndex('fcmToken', 'fcmToken', { unique: true });
195+
}
194196
}
195197
});
196198

197199
try {
198200
const tx = db.transaction('fcm_token_object_Store', 'readwrite');
199201
await tx.objectStore('fcm_token_object_Store').put(value);
200-
await tx.complete;
202+
await tx.done;
201203
} finally {
202204
db.close();
203205
}

packages/messaging/src/helpers/migrate-old-database.ts

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { deleteDb, openDb } from 'idb';
18+
import { deleteDB, openDB } from 'idb';
1919

2020
import { TokenDetails } from '../interfaces/token-details';
2121
import { arrayToBase64 } from './array-base64-translator';
@@ -88,83 +88,85 @@ export async function migrateOldDatabase(
8888

8989
let tokenDetails: TokenDetails | null = null;
9090

91-
const db = await openDb(OLD_DB_NAME, OLD_DB_VERSION, async db => {
92-
if (db.oldVersion < 2) {
93-
// Database too old, skip migration.
94-
return;
95-
}
96-
97-
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
98-
// Database did not exist. Nothing to do.
99-
return;
100-
}
101-
102-
const objectStore = db.transaction.objectStore(OLD_OBJECT_STORE_NAME);
103-
const value = await objectStore.index('fcmSenderId').get(senderId);
104-
await objectStore.clear();
91+
const db = await openDB(OLD_DB_NAME, OLD_DB_VERSION, {
92+
upgrade: async (db, oldVersion, _newVersion, transaction) => {
93+
if (oldVersion < 2) {
94+
// Database too old, skip migration.
95+
return;
96+
}
10597

106-
if (!value) {
107-
// No entry in the database, nothing to migrate.
108-
return;
109-
}
98+
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
99+
// Database did not exist. Nothing to do.
100+
return;
101+
}
110102

111-
if (db.oldVersion === 2) {
112-
const oldDetails = value as V2TokenDetails;
103+
const objectStore = transaction.objectStore(OLD_OBJECT_STORE_NAME);
104+
const value = await objectStore.index('fcmSenderId').get(senderId);
105+
await objectStore.clear();
113106

114-
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
107+
if (!value) {
108+
// No entry in the database, nothing to migrate.
115109
return;
116110
}
117111

118-
tokenDetails = {
119-
token: oldDetails.fcmToken,
120-
createTime: oldDetails.createTime ?? Date.now(),
121-
subscriptionOptions: {
122-
auth: oldDetails.auth,
123-
p256dh: oldDetails.p256dh,
124-
endpoint: oldDetails.endpoint,
125-
swScope: oldDetails.swScope,
126-
vapidKey:
127-
typeof oldDetails.vapidKey === 'string'
128-
? oldDetails.vapidKey
129-
: arrayToBase64(oldDetails.vapidKey)
130-
}
131-
};
132-
} else if (db.oldVersion === 3) {
133-
const oldDetails = value as V3TokenDetails;
134-
135-
tokenDetails = {
136-
token: oldDetails.fcmToken,
137-
createTime: oldDetails.createTime,
138-
subscriptionOptions: {
139-
auth: arrayToBase64(oldDetails.auth),
140-
p256dh: arrayToBase64(oldDetails.p256dh),
141-
endpoint: oldDetails.endpoint,
142-
swScope: oldDetails.swScope,
143-
vapidKey: arrayToBase64(oldDetails.vapidKey)
144-
}
145-
};
146-
} else if (db.oldVersion === 4) {
147-
const oldDetails = value as V4TokenDetails;
148-
149-
tokenDetails = {
150-
token: oldDetails.fcmToken,
151-
createTime: oldDetails.createTime,
152-
subscriptionOptions: {
153-
auth: arrayToBase64(oldDetails.auth),
154-
p256dh: arrayToBase64(oldDetails.p256dh),
155-
endpoint: oldDetails.endpoint,
156-
swScope: oldDetails.swScope,
157-
vapidKey: arrayToBase64(oldDetails.vapidKey)
112+
if (oldVersion === 2) {
113+
const oldDetails = value as V2TokenDetails;
114+
115+
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
116+
return;
158117
}
159-
};
118+
119+
tokenDetails = {
120+
token: oldDetails.fcmToken,
121+
createTime: oldDetails.createTime ?? Date.now(),
122+
subscriptionOptions: {
123+
auth: oldDetails.auth,
124+
p256dh: oldDetails.p256dh,
125+
endpoint: oldDetails.endpoint,
126+
swScope: oldDetails.swScope,
127+
vapidKey:
128+
typeof oldDetails.vapidKey === 'string'
129+
? oldDetails.vapidKey
130+
: arrayToBase64(oldDetails.vapidKey)
131+
}
132+
};
133+
} else if (oldVersion === 3) {
134+
const oldDetails = value as V3TokenDetails;
135+
136+
tokenDetails = {
137+
token: oldDetails.fcmToken,
138+
createTime: oldDetails.createTime,
139+
subscriptionOptions: {
140+
auth: arrayToBase64(oldDetails.auth),
141+
p256dh: arrayToBase64(oldDetails.p256dh),
142+
endpoint: oldDetails.endpoint,
143+
swScope: oldDetails.swScope,
144+
vapidKey: arrayToBase64(oldDetails.vapidKey)
145+
}
146+
};
147+
} else if (oldVersion === 4) {
148+
const oldDetails = value as V4TokenDetails;
149+
150+
tokenDetails = {
151+
token: oldDetails.fcmToken,
152+
createTime: oldDetails.createTime,
153+
subscriptionOptions: {
154+
auth: arrayToBase64(oldDetails.auth),
155+
p256dh: arrayToBase64(oldDetails.p256dh),
156+
endpoint: oldDetails.endpoint,
157+
swScope: oldDetails.swScope,
158+
vapidKey: arrayToBase64(oldDetails.vapidKey)
159+
}
160+
};
161+
}
160162
}
161163
});
162164
db.close();
163165

164166
// Delete all old databases.
165-
await deleteDb(OLD_DB_NAME);
166-
await deleteDb('fcm_vapid_details_db');
167-
await deleteDb('undefined');
167+
await deleteDB(OLD_DB_NAME);
168+
await deleteDB('fcm_vapid_details_db');
169+
await deleteDB('undefined');
168170

169171
return checkTokenDetails(tokenDetails) ? tokenDetails : null;
170172
}

packages/messaging/src/internals/idb-manager.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DB, deleteDb, openDb } from 'idb';
18+
import { IDBPDatabase, deleteDB, openDB } from 'idb';
1919

2020
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
2121
import { TokenDetails } from '../interfaces/token-details';
@@ -26,17 +26,19 @@ export const DATABASE_NAME = 'firebase-messaging-database';
2626
const DATABASE_VERSION = 1;
2727
const OBJECT_STORE_NAME = 'firebase-messaging-store';
2828

29-
let dbPromise: Promise<DB> | null = null;
30-
function getDbPromise(): Promise<DB> {
29+
let dbPromise: Promise<IDBPDatabase> | null = null;
30+
function getDbPromise(): Promise<IDBPDatabase> {
3131
if (!dbPromise) {
32-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDb => {
33-
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
34-
// because if there are multiple versions between the old version and the current version, we
35-
// want ALL the migrations that correspond to those versions to run, not only the last one.
36-
// eslint-disable-next-line default-case
37-
switch (upgradeDb.oldVersion) {
38-
case 0:
39-
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
32+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {
33+
upgrade: (upgradeDb, oldVersion) => {
34+
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
35+
// because if there are multiple versions between the old version and the current version, we
36+
// want ALL the migrations that correspond to those versions to run, not only the last one.
37+
// eslint-disable-next-line default-case
38+
switch (oldVersion) {
39+
case 0:
40+
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
41+
}
4042
}
4143
});
4244
}
@@ -77,7 +79,7 @@ export async function dbSet(
7779
const db = await getDbPromise();
7880
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
7981
await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key);
80-
await tx.complete;
82+
await tx.done;
8183
return tokenDetails;
8284
}
8385

@@ -89,14 +91,14 @@ export async function dbRemove(
8991
const db = await getDbPromise();
9092
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
9193
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
92-
await tx.complete;
94+
await tx.done;
9395
}
9496

9597
/** Deletes the DB. Useful for tests. */
9698
export async function dbDelete(): Promise<void> {
9799
if (dbPromise) {
98100
(await dbPromise).close();
99-
await deleteDb(DATABASE_NAME);
101+
await deleteDB(DATABASE_NAME);
100102
dbPromise = null;
101103
}
102104
}

0 commit comments

Comments
 (0)