Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include appId in WebConfig #3267

Merged
merged 6 commits into from
Apr 14, 2021
Merged

Include appId in WebConfig #3267

merged 6 commits into from
Apr 14, 2021

Conversation

samtstern
Copy link
Contributor

Description

Fixes #2798

Scenarios Tested

Fetch __init.js with a site<>webapp linked:

$ http http:https://localhost:5090/__/firebase/init.js?useEmulator=true
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 1900
Content-Type: application/javascript
Date: Thu, 08 Apr 2021 13:28:40 GMT
Keep-Alive: timeout=5

if (typeof firebase === 'undefined') throw new Error('hosting/init-error: Firebase SDK not detected. You must include it before /__/firebase/init.js');
var firebaseConfig = {
  "projectId": "fir-dumpster",
  "appId": "1:733471010301:web:1d80ddc9385abaa7d7fd94",
  "databaseURL": "https://fir-dumpster.firebaseio.com",
  "storageBucket": "fir-dumpster.appspot.com",
  "locationId": "us-central",
  "apiKey": "AIzaSyD3Vh8KNo27y8uF6z3hCPx7vJ20i_KjBPk",
  "authDomain": "fir-dumpster.firebaseapp.com",
  "messagingSenderId": "733471010301"
};
if (firebaseConfig) {
  firebase.initializeApp(firebaseConfig);

  var firebaseEmulators = {};
  if (firebaseEmulators) {
    console.log("Automatically connecting Firebase SDKs to running emulators:");
    Object.keys(firebaseEmulators).forEach(function(key) {
      console.log('\t' + key + ': http:https://' +  firebaseEmulators[key].host + ':' + firebaseEmulators[key].port );
    });

    if (firebaseEmulators.database && typeof firebase.database === 'function') {
      firebase.database().useEmulator(firebaseEmulators.database.host, firebaseEmulators.database.port);
    }

    if (firebaseEmulators.firestore && typeof firebase.firestore === 'function') {
      firebase.firestore().useEmulator(firebaseEmulators.firestore.host, firebaseEmulators.firestore.port);
    }

    if (firebaseEmulators.functions && typeof firebase.functions === 'function') {
      firebase.functions().useEmulator(firebaseEmulators.functions.host, firebaseEmulators.functions.port);
    }

    if (firebaseEmulators.auth && typeof firebase.auth === 'function') {
      firebase.auth().useEmulator('http:https://' + firebaseEmulators.auth.host + ':' + firebaseEmulators.auth.port);
    }
  } else {
    console.log("To automatically connect the Firebase SDKs to running emulators, replace '/__/firebase/init.js' with '/__/firebase/init.js?useEmulator=true' in your index.html");
  }
}

Fetch __init.js with a totally bogus project ID:

$ http http:https://localhost:5090/__/firebase/init.js?useEmulator=true
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 1545
Content-Type: application/javascript
Date: Thu, 08 Apr 2021 13:30:51 GMT
Keep-Alive: timeout=5

if (typeof firebase === 'undefined') throw new Error('hosting/init-error: Firebase SDK not detected. You must include it before /__/firebase/init.js');
var firebaseConfig = undefined;
if (firebaseConfig) {
  firebase.initializeApp(firebaseConfig);

  var firebaseEmulators = {};
  if (firebaseEmulators) {
    console.log("Automatically connecting Firebase SDKs to running emulators:");
    Object.keys(firebaseEmulators).forEach(function(key) {
      console.log('\t' + key + ': http:https://' +  firebaseEmulators[key].host + ':' + firebaseEmulators[key].port );
    });

    if (firebaseEmulators.database && typeof firebase.database === 'function') {
      firebase.database().useEmulator(firebaseEmulators.database.host, firebaseEmulators.database.port);
    }

    if (firebaseEmulators.firestore && typeof firebase.firestore === 'function') {
      firebase.firestore().useEmulator(firebaseEmulators.firestore.host, firebaseEmulators.firestore.port);
    }

    if (firebaseEmulators.functions && typeof firebase.functions === 'function') {
      firebase.functions().useEmulator(firebaseEmulators.functions.host, firebaseEmulators.functions.port);
    }

    if (firebaseEmulators.auth && typeof firebase.auth === 'function') {
      firebase.auth().useEmulator('http:https://' + firebaseEmulators.auth.host + ':' + firebaseEmulators.auth.port);
    }
  } else {
    console.log("To automatically connect the Firebase SDKs to running emulators, replace '/__/firebase/init.js' with '/__/firebase/init.js?useEmulator=true' in your index.html");
  }
}

Sample Commands

@google-cla google-cla bot added the cla: yes Manual indication that this has passed CLA. label Apr 8, 2021
@samtstern samtstern requested a review from kmcnellis April 8, 2021 13:31
@samtstern
Copy link
Contributor Author

Bump @kmcnellis can you take a quick look at this one?

src/fetchWebSetup.ts Outdated Show resolved Hide resolved
let hostingAppId: string | undefined = undefined;
try {
const sites = await hostingApiClient.get<ListSitesResponse>(`/projects/${projectId}/sites`);
const defaultSite = sites.body.sites.find((s) => s.type === "DEFAULT_SITE");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the emulator support non-default sites?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, no. One day!


// Get the web app config for the appId, or use the '-' special value if the appId is not known
const appId = hostingAppId || "-";
const res = await apiClient.get<WebConfig>(`/projects/${projectId}/webApps/${appId}/config`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth a try/catch around this

Copy link
Contributor Author

@samtstern samtstern Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only caller has a try/catch that does the right thing. I'm gonna leave it rather than increase the scope of this refactor.

Copy link
Member

@kmcnellis kmcnellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a bogus project ID, should we make a bogus config object as well? Or does firebaseConfig = undefined allow everything to still work in the emulator? I don't think it should be a hard requirement to use a real project id with the emulators

@samtstern
Copy link
Contributor Author

@kmcnellis we're working on some emulator-suite-wide handling for bogus project IDs but right now this maintains the status quo: the emulator will start in a somewhat degraded mode with a bogus project ID.

@samtstern samtstern merged commit 122f448 into master Apr 14, 2021
@bkendall bkendall deleted the ss-fix-2798 branch August 4, 2021 19:27
devpeerapong pushed a commit to devpeerapong/firebase-tools that referenced this pull request Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Manual indication that this has passed CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Local __init.js does not include appId
2 participants