Implementation:Puppeteer Puppeteer Function Util
| Property | Value |
|---|---|
| sources | packages/puppeteer-core/src/util/Function.ts |
| domains | Utility, Function Serialization |
| last_updated | 2026-02-12 00:00 GMT |
Overview
Description
The Function Util module provides utilities for serializing, deserializing, and interpolating JavaScript functions as strings. These utilities are critical for Puppeteer's page.evaluate and related APIs, where user-supplied functions must be serialized into strings, transmitted over the Chrome DevTools Protocol (CDP) or WebDriver BiDi, and then reconstructed and executed inside the browser context.
The module provides three primary functions:
- createFunction -- Converts a function string back into an executable function, with a caching layer using a Map to avoid redundant construction via the Function constructor.
- stringifyFunction -- Converts a function reference into its string form, handling edge cases such as arrow functions, async functions, generator functions, and object method shorthand syntax.
- interpolateFunction -- Replaces PLACEHOLDER('name') calls within a stringified function with provided replacement code strings, enabling compile-time injection of values into functions that will execute in the browser context.
Usage
These functions are used internally by Puppeteer when evaluating expressions in the browser context. stringifyFunction serializes a user callback, interpolateFunction injects runtime values, and createFunction reconstructs the function from its string representation.
Code Reference
Source Location
packages/puppeteer-core/src/util/Function.ts
Signature
export const createFunction = (
functionValue: string,
): ((...args: unknown[]) => unknown);
export function stringifyFunction(fn: (...args: never) => unknown): string;
export const interpolateFunction = <T extends (...args: never[]) => unknown>(
fn: T,
replacements: Record<string, string>,
): T;
declare global {
function PLACEHOLDER<T>(name: string): T;
}
Import
import {createFunction, stringifyFunction, interpolateFunction} from '../util/Function.js';
I/O Contract
| Function | Parameter | Type | Description |
|---|---|---|---|
| createFunction | functionValue | string |
A string representation of a JavaScript function |
| stringifyFunction | fn | (...args: never) => unknown |
A JavaScript function reference to serialize |
| interpolateFunction | fn | T |
The function containing PLACEHOLDER calls |
| interpolateFunction | replacements | Record<string, string> |
Map of placeholder names to replacement JS code strings |
| Function | Return Type | Description |
|---|---|---|
| createFunction | (...args: unknown[]) => unknown |
An executable function reconstructed from the string |
| stringifyFunction | string |
The string representation of the provided function |
| interpolateFunction | T |
A new function with all PLACEHOLDER calls replaced by the provided values |
Usage Examples
// Stringify a function for transmission over CDP
const fnString = stringifyFunction((a: number, b: number) => a + b);
// Result: "(a, b) => a + b"
// Recreate the function from its string form
const fn = createFunction(fnString);
fn(2, 3); // 5
// Interpolate placeholders with runtime values
const interpolated = interpolateFunction(
() => PLACEHOLDER<string>('greeting'),
{ greeting: '"Hello, World!"' },
);
interpolated(); // "Hello, World!"