1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/utils/get-port-from-stream.injectable.ts
Sebastian Malton d2ff35551c Remove dependency on url-parse due to side effects
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-20 10:13:07 -05:00

77 lines
2.1 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Readable } from "stream";
import { getInjectable } from "@ogre-tools/injectable";
import loggerInjectable from "../../common/logger.injectable";
export interface GetPortFromStreamArgs {
/**
* Should be case insensitive
* Must have a named matching group called `address`
*/
lineRegex: {
match: (line: string) => {
matched: boolean;
groups?: {
address?: string;
};
raw?: RegExpExecArray;
};
rawMatcher: string;
};
/**
* Called when the port is found
*/
onFind?: () => void;
/**
* Timeout for how long to wait for the port.
* Default: 15s
*/
timeout?: number;
}
export type GetPortFromStream = (stream: Readable, args: GetPortFromStreamArgs) => Promise<number>;
const getPortFromStreamInjectable = getInjectable({
id: "get-port-from-stream",
instantiate: (di): GetPortFromStream => {
const logger = di.inject(loggerInjectable);
return (stream, args) => {
const logLines: string[] = [];
return new Promise<number>((resolve, reject) => {
const handler = (data: unknown) => {
const logItem = String(data);
const match = args.lineRegex.match(logItem);
logLines.push(logItem);
if (match.matched) {
// use unknown protocol so that there is no default port
const addr = new URL(`s://${match.groups?.address?.trim()}`);
args.onFind?.();
stream.off("data", handler);
clearTimeout(timeoutID);
resolve(+addr.port);
}
};
const timeoutID = setTimeout(() => {
stream.off("data", handler);
logger.warn(`[getPortFrom]: failed to retrieve port via ${args.lineRegex.rawMatcher}`, logLines);
reject(new Error("failed to retrieve port from stream"));
}, args.timeout ?? 15000);
stream.on("data", handler);
});
};
},
});
export default getPortFromStreamInjectable;