/** * Serialized payload ready for postMessage transfer. */ interface SerializedPayload { /** Serialized data (may be string, ArrayBuffer, or structured-clone-safe object) */ data: unknown; /** Transferable objects to pass to postMessage (zero-copy) */ transferables: Transferable[]; /** Format identifier for deserialization */ format: 'structured-clone' | 'toon' | 'seroval' | 'custom'; } /** * Pluggable serializer interface for crossing the worker boundary. */ interface WorkerSerializer { /** Serialize data for transfer to/from worker */ serialize(data: unknown): SerializedPayload; /** Deserialize data received from worker/main thread */ deserialize(payload: SerializedPayload): unknown; } /** * Strategy for automatic serializer selection based on payload shape. */ type SerializerStrategy = 'auto' | 'structured-clone' | 'toon' | 'seroval' | 'custom'; /** * Configuration for the auto-detect serializer. */ interface AutoSerializerConfig { /** Size threshold (bytes) above which to use ArrayBuffer transfer (default: 102400 = 100 KiB) */ transferThreshold?: number; /** Custom serializer to use when strategy is 'custom' */ custom?: WorkerSerializer; } /** * Default serializer that relies on the browser's native structured clone algorithm. * Zero overhead — data is passed directly to postMessage without transformation. * * Best for: small payloads (< 100 KiB), simple types. * Limitations: cannot handle functions, DOM nodes, or class instances with prototype chains. */ declare const structuredCloneSerializer: WorkerSerializer; /** * Creates a `WorkerSerializer` backed by `seroval` for full type fidelity. * * Supports `Date`, `Map`, `Set`, `BigInt`, `RegExp`, circular references, and more. * The factory is async because it dynamically imports the optional `seroval` peer. * * `seroval` must be installed separately: * ``` * npm install seroval * ``` * * @example * ```typescript * const serializer = await createSerovalSerializer(); * const payload = serializer.serialize({ date: new Date(), map: new Map() }); * const original = serializer.deserialize(payload); * ``` * * @see https://github.com/lxsmnsyc/seroval */ declare function createSerovalSerializer(): Promise; /** * Creates a `WorkerSerializer` backed by `@toon-format/toon` (Token-Oriented Object Notation). * * TOON is a compact, schema-aware encoding of the JSON data model that declares object * keys once and emits values as CSV-like rows. It typically reduces size by **30–60%** * for uniform arrays of objects (e.g. `User[]`, `Product[]`, paginated lists), with * negligible parsing overhead. * * **When to use it**: * - Worker↔main `postMessage` payloads dominated by uniform arrays of objects * - Cases where `structuredClone` cost is dominated by repeated key strings * * **When NOT to use it**: * - Payloads containing `Date`, `Map`, `Set`, `RegExp` (use `seroval` instead) * - Small / single-object payloads (overhead not justified) * * The factory is async because it dynamically imports the optional `@toon-format/toon` peer. * * `@toon-format/toon` must be installed separately: * ``` * npm install @toon-format/toon * ``` * * @example * ```typescript * const serializer = await createToonSerializer(); * const payload = serializer.serialize([ * { id: 1, name: 'Alice' }, * { id: 2, name: 'Bob' }, * { id: 3, name: 'Carol' }, * { id: 4, name: 'Dave' }, * { id: 5, name: 'Eve' }, * ]); * worker.postMessage({ payload }, payload.transferables); * ``` * * @see https://toonformat.dev */ declare function createToonSerializer(): Promise; /** * Conservative threshold below which TOON's overhead outweighs its size benefit. * Auto-serializer keeps shorter arrays on `structured-clone`. * * Exported for testing and for consumers building custom routing logic. */ declare const MIN_UNIFORM_ARRAY_LENGTH = 5; /** * Detects whether a value is a depth-1 uniform array of plain objects with primitive values. * * Pure function — no side effects. Used by `createAutoSerializer()` to decide * whether to route a payload through TOON. * * Conditions checked (all must pass): * 1. Value is an array with `length >= MIN_UNIFORM_ARRAY_LENGTH` * 2. Every item is a non-null, non-array plain object * 3. Every item has the same set of keys as the first item * 4. Every value across all items is a primitive (string, number, boolean, null) * * @example * ```typescript * isUniformObjectArray([{a:1},{a:2},{a:3},{a:4},{a:5}]); // true * isUniformObjectArray([{a:1},{b:2}]); // false (heterogeneous keys) * isUniformObjectArray([{a:[1,2]},{a:[3,4]}]); // false (nested array value) * isUniformObjectArray([{a:1}]); // false (length < 5) * ``` */ declare function isUniformObjectArray(value: unknown): boolean; /** * Creates an auto-detecting `WorkerSerializer` that picks the best strategy per payload. * * The factory is async because it pre-loads `seroval` during initialization * so the returned serializer methods are fully synchronous (no await in hot path). * * Strategy selection per `serialize()` call (top-down, first match wins): * 1. Contains `Date`, `Map`, `Set`, or `RegExp` at depth-1 → `seroval` (full fidelity) * 2. Uniform array of plain objects (length ≥ 5, primitive values, identical key set) * → `toon` (30–60% size reduction) * 3. Otherwise → structured clone (native, zero overhead) * * Large payloads (> `transferThreshold`, default 100 KiB) are encoded to * `ArrayBuffer` and added to `transferables` for zero-copy `postMessage` transfer. * * @example * ```typescript * const auto = await createAutoSerializer(); * const payload = auto.serialize({ users, fetchedAt: new Date() }); * worker.postMessage({ payload }, payload.transferables); * ``` */ declare function createAutoSerializer(config?: AutoSerializerConfig): Promise; export { MIN_UNIFORM_ARRAY_LENGTH, createAutoSerializer, createSerovalSerializer, createToonSerializer, isUniformObjectArray, structuredCloneSerializer }; export type { AutoSerializerConfig, SerializedPayload, SerializerStrategy, WorkerSerializer };