1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

getPortFrom should log the lines it retrived if it fails to get a port (#3395)

This commit is contained in:
Sebastian Malton 2021-07-15 14:19:33 -04:00 committed by GitHub
parent c3823b9d27
commit ea4a5a4c57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,7 @@
import type { Readable } from "stream";
import URLParse from "url-parse";
import logger from "../logger";
interface GetPortArgs {
/**
@ -47,11 +48,15 @@ interface GetPortArgs {
* @returns A Promise for port number
*/
export function getPortFrom(stream: Readable, args: GetPortArgs): Promise<number> {
const logLines: string[] = [];
return new Promise<number>((resolve, reject) => {
const handler = (data: any) => {
const logItem: string = data.toString();
const match = logItem.match(args.lineRegex);
logLines.push(logItem);
if (match) {
// use unknown protocol so that there is no default port
const addr = new URLParse(`s://${match.groups.address.trim()}`);
@ -64,6 +69,7 @@ export function getPortFrom(stream: Readable, args: GetPortArgs): Promise<number
};
const timeoutID = setTimeout(() => {
stream.off("data", handler);
logger.warn(`[getPortFrom]: failed to retrieve port via ${args.lineRegex.toString()}: ${logLines}`);
reject(new Error("failed to retrieve port from stream"));
}, args.timeout ?? 5000);