From bb82085174f444011d381f341ce1c55a044554ac Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Fri, 9 Jul 2021 17:44:52 -0700 Subject: [PATCH 1/2] RemoteConfig can be loaded in windows CMD.exe --- spec/v1/config.spec.ts | 29 +++++++++++++++++++---------- src/v1/config.ts | 10 ++++++---- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/spec/v1/config.spec.ts b/spec/v1/config.spec.ts index 8a40fe523..d1deea4fd 100644 --- a/spec/v1/config.spec.ts +++ b/spec/v1/config.spec.ts @@ -22,27 +22,27 @@ import { expect } from 'chai'; import * as fs from 'fs'; -import * as mockRequire from 'mock-require'; +import * as process from 'process'; import Sinon = require('sinon'); import * as 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'); - process.env.PWD = '/srv'; + cwdStub = Sinon.stub(process, 'cwd'); + cwdStub.returns('/srv'); }); after(() => { - delete process.env.PWD; Sinon.verifyAndRestore(); }); afterEach(() => { - mockRequire.stopAll(); delete config.config.singleton; (config as any).firebaseConfigCache = null; delete process.env.FIREBASE_CONFIG; @@ -50,19 +50,27 @@ describe('config()', () => { }); it('loads config values from .runtimeconfig.json', () => { - mockRequire('/srv/.runtimeconfig.json', { foo: 'bar', firebase: {} }); + const json = JSON.stringify({ + foo: 'bar', + firebase: {}, + }); + readFileSync + .withArgs('/srv/.runtimeconfig.json') + .returns(Buffer.from(json)); const loaded = config.config(); expect(loaded).to.not.have.property('firebase'); expect(loaded).to.have.property('foo', 'bar'); }); it('does not provide firebase config if .runtimeconfig.json not invalid', () => { - mockRequire('/srv/.runtimeconfig.json', 'does-not-exist'); + readFileSync.withArgs('/srv/.runtimeconfig.json').returns('invalid JSON'); expect(config.firebaseConfig()).to.be.null; }); it('does not provide firebase config if .ruuntimeconfig.json has no firebase property', () => { - mockRequire('/srv/.runtimeconfig.json', {}); + readFileSync + .withArgs('/srv/.runtimeconfig.json') + .returns(Buffer.from('{}')); expect(config.firebaseConfig()).to.be.null; }); @@ -78,7 +86,7 @@ describe('config()', () => { it('loads Firebase configs from FIREBASE_CONFIG env variable pointing to a file', () => { const oldEnv = process.env; - process.env = { + (process as any).env = { ...oldEnv, FIREBASE_CONFIG: '.firebaseconfig.json', }; @@ -91,13 +99,14 @@ describe('config()', () => { 'foo@firebaseio.com' ); } finally { - process.env = oldEnv; + (process as any).env = oldEnv; } }); it('accepts alternative locations for config file', () => { process.env.CLOUD_RUNTIME_CONFIG = 'another.json'; - mockRequire('another.json', { foo: 'bar', firebase: {} }); + const json = JSON.stringify({ foo: 'bar', firebase: {} }); + readFileSync.withArgs('another.json').returns(Buffer.from(json)); expect(config.firebaseConfig()).to.not.be.null; expect(config.config()).to.have.property('foo', 'bar'); }); diff --git a/src/v1/config.ts b/src/v1/config.ts index c49e7d03f..f0793957d 100644 --- a/src/v1/config.ts +++ b/src/v1/config.ts @@ -89,8 +89,9 @@ export function firebaseConfig(): firebase.AppOptions | null { try { const configPath = process.env.CLOUD_RUNTIME_CONFIG || - path.join(process.env.PWD, '.runtimeconfig.json'); - const config = require(configPath); + path.join(process.cwd(), '.runtimeconfig.json'); + const contents = fs.readFileSync(configPath); + const config = JSON.parse(contents.toString('utf8')); if (config.firebase) { firebaseConfigCache = config.firebase; return firebaseConfigCache; @@ -115,8 +116,9 @@ function init() { try { const configPath = process.env.CLOUD_RUNTIME_CONFIG || - path.join(process.env.PWD, '.runtimeconfig.json'); - const parsed = require(configPath); + path.join(process.cwd(), '.runtimeconfig.json'); + const contents = fs.readFileSync(configPath); + const parsed = JSON.parse(contents.toString('utf8')); delete parsed.firebase; config.singleton = parsed; return; From e1e87ef8626ac7d81777c606ccf3af89072b232f Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Mon, 12 Jul 2021 11:26:02 -0700 Subject: [PATCH 2/2] Add relnotes --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index adfc05c3a..45844cd0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ - Adds support for setting user labels on functions via `runWith()`. - Adds support for FIREBASE_CONFIG env as the name of a JSON file - Formalize module exports. Loggers can now be accessed at 'firebase-functions/logger' and 'firebase-functions/logger/compat' +- Fixes an issue where Remote Config coiuld not be emulated in Windows machines on the classic Command Prompt.