UNPKG

@cyclonedx/cdxgen

Version:

Creates CycloneDX Software Bill of Materials (SBOM) from source or container image

100 lines 4.38 kB
/** * A tiny, `got`-compatible HTTP client built on top of undici. * * cdxgen historically relied on the `got` library. `got` keeps a per-request * HTTP cache backed by an EventEmitter which, during large `--deep` scans that * issue thousands of parallel license/metadata lookups, leaks "error" listeners * and floods the console with `MaxListenersExceededWarning` messages. undici * uses a pooled dispatcher and does not exhibit this behaviour. * * This module intentionally implements only the subset of the `got` surface * that cdxgen consumes: * * - Callable form: `client(url, options)` and the verb helpers `client.get`, * `client.post`, `client.put` and `client.head`. * - `client.extend(defaults)` to derive a new client with merged defaults. * - Request options: `method`, `headers`, `body`, `json`, `responseType` * (`"json"` | `"buffer"` | `"text"`), `throwHttpErrors`, `followRedirect`, * `timeout` (number of milliseconds or a `got`-style phase object), `retry` * (accepted for API compatibility; no automatic retries are performed), * `https.rejectUnauthorized` and `context`. * - `beforeRequest`, `afterResponse` and `beforeError` hooks with the same * calling conventions cdxgen uses today. These hooks continue to power * cdxgen's dry-run enforcement, host allow-listing, network-activity * recording and HTTP trace logging. * - Automatic response decompression (`gzip`, `deflate`, `br`) driven by the * `Content-Encoding` header, matching `got`'s transparent decoding. * - Proxy support via the standard `HTTP_PROXY`/`HTTPS_PROXY`/`NO_PROXY` * environment variables (through undici's `EnvHttpProxyAgent`). * - An in-memory GET response cache (enabled by default, disabled by setting * the `CDXGEN_NO_CACHE` environment variable) that replaces the got + Keyv * cache cdxgen previously relied on. * - Response objects exposing `statusCode`, `headers`, `body`, `rawBody`, * `url` and `request.options`. * - A lazily-resolved `.json()` method on the returned promise, mirroring * `got`'s `client(url).json()` usage. * * @module httpClient */ /** * Error thrown when the server responds with a non 2xx/3xx status code and * `throwHttpErrors` has not been disabled. Shaped like `got`'s `HTTPError` so * that existing `error.response.statusCode` and `error.options.context` checks * keep working. */ export declare class HTTPError extends Error { response: Object; options: Object; code: string; /** * @param {Object} response Response object produced by this client. * @param {Object} options Merged request options for the failed request. */ constructor(response: Object, options: Object); } /** * Error thrown for transport-level failures (DNS, connection reset, timeouts). * Carries the merged request `options` so `beforeError` hooks can inspect the * request context. */ export declare class RequestError extends Error { options: Object; code: any; cause: Error; /** * @param {Error} cause Underlying error thrown by undici. * @param {Object} options Merged request options for the failed request. */ constructor(cause: Error, options: Object); } /** * Resolve the default HTTP request timeout, honoring the validated * CDXGEN_HTTP_TIMEOUT_MS environment variable when set to a positive integer. * * @returns {number} Timeout in milliseconds */ export declare function getDefaultHttpTimeoutMs(): number; export declare function resolveTimeout(timeout: any): any; /** * Determine whether the in-memory HTTP response cache is disabled via the * CDXGEN_NO_CACHE environment variable. Evaluated per request so tests and * callers can toggle it at runtime. * * @returns {boolean} True when caching should be skipped. */ export declare function isCacheDisabled(): boolean; /** * Clear the in-memory HTTP response cache. Primarily useful for tests. * * @returns {void} */ export declare function clearHttpCache(): void; /** * Create a `got`-compatible HTTP client bound to the supplied defaults. * * @param {Object} [defaults] Default request options merged into every call. * @returns {Function} Callable client exposing `get`/`post`/`put`/`head`, * `extend`, `defaults` and `hooks`. */ export declare function createHttpClient(defaults?: Object): Function; //# sourceMappingURL=httpClient.d.ts.map