diff --git a/src/index.ts b/src/index.ts index a10cd26b3..cf7597fb1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import {replaceProjectIdToken} from '@google-cloud/projectify'; import {paginator} from '@google-cloud/paginator'; +import {replaceProjectIdToken} from '@google-cloud/projectify'; import {promisifyAll} from '@google-cloud/promisify'; import * as extend from 'extend'; import {GoogleAuth} from 'google-auth-library'; @@ -28,7 +28,7 @@ const v1 = require('./v1'); import {Snapshot} from './snapshot'; import {Subscription} from './subscription'; import {Topic} from './topic'; -import { Readable } from 'stream'; +import {Readable} from 'stream'; const opts = {} as gax.GrpcClientOptions; const {grpc} = new gax.GrpcClient(opts); @@ -44,8 +44,10 @@ const PROJECT_ID_PLACEHOLDER = '{{projectId}}'; * @property {string} [projectId] The project ID from the Google Developer's * Console, e.g. 'grape-spaceship-123'. We will also check the environment * variable `GCLOUD_PROJECT` for your project ID. If your app is running in - * an environment which supports {@link https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application Application Default Credentials}, - * your project ID will be detected automatically. + * an environment which supports {@link + * https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application + * Application Default Credentials}, your project ID will be detected + * automatically. * @property {string} [keyFilename] Full path to the a .json, .pem, or .p12 key * downloaded from the Google Developers Console. If you provide a path to a * JSON file, the `projectId` option above is not necessary. NOTE: .pem and @@ -81,13 +83,14 @@ const PROJECT_ID_PLACEHOLDER = '{{projectId}}'; * @example Import the client library * const {PubSub} = require('@google-cloud/pubsub'); * - * @example Create a client that uses Application Default Credentials (ADC): - * const pubsub = new PubSub(); + * @example Create a client that uses Application + * Default Credentials (ADC): const pubsub = new PubSub(); * - * @example Create a client with explicit credentials: - * const pubsub = new PubSub({ - * projectId: 'your-project-id', - * keyFilename: '/path/to/keyfile.json' + * @example Create a client with explicit + * credentials: const pubsub = new PubSub({ projectId: + * 'your-project-id', keyFilename: '/path/to/keyfile.json' * }); * * @example include:samples/quickstart.js @@ -100,9 +103,13 @@ export class PubSub { api; auth: GoogleAuth; projectId: string; + // tslint:disable-next-line variable-name Promise?: PromiseConstructor; - getSubscriptionsStream = paginator.streamify('getSubscriptions') as () => Readable; - getSnapshotsStream = paginator.streamify('getSnapshots') as () => Readable; + getSubscriptionsStream = paginator.streamify('getSubscriptions') as() => + Readable; + getSnapshotsStream = paginator.streamify('getSnapshots') as() => Readable; + getTopicsStream = paginator.streamify('getTopics') as() => Readable; + constructor(options?) { options = options || {}; // Determine what scopes are needed. @@ -115,15 +122,14 @@ export class PubSub { } } this.options = Object.assign( - { - 'grpc.keepalive_time_ms': 300000, - 'grpc.max_receive_message_length': 20000001, - libName: 'gccl', - libVersion: PKG.version, - scopes: Object.keys(allScopes), - }, - options - ); + { + 'grpc.keepalive_time_ms': 300000, + 'grpc.max_receive_message_length': 20000001, + libName: 'gccl', + libVersion: PKG.version, + scopes: Object.keys(allScopes), + }, + options); /** * @name PubSub#isEmulator * @type {boolean} @@ -140,7 +146,8 @@ export class PubSub { /** * Options for creating a subscription. * - * See a [Subscription resource](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions). + * See a [Subscription + * resource](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions). * * @typedef {object} CreateSubscriptionRequest * @property {object} [flowControl] Flow control configurations for @@ -187,7 +194,8 @@ export class PubSub { * subscription to. * @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). + * [Subscription + * resource](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions). * @param {CreateSubscriptionCallback} [callback] Callback function. * @returns {Promise} * @@ -202,10 +210,10 @@ export class PubSub { * * pubsub.createSubscription(topic, name, callback); * - * @example If the callback is omitted, we'll return a Promise. - * pubsub.createSubscription(topic, name).then(function(data) { - * const subscription = data[0]; - * const apiResponse = data[1]; + * @example If the callback is omitted, we'll return a + * Promise. pubsub.createSubscription(topic, + * name).then(function(data) { const subscription = data[0]; const apiResponse + * = data[1]; * }); */ createSubscription(topic: Topic|string, name: string, options, callback) { @@ -232,21 +240,20 @@ export class PubSub { delete reqOpts.gaxOpts; delete reqOpts.flowControl; this.request( - { - client: 'SubscriberClient', - method: 'createSubscription', - reqOpts: reqOpts, - gaxOpts: options.gaxOpts, - }, - function(err, resp) { - if (err) { - callback(err, null, resp); - return; - } - subscription.metadata = resp; - callback(null, subscription, resp); - } - ); + { + client: 'SubscriberClient', + method: 'createSubscription', + reqOpts, + gaxOpts: options.gaxOpts, + }, + (err, resp) => { + if (err) { + callback(err, null, resp); + return; + } + subscription.metadata = resp; + callback(null, subscription, resp); + }); } /** * @typedef {array} CreateTopicResponse @@ -298,27 +305,26 @@ export class PubSub { gaxOpts = {}; } this.request( - { - client: 'PublisherClient', - method: 'createTopic', - reqOpts: reqOpts, - gaxOpts: gaxOpts, - }, - function(err, resp) { - if (err) { - callback(err, null, resp); - return; - } - topic.metadata = resp; - callback(null, topic, resp); - } - ); + { + client: 'PublisherClient', + method: 'createTopic', + reqOpts, + gaxOpts, + }, + (err, resp) => { + if (err) { + callback(err, null, resp); + return; + } + topic.metadata = resp; + callback(null, topic, resp); + }); } /** - * Determine the appropriate endpoint to use for API requests, first trying the - * local `apiEndpoint` parameter. If the `apiEndpoint` parameter is null we try - * Pub/Sub emulator environment variable (PUBSUB_EMULATOR_HOST), otherwise the - * default JSON API. + * Determine the appropriate endpoint to use for API requests, first trying + * the local `apiEndpoint` parameter. If the `apiEndpoint` parameter is null + * we try Pub/Sub emulator environment variable (PUBSUB_EMULATOR_HOST), + * otherwise the default JSON API. * * @private */ @@ -330,10 +336,9 @@ export class PubSub { const baseUrl = apiEndpoint || process.env.PUBSUB_EMULATOR_HOST; const leadingProtocol = new RegExp('^https*://'); const trailingSlashes = new RegExp('/*$'); - const baseUrlParts = baseUrl - .replace(leadingProtocol, '') - .replace(trailingSlashes, '') - .split(':'); + const baseUrlParts = baseUrl.replace(leadingProtocol, '') + .replace(trailingSlashes, '') + .split(':'); this.options.servicePath = baseUrlParts[0]; this.options.port = baseUrlParts[1]; this.options.sslCreds = grpc.credentials.createInsecure(); @@ -392,38 +397,36 @@ export class PubSub { options = {}; } const reqOpts = Object.assign( - { - project: 'projects/' + this.projectId, - }, - options - ); + { + project: 'projects/' + this.projectId, + }, + options); delete reqOpts.gaxOpts; delete reqOpts.autoPaginate; const gaxOpts = Object.assign( - { - autoPaginate: options.autoPaginate, - }, - options.gaxOpts - ); + { + autoPaginate: options.autoPaginate, + }, + options.gaxOpts); this.request( - { - client: 'SubscriberClient', - method: 'listSnapshots', - reqOpts: reqOpts, - gaxOpts: gaxOpts, - }, - function() { - const snapshots = arguments[1]; - if (snapshots) { - arguments[1] = snapshots.map(function(snapshot) { - const snapshotInstance = self.snapshot(snapshot.name); - snapshotInstance.metadata = snapshot; - return snapshotInstance; - }); - } - callback.apply(null, arguments); - } - ); + { + client: 'SubscriberClient', + method: 'listSnapshots', + reqOpts, + gaxOpts, + }, + // tslint:disable-next-line only-arrow-functions + function() { + const snapshots = arguments[1]; + if (snapshots) { + arguments[1] = snapshots.map(snapshot => { + const snapshotInstance = self.snapshot(snapshot.name); + snapshotInstance.metadata = snapshot; + return snapshotInstance; + }); + } + callback.apply(null, arguments); + }); } /** * Query object for listing subscriptions. @@ -451,8 +454,8 @@ export class PubSub { */ /** * Get a list of the subscriptions registered to all of your project's topics. - * You may optionally provide a query object as the first argument to customize - * the response. + * 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 {@link Subscription} objects. @@ -500,30 +503,29 @@ export class PubSub { delete reqOpts.gaxOpts; delete reqOpts.autoPaginate; const gaxOpts = Object.assign( - { - autoPaginate: options.autoPaginate, - }, - options.gaxOpts - ); + { + autoPaginate: options.autoPaginate, + }, + options.gaxOpts); this.request( - { - client: 'SubscriberClient', - method: 'listSubscriptions', - reqOpts: reqOpts, - gaxOpts: gaxOpts, - }, - function() { - const subscriptions = arguments[1]; - if (subscriptions) { - arguments[1] = subscriptions.map(function(sub) { - const subscriptionInstance = self.subscription(sub.name); - subscriptionInstance.metadata = sub; - return subscriptionInstance; - }); - } - callback.apply(null, arguments); - } - ); + { + client: 'SubscriberClient', + method: 'listSubscriptions', + reqOpts, + gaxOpts, + }, + // tslint:disable-next-line only-arrow-functions + function() { + const subscriptions = arguments[1]; + if (subscriptions) { + arguments[1] = subscriptions.map(sub => { + const subscriptionInstance = self.subscription(sub.name); + subscriptionInstance.metadata = sub; + return subscriptionInstance; + }); + } + callback.apply(null, arguments); + }); } /** * Query object for listing topics. @@ -588,38 +590,36 @@ export class PubSub { options = {}; } const reqOpts = Object.assign( - { - project: 'projects/' + this.projectId, - }, - options - ); + { + project: 'projects/' + this.projectId, + }, + options); delete reqOpts.gaxOpts; delete reqOpts.autoPaginate; const gaxOpts = Object.assign( - { - autoPaginate: options.autoPaginate, - }, - options.gaxOpts - ); + { + autoPaginate: options.autoPaginate, + }, + options.gaxOpts); this.request( - { - client: 'PublisherClient', - method: 'listTopics', - reqOpts: reqOpts, - gaxOpts: gaxOpts, - }, - function() { - const topics = arguments[1]; - if (topics) { - arguments[1] = topics.map(function(topic) { - const topicInstance = self.topic(topic.name); - topicInstance.metadata = topic; - return topicInstance; - }); - } - callback.apply(null, arguments); - } - ); + { + client: 'PublisherClient', + method: 'listTopics', + reqOpts, + gaxOpts, + }, + // tslint:disable-next-line only-arrow-functions + function() { + const topics = arguments[1]; + if (topics) { + arguments[1] = topics.map(topic => { + const topicInstance = self.topic(topic.name); + topicInstance.metadata = topic; + return topicInstance; + }); + } + callback.apply(null, arguments); + }); } /** * Get the PubSub client object. @@ -635,9 +635,9 @@ export class PubSub { getClient_(config, callback) { const self = this; const hasProjectId = - this.projectId && this.projectId !== PROJECT_ID_PLACEHOLDER; + this.projectId && this.projectId !== PROJECT_ID_PLACEHOLDER; if (!hasProjectId && !this.isEmulator) { - this.auth.getProjectId(function(err, projectId) { + this.auth.getProjectId((err, projectId) => { if (err) { callback(err); return; @@ -656,7 +656,8 @@ export class PubSub { callback(null, gaxClient); } /** - * Funnel all API requests through this method, to be sure we have a project ID. + * Funnel all API requests through this method, to be sure we have a project + * ID. * * @private * @@ -668,7 +669,7 @@ export class PubSub { */ request(config, callback) { const self = this; - this.getClient_(config, function(err, client) { + this.getClient_(config, (err, client) => { if (err) { callback(err); return; @@ -859,7 +860,6 @@ export class PubSub { * this.end(); * }); */ -(PubSub.prototype as any).getTopicsStream = paginator.streamify('getTopics'); /*! Developer Documentation * @@ -888,19 +888,21 @@ export {Subscription, Topic}; * @module {PubSub} @google-cloud/pubsub * @alias nodejs-pubsub * - * @example Install the client library with npm: - * npm install --save @google-cloud/pubsub + * @example Install the client library with npm: npm install --save + * @google-cloud/pubsub * * @example Import the client library * const {PubSub} = require('@google-cloud/pubsub'); * - * @example Create a client that uses Application Default Credentials (ADC): - * const pubsub = new PubSub(); + * @example Create a client that uses Application + * Default Credentials (ADC): const pubsub = new PubSub(); * - * @example Create a client with explicit credentials: - * const pubsub = new PubSub({ - * projectId: 'your-project-id', - * keyFilename: '/path/to/keyfile.json' + * @example Create a client with explicit + * credentials: const pubsub = new PubSub({ projectId: + * 'your-project-id', keyFilename: '/path/to/keyfile.json' * }); * * @example include:samples/quickstart.js