@hot-updater/firebase 1.2.0-rc.49 → 1.2.0-rc.50

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
@@ -167,14 +167,67 @@ const createFirestoreStore = ({ firestore, collection, nativePageSize }) => {
167
167
  };
168
168
  };
169
169
  //#endregion
170
+ //#region src/googleCredential.ts
171
+ const CLOUD_PLATFORM_SCOPE$1 = "https://www.googleapis.com/auth/cloud-platform";
172
+ /**
173
+ * Resolves application default credentials as Firestore will, from
174
+ * GOOGLE_APPLICATION_CREDENTIALS's file or the environment, for the app's
175
+ * `projectId`, so it neither exchanges a token nor discovers a project
176
+ * Firestore itself wouldn't. Only `applicationDefault()`'s credential: a
177
+ * service account's or refresh token's was read from its file already.
178
+ */
179
+ const resolveApplicationDefault = async (credential, projectId) => {
180
+ if (credential.constructor.name !== "ApplicationDefaultCredential") return;
181
+ const { GoogleAuth } = await import("google-auth-library");
182
+ await new GoogleAuth({
183
+ scopes: [CLOUD_PLATFORM_SCOPE$1],
184
+ ...projectId === void 0 ? {} : { projectId }
185
+ }).getClient();
186
+ };
187
+ //#endregion
170
188
  //#region src/firebaseDatabase.ts
189
+ /**
190
+ * `store` that loads `credential` before its first call: one that cannot
191
+ * load, such as GOOGLE_APPLICATION_CREDENTIALS naming a missing file, fails
192
+ * that call with what is wrong. Firestore's own client would also reject a
193
+ * promise nobody awaits, which ends a CLI process with a stack trace.
194
+ */
195
+ const withLoadedCredential = (store, credential, projectId) => {
196
+ let loaded;
197
+ const load = () => loaded ??= resolveApplicationDefault(credential, projectId).then(() => void 0, (error) => {
198
+ loaded = void 0;
199
+ throw new Error(`Could not load the Google credentials for Firestore: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
200
+ });
201
+ const { deleteConsumed } = store;
202
+ return {
203
+ ...store,
204
+ get: async (keys) => {
205
+ await load();
206
+ return store.get(keys);
207
+ },
208
+ query: async (range) => {
209
+ await load();
210
+ return store.query(range);
211
+ },
212
+ write: async (ops) => {
213
+ await load();
214
+ return store.write(ops);
215
+ },
216
+ ...deleteConsumed && { deleteConsumed: async (keys) => {
217
+ await load();
218
+ return deleteConsumed.call(store, keys);
219
+ } }
220
+ };
221
+ };
171
222
  const adapterOf = ({ databaseId, collection = FIREBASE_V1_COLLECTION, aggregateBatching: _, ...appOptions }) => {
172
223
  const app = (0, firebase_admin_app.getApps)().length ? (0, firebase_admin_app.getApp)() : (0, firebase_admin_app.initializeApp)(appOptions);
173
224
  const firestore = databaseId ? (0, firebase_admin_firestore.getFirestore)(app, databaseId) : (0, firebase_admin_firestore.getFirestore)(app);
174
- return (0, _hot_updater_plugin_core.createKvAdapter)({ store: createFirestoreStore({
225
+ const store = createFirestoreStore({
175
226
  firestore,
176
227
  collection
177
- }) });
228
+ });
229
+ const credential = app.options.credential;
230
+ return (0, _hot_updater_plugin_core.createKvAdapter)({ store: appOptions.credential === void 0 || credential === void 0 || process.env["FIRESTORE_EMULATOR_HOST"] ? store : withLoadedCredential(store, credential, app.options.projectId) });
178
231
  };
179
232
  /**
180
233
  * Writes the schema settings of core and `plugins`, the plugins the server
package/dist/index.mjs CHANGED
@@ -166,14 +166,67 @@ const createFirestoreStore = ({ firestore, collection, nativePageSize }) => {
166
166
  };
167
167
  };
168
168
  //#endregion
169
+ //#region src/googleCredential.ts
170
+ const CLOUD_PLATFORM_SCOPE$1 = "https://www.googleapis.com/auth/cloud-platform";
171
+ /**
172
+ * Resolves application default credentials as Firestore will, from
173
+ * GOOGLE_APPLICATION_CREDENTIALS's file or the environment, for the app's
174
+ * `projectId`, so it neither exchanges a token nor discovers a project
175
+ * Firestore itself wouldn't. Only `applicationDefault()`'s credential: a
176
+ * service account's or refresh token's was read from its file already.
177
+ */
178
+ const resolveApplicationDefault = async (credential, projectId) => {
179
+ if (credential.constructor.name !== "ApplicationDefaultCredential") return;
180
+ const { GoogleAuth } = await import("google-auth-library");
181
+ await new GoogleAuth({
182
+ scopes: [CLOUD_PLATFORM_SCOPE$1],
183
+ ...projectId === void 0 ? {} : { projectId }
184
+ }).getClient();
185
+ };
186
+ //#endregion
169
187
  //#region src/firebaseDatabase.ts
188
+ /**
189
+ * `store` that loads `credential` before its first call: one that cannot
190
+ * load, such as GOOGLE_APPLICATION_CREDENTIALS naming a missing file, fails
191
+ * that call with what is wrong. Firestore's own client would also reject a
192
+ * promise nobody awaits, which ends a CLI process with a stack trace.
193
+ */
194
+ const withLoadedCredential = (store, credential, projectId) => {
195
+ let loaded;
196
+ const load = () => loaded ??= resolveApplicationDefault(credential, projectId).then(() => void 0, (error) => {
197
+ loaded = void 0;
198
+ throw new Error(`Could not load the Google credentials for Firestore: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
199
+ });
200
+ const { deleteConsumed } = store;
201
+ return {
202
+ ...store,
203
+ get: async (keys) => {
204
+ await load();
205
+ return store.get(keys);
206
+ },
207
+ query: async (range) => {
208
+ await load();
209
+ return store.query(range);
210
+ },
211
+ write: async (ops) => {
212
+ await load();
213
+ return store.write(ops);
214
+ },
215
+ ...deleteConsumed && { deleteConsumed: async (keys) => {
216
+ await load();
217
+ return deleteConsumed.call(store, keys);
218
+ } }
219
+ };
220
+ };
170
221
  const adapterOf = ({ databaseId, collection = FIREBASE_V1_COLLECTION, aggregateBatching: _, ...appOptions }) => {
171
222
  const app = getApps().length ? getApp() : initializeApp(appOptions);
172
223
  const firestore = databaseId ? getFirestore(app, databaseId) : getFirestore(app);
173
- return createKvAdapter({ store: createFirestoreStore({
224
+ const store = createFirestoreStore({
174
225
  firestore,
175
226
  collection
176
- }) });
227
+ });
228
+ const credential = app.options.credential;
229
+ return createKvAdapter({ store: appOptions.credential === void 0 || credential === void 0 || process.env["FIRESTORE_EMULATOR_HOST"] ? store : withLoadedCredential(store, credential, app.options.projectId) });
177
230
  };
178
231
  /**
179
232
  * Writes the schema settings of core and `plugins`, the plugins the server
@@ -6863,14 +6863,67 @@ const createFirestoreStore = ({ firestore, collection, nativePageSize }) => {
6863
6863
  };
6864
6864
  };
6865
6865
  //#endregion
6866
+ //#region src/googleCredential.ts
6867
+ const CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
6868
+ /**
6869
+ * Resolves application default credentials as Firestore will, from
6870
+ * GOOGLE_APPLICATION_CREDENTIALS's file or the environment, for the app's
6871
+ * `projectId`, so it neither exchanges a token nor discovers a project
6872
+ * Firestore itself wouldn't. Only `applicationDefault()`'s credential: a
6873
+ * service account's or refresh token's was read from its file already.
6874
+ */
6875
+ const resolveApplicationDefault = async (credential, projectId) => {
6876
+ if (credential.constructor.name !== "ApplicationDefaultCredential") return;
6877
+ const { GoogleAuth } = await import("google-auth-library");
6878
+ await new GoogleAuth({
6879
+ scopes: [CLOUD_PLATFORM_SCOPE],
6880
+ ...projectId === void 0 ? {} : { projectId }
6881
+ }).getClient();
6882
+ };
6883
+ //#endregion
6866
6884
  //#region src/firebaseDatabase.ts
6885
+ /**
6886
+ * `store` that loads `credential` before its first call: one that cannot
6887
+ * load, such as GOOGLE_APPLICATION_CREDENTIALS naming a missing file, fails
6888
+ * that call with what is wrong. Firestore's own client would also reject a
6889
+ * promise nobody awaits, which ends a CLI process with a stack trace.
6890
+ */
6891
+ const withLoadedCredential = (store, credential, projectId) => {
6892
+ let loaded;
6893
+ const load = () => loaded ??= resolveApplicationDefault(credential, projectId).then(() => void 0, (error) => {
6894
+ loaded = void 0;
6895
+ throw new Error(`Could not load the Google credentials for Firestore: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
6896
+ });
6897
+ const { deleteConsumed } = store;
6898
+ return {
6899
+ ...store,
6900
+ get: async (keys) => {
6901
+ await load();
6902
+ return store.get(keys);
6903
+ },
6904
+ query: async (range) => {
6905
+ await load();
6906
+ return store.query(range);
6907
+ },
6908
+ write: async (ops) => {
6909
+ await load();
6910
+ return store.write(ops);
6911
+ },
6912
+ ...deleteConsumed && { deleteConsumed: async (keys) => {
6913
+ await load();
6914
+ return deleteConsumed.call(store, keys);
6915
+ } }
6916
+ };
6917
+ };
6867
6918
  const adapterOf = ({ databaseId, collection = FIREBASE_V1_COLLECTION, aggregateBatching: _, ...appOptions }) => {
6868
6919
  const app = (0, firebase_admin_app.getApps)().length ? (0, firebase_admin_app.getApp)() : (0, firebase_admin_app.initializeApp)(appOptions);
6869
6920
  const firestore = databaseId ? (0, firebase_admin_firestore.getFirestore)(app, databaseId) : (0, firebase_admin_firestore.getFirestore)(app);
6870
- return (0, _hot_updater_plugin_core.createKvAdapter)({ store: createFirestoreStore({
6921
+ const store = createFirestoreStore({
6871
6922
  firestore,
6872
6923
  collection
6873
- }) });
6924
+ });
6925
+ const credential = app.options.credential;
6926
+ return (0, _hot_updater_plugin_core.createKvAdapter)({ store: appOptions.credential === void 0 || credential === void 0 || process.env["FIRESTORE_EMULATOR_HOST"] ? store : withLoadedCredential(store, credential, app.options.projectId) });
6874
6927
  };
6875
6928
  /**
6876
6929
  * Writes the schema settings of core and `plugins`, the plugins the server
@@ -6976,8 +7029,32 @@ const firebaseEngineSettingDocumentId = () => firestoreDocumentId({
6976
7029
  pk: _hot_updater_plugin_core.SETTINGS_TABLE.name,
6977
7030
  sk: (0, _hot_updater_plugin_core.encodeKvKey)([ENGINE_SETTING])
6978
7031
  });
7032
+ /**
7033
+ * The Google credential init reads Firestore with, once it loads: the
7034
+ * service account key file `applicationCredentials` names, or the
7035
+ * application default credentials. Before any change, an InitError says
7036
+ * what is wrong with one that does not load, where Firestore's client would
7037
+ * end init with a stack trace.
7038
+ */
7039
+ const loadGoogleCredential = async (applicationCredentials, projectId) => {
7040
+ const next = "Set GOOGLE_APPLICATION_CREDENTIALS to a service account key file, then rerun init.";
7041
+ let credential;
7042
+ try {
7043
+ credential = applicationCredentials ? (0, firebase_admin_app.cert)(JSON.parse(await fs_promises.default.readFile(applicationCredentials, "utf8"))) : (0, firebase_admin_app.applicationDefault)();
7044
+ } catch {
7045
+ throw new _hot_updater_cli_tools.InitError(`GOOGLE_APPLICATION_CREDENTIALS names ${applicationCredentials}, which is not a readable service account key file. ${next}`);
7046
+ }
7047
+ if (process.env["FIRESTORE_EMULATOR_HOST"]) return credential;
7048
+ try {
7049
+ await resolveApplicationDefault(credential, projectId);
7050
+ } catch (error) {
7051
+ const reason = (error instanceof Error ? error.message : String(error)).split(/(?<=\.)\s/u)[0].replace(/\.$/u, "");
7052
+ throw new _hot_updater_cli_tools.InitError(`Could not load Google credentials: ${reason}. ${next}`);
7053
+ }
7054
+ return credential;
7055
+ };
6979
7056
  const assertFirebaseInfrastructureCanInitialize = async ({ applicationCredentials, databaseId, projectId }) => {
6980
- const credential = applicationCredentials ? (0, firebase_admin_app.cert)(JSON.parse(await fs_promises.default.readFile(applicationCredentials, "utf8"))) : (0, firebase_admin_app.applicationDefault)();
7057
+ const credential = await loadGoogleCredential(applicationCredentials, projectId);
6981
7058
  const app = (0, firebase_admin_app.initializeApp)({
6982
7059
  credential,
6983
7060
  projectId
@@ -7853,7 +7930,7 @@ const runInit = async ({ build, envFile, secretsEnv }) => {
7853
7930
  }
7854
7931
  }]);
7855
7932
  await deployFirestore(tmpDir, initializeVariable.databaseId, nonInteractive, cliEnv);
7856
- const credential = applicationCredentials ? (0, firebase_admin_app.cert)(JSON.parse(await fs.default.promises.readFile(applicationCredentials, "utf-8"))) : (0, firebase_admin_app.applicationDefault)();
7933
+ const credential = await loadGoogleCredential(applicationCredentials, initializeVariable.projectId);
7857
7934
  await Promise.all((0, firebase_admin_app.getApps)().map((app) => (0, firebase_admin_app.deleteApp)(app)));
7858
7935
  const databaseConfig = {
7859
7936
  credential,
@@ -6856,14 +6856,67 @@ const createFirestoreStore = ({ firestore, collection, nativePageSize }) => {
6856
6856
  };
6857
6857
  };
6858
6858
  //#endregion
6859
+ //#region src/googleCredential.ts
6860
+ const CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
6861
+ /**
6862
+ * Resolves application default credentials as Firestore will, from
6863
+ * GOOGLE_APPLICATION_CREDENTIALS's file or the environment, for the app's
6864
+ * `projectId`, so it neither exchanges a token nor discovers a project
6865
+ * Firestore itself wouldn't. Only `applicationDefault()`'s credential: a
6866
+ * service account's or refresh token's was read from its file already.
6867
+ */
6868
+ const resolveApplicationDefault = async (credential, projectId) => {
6869
+ if (credential.constructor.name !== "ApplicationDefaultCredential") return;
6870
+ const { GoogleAuth } = await import("google-auth-library");
6871
+ await new GoogleAuth({
6872
+ scopes: [CLOUD_PLATFORM_SCOPE],
6873
+ ...projectId === void 0 ? {} : { projectId }
6874
+ }).getClient();
6875
+ };
6876
+ //#endregion
6859
6877
  //#region src/firebaseDatabase.ts
6878
+ /**
6879
+ * `store` that loads `credential` before its first call: one that cannot
6880
+ * load, such as GOOGLE_APPLICATION_CREDENTIALS naming a missing file, fails
6881
+ * that call with what is wrong. Firestore's own client would also reject a
6882
+ * promise nobody awaits, which ends a CLI process with a stack trace.
6883
+ */
6884
+ const withLoadedCredential = (store, credential, projectId) => {
6885
+ let loaded;
6886
+ const load = () => loaded ??= resolveApplicationDefault(credential, projectId).then(() => void 0, (error) => {
6887
+ loaded = void 0;
6888
+ throw new Error(`Could not load the Google credentials for Firestore: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
6889
+ });
6890
+ const { deleteConsumed } = store;
6891
+ return {
6892
+ ...store,
6893
+ get: async (keys) => {
6894
+ await load();
6895
+ return store.get(keys);
6896
+ },
6897
+ query: async (range) => {
6898
+ await load();
6899
+ return store.query(range);
6900
+ },
6901
+ write: async (ops) => {
6902
+ await load();
6903
+ return store.write(ops);
6904
+ },
6905
+ ...deleteConsumed && { deleteConsumed: async (keys) => {
6906
+ await load();
6907
+ return deleteConsumed.call(store, keys);
6908
+ } }
6909
+ };
6910
+ };
6860
6911
  const adapterOf = ({ databaseId, collection = FIREBASE_V1_COLLECTION, aggregateBatching: _, ...appOptions }) => {
6861
6912
  const app = getApps().length ? getApp() : initializeApp(appOptions);
6862
6913
  const firestore = databaseId ? getFirestore(app, databaseId) : getFirestore(app);
6863
- return createKvAdapter({ store: createFirestoreStore({
6914
+ const store = createFirestoreStore({
6864
6915
  firestore,
6865
6916
  collection
6866
- }) });
6917
+ });
6918
+ const credential = app.options.credential;
6919
+ return createKvAdapter({ store: appOptions.credential === void 0 || credential === void 0 || process.env["FIRESTORE_EMULATOR_HOST"] ? store : withLoadedCredential(store, credential, app.options.projectId) });
6867
6920
  };
6868
6921
  /**
6869
6922
  * Writes the schema settings of core and `plugins`, the plugins the server
@@ -6969,8 +7022,32 @@ const firebaseEngineSettingDocumentId = () => firestoreDocumentId({
6969
7022
  pk: SETTINGS_TABLE.name,
6970
7023
  sk: encodeKvKey([ENGINE_SETTING])
6971
7024
  });
7025
+ /**
7026
+ * The Google credential init reads Firestore with, once it loads: the
7027
+ * service account key file `applicationCredentials` names, or the
7028
+ * application default credentials. Before any change, an InitError says
7029
+ * what is wrong with one that does not load, where Firestore's client would
7030
+ * end init with a stack trace.
7031
+ */
7032
+ const loadGoogleCredential = async (applicationCredentials, projectId) => {
7033
+ const next = "Set GOOGLE_APPLICATION_CREDENTIALS to a service account key file, then rerun init.";
7034
+ let credential;
7035
+ try {
7036
+ credential = applicationCredentials ? cert(JSON.parse(await fs$1.readFile(applicationCredentials, "utf8"))) : applicationDefault();
7037
+ } catch {
7038
+ throw new InitError(`GOOGLE_APPLICATION_CREDENTIALS names ${applicationCredentials}, which is not a readable service account key file. ${next}`);
7039
+ }
7040
+ if (process.env["FIRESTORE_EMULATOR_HOST"]) return credential;
7041
+ try {
7042
+ await resolveApplicationDefault(credential, projectId);
7043
+ } catch (error) {
7044
+ const reason = (error instanceof Error ? error.message : String(error)).split(/(?<=\.)\s/u)[0].replace(/\.$/u, "");
7045
+ throw new InitError(`Could not load Google credentials: ${reason}. ${next}`);
7046
+ }
7047
+ return credential;
7048
+ };
6972
7049
  const assertFirebaseInfrastructureCanInitialize = async ({ applicationCredentials, databaseId, projectId }) => {
6973
- const credential = applicationCredentials ? cert(JSON.parse(await fs$1.readFile(applicationCredentials, "utf8"))) : applicationDefault();
7050
+ const credential = await loadGoogleCredential(applicationCredentials, projectId);
6974
7051
  const app = initializeApp({
6975
7052
  credential,
6976
7053
  projectId
@@ -7846,7 +7923,7 @@ const runInit = async ({ build, envFile, secretsEnv }) => {
7846
7923
  }
7847
7924
  }]);
7848
7925
  await deployFirestore(tmpDir, initializeVariable.databaseId, nonInteractive, cliEnv);
7849
- const credential = applicationCredentials ? cert(JSON.parse(await fs.promises.readFile(applicationCredentials, "utf-8"))) : applicationDefault();
7926
+ const credential = await loadGoogleCredential(applicationCredentials, initializeVariable.projectId);
7850
7927
  await Promise.all(getApps().map((app) => deleteApp(app)));
7851
7928
  const databaseConfig = {
7852
7929
  credential,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hot-updater/firebase",
3
3
  "type": "module",
4
- "version": "1.2.0-rc.49",
4
+ "version": "1.2.0-rc.50",
5
5
  "description": "React Native OTA solution for self-hosted",
6
6
  "main": "dist/index.cjs",
7
7
  "types": "dist/index.d.cts",
@@ -38,10 +38,10 @@
38
38
  "dependencies": {
39
39
  "hono": "4.12.34",
40
40
  "google-auth-library": "10.7.0",
41
- "@hot-updater/protocol": "1.2.0-rc.49",
42
- "@hot-updater/cli-tools": "1.2.0-rc.49",
43
- "@hot-updater/server": "1.2.0-rc.49",
44
- "@hot-updater/plugin-core": "1.2.0-rc.49"
41
+ "@hot-updater/protocol": "1.2.0-rc.50",
42
+ "@hot-updater/cli-tools": "1.2.0-rc.50",
43
+ "@hot-updater/plugin-core": "1.2.0-rc.50",
44
+ "@hot-updater/server": "1.2.0-rc.50"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"
@@ -56,8 +56,8 @@
56
56
  "firebase-tools": "15.29.0",
57
57
  "fkill": "^9.0.0",
58
58
  "mime": "^4.0.4",
59
- "@hot-updater/mock": "1.2.0-rc.49",
60
- "@hot-updater/test-utils": "1.2.0-rc.49"
59
+ "@hot-updater/mock": "1.2.0-rc.50",
60
+ "@hot-updater/test-utils": "1.2.0-rc.50"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "firebase-admin": ">=11.10.0",