Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Puppeteer Puppeteer HttpUtil

From Leeroopedia
Property Value
sources packages/browsers/src/httpUtil.ts
domains HTTP Client, File Download, Network Utilities
last_updated 2026-02-12 00:00 GMT

Overview

Description

The HttpUtil module provides low-level HTTP/HTTPS client utilities for the @puppeteer/browsers package. It handles file downloads, JSON fetching, text retrieval, and URL availability checks. All requests are proxy-aware through the proxy-agent library, which automatically detects and uses system proxy settings.

Key features include:

  • Automatic redirect following -- HTTP 3xx responses are transparently followed
  • Proxy support -- Uses ProxyAgent for automatic proxy detection
  • Protocol detection -- Automatically selects http or https module based on URL protocol
  • Download progress tracking -- The downloadFile function supports progress callbacks
  • Connection management -- Supports both keep-alive and non-keep-alive connections

The module is used extensively by the browser-data modules (Chrome, Firefox) to fetch version metadata from Google and Mozilla APIs, and by the install module to download browser archives.

Usage

This module is used internally throughout the @puppeteer/browsers package for all network operations, from checking download availability to downloading multi-hundred-megabyte browser archives with progress reporting.

Code Reference

Source Location

packages/browsers/src/httpUtil.ts

Signature

export function headHttpRequest(url: URL): Promise<boolean>;

export function httpRequest(
  url: URL,
  method: string,
  response: (x: http.IncomingMessage) => void,
  keepAlive?: boolean,
): http.ClientRequest;

export function downloadFile(
  url: URL,
  destinationPath: string,
  progressCallback?: (downloadedBytes: number, totalBytes: number) => void,
): Promise<void>;

export async function getJSON(url: URL): Promise<unknown>;

export function getText(url: URL): Promise<string>;

Import

import {headHttpRequest, httpRequest, downloadFile, getJSON, getText} from './httpUtil.js';

I/O Contract

Inputs

Parameter Type Description
url URL Target URL (http or https)
method string HTTP method (GET, HEAD, etc.)
response (x: http.IncomingMessage) => void Callback to handle the HTTP response
keepAlive boolean (optional) Whether to set Connection: keep-alive header; defaults to true
destinationPath string Local filesystem path for downloaded file
progressCallback (downloadedBytes: number, totalBytes: number) => void (optional) Progress reporting callback

Outputs

Function Return Type Description
headHttpRequest Promise<boolean> true if the URL returns HTTP 200, false otherwise
httpRequest http.ClientRequest The underlying Node.js HTTP request object
downloadFile Promise<void> Resolves when download completes; rejects on HTTP error or network failure
getJSON Promise<unknown> Parsed JSON object from the response body
getText Promise<string> Raw text content of the response body

Usage Examples

import {headHttpRequest, downloadFile, getJSON, getText} from './httpUtil.js';

// Check if a download URL exists
const exists = await headHttpRequest(
  new URL('https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.109/linux64/chrome-linux64.zip')
);
// => true or false

// Download a file with progress tracking
await downloadFile(
  new URL('https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.109/linux64/chrome-linux64.zip'),
  '/tmp/chrome-linux64.zip',
  (downloaded, total) => {
    const percent = Math.round((downloaded / total) * 100);
    console.log(`Progress: ${percent}%`);
  }
);

// Fetch JSON data
const versions = await getJSON(
  new URL('https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json')
);

// Fetch text content
const text = await getText(
  new URL('https://product-details.mozilla.org/1.0/firefox_versions.json')
);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment