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

Moving the emulator UI server to firebase-tools. #7897

Merged
merged 16 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions src/emulator/downloadableEmulators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const lsofi = require("lsofi");

Check warning on line 1 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 1 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Require statement not part of import statement
import {
Emulators,
DownloadableEmulators,
Expand Down Expand Up @@ -274,15 +274,15 @@
shell: false,
},
pubsub: {
binary: `${getExecPath(Emulators.PUBSUB)!}`,

Check warning on line 277 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
args: [],
optionalArgs: ["port", "host"],
joinArgs: true,
shell: true,
},
ui: {
binary: "node",
args: [getExecPath(Emulators.UI)],
binary: "",
args: [],
optionalArgs: [],
joinArgs: false,
shell: false,
Expand Down Expand Up @@ -310,7 +310,7 @@
}

/**
* @param name

Check warning on line 313 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc @param "name" description
*/
export function getLogFileName(name: string): string {
return `${name}-debug.log`;
Expand All @@ -323,7 +323,7 @@
*/
export function _getCommand(
emulator: DownloadableEmulators,
args: { [s: string]: any },

Check warning on line 326 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
): DownloadableEmulatorCommand {
const baseCmd = Commands[emulator];
const defaultPort = Constants.getDefaultPort(emulator);
Expand All @@ -336,7 +336,7 @@
if (
baseCmd.binary === "java" &&
utils.isRunningInWSL() &&
(!args.host || !args.host.includes(":"))

Check warning on line 339 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .includes on an `any` value

Check warning on line 339 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe call of an `any` typed value
) {
// HACK(https://github.com/firebase/firebase-tools-ui/issues/332): Force
// Java to use IPv4 sockets in WSL (unless IPv6 address explicitly used).
Expand All @@ -355,7 +355,7 @@
}

const argKey = "--" + key;
const argVal = args[key];

Check warning on line 358 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

if (argVal === undefined) {
logger.log("DEBUG", `Ignoring empty arg for key: ${key}`);
Expand All @@ -364,9 +364,9 @@

// Sigh ... RTDB emulator needs "--arg val" and PubSub emulator needs "--arg=val"
if (baseCmd.joinArgs) {
cmdLineArgs.push(`${argKey}=${argVal}`);

Check warning on line 367 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "any" of template literal expression
} else {
cmdLineArgs.push(argKey, argVal);

Check warning on line 369 in src/emulator/downloadableEmulators.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `string`
}
});

Expand Down Expand Up @@ -527,7 +527,7 @@
*/
export function getDownloadDetails(emulator: DownloadableEmulators): EmulatorDownloadDetails {
const details = DownloadDetails[emulator];
const pathOverride = process.env[`${emulator.toUpperCase()}_EMULATOR_BINARY_PATH`];
const pathOverride = process.env[`${emulator.toUpperCase()}_EMULATOR_BINARY_PATH`]; // FIXME care about this for UI?
if (pathOverride) {
const logger = EmulatorLogger.forEmulator(emulator);
logger.logLabeled(
Expand Down
1 change: 1 addition & 0 deletions src/emulator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type DownloadableEmulators =
| Emulators.UI
| Emulators.STORAGE
| Emulators.DATACONNECT;

export const DOWNLOADABLE_EMULATORS = [
Emulators.FIRESTORE,
Emulators.DATABASE,
Expand Down
107 changes: 77 additions & 30 deletions src/emulator/ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { EmulatorInstance, EmulatorInfo, Emulators, ListenSpec } from "./types";
import * as express from "express";
import * as path from "path";
import fetch from "node-fetch";
import { Emulators, ListenSpec } from "./types";
import * as downloadableEmulators from "./downloadableEmulators";
import { EmulatorRegistry } from "./registry";
import { FirebaseError } from "../error";
import { EmulatorLogger } from "./emulatorLogger";
import { Constants } from "./constants";
import { emulatorSession } from "../track";
import { ExpressBasedEmulator } from "./ExpressBasedEmulator";
Expand All @@ -13,55 +17,98 @@
auto_download?: boolean;
}

export class EmulatorUI implements EmulatorInstance {
constructor(private args: EmulatorUIOptions) {}
export class EmulatorUI extends ExpressBasedEmulator {

Check failure on line 21 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Delete `⏎`

Check failure on line 21 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Delete `⏎`

Check failure on line 21 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Delete `⏎`
start(): Promise<void> {
constructor(private args: EmulatorUIOptions) {
super({
listen: args.listen,
});
}

override async start(): Promise<void> {
christhompsongoogle marked this conversation as resolved.
Show resolved Hide resolved
await super.start();
}

protected override async createExpressApp(): Promise<express.Express> {
if (!EmulatorRegistry.isRunning(Emulators.HUB)) {
throw new FirebaseError(
`Cannot start ${Constants.description(Emulators.UI)} without ${Constants.description(
Emulators.HUB,
)}!`,
);
}
const { auto_download: autoDownload, projectId } = this.args;
const env: Partial<NodeJS.ProcessEnv> = {
LISTEN: JSON.stringify(ExpressBasedEmulator.listenOptionsFromSpecs(this.args.listen)),
GCLOUD_PROJECT: projectId,
[Constants.FIREBASE_EMULATOR_HUB]: EmulatorRegistry.url(Emulators.HUB).host,
};
const app = await super.createExpressApp();
const { projectId } = this.args;
const enabledExperiments: Array<ExperimentName> = (Object.keys(ALL_EXPERIMENTS) as Array<ExperimentName>).filter(

Check failure on line 42 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `Object.keys(ALL_EXPERIMENTS)·as·Array<ExperimentName>).filter(⏎······(experimentName)·=>·isEnabled(experimentName),⏎····` with `⏎······Object.keys(ALL_EXPERIMENTS)·as·Array<ExperimentName>⏎····).filter((experimentName)·=>·isEnabled(experimentName)`

Check failure on line 42 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `Object.keys(ALL_EXPERIMENTS)·as·Array<ExperimentName>).filter(⏎······(experimentName)·=>·isEnabled(experimentName),⏎····` with `⏎······Object.keys(ALL_EXPERIMENTS)·as·Array<ExperimentName>⏎····).filter((experimentName)·=>·isEnabled(experimentName)`

Check failure on line 42 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `Object.keys(ALL_EXPERIMENTS)·as·Array<ExperimentName>).filter(⏎······(experimentName)·=>·isEnabled(experimentName),⏎····` with `⏎······Object.keys(ALL_EXPERIMENTS)·as·Array<ExperimentName>⏎····).filter((experimentName)·=>·isEnabled(experimentName)`
(experimentName) => isEnabled(experimentName),
);
const emulatorGaSession = emulatorSession();

const session = emulatorSession();
if (session) {
env[Constants.FIREBASE_GA_SESSION] = JSON.stringify(session);
}
await downloadableEmulators.downloadIfNecessary(Emulators.UI);
const downloadDetails = downloadableEmulators.getDownloadDetails(Emulators.UI);
const webDir = path.join(downloadDetails.unzipDir!!, 'client');

Check failure on line 49 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `'client'` with `"client"`

Check failure on line 49 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'client'` with `"client"`

Check failure on line 49 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'client'` with `"client"`

const enabledExperiments = (Object.keys(ALL_EXPERIMENTS) as Array<ExperimentName>).filter(
(experimentName) => isEnabled(experimentName),
// Exposes the host and port of various emulators to facilitate accessing
// them using client SDKs. For features that involve multiple emulators or
// hard to accomplish using client SDKs, consider adding an API below.
app.get(
'/api/config',

Check failure on line 55 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `'/api/config'` with `"/api/config"`

Check failure on line 55 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'/api/config'` with `"/api/config"`

Check failure on line 55 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'/api/config'` with `"/api/config"`
this.jsonHandler(async () => {
const hubDiscoveryUrl = new URL(`http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`);

Check failure on line 57 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace ``http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`` with `⏎··········`http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`,⏎········`

Check failure on line 57 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace ``http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`` with `⏎··········`http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`,⏎········`

Check failure on line 57 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace ``http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`` with `⏎··········`http://${EmulatorRegistry.url(Emulators.HUB).host}/emulators`,⏎········`
const emulatorsRes = await fetch(hubDiscoveryUrl.toString());
Fixed Show fixed Hide fixed
christhompsongoogle marked this conversation as resolved.
Show resolved Hide resolved
const emulators = (await emulatorsRes.json()) as any;
yuchenshi marked this conversation as resolved.
Show resolved Hide resolved

const json = { projectId, experiments: [], ...emulators };

// Googlers: see go/firebase-emulator-ui-usage-collection-design?pli=1#heading=h.jwz7lj6r67z8
// for more detail
if (emulatorGaSession) {
json.analytics = emulatorGaSession;
}

// pick up any experiments enabled with `firebase experiment:enable`
if (enabledExperiments) {
json.experiments = enabledExperiments;
}

return json;
})

Check failure on line 75 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `,`

Check failure on line 75 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `,`

Check failure on line 75 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `,`
);
env[Constants.FIREBASE_ENABLED_EXPERIMENTS] = JSON.stringify(enabledExperiments);

return downloadableEmulators.start(Emulators.UI, { auto_download: autoDownload }, env);
app.use(express.static(webDir));
// Required for the router to work properly.
app.get('*', function (_, res) {

Check failure on line 80 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `'*'` with `"*"`

Check failure on line 80 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected function expression

Check failure on line 80 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'*'` with `"*"`

Check failure on line 80 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Unexpected function expression

Check failure on line 80 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'*'` with `"*"`

Check failure on line 80 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Unexpected function expression
res.sendFile(path.join(webDir, 'index.html'));

Check failure on line 81 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `'index.html'` with `"index.html"`

Check failure on line 81 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'index.html'` with `"index.html"`

Check failure on line 81 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'index.html'` with `"index.html"`
});
Fixed Show fixed Hide fixed

return app

Check failure on line 84 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `;`

Check failure on line 84 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`

Check failure on line 84 in src/emulator/ui.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`
}

connect(): Promise<void> {
return Promise.resolve();
}

stop(): Promise<void> {
return downloadableEmulators.stop(Emulators.UI);
getName(): Emulators {
return Emulators.UI;
}

getInfo(): EmulatorInfo {
return {
name: this.getName(),
host: this.args.listen[0].address,
port: this.args.listen[0].port,
pid: downloadableEmulators.getPID(Emulators.UI),
jsonHandler(
handler: (req: express.Request) => Promise<object>
): express.Handler {
return (req, res) => {
handler(req).then(
(body) => {
res.status(200).json(body);
},
(err) => {
EmulatorLogger.forEmulator(Emulators.UI).log("ERROR", err);
res.status(500).json({
message: err.message,
stack: err.stack,
raw: err,
});
}
);
};
}

getName(): Emulators {
return Emulators.UI;
}
}
Loading