From 6db4ca9fb0f51c3086b06f4b35e3bbd0212e411e Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Sat, 2 Mar 2019 15:14:20 -0800 Subject: [PATCH 1/2] refactor: clean up unused packages --- package.json | 11 +++----- src/iam.ts | 3 +-- src/message-stream.ts | 1 - src/publisher.ts | 3 +-- src/pubsub.ts | 6 ++--- src/subscription.ts | 7 +++-- src/topic.ts | 6 ++--- system-test/pubsub.ts | 61 +++++++++++++++---------------------------- test/topic.ts | 28 +++++++------------- 9 files changed, 43 insertions(+), 83 deletions(-) diff --git a/package.json b/package.json index 7df205ec..2f8d2d5f 100644 --- a/package.json +++ b/package.json @@ -56,28 +56,22 @@ "@google-cloud/precise-date": "^0.1.0", "@google-cloud/projectify": "^0.3.0", "@google-cloud/promisify": "^0.4.0", + "@sindresorhus/is": "^0.15.0", "arrify": "^1.0.0", "async-each": "^1.0.1", - "delay": "^4.0.0", - "duplexify": "^4.0.0", "extend": "^3.0.1", "google-auth-library": "^3.0.0", "google-gax": "^0.25.0", - "is": "^3.0.1", "is-stream-ended": "^0.1.4", - "lodash.chunk": "^4.2.0", "lodash.merge": "^4.6.0", "lodash.snakecase": "^4.1.1", "p-defer": "^1.0.0", - "protobufjs": "^6.8.1", - "through2": "^3.0.0" + "protobufjs": "^6.8.1" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^3.0.0", "@types/arrify": "^1.0.4", - "@types/duplexify": "^3.6.0", "@types/extend": "^3.0.0", - "@types/is": "0.0.21", "@types/mocha": "^5.2.5", "@types/p-defer": "^1.0.3", "@types/proxyquire": "^1.3.28", @@ -100,6 +94,7 @@ "proxyquire": "^2.0.0", "sinon": "^7.1.1", "source-map-support": "^0.5.9", + "through2": "^3.0.0", "typescript": "~3.3.0", "uuid": "^3.1.0", "linkinator": "^1.1.2" diff --git a/src/iam.ts b/src/iam.ts index b9bef871..a797bf00 100644 --- a/src/iam.ts +++ b/src/iam.ts @@ -100,8 +100,7 @@ export class IAM { this.Promise = pubsub.Promise; } this.pubsub = pubsub; - // tslint:disable-next-line no-any - this.request = pubsub.request.bind(pubsub) as any; + this.request = pubsub.request.bind(pubsub); this.id = id; } diff --git a/src/message-stream.ts b/src/message-stream.ts index 96cfa4d4..c52400a3 100644 --- a/src/message-stream.ts +++ b/src/message-stream.ts @@ -15,7 +15,6 @@ */ import {promisify} from '@google-cloud/promisify'; -import {Gaxios} from 'gaxios'; import {ClientStub} from 'google-gax'; import {ClientDuplexStream, Metadata, ServiceError, status, StatusObject} from 'grpc'; import * as isStreamEnded from 'is-stream-ended'; diff --git a/src/publisher.ts b/src/publisher.ts index bdeecd07..7e2a1fd5 100644 --- a/src/publisher.ts +++ b/src/publisher.ts @@ -21,10 +21,9 @@ import {google} from '../proto/pubsub'; const each = require('async-each'); import * as extend from 'extend'; -import * as is from 'is'; +import is from '@sindresorhus/is'; import {Topic} from './topic'; import {RequestCallback} from './pubsub'; -import {ServiceError} from 'grpc'; interface Inventory { callbacks: PublishCallback[]; diff --git a/src/pubsub.ts b/src/pubsub.ts index 136134aa..ffc87b5b 100644 --- a/src/pubsub.ts +++ b/src/pubsub.ts @@ -18,10 +18,10 @@ import {paginator} from '@google-cloud/paginator'; import {replaceProjectIdToken} from '@google-cloud/projectify'; import {promisifyAll} from '@google-cloud/promisify'; +import is from '@sindresorhus/is'; import * as extend from 'extend'; import {GoogleAuth} from 'google-auth-library'; import * as gax from 'google-gax'; -import * as is from 'is'; const PKG = require('../../package.json'); const v1 = require('./v1'); @@ -34,8 +34,6 @@ import {CallOptions} from 'google-gax'; import {Transform} from 'stream'; import {google} from '../proto/pubsub'; import {ServiceError, ChannelCredentials} from 'grpc'; -import {FlowControlOptions} from './lease-manager'; -import {BatchPublishOptions} from './publisher'; const opts = {} as gax.GrpcClientOptions; const {grpc} = new gax.GrpcClient(opts); @@ -697,7 +695,7 @@ export class PubSub { }); } - getTopics(options: PageOptions): Promise; + getTopics(options?: PageOptions): Promise; getTopics(callback: GetTopicsCallback): void; getTopics(options: PageOptions, callback: GetTopicsCallback): void; /** diff --git a/src/subscription.ts b/src/subscription.ts index 838e38e3..0b0e4d32 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -15,10 +15,10 @@ */ import {promisifyAll} from '@google-cloud/promisify'; +import is from '@sindresorhus/is'; import {EventEmitter} from 'events'; import * as extend from 'extend'; import {CallOptions} from 'google-gax'; -import * as is from 'is'; import * as snakeCase from 'lodash.snakecase'; import {google} from '../proto/pubsub'; @@ -27,7 +27,7 @@ import {IAM} from './iam'; import {FlowControlOptions} from './lease-manager'; import {EmptyCallback, EmptyResponse, ExistsCallback, ExistsResponse, Omit, PubSub, RequestCallback, ResourceCallback} from './pubsub'; import {CreateSnapshotCallback, CreateSnapshotResponse, SeekCallback, SeekResponse, Snapshot} from './snapshot'; -import {Message, Subscriber, SubscriberOptions} from './subscriber'; +import {Subscriber, SubscriberOptions} from './subscriber'; import {Topic} from './topic'; import {noop} from './util'; @@ -202,8 +202,7 @@ export class Subscription extends EventEmitter { options = options || {}; this.pubsub = pubsub; - // tslint:disable-next-line no-any - this.request = pubsub.request.bind(pubsub) as any; + this.request = pubsub.request.bind(pubsub); this.name = Subscription.formatName_(this.projectId, name); this.topic = options.topic; diff --git a/src/topic.ts b/src/topic.ts index 9f625b9b..bd5277d5 100644 --- a/src/topic.ts +++ b/src/topic.ts @@ -16,9 +16,8 @@ import {paginator} from '@google-cloud/paginator'; import {promisifyAll} from '@google-cloud/promisify'; +import is from '@sindresorhus/is'; import {CallOptions} from 'google-gax'; -import * as is from 'is'; -import {Readable} from 'stream'; import {google} from '../proto/pubsub'; @@ -95,8 +94,7 @@ export class Topic { * @type {PubSub} */ this.parent = this.pubsub = pubsub; - // tslint:disable-next-line no-any - this.request = pubsub.request.bind(pubsub) as any; + this.request = pubsub.request.bind(pubsub); /** * [IAM (Identity and Access * Management)](https://cloud.google.com/pubsub/access_control) allows you diff --git a/system-test/pubsub.ts b/system-test/pubsub.ts index 5b75e3a7..7136c895 100644 --- a/system-test/pubsub.ts +++ b/system-test/pubsub.ts @@ -17,7 +17,8 @@ import * as assert from 'assert'; import * as defer from 'p-defer'; import * as uuid from 'uuid'; -import {PubSub, Subscription, Topic} from '../src'; + +import {Message, PubSub, Snapshot, Subscription, Topic} from '../src'; const pubsub = new PubSub(); @@ -60,7 +61,7 @@ describe('pubsub', () => { for (let i = 0; i < 6; i++) { await topic.publish(Buffer.from(message), options); } - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { subscription.on('error', reject); subscription.once('message', resolve); }); @@ -77,28 +78,19 @@ describe('pubsub', () => { }); describe('Topic', () => { - it('should be listed', done => { - pubsub.getTopics((err, topics) => { - assert.ifError(err); - - const results = topics!.filter(topic => { - const name = getTopicName(topic); - return TOPIC_FULL_NAMES.indexOf(name) !== -1; - }); - - // get all topics in list of known names - assert.strictEqual(results.length, TOPIC_NAMES.length); - done(); + it('should be listed', async () => { + const [topics] = await pubsub.getTopics(); + const results = topics.filter(topic => { + const name = getTopicName(topic); + return TOPIC_FULL_NAMES.indexOf(name) !== -1; }); + // get all topics in list of known names + assert.strictEqual(results.length, TOPIC_NAMES.length); }); it('should list topics in a stream', done => { - // tslint:disable-next-line no-any - const topicsEmitted: any[] = []; - - // tslint:disable-next-line no-any - (pubsub as any) - .getTopicsStream() + const topicsEmitted = new Array(); + pubsub.getTopicsStream() .on('error', done) .on('data', topic => { @@ -115,17 +107,12 @@ describe('pubsub', () => { }); }); - it('should allow manual paging', done => { - pubsub.getTopics( - { - pageSize: TOPIC_NAMES.length - 1, - gaxOpts: {autoPaginate: false}, - }, - (err, topics) => { - assert.ifError(err); - assert.strictEqual(topics!.length, TOPIC_NAMES.length - 1); - done(); - }); + it('should allow manual paging', async () => { + const [topics] = await pubsub.getTopics({ + pageSize: TOPIC_NAMES.length - 1, + gaxOpts: {autoPaginate: false}, + }); + assert.strictEqual(topics.length, TOPIC_NAMES.length - 1); }); it('should be created and deleted', done => { @@ -178,8 +165,7 @@ describe('pubsub', () => { const attrs = { customAttribute: 'value', }; - // tslint:disable-next-line no-any - const message: any = await publishPop(data, attrs); + const message = await publishPop(data, attrs); assert.deepStrictEqual(message.data, data); assert.deepStrictEqual(message.attributes, attrs); }); @@ -610,15 +596,10 @@ describe('pubsub', () => { }); it('should get a list of snapshots as a stream', done => { - // tslint:disable-next-line no-any - const snapshots: any[] = []; - + const snapshots = new Array(); pubsub.getSnapshotsStream() .on('error', done) - .on('data', - snapshot => { - snapshots.push(snapshot); - }) + .on('data', snapshots.push) .on('end', () => { assert(snapshots.length > 0); const names = snapshots.map(getSnapshotName); diff --git a/test/topic.ts b/test/topic.ts index b35f4919..e8cd6533 100644 --- a/test/topic.ts +++ b/test/topic.ts @@ -23,8 +23,8 @@ import * as sinon from 'sinon'; import {google} from '../proto/pubsub'; import {ExistsCallback, RequestCallback, RequestConfig} from '../src/pubsub'; -import {CreateSubscriptionCallback, CreateSubscriptionOptions, Subscription, SubscriptionOptions} from '../src/subscription'; -import {CreateTopicCallback, GetTopicMetadataCallback, Topic} from '../src/topic'; +import {CreateSubscriptionOptions, Subscription, SubscriptionOptions} from '../src/subscription'; +import {GetTopicMetadataCallback, Topic} from '../src/topic'; import * as util from '../src/util'; let promisified = false; @@ -41,26 +41,20 @@ const fakePromisify = Object.assign({}, pfy, { }); class FakeIAM { - // tslint:disable-next-line no-any - calledWith_: any[]; - // tslint:disable-next-line no-any - constructor(...args: any[]) { + calledWith_: Array<{}>; + constructor(...args: Array<{}>) { this.calledWith_ = args; } } class FakePublisher { - // tslint:disable-next-line no-any - calledWith_: any[]; - // tslint:disable-next-line no-any - published_!: any[]; + calledWith_: Array<{}>; + published_!: Array<{}>; options_!: object; - // tslint:disable-next-line no-any - constructor(...args: any[]) { + constructor(...args: Array<{}>) { this.calledWith_ = args; } - // tslint:disable-next-line no-any - publish(...args: any[]) { + publish(...args: Array<{}>) { this.published_ = args; } setOptions(options: object) { @@ -70,12 +64,10 @@ class FakePublisher { let extended = false; const fakePaginator = { - // tslint:disable-next-line variable-name - extend(Class: Function, methods: string[]) { - if (Class.name !== 'Topic') { + extend(klass: Function, methods: string[]) { + if (klass.name !== 'Topic') { return; } - assert.deepStrictEqual(methods, ['getSubscriptions']); extended = true; }, From f8d870a6c24747665cfdceed0ad58df94d92620f Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Sun, 3 Mar 2019 00:13:07 -0500 Subject: [PATCH 2/2] fix system test --- system-test/pubsub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system-test/pubsub.ts b/system-test/pubsub.ts index 7136c895..742b0f16 100644 --- a/system-test/pubsub.ts +++ b/system-test/pubsub.ts @@ -599,7 +599,7 @@ describe('pubsub', () => { const snapshots = new Array(); pubsub.getSnapshotsStream() .on('error', done) - .on('data', snapshots.push) + .on('data', snapshot => snapshots.push(snapshot)) .on('end', () => { assert(snapshots.length > 0); const names = snapshots.map(getSnapshotName);