UNPKG

6.83 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.onMessagePublished = exports.Message = void 0;
25/**
26 * Cloud functions to handle events from Google Cloud Pub/Sub.
27 * @packageDocumentation
28 */
29const encoding_1 = require("../../common/encoding");
30const manifest_1 = require("../../runtime/manifest");
31const trace_1 = require("../trace");
32const options = require("../options");
33const onInit_1 = require("../../common/onInit");
34/**
35 * Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
36 * You can create a function ({@link onMessagePublished}) that handles pub/sub events by using functions.pubsub.
37 *
38 * This function triggers whenever a new pub/sub message is sent to a specific topic.
39 * You must specify the Pub/Sub topic name that you want to trigger your function, and set the event within the
40 * onPublish() event handler.
41 *
42 * PubSub Topic:
43 * <ul>
44 * <li>A resource that you can publish messages to and then consume those messages via subscriptions.
45 * <li>An isolated data stream for pub/sub messages.
46 * <li>Messages are published to a topic.
47 * <li>Messages are listened to via a subscription.
48 * <li>Each subscription listens to the messages published to exactly one topic.
49 *
50 * Subscriptions - Resource that listens to the messages published by exactly one topic.
51 *
52 * [More info here](https://firebase.google.com/docs/functions/pubsub-events)
53 */
54/**
55 * Interface representing a Google Cloud Pub/Sub message.
56 *
57 * @param data - Payload of a Pub/Sub message.
58 * @typeParam T - Type representing `Message.data`'s JSON format
59 */
60class Message {
61 /**
62 * @hidden
63 * @alpha
64 */
65 constructor(data) {
66 this.messageId = data.messageId;
67 this.data = data.data;
68 this.attributes = data.attributes || {};
69 this.orderingKey = data.orderingKey || "";
70 this.publishTime = data.publishTime || new Date().toISOString();
71 this._json = data.json;
72 }
73 /**
74 * The JSON data payload of this message object, if any.
75 */
76 get json() {
77 if (typeof this._json === "undefined") {
78 try {
79 this._json = JSON.parse(Buffer.from(this.data, "base64").toString("utf8"));
80 }
81 catch (err) {
82 throw new Error(`Unable to parse Pub/Sub message data as JSON: ${err.message}`);
83 }
84 }
85 return this._json;
86 }
87 /**
88 * Returns a JSON-serializable representation of this object.
89 *
90 * @returns A JSON-serializable representation of this object.
91 */
92 toJSON() {
93 const json = {
94 messageId: this.messageId,
95 data: this.data,
96 publishTime: this.publishTime,
97 };
98 if (Object.keys(this.attributes).length) {
99 json.attributes = this.attributes;
100 }
101 if (this.orderingKey) {
102 json.orderingKey = this.orderingKey;
103 }
104 return json;
105 }
106}
107exports.Message = Message;
108/**
109 * Handle a message being published to a Pub/Sub topic.
110 * @param topicOrOptions - A string representing the PubSub topic or an option (which contains the topic)
111 * @param handler - runs every time a Cloud Pub/Sub message is published
112 * @typeParam T - Type representing `Message.data`'s JSON format
113 */
114function onMessagePublished(topicOrOptions, handler) {
115 var _a;
116 let topic;
117 let opts;
118 if (typeof topicOrOptions === "string") {
119 topic = topicOrOptions;
120 opts = {};
121 }
122 else {
123 topic = topicOrOptions.topic;
124 opts = { ...topicOrOptions };
125 delete opts.topic;
126 }
127 const func = (raw) => {
128 const messagePublishedData = raw.data;
129 messagePublishedData.message = new Message(messagePublishedData.message);
130 return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(raw);
131 };
132 func.run = handler;
133 Object.defineProperty(func, "__trigger", {
134 get: () => {
135 const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
136 const specificOpts = options.optionsToTriggerAnnotations(opts);
137 return {
138 platform: "gcfv2",
139 ...baseOpts,
140 ...specificOpts,
141 labels: {
142 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
143 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
144 },
145 eventTrigger: {
146 eventType: "google.cloud.pubsub.topic.v1.messagePublished",
147 resource: `projects/${process.env.GCLOUD_PROJECT}/topics/${topic}`,
148 },
149 };
150 },
151 });
152 const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
153 const specificOpts = options.optionsToEndpoint(opts);
154 const endpoint = {
155 ...(0, manifest_1.initV2Endpoint)(options.getGlobalOptions(), opts),
156 platform: "gcfv2",
157 ...baseOpts,
158 ...specificOpts,
159 labels: {
160 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
161 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
162 },
163 eventTrigger: {
164 eventType: "google.cloud.pubsub.topic.v1.messagePublished",
165 eventFilters: { topic },
166 retry: (_a = opts.retry) !== null && _a !== void 0 ? _a : false,
167 },
168 };
169 (0, encoding_1.copyIfPresent)(endpoint.eventTrigger, opts, "retry", "retry");
170 func.__endpoint = endpoint;
171 return func;
172}
173exports.onMessagePublished = onMessagePublished;