1 | import * as express from "express";
|
2 | import { ResetValue } from "../../common/options";
|
3 | import { CallableRequest, FunctionsErrorCode, HttpsError, Request } from "../../common/providers/https";
|
4 | import { ManifestEndpoint } from "../../runtime/manifest";
|
5 | import { GlobalOptions, SupportedRegion } from "../options";
|
6 | import { Expression } from "../../params";
|
7 | import { SecretParam } from "../../params/types";
|
8 | import * as options from "../options";
|
9 | export { Request, CallableRequest, FunctionsErrorCode, HttpsError };
|
10 | /**
|
11 | * Options that can be set on an onRequest HTTPS function.
|
12 | */
|
13 | export interface HttpsOptions extends Omit<GlobalOptions, "region" | "enforceAppCheck"> {
|
14 | /**
|
15 | * If true, do not deploy or emulate this function.
|
16 | */
|
17 | omit?: boolean | Expression<boolean>;
|
18 | /** HTTP functions can override global options and can specify multiple regions to deploy to. */
|
19 | region?: SupportedRegion | string | Array<SupportedRegion | string> | Expression<string> | ResetValue;
|
20 | /** If true, allows CORS on requests to this function.
|
21 | * If this is a `string` or `RegExp`, allows requests from domains that match the provided value.
|
22 | * If this is an `Array`, allows requests from domains matching at least one entry of the array.
|
23 | * Defaults to true for {@link https.CallableFunction} and false otherwise.
|
24 | */
|
25 | cors?: string | boolean | RegExp | Array<string | RegExp>;
|
26 | /**
|
27 | * Amount of memory to allocate to a function.
|
28 | */
|
29 | memory?: options.MemoryOption | Expression<number> | ResetValue;
|
30 | /**
|
31 | * Timeout for the function in seconds, possible values are 0 to 540.
|
32 | * HTTPS functions can specify a higher timeout.
|
33 | *
|
34 | * @remarks
|
35 | * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
|
36 | * function depends on the type of function: Event handling functions have a
|
37 | * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
|
38 | * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
|
39 | * timeout of 1,800s (30 minutes)
|
40 | */
|
41 | timeoutSeconds?: number | Expression<number> | ResetValue;
|
42 | /**
|
43 | * Min number of actual instances to be running at a given time.
|
44 | *
|
45 | * @remarks
|
46 | * Instances will be billed for memory allocation and 10% of CPU allocation
|
47 | * while idle.
|
48 | */
|
49 | minInstances?: number | Expression<number> | ResetValue;
|
50 | /**
|
51 | * Max number of instances to be running in parallel.
|
52 | */
|
53 | maxInstances?: number | Expression<number> | ResetValue;
|
54 | /**
|
55 | * Number of requests a function can serve at once.
|
56 | *
|
57 | * @remarks
|
58 | * Can only be applied to functions running on Cloud Functions v2.
|
59 | * A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
|
60 | * Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
|
61 | * The maximum value for concurrency is 1,000.
|
62 | */
|
63 | concurrency?: number | Expression<number> | ResetValue;
|
64 | /**
|
65 | * Fractional number of CPUs to allocate to a function.
|
66 | *
|
67 | * @remarks
|
68 | * Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
|
69 | * This is different from the defaults when using the gcloud utility and is different from
|
70 | * the fixed amount assigned in Google Cloud Functions generation 1.
|
71 | * To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
|
72 | * to the value "gcf_gen1"
|
73 | */
|
74 | cpu?: number | "gcf_gen1";
|
75 | /**
|
76 | * Connect cloud function to specified VPC connector.
|
77 | */
|
78 | vpcConnector?: string | Expression<string> | ResetValue;
|
79 | /**
|
80 | * Egress settings for VPC connector.
|
81 | */
|
82 | vpcConnectorEgressSettings?: options.VpcEgressSetting | ResetValue;
|
83 | /**
|
84 | * Specific service account for the function to run as.
|
85 | */
|
86 | serviceAccount?: string | Expression<string> | ResetValue;
|
87 | /**
|
88 | * Ingress settings which control where this function can be called from.
|
89 | */
|
90 | ingressSettings?: options.IngressSetting | ResetValue;
|
91 | /**
|
92 | * User labels to set on the function.
|
93 | */
|
94 | labels?: Record<string, string>;
|
95 | secrets?: (string | SecretParam)[];
|
96 | /**
|
97 | * Invoker to set access control on https functions.
|
98 | */
|
99 | invoker?: "public" | "private" | string | string[];
|
100 | }
|
101 | /**
|
102 | * Options that can be set on a callable HTTPS function.
|
103 | */
|
104 | export interface CallableOptions extends HttpsOptions {
|
105 | /**
|
106 | * Determines whether Firebase AppCheck is enforced.
|
107 | * When true, requests with invalid tokens autorespond with a 401
|
108 | * (Unauthorized) error.
|
109 | * When false, requests with invalid tokens set event.app to undefiend.
|
110 | */
|
111 | enforceAppCheck?: boolean;
|
112 | /**
|
113 | * Determines whether Firebase App Check token is consumed on request. Defaults to false.
|
114 | *
|
115 | * @remarks
|
116 | * Set this to true to enable the App Check replay protection feature by consuming the App Check token on callable
|
117 | * request. Tokens that are found to be already consumed will have request.app.alreadyConsumed property set true.
|
118 | *
|
119 | *
|
120 | * Tokens are only considered to be consumed if it is sent to the App Check service by setting this option to true.
|
121 | * Other uses of the token do not consume it.
|
122 | *
|
123 | * This replay protection feature requires an additional network call to the App Check backend and forces the clients
|
124 | * to obtain a fresh attestation from the chosen attestation providers. This can therefore negatively impact
|
125 | * performance and can potentially deplete your attestation providers' quotas faster. Use this feature only for
|
126 | * protecting low volume, security critical, or expensive operations.
|
127 | *
|
128 | * This option does not affect the enforceAppCheck option. Setting the latter to true will cause the callable function
|
129 | * to automatically respond with a 401 Unauthorized status code when request includes an invalid App Check token.
|
130 | * When request includes valid but consumed App Check tokens, requests will not be automatically rejected. Instead,
|
131 | * the request.app.alreadyConsumed property will be set to true and pass the execution to the handler code for making
|
132 | * further decisions, such as requiring additional security checks or rejecting the request.
|
133 | */
|
134 | consumeAppCheckToken?: boolean;
|
135 | }
|
136 | /**
|
137 | * Handles HTTPS requests.
|
138 | */
|
139 | export type HttpsFunction = ((
|
140 | /** An Express request object representing the HTTPS call to the function. */
|
141 | req: Request,
|
142 | /** An Express response object, for this function to respond to callers. */
|
143 | res: express.Response) => void | Promise<void>) & {
|
144 | /** @alpha */
|
145 | __trigger?: unknown;
|
146 | /** @alpha */
|
147 | __endpoint: ManifestEndpoint;
|
148 | };
|
149 | /**
|
150 | * Creates a callable method for clients to call using a Firebase SDK.
|
151 | */
|
152 | export interface CallableFunction<T, Return> extends HttpsFunction {
|
153 | /** Executes the handler function with the provided data as input. Used for unit testing.
|
154 | * @param data - An input for the handler function.
|
155 | * @returns The output of the handler function.
|
156 | */
|
157 | run(data: CallableRequest<T>): Return;
|
158 | }
|
159 | /**
|
160 | * Handles HTTPS requests.
|
161 | * @param opts - Options to set on this function
|
162 | * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app.
|
163 | * @returns A function that you can export and deploy.
|
164 | */
|
165 | export declare function onRequest(opts: HttpsOptions, handler: (request: Request, response: express.Response) => void | Promise<void>): HttpsFunction;
|
166 | /**
|
167 | * Handles HTTPS requests.
|
168 | * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app.
|
169 | * @returns A function that you can export and deploy.
|
170 | */
|
171 | export declare function onRequest(handler: (request: Request, response: express.Response) => void | Promise<void>): HttpsFunction;
|
172 | /**
|
173 | * Declares a callable method for clients to call using a Firebase SDK.
|
174 | * @param opts - Options to set on this function.
|
175 | * @param handler - A function that takes a {@link https.CallableRequest}.
|
176 | * @returns A function that you can export and deploy.
|
177 | */
|
178 | export declare function onCall<T = any, Return = any | Promise<any>>(opts: CallableOptions, handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return extends Promise<unknown> ? Return : Promise<Return>>;
|
179 | /**
|
180 | * Declares a callable method for clients to call using a Firebase SDK.
|
181 | * @param handler - A function that takes a {@link https.CallableRequest}.
|
182 | * @returns A function that you can export and deploy.
|
183 | */
|
184 | export declare function onCall<T = any, Return = any | Promise<any>>(handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return extends Promise<unknown> ? Return : Promise<Return>>;
|