Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Added `*_EMULATOR_VERSION` env variables to allow overriding specific versions of downloadable emulators
- Updated the functions.config deprecation notice from March 2026 to March 2027 (#9941)
- Detects when App Hosting fails to deploy, returning an error. (#8866)
4 changes: 3 additions & 1 deletion src/downloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @param remoteUrl URL to download.
* @param auth Whether to include an access token in the download request. Defaults to false.
*/
export async function downloadToTmp(remoteUrl: string, auth: boolean = false): Promise<string> {

Check warning on line 15 in src/downloadUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Type boolean trivially inferred from a boolean literal, remove type annotation
const u = new URL(remoteUrl);
const c = new Client({ urlPrefix: u.origin, auth });
const tmpfile = tmp.fileSync();
Expand All @@ -27,7 +27,9 @@
resolveOnHTTPError: true,
});
if (res.status !== 200) {
throw new FirebaseError(`download failed, status ${res.status}: ${await res.response.text()}`);
throw new FirebaseError(`download failed, status ${res.status}: ${await res.response.text()}`, {
status: res.status,
});
}

const total = parseInt(res.response.headers.get("content-length") || "0", 10);
Expand Down
21 changes: 20 additions & 1 deletion src/emulator/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,43 @@

tmp.setGracefulCleanup();

export async function downloadEmulator(name: DownloadableEmulators): Promise<void> {

Check warning on line 15 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const emulator = downloadableEmulators.getDownloadDetails(name);
if (emulator.localOnly) {
EmulatorLogger.forEmulator(name).logLabeled(
"WARN",
name,
`Env variable override detected, skipping download. Using ${emulator} emulator at ${emulator.binaryPath}`,

Check warning on line 21 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "string | undefined" of template literal expression

Check warning on line 21 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "EmulatorDownloadDetails" of template literal expression
);
return;
}
const overrideVersion = downloadableEmulators.emulatorVersionOverride(name);
if (overrideVersion) {
EmulatorLogger.forEmulator(name).logLabeled(
"WARN",
name,
`Env variable override detected. Using custom ${name} emulator version ${overrideVersion}.`,
);
}
EmulatorLogger.forEmulator(name).logLabeled(
"BULLET",
name,
`downloading ${path.basename(emulator.downloadPath)}...`,
);
fs.ensureDirSync(emulator.opts.cacheDir);

const tmpfile = await downloadUtils.downloadToTmp(emulator.opts.remoteUrl, !!emulator.opts.auth);
let tmpfile: string;
try {
tmpfile = await downloadUtils.downloadToTmp(emulator.opts.remoteUrl, !!emulator.opts.auth);
} catch (err: any) {

Check warning on line 43 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (overrideVersion && err instanceof FirebaseError && err.status === 404) {
throw new FirebaseError(
`env variable ${name.toUpperCase()}_EMULATOR_VERSION set to ${overrideVersion},
but no such version of ${name} was found. Please double check the version number, or unset this environment variable to use the latest default.`,
);
}
throw err;
}

if (!emulator.opts.skipChecksumAndSize) {
await validateSize(tmpfile, emulator.opts.expectedSize);
Expand All @@ -51,7 +70,7 @@
removeOldFiles(name, emulator);
}

export async function downloadExtensionVersion(

Check warning on line 73 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
extensionVersionRef: string,
sourceDownloadUri: string,
targetDir: string,
Expand Down Expand Up @@ -94,7 +113,7 @@
for (const file of files) {
const fullFilePath = path.join(emulator.opts.cacheDir, file);

if (file.indexOf(emulator.opts.namePrefix) < 0) {

Check warning on line 116 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Use 'includes()' method instead
// This file is not related to this emulator, could be a JAR
// from a different emulator or just a random file.
continue;
Expand Down Expand Up @@ -138,7 +157,7 @@
return new Promise((resolve, reject) => {
const hash = crypto.createHash("md5");
const stream = fs.createReadStream(filepath);
stream.on("data", (data: any) => hash.update(data));

Check warning on line 160 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `BinaryLike`

Check warning on line 160 in src/emulator/download.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
stream.on("end", () => {
const checksum = hash.digest("hex");
return checksum === expectedChecksum
Expand Down
18 changes: 18 additions & 0 deletions src/emulator/downloadableEmulators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,22 @@ describe("downloadDetails", () => {
emulatorUpdateDetails.dataconnect.darwin_arm64.remoteUrl,
);
});

it("should override emulator version when PUBSUB_EMULATOR_VERSION is set", () => {
const fakeVersion = "1.2.3";
sandbox.stub(process, "env").value({
...process.env,
PUBSUB_EMULATOR_VERSION: fakeVersion,
});

const pubsubEmulatorDetails = downloadableEmulators.getDownloadDetails(Emulators.PUBSUB);
expect(pubsubEmulatorDetails.version).to.equal(fakeVersion);
expect(pubsubEmulatorDetails.downloadPath).to.contain(fakeVersion);
expect(pubsubEmulatorDetails.opts.remoteUrl).to.contain(fakeVersion);
expect(pubsubEmulatorDetails.opts.skipChecksumAndSize).to.be.true;

expect(downloadableEmulators.emulatorVersionOverride(Emulators.FIRESTORE)).to.be.undefined;
expect(downloadableEmulators.emulatorVersionOverride(Emulators.DATABASE)).to.be.undefined;
expect(downloadableEmulators.emulatorVersionOverride(Emulators.PUBSUB)).to.equal(fakeVersion);
});
});
43 changes: 37 additions & 6 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
import {
Emulators,
DownloadableEmulators,
Expand Down Expand Up @@ -66,9 +66,11 @@
? EMULATOR_UPDATE_DETAILS.dataconnect.win32
: EMULATOR_UPDATE_DETAILS.dataconnect.linux;

let details: EmulatorDownloadDetails;
const overrideVersion = emulatorVersionOverride(emulator);
switch (emulator) {
case "database":
return {
details = {
downloadPath: path.join(
CACHE_DIR,
EMULATOR_UPDATE_DETAILS.database.downloadPathRelativeToCacheDir,
Expand All @@ -80,8 +82,9 @@
namePrefix: "firebase-database-emulator",
},
};
break;
case "firestore":
return {
details = {
downloadPath: path.join(
CACHE_DIR,
EMULATOR_UPDATE_DETAILS.firestore.downloadPathRelativeToCacheDir,
Expand All @@ -93,8 +96,9 @@
namePrefix: "cloud-firestore-emulator",
},
};
break;
case "storage":
return {
details = {
downloadPath: path.join(
CACHE_DIR,
EMULATOR_UPDATE_DETAILS.storage.downloadPathRelativeToCacheDir,
Expand All @@ -106,8 +110,9 @@
namePrefix: "cloud-storage-rules-emulator",
},
};
break;
case "ui":
return {
details = {
version: emulatorUiDetails.version,
downloadPath: path.join(CACHE_DIR, emulatorUiDetails.downloadPathRelativeToCacheDir),
unzipDir: path.join(CACHE_DIR, `ui-v${emulatorUiDetails.version}`),
Expand All @@ -120,8 +125,9 @@
namePrefix: "ui",
},
};
break;
case "pubsub":
return {
details = {
downloadPath: path.join(
CACHE_DIR,
EMULATOR_UPDATE_DETAILS.pubsub.downloadPathRelativeToCacheDir,
Expand All @@ -138,8 +144,9 @@
namePrefix: "pubsub-emulator",
},
};
break;
case "dataconnect":
return {
details = {
downloadPath: path.join(CACHE_DIR, dataconnectDetails.downloadPathRelativeToCacheDir),
version: dataconnectDetails.version,
binaryPath: path.join(CACHE_DIR, dataconnectDetails.downloadPathRelativeToCacheDir),
Expand All @@ -151,9 +158,29 @@
auth: false,
},
};
break;
default:
throw new Error(`Invalid downloadable emulator: ${emulator}`);
}

if (overrideVersion && overrideVersion !== details.version) {
const oldVersion = details.version;
const replaceVersion = (s: string) => s.split(oldVersion).join(overrideVersion);

details.version = overrideVersion;
details.downloadPath = replaceVersion(details.downloadPath);
Copy link
Member

Choose a reason for hiding this comment

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

I'd love to have extra error handling for the case where the user sets a version number that doesn't exist in our bucket. Might require some more piping, but ideally, it should print an error message like:

<OVERRIDE_ENV_VAR> set to 99.99.99, but no such version of <emulator name> was found.  Please double check the version number, or unset this environment variable to use the latest default.

if (details.unzipDir) {
details.unzipDir = replaceVersion(details.unzipDir);
}
if (details.binaryPath) {
details.binaryPath = replaceVersion(details.binaryPath);
}

details.opts.remoteUrl = replaceVersion(details.opts.remoteUrl);
details.opts.skipChecksumAndSize = true;
}

return details;
}

const EmulatorDetails: { [s in DownloadableEmulators]: DownloadableEmulatorDetails } = {
Expand Down Expand Up @@ -631,3 +658,7 @@
process.platform === "darwin"
);
}

export function emulatorVersionOverride(emulator: DownloadableEmulators) {
return process.env[`${emulator.toUpperCase()}_EMULATOR_VERSION`];
}
Loading