Skip to content
Open
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
15 changes: 10 additions & 5 deletions src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,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 311 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 @@ -350,15 +350,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 353 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 356 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 356 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 358 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 358 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 360 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 361 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 361 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 @@ -403,7 +403,7 @@
return hub;
}

async sendRequest(trigger: EmulatedTriggerDefinition, body?: any) {

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

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
const record = this.getTriggerRecordByKey(this.getTriggerKey(trigger));
const pool = this.workerPools[record.backend.codebase];
if (!pool.readyForWork(trigger.id, record.backend.runtime)) {
Expand Down Expand Up @@ -449,11 +449,16 @@
}

async start(): Promise<void> {
const credentialEnv = await getCredentialsEnvironment(
this.args.account,
this.logger,
"functions",
);
let credentialEnv: Record<string, string> = {};
// Skip fetching credentials for demo projects to avoid hanging on network calls
// to the GCP metadata server in local environments without credentials.
if (!Constants.isDemoProject(this.args.projectId)) {
credentialEnv = await getCredentialsEnvironment(

Check failure on line 456 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `⏎········this.args.account,⏎········this.logger,⏎········"functions",⏎······` with `this.args.account,·this.logger,·"functions"`

Check failure on line 456 in src/emulator/functionsEmulator.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `⏎········this.args.account,⏎········this.logger,⏎········"functions",⏎······` with `this.args.account,·this.logger,·"functions"`
this.args.account,
this.logger,
"functions",
);
}
Comment on lines +452 to +461
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.

high

The fix is incomplete because getCredentialsEnvironment is also called in loadDynamicExtensionBackends (line 282), which will still cause the emulator to hang when using demo projects with extensions.

Additionally, you can simplify this logic using a ternary operator to avoid the let variable and reduce nesting, which aligns with the repository's style guide.

    // Skip fetching credentials for demo projects to avoid hanging on network calls
    // to the GCP metadata server in local environments without credentials.
    const credentialEnv = Constants.isDemoProject(this.args.projectId)
      ? {}
      : await getCredentialsEnvironment(this.args.account, this.logger, "functions");
References
  1. Reduce nesting as much as possible. Code should avoid unnecessarily deep nesting or long periods of nesting. (link)

for (const e of this.staticBackends) {
e.env = { ...credentialEnv, ...e.env };
}
Expand Down
Loading