procxy / sanitizeForV8
Function: sanitizeForV8()
function sanitizeForV8(value, seen?): any;Defined in: src/shared/serialization.ts:343
Use When Avoid When Pitfalls
Strip non-V8-serializable properties from a value, returning a deep-cloned plain version.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | unknown | The value to sanitize; primitives are returned as-is |
seen | WeakSet<object> | Internal WeakSet used for circular-reference tracking; callers should omit this |
Returns
any
A new, plain-object copy of value with all non-serializable properties removed
You are passing a third-party config object as a constructor argument and cannot guarantee it contains no functions
You need a quick workaround for objects with hidden getters/setters that fail V8 validation
The dropped properties are load-bearing — sanitization silently loses data with no warning
You control the data shape — fix the type instead of sanitizing
NEVER rely on sanitized output for equality checks — keys may be missing compared to input
NEVER use on
MaporSetvalues that contain functions as keys — those entries are recursively sanitized but not removed
Remarks
Performs a recursive walk of the object graph and drops anything that cannot cross the IPC boundary: functions, getter-only properties, and class instances with custom prototypes (other than Date, RegExp, Error, Buffer, ArrayBuffer, TypedArray, Map, Set). Circular references are replaced with the string '[Circular]'.
The sanitization is intentionally lossy: dropped properties are gone without warning. This is appropriate as a last-resort safety net for configuration objects sourced from third-party libraries, but for application data it is better to fix the types at the source.
Enable automatic sanitization of constructor arguments via sanitizeV8: true in ProcxyOptions. Sanitization in that context is lazy — it only runs when initial validation fails, so there is no overhead for objects that are already clean.
Example
import { sanitizeForV8 } from 'procxy';
const config = {
data: 'hello',
handler: () => {}, // dropped — function
nested: {
value: 42,
method: () => {} // dropped — function
}
};
const sanitized = sanitizeForV8(config);
// Result: { data: 'hello', nested: { value: 42 } }See
- sanitizeForV8Array — sanitize an array of values in one call
- V8Serializable — the type constraint sanitization produces