forked from googleapis/nodejs-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic.js
More file actions
597 lines (589 loc) · 17.9 KB
/
topic.js
File metadata and controls
597 lines (589 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*!
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const util = require('./util');
const {promisifyAll} = require('@google-cloud/promisify');
const {paginator} = require('@google-cloud/paginator');
const extend = require('extend');
const is = require('is');
const IAM = require('./iam');
const Publisher = require('./publisher');
/**
* A Topic object allows you to interact with a Cloud Pub/Sub topic.
*
* @class
* @param {PubSub} pubsub PubSub object.
* @param {string} name Name of the topic.
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*/
class Topic {
constructor(pubsub, name) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
}
/**
* The fully qualified name of this topic.
* @name Topic#name
* @type {string}
*/
this.name = Topic.formatName_(pubsub.projectId, name);
/**
* The parent {@link PubSub} instance of this topic instance.
* @name Topic#pubsub
* @type {PubSub}
*/
/**
* The parent {@link PubSub} instance of this topic instance.
* @name Topic#parent
* @type {PubSub}
*/
this.parent = this.pubsub = pubsub;
this.request = pubsub.request.bind(pubsub);
/**
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
* allows you to set permissions on individual resources and offers a wider
* range of roles: editor, owner, publisher, subscriber, and viewer. This
* gives you greater flexibility and allows you to set more fine-grained
* access control.
*
* *The IAM access control features described in this document are Beta,
* including the API methods to get and set IAM policies, and to test IAM
* permissions. Cloud Pub/Sub's use of IAM features is not covered by
* any SLA or deprecation policy, and may be subject to backward-incompatible
* changes.*
*
* @name Topic#iam
* @mixes IAM
*
* @see [Access Control Overview]{@link https://cloud.google.com/pubsub/access_control}
* @see [What is Cloud IAM?]{@link https://cloud.google.com/iam/}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* //-
* // Get the IAM policy for your topic.
* //-
* topic.iam.getPolicy((err, policy) => {
* console.log(policy);
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.iam.getPolicy().then((data) => {
* const policy = data[0];
* const apiResponse = data[1];
* });
*/
this.iam = new IAM(pubsub, this.name);
}
/**
* Create a topic.
*
* @param {object} [gaxOpts] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {CreateTopicCallback} [callback] Callback function.
* @returns {Promise<CreateTopicResponse>}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.create((err, topic, apiResponse) => {
* if (!err) {
* // The topic was created successfully.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.create().then((data) => {
* const topic = data[0];
* const apiResponse = data[1];
* });
*/
create(gaxOpts, callback) {
this.pubsub.createTopic(this.name, gaxOpts, callback);
}
/**
* Create a subscription to this topic.
*
* @see [Subscriptions: create API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create}
*
* @throws {Error} If subscription name is omitted.
*
* @param {string} name The name of the subscription.
* @param {CreateSubscriptionRequest} [options] See a
* [Subscription resource](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions).
* @param {CreateSubscriptionCallback} [callback] Callback function.
* @returns {Promise<CreateSubscriptionResponse>}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const callback = function(err, subscription, apiResponse) {};
*
* // Without specifying any options.
* topic.createSubscription('newMessages', callback);
*
* // With options.
* topic.createSubscription('newMessages', {
* ackDeadlineSeconds: 90
* }, callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.createSubscription('newMessages').then((data) => {
* const subscription = data[0];
* const apiResponse = data[1];
* });
*/
createSubscription(name, options, callback) {
this.pubsub.createSubscription(this, name, options, callback);
}
/**
* Delete the topic. This will not delete subscriptions to this topic.
*
* @see [Topics: delete API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/delete}
*
* @param {object} [gaxOpts] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {object} callback.apiResponse Raw API response.
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.delete((err, apiResponse) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.delete().then((data) => {
* const apiResponse = data[0];
* });
*/
delete(gaxOpts, callback) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = {};
}
callback = callback || util.noop;
const reqOpts = {
topic: this.name,
};
this.request(
{
client: 'PublisherClient',
method: 'deleteTopic',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
callback
);
}
/**
* @typedef {array} TopicExistsResponse
* @property {boolean} 0 Whether the topic exists
*/
/**
* @callback TopicExistsCallback
* @param {?Error} err Request error, if any.
* @param {boolean} exists Whether the topic exists.
*/
/**
* Check if a topic exists.
*
* @param {TopicExistsCallback} [callback] Callback function.
* @returns {Promise<TopicExistsResponse>}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.exists((err, exists) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.exists().then((data) => {
* const exists = data[0];
* });
*/
exists(callback) {
this.getMetadata(function(err) {
if (!err) {
callback(null, true);
return;
}
if (err.code === 5) {
callback(null, false);
return;
}
callback(err);
});
}
/**
* @typedef {array} GetTopicResponse
* @property {Topic} 0 The {@link Topic}.
* @property {object} 1 The full API response.
*/
/**
* @callback GetTopicCallback
* @param {?Error} err Request error, if any.
* @param {Topic} topic The {@link Topic}.
* @param {object} apiResponse The full API response.
*/
/**
* Get a topic if it exists.
*
* @param {object} [gaxOpts] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {boolean} [gaxOpts.autoCreate=false] Automatically create the topic
* does not already exist.
* @param {GetTopicCallback} [callback] Callback function.
* @returns {Promise<GetTopicResponse>}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.get((err, topic, apiResponse) => {
* // The `topic` data has been populated.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.get().then((data) => {
* const topic = data[0];
* const apiResponse = data[1];
* });
*/
get(gaxOpts, callback) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = {};
}
const autoCreate = !!gaxOpts.autoCreate;
delete gaxOpts.autoCreate;
this.getMetadata(gaxOpts, (err, apiResponse) => {
if (!err) {
callback(null, this, apiResponse);
return;
}
if (err.code !== 5 || !autoCreate) {
callback(err, null, apiResponse);
return;
}
this.create(gaxOpts, callback);
});
}
/**
* @typedef {array} GetTopicMetadataResponse
* @property {object} 0 The full API response.
*/
/**
* @callback GetTopicMetadataCallback
* @param {?Error} err Request error, if any.
* @param {object} apiResponse The full API response.
*/
/**
* Get the official representation of this topic from the API.
*
* @see [Topics: get API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/get}
*
* @param {object} [gaxOpts] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {GetTopicMetadataCallback} [callback] Callback function.
* @returns {Promise<GetTopicMetadataResponse>}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.getMetadata((err, apiResponse) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.getMetadata().then((data) => {
* const apiResponse = data[0];
* });
*/
getMetadata(gaxOpts, callback) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = {};
}
const reqOpts = {
topic: this.name,
};
this.request(
{
client: 'PublisherClient',
method: 'getTopic',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
(err, apiResponse) => {
if (!err) {
this.metadata = apiResponse;
}
callback(err, apiResponse);
}
);
}
/**
* Get a list of the subscriptions registered to this topic. You may optionally
* provide a query object as the first argument to customize the response.
*
* Your provided callback will be invoked with an error object if an API error
* occurred or an array of {module:pubsub/subscription} objects.
*
* @see [Subscriptions: list API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics.subscriptions/list}
*
* @param {GetSubscriptionsRequest} [query] Query object for listing subscriptions.
* @param {GetSubscriptionsCallback} [callback] Callback function.
* @returns {Promise<GetSubscriptionsResponse>}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.getSubscriptions((err, subscriptions) => {
* // subscriptions is an array of `Subscription` objects.
* });
*
* // Customize the query.
* topic.getSubscriptions({
* pageSize: 3
* }, callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.getSubscriptions().then((data) => {
* const subscriptions = data[0];
* });
*/
getSubscriptions(options, callback) {
const self = this;
if (is.fn(options)) {
callback = options;
options = {};
}
const reqOpts = extend(
{
topic: this.name,
},
options
);
delete reqOpts.gaxOpts;
delete reqOpts.autoPaginate;
const gaxOpts = extend(
{
autoPaginate: options.autoPaginate,
},
options.gaxOpts
);
this.request(
{
client: 'PublisherClient',
method: 'listTopicSubscriptions',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
function() {
const subscriptions = arguments[1];
if (subscriptions) {
arguments[1] = subscriptions.map(function(sub) {
// ListTopicSubscriptions only returns sub names
return self.subscription(sub);
});
}
callback.apply(null, arguments);
}
);
}
/**
* Creates a Publisher object that allows you to publish messages to this topic.
*
* @param {object} [options] Configuration object.
* @param {object} [options.batching] Batching settings.
* @param {number} [options.batching.maxBytes] The maximum number of bytes to
* buffer before sending a payload.
* @param {number} [options.batching.maxMessages] The maximum number of messages
* to buffer before sending a payload.
* @param {number} [options.batching.maxMilliseconds] The maximum duration to
* wait before sending a payload.
*
* @return {Publisher}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const publisher = topic.publisher();
*
* publisher.publish(Buffer.from('Hello, world!'), (err, messageId) => {
* if (err) {
* // Error handling omitted.
* }
* });
*/
publisher(options) {
return new Publisher(this, options);
}
/**
* Create a Subscription object. This command by itself will not run any API
* requests. You will receive a {module:pubsub/subscription} object,
* which will allow you to interact with a subscription.
*
* @throws {Error} If subscription name is omitted.
*
* @param {string} name Name of the subscription.
* @param {object} [options] Configuration object.
* @param {object} [options.flowControl] Flow control configurations for
* receiving messages. Note that these options do not persist across
* subscription instances.
* @param {number} [options.flowControl.maxBytes] The maximum number of bytes
* in un-acked messages to allow before the subscription pauses incoming
* messages. Defaults to 20% of free memory.
* @param {number} [options.flowControl.maxMessages=Infinity] The maximum number
* of un-acked messages to allow before the subscription pauses incoming
* messages.
* @param {number} [options.maxConnections=5] Use this to limit the number of
* connections to be used when sending and receiving messages.
* @return {Subscription}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const subscription = topic.subscription('my-subscription');
*
* // Register a listener for `message` events.
* subscription.on('message', (message) => {
* // Called every time a message is received.
* // message.id = ID of the message.
* // message.ackId = ID used to acknowledge the message receival.
* // message.data = Contents of the message.
* // message.attributes = Attributes of the message.
* // message.publishTime = Timestamp when Pub/Sub received the message.
* });
*/
subscription(name, options) {
options = options || {};
options.topic = this;
return this.pubsub.subscription(name, options);
}
/**
* Format the name of a topic. A Topic's full name is in the format of
* 'projects/{projectId}/topics/{topicName}'.
*
* @private
*
* @return {string}
*/
static formatName_(projectId, name) {
// Simple check if the name is already formatted.
if (name.indexOf('/') > -1) {
return name;
}
return 'projects/' + projectId + '/topics/' + name;
}
}
/**
* Get a list of the {module:pubsub/subscription} objects registered to this
* topic as a readable object stream.
*
* @method PubSub#getSubscriptionsStream
* @param {GetSubscriptionsRequest} [options] Configuration object. See
* {@link PubSub#getSubscriptions} for a complete list of options.
* @returns {ReadableStream} A readable stream of {@link Subscription} instances.
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
*
* topic.getSubscriptionsStream()
* .on('error', console.error)
* .on('data', (subscription) => {
* // subscription is a Subscription object.
* })
* .on('end', () => {
* // All subscriptions retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* topic.getSubscriptionsStream()
* .on('data', function(subscription) {
* this.end();
* });
*/
Topic.prototype.getSubscriptionsStream = paginator.streamify(
'getSubscriptions'
);
/*! Developer Documentation
*
* These methods can be agto-paginated.
*/
paginator.extend(Topic, ['getSubscriptions']);
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
promisifyAll(Topic, {
exclude: ['publisher', 'subscription'],
});
module.exports = Topic;