### Install TlsClientWrapper Source: https://github.com/demonmartin/tlsclient/blob/main/readme.md Install the library using npm or pnpm. ```bash npm install tlsclientwrapper # or pnpm add tlsclientwrapper ``` -------------------------------- ### Initialize and Make Requests with tlsclientwrapper Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/00-START-HERE.md This snippet demonstrates how to initialize the ModuleClient and SessionClient, configure TLS fingerprinting and request options, make a GET request, and clean up resources. Ensure you have the 'tlsclientwrapper' library installed. ```typescript import { ModuleClient, SessionClient } from 'tlsclientwrapper'; // Initialize const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'chrome_146', // Emulate Chrome timeoutSeconds: 30, retryMaxCount: 3, }); // Make requests const response = await session.get('https://api.example.com/data'); console.log('Status:', response.status); console.log('Body:', response.body); // Clean up await session.destroySession(); await moduleClient.terminate(); ``` -------------------------------- ### Basic Usage with TypeScript Source: https://github.com/demonmartin/tlsclient/blob/main/readme.md Demonstrates the fundamental steps to create a ModuleClient, a SessionClient, and make a GET request. Ensure to clean up resources by terminating the clients. ```typescript import { ModuleClient, SessionClient } from 'tlsclientwrapper'; // 1. Create the worker pool manager const moduleClient = new ModuleClient(); // 2. Create a session for making requests const session = new SessionClient(moduleClient); // 3. Make requests const response = await session.get('https://example.com'); // 4. Clean up await session.destroySession(); await moduleClient.terminate(); ``` -------------------------------- ### Module Initialization Examples Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Demonstrates various ways to initialize ModuleClient, from default settings to custom thread counts, pre-downloaded libraries, and specific download locations. ```typescript import { ModuleClient } from 'tlsclientwrapper'; // Default configuration const moduleClient = new ModuleClient(); ``` ```typescript import { ModuleClient } from 'tlsclientwrapper'; // With custom thread count for high concurrency const moduleClient = new ModuleClient({ maxThreads: 32, }); ``` ```typescript import { ModuleClient } from 'tlsclientwrapper'; // With pre-downloaded library const moduleClient = new ModuleClient({ customLibraryPath: '/opt/tls-library/tls-client-xgo-1.14.0-linux-amd64.so', maxThreads: 16, }); ``` ```typescript import { ModuleClient } from 'tlsclientwrapper'; // With custom download location const moduleClient = new ModuleClient({ customLibraryDownloadPath: '/data/tls-libraries', maxThreads: 24, }); ``` -------------------------------- ### Initialize ModuleClient and SessionClient Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/README.md Initializes the ModuleClient and a SessionClient instance. Use this to start using the library's functionalities. ```typescript import { ModuleClient, SessionClient } from 'tlsclientwrapper'; const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient); ``` -------------------------------- ### Perform a Simple GET Request Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Initiates a GET request to a specified URL and logs the response status and body. Ensures session and module client are properly terminated. ```typescript import { ModuleClient, SessionClient } from 'tlsclientwrapper'; const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient); try { const response = await session.get('https://example.com'); console.log('Status:', response.status); console.log('Body:', response.body); } finally { await session.destroySession(); await moduleClient.terminate(); } ``` -------------------------------- ### Build Configuration with tsup Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Configure the build process using tsup. This example shows entry points, output formats, target ECMAScript version, and options for generating type definitions. ```typescript // tsup.config.ts { entry: ['src/index.ts'], format: ['esm', 'cjs'], target: 'ES2022', splitting: false, shims: true, dts: true, } ``` -------------------------------- ### Perform Basic Authentication Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Configures a session with Basic Authentication headers by encoding username and password in Base64. Then, it makes a GET request to a protected resource. ```typescript const session = new SessionClient(moduleClient, { defaultHeaders: { 'Authorization': 'Basic ' + Buffer.from('username:password').toString('base64'), }, }); const response = await session.get('https://api.example.com/protected'); ``` -------------------------------- ### Make a GET Request Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/README.md Performs an HTTP GET request to a specified URL and logs the response status and body. This is a basic usage pattern for fetching data. ```typescript const response = await session.get('https://example.com'); console.log(response.status, response.body); ``` -------------------------------- ### get Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Sends a GET request to the specified URL. It returns a Promise that resolves to TlsClientResponse containing status, headers, body, and cookies. ```APIDOC ## get ### Description Sends a GET request to the specified URL. It returns a Promise that resolves to TlsClientResponse containing status, headers, body, and cookies. ### Method GET ### Endpoint [URL] ### Parameters #### Path Parameters - **url** (URL | string) - Required - Target URL as string or URL object #### Query Parameters - **options** (Partial) - Optional - Request-specific options that override defaults ### Response #### Success Response (200) - **status** (number) - The HTTP status code of the response. - **headers** (object) - The response headers. - **body** (any) - The response body. - **cookies** (object) - The response cookies. ### Request Example ```typescript const response = await session.get('https://example.com'); console.log(response.status, response.body); // With custom headers const response = await session.get('https://api.example.com/data', { headers: { Authorization: 'Bearer token' }, }); ``` ``` -------------------------------- ### Worker Pool Architecture (Piscina) Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Describes the worker pool setup using Piscina, including thread management and queueing. ```text ModuleClient (Main Thread) ├─ Worker Pool Manager (Piscina) │ ├─ Min Threads: 1 │ ├─ Max Threads: cpuCount * 2 (configurable) │ ├─ Idle Timeout: 30 seconds │ └─ Queue: Unlimited capacity │ └─ Worker Threads (N instances of worker.ts) ├─ Thread 1: worker.ts → Koffi → TLS Library ├─ Thread 2: worker.ts → Koffi → TLS Library ├─ Thread N: worker.ts → Koffi → TLS Library └─ Threads auto-cleanup after idleTimeout ``` -------------------------------- ### Make a Request with Custom Headers Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Includes custom headers such as Authorization, Accept, and a custom header in the GET request. ```typescript const response = await session.get('https://api.example.com/data', { headers: { 'Authorization': 'Bearer your-api-token', 'Accept': 'application/json', 'X-Custom-Header': 'custom-value', }, }); ``` -------------------------------- ### Send GET Request with SessionClient Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Perform a GET request to a specified URL using the SessionClient. Custom headers can be provided via options. ```typescript const response = await session.get('https://example.com'); console.log(response.status, response.body); // With custom headers const response = await session.get('https://api.example.com/data', { headers: { Authorization: 'Bearer token' }, }); ``` -------------------------------- ### Handling TLS Certificate Pinning Failures Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/errors.md This example demonstrates how to configure certificate pinning and catch errors when the server's certificate does not match the pinned public key hash. Incorrect pins or certificate rotation can cause this. ```typescript const session = new SessionClient(moduleClient, { certificatePinningHosts: { 'api.example.com': ['sha256/AAAAAAA=='], // Wrong hash }, }); try { const response = await session.get('https://api.example.com'); } catch (error) { console.error('Pin verification failed:', error); } ``` ```bash openssl s_client -connect api.example.com:443 < /dev/null \ | openssl x509 -pubkey -noout \ | openssl pkey -pubin -outform DER \ | openssl dgst -sha256 -binary \ | openssl enc -base64 ``` -------------------------------- ### Reuse Session for Multiple Requests Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Demonstrates maintaining session state across multiple requests, including GET and POST, to the same API. Ensures the session is destroyed upon completion. ```typescript const session = new SessionClient(moduleClient); // Multiple requests reuse the same session state const response1 = await session.get('https://api.example.com/page1'); const response2 = await session.get('https://api.example.com/page2'); const response3 = await session.post('https://api.example.com/action', { action: 'click' }); // All requests maintain cookies and session state from the server console.log('All requests completed'); await session.destroySession(); ``` -------------------------------- ### Structured Error Logging Strategy Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/errors.md Implements a structured logging approach for production applications, distinguishing between informational messages and errors. This example shows how to log request success and failure details, including error names, messages, and stacks. ```typescript import { SessionClient, ModuleClient } from 'tlsclientwrapper'; const logger = { error: (msg: string, error: unknown) => { console.error(JSON.stringify({ level: 'error', message: msg, error: error instanceof Error ? { name: error.name, message: error.message, stack: error.stack, } : error, timestamp: new Date().toISOString(), })); }, info: (msg: string, data?: unknown) => { console.log(JSON.stringify({ level: 'info', message: msg, data, timestamp: new Date().toISOString(), })); }, }; const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient); try { const response = await session.get('https://api.example.com'); logger.info('Request succeeded', { status: response.status, url: response.target, }); } catch (error) { logger.error('Request failed', error); } finally { await session.destroySession(); await moduleClient.terminate(); } ``` -------------------------------- ### Log TLS Client Requests and Responses Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Sends a request (GET or POST) and logs its details, including method, URL, status, duration, and retries, or logs an error if the request fails. This pattern helps in monitoring and debugging application requests. ```typescript async function loggedRequest( session: SessionClient, method: 'get' | 'post', url: string, data?: any ) { const startTime = performance.now(); try { const response = method === 'get' ? await session.get(url) : await session.post(url, data); const duration = performance.now() - startTime; console.log(JSON.stringify({ level: 'info', type: 'request', method, url, status: response.status, durationMs: duration, retries: response.retryCount, timestamp: new Date().toISOString(), })); return response; } catch (error) { const duration = performance.now() - startTime; console.log(JSON.stringify({ level: 'error', type: 'request', method, url, error: error instanceof Error ? error.message : String(error), durationMs: duration, timestamp: new Date().toISOString(), })); throw error; } } await loggedRequest(session, 'get', 'https://api.example.com/data'); ``` -------------------------------- ### Authenticate Using Session Cookies Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Logs in to a service via a POST request, which sets a session cookie. Subsequent GET requests to the same domain automatically include this cookie for authentication. ```typescript // First request: login const loginResponse = await session.post('https://example.com/login', { email: 'user@example.com', password: 'password123', }); // Server sets session cookie in response console.log('Login cookies:', loginResponse.cookies); // Subsequent requests automatically include the session cookie const response = await session.get('https://example.com/dashboard'); // Cookie is automatically sent by the session ``` -------------------------------- ### Managing Multiple Sessions with Customization Source: https://github.com/demonmartin/tlsclient/blob/main/readme.md Shows how to configure the ModuleClient with a specific thread count and create multiple SessionClients with distinct default headers for different tasks. It also demonstrates concurrent request execution using Promise.all. ```javascript const moduleClient = new ModuleClient({ maxThreads: 8, // Optimize thread count (more Threads = more concurrent Requests, test whats the best for you) }); // Create multiple sessions for different purposes const loginSession = new SessionClient(moduleClient, { defaultHeaders: { 'User-Agent': 'Chrome/146.0.0.0' }, }); const apiSession = new SessionClient(moduleClient, { defaultHeaders: { Authorization: 'Bearer token' }, }); // Use sessions concurrently await Promise.all([ loginSession.post('https://example.com/login', credentials), apiSession.get('https://example.com/api/data'), ]); // Clean up await loginSession.destroySession(); await apiSession.destroySession(); await moduleClient.terminate(); ``` -------------------------------- ### Get Cookies from Session Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Retrieves cookies for a specific session and URL. Ensure sessionId and url are provided. ```typescript const sessionId = session.getSession(); const cookieResponse = await session.getCookiesFromSession(sessionId, 'https://example.com'); if (cookieResponse.cookies) { console.log('Cookies:', cookieResponse.cookies); } ``` -------------------------------- ### Correct ModuleClient Initialization for SessionClient Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/errors.md Demonstrates the correct way to initialize SessionClient by first creating a ModuleClient instance and then passing it as an argument. ```typescript const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient); // Pass moduleClient ``` -------------------------------- ### Configure Custom Library Path or Download Path Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Initialize `ModuleClient` with `customLibraryPath` to use a locally stored TLS library or `customLibraryDownloadPath` to specify a custom directory for automatic downloads. Call `open()` to ensure the library is ready. ```typescript // If you have the TLS library downloaded locally or from a custom source const moduleClient = new ModuleClient({ customLibraryPath: '/opt/custom/tls-client-xgo-1.14.0-linux-amd64.so', }); // If you want automatic downloads to a specific directory const moduleClient = new ModuleClient({ customLibraryDownloadPath: '/data/libraries', maxThreads: 12, }); await moduleClient.open(); ``` -------------------------------- ### Proxy with Authentication Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Set up an HTTP proxy that requires authentication by including username and password in the proxy URL. Ensure credentials are kept secure. ```typescript const session = new SessionClient(moduleClient, { proxyUrl: 'http://username:password@proxy.example.com:8080', }); const response = await session.get('https://api.example.com/data'); ``` -------------------------------- ### CertCompressionAlgorithm Type Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/types.md Specifies supported certificate compression algorithms for the TLS handshake. Helps optimize data transfer during connection setup. ```typescript type CertCompressionAlgorithm = 'zlib' | 'brotli' | 'zstd' ``` -------------------------------- ### Instantiate ModuleClient with Custom Library Path Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Provide a direct path to a pre-downloaded TLS library. This bypasses the automatic download process, useful for environments with restricted network access or specific deployment needs. ```typescript import { ModuleClient } from 'tlsclientwrapper'; // With custom library path const moduleClient = new ModuleClient({ customLibraryPath: '/opt/tls-client/tls-client-xgo-1.14.0-linux-amd64.so', }); ``` -------------------------------- ### Override Request Timeout Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Demonstrates how to override the default session timeout for a single GET request. Useful for handling requests to slower endpoints. ```typescript const session = new SessionClient(moduleClient, { timeoutSeconds: 60, retryMaxCount: 3, }); // Override timeout for single request const response = await session.get('https://slow-api.example.com', { timeoutSeconds: 120, }); ``` -------------------------------- ### Instantiate ModuleClient with Default Configuration Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Use the default configuration to create a ModuleClient instance. This will use default paths and thread counts. ```typescript import { ModuleClient } from 'tlsclientwrapper'; // Default configuration const moduleClient = new ModuleClient(); ``` -------------------------------- ### Configuring Request Options and Retry Logic Source: https://github.com/demonmartin/tlsclient/blob/main/readme.md Illustrates how to configure a SessionClient with various options including TLS identification, retry settings, network timeouts, proxy configuration, and default headers/cookies. ```javascript const session = new SessionClient(moduleClient, { // TLS Configuration tlsClientIdentifier: 'chrome_146', // Retry Configuration retryIsEnabled: true, retryMaxCount: 3, retryStatusCodes: [429, 503, 504], // Network Configuration timeoutSeconds: 30, proxyUrl: 'http://proxy:8080', // Default Headers & Cookies defaultHeaders: { 'User-Agent': 'Custom/1.0', }, defaultCookies: [ { domain: 'example.com', name: 'session', value: 'xyz', }, ], }); ``` -------------------------------- ### Instantiate ModuleClient with Custom Download Directory Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Specify a custom directory for downloading the TLS library. This is helpful for managing library storage in specific locations or when the default temporary directory is not suitable. ```typescript import { ModuleClient } from 'tlsclientwrapper'; // With custom download directory const moduleClient = new ModuleClient({ customLibraryDownloadPath: '/var/lib/tlsclient', }); ``` -------------------------------- ### Override Request Cookies Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Illustrates how to set specific cookies for an individual GET request, overriding session-level cookies. Useful for managing per-request state. ```typescript const session = new SessionClient(moduleClient, { timeoutSeconds: 60, retryMaxCount: 3, }); // Override cookies for single request const response = await session.get('https://example.com', { requestCookies: [{ name: 'override', value: 'test', domain: 'example.com', path: '/', expires: Math.floor(Date.now() / 1000) + 3600, }], }); ``` -------------------------------- ### ModuleClientOptions Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/types.md Configuration options for initializing the ModuleClient. These options allow customization of library paths and thread management for worker pools. ```APIDOC ## ModuleClientOptions ### Description Configuration options for ModuleClient initialization (defined in `src/utils/client.ts`). ### Type Definition ```typescript interface ModuleClientOptions { customLibraryPath?: string; customLibraryDownloadPath?: string; maxThreads?: number; } ``` ### Fields - **customLibraryPath** (string) - Path to pre-downloaded TLS library - **customLibraryDownloadPath** (string) - Custom directory for library downloads (Default: OS temp) - **maxThreads** (number) - Maximum worker pool threads (Default: cpuCount × 2) ### Used By - ModuleClient constructor ``` -------------------------------- ### Send a Request with Cookies Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Attaches specific cookies to a GET request, including domain and path information. The expiration is set to 24 hours from the current time. ```typescript const response = await session.get('https://example.com/protected', { requestCookies: [ { name: 'session_id', value: 'abc123xyz789', domain: 'example.com', path: '/', expires: Math.floor(Date.now() / 1000) + 86400, }, ], }); ``` -------------------------------- ### Disable Retry for Single Request Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Demonstrates disabling the retry mechanism for a specific GET request. Useful when retrying a request is undesirable or could cause issues. ```typescript const session = new SessionClient(moduleClient, { timeoutSeconds: 60, retryMaxCount: 3, }); // Disable retry for single request const response = await session.get('https://api.example.com', { retryIsEnabled: false, }); ``` -------------------------------- ### Configure Corporate Environment with Proxy and Local Address Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Set up a session client for a corporate environment, specifying a proxy, custom user agent, and a local IP address. Includes disabling IPv6 for compatibility with internal networks. ```typescript const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'firefox_147', proxyUrl: 'http://corporate-proxy.internal:3128', defaultHeaders: { 'User-Agent': 'CompanyBot/1.0 (+https://company.internal/bot)', 'Accept-Language': 'en-US,en;q=0.9', }, disableIPV6: true, localAddress: '10.0.0.100', insecureSkipVerify: false, }); const response = await session.get('https://internal-api.company.com/data'); ``` -------------------------------- ### Use Pre-Downloaded TLS Library Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Configures the ModuleClient to use a pre-downloaded TLS library from a specified direct path, bypassing the automatic download process. Useful when the library is already deployed. ```typescript const moduleClient = new ModuleClient({ customLibraryPath: '/usr/local/lib/libtlsclient.so', }); // No download occurs; path is used directly ``` -------------------------------- ### Get Session ID from SessionClient Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Retrieve the unique session ID associated with the current SessionClient instance. This ID is typically a UUID and can be used for logging or debugging purposes. ```typescript const sessionId = session.getSession(); console.log('Current session:', sessionId); ``` -------------------------------- ### SessionClient Constructor Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Instantiate SessionClient, extending TlsClientDefaultOptions. Options set here become defaults for all requests within the session. ```typescript new SessionClient(moduleClient: ModuleClient, options?: TlsClientDefaultOptions) ``` -------------------------------- ### Initialize SessionClient Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Instantiate SessionClient with a ModuleClient and optional session-specific options. Defaults are applied if options are not provided. ```typescript import { ModuleClient, SessionClient } from 'tlsclientwrapper'; const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'firefox_147', timeoutSeconds: 30, retryMaxCount: 2, }); ``` -------------------------------- ### Get Worker Pool Statistics Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Retrieve current statistics of the worker pool, including utilization, completed tasks, waiting tasks, and total threads. This is useful for monitoring and debugging. ```typescript const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient); // Make some requests await session.get('https://example.com/1'); await session.get('https://example.com/2'); // Check pool stats const stats = moduleClient.getPoolStats(); if (stats) { console.log('Active threads:', stats.utilization); console.log('Tasks completed:', stats.completed); console.log('Tasks waiting:', stats.waiting); console.log('Total threads:', stats.threads); } // Monitor pool over time setInterval(() => { const stats = moduleClient.getPoolStats(); if (stats) { console.log(`[${new Date().toISOString()}] Pool: ${stats.utilization}/${stats.threads} threads active, ${stats.waiting} tasks queued`); } }, 5000); ``` -------------------------------- ### Configure Mobile Browser Emulation with Retries Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Set up a session client to emulate a mobile browser, including custom TLS identifiers, timeouts, and retry logic for specific HTTP status codes. Useful for mimicking mobile client behavior and handling transient network issues. ```typescript const moduleClient = new ModuleClient({ maxThreads: 8 }); const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'safari_ios_26_0', timeoutSeconds: 45, retryMaxCount: 5, retryStatusCodes: [408, 429, 500, 502, 503, 504], defaultHeaders: { 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)', }, }); const response = await session.get('https://api.example.com/data'); ``` -------------------------------- ### Module Organization Structure Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Shows the directory structure of the tlsclientwrapper project, highlighting key files and their roles. ```tree tlsclientwrapper/ ├── src/ │ ├── index.ts # Public API: SessionClient, types, interfaces │ ├── utils/ │ │ ├── client.ts # ModuleClient: worker pool management │ │ ├── path.ts # TlsDependency: platform detection and library downloading │ │ └── worker.ts # Worker thread handler: Koffi FFI bindings to TLS library │ └── ... (compiled output in dist/) ├── package.json └── tsconfig.json ``` -------------------------------- ### Configure Proxy with Certificate Pinning Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Set up a session client to use a proxy server and enforce certificate pinning for enhanced security. This is crucial when connecting to services that require specific certificate validation. ```typescript const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'chrome_146', proxyUrl: 'http://proxy.company.com:8080', isRotatingProxy: false, certificatePinningHosts: { 'secure.example.com': ['sha256/AAAAAAAAAAAAAAAA==', 'sha256/BBBBBBBBBBBBBBBB=='], }, }); const response = await session.get('https://secure.example.com/api'); ``` -------------------------------- ### options Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Sends an OPTIONS request to the specified URL with custom options. ```APIDOC ## options ### Description Sends an OPTIONS request to the specified URL with custom options. ### Method OPTIONS ### Endpoint `url` (URL | string) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None #### Options - **options** (Partial) - Optional - Request-specific options ### Response #### Success Response - **TlsClientResponse** - Resolves to TlsClientResponse ### Request Example ```typescript const response = await session.options('https://api.example.com'); console.log(response.headers); ``` ``` -------------------------------- ### Configure OkHttp 4 Android 13 TLS Client Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Sets up a session client to emulate OkHttp 4 on Android 13. Useful for mimicking specific Android client behavior. ```typescript const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'okhttp4_android_13', timeoutSeconds: 30, defaultHeaders: { 'User-Agent': 'okhttp/4.x.x (compatible)', }, disableHttp3: true, forceHttp1: false, }); const response = await session.post('https://api.example.com/login', { username: 'user', password: 'pass', }); ``` -------------------------------- ### ModuleClient Constructor Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Initializes the ModuleClient, managing the TLS library and worker pool. It can be configured with options for library path and worker pool size. ```APIDOC ## ModuleClient Constructor ### Description Initializes the ModuleClient, managing the TLS library and worker pool. It can be configured with options for library path and worker pool size. ### Parameters #### Options - **options** (ModuleClientOptions) - Optional - Configuration for library path and worker pool. - **customLibraryPath** (string) - Optional - Path to a pre-downloaded TLS library. - **customLibraryDownloadPath** (string) - Optional - Custom directory for TLS library download. - **maxThreads** (number) - Optional - Maximum number of worker threads in the pool. ### Behavior - Validates library path availability. - Downloads the library on the first `open()` call. - Defaults `maxThreads` to `Math.max(os.cpus().length * 2, 1)` if not specified. - Throws an error if the TLS library path is not available. ### Example ```typescript import { ModuleClient } from 'tlsclientwrapper'; // Default configuration const moduleClient = new ModuleClient(); // With custom thread count const moduleClient = new ModuleClient({ maxThreads: 16, }); // With custom library path const moduleClient = new ModuleClient({ customLibraryPath: '/opt/tls-client/tls-client-xgo-1.14.0-linux-amd64.so', }); // With custom download directory const moduleClient = new ModuleClient({ customLibraryDownloadPath: '/var/lib/tlsclient', }); ``` ``` -------------------------------- ### Handle TLS Library Path Not Available Error Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/errors.md Catch errors during ModuleClient initialization when the TLS library path cannot be determined. This typically occurs if download paths are invalid or inaccessible. ```typescript try { const moduleClient = new ModuleClient({ customLibraryDownloadPath: '/invalid/nonexistent/path', }); } catch (error) { console.error('Failed to initialize ModuleClient:', error.message); // Output: "TLS library path not available" } ``` -------------------------------- ### Request Flow Diagram Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Illustrates the step-by-step data flow for a request, from application code to the TLS client library and back. ```text Application Code ↓ SessionClient.get/post/put/patch/delete/head/options() ↓ combine options (defaults + request-specific) ↓ convertBody (string/object/number/boolean → string) ↓ retryRequest() ├─ sendRequest() │ └─ worker pool .run() │ ↓ │ Worker Thread │ ├─ handler() in worker.ts │ ├─ Koffi function call (request, getCookiesFromSession, etc.) │ └─ TLS Client Library (Go binary) │ ├─ TLS Handshake │ ├─ HTTP Request │ └─ Response Processing │ ↓ │ Return JSON response string ├─ Check response.status against retryStatusCodes ├─ If retry needed and retryCount < retryMaxCount: repeat sendRequest() └─ Return final response ↓ TlsClientResponse ├─ status: number ├─ body: string ├─ headers: Record ├─ cookies: Record ├─ usedProtocol: string └─ retryCount: number ``` -------------------------------- ### Production TLS Client Configuration Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Sets up a long-lived ModuleClient and per-domain SessionClient instances for production use. Includes graceful shutdown handling. ```typescript // Single, long-lived ModuleClient for application const moduleClient = new ModuleClient({ maxThreads: Math.min(32, os.cpus().length * 4), }); // Per-domain SessionClient instances const apiSession = new SessionClient(moduleClient, { tlsClientIdentifier: 'chrome_146', timeoutSeconds: 30, retryMaxCount: 2, retryStatusCodes: [429, 503, 504], defaultHeaders: { 'User-Agent': 'MyApp/1.0', 'Accept': 'application/json', }, }); // Graceful shutdown process.on('SIGTERM', async () => { await apiSession.destroySession(); await moduleClient.terminate(); process.exit(0); }); ``` -------------------------------- ### Session Lifecycle Flow Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Details the lifecycle of a session, from ModuleClient initialization to request processing and final cleanup. ```text ModuleClient Initialization ├─ Constructor: Set configuration, validate paths ├─ open(): Download library if needed, initialize worker pool └─ Worker Pool Ready ↓ SessionClient Creation (per session) ├─ Constructor: Generate sessionId, merge default options ├─ Register finalization handler (for GC cleanup) └─ Session Ready for Requests ↓ Request Methods (multiple requests reuse same session) ├─ Each request: │ ├─ Combine session defaults with request options │ ├─ Execute via worker pool │ └─ Return response ↓ Session Cleanup ├─ destroySession(): Tell TLS library to destroy session state └─ Memory freed ↓ ModuleClient Termination ├─ terminate(): Destroy all sessions, shut down worker pool └─ Application exit ``` -------------------------------- ### Configure Multiple Sessions Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Sets up distinct sessions with different configurations, including default headers and proxy URLs, for various API interactions. Cleans up all sessions and the module client afterward. ```typescript const moduleClient = new ModuleClient(); // Create separate sessions for different API domains const session1 = new SessionClient(moduleClient, { defaultHeaders: { 'User-Agent': 'App1/1.0' }, }); const session2 = new SessionClient(moduleClient, { defaultHeaders: { 'User-Agent': 'App2/1.0' }, }); const session3 = new SessionClient(moduleClient, { proxyUrl: 'http://proxy.company.com:8080', }); // Use independently await Promise.all([ session1.get('https://api1.example.com'), session2.get('https://api2.example.com'), session3.get('https://protected.internal.com'), ]); // Cleanup await session1.destroySession(); await session2.destroySession(); await session3.destroySession(); await moduleClient.terminate(); ``` -------------------------------- ### Concurrency Model Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Outlines the concurrency strategy, including thread synchronization, queueing, and load balancing. ```text - **Synchronous within thread:** Each worker processes one request at a time - **Concurrent across threads:** Up to `maxThreads` requests execute simultaneously - **Bounded queue:** Tasks queue until thread available (no size limit on queue) - **Fair scheduling:** Piscina uses work-stealing queue for load balancing ``` -------------------------------- ### Importing TLS Client Types Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/types.md Demonstrates how to import various type definitions and client classes from the 'tlsclientwrapper' module. This is essential for using the library's functionalities with proper type safety. ```typescript import { type RequestBody, type ClientProfile, type Cookie, type TlsClientDefaultOptions, type TlsClientOptions, type TlsClientResponse, type CookieResponse, type CustomTLSClient, type TransportOptions, type ModuleClientOptions, type PoolStats, SessionClient, ModuleClient, } from 'tlsclientwrapper'; ``` -------------------------------- ### Configure High-Performance Client with Protocol Racing Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Set up a high-performance TLS client with increased threads, shorter timeouts, and enabled protocol racing for faster connections. Suitable for batch processing or scenarios demanding maximum throughput. ```typescript const moduleClient = new ModuleClient({ maxThreads: 32, }); const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'chrome_146', timeoutSeconds: 15, retryIsEnabled: false, disableCompression: false, withProtocolRacing: true, maxConnsPerHost: 10, maxIdleConnsPerHost: 5, }); // Handle batch requests const urls = Array.from({ length: 100 }, (_, i) => `https://api.example.com/item/${i}`); for (const url of urls) { session.get(url).catch(console.error); } ``` -------------------------------- ### open Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Opens the TLS library and initializes the worker pool. This method is called automatically by SessionClient on first use, but can be called explicitly for early initialization. ```APIDOC ## open ### Description Opens the TLS library and initializes the worker pool. This method is called automatically by SessionClient on first use, but can be called explicitly for early initialization. ### Method `open(): Promise` ### Behavior - Downloads the TLS library if it does not exist (only on main thread). - Initializes the Piscina worker pool with configured parameters. - Is idempotent: calling multiple times has no effect. - Logs download progress to the console. ### Throws Error if library path is invalid or download fails. ### Example ```typescript const moduleClient = new ModuleClient(); // Explicit initialization await moduleClient.open(); // Or let SessionClient initialize automatically const session = new SessionClient(moduleClient); // open() is called automatically here const response = await session.get('https://example.com'); ``` ``` -------------------------------- ### ModuleClientOptions Interface Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/types.md Configuration options for initializing the ModuleClient. Allows specifying custom paths for libraries and controlling the maximum number of worker threads. ```typescript interface ModuleClientOptions { customLibraryPath?: string; customLibraryDownloadPath?: string; maxThreads?: number; } ``` -------------------------------- ### head Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Sends a HEAD request to the specified URL with custom options. ```APIDOC ## head ### Description Sends a HEAD request to the specified URL with custom options. ### Method HEAD ### Endpoint `url` (URL | string) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None #### Options - **options** (Partial) - Optional - Request-specific options ### Response #### Success Response - **TlsClientResponse** - Resolves to TlsClientResponse ### Request Example ```typescript const response = await session.head('https://example.com'); console.log(response.headers); ``` ``` -------------------------------- ### [Symbol.asyncDispose] Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Enables TypeScript 5.2+ `await using` syntax for automatic resource cleanup of the session. ```APIDOC ## [Symbol.asyncDispose] ### Description Enables TypeScript 5.2+ `await using` syntax for automatic resource cleanup. ### Method (Not specified, likely a method call on a SessionClient instance) ### Parameters None ### Response #### Success Response - **(Type not specified, resolves to void)** ### Throws None explicitly mentioned. ``` -------------------------------- ### SessionClient Constructor Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Initializes a new SessionClient instance. It manages session-specific state and configuration, delegating network requests to a shared worker pool. ```APIDOC ## Constructor SessionClient ### Description Initializes a new SessionClient instance. It manages session-specific state and configuration, delegating network requests to a shared worker pool. ### Parameters #### Path Parameters - **moduleClient** (ModuleClient) - Required - Shared ModuleClient instance managing the worker pool - **options** (TlsClientDefaultOptions) - Optional - Session-level configuration options merged with defaults ### Behavior - Generates a unique session ID using `crypto.randomUUID()` for session-specific state tracking in the TLS library - Registers finalization handler for automatic cleanup if `destroySession()` is not called explicitly - Merges provided options with sensible defaults (see TlsClientDefaultOptions for full list) - Throws error if `moduleClient` is missing or not an instance of ModuleClient - Default TLS client identifier is `'chrome_146'` if not specified ### Example ```typescript import { ModuleClient, SessionClient } from 'tlsclientwrapper'; const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'firefox_147', timeoutSeconds: 30, retryMaxCount: 2, }); ``` ``` -------------------------------- ### Explicitly Open ModuleClient and Initialize Worker Pool Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Manually initialize the TLS library and worker pool before making requests. This ensures resources are ready and can be useful for pre-warming the client. ```typescript const moduleClient = new ModuleClient(); // Explicit initialization await moduleClient.open(); // Or let SessionClient initialize automatically const session = new SessionClient(moduleClient); // open() is called automatically here const response = await session.get('https://example.com'); ``` -------------------------------- ### Configure Anti-Bot Resistant TLS Client with Chrome 146 Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Establishes a session client mimicking Chrome 146 with randomized TLS extension order and retry logic. Suitable for bypassing bot detection. ```typescript const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'chrome_146', withRandomTLSExtensionOrder: true, followRedirects: true, retryMaxCount: 3, timeoutSeconds: 30, defaultHeaders: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', 'Referer': 'https://example.com/', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'none', }, defaultCookies: [{ name: 'session', value: 'abc123', domain: 'example.com', path: '/', expires: Math.floor(Date.now() / 1000) + 86400, }], }); const response = await session.get('https://example.com/protected'); ``` -------------------------------- ### Batch Processing of URLs Source: https://github.com/demonmartin/tlsclient/blob/main/readme.md Demonstrates how to efficiently process a large number of URLs by batching requests with a specified concurrency limit. This pattern is useful for making many similar requests in parallel. ```javascript const moduleClient = new ModuleClient(); const session = new SessionClient(moduleClient); // Process multiple URLs efficiently const urls = Array.from({ length: 100 }, (_, i) => `https://api.example.com/item/${i}`); // Batch requests with concurrency control const batchSize = 10; for (let i = 0; i < urls.length; i += batchSize) { const batch = urls.slice(i, i + batchSize); const responses = await Promise.all(batch.map((url) => session.get(url))); console.log(`Processed batch ${i / batchSize + 1}`); } await session.destroySession(); await moduleClient.terminate(); ``` -------------------------------- ### Task Processing Model Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Explains how tasks are submitted, queued, executed, and processed within the worker pool. ```text 1. **Submission:** SessionClient calls `pool.run({ fn: 'request', args: [...] })` 2. **Queuing:** Task enters queue if all threads busy 3. **Execution:** Available worker thread picks up task 4. **Processing:** Koffi calls TLS library function with provided args 5. **Result:** Worker returns JSON string result 6. **Parsing:** SessionClient parses JSON response 7. **Completion:** Promise resolves with TlsClientResponse ``` -------------------------------- ### TlsClientOptions Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/types.md Defines the per-request configuration options. It inherits from `TlsClientDefaultOptions` but allows for request-specific settings and overrides default headers/cookies. ```APIDOC ## TlsClientOptions ### Description Per-request configuration options. Inherits from TlsClientDefaultOptions but adds request-specific fields and removes default headers/cookies (which are merged separately). ### Type Definition ```typescript interface TlsClientOptions extends Omit { headers?: Record | null; requestBody?: string | null; requestCookies?: Cookie[] | null; sessionId?: string | null; requestUrl?: string; requestMethod?: string; } ``` ### Parameters #### Additional Fields - **headers** (Record) - Request headers (merged with defaults) - **requestBody** (string) - Serialized request body - **requestCookies** (Cookie[]) - Request cookies (merged with defaults) - **sessionId** (string) - Session identifier for state tracking - **requestUrl** (string) - Target URL for the request - **requestMethod** (string) - HTTP method (GET, POST, etc.) ### Used By - SessionClient HTTP methods (get, post, put, patch, delete, head, options) ``` -------------------------------- ### Custom TLS Library Download Path Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Configures the ModuleClient to download the TLS library to a specified custom path instead of the system temporary directory. Useful for managing library locations. ```typescript const moduleClient = new ModuleClient({ customLibraryDownloadPath: '/opt/libraries', }); // Library will be saved to: // /opt/libraries/tls-client-xgo-1.14.0-linux-amd64.so ``` -------------------------------- ### Monitoring Worker Pool and Debugging Requests Source: https://github.com/demonmartin/tlsclient/blob/main/readme.md Shows how to monitor the performance statistics of the ModuleClient's worker pool using setInterval and how to enable debug logging for a SessionClient to troubleshoot requests. ```javascript const moduleClient = new ModuleClient(); // Monitor worker pool performance setInterval(() => { const stats = moduleClient.getPoolStats(); console.log(stats); }, 5000); const session = new SessionClient(moduleClient, { withDebug: true, // Enable debug logging }); // ... your requests ... ``` -------------------------------- ### Configure Custom TLS Client Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Provide advanced, custom TLS client configuration for fine-grained control. This includes specifying supported TLS versions and key share curves, among many other options. ```typescript customTlsClient: { supportedVersions: ['1.3', '1.2'], keyShareCurves: ['X25519', 'P256'], // ... 30+ other options } ``` -------------------------------- ### ModuleClient Constructor Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/configuration.md Instantiate ModuleClient with optional configuration. Controls worker pool and library initialization. ```typescript new ModuleClient(options?: ModuleClientOptions) ``` -------------------------------- ### HTTP Proxy Configuration Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Configure the client to use an HTTP proxy by providing the proxy URL. This is useful for routing traffic through a specified proxy server. ```typescript const session = new SessionClient(moduleClient, { proxyUrl: 'http://proxy.example.com:8080', }); const response = await session.get('https://api.example.com/data'); ``` -------------------------------- ### Use a Proxy Server Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/README.md Configures a SessionClient to route requests through a specified proxy server. Useful for network anonymity or accessing restricted resources. ```typescript const session = new SessionClient(moduleClient, { proxyUrl: 'http://user:pass@proxy.example.com:8080', }); ``` -------------------------------- ### Override Session Defaults with Constructor Options Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/architecture.md Demonstrates how to override default session configurations like TLS client identifier and timeout by passing options to the SessionClient constructor. ```typescript const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'firefox_147', // Overrides default chrome_146 timeoutSeconds: 120, // Overrides default 60 }); ``` -------------------------------- ### Send OPTIONS Request with SessionClient Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/SessionClient.md Use this method to send an OPTIONS request to a specified URL. It allows you to determine the communication options for the target resource. ```typescript const response = await session.options('https://api.example.com'); console.log(response.headers); ``` -------------------------------- ### Connection Pooling Configuration Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/usage-patterns.md Enable and configure connection pooling to reuse existing connections for efficiency. This includes setting maximum idle connections, maximum total connections, and idle connection timeouts. ```typescript const session = new SessionClient(moduleClient, { disableKeepAlives: false, // Enable keep-alive maxIdleConnsPerHost: 10, // Keep connections alive maxConnsPerHost: 20, // Allow parallel connections idleConnTimeout: 30000, // 30s idle timeout }); // This session will reuse connections efficiently for (let i = 0; i < 100; i++) { await session.get('https://api.example.com/data'); } ``` -------------------------------- ### Instantiate ModuleClient with Custom Thread Count Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/api-reference/ModuleClient.md Configure the ModuleClient with a specific maximum number of worker threads. This allows for fine-tuning performance based on system resources or workload. ```typescript import { ModuleClient } from 'tlsclientwrapper'; // With custom thread count const moduleClient = new ModuleClient({ maxThreads: 16, }); ``` -------------------------------- ### Configure TLS Fingerprint Source: https://github.com/demonmartin/tlsclient/blob/main/_autodocs/README.md Initializes a SessionClient with a specific TLS fingerprint to emulate a browser or client. This is crucial for bypassing TLS inspection or fingerprinting defenses. ```typescript const session = new SessionClient(moduleClient, { tlsClientIdentifier: 'chrome_146', // or firefox_147, safari_ios_26_0, etc. }); ```