Skip to content
Draft
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
79 changes: 1 addition & 78 deletions src/emulator/functionsEmulatorShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
import * as path from "path";
import * as fs from "fs";
import { randomBytes } from "crypto";
import * as _ from "lodash";
import * as express from "express";
import { CloudFunction } from "firebase-functions";

import * as backend from "../deploy/functions/backend";
import * as build from "../deploy/functions/build";
import { Constants } from "./constants";
import { BackendInfo, EmulatableBackend, InvokeRuntimeOpts } from "./functionsEmulator";
import { BackendInfo, EmulatableBackend } from "./functionsEmulator";
import { ENV_DIRECTORY } from "../extensions/manifest";
import { substituteParams } from "../extensions/extensionsHelper";
import { ExtensionSpec, ExtensionVersion } from "../extensions/types";
Expand Down Expand Up @@ -68,12 +64,12 @@
timeoutSeconds?: number;
regions?: string[];
availableMemoryMb?: backend.MemoryOptions;
httpsTrigger?: any;

Check warning on line 67 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type
eventTrigger?: EventTrigger;
taskQueueTrigger?: backend.TaskQueueTrigger;
schedule?: EventSchedule;
blockingTrigger?: BlockingTrigger;
labels?: { [key: string]: any };

Check warning on line 72 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type
codebase?: string;
}

Expand Down Expand Up @@ -103,17 +99,8 @@
service?: string;
}

export interface EmulatedTriggerMap {
[name: string]: EmulatedTrigger;
}

export interface FunctionsRuntimeArgs {
frb: FunctionsRuntimeBundle;
opts?: InvokeRuntimeOpts;
}

export interface FunctionsRuntimeBundle {
proto: any;

Check warning on line 103 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type
disabled_features?: FunctionsRuntimeFeatures;
// TODO(danielylee): To make debugging in Functions Emulator w/ --inspect-functions flag a good experience, we run
// all functions in a single runtime process. This is drastically different to production environment where each
Expand All @@ -131,35 +118,6 @@
timeout?: boolean;
}

export class EmulatedTrigger {
/*
Here we create a trigger from a single definition (data about what resources does this trigger on, etc) and
the actual module which contains multiple functions / definitions. We locate the one we need below using
definition.entryPoint
*/
constructor(
public definition: EmulatedTriggerDefinition,
private module: any,
) {}

get memoryLimitBytes(): number {
return (this.definition.availableMemoryMb || 128) * 1024 * 1024;
}

get timeoutMs(): number {
return (this.definition.timeoutSeconds || 60) * 1000;
}

getRawFunction(): CloudFunction<any> {
if (!this.module) {
throw new Error("EmulatedTrigger has not been provided a module.");
}

const func = _.get(this.module, this.definition.entryPoint);
return func.__emulator_func || func;
}
}

/**
* Checks if the v2 event service has been implemented in the emulator
*/
Expand All @@ -170,17 +128,17 @@
/**
* Validates that triggers are correctly formed and fills in some defaults.
*/
export function prepareEndpoints(endpoints: backend.Endpoint[]) {

Check warning on line 131 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Missing return type on function
const bkend = backend.of(...endpoints);
for (const ep of endpoints) {
serviceForEndpoint(ep).validateTrigger(ep as any, bkend);

Check warning on line 134 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type

Check warning on line 134 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe argument of type `any` assigned to a parameter of type `Endpoint`
}
inferBlockingDetails(bkend);
}

/**
* Creates a unique trigger definition from Endpoints.
* @param Endpoints A list of all CloudFunctions in the deployment.

Check warning on line 141 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Expected @PARAM names to be "endpoints". Got "Endpoints"
* @return A list of all CloudFunctions in the deployment.
*/
export function emulatedFunctionsFromEndpoints(
Expand Down Expand Up @@ -234,7 +192,7 @@
if (endpoint.platform === "gcfv1") {
def.eventTrigger = {
eventType: eventTrigger.eventType,
resource: eventTrigger.eventFilters!.resource,

Check warning on line 195 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Forbidden non-null assertion
};
} else {
// v2 events implemented are pubsub, storage, rtdb, firestore, alerts, auth, and custom events
Expand All @@ -243,7 +201,7 @@
}

// We use resource for pubsub & storage
const { resource, topic, bucket } = endpoint.eventTrigger.eventFilters as any;

Check warning on line 204 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type

Check warning on line 204 in src/emulator/functionsEmulatorShared.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe assignment of an `any` value
const eventResource = resource || topic || bucket;

def.eventTrigger = {
Expand Down Expand Up @@ -319,25 +277,6 @@
return regionDefinitions;
}

/**
* Converts an array of EmulatedTriggerDefinitions to a map of EmulatedTriggers, which contain information on execution,
* @param {EmulatedTriggerDefinition[]} definitions An array of regionalized, parsed trigger definitions
* @param {object} module Actual module which contains multiple functions / definitions
* @return a map of trigger ids to EmulatedTriggers
*/
export function getEmulatedTriggersFromDefinitions(
definitions: EmulatedTriggerDefinition[],
module: any, // eslint-disable-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
): EmulatedTriggerMap {
return definitions.reduce(
(obj: { [triggerName: string]: EmulatedTrigger }, definition: EmulatedTriggerDefinition) => {
obj[definition.id] = new EmulatedTrigger(definition, module);
return obj;
},
{},
);
}

/**
* Create a path that used to create a tempfile for IPC over socket files.
*/
Expand Down Expand Up @@ -429,22 +368,6 @@
return "";
}

/**
* Create a Promise which can be awaited to recieve request bodies as strings.
*/
export function waitForBody(req: express.Request): Promise<string> {
let data = "";
return new Promise((resolve) => {
req.on("data", (chunk: any) => {
data += chunk;
});

req.on("end", () => {
resolve(data);
});
});
}

/**
* Find the root directory housing a node module.
*/
Expand Down
Loading