-
Notifications
You must be signed in to change notification settings - Fork 0
Add verbose CLI debug logging #13
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f833cef
feat(scrape): add retry backoff and root timeout options
paulius-krutkis-dcd 5edb0d3
feat(scrape): add verbose stderr diagnostics
paulius-krutkis-dcd 005f3b4
refactor(auth): reorder properties in RootOptions interface
paulius-krutkis-dcd 47f7748
refactor(cli): remove timeout and max-retries options from CLI
paulius-krutkis-dcd 85ec249
refactor(tests): simplify command argument formatting in target scrap…
paulius-krutkis-dcd 2ba30f4
feat(cli): introduce global options and verbose logging for CLI commands
paulius-krutkis-dcd 09a76fd
refactor(auth): remove requireAuthToken function and related tests
paulius-krutkis-dcd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { Command } from "commander"; | ||
|
|
||
| export interface RootOptions { | ||
| token?: string; | ||
| verbose?: boolean; | ||
| } | ||
|
|
||
| export function getRootOpts(command: Command): RootOptions { | ||
| let current: Command = command; | ||
|
|
||
| while (current.parent) { | ||
| current = current.parent; | ||
| } | ||
|
|
||
| return current.opts() as RootOptions; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export function verboseLog(enabled: boolean, message: string): void { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
|
|
||
| process.stderr.write(`[verbose] ${message}\n`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const SENSITIVE_QUERY_PARAM_KEYS = new Set([ | ||
| "auth", | ||
| "authorization", | ||
| "apikey", | ||
| "api_key", | ||
| "key", | ||
| "password", | ||
| "secret", | ||
| "token", | ||
| ]); | ||
|
|
||
| function sanitizeUrlForLog(value: string): string { | ||
| try { | ||
| const parsed = new URL(value); | ||
| for (const key of parsed.searchParams.keys()) { | ||
| if (SENSITIVE_QUERY_PARAM_KEYS.has(key.toLowerCase())) { | ||
| parsed.searchParams.set(key, "<redacted>"); | ||
| } | ||
| } | ||
| return parsed.toString(); | ||
| } catch { | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| export function formatScrapeRequestLog(body: Record<string, unknown>): string { | ||
| const target = typeof body.target === "string" ? body.target : "unknown"; | ||
| const url = typeof body.url === "string" ? sanitizeUrlForLog(body.url) : null; | ||
| if (url !== null) { | ||
| return `request target=${target} url=${url}`; | ||
| } | ||
|
|
||
| const query = typeof body.query === "string" ? body.query : null; | ||
| if (query !== null) { | ||
| return `request target=${target} query=${query}`; | ||
| } | ||
|
|
||
| return `request target=${target}`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import type { DecodoSchema, ScrapeRequest } from "@decodo/sdk-ts"; | ||
| import type { Command } from "commander"; | ||
| import { getRootOpts } from "../../auth/services/global-opts.js"; | ||
| import { requireAuthToken } from "../../auth/services/resolve-token.js"; | ||
| import { AuthRequiredError } from "../../auth/errors/auth-required-error.js"; | ||
| import { resolveAuthToken } from "../../auth/services/resolve-token.js"; | ||
| import { getRootOpts } from "../../cli/services/global-opts.js"; | ||
| import { verboseLog } from "../../cli/services/verbose-log.js"; | ||
| import { writeScrapeResponse } from "../../output/services/write-scrape-response.js"; | ||
| import type { OutputOptions } from "../../output/types/output-options.js"; | ||
| import type { WriteScrapeResponseContext } from "../../output/types/write-scrape-response.js"; | ||
|
|
@@ -12,19 +14,23 @@ import type { | |
| } from "../types/run-target-scrape.js"; | ||
| import { createDecodoClient } from "./client.js"; | ||
| import { buildScrapeBody, getTargetCommandConfig } from "./command-builder.js"; | ||
| import { formatScrapeRequestLog } from "./format-scrape-request-log.js"; | ||
|
|
||
| export async function executeScrape( | ||
| token: string, | ||
| schema: DecodoSchema, | ||
| body: Record<string, unknown>, | ||
| options: Record<string, unknown>, | ||
| outputContext?: Partial<WriteScrapeResponseContext>, | ||
| input?: string | ||
| input?: string, | ||
| verbose = false | ||
| ): Promise<void> { | ||
| const client = createDecodoClient(token, schema); | ||
| const startedAt = Date.now(); | ||
| const response = await client.webScrapingApi.scrape( | ||
| body as unknown as ScrapeRequest | ||
| ); | ||
| verboseLog(verbose, `response latency_ms=${Date.now() - startedAt}`); | ||
|
|
||
| writeScrapeResponse(response, { | ||
| options: options as OutputOptions, | ||
|
|
@@ -51,12 +57,27 @@ export function createTargetAction( | |
| command: Command | ||
| ): Promise<void> => { | ||
| const rootOpts = getRootOpts(command); | ||
| const verbose = rootOpts.verbose === true; | ||
|
|
||
| try { | ||
| const token = await requireAuthToken({ token: rootOpts.token }); | ||
| const auth = await resolveAuthToken({ token: rootOpts.token }); | ||
| verboseLog(verbose, `auth source=${auth.source}`); | ||
| if (!auth.token) { | ||
| throw new AuthRequiredError(); | ||
| } | ||
|
|
||
| const body = resolveBody(input, options); | ||
| verboseLog(verbose, formatScrapeRequestLog(body)); | ||
| const outputContext = getOutputContext?.(input, options); | ||
| await executeScrape(token, schema, body, options, outputContext, input); | ||
| await executeScrape( | ||
|
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. nit: consider moving params to an object (not necessarily this PR) |
||
| auth.token, | ||
| schema, | ||
| body, | ||
| options, | ||
| outputContext, | ||
| input, | ||
| verbose | ||
| ); | ||
| } catch (err) { | ||
| handleCliError(err, { fallbackMessage: "Scrape failed." }); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { formatScrapeRequestLog } from "../../../src/scrape/services/format-scrape-request-log.js"; | ||
|
|
||
| describe("formatScrapeRequestLog", () => { | ||
| it("formats query-based requests", () => { | ||
| expect( | ||
| formatScrapeRequestLog({ target: "google_search", query: "coffee" }) | ||
| ).toBe("request target=google_search query=coffee"); | ||
| }); | ||
|
|
||
| it("redacts sensitive URL query params", () => { | ||
| expect( | ||
| formatScrapeRequestLog({ | ||
| target: "universal", | ||
| url: "https://example.com?token=secret&page=1", | ||
| }) | ||
| ).toBe( | ||
| "request target=universal url=https://example.com/?token=%3Credacted%3E&page=1" | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to target only when body has no url or query", () => { | ||
| expect(formatScrapeRequestLog({ target: "amazon_product" })).toBe( | ||
| "request target=amazon_product" | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
id suggest flipping this inside out to check if key.toLowerCase() has any of the SENSITIVE_QUERY_PARAM_KEYS. This would catch cases where key may be
access_token.thank u claude