Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 8 additions & 45 deletions spec/v1/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});
});
62 changes: 8 additions & 54 deletions src/v1/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;

/** @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<string, any> {
// 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"
);
}
Loading