UNPKG

13.1 kBTypeScriptView Raw
1/*!
2 * Copyright 2014 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/// <reference types="node" />
17import { EventEmitter } from 'events';
18import { CallOptions } from 'google-gax';
19import { google } from '../protos/protos';
20import { IAM } from './iam';
21import { FlowControlOptions } from './lease-manager';
22import { DetachedCallback, DetachedResponse, EmptyCallback, EmptyResponse, ExistsCallback, ExistsResponse, Omit, PubSub, RequestCallback, ResourceCallback } from './pubsub';
23import { CreateSnapshotCallback, CreateSnapshotResponse, SeekCallback, SeekResponse, Snapshot } from './snapshot';
24import { SubscriberOptions } from './subscriber';
25import { Topic } from './topic';
26export declare type PushConfig = google.pubsub.v1.IPushConfig;
27export declare type OidcToken = google.pubsub.v1.PushConfig.IOidcToken;
28export declare type SubscriptionMetadata = {
29 messageRetentionDuration?: google.protobuf.IDuration | number;
30 pushEndpoint?: string;
31 oidcToken?: OidcToken;
32} & Omit<google.pubsub.v1.ISubscription, 'messageRetentionDuration'>;
33export declare type SubscriptionOptions = SubscriberOptions & {
34 topic?: Topic;
35};
36export declare type SubscriptionCloseCallback = (err?: Error) => void;
37declare type SubscriptionCallback = ResourceCallback<Subscription, google.pubsub.v1.ISubscription>;
38declare type SubscriptionResponse = [Subscription, google.pubsub.v1.ISubscription];
39export declare type CreateSubscriptionOptions = SubscriptionMetadata & {
40 gaxOpts?: CallOptions;
41 flowControl?: FlowControlOptions;
42};
43export declare type CreateSubscriptionCallback = SubscriptionCallback;
44export declare type CreateSubscriptionResponse = SubscriptionResponse;
45export declare type GetSubscriptionOptions = CallOptions & {
46 autoCreate?: boolean;
47};
48export declare type GetSubscriptionCallback = SubscriptionCallback;
49export declare type GetSubscriptionResponse = SubscriptionResponse;
50declare type MetadataCallback = RequestCallback<google.pubsub.v1.ISubscription>;
51declare type MetadataResponse = [google.pubsub.v1.ISubscription];
52export declare type GetSubscriptionMetadataCallback = MetadataCallback;
53export declare type GetSubscriptionMetadataResponse = MetadataResponse;
54export declare type SetSubscriptionMetadataCallback = MetadataCallback;
55export declare type SetSubscriptionMetadataResponse = MetadataResponse;
56export declare type DetachSubscriptionCallback = EmptyCallback;
57export declare type DetachSubscriptionResponse = EmptyResponse;
58/**
59 * @typedef {object} ExpirationPolicy
60 * A policy that specifies the conditions for this subscription's expiration. A
61 * subscription is considered active as long as any connected subscriber is
62 * successfully consuming messages from the subscription or is issuing
63 * operations on the subscription. If expirationPolicy is not set, a default
64 * policy with ttl of 31 days will be used. The minimum allowed value for
65 * expirationPolicy.ttl is 1 day.
66 * @property {google.protobuf.Duration} ttl Specifies the "time-to-live"
67 * duration for an associated resource. The resource expires if it is not
68 * active for a period of `ttl`. The definition of "activity" depends on the
69 * type of the associated resource. The minimum and maximum allowed values
70 * for `ttl` depend on the type of the associated resource, as well. If
71 * `ttl` is not set, the associated resource never expires.
72 */
73/**
74 * A Subscription object will give you access to your Cloud Pub/Sub
75 * subscription.
76 *
77 * Subscriptions are sometimes retrieved when using various methods:
78 *
79 * - {@link PubSub#getSubscriptions}
80 * - {@link Topic#getSubscriptions}
81 *
82 * Subscription objects may be created directly with:
83 *
84 * - {@link PubSub#createSubscription}
85 * - {@link Topic#createSubscription}
86 *
87 * All Subscription objects are instances of an
88 * [EventEmitter](http://nodejs.org/api/events.html). The subscription will pull
89 * for messages automatically as long as there is at least one listener assigned
90 * for the `message` event. Available events:
91 *
92 * Upon receipt of a message:
93 * on(event: 'message', listener: (message: {@link Message}) => void): this;
94 *
95 * Upon receipt of an error:
96 * on(event: 'error', listener: (error: Error) => void): this;
97 *
98 * Upon the closing of the subscriber:
99 * on(event: 'close', listener: Function): this;
100 *
101 * By default Subscription objects allow you to process 100 messages at the same
102 * time. You can fine tune this value by adjusting the
103 * `options.flowControl.maxMessages` option.
104 *
105 * If your subscription is seeing more re-deliveries than preferable, you might
106 * try increasing your `options.ackDeadline` value or decreasing the
107 * `options.streamingOptions.maxStreams` value.
108 *
109 * Subscription objects handle ack management, by automatically extending the
110 * ack deadline while the message is being processed, to then issue the ack or
111 * nack of such message when the processing is done. **Note:** message
112 * redelivery is still possible.
113 *
114 * By default each {@link PubSub} instance can handle 100 open streams, with
115 * default options this translates to less than 20 Subscriptions per PubSub
116 * instance. If you wish to create more Subscriptions than that, you can either
117 * create multiple PubSub instances or lower the
118 * `options.streamingOptions.maxStreams` value on each Subscription object.
119 *
120 * @class
121 *
122 * @param {PubSub} pubsub PubSub object.
123 * @param {string} name The name of the subscription.
124 * @param {SubscriberOptions} [options] Options for handling messages.
125 *
126 * @example <caption>From {@link PubSub#getSubscriptions}</caption>
127 * const {PubSub} = require('@google-cloud/pubsub');
128 * const pubsub = new PubSub();
129 *
130 * pubsub.getSubscriptions((err, subscriptions) => {
131 * // `subscriptions` is an array of Subscription objects.
132 * });
133 *
134 * @example <caption>From {@link Topic#getSubscriptions}</caption>
135 * const topic = pubsub.topic('my-topic');
136 * topic.getSubscriptions((err, subscriptions) => {
137 * // `subscriptions` is an array of Subscription objects.
138 * });
139 *
140 * @example <caption>{@link Topic#createSubscription}</caption>
141 * const topic = pubsub.topic('my-topic');
142 * topic.createSubscription('new-subscription', (err, subscription) => {
143 * // `subscription` is a Subscription object.
144 * });
145 *
146 * @example <caption>{@link Topic#subscription}</caption>
147 * const topic = pubsub.topic('my-topic');
148 * const subscription = topic.subscription('my-subscription');
149 * // `subscription` is a Subscription object.
150 *
151 * @example <caption>Once you have obtained a subscription object, you may begin
152 * to register listeners. This will automatically trigger pulling for messages.
153 * </caption>
154 * // Register an error handler.
155 * subscription.on('error', (err) => {});
156 *
157 * // Register a close handler in case the subscriber closes unexpectedly
158 * subscription.on('close', () => {});
159 *
160 * // Register a listener for `message` events.
161 * function onMessage(message) {
162 * // Called every time a message is received.
163 *
164 * // message.id = ID of the message.
165 * // message.ackId = ID used to acknowledge the message receival.
166 * // message.data = Contents of the message.
167 * // message.attributes = Attributes of the message.
168 * // message.publishTime = Date when Pub/Sub received the message.
169 *
170 * // Ack the message:
171 * // message.ack();
172 *
173 * // This doesn't ack the message, but allows more messages to be retrieved
174 * // if your limit was hit or if you don't want to ack the message.
175 * // message.nack();
176 * }
177 * subscription.on('message', onMessage);
178 *
179 * // Remove the listener from receiving `message` events.
180 * subscription.removeListener('message', onMessage);
181 *
182 * @example <caption>To apply a fine level of flow control, consider the
183 * following configuration</caption>
184 * const subscription = topic.subscription('my-sub', {
185 * flowControl: {
186 * maxMessages: 1,
187 * // this tells the client to manage and lock any excess messages
188 * allowExcessMessages: false
189 * }
190 * });
191 */
192export declare class Subscription extends EventEmitter {
193 pubsub: PubSub;
194 iam: IAM;
195 name: string;
196 topic?: Topic | string;
197 metadata?: google.pubsub.v1.ISubscription;
198 request: typeof PubSub.prototype.request;
199 private _subscriber;
200 constructor(pubsub: PubSub, name: string, options?: SubscriptionOptions);
201 /**
202 * Indicates if the Subscription is open and receiving messages.
203 *
204 * @type {boolean}
205 */
206 get isOpen(): boolean;
207 /**
208 * @type {string}
209 */
210 get projectId(): string;
211 close(): Promise<void>;
212 close(callback: SubscriptionCloseCallback): void;
213 create(options?: CreateSubscriptionOptions): Promise<CreateSubscriptionResponse>;
214 create(callback: CreateSubscriptionCallback): void;
215 create(options: CreateSubscriptionOptions, callback: CreateSubscriptionCallback): void;
216 createSnapshot(name: string, gaxOpts?: CallOptions): Promise<CreateSnapshotResponse>;
217 createSnapshot(name: string, callback: CreateSnapshotCallback): void;
218 createSnapshot(name: string, gaxOpts: CallOptions, callback: CreateSnapshotCallback): void;
219 delete(gaxOpts?: CallOptions): Promise<EmptyResponse>;
220 delete(callback: EmptyCallback): void;
221 delete(gaxOpts: CallOptions, callback: EmptyCallback): void;
222 detached(): Promise<DetachedResponse>;
223 detached(callback: DetachedCallback): void;
224 exists(): Promise<ExistsResponse>;
225 exists(callback: ExistsCallback): void;
226 get(gaxOpts?: GetSubscriptionOptions): Promise<GetSubscriptionResponse>;
227 get(callback: GetSubscriptionCallback): void;
228 get(gaxOpts: GetSubscriptionOptions, callback: GetSubscriptionCallback): void;
229 getMetadata(gaxOpts?: CallOptions): Promise<GetSubscriptionMetadataResponse>;
230 getMetadata(callback: GetSubscriptionMetadataCallback): void;
231 getMetadata(gaxOpts: CallOptions, callback: GetSubscriptionMetadataCallback): void;
232 modifyPushConfig(config: PushConfig, gaxOpts?: CallOptions): Promise<EmptyResponse>;
233 modifyPushConfig(config: PushConfig, callback: EmptyCallback): void;
234 modifyPushConfig(config: PushConfig, gaxOpts: CallOptions, callback: EmptyCallback): void;
235 /**
236 * Opens the Subscription to receive messages. In general this method
237 * shouldn't need to be called, unless you wish to receive messages after
238 * calling {@link Subscription#close}. Alternatively one could just assign a
239 * new `message` event listener which will also re-open the Subscription.
240 *
241 * @example
242 * subscription.on('message', message => message.ack());
243 *
244 * // Close the subscription.
245 * subscription.close(err => {
246 * if (err) {
247 * // Error handling omitted.
248 * }
249 *
250 * The subscription has been closed and messages will no longer be received.
251 * });
252 *
253 * // Resume receiving messages.
254 * subscription.open();
255 */
256 open(): void;
257 seek(snapshot: string | Date, gaxOpts?: CallOptions): Promise<SeekResponse>;
258 seek(snapshot: string | Date, callback: SeekCallback): void;
259 seek(snapshot: string | Date, gaxOpts: CallOptions, callback: SeekCallback): void;
260 setMetadata(metadata: SubscriptionMetadata, gaxOpts?: CallOptions): Promise<SetSubscriptionMetadataResponse>;
261 setMetadata(metadata: SubscriptionMetadata, callback: SetSubscriptionMetadataCallback): void;
262 setMetadata(metadata: SubscriptionMetadata, gaxOpts: CallOptions, callback: SetSubscriptionMetadataCallback): void;
263 /**
264 * Sets the Subscription options.
265 *
266 * @param {SubscriberOptions} options The options.
267 */
268 setOptions(options: SubscriberOptions): void;
269 /**
270 * Create a Snapshot object. See {@link Subscription#createSnapshot} to
271 * create a snapshot.
272 *
273 * @throws {Error} If a name is not provided.
274 *
275 * @param {string} name The name of the snapshot.
276 * @returns {Snapshot}
277 *
278 * @example
279 * const snapshot = subscription.snapshot('my-snapshot');
280 */
281 snapshot(name: string): Snapshot;
282 /**
283 * Watches for incoming message event handlers and open/closes the
284 * subscriber as needed.
285 *
286 * @private
287 */
288 private _listen;
289 /*!
290 * Formats Subscription metadata.
291 *
292 * @private
293 */
294 static formatMetadata_(metadata: SubscriptionMetadata): google.pubsub.v1.ISubscription;
295 /*!
296 * Format the name of a subscription. A subscription's full name is in the
297 * format of projects/{projectId}/subscriptions/{subName}.
298 *
299 * @private
300 */
301 static formatName_(projectId: string, name: string): string;
302}
303export {};