@hot-updater/firebase 0.20.11 → 0.20.13

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.
package/dist/index.cjs CHANGED
@@ -21,15 +21,166 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  }) : target, mod));
22
22
 
23
23
  //#endregion
24
- let path = require("path");
25
- path = __toESM(path);
26
24
  let __hot_updater_plugin_core = require("@hot-updater/plugin-core");
27
25
  __hot_updater_plugin_core = __toESM(__hot_updater_plugin_core);
28
26
  let firebase_admin = require("firebase-admin");
29
27
  firebase_admin = __toESM(firebase_admin);
30
28
  let fs_promises = require("fs/promises");
31
29
  fs_promises = __toESM(fs_promises);
30
+ let path = require("path");
31
+ path = __toESM(path);
32
+
33
+ //#region src/firebaseDatabase.ts
34
+ const convertToBundle = (firestoreData) => ({
35
+ channel: firestoreData.channel,
36
+ enabled: Boolean(firestoreData.enabled),
37
+ shouldForceUpdate: Boolean(firestoreData.should_force_update),
38
+ fileHash: firestoreData.file_hash,
39
+ gitCommitHash: firestoreData.git_commit_hash,
40
+ id: firestoreData.id,
41
+ message: firestoreData.message,
42
+ platform: firestoreData.platform,
43
+ targetAppVersion: firestoreData.target_app_version,
44
+ storageUri: firestoreData.storage_uri,
45
+ fingerprintHash: firestoreData.fingerprint_hash,
46
+ metadata: firestoreData?.metadata ?? {}
47
+ });
48
+ const firebaseDatabase = (config, hooks) => {
49
+ let bundles = [];
50
+ return (0, __hot_updater_plugin_core.createDatabasePlugin)("firebaseDatabase", {
51
+ getContext: () => {
52
+ let app;
53
+ try {
54
+ app = firebase_admin.app();
55
+ } catch {
56
+ app = firebase_admin.initializeApp(config);
57
+ }
58
+ const db = firebase_admin.firestore(app);
59
+ return {
60
+ db,
61
+ bundlesCollection: db.collection("bundles")
62
+ };
63
+ },
64
+ async getBundleById(context, bundleId) {
65
+ const found = bundles.find((b) => b.id === bundleId);
66
+ if (found) return found;
67
+ const bundleSnap = await context.bundlesCollection.doc(bundleId).get();
68
+ if (!bundleSnap.exists) return null;
69
+ return convertToBundle(bundleSnap.data());
70
+ },
71
+ async getBundles(context, options) {
72
+ const { where, limit, offset } = options;
73
+ let query = context.bundlesCollection;
74
+ if (where?.channel) query = query.where("channel", "==", where.channel);
75
+ if (where?.platform) query = query.where("platform", "==", where.platform);
76
+ query = query.orderBy("id", "desc");
77
+ const total = (await query.get()).size;
78
+ if (offset > 0) query = query.offset(offset);
79
+ if (limit) query = query.limit(limit);
80
+ bundles = (await query.get()).docs.map((doc) => convertToBundle(doc.data()));
81
+ return {
82
+ data: bundles,
83
+ pagination: (0, __hot_updater_plugin_core.calculatePagination)(total, {
84
+ limit,
85
+ offset
86
+ })
87
+ };
88
+ },
89
+ async getChannels(context) {
90
+ const querySnapshot = await context.db.collection("channels").get();
91
+ if (querySnapshot.empty) return [];
92
+ const channels = /* @__PURE__ */ new Set();
93
+ for (const doc of querySnapshot.docs) {
94
+ const data = doc.data();
95
+ if (data.name) channels.add(data.name);
96
+ }
97
+ return Array.from(channels);
98
+ },
99
+ async commitBundle(context, { changedSets }) {
100
+ if (changedSets.length === 0) return;
101
+ let isTargetAppVersionChanged = false;
102
+ const deletedBundleIds = /* @__PURE__ */ new Set();
103
+ await context.db.runTransaction(async (transaction) => {
104
+ const bundlesSnapshot = await transaction.get(context.bundlesCollection);
105
+ const targetVersionsSnapshot = await transaction.get(context.db.collection("target_app_versions"));
106
+ const channelsSnapshot = await transaction.get(context.db.collection("channels"));
107
+ const bundlesMap = {};
108
+ for (const doc of bundlesSnapshot.docs) bundlesMap[doc.id] = doc.data();
109
+ for (const { operation, data } of changedSets) {
110
+ if (data.targetAppVersion) isTargetAppVersionChanged = true;
111
+ if (operation === "insert" || operation === "update") {
112
+ bundlesMap[data.id] = {
113
+ id: data.id,
114
+ channel: data.channel,
115
+ enabled: data.enabled,
116
+ should_force_update: data.shouldForceUpdate,
117
+ file_hash: data.fileHash,
118
+ git_commit_hash: data.gitCommitHash || null,
119
+ message: data.message || null,
120
+ platform: data.platform,
121
+ target_app_version: data.targetAppVersion,
122
+ storage_uri: data.storageUri,
123
+ fingerprint_hash: data.fingerprintHash,
124
+ metadata: data.metadata ?? {}
125
+ };
126
+ const channelRef = context.db.collection("channels").doc(data.channel);
127
+ transaction.set(channelRef, { name: data.channel }, { merge: true });
128
+ } else if (operation === "delete") {
129
+ if (!bundlesMap[data.id]) throw new Error(`Bundle with id ${data.id} not found`);
130
+ delete bundlesMap[data.id];
131
+ deletedBundleIds.add(data.id);
132
+ isTargetAppVersionChanged = true;
133
+ }
134
+ }
135
+ const requiredTargetVersionKeys = /* @__PURE__ */ new Set();
136
+ const requiredChannels = /* @__PURE__ */ new Set();
137
+ for (const bundle of Object.values(bundlesMap)) {
138
+ if (bundle.target_app_version) {
139
+ const key = `${bundle.platform}_${bundle.channel}_${bundle.target_app_version}`;
140
+ requiredTargetVersionKeys.add(key);
141
+ }
142
+ requiredChannels.add(bundle.channel);
143
+ }
144
+ for (const { operation, data } of changedSets) {
145
+ const bundleRef = context.bundlesCollection.doc(data.id);
146
+ if (operation === "insert" || operation === "update") {
147
+ transaction.set(bundleRef, {
148
+ id: data.id,
149
+ channel: data.channel,
150
+ enabled: data.enabled,
151
+ should_force_update: data.shouldForceUpdate,
152
+ file_hash: data.fileHash,
153
+ git_commit_hash: data.gitCommitHash || null,
154
+ message: data.message || null,
155
+ platform: data.platform,
156
+ target_app_version: data.targetAppVersion || null,
157
+ storage_uri: data.storageUri,
158
+ fingerprint_hash: data.fingerprintHash,
159
+ metadata: data.metadata ?? {}
160
+ }, { merge: true });
161
+ if (data.targetAppVersion) {
162
+ const versionDocId = `${data.platform}_${data.channel}_${data.targetAppVersion}`;
163
+ const targetAppVersionsRef = context.db.collection("target_app_versions").doc(versionDocId);
164
+ transaction.set(targetAppVersionsRef, {
165
+ channel: data.channel,
166
+ platform: data.platform,
167
+ target_app_version: data.targetAppVersion
168
+ }, { merge: true });
169
+ }
170
+ } else if (operation === "delete") transaction.delete(bundleRef);
171
+ }
172
+ if (isTargetAppVersionChanged) {
173
+ for (const targetDoc of targetVersionsSnapshot.docs) if (!requiredTargetVersionKeys.has(targetDoc.id)) transaction.delete(targetDoc.ref);
174
+ }
175
+ for (const channelDoc of channelsSnapshot.docs) if (!requiredChannels.has(channelDoc.id)) transaction.delete(channelDoc.ref);
176
+ });
177
+ for (const bundleId of deletedBundleIds) bundles = bundles.filter((b) => b.id !== bundleId);
178
+ hooks?.onDatabaseUpdated?.();
179
+ }
180
+ }, hooks);
181
+ };
32
182
 
183
+ //#endregion
33
184
  //#region ../../node_modules/.pnpm/mime@4.0.4/node_modules/mime/dist/types/other.js
34
185
  const types$1 = {
35
186
  "application/prs.cww": ["cww"],
@@ -1333,8 +1484,7 @@ var Mime = class {
1333
1484
  const last = path$2.replace(/^.*[/\\]/, "").toLowerCase();
1334
1485
  const ext = last.replace(/^.*\./, "").toLowerCase();
1335
1486
  const hasPath = last.length < path$2.length;
1336
- const hasDot = ext.length < last.length - 1;
1337
- if (!hasDot && hasPath) return null;
1487
+ if (!(ext.length < last.length - 1) && hasPath) return null;
1338
1488
  return __classPrivateFieldGet(this, _Mime_extensionToType, "f").get(ext) ?? null;
1339
1489
  }
1340
1490
  getExtension(type) {
@@ -1374,7 +1524,7 @@ const firebaseStorage = (config, hooks) => (_) => {
1374
1524
  let app;
1375
1525
  try {
1376
1526
  app = firebase_admin.app();
1377
- } catch (e) {
1527
+ } catch {
1378
1528
  app = firebase_admin.initializeApp(config);
1379
1529
  }
1380
1530
  const bucket = app.storage().bucket(config.storageBucket);
@@ -1396,10 +1546,8 @@ const firebaseStorage = (config, hooks) => (_) => {
1396
1546
  try {
1397
1547
  const fileContent = await fs_promises.default.readFile(bundlePath);
1398
1548
  const contentType = src_default.getType(bundlePath) ?? "application/octet-stream";
1399
- const filename = path.default.basename(bundlePath);
1400
- const key = getStorageKey(bundleId, filename);
1401
- const file = bucket.file(key);
1402
- await file.save(fileContent, { metadata: { contentType } });
1549
+ const key = getStorageKey(bundleId, path.default.basename(bundlePath));
1550
+ await bucket.file(key).save(fileContent, { metadata: { contentType } });
1403
1551
  hooks?.onStorageUploaded?.();
1404
1552
  return { storageUri: `gs://${config.storageBucket}/${key}` };
1405
1553
  } catch (error) {
@@ -1411,164 +1559,6 @@ const firebaseStorage = (config, hooks) => (_) => {
1411
1559
  };
1412
1560
  };
1413
1561
 
1414
- //#endregion
1415
- //#region src/firebaseDatabase.ts
1416
- const convertToBundle = (firestoreData) => ({
1417
- channel: firestoreData.channel,
1418
- enabled: Boolean(firestoreData.enabled),
1419
- shouldForceUpdate: Boolean(firestoreData.should_force_update),
1420
- fileHash: firestoreData.file_hash,
1421
- gitCommitHash: firestoreData.git_commit_hash,
1422
- id: firestoreData.id,
1423
- message: firestoreData.message,
1424
- platform: firestoreData.platform,
1425
- targetAppVersion: firestoreData.target_app_version,
1426
- storageUri: firestoreData.storage_uri,
1427
- fingerprintHash: firestoreData.fingerprint_hash,
1428
- metadata: firestoreData?.metadata ?? {}
1429
- });
1430
- const firebaseDatabase = (config, hooks) => {
1431
- let bundles = [];
1432
- return (0, __hot_updater_plugin_core.createDatabasePlugin)("firebaseDatabase", {
1433
- getContext: () => {
1434
- let app;
1435
- try {
1436
- app = firebase_admin.app();
1437
- } catch (e) {
1438
- app = firebase_admin.initializeApp(config);
1439
- }
1440
- const db = firebase_admin.firestore(app);
1441
- const bundlesCollection = db.collection("bundles");
1442
- return {
1443
- db,
1444
- bundlesCollection
1445
- };
1446
- },
1447
- async getBundleById(context, bundleId) {
1448
- const found = bundles.find((b) => b.id === bundleId);
1449
- if (found) return found;
1450
- const bundleRef = context.bundlesCollection.doc(bundleId);
1451
- const bundleSnap = await bundleRef.get();
1452
- if (!bundleSnap.exists) return null;
1453
- const firestoreData = bundleSnap.data();
1454
- return convertToBundle(firestoreData);
1455
- },
1456
- async getBundles(context, options) {
1457
- const { where, limit, offset } = options;
1458
- let query = context.bundlesCollection;
1459
- if (where?.channel) query = query.where("channel", "==", where.channel);
1460
- if (where?.platform) query = query.where("platform", "==", where.platform);
1461
- query = query.orderBy("id", "desc");
1462
- const totalCountQuery = query;
1463
- const totalSnapshot = await totalCountQuery.get();
1464
- const total = totalSnapshot.size;
1465
- if (offset > 0) query = query.offset(offset);
1466
- if (limit) query = query.limit(limit);
1467
- const querySnapshot = await query.get();
1468
- bundles = querySnapshot.docs.map((doc) => convertToBundle(doc.data()));
1469
- return {
1470
- data: bundles,
1471
- pagination: (0, __hot_updater_plugin_core.calculatePagination)(total, {
1472
- limit,
1473
- offset
1474
- })
1475
- };
1476
- },
1477
- async getChannels(context) {
1478
- const channelsCollection = context.db.collection("channels");
1479
- const querySnapshot = await channelsCollection.get();
1480
- if (querySnapshot.empty) return [];
1481
- const channels = /* @__PURE__ */ new Set();
1482
- for (const doc of querySnapshot.docs) {
1483
- const data = doc.data();
1484
- if (data.name) channels.add(data.name);
1485
- }
1486
- return Array.from(channels);
1487
- },
1488
- async commitBundle(context, { changedSets }) {
1489
- if (changedSets.length === 0) return;
1490
- let isTargetAppVersionChanged = false;
1491
- const deletedBundleIds = /* @__PURE__ */ new Set();
1492
- await context.db.runTransaction(async (transaction) => {
1493
- const bundlesSnapshot = await transaction.get(context.bundlesCollection);
1494
- const targetVersionsSnapshot = await transaction.get(context.db.collection("target_app_versions"));
1495
- const channelsSnapshot = await transaction.get(context.db.collection("channels"));
1496
- const bundlesMap = {};
1497
- for (const doc of bundlesSnapshot.docs) bundlesMap[doc.id] = doc.data();
1498
- for (const { operation, data } of changedSets) {
1499
- if (data.targetAppVersion) isTargetAppVersionChanged = true;
1500
- if (operation === "insert" || operation === "update") {
1501
- bundlesMap[data.id] = {
1502
- id: data.id,
1503
- channel: data.channel,
1504
- enabled: data.enabled,
1505
- should_force_update: data.shouldForceUpdate,
1506
- file_hash: data.fileHash,
1507
- git_commit_hash: data.gitCommitHash || null,
1508
- message: data.message || null,
1509
- platform: data.platform,
1510
- target_app_version: data.targetAppVersion,
1511
- storage_uri: data.storageUri,
1512
- fingerprint_hash: data.fingerprintHash,
1513
- metadata: data.metadata ?? {}
1514
- };
1515
- const channelRef = context.db.collection("channels").doc(data.channel);
1516
- transaction.set(channelRef, { name: data.channel }, { merge: true });
1517
- } else if (operation === "delete") {
1518
- if (!bundlesMap[data.id]) throw new Error(`Bundle with id ${data.id} not found`);
1519
- delete bundlesMap[data.id];
1520
- deletedBundleIds.add(data.id);
1521
- isTargetAppVersionChanged = true;
1522
- }
1523
- }
1524
- const requiredTargetVersionKeys = /* @__PURE__ */ new Set();
1525
- const requiredChannels = /* @__PURE__ */ new Set();
1526
- for (const bundle of Object.values(bundlesMap)) {
1527
- if (bundle.target_app_version) {
1528
- const key = `${bundle.platform}_${bundle.channel}_${bundle.target_app_version}`;
1529
- requiredTargetVersionKeys.add(key);
1530
- }
1531
- requiredChannels.add(bundle.channel);
1532
- }
1533
- for (const { operation, data } of changedSets) {
1534
- const bundleRef = context.bundlesCollection.doc(data.id);
1535
- if (operation === "insert" || operation === "update") {
1536
- transaction.set(bundleRef, {
1537
- id: data.id,
1538
- channel: data.channel,
1539
- enabled: data.enabled,
1540
- should_force_update: data.shouldForceUpdate,
1541
- file_hash: data.fileHash,
1542
- git_commit_hash: data.gitCommitHash || null,
1543
- message: data.message || null,
1544
- platform: data.platform,
1545
- target_app_version: data.targetAppVersion || null,
1546
- storage_uri: data.storageUri,
1547
- fingerprint_hash: data.fingerprintHash,
1548
- metadata: data.metadata ?? {}
1549
- }, { merge: true });
1550
- if (data.targetAppVersion) {
1551
- const versionDocId = `${data.platform}_${data.channel}_${data.targetAppVersion}`;
1552
- const targetAppVersionsRef = context.db.collection("target_app_versions").doc(versionDocId);
1553
- transaction.set(targetAppVersionsRef, {
1554
- channel: data.channel,
1555
- platform: data.platform,
1556
- target_app_version: data.targetAppVersion
1557
- }, { merge: true });
1558
- }
1559
- } else if (operation === "delete") transaction.delete(bundleRef);
1560
- }
1561
- if (isTargetAppVersionChanged) {
1562
- for (const targetDoc of targetVersionsSnapshot.docs) if (!requiredTargetVersionKeys.has(targetDoc.id)) transaction.delete(targetDoc.ref);
1563
- }
1564
- for (const channelDoc of channelsSnapshot.docs) if (!requiredChannels.has(channelDoc.id)) transaction.delete(channelDoc.ref);
1565
- });
1566
- for (const bundleId of deletedBundleIds) bundles = bundles.filter((b) => b.id !== bundleId);
1567
- hooks?.onDatabaseUpdated?.();
1568
- }
1569
- }, hooks);
1570
- };
1571
-
1572
1562
  //#endregion
1573
1563
  exports.firebaseDatabase = firebaseDatabase;
1574
1564
  exports.firebaseStorage = firebaseStorage;
package/dist/index.d.cts CHANGED
@@ -2,6 +2,9 @@ import * as _hot_updater_plugin_core0 from "@hot-updater/plugin-core";
2
2
  import { BasePluginArgs, DatabasePluginHooks, StoragePlugin, StoragePluginHooks } from "@hot-updater/plugin-core";
3
3
  import * as admin from "firebase-admin";
4
4
 
5
+ //#region src/firebaseDatabase.d.ts
6
+ declare const firebaseDatabase: (config: admin.AppOptions, hooks?: DatabasePluginHooks) => (options: _hot_updater_plugin_core0.BasePluginArgs) => _hot_updater_plugin_core0.DatabasePlugin;
7
+ //#endregion
5
8
  //#region src/firebaseStorage.d.ts
6
9
  interface FirebaseStorageConfig extends admin.AppOptions {
7
10
  storageBucket: string;
@@ -12,7 +15,4 @@ interface FirebaseStorageConfig extends admin.AppOptions {
12
15
  }
13
16
  declare const firebaseStorage: (config: FirebaseStorageConfig, hooks?: StoragePluginHooks) => (_: BasePluginArgs) => StoragePlugin;
14
17
  //#endregion
15
- //#region src/firebaseDatabase.d.ts
16
- declare const firebaseDatabase: (config: admin.AppOptions, hooks?: DatabasePluginHooks) => (options: _hot_updater_plugin_core0.BasePluginArgs) => _hot_updater_plugin_core0.DatabasePlugin;
17
- //#endregion
18
18
  export { FirebaseStorageConfig, firebaseDatabase, firebaseStorage };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,9 @@ import * as _hot_updater_plugin_core0 from "@hot-updater/plugin-core";
2
2
  import { BasePluginArgs, DatabasePluginHooks, StoragePlugin, StoragePluginHooks } from "@hot-updater/plugin-core";
3
3
  import * as admin from "firebase-admin";
4
4
 
5
+ //#region src/firebaseDatabase.d.ts
6
+ declare const firebaseDatabase: (config: admin.AppOptions, hooks?: DatabasePluginHooks) => (options: _hot_updater_plugin_core0.BasePluginArgs) => _hot_updater_plugin_core0.DatabasePlugin;
7
+ //#endregion
5
8
  //#region src/firebaseStorage.d.ts
6
9
  interface FirebaseStorageConfig extends admin.AppOptions {
7
10
  storageBucket: string;
@@ -12,7 +15,4 @@ interface FirebaseStorageConfig extends admin.AppOptions {
12
15
  }
13
16
  declare const firebaseStorage: (config: FirebaseStorageConfig, hooks?: StoragePluginHooks) => (_: BasePluginArgs) => StoragePlugin;
14
17
  //#endregion
15
- //#region src/firebaseDatabase.d.ts
16
- declare const firebaseDatabase: (config: admin.AppOptions, hooks?: DatabasePluginHooks) => (options: _hot_updater_plugin_core0.BasePluginArgs) => _hot_updater_plugin_core0.DatabasePlugin;
17
- //#endregion
18
18
  export { FirebaseStorageConfig, firebaseDatabase, firebaseStorage };