From eecefd324930703c18dad9d4fe27a3602992675c Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 1 Sep 2018 11:26:15 -0400 Subject: [PATCH] grpc-js-core: fix lint This commit makes the lint Gulp task pass again. --- .../grpc-js-core/src/channel-credentials.ts | 6 +- packages/grpc-js-core/src/channel.ts | 12 +-- packages/grpc-js-core/src/client.ts | 18 ++--- .../grpc-js-core/src/compression-filter.ts | 74 ++++++++++--------- packages/grpc-js-core/src/index.ts | 6 +- packages/grpc-js-core/src/logging.ts | 1 + packages/grpc-js-core/src/subchannel.ts | 8 +- .../grpc-js-core/test/test-call-stream.ts | 20 ++--- packages/grpc-js-core/test/test-logging.ts | 6 +- 9 files changed, 78 insertions(+), 73 deletions(-) diff --git a/packages/grpc-js-core/src/channel-credentials.ts b/packages/grpc-js-core/src/channel-credentials.ts index cb29fa64..e123df08 100644 --- a/packages/grpc-js-core/src/channel-credentials.ts +++ b/packages/grpc-js-core/src/channel-credentials.ts @@ -104,13 +104,13 @@ export abstract class ChannelCredentials { key: privateKey || undefined, cert: certChain || undefined }); - let connectionOptions: ConnectionOptions = { + const connectionOptions: ConnectionOptions = { secureContext }; if (verifyOptions && verifyOptions.checkServerIdentity) { connectionOptions.checkServerIdentity = (host: string, cert: PeerCertificate) => { return verifyOptions.checkServerIdentity!(host, {raw: cert.raw}); - } + }; } return new SecureChannelCredentialsImpl(connectionOptions); } @@ -141,7 +141,7 @@ class InsecureChannelCredentialsImpl extends ChannelCredentials { } class SecureChannelCredentialsImpl extends ChannelCredentials { - connectionOptions: ConnectionOptions + connectionOptions: ConnectionOptions; constructor(connectionOptions: ConnectionOptions, callCredentials?: CallCredentials) { super(callCredentials); diff --git a/packages/grpc-js-core/src/channel.ts b/packages/grpc-js-core/src/channel.ts index 41b52562..a8a76591 100644 --- a/packages/grpc-js-core/src/channel.ts +++ b/packages/grpc-js-core/src/channel.ts @@ -177,7 +177,7 @@ export class Http2Channel extends EventEmitter implements Channel { } private startConnecting(): void { - let connectionOptions: http2.SecureClientSessionOptions = this.credentials._getConnectionOptions() || {}; + const connectionOptions: http2.SecureClientSessionOptions = this.credentials._getConnectionOptions() || {}; if (connectionOptions.secureContext !== null) { // If provided, the value of grpc.ssl_target_name_override should be used // to override the target hostname when checking server identity. @@ -226,7 +226,7 @@ export class Http2Channel extends EventEmitter implements Channel { address: string, readonly credentials: ChannelCredentials, private readonly options: Partial) { super(); - for (let option in options) { + for (const option in options) { if (options.hasOwnProperty(option)) { if (!recognizedOptions.hasOwnProperty(option)) { console.warn(`Unrecognized channel argument '${option}' will be ignored.`); @@ -300,7 +300,7 @@ export class Http2Channel extends EventEmitter implements Channel { throw new Error('Channel has been shut down'); } const finalOptions: CallStreamOptions = { - deadline: (deadline === null || deadline == undefined) ? Infinity : deadline, + deadline: (deadline === null || deadline === undefined) ? Infinity : deadline, flags: propagateFlags || 0, host: host || this.defaultAuthority, parentCall: parentCall || null @@ -358,7 +358,7 @@ export class Http2Channel extends EventEmitter implements Channel { * we assume that a state change has successfully occurred */ setImmediate(callback); } else { - let deadlineMs: number = 0; + let deadlineMs = 0; if (deadline instanceof Date) { deadlineMs = deadline.getTime(); } else { @@ -368,11 +368,11 @@ export class Http2Channel extends EventEmitter implements Channel { if (timeout < 0) { timeout = 0; } - let timeoutId = setTimeout(() => { + const timeoutId = setTimeout(() => { this.removeListener('connectivityStateChanged', eventCb); callback(new Error('Channel state did not change before deadline')); }, timeout); - let eventCb = () => { + const eventCb = () => { clearTimeout(timeoutId); callback(); }; diff --git a/packages/grpc-js-core/src/client.ts b/packages/grpc-js-core/src/client.ts index 7bfc19bb..85597d96 100644 --- a/packages/grpc-js-core/src/client.ts +++ b/packages/grpc-js-core/src/client.ts @@ -19,11 +19,11 @@ export interface UnaryCallback { } export interface CallOptions { - deadline?: Deadline, - host?: string, - parent?: Call, - propagate_flags?: number, - credentials?: CallCredentials + deadline?: Deadline; + host?: string; + parent?: Call; + propagate_flags?: number; + credentials?: CallCredentials; } export type ClientOptions = Partial & { @@ -64,18 +64,18 @@ export class Client { callback(new Error('Failed to connect before the deadline')); return; } - var new_state; + let newState; try { - new_state = this[kChannel].getConnectivityState(true); + newState = this[kChannel].getConnectivityState(true); } catch (e) { callback(new Error('The channel has been closed')); return; } - if (new_state === ConnectivityState.READY) { + if (newState === ConnectivityState.READY) { callback(); } else { try { - this[kChannel].watchConnectivityState(new_state, deadline, checkState); + this[kChannel].watchConnectivityState(newState, deadline, checkState); } catch (e) { callback(new Error('The channel has been closed')); } diff --git a/packages/grpc-js-core/src/compression-filter.ts b/packages/grpc-js-core/src/compression-filter.ts index d694f239..11b20598 100644 --- a/packages/grpc-js-core/src/compression-filter.ts +++ b/packages/grpc-js-core/src/compression-filter.ts @@ -19,7 +19,7 @@ abstract class CompressionHandler { if (compress) { messageBuffer = await this.compressMessage(messageBuffer); } - let output = Buffer.allocUnsafe(messageBuffer.length + 5); + const output = Buffer.allocUnsafe(messageBuffer.length + 5); output.writeUInt8(compress ? 1 : 0, 0); output.writeUInt32BE(messageBuffer.length, 1); messageBuffer.copy(output, 5); @@ -45,7 +45,7 @@ class IdentityHandler extends CompressionHandler { } async writeMessage(message: Buffer, compress: boolean): Promise { - let output = Buffer.allocUnsafe(message.length + 5); + const output = Buffer.allocUnsafe(message.length + 5); /* With "identity" compression, messages should always be marked as * uncompressed */ output.writeUInt8(0, 0); @@ -62,49 +62,53 @@ class IdentityHandler extends CompressionHandler { class DeflateHandler extends CompressionHandler { compressMessage(message: Buffer) { - return new Promise( - (resolve, reject) => {zlib.deflate(message, (err, output) => { - if (err) { - reject(err); - } else { - resolve(output); - } - })}); + return new Promise((resolve, reject) => { + zlib.deflate(message, (err, output) => { + if (err) { + reject(err); + } else { + resolve(output); + } + }); + }); } decompressMessage(message: Buffer) { - return new Promise( - (resolve, reject) => {zlib.inflate(message, (err, output) => { - if (err) { - reject(err); - } else { - resolve(output); - } - })}); + return new Promise((resolve, reject) => { + zlib.inflate(message, (err, output) => { + if (err) { + reject(err); + } else { + resolve(output); + } + }); + }); } } class GzipHandler extends CompressionHandler { compressMessage(message: Buffer) { - return new Promise( - (resolve, reject) => {zlib.gzip(message, (err, output) => { - if (err) { - reject(err); - } else { - resolve(output); - } - })}); + return new Promise((resolve, reject) => { + zlib.gzip(message, (err, output) => { + if (err) { + reject(err); + } else { + resolve(output); + } + }); + }); } decompressMessage(message: Buffer) { - return new Promise( - (resolve, reject) => {zlib.unzip(message, (err, output) => { - if (err) { - reject(err); - } else { - resolve(output); - } - })}); + return new Promise((resolve, reject) => { + zlib.unzip(message, (err, output) => { + if (err) { + reject(err); + } else { + resolve(output); + } + }); + }); } } @@ -150,7 +154,7 @@ export class CompressionFilter extends BaseFilter implements Filter { async receiveMetadata(metadata: Promise): Promise { const headers: Metadata = await metadata; - let receiveEncoding: MetadataValue[] = headers.get('grpc-encoding'); + const receiveEncoding: MetadataValue[] = headers.get('grpc-encoding'); if (receiveEncoding.length > 0) { const encoding: MetadataValue = receiveEncoding[0]; if (typeof encoding === 'string') { diff --git a/packages/grpc-js-core/src/index.ts b/packages/grpc-js-core/src/index.ts index 79318a6d..71327316 100644 --- a/packages/grpc-js-core/src/index.ts +++ b/packages/grpc-js-core/src/index.ts @@ -18,15 +18,15 @@ if (!semver.satisfies(process.version, supportedNodeVersions)) { } interface IndexedObject { - [key: string]: any; - [key: number]: any; + [key: string]: any; // tslint:disable-line no-any + [key: number]: any; // tslint:disable-line no-any } function mixin(...sources: IndexedObject[]) { const result: {[key: string]: Function} = {}; for (const source of sources) { for (const propName of Object.getOwnPropertyNames(source)) { - const property: any = source[propName]; + const property: any = source[propName]; // tslint:disable-line no-any if (typeof property === 'function') { result[propName] = property; } diff --git a/packages/grpc-js-core/src/logging.ts b/packages/grpc-js-core/src/logging.ts index b5e151c4..a0e4e8c5 100644 --- a/packages/grpc-js-core/src/logging.ts +++ b/packages/grpc-js-core/src/logging.ts @@ -15,6 +15,7 @@ export const setLoggerVerbosity = (verbosity: LogVerbosity): void => { _logVerbosity = verbosity; }; +// tslint:disable-next-line no-any export const log = (severity: LogVerbosity, ...args: any[]): void => { if (severity >= _logVerbosity && typeof _logger.error === 'function') { _logger.error(...args); diff --git a/packages/grpc-js-core/src/subchannel.ts b/packages/grpc-js-core/src/subchannel.ts index f793aebb..6182de59 100644 --- a/packages/grpc-js-core/src/subchannel.ts +++ b/packages/grpc-js-core/src/subchannel.ts @@ -35,7 +35,7 @@ export interface SubChannel extends EventEmitter { export class Http2SubChannel extends EventEmitter implements SubChannel { private session: http2.ClientHttp2Session; - private refCount: number = 0; + private refCount = 0; private userAgent: string; private keepaliveTimeMs: number = KEEPALIVE_TIME_MS; @@ -57,7 +57,7 @@ export class Http2SubChannel extends EventEmitter implements SubChannel { this.session.on('error', () => { this.stopKeepalivePings(); this.emit('close'); - }) + }); this.userAgent = userAgent; if (channelArgs['grpc.keepalive_time_ms']) { @@ -120,7 +120,7 @@ export class Http2SubChannel extends EventEmitter implements SubChannel { headers[HTTP2_HEADER_METHOD] = 'POST'; headers[HTTP2_HEADER_PATH] = callStream.getMethod(); headers[HTTP2_HEADER_TE] = 'trailers'; - let http2Stream = this.session.request(headers); + const http2Stream = this.session.request(headers); this.ref(); http2Stream.on('close', () => { this.unref(); @@ -131,4 +131,4 @@ export class Http2SubChannel extends EventEmitter implements SubChannel { close() { this.session.close(); } -} \ No newline at end of file +} diff --git a/packages/grpc-js-core/test/test-call-stream.ts b/packages/grpc-js-core/test/test-call-stream.ts index 902007fd..d405dfd0 100644 --- a/packages/grpc-js-core/test/test-call-stream.ts +++ b/packages/grpc-js-core/test/test-call-stream.ts @@ -90,7 +90,7 @@ describe('CallStream', () => { * but this might break if we start checking channel arguments, in which case * we will need a more sophisticated fake */ const filterStackFactory = - new FilterStackFactory([new CompressionFilterFactory({})]); + new FilterStackFactory([new CompressionFilterFactory({} as Channel)]); const message = 'eat this message'; // 16 bytes beforeEach(() => { @@ -102,7 +102,7 @@ describe('CallStream', () => { const responseMetadata = new Metadata(); responseMetadata.add('key', 'value'); const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock( {payload: Buffer.alloc(0), frameLengths: []}); @@ -140,7 +140,7 @@ describe('CallStream', () => { maybeSkip(it)(`for error code ${key}`, () => { return new Promise((resolve, reject) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock( {payload: Buffer.alloc(0), frameLengths: []}); callStream.attachHttp2Stream(http2Stream); @@ -160,7 +160,7 @@ describe('CallStream', () => { it('should have functioning getters', (done) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); assert.strictEqual(callStream.getDeadline(), callStreamArgs.deadline); assert.strictEqual(callStream.getStatus(), null); const credentials = CallCredentials.createEmpty(); @@ -179,7 +179,7 @@ describe('CallStream', () => { describe('attachHttp2Stream', () => { it('should handle an empty message', (done) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock({payload: serialize(''), frameLengths: []}); callStream.once('data', assert2.mustCall((buffer) => { @@ -206,7 +206,7 @@ describe('CallStream', () => { it(`should handle a short message where ${testCase.description}`, (done) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock({ payload: serialize(message), // 21 bytes frameLengths: testCase.frameLengths @@ -236,7 +236,7 @@ describe('CallStream', () => { }].forEach((testCase: {description: string, frameLengths: number[]}) => { it(`should handle two messages where ${testCase.description}`, (done) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock({ payload: Buffer.concat( [serialize(message), serialize(message)]), // 42 bytes @@ -256,7 +256,7 @@ describe('CallStream', () => { it('should send buffered writes', (done) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock( {payload: Buffer.alloc(0), frameLengths: []}); let streamFlushed = false; @@ -279,7 +279,7 @@ describe('CallStream', () => { it('should cause data chunks in write calls afterward to be written to the given stream', (done) => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock( {payload: Buffer.alloc(0), frameLengths: []}); http2Stream.once('write', assert2.mustCall((chunk: Buffer) => { @@ -297,7 +297,7 @@ describe('CallStream', () => { it('should handle underlying stream errors', () => { const callStream = - new Http2CallStream('foo', {}, callStreamArgs, filterStackFactory); + new Http2CallStream('foo', {} as Http2Channel, callStreamArgs, filterStackFactory); const http2Stream = new ClientHttp2StreamMock( {payload: Buffer.alloc(0), frameLengths: []}); callStream.once('status', assert2.mustCall((status) => { diff --git a/packages/grpc-js-core/test/test-logging.ts b/packages/grpc-js-core/test/test-logging.ts index c1c0c25b..46223a52 100644 --- a/packages/grpc-js-core/test/test-logging.ts +++ b/packages/grpc-js-core/test/test-logging.ts @@ -4,7 +4,7 @@ import * as grpc from '../src'; import * as logging from '../src/logging'; describe('Logging', () => { - afterEach(function() { + afterEach(() => { // Ensure that the logger is restored to its defaults after each test. grpc.setLogger(console); grpc.setLogVerbosity(grpc.logVerbosity.DEBUG); @@ -22,9 +22,9 @@ describe('Logging', () => { }); it('gates logging based on severity', () => { - const output: any[] = []; + const output: Array = []; const logger: Partial = { - error(...args: any[]): void { + error(...args: string[]): void { output.push(args); } };