UNPKG

7.67 kBTypeScriptView Raw
1/*!
2 * Copyright 2018 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 { PreciseDate } from '@google-cloud/precise-date';
18import { EventEmitter } from 'events';
19import { google } from '../protos/protos';
20import { FlowControlOptions } from './lease-manager';
21import { BatchOptions } from './message-queues';
22import { MessageStreamOptions } from './message-stream';
23import { Subscription } from './subscription';
24import { SubscriberClient } from './v1';
25export declare type PullResponse = google.pubsub.v1.IPullResponse;
26/**
27 * Date object with nanosecond precision. Supports all standard Date arguments
28 * in addition to several custom types.
29 *
30 * @external PreciseDate
31 * @see {@link https://github.com/googleapis/nodejs-precise-date|PreciseDate}
32 */
33/**
34 * Message objects provide a simple interface for users to get message data and
35 * acknowledge the message.
36 *
37 * @example
38 * ```
39 * subscription.on('message', message => {
40 * // {
41 * // ackId: 'RUFeQBJMJAxESVMrQwsqWBFOBCEhPjA',
42 * // attributes: {key: 'value'},
43 * // data: Buffer.from('Hello, world!'),
44 * // id: '1551297743043',
45 * // orderingKey: 'ordering-key',
46 * // publishTime: new PreciseDate('2019-02-27T20:02:19.029534186Z'),
47 * // received: 1551297743043,
48 * // length: 13
49 * // }
50 * });
51 * ```
52 */
53export declare class Message {
54 ackId: string;
55 attributes: {
56 [key: string]: string;
57 };
58 data: Buffer;
59 deliveryAttempt: number;
60 id: string;
61 orderingKey?: string;
62 publishTime: PreciseDate;
63 received: number;
64 private _handled;
65 private _length;
66 private _subscriber;
67 /**
68 * @hideconstructor
69 *
70 * @param {Subscriber} sub The parent subscriber.
71 * @param {object} message The raw message response.
72 */
73 constructor(sub: Subscriber, { ackId, message, deliveryAttempt }: google.pubsub.v1.IReceivedMessage);
74 /**
75 * The length of the message data.
76 *
77 * @type {number}
78 */
79 get length(): number;
80 /**
81 * Acknowledges the message.
82 *
83 * @example
84 * ```
85 * subscription.on('message', message => {
86 * message.ack();
87 * });
88 * ```
89 */
90 ack(): void;
91 /**
92 * Modifies the ack deadline.
93 *
94 * @param {number} deadline The number of seconds to extend the deadline.
95 * @private
96 */
97 modAck(deadline: number): void;
98 /**
99 * Removes the message from our inventory and schedules it to be redelivered.
100 *
101 * @example
102 * ```
103 * subscription.on('message', message => {
104 * message.nack();
105 * });
106 * ```
107 */
108 nack(): void;
109}
110export interface SubscriberOptions {
111 ackDeadline?: number;
112 batching?: BatchOptions;
113 flowControl?: FlowControlOptions;
114 useLegacyFlowControl?: boolean;
115 streamingOptions?: MessageStreamOptions;
116 enableOpenTelemetryTracing?: boolean;
117}
118/**
119 * @typedef {object} SubscriberOptions
120 * @property {number} [ackDeadline=10] Acknowledge deadline in seconds. If left
121 * unset the initial value will be 10 seconds, but it will evolve into the
122 * 99th percentile time it takes to acknowledge a message.
123 * @property {BatchOptions} [batching] Request batching options.
124 * @property {FlowControlOptions} [flowControl] Flow control options.
125 * @property {boolean} [useLegacyFlowControl] Disables enforcing flow control
126 * settings at the Cloud PubSub server and uses the less accurate method
127 * of only enforcing flow control at the client side.
128 * @property {MessageStreamOptions} [streamingOptions] Streaming options.
129 */
130/**
131 * Subscriber class is used to manage all message related functionality.
132 *
133 * @private
134 * @class
135 *
136 * @param {Subscription} subscription The corresponding subscription.
137 * @param {SubscriberOptions} options The subscriber options.
138 */
139export declare class Subscriber extends EventEmitter {
140 ackDeadline: number;
141 maxMessages: number;
142 maxBytes: number;
143 useLegacyFlowControl: boolean;
144 isOpen: boolean;
145 private _acks;
146 private _histogram;
147 private _inventory;
148 private _isUserSetDeadline;
149 private _useOpentelemetry;
150 private _latencies;
151 private _modAcks;
152 private _name;
153 private _options;
154 private _stream;
155 private _subscription;
156 constructor(subscription: Subscription, options?: {});
157 /**
158 * The 99th percentile of request latencies.
159 *
160 * @type {number}
161 * @private
162 */
163 get modAckLatency(): number;
164 /**
165 * The full name of the Subscription.
166 *
167 * @type {string}
168 * @private
169 */
170 get name(): string;
171 /**
172 * Acknowledges the supplied message.
173 *
174 * @param {Message} message The message to acknowledge.
175 * @returns {Promise}
176 * @private
177 */
178 ack(message: Message): Promise<void>;
179 /**
180 * Closes the subscriber. The returned promise will resolve once any pending
181 * acks/modAcks are finished.
182 *
183 * @returns {Promise}
184 * @private
185 */
186 close(): Promise<void>;
187 /**
188 * Gets the subscriber client instance.
189 *
190 * @returns {Promise<object>}
191 * @private
192 */
193 getClient(): Promise<SubscriberClient>;
194 /**
195 * Modifies the acknowledge deadline for the provided message.
196 *
197 * @param {Message} message The message to modify.
198 * @param {number} deadline The deadline.
199 * @returns {Promise}
200 * @private
201 */
202 modAck(message: Message, deadline: number): Promise<void>;
203 /**
204 * Modfies the acknowledge deadline for the provided message and then removes
205 * it from our inventory.
206 *
207 * @param {Message} message The message.
208 * @return {Promise}
209 * @private
210 */
211 nack(message: Message): Promise<void>;
212 /**
213 * Starts pulling messages.
214 * @private
215 */
216 open(): void;
217 /**
218 * Sets subscriber options.
219 *
220 * @param {SubscriberOptions} options The options.
221 * @private
222 */
223 setOptions(options: SubscriberOptions): void;
224 /**
225 * Constructs an OpenTelemetry span from the incoming message.
226 *
227 * @param {Message} message One of the received messages
228 * @private
229 */
230 private _constructSpan;
231 /**
232 * Callback to be invoked when a new message is available.
233 *
234 * New messages will be added to the subscribers inventory, which in turn will
235 * automatically extend the messages ack deadline until either:
236 * a. the user acks/nacks it
237 * b. the maxExtension option is hit
238 *
239 * If the message puts us at/over capacity, then we'll pause our message
240 * stream until we've freed up some inventory space.
241 *
242 * New messages must immediately issue a ModifyAckDeadline request
243 * (aka receipt) to confirm with the backend that we did infact receive the
244 * message and its ok to start ticking down on the deadline.
245 *
246 * @private
247 */
248 private _onData;
249 /**
250 * Returns a promise that will resolve once all pending requests have settled.
251 *
252 * @private
253 *
254 * @returns {Promise}
255 */
256 private _waitForFlush;
257}