diff --git a/src/cache.ts b/src/cache.ts index 07765e3e9a..6491a55ac0 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -33,14 +33,14 @@ export class ListWatch implements ObjectCache, In await this.doneHandler(null); } - public on(verb: string, cb: ObjectCallback) { + public on(verb: string, cb: ObjectCallback): void { if (this.callbackCache[verb] === undefined) { throw new Error(`Unknown verb: ${verb}`); } this.callbackCache[verb].push(cb); } - public off(verb: string, cb: ObjectCallback) { + public off(verb: string, cb: ObjectCallback): void { if (this.callbackCache[verb] === undefined) { throw new Error(`Unknown verb: ${verb}`); } @@ -72,7 +72,7 @@ export class ListWatch implements ObjectCache, In return this.resourceVersion; } - private async doneHandler(err: any) { + private async doneHandler(err: any): Promise { if (err) { this.callbackCache[ERROR].forEach((elt: ObjectCallback) => elt(err)); return; @@ -93,7 +93,7 @@ export class ListWatch implements ObjectCache, In ); } - private addOrUpdateItems(items: T[]) { + private addOrUpdateItems(items: T[]): void { items.forEach((obj: T) => { addOrUpdateObject( this.objects, @@ -107,7 +107,7 @@ export class ListWatch implements ObjectCache, In }); } - private indexObj(obj: T) { + private indexObj(obj: T): void { let namespaceList = this.indexCache[obj.metadata!.namespace!] as T[]; if (!namespaceList) { namespaceList = []; @@ -116,7 +116,7 @@ export class ListWatch implements ObjectCache, In addOrUpdateObject(namespaceList, obj); } - private watchHandler(phase: string, obj: T, watchObj?: any) { + private watchHandler(phase: string, obj: T, watchObj?: any): void { switch (phase) { case 'ADDED': case 'MODIFIED': @@ -172,7 +172,7 @@ export function addOrUpdateObject( obj: T, addCallback?: Array>, updateCallback?: Array>, -) { +): void { const ix = findKubernetesObject(objects, obj); if (ix === -1) { objects.push(obj); @@ -202,7 +202,7 @@ export function deleteObject( objects: T[], obj: T, deleteCallback?: Array>, -) { +): void { const ix = findKubernetesObject(objects, obj); if (ix !== -1) { objects.splice(ix, 1); diff --git a/src/cloud_auth.ts b/src/cloud_auth.ts index bc175f1ade..5fb36abe83 100644 --- a/src/cloud_auth.ts +++ b/src/cloud_auth.ts @@ -26,7 +26,10 @@ export class CloudAuth implements Authenticator { return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp'; } - public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) { + public async applyAuthentication( + user: User, + opts: request.Options | https.RequestOptions, + ): Promise { const token = this.getToken(user); if (token) { opts.headers!.Authorization = `Bearer ${token}`; @@ -41,7 +44,7 @@ export class CloudAuth implements Authenticator { return config['access-token']; } - private isExpired(config: Config) { + private isExpired(config: Config): boolean { const token = config['access-token']; const expiry = config.expiry; if (!token) { @@ -58,7 +61,7 @@ export class CloudAuth implements Authenticator { return false; } - private updateAccessToken(config: Config) { + private updateAccessToken(config: Config): void { let cmd = config['cmd-path']; if (!cmd) { throw new Error('Token is expired!'); diff --git a/src/config.ts b/src/config.ts index 868a23b332..01a3572a44 100644 --- a/src/config.ts +++ b/src/config.ts @@ -71,27 +71,27 @@ export class KubeConfig { this.users = []; } - public getContexts() { + public getContexts(): Context[] { return this.contexts; } - public getClusters() { + public getClusters(): Cluster[] { return this.clusters; } - public getUsers() { + public getUsers(): User[] { return this.users; } - public getCurrentContext() { + public getCurrentContext(): string { return this.currentContext; } - public setCurrentContext(context: string) { + public setCurrentContext(context: string): void { this.currentContext = context; } - public getContextObject(name: string) { + public getContextObject(name: string): Context | null { if (!this.contexts) { return null; } @@ -122,13 +122,13 @@ export class KubeConfig { return findObject(this.users, name, 'user'); } - public loadFromFile(file: string, opts?: Partial) { + public loadFromFile(file: string, opts?: Partial): void { const rootDirectory = path.dirname(file); this.loadFromString(fs.readFileSync(file, 'utf8'), opts); this.makePathsAbsolute(rootDirectory); } - public async applytoHTTPSOptions(opts: https.RequestOptions) { + public async applytoHTTPSOptions(opts: https.RequestOptions): Promise { const user = this.getCurrentUser(); await this.applyOptions(opts); @@ -138,7 +138,7 @@ export class KubeConfig { } } - public async applyToRequest(opts: request.Options) { + public async applyToRequest(opts: request.Options): Promise { const cluster = this.getCurrentCluster(); const user = this.getCurrentUser(); @@ -156,7 +156,7 @@ export class KubeConfig { } } - public loadFromString(config: string, opts?: Partial) { + public loadFromString(config: string, opts?: Partial): void { const obj = yaml.safeLoad(config); this.clusters = newClusters(obj.clusters, opts); this.contexts = newContexts(obj.contexts, opts); @@ -164,14 +164,14 @@ export class KubeConfig { this.currentContext = obj['current-context']; } - public loadFromOptions(options: any) { + public loadFromOptions(options: any): void { this.clusters = options.clusters; this.contexts = options.contexts; this.users = options.users; this.currentContext = options.currentContext; } - public loadFromClusterAndUser(cluster: Cluster, user: User) { + public loadFromClusterAndUser(cluster: Cluster, user: User): void { this.clusters = [cluster]; this.users = [user]; this.currentContext = 'loaded-context'; @@ -184,7 +184,7 @@ export class KubeConfig { ]; } - public loadFromCluster(pathPrefix: string = '') { + public loadFromCluster(pathPrefix: string = ''): void { const host = process.env.KUBERNETES_SERVICE_HOST; const port = process.env.KUBERNETES_SERVICE_PORT; const clusterName = 'inCluster'; @@ -231,7 +231,7 @@ export class KubeConfig { this.currentContext = contextName; } - public mergeConfig(config: KubeConfig) { + public mergeConfig(config: KubeConfig): void { this.currentContext = config.currentContext; config.clusters.forEach((cluster: Cluster) => { this.addCluster(cluster); @@ -244,7 +244,7 @@ export class KubeConfig { }); } - public addCluster(cluster: Cluster) { + public addCluster(cluster: Cluster): void { if (!this.clusters) { this.clusters = []; } @@ -256,7 +256,7 @@ export class KubeConfig { this.clusters.push(cluster); } - public addUser(user: User) { + public addUser(user: User): void { if (!this.users) { this.users = []; } @@ -268,7 +268,7 @@ export class KubeConfig { this.users.push(user); } - public addContext(ctx: Context) { + public addContext(ctx: Context): void { if (!this.contexts) { this.contexts = []; } @@ -280,7 +280,7 @@ export class KubeConfig { this.contexts.push(ctx); } - public loadFromDefault(opts?: Partial) { + public loadFromDefault(opts?: Partial): void { if (process.env.KUBECONFIG && process.env.KUBECONFIG.length > 0) { const files = process.env.KUBECONFIG.split(path.delimiter); this.loadFromFile(files[0], opts); @@ -323,7 +323,7 @@ export class KubeConfig { ); } - public makeApiClient(apiClientType: ApiConstructor) { + public makeApiClient(apiClientType: ApiConstructor): T { const cluster = this.getCurrentCluster(); if (!cluster) { throw new Error('No active cluster!'); @@ -334,7 +334,7 @@ export class KubeConfig { return apiClient; } - public makePathsAbsolute(rootDirectory: string) { + public makePathsAbsolute(rootDirectory: string): void { this.clusters.forEach((cluster: Cluster) => { if (cluster.caFile) { cluster.caFile = makeAbsolutePath(rootDirectory, cluster.caFile); @@ -364,11 +364,11 @@ export class KubeConfig { return JSON.stringify(configObj); } - private getCurrentContextObject() { + private getCurrentContextObject(): Context | null { return this.getContextObject(this.currentContext); } - private applyHTTPSOptions(opts: request.Options | https.RequestOptions) { + private applyHTTPSOptions(opts: request.Options | https.RequestOptions): void { const cluster = this.getCurrentCluster(); const user = this.getCurrentUser(); if (!user) { @@ -392,7 +392,7 @@ export class KubeConfig { } } - private async applyAuthorizationHeader(opts: request.Options | https.RequestOptions) { + private async applyAuthorizationHeader(opts: request.Options | https.RequestOptions): Promise { const user = this.getCurrentUser(); if (!user) { return; @@ -413,7 +413,7 @@ export class KubeConfig { } } - private async applyOptions(opts: request.Options | https.RequestOptions) { + private async applyOptions(opts: request.Options | https.RequestOptions): Promise { this.applyHTTPSOptions(opts); await this.applyAuthorizationHeader(opts); } @@ -428,9 +428,9 @@ type ApiConstructor = new (server: string) => T; // This class is deprecated and will eventually be removed. export class Config { - public static SERVICEACCOUNT_ROOT = '/var/run/secrets/kubernetes.io/serviceaccount'; - public static SERVICEACCOUNT_CA_PATH = Config.SERVICEACCOUNT_ROOT + '/ca.crt'; - public static SERVICEACCOUNT_TOKEN_PATH = Config.SERVICEACCOUNT_ROOT + '/token'; + public static SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount'; + public static SERVICEACCOUNT_CA_PATH: string = Config.SERVICEACCOUNT_ROOT + '/ca.crt'; + public static SERVICEACCOUNT_TOKEN_PATH: string = Config.SERVICEACCOUNT_ROOT + '/token'; public static fromFile(filename: string): api.CoreV1Api { return Config.apiFromFile(filename, api.CoreV1Api); diff --git a/src/exec_auth.ts b/src/exec_auth.ts index 054818ce47..c61531de52 100644 --- a/src/exec_auth.ts +++ b/src/exec_auth.ts @@ -36,7 +36,10 @@ export class ExecAuth implements Authenticator { ); } - public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) { + public async applyAuthentication( + user: User, + opts: request.Options | https.RequestOptions, + ): Promise { const credential = this.getCredential(user); if (!credential) { return; diff --git a/src/file_auth.ts b/src/file_auth.ts index 3fc5cd2a4c..a1380cbb82 100644 --- a/src/file_auth.ts +++ b/src/file_auth.ts @@ -13,7 +13,10 @@ export class FileAuth implements Authenticator { return user.authProvider && user.authProvider.config && user.authProvider.config.tokenFile; } - public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) { + public async applyAuthentication( + user: User, + opts: request.Options | https.RequestOptions, + ): Promise { if (this.token == null) { this.refreshToken(user.authProvider.config.tokenFile); } @@ -25,7 +28,7 @@ export class FileAuth implements Authenticator { } } - private refreshToken(filePath: string) { + private refreshToken(filePath: string): void { // TODO make this async? this.token = fs.readFileSync(filePath).toString('UTF-8'); this.lastRead = new Date(); diff --git a/src/informer.ts b/src/informer.ts index dd8949cefe..1bd0f34631 100644 --- a/src/informer.ts +++ b/src/informer.ts @@ -18,8 +18,8 @@ export const DELETE: string = 'delete'; export const ERROR: string = 'error'; export interface Informer { - on(verb: string, fn: ObjectCallback); - off(verb: string, fn: ObjectCallback); + on(verb: string, fn: ObjectCallback): void; + off(verb: string, fn: ObjectCallback): void; start(): Promise; } diff --git a/src/oidc_auth.ts b/src/oidc_auth.ts index 379a67a109..dfe39011a4 100644 --- a/src/oidc_auth.ts +++ b/src/oidc_auth.ts @@ -40,7 +40,7 @@ export class OpenIDConnectAuth implements Authenticator { } // public for testing purposes. - private currentTokenExpiration = 0; + private currentTokenExpiration: number = 0; public isAuthProvider(user: User): boolean { if (!user.authProvider) { return false; @@ -58,7 +58,7 @@ export class OpenIDConnectAuth implements Authenticator { user: User, opts: request.Options | https.RequestOptions, overrideClient?: any, - ) { + ): Promise { const token = await this.getToken(user, overrideClient); if (token) { opts.headers!.Authorization = `Bearer ${token}`; @@ -102,7 +102,7 @@ export class OpenIDConnectAuth implements Authenticator { return user.authProvider.config['id-token']; } - private async getClient(user: User) { + private async getClient(user: User): Promise { const oidcIssuer = await Issuer.discover(user.authProvider.config['idp-issuer-url']); return new oidcIssuer.Client({ client_id: user.authProvider.config['client-id'], diff --git a/src/terminal-size-queue.ts b/src/terminal-size-queue.ts index b461f8f0ff..c6c6cdee5a 100644 --- a/src/terminal-size-queue.ts +++ b/src/terminal-size-queue.ts @@ -3,7 +3,7 @@ import { Readable, ReadableOptions } from 'stream'; export interface ResizableStream { columns: number; rows: number; - on(event: 'resize', cb: () => void); + on(event: 'resize', cb: () => void): void; } export interface TerminalSize { @@ -16,11 +16,11 @@ export class TerminalSizeQueue extends Readable { super({ ...opts, // tslint:disable-next-line:no-empty - read() {}, + read(): void {}, }); } - public handleResizes(writeStream: ResizableStream) { + public handleResizes(writeStream: ResizableStream): void { // Set initial size this.resize(getTerminalSize(writeStream)); @@ -28,12 +28,12 @@ export class TerminalSizeQueue extends Readable { writeStream.on('resize', () => this.resize(getTerminalSize(writeStream))); } - private resize(size: TerminalSize) { + private resize(size: TerminalSize): void { this.push(JSON.stringify(size)); } } -export function isResizable(stream: any) { +export function isResizable(stream: any): boolean { if (stream == null) { return false; } diff --git a/src/watch.ts b/src/watch.ts index 9593c056d1..5a98bb76f0 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -7,18 +7,29 @@ export interface WatchUpdate { object: object; } +export interface Response { + statusCode: number; + statusMessage: string; +} + export interface RequestInterface { - webRequest(opts: request.Options, callback: (err, response, body) => void): any; + webRequest( + opts: request.Options, + callback: (err: object | null, response: Response | null, body: object | null) => void, + ): any; } export class DefaultRequest implements RequestInterface { - public webRequest(opts: request.Options, callback: (err, response, body) => void): any { + public webRequest( + opts: request.Options, + callback: (err: object | null, response: Response | null, body: object | null) => void, + ): any { return request(opts, callback); } } export class Watch { - public static SERVER_SIDE_CLOSE = { error: 'Connection closed on server' }; + public static SERVER_SIDE_CLOSE: object = { error: 'Connection closed on server' }; public config: KubeConfig; private readonly requestImpl: RequestInterface; diff --git a/src/web-socket-handler.ts b/src/web-socket-handler.ts index b6387c2dd9..416369bbf8 100644 --- a/src/web-socket-handler.ts +++ b/src/web-socket-handler.ts @@ -15,11 +15,11 @@ export interface WebSocketInterface { } export class WebSocketHandler implements WebSocketInterface { - public static readonly StdinStream = 0; - public static readonly StdoutStream = 1; - public static readonly StderrStream = 2; - public static readonly StatusStream = 3; - public static readonly ResizeStream = 4; + public static readonly StdinStream: number = 0; + public static readonly StdoutStream: number = 1; + public static readonly StderrStream: number = 2; + public static readonly StatusStream: number = 3; + public static readonly ResizeStream: number = 4; public static handleStandardStreams( streamNum: number, diff --git a/tslint.json b/tslint.json index d9524eed15..bfdc3a013e 100644 --- a/tslint.json +++ b/tslint.json @@ -14,7 +14,8 @@ "interface-name": [true, "never-prefix"], "object-literal-sort-keys": false, "object-literal-key-quotes": [true, "as-needed"], - "max-classes-per-file": false + "max-classes-per-file": false, + "typedef": [true, "call-signature", "parameter", "member-variable-declaration"] }, "rulesDirectory": [] }