UNPKG

7.09 kBTypeScriptView Raw
1import { ResetValue } from "../../common/options";
2import { CloudEvent, CloudFunction } from "../core";
3import { Expression } from "../../params";
4import * as options from "../options";
5import { SecretParam } from "../../params/types";
6/**
7 * Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
8 * You can create a function ({@link onMessagePublished}) that handles pub/sub events by using functions.pubsub.
9 *
10 * This function triggers whenever a new pub/sub message is sent to a specific topic.
11 * You must specify the Pub/Sub topic name that you want to trigger your function, and set the event within the
12 * onPublish() event handler.
13 *
14 * PubSub Topic:
15 * <ul>
16 * <li>A resource that you can publish messages to and then consume those messages via subscriptions.
17 * <li>An isolated data stream for pub/sub messages.
18 * <li>Messages are published to a topic.
19 * <li>Messages are listened to via a subscription.
20 * <li>Each subscription listens to the messages published to exactly one topic.
21 *
22 * Subscriptions - Resource that listens to the messages published by exactly one topic.
23 *
24 * [More info here](https://firebase.google.com/docs/functions/pubsub-events)
25 */
26/**
27 * Interface representing a Google Cloud Pub/Sub message.
28 *
29 * @param data - Payload of a Pub/Sub message.
30 * @typeParam T - Type representing `Message.data`'s JSON format
31 */
32export declare class Message<T> {
33 /**
34 * Autogenerated ID that uniquely identifies this message.
35 */
36 readonly messageId: string;
37 /**
38 * Time the message was published
39 */
40 readonly publishTime: string;
41 /**
42 * The data payload of this message object as a base64-encoded string.
43 */
44 readonly data: string;
45 /**
46 * User-defined attributes published with the message, if any.
47 */
48 readonly attributes: {
49 [key: string]: string;
50 };
51 /**
52 * User-defined key used to ensure ordering amongst messages with the same key.
53 */
54 readonly orderingKey: string;
55 /** @hidden */
56 private _json;
57 /**
58 * @hidden
59 * @alpha
60 */
61 constructor(data: any);
62 /**
63 * The JSON data payload of this message object, if any.
64 */
65 get json(): T;
66 /**
67 * Returns a JSON-serializable representation of this object.
68 *
69 * @returns A JSON-serializable representation of this object.
70 */
71 toJSON(): any;
72}
73/**
74 * The interface published in a Pub/Sub publish subscription.
75 * @typeParam T - Type representing `Message.data`'s JSON format
76 */
77export interface MessagePublishedData<T = any> {
78 /** Google Cloud Pub/Sub message. */
79 readonly message: Message<T>;
80 /** A subscription resource. */
81 readonly subscription: string;
82}
83/** PubSubOptions extend EventHandlerOptions but must include a topic. */
84export interface PubSubOptions extends options.EventHandlerOptions {
85 /** The Pub/Sub topic to watch for message events */
86 topic: string;
87 /**
88 * If true, do not deploy or emulate this function.
89 */
90 omit?: boolean | Expression<boolean>;
91 /**
92 * Region where functions should be deployed.
93 */
94 region?: options.SupportedRegion | string | Expression<string> | ResetValue;
95 /**
96 * Amount of memory to allocate to a function.
97 */
98 memory?: options.MemoryOption | Expression<number> | ResetValue;
99 /**
100 * Timeout for the function in seconds, possible values are 0 to 540.
101 * HTTPS functions can specify a higher timeout.
102 *
103 * @remarks
104 * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
105 * function depends on the type of function: Event handling functions have a
106 * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
107 * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
108 * timeout of 1,800s (30 minutes)
109 */
110 timeoutSeconds?: number | Expression<number> | ResetValue;
111 /**
112 * Min number of actual instances to be running at a given time.
113 *
114 * @remarks
115 * Instances will be billed for memory allocation and 10% of CPU allocation
116 * while idle.
117 */
118 minInstances?: number | Expression<number> | ResetValue;
119 /**
120 * Max number of instances to be running in parallel.
121 */
122 maxInstances?: number | Expression<number> | ResetValue;
123 /**
124 * Number of requests a function can serve at once.
125 *
126 * @remarks
127 * Can only be applied to functions running on Cloud Functions v2.
128 * A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
129 * Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
130 * The maximum value for concurrency is 1,000.
131 */
132 concurrency?: number | Expression<number> | ResetValue;
133 /**
134 * Fractional number of CPUs to allocate to a function.
135 *
136 * @remarks
137 * Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
138 * This is different from the defaults when using the gcloud utility and is different from
139 * the fixed amount assigned in Google Cloud Functions generation 1.
140 * To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
141 * to the value "gcf_gen1"
142 */
143 cpu?: number | "gcf_gen1";
144 /**
145 * Connect cloud function to specified VPC connector.
146 */
147 vpcConnector?: string | Expression<string> | ResetValue;
148 /**
149 * Egress settings for VPC connector.
150 */
151 vpcConnectorEgressSettings?: options.VpcEgressSetting | ResetValue;
152 /**
153 * Specific service account for the function to run as.
154 */
155 serviceAccount?: string | Expression<string> | ResetValue;
156 /**
157 * Ingress settings which control where this function can be called from.
158 */
159 ingressSettings?: options.IngressSetting | ResetValue;
160 /**
161 * User labels to set on the function.
162 */
163 labels?: Record<string, string>;
164 secrets?: (string | SecretParam)[];
165 /** Whether failed executions should be delivered again. */
166 retry?: boolean | Expression<boolean> | ResetValue;
167}
168/**
169 * Handle a message being published to a Pub/Sub topic.
170 * @param topic - The Pub/Sub topic to watch for message events.
171 * @param handler - runs every time a Cloud Pub/Sub message is published
172 * @typeParam T - Type representing `Message.data`'s JSON format
173 */
174export declare function onMessagePublished<T = any>(topic: string, handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>): CloudFunction<CloudEvent<MessagePublishedData<T>>>;
175/**
176 * Handle a message being published to a Pub/Sub topic.
177 * @param options - Option containing information (topic) for event
178 * @param handler - runs every time a Cloud Pub/Sub message is published
179 * @typeParam T - Type representing `Message.data`'s JSON format
180 */
181export declare function onMessagePublished<T = any>(options: PubSubOptions, handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>): CloudFunction<CloudEvent<MessagePublishedData<T>>>;