@hot-updater/firebase 1.2.0-rc.6 → 1.2.0-rc.61

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.
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
- import { ConfigBuilder, HOT_UPDATER_SERVER_PACKAGE_VERSION_ENV, InitError, LegacyInfrastructureError, MissingInitInputsError, assembleServer, assertInfrastructureGenerationAtUrl, assertInitProviderInputs, confirmInitInputPersistence, copyDirToTmp, createHotUpdaterConfigScaffoldFromBuilder, getHotUpdaterInitInputEnv, getInitProviderEnvVars, getInitProviderTextPromptValues, link, makeEnv, p, printAppSetup, provisionClientCredential, readHotUpdaterInitEnv, resolveHotUpdaterServerVersion, resolveInitProviderInput, resolvePackageVersion, transformEnv, writeHotUpdaterFiles } from "@hot-updater/cli-tools";
4
+ import { ConfigBuilder, HOT_UPDATER_SERVER_PACKAGE_VERSION_ENV, InitError, LegacyInfrastructureError, MissingInitInputsError, assembleServer, assertInfrastructureGenerationAtUrl, assertInitProviderInputs, backfillInsightsReleaseRefs, confirmInitInputPersistence, copyDirToTmp, createHotUpdaterConfigScaffoldFromBuilder, fillReleaseLabels, getHotUpdaterInitInputEnv, getInitProviderEnvVars, getInitProviderTextPromptValues, link, makeEnv, p, printAppSetup, provisionClientCredential, readHotUpdaterInitEnv, resolveHotUpdaterServerVersion, resolveInitProviderInput, resolvePackageVersion, transformEnv, writeHotUpdaterFiles } from "@hot-updater/cli-tools";
5
5
  import { apiKeys, insights, remoteConfig } from "@hot-updater/server/plugins";
6
6
  import { fileURLToPath } from "node:url";
7
7
  import { ChildProcess, execFile, spawn, spawnSync } from "node:child_process";
@@ -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
@@ -7816,7 +7893,7 @@ const runInit = async ({ build, envFile, secretsEnv }) => {
7816
7893
  });
7817
7894
  await makeEnv(persistedInputs);
7818
7895
  await removeTmpDir();
7819
- return;
7896
+ return null;
7820
7897
  }
7821
7898
  const functionsCode = transformEnv(functionsIndexPath, {
7822
7899
  REGION: currentRegion,
@@ -7846,8 +7923,8 @@ 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();
7850
- const existingApps = new Set(getApps());
7926
+ const credential = await loadGoogleCredential(applicationCredentials, initializeVariable.projectId);
7927
+ await Promise.all(getApps().map((app) => deleteApp(app)));
7851
7928
  const databaseConfig = {
7852
7929
  credential,
7853
7930
  databaseId: initializeVariable.databaseId,
@@ -7865,6 +7942,7 @@ const runInit = async ({ build, envFile, secretsEnv }) => {
7865
7942
  let clientCredential;
7866
7943
  try {
7867
7944
  await migrateFirebaseDatabase(databaseConfig, server.plugins);
7945
+ await fillReleaseLabels(server);
7868
7946
  clientCredential = await provisionClientCredential(server, {
7869
7947
  env: initInputEnv,
7870
7948
  name: "Firebase init"
@@ -7872,7 +7950,7 @@ const runInit = async ({ build, envFile, secretsEnv }) => {
7872
7950
  if (clientCredential !== void 0) await makeEnv({ [clientCredential.env]: clientCredential.value });
7873
7951
  } finally {
7874
7952
  await database.dispose?.();
7875
- await Promise.all(getApps().filter((app) => !existingApps.has(app)).map((app) => deleteApp(app)));
7953
+ await Promise.all(getApps().map((app) => deleteApp(app)));
7876
7954
  }
7877
7955
  await deployFunctions(tmpDir, nonInteractive, cliEnv);
7878
7956
  await p.tasks([{
@@ -7917,11 +7995,19 @@ const runInit = async ({ build, envFile, secretsEnv }) => {
7917
7995
  await removeTmpDir();
7918
7996
  process.exit(1);
7919
7997
  }
7998
+ const backfillDatabase = firebaseDatabase(databaseConfig);
7999
+ try {
8000
+ await backfillInsightsReleaseRefs(assembleServer({
8001
+ database: backfillDatabase,
8002
+ plugins: server.plugins
8003
+ }));
8004
+ } finally {
8005
+ await backfillDatabase.dispose?.();
8006
+ await Promise.all(getApps().map((app) => deleteApp(app)));
8007
+ }
7920
8008
  await printTemplate(clientCredential, initializeVariable.projectId, currentRegion, server.clientPlugins, cliEnv);
7921
8009
  await removeTmpDir();
7922
- p.log.message(`Next step: ${link("https://hot-updater.dev/docs/managed/firebase#step-3-add-hotupdater-to-your-project")}`);
7923
- if (!applicationCredentials) p.log.message("Next step: Change GOOGLE_APPLICATION_CREDENTIALS=your-credentials.json in .env.hotupdater");
7924
- p.log.success("Done! 🎉");
8010
+ return { nextSteps: [link("https://hot-updater.dev/docs/managed/firebase#step-3-add-hotupdater-to-your-project"), ...applicationCredentials ? [] : ["Change GOOGLE_APPLICATION_CREDENTIALS=your-credentials.json in .env.hotupdater"]] };
7925
8011
  };
7926
8012
  //#endregion
7927
8013
  export { initProvider, mergeIndexes, runInit, setFirestoreDatabase };
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.6",
4
+ "version": "1.2.0-rc.61",
5
5
  "description": "React Native OTA solution for self-hosted",
6
6
  "main": "dist/index.cjs",
7
7
  "types": "dist/index.d.cts",
@@ -36,12 +36,12 @@
36
36
  "package.json"
37
37
  ],
38
38
  "dependencies": {
39
- "hono": "4.12.34",
39
+ "hono": "4.13.21",
40
40
  "google-auth-library": "10.7.0",
41
- "@hot-updater/cli-tools": "1.2.0-rc.6",
42
- "@hot-updater/protocol": "1.2.0-rc.6",
43
- "@hot-updater/plugin-core": "1.2.0-rc.6",
44
- "@hot-updater/server": "1.2.0-rc.6"
41
+ "@hot-updater/protocol": "1.2.0-rc.61",
42
+ "@hot-updater/plugin-core": "1.2.0-rc.61",
43
+ "@hot-updater/server": "1.2.0-rc.61",
44
+ "@hot-updater/cli-tools": "1.2.0-rc.61"
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.6",
60
- "@hot-updater/test-utils": "1.2.0-rc.6"
59
+ "@hot-updater/mock": "1.2.0-rc.61",
60
+ "@hot-updater/test-utils": "1.2.0-rc.61"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "firebase-admin": ">=11.10.0",