From 1c635c184c3a472da61605a1052e56d5650d68a1 Mon Sep 17 00:00:00 2001 From: Leon Kiefer Date: Sat, 24 Jul 2021 16:35:40 +0200 Subject: [PATCH 1/2] replace callback with promise in Log return HttpError in Log --- examples/follow-logs.js | 4 ++-- src/log.ts | 33 ++++++++++++++++----------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/examples/follow-logs.js b/examples/follow-logs.js index d378f56b6c..caa7e982af 100644 --- a/examples/follow-logs.js +++ b/examples/follow-logs.js @@ -13,11 +13,11 @@ logStream.on('data', (chunk) => { process.stdout.write(chunk); }); -log.log('default', 'pod1', 'container1', logStream, (err) => {console.log(err)}, {follow: true, tailLines: 50, pretty: false, timestamps: false}) +log.log('default', 'pod1', 'container1', logStream, {follow: true, tailLines: 50, pretty: false, timestamps: false}) +.catch(err => {console.log(err)}) .then(req => { // disconnects after 5 seconds setTimeout(function(){ req.abort(); }, 5000); }); - diff --git a/src/log.ts b/src/log.ts index 11bb6adbac..021d2cb370 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,7 +1,7 @@ -import request = require('request'); +import * as request from 'request'; import { Writable } from 'stream'; - import { KubeConfig } from './config'; +import { HttpError, ObjectSerializer } from './gen/api'; export interface LogOptions { /** @@ -56,7 +56,6 @@ export class Log { podName: string, containerName: string, stream: Writable, - done: (err: any) => void, options: LogOptions = {}, ): Promise { const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`; @@ -77,20 +76,20 @@ export class Log { }; await this.config.applyToRequest(requestOptions); - const req = request(requestOptions, (error, response, body) => { - if (error) { - done(error); - } else if (response && response.statusCode !== 200) { - done(body); - } else { - done(null); - } - }).on('response', (response) => { - if (response.statusCode === 200) { - req.pipe(stream); - } + return new Promise((resolve, reject) => { + const req = request(requestOptions, (error, response, body) => { + if (error) { + reject(error); + } else if (response.statusCode !== 200) { + body = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status'); + reject(new HttpError(response, body, response.statusCode)); + } + }).on('response', (response) => { + if (response.statusCode === 200) { + req.pipe(stream); + resolve(req); + } + }); }); - - return req; } } From de1cbb557e8e791d448b2aa559d0ff8ac7d34099 Mon Sep 17 00:00:00 2001 From: Leon Kiefer Date: Thu, 29 Jul 2021 10:25:11 +0200 Subject: [PATCH 2/2] use typescript overloading to retain old log signature --- src/log.ts | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/log.ts b/src/log.ts index 021d2cb370..b1cdee98a8 100644 --- a/src/log.ts +++ b/src/log.ts @@ -56,8 +56,32 @@ export class Log { podName: string, containerName: string, stream: Writable, - options: LogOptions = {}, + options?: LogOptions, + ): Promise; + /** @deprecated done callback is deprecated */ + public async log( + namespace: string, + podName: string, + containerName: string, + stream: Writable, + done: (err: any) => void, + options?: LogOptions, + ): Promise; + public async log( + namespace: string, + podName: string, + containerName: string, + stream: Writable, + doneOrOptions?: ((err: any) => void) | LogOptions, + options?: LogOptions, ): Promise { + let done: (err: any) => void = () => undefined; + if (typeof doneOrOptions === 'function') { + done = doneOrOptions; + } else { + options = doneOrOptions; + } + const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`; const cluster = this.config.getCurrentCluster(); @@ -80,9 +104,13 @@ export class Log { const req = request(requestOptions, (error, response, body) => { if (error) { reject(error); + done(error); } else if (response.statusCode !== 200) { - body = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status'); - reject(new HttpError(response, body, response.statusCode)); + const deserializedBody = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status'); + reject(new HttpError(response, deserializedBody, response.statusCode)); + done(body); + } else { + done(null); } }).on('response', (response) => { if (response.statusCode === 200) {