-
Notifications
You must be signed in to change notification settings - Fork 0
feat: replace polling log spam with progress bar #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,11 @@ const IssueCountsBySeverityObject = z.object({ | |
|
|
||
| export type IssueCountsBySeverity = z.infer<typeof IssueCountsBySeverityObject>; | ||
|
|
||
| const ScanProgressObject = z.object({ | ||
| completedEndpoints: z.number(), | ||
| totalEndpoints: z.number(), | ||
| }); | ||
|
|
||
| const ScanStatusResponseObject = z.object({ | ||
| scanId: z.string(), | ||
| label: z.string(), | ||
|
|
@@ -101,6 +106,7 @@ const ScanStatusResponseObject = z.object({ | |
| issueCountsBySeverity: IssueCountsBySeverityObject.optional(), | ||
| reportReady: z.boolean(), | ||
| error: z.string().optional(), | ||
| progress: ScanProgressObject.optional(), | ||
| }); | ||
|
|
||
| export type ScanStatus = z.infer<typeof ScanStatusResponseObject>; | ||
|
|
@@ -212,10 +218,27 @@ export interface PollScanStatusParams { | |
| onStatusUpdate?: (status: ScanStatus) => void; | ||
| } | ||
|
|
||
| function renderProgressBar( | ||
| label: string, | ||
| completed: number, | ||
| total: number, | ||
| barWidth = 30 | ||
| ): void { | ||
| const ratio = total > 0 ? completed / total : 0; | ||
| const percent = Math.round(ratio * 100); | ||
| const filled = Math.round(ratio * barWidth); | ||
| const empty = barWidth - filled; | ||
| const bar = "█".repeat(filled) + "░".repeat(empty); | ||
| const line = ` ${label} ${bar} ${percent}% (${completed}/${total} endpoints)`; | ||
|
|
||
| process.stdout.write(`\r${line}`); | ||
| } | ||
|
|
||
| export async function pollScanStatus( | ||
| params: PollScanStatusParams | ||
| ): Promise<ScanStatus> { | ||
| const pollIntervalMs = params.pollIntervalMs ?? 5000; | ||
| let progressShown = false; | ||
|
|
||
| const sleep = (ms: number): Promise<void> => | ||
| new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
|
@@ -229,23 +252,34 @@ export async function pollScanStatus( | |
|
|
||
| params.onStatusUpdate?.(status); | ||
|
|
||
| if (status.progress && status.progress.totalEndpoints > 0) { | ||
| renderProgressBar( | ||
| status.label, | ||
| status.progress.completedEndpoints, | ||
| status.progress.totalEndpoints | ||
| ); | ||
| progressShown = true; | ||
| } else if (!progressShown) { | ||
| process.stdout.write( | ||
| `\r ${status.label} status: ${status.status}...` | ||
| ); | ||
| } | ||
|
|
||
| if (status.status === "failed") { | ||
| if (progressShown) process.stdout.write("\n"); | ||
| throw new Error(`Pentest failed: ${status.errorMessage}`); | ||
| } | ||
|
|
||
| if (status.status === "completed") { | ||
| if (progressShown) process.stdout.write("\n"); | ||
| return status; | ||
| } | ||
|
|
||
| if (status.status === "paused") { | ||
| if (progressShown) process.stdout.write("\n"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline when fallback status line was writtenMedium Severity When the scan completes without ever receiving Additional Locations (1)There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No terminal cleanup when getScanStatus throws mid-pollingLow Severity If |
||
| throw new Error("Pentest was paused"); | ||
| } | ||
|
|
||
| console.log( | ||
| `Pentest ${status.label} status: ${status.status}. Polling again in ${ | ||
| pollIntervalMs / 1000 | ||
| }s...` | ||
| ); | ||
| await sleep(pollIntervalMs); | ||
| } | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Progress bar crashes when completed exceeds total endpoints
Medium Severity
If the server returns
completedEndpoints > totalEndpoints(a plausible race condition or data inconsistency),filledexceedsbarWidth, makingemptynegative. Calling"░".repeat(negative)throws aRangeError, crashing the entire polling loop. Clampingfilledto[0, barWidth]would prevent this.