Implementation:Webdriverio Webdriverio FetchRequest Class
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, HTTP_Client |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The FetchRequest class is the Node.js HTTP request implementation that extends WebDriverRequest, using undici's fetch() with connection pooling via per-session dispatchers and automatic proxy support.
Description
FetchRequest implements the abstract fetch() method from WebDriverRequest using undici's fetch() function. It manages a module-level SESSION_DISPATCHERS map that caches Dispatcher instances (either Agent or ProxyAgent) per session ID to enable connection pooling. When a session is deleted (detected by matching the DELETE request URL against the session path pattern), the corresponding dispatcher is cleaned up and its connections closed.
The private getDispatcher() method resolves the appropriate dispatcher by: (1) checking if a cached dispatcher exists for the session, (2) checking if a custom global dispatcher has been set (e.g., ProxyAgent), and (3) falling back to creating a new dispatcher based on environment variables (PROXY_URL and NO_PROXY). All dispatchers are configured with the connectionRetryTimeout for connect, headers, and body timeouts.
The createOptions() override calls the parent implementation and then attaches the resolved dispatcher to the undici request options.
Usage
Use FetchRequest as the Node.js HTTP transport for WebDriver commands. It is registered as the default Request class in the Node.js environment adapter and is instantiated by the command factory for every protocol command execution.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/webdriver/src/request/node.ts
Signature
export const SESSION_DISPATCHERS: Map<string, Dispatcher> = new Map()
export class FetchRequest extends WebDriverRequest {
async fetch(url: URL, opts: RequestInit): Promise<Response>
private getDispatcher(url: URL, options: RequestOptions, sessionId?: string): Dispatcher
private cleanupSessionDispatcher(sessionId: string): void
async createOptions(options: RequestOptions, sessionId?: string, isBrowser?: boolean): Promise<{ url: URL; requestOptions: RequestInit }>
}
Import
import { FetchRequest, SESSION_DISPATCHERS } from './request/node.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | URL |
Yes | The target URL for the HTTP request. |
| opts | RequestInit |
Yes | Fetch request options including method, headers, body, and signal. |
| options (createOptions) | RequestOptions |
Yes | WebDriver connection options including protocol, hostname, port, and timeouts. |
| sessionId | string |
No | Session ID used to look up or create a cached dispatcher for connection pooling. |
Outputs
| Name | Type | Description |
|---|---|---|
| fetch() | Promise<Response> |
The HTTP response from the undici fetch call. |
| createOptions() | Promise<{ url: URL; requestOptions: RequestInit }> |
URL and request options with the undici dispatcher attached. |
| SESSION_DISPATCHERS | Map<string, Dispatcher> |
Module-level cache of per-session undici dispatchers for connection pooling. |
Usage Examples
import { FetchRequest, SESSION_DISPATCHERS } from './request/node.js'
// Create and use a FetchRequest instance
const request = new FetchRequest('POST', '/session/:sessionId/url', { url: 'https://example.com' });
const result = await request.makeRequest({
protocol: 'http',
hostname: 'localhost',
port: 4444,
path: '/',
connectionRetryTimeout: 120000,
connectionRetryCount: 3
}, 'session-id-123');
// Check the number of active session dispatchers
console.log(SESSION_DISPATCHERS.size); // 1
// After deleteSession, dispatcher is cleaned up automatically
const deleteRequest = new FetchRequest('DELETE', '/session/:sessionId', {});
await deleteRequest.makeRequest(options, 'session-id-123');
console.log(SESSION_DISPATCHERS.size); // 0