|  | 
|  | 1 | +/** | 
|  | 2 | + * @license | 
|  | 3 | + * Copyright 2025 Google LLC | 
|  | 4 | + * | 
|  | 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | + * you may not use this file except in compliance with the License. | 
|  | 7 | + * You may obtain a copy of the License at | 
|  | 8 | + * | 
|  | 9 | + *   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | + * | 
|  | 11 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 12 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | + * See the License for the specific language governing permissions and | 
|  | 15 | + * limitations under the License. | 
|  | 16 | + */ | 
|  | 17 | + | 
|  | 18 | +const { writeFile, readFile } = require('node:fs/promises'); | 
|  | 19 | +const { pathToFileURL } = require('node:url'); | 
|  | 20 | +const { isAbsolute, join } = require('node:path'); | 
|  | 21 | + | 
|  | 22 | +const ENV_VARIABLE = 'FIREBASE_WEBAPP_CONFIG'; | 
|  | 23 | + | 
|  | 24 | +async function getPartialConfig() { | 
|  | 25 | +  const envVariable = process.env[ENV_VARIABLE]?.trim(); | 
|  | 26 | + | 
|  | 27 | +  if (!envVariable) { | 
|  | 28 | +    return undefined; | 
|  | 29 | +  } | 
|  | 30 | + | 
|  | 31 | +  // Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be | 
|  | 32 | +  // either a JSON representation of FirebaseOptions or the path to a filename | 
|  | 33 | +  if (envVariable.startsWith('{"')) { | 
|  | 34 | +    try { | 
|  | 35 | +      return JSON.parse(envVariable); | 
|  | 36 | +    } catch (e) { | 
|  | 37 | +      console.warn( | 
|  | 38 | +        `JSON payload in \$${ENV_VARIABLE} could not be parsed, ignoring.\n`, | 
|  | 39 | +        e | 
|  | 40 | +      ); | 
|  | 41 | +      return undefined; | 
|  | 42 | +    } | 
|  | 43 | +  } | 
|  | 44 | + | 
|  | 45 | +  const fileURL = pathToFileURL( | 
|  | 46 | +    isAbsolute(envVariable) ? envVariable : join(process.cwd(), envVariable) | 
|  | 47 | +  ); | 
|  | 48 | + | 
|  | 49 | +  try { | 
|  | 50 | +    const fileContents = await readFile(fileURL, 'utf-8'); | 
|  | 51 | +    return JSON.parse(fileContents); | 
|  | 52 | +  } catch (e) { | 
|  | 53 | +    console.warn( | 
|  | 54 | +      `Contents of "${envVariable}" could not be parsed, ignoring \$${ENV_VARIABLE}.\n`, | 
|  | 55 | +      e | 
|  | 56 | +    ); | 
|  | 57 | +    return undefined; | 
|  | 58 | +  } | 
|  | 59 | +} | 
|  | 60 | + | 
|  | 61 | +async function getFinalConfig(partialConfig) { | 
|  | 62 | +  if (!partialConfig) { | 
|  | 63 | +    return undefined; | 
|  | 64 | +  } | 
|  | 65 | +  // In Firebase App Hosting the config provided to the environment variable is up-to-date and | 
|  | 66 | +  // "complete" we should not reach out to the webConfig endpoint to freshen it | 
|  | 67 | +  if (process.env.X_GOOGLE_TARGET_PLATFORM === 'fah') { | 
|  | 68 | +    return partialConfig; | 
|  | 69 | +  } | 
|  | 70 | +  const projectId = partialConfig.projectId || '-'; | 
|  | 71 | +  // If the projectId starts with demo- this is an demo project from the firebase emulators | 
|  | 72 | +  // treat the config as whole | 
|  | 73 | +  if (projectId.startsWith('demo-')) { | 
|  | 74 | +    return partialConfig; | 
|  | 75 | +  } | 
|  | 76 | +  const appId = partialConfig.appId; | 
|  | 77 | +  const apiKey = partialConfig.apiKey; | 
|  | 78 | +  if (!appId || !apiKey) { | 
|  | 79 | +    console.warn( | 
|  | 80 | +      `Unable to fetch Firebase config, appId and apiKey are required, ignoring \$${ENV_VARIABLE}.` | 
|  | 81 | +    ); | 
|  | 82 | +    return undefined; | 
|  | 83 | +  } | 
|  | 84 | + | 
|  | 85 | +  const url = `https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`; | 
|  | 86 | + | 
|  | 87 | +  try { | 
|  | 88 | +    const response = await fetch(url, { | 
|  | 89 | +      headers: { 'x-goog-api-key': apiKey } | 
|  | 90 | +    }); | 
|  | 91 | +    if (!response.ok) { | 
|  | 92 | +      console.warn( | 
|  | 93 | +        `Unable to fetch Firebase config, ignoring \$${ENV_VARIABLE}.` | 
|  | 94 | +      ); | 
|  | 95 | +      console.warn( | 
|  | 96 | +        `${url} returned ${response.statusText} (${response.status})` | 
|  | 97 | +      ); | 
|  | 98 | +      try { | 
|  | 99 | +        console.warn((await response.json()).error.message); | 
|  | 100 | +      } catch (e) {} | 
|  | 101 | +      return undefined; | 
|  | 102 | +    } | 
|  | 103 | +    const json = await response.json(); | 
|  | 104 | +    return { ...json, apiKey }; | 
|  | 105 | +  } catch (e) { | 
|  | 106 | +    console.warn( | 
|  | 107 | +      `Unable to fetch Firebase config, ignoring \$${ENV_VARIABLE}.\n`, | 
|  | 108 | +      e | 
|  | 109 | +    ); | 
|  | 110 | +    return undefined; | 
|  | 111 | +  } | 
|  | 112 | +} | 
|  | 113 | + | 
|  | 114 | +function handleUnexpectedError(e) { | 
|  | 115 | +  console.warn( | 
|  | 116 | +    `Unexpected error encountered in @firebase/util postinstall script, ignoring \$${ENV_VARIABLE}.` | 
|  | 117 | +  ); | 
|  | 118 | +  console.warn(e); | 
|  | 119 | +  process.exit(0); | 
|  | 120 | +} | 
|  | 121 | + | 
|  | 122 | +getPartialConfig() | 
|  | 123 | +  .catch(handleUnexpectedError) | 
|  | 124 | +  .then(getFinalConfig) | 
|  | 125 | +  .catch(handleUnexpectedError) | 
|  | 126 | +  .then(async finalConfig => { | 
|  | 127 | +    const defaults = finalConfig && { | 
|  | 128 | +      config: finalConfig, | 
|  | 129 | +      emulatorHosts: { | 
|  | 130 | +        firestore: process.env.FIRESTORE_EMULATOR_HOST, | 
|  | 131 | +        database: process.env.FIREBASE_DATABASE_EMULATOR_HOST, | 
|  | 132 | +        storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST, | 
|  | 133 | +        auth: process.env.FIREBASE_AUTH_EMULATOR_HOST | 
|  | 134 | +      } | 
|  | 135 | +    }; | 
|  | 136 | + | 
|  | 137 | +    await Promise.all([ | 
|  | 138 | +      writeFile( | 
|  | 139 | +        join(__dirname, 'dist', 'postinstall.js'), | 
|  | 140 | +        `'use strict'; | 
|  | 141 | +Object.defineProperty(exports, '__esModule', { value: true }); | 
|  | 142 | +exports.getDefaultsFromPostinstall = () => (${JSON.stringify(defaults)});` | 
|  | 143 | +      ), | 
|  | 144 | +      writeFile( | 
|  | 145 | +        join(__dirname, 'dist', 'postinstall.mjs'), | 
|  | 146 | +        `const getDefaultsFromPostinstall = () => (${JSON.stringify(defaults)}); | 
|  | 147 | +export { getDefaultsFromPostinstall };` | 
|  | 148 | +      ) | 
|  | 149 | +    ]); | 
|  | 150 | + | 
|  | 151 | +    process.exit(0); | 
|  | 152 | +  }) | 
|  | 153 | +  .catch(handleUnexpectedError); | 
0 commit comments