Implementation:Puppeteer Puppeteer Encoding
| Property | Value |
|---|---|
| sources | packages/puppeteer-core/src/util/encoding.ts |
| domains | Utility, Binary Encoding |
| last_updated | 2026-02-12 00:00 GMT |
Overview
Description
The encoding module provides utility functions for converting between strings, base64-encoded data, and Uint8Array typed arrays. These functions are used throughout Puppeteer for handling binary data received from the Chrome DevTools Protocol, such as screenshots (PNG/JPEG), PDF content, and network response bodies, which are transmitted as base64-encoded strings.
The module exports four functions:
- stringToTypedArray -- Converts a string into a Uint8Array. When
base64Encodedis true, it decodes the base64 string using the best available method:Uint8Array.fromBase64(modern runtimes),Buffer.from(Node.js), oratob(fallback). When false, it uses TextEncoder to encode UTF-8. - stringToBase64 -- Encodes a plain UTF-8 string to base64 by first encoding it to a Uint8Array via TextEncoder and then converting to base64.
- typedArrayToBase64 -- Converts a Uint8Array to a base64 string. It processes the array in chunks of 65,534 bytes to avoid exceeding the V8 argument limit for
String.fromCodePoint. - mergeUint8Arrays -- Concatenates multiple Uint8Array instances into a single contiguous array.
Usage
These functions are used internally when Puppeteer receives binary data over the protocol (e.g., screenshot data, PDF bytes) or needs to send binary payloads. They are not part of the public API.
Code Reference
Source Location
packages/puppeteer-core/src/util/encoding.ts
Signature
export function stringToTypedArray(
string: string,
base64Encoded?: boolean,
): Uint8Array;
export function stringToBase64(str: string): string;
export function typedArrayToBase64(typedArray: Uint8Array): string;
export function mergeUint8Arrays(items: Uint8Array[]): Uint8Array;
Import
import {stringToTypedArray, stringToBase64, typedArrayToBase64, mergeUint8Arrays} from '../util/encoding.js';
I/O Contract
| Function | Parameter | Type | Description |
|---|---|---|---|
| stringToTypedArray | string | string |
The input string (plain text or base64-encoded) |
| stringToTypedArray | base64Encoded | boolean |
Whether the input is base64-encoded (default: false) |
| stringToBase64 | str | string |
A plain UTF-8 string to encode |
| typedArrayToBase64 | typedArray | Uint8Array |
A typed array to encode as base64 |
| mergeUint8Arrays | items | Uint8Array[] |
An array of typed arrays to concatenate |
| Function | Return Type | Description |
|---|---|---|
| stringToTypedArray | Uint8Array |
The decoded or encoded byte array |
| stringToBase64 | string |
The base64-encoded string |
| typedArrayToBase64 | string |
The base64-encoded string representation |
| mergeUint8Arrays | Uint8Array |
A single concatenated typed array |
Usage Examples
// Decode a base64 screenshot from CDP
const base64Data = 'iVBORw0KGgoAAAANSUhEUgAA...';
const imageBytes = stringToTypedArray(base64Data, true);
// Encode a string to base64 for protocol transmission
const encoded = stringToBase64('Hello, Puppeteer!');
// Merge multiple response chunks into a single buffer
const merged = mergeUint8Arrays([chunk1, chunk2, chunk3]);