1 | ;
|
2 | // The MIT License (MIT)
|
3 | //
|
4 | // Copyright (c) 2017 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.
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.Message = exports.ScheduleBuilder = exports._scheduleWithOptions = exports.schedule = exports.TopicBuilder = exports._topicWithOptions = exports.topic = exports.service = exports.provider = void 0;
|
25 | const cloud_functions_1 = require("../cloud-functions");
|
26 | /** @internal */
|
27 | exports.provider = "google.pubsub";
|
28 | /** @internal */
|
29 | exports.service = "pubsub.googleapis.com";
|
30 | /**
|
31 | * Registers a Cloud Function triggered when a Google Cloud Pub/Sub message
|
32 | * is sent to a specified topic.
|
33 | *
|
34 | * @param topic - The Pub/Sub topic to watch for message events.
|
35 | * @returns Pub/Sub topic builder interface.
|
36 | */
|
37 | function topic(topic) {
|
38 | return _topicWithOptions(topic, {});
|
39 | }
|
40 | exports.topic = topic;
|
41 | /** @internal */
|
42 | function _topicWithOptions(topic, options) {
|
43 | if (topic.indexOf("/") !== -1) {
|
44 | throw new Error("Topic name may not have a /");
|
45 | }
|
46 | return new TopicBuilder(() => {
|
47 | if (!process.env.GCLOUD_PROJECT) {
|
48 | throw new Error("process.env.GCLOUD_PROJECT is not set.");
|
49 | }
|
50 | return `projects/${process.env.GCLOUD_PROJECT}/topics/${topic}`;
|
51 | }, options);
|
52 | }
|
53 | exports._topicWithOptions = _topicWithOptions;
|
54 | /**
|
55 | * The Google Cloud Pub/Sub topic builder.
|
56 | *
|
57 | * Access via `functions.pubsub.topic()`.
|
58 | */
|
59 | class TopicBuilder {
|
60 | /** @hidden */
|
61 | constructor(triggerResource, options) {
|
62 | this.triggerResource = triggerResource;
|
63 | this.options = options;
|
64 | }
|
65 | /**
|
66 | * Event handler that fires every time a Cloud Pub/Sub message is
|
67 | * published.
|
68 | *
|
69 | * @param handler - Event handler that runs every time a Cloud Pub/Sub message
|
70 | * is published.
|
71 | * @returns A function that you can export and deploy.
|
72 | */
|
73 | onPublish(handler) {
|
74 | return (0, cloud_functions_1.makeCloudFunction)({
|
75 | handler,
|
76 | provider: exports.provider,
|
77 | service: exports.service,
|
78 | triggerResource: this.triggerResource,
|
79 | eventType: "topic.publish",
|
80 | dataConstructor: (raw) => new Message(raw.data),
|
81 | options: this.options,
|
82 | });
|
83 | }
|
84 | }
|
85 | exports.TopicBuilder = TopicBuilder;
|
86 | /**
|
87 | * Registers a Cloud Function to run at specified times.
|
88 | *
|
89 | * @param schedule - The schedule, in Unix Crontab or AppEngine syntax.
|
90 | * @returns ScheduleBuilder interface.
|
91 | */
|
92 | function schedule(schedule) {
|
93 | return _scheduleWithOptions(schedule, {});
|
94 | }
|
95 | exports.schedule = schedule;
|
96 | /** @internal */
|
97 | function _scheduleWithOptions(schedule, options) {
|
98 | const triggerResource = () => {
|
99 | if (!process.env.GCLOUD_PROJECT) {
|
100 | throw new Error("process.env.GCLOUD_PROJECT is not set.");
|
101 | }
|
102 | // The CLI will append the correct topic name based on region and function name
|
103 | return `projects/${process.env.GCLOUD_PROJECT}/topics`;
|
104 | };
|
105 | return new ScheduleBuilder(triggerResource, {
|
106 | ...options,
|
107 | schedule: { schedule },
|
108 | });
|
109 | }
|
110 | exports._scheduleWithOptions = _scheduleWithOptions;
|
111 | /**
|
112 | * The builder for scheduled functions, which are powered by
|
113 | * Google Pub/Sub and Cloud Scheduler. Describes the Cloud Scheduler
|
114 | * job that is deployed to trigger a scheduled function at the provided
|
115 | * frequency. For more information, see
|
116 | * [Schedule functions](/docs/functions/schedule-functions).
|
117 | *
|
118 | * Access via `functions.pubsub.schedule()`.
|
119 | */
|
120 | class ScheduleBuilder {
|
121 | /** @hidden */
|
122 | constructor(triggerResource, options) {
|
123 | this.triggerResource = triggerResource;
|
124 | this.options = options;
|
125 | }
|
126 | retryConfig(config) {
|
127 | this.options.schedule.retryConfig = config;
|
128 | return this;
|
129 | }
|
130 | timeZone(timeZone) {
|
131 | this.options.schedule.timeZone = timeZone;
|
132 | return this;
|
133 | }
|
134 | /**
|
135 | * Event handler for scheduled functions. Triggered whenever the associated
|
136 | * scheduler job sends a Pub/Sub message.
|
137 | *
|
138 | * @param handler - Handler that fires whenever the associated
|
139 | * scheduler job sends a Pub/Sub message.
|
140 | * @returns A function that you can export and deploy.
|
141 | */
|
142 | onRun(handler) {
|
143 | const cloudFunction = (0, cloud_functions_1.makeCloudFunction)({
|
144 | contextOnlyHandler: handler,
|
145 | provider: exports.provider,
|
146 | service: exports.service,
|
147 | triggerResource: this.triggerResource,
|
148 | eventType: "topic.publish",
|
149 | options: this.options,
|
150 | labels: { "deployment-scheduled": "true" },
|
151 | });
|
152 | return cloudFunction;
|
153 | }
|
154 | }
|
155 | exports.ScheduleBuilder = ScheduleBuilder;
|
156 | /**
|
157 | * Interface representing a Google Cloud Pub/Sub message.
|
158 | *
|
159 | * @param data - Payload of a Pub/Sub message.
|
160 | */
|
161 | class Message {
|
162 | constructor(data) {
|
163 | [this.data, this.attributes, this._json] = [data.data, data.attributes || {}, data.json];
|
164 | }
|
165 | /**
|
166 | * The JSON data payload of this message object, if any.
|
167 | */
|
168 | get json() {
|
169 | if (typeof this._json === "undefined") {
|
170 | this._json = JSON.parse(Buffer.from(this.data, "base64").toString("utf8"));
|
171 | }
|
172 | return this._json;
|
173 | }
|
174 | /**
|
175 | * Returns a JSON-serializable representation of this object.
|
176 | *
|
177 | * @returns A JSON-serializable representation of this object.
|
178 | */
|
179 | toJSON() {
|
180 | return {
|
181 | data: this.data,
|
182 | attributes: this.attributes,
|
183 | };
|
184 | }
|
185 | }
|
186 | exports.Message = Message;
|