diff --git a/spec/v1/config.spec.ts b/spec/v1/config.spec.ts index 97d7b6faa..45d51f09c 100644 --- a/spec/v1/config.spec.ts +++ b/spec/v1/config.spec.ts @@ -21,53 +21,16 @@ // SOFTWARE. import { expect } from "chai"; -import * as fs from "fs"; -import * as process from "process"; -import * as sinon from "sinon"; -import { config, resetCache } from "../../src/v1/config"; +import { config } from "../../src/v1/config"; describe("config()", () => { - let readFileSync: sinon.SinonStub; - let cwdStub: sinon.SinonStub; - - before(() => { - readFileSync = sinon.stub(fs, "readFileSync"); - readFileSync.throws("Unexpected call"); - cwdStub = sinon.stub(process, "cwd"); - cwdStub.returns("/srv"); - }); - - after(() => { - sinon.verifyAndRestore(); - }); - - afterEach(() => { - resetCache(); - delete process.env.FIREBASE_CONFIG; - delete process.env.CLOUD_RUNTIME_CONFIG; - delete process.env.K_CONFIGURATION; - }); - - it("will never load in GCFv2", () => { - const json = JSON.stringify({ - foo: "bar", - firebase: {}, - }); - readFileSync.withArgs("/srv/.runtimeconfig.json").returns(Buffer.from(json)); - - process.env.K_CONFIGURATION = "my-service"; - expect(config).to.throw(Error, /transition to using environment variables/); - }); - - it("loads config values from .runtimeconfig.json", () => { - const json = JSON.stringify({ - foo: "bar", - firebase: {}, - }); - readFileSync.withArgs("/srv/.runtimeconfig.json").returns(Buffer.from(json)); - const loaded = config(); - expect(loaded).to.not.have.property("firebase"); - expect(loaded).to.have.property("foo", "bar"); + it("throws an error with migration guidance", () => { + expect(config).to.throw( + Error, + "functions.config() has been removed in firebase-functions v7. " + + "Migrate to environment parameters using the params module. " + + "Migration guide: https://firebase.google.com/docs/functions/config-env#migrate-config" + ); }); }); diff --git a/src/v1/config.ts b/src/v1/config.ts index 2eafa3150..6b1522f33 100644 --- a/src/v1/config.ts +++ b/src/v1/config.ts @@ -20,63 +20,17 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import * as fs from "fs"; -import * as path from "path"; - export { firebaseConfig } from "../common/config"; -/** @internal */ -export let singleton: Record; - -/** @internal */ -export function resetCache(): void { - singleton = undefined; -} - /** - * Store and retrieve project configuration data such as third-party API - * keys or other settings. You can set configuration values using the - * Firebase CLI as described in - * https://firebase.google.com/docs/functions/config-env. - * - * @deprecated Using functions.config() is discouraged. See https://firebase.google.com/docs/functions/config-env. + * @deprecated `functions.config()` has been removed in firebase-functions v7. + * Migrate to environment parameters using the `params` module immediately. + * Migration guide: https://firebase.google.com/docs/functions/config-env#migrate-config */ export function config(): Record { - // K_CONFIGURATION is only set in GCFv2 - if (process.env.K_CONFIGURATION) { - throw new Error( - "functions.config() is no longer available in Cloud Functions for " + - "Firebase v2. Please see the latest documentation for information " + - "on how to transition to using environment variables" - ); - } - if (typeof singleton === "undefined") { - init(); - } - return singleton; -} - -function init() { - try { - const parsed = JSON.parse(process.env.CLOUD_RUNTIME_CONFIG); - delete parsed.firebase; - singleton = parsed; - return; - } catch (e) { - // Do nothing - } - - try { - const configPath = - process.env.CLOUD_RUNTIME_CONFIG || path.join(process.cwd(), ".runtimeconfig.json"); - const contents = fs.readFileSync(configPath); - const parsed = JSON.parse(contents.toString("utf8")); - delete parsed.firebase; - singleton = parsed; - return; - } catch (e) { - // Do nothing - } - - singleton = {}; + throw new Error( + "functions.config() has been removed in firebase-functions v7. " + + "Migrate to environment parameters using the params module. " + + "Migration guide: https://firebase.google.com/docs/functions/config-env#migrate-config" + ); }