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
12 changes: 12 additions & 0 deletions src/deploy/functions/release/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { getProjectNumber } from "../../../getProjectNumber";
import { release as extRelease } from "../../extensions";
import * as artifacts from "../../../functions/artifacts";
import { runtimeIsLanguage } from "../runtimes/supported";
Comment thread
inlined marked this conversation as resolved.
Resolved

/** Releases new versions of functions and extensions to prod. */
export async function release(
Expand Down Expand Up @@ -114,9 +115,20 @@
const wantBackend = backend.merge(...Object.values(payload.functions).map((p) => p.wantBackend));
printTriggerUrls(wantBackend, projectNumber);

// TODO: Remove once the Firebase console has support.
if (
backend.someEndpoint(wantBackend, (endpoint) => runtimeIsLanguage(endpoint.runtime, "dart"))
) {
utils.logLabeledBullet(
"functions",
"Dart functions may not yet be visible in the Firebase Console. " +
`View them in the Cloud Console at https://console.cloud.google.com/run/services?project=${context.projectId}`,
);
}

await setupArtifactCleanupPolicies(
options,
options.projectId!,

Check warning on line 131 in src/deploy/functions/release/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
Object.keys(wantBackend.endpoints),
);

Expand Down
7 changes: 1 addition & 6 deletions src/deploy/functions/runtimes/supported/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ export function isRuntime(maybe: string): maybe is Runtime {

/** Type deduction helper to narrow a runtime to a language. */
export function runtimeIsLanguage<L extends Language>(
runtime: Runtime,
runtime: Runtime | undefined,
language: L,
): runtime is Runtime & RuntimeOf<L> {
return runtime.startsWith(language);
}

/** Check if an optional runtime string belongs to a given language. */
export function isLanguageRuntime(runtime: string | undefined, language: Language): boolean {
return !!runtime && runtime.startsWith(language);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In TypeScript I know we can do thing like runtime?.startsWith(language) instead. I'm assuming the original code went this way so there's a very explicit false return value? Works for me but just checking.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I love null coalescing, but functions that return type information rather than booleans are weeeeeird. E.g. you have to say if ("field" in object) and not if (object.field)

}

Expand Down
13 changes: 6 additions & 7 deletions src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import { resolveBackend } from "../deploy/functions/build";
import { getCredentialsEnvironment, setEnvVarsForEmulators } from "./env";
import { runWithVirtualEnv } from "../functions/python";
import { isLanguageRuntime, Runtime } from "../deploy/functions/runtimes/supported";
import { runtimeIsLanguage, Runtime } from "../deploy/functions/runtimes/supported";
import { DART_ENTRY_POINT } from "../deploy/functions/runtimes/dart";
import { ExtensionsEmulator } from "./extensionsEmulator";

Expand Down Expand Up @@ -303,7 +303,7 @@
createHubServer(): express.Application {
// TODO(samstern): Should not need this here but some tests are directly calling this method
// because FunctionsEmulator.start() used to not be test safe.
this.workQueue.start();

Check warning on line 306 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

const hub = express();

Expand Down Expand Up @@ -345,15 +345,15 @@
const multicastHandler: express.RequestHandler = (req: express.Request, res) => {
const projectId = req.params.project_id;
const rawBody = (req as RequestWithRawBody).rawBody;
const event = JSON.parse(rawBody.toString());

Check warning on line 348 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
let triggerKey: string;
if (req.headers["content-type"]?.includes("cloudevent")) {
triggerKey = `${this.args.projectId}:${event.type}`;

Check warning on line 351 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .type on an `any` value

Check warning on line 351 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "any" of template literal expression
} else {
triggerKey = `${this.args.projectId}:${event.eventType}`;

Check warning on line 353 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .eventType on an `any` value

Check warning on line 353 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "any" of template literal expression
}
if (event.data.bucket) {

Check warning on line 355 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .data on an `any` value
triggerKey += `:${event.data.bucket}`;

Check warning on line 356 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .data on an `any` value

Check warning on line 356 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "any" of template literal expression
}
const triggers = this.multicastTriggers[triggerKey] || [];

Expand Down Expand Up @@ -424,7 +424,7 @@

// For Dart, include the function name in the path so the server can route
// For other runtimes, use / as they use FUNCTION_TARGET env var
const isDart = isLanguageRuntime(record.backend.runtime, "dart");
const isDart = runtimeIsLanguage(record.backend.runtime, "dart");
const path = isDart ? `/${trigger.entryPoint}` : `/`;

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -485,7 +485,7 @@
// First load triggers to discover the runtime type
await this.loadTriggers(backend, /* force= */ true);

const isDart = isLanguageRuntime(backend.runtime, "dart");
const isDart = runtimeIsLanguage(backend.runtime, "dart");

if (isDart) {
// For Dart, build_runner watch handles source file watching and rebuilds
Expand Down Expand Up @@ -1771,9 +1771,9 @@
const secretEnvs = await this.resolveSecretEnvs(backend, trigger);

let runtime;
if (isLanguageRuntime(backend.runtime, "python")) {
if (runtimeIsLanguage(backend.runtime, "python")) {
runtime = await this.startPython(backend, { ...runtimeEnv, ...secretEnvs });
} else if (isLanguageRuntime(backend.runtime, "dart")) {
} else if (runtimeIsLanguage(backend.runtime, "dart")) {
runtime = await this.startDart(backend, { ...runtimeEnv, ...secretEnvs });
} else {
runtime = await this.startNode(backend, { ...runtimeEnv, ...secretEnvs });
Expand Down Expand Up @@ -1884,8 +1884,7 @@
// Use trigger.entryPoint (e.g. "helloworld") which is the actual function name
// registered in the Dart server, not trigger_name which may include region prefix
// (e.g. "us-central1-helloworld-0" from background function routes).
const isDart = isLanguageRuntime(record.backend.runtime, "dart");
if (isDart) {
if (runtimeIsLanguage(record.backend.runtime, "dart")) {
// Background trigger routes (e.g., /functions/projects/.../triggers/...)
// leave a path artifact (/functions/projects/) after regex replacement.
// Only append remaining path for HTTP trigger routes where the user may
Expand Down
18 changes: 9 additions & 9 deletions src/emulator/functionsRuntimeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EmulatorLogger, ExtensionLogInfo } from "./emulatorLogger";
import { FirebaseError } from "../error";
import { Serializable } from "child_process";
import { getFunctionDiscoveryTimeout } from "../deploy/functions/runtimes/discovery";
import { isLanguageRuntime } from "../deploy/functions/runtimes/supported";
import { Runtime, runtimeIsLanguage } from "../deploy/functions/runtimes/supported";

type LogListener = (el: EmulatorLog) => any;

Expand Down Expand Up @@ -297,13 +297,13 @@ export class RuntimeWorkerPool {

constructor(private mode: FunctionsExecutionMode = FunctionsExecutionMode.AUTO) {}

getKey(triggerId: string | undefined, runtime?: string): string {
getKey(triggerId: string | undefined, runtime?: Runtime): string {
if (this.mode === FunctionsExecutionMode.SEQUENTIAL) {
return "~shared~";
}
// For Dart, use a shared key so all functions in a codebase share the same worker process.
// Dart loads all functions into a single process and routes based on request path.
if (isLanguageRuntime(runtime, "dart")) {
if (runtimeIsLanguage(runtime, "dart")) {
return "~dart-shared~";
}
return triggerId || "~diagnostic~";
Expand Down Expand Up @@ -350,7 +350,7 @@ export class RuntimeWorkerPool {
*
* @param triggerId
*/
readyForWork(triggerId: string | undefined, runtime?: string): boolean {
readyForWork(triggerId: string | undefined, runtime?: Runtime): boolean {
const idleWorker = this.getIdleWorker(triggerId, runtime);
return !!idleWorker;
}
Expand All @@ -371,7 +371,7 @@ export class RuntimeWorkerPool {
resp: http.ServerResponse,
body: unknown,
debug?: FunctionsRuntimeBundle["debug"],
runtime?: string,
runtime?: Runtime,
): Promise<void> {
this.log(`submitRequest(triggerId=${triggerId})`);
const worker = this.getIdleWorker(triggerId, runtime);
Expand All @@ -386,7 +386,7 @@ export class RuntimeWorkerPool {
return worker.request(req, resp, body, !!debug);
}

getIdleWorker(triggerId: string | undefined, runtime?: string): RuntimeWorker | undefined {
getIdleWorker(triggerId: string | undefined, runtime?: Runtime): RuntimeWorker | undefined {
this.cleanUpWorkers();
const triggerWorkers = this.getTriggerWorkers(triggerId, runtime);
if (!triggerWorkers.length) {
Expand All @@ -412,7 +412,7 @@ export class RuntimeWorkerPool {
trigger: EmulatedTriggerDefinition | undefined,
runtime: FunctionsRuntimeInstance,
extensionLogInfo: ExtensionLogInfo,
runtimeType?: string,
runtimeType?: Runtime,
): RuntimeWorker {
const key = this.getKey(trigger?.id, runtimeType);
this.log(`addWorker(${key})`);
Expand All @@ -435,14 +435,14 @@ export class RuntimeWorkerPool {
return worker;
}

getTriggerWorkers(triggerId: string | undefined, runtime?: string): Array<RuntimeWorker> {
getTriggerWorkers(triggerId: string | undefined, runtime?: Runtime): Array<RuntimeWorker> {
return this.workers.get(this.getKey(triggerId, runtime)) || [];
}

private setTriggerWorkers(
triggerId: string | undefined,
workers: Array<RuntimeWorker>,
runtime?: string,
runtime?: Runtime,
) {
this.workers.set(this.getKey(triggerId, runtime), workers);
}
Expand Down
Loading