Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.7 KB
Raw Blame
import type { RecordingSegment } from '@posthog/replay-shared'

import { PLAYER_CONFIG_KEY, PLAYER_EMIT_FN, PLAYER_START_EVENT } from './protocol'
import type { PlayerConfig, PlayerError, PlayerMessage } from './protocol'

/**
 * Player-side counterpart to the rasterizer's PlayerController.
 *
 * Sends messages to the host process (Puppeteer) via an exposed function
 * callback, and receives commands via DOM events. All player → rasterizer
 * communication goes through {@link emit} so the protocol is defined in
 * one place.
 */
export class HostBridge {
    // --- Emit ---

    private emit(msg: PlayerMessage): void {
        const fn = window[PLAYER_EMIT_FN]
        if (typeof fn === 'function') {
            void fn(msg)
        }
    }

    // --- Signals ---

    signalStarted(): void {
        this.emit({ type: 'started' })
    }

    signalEnded(): void {
        this.emit({ type: 'ended' })
    }

    setError(error: PlayerError): void {
        this.emit({ type: 'error', ...error })
    }

    reportLoadingProgress(loaded: number, total: number): void {
        this.emit({ type: 'loading_progress', loaded, total })
    }

    // --- Data ---

    publishSegments(segments: RecordingSegment[], firstTimestamp: number): void {
        const periods = segments.map((seg) => ({
            ts_from_s: Math.round(seg.startTimestamp - firstTimestamp) / 1000,
            ts_to_s: Math.round(seg.endTimestamp - firstTimestamp) / 1000,
            active: seg.isActive,
        }))
        this.emit({ type: 'inactivity_periods', periods })
    }

    publishFrameTimeline(frameSessionMs: number[]): void {
        this.emit({ type: 'frame_timeline', frameSessionMs })
    }

    // --- Config ---

    /**
     * Read config injected by the rasterizer via evaluateOnNewDocument.
     * The config is set as a window global before page load, so it's
     * always available by the time this runs.
     */
    getConfig(): PlayerConfig {
        const config = window[PLAYER_CONFIG_KEY]
        if (!config) {
            throw new Error('Player config not found — was it injected via evaluateOnNewDocument?')
        }
        return config
    }

    // --- Events ---

    async waitForStart(timeoutMs = 30000): Promise<void> {
        await new Promise<void>((resolve, reject) => {
            const handler = (): void => {
                clearTimeout(timer)
                resolve()
            }
            const timer = setTimeout(() => {
                window.removeEventListener(PLAYER_START_EVENT, handler)
                reject(new Error(`${PLAYER_START_EVENT} not received within ${timeoutMs / 1000}s`))
            }, timeoutMs)
            window.addEventListener(PLAYER_START_EVENT, handler, { once: true })
        })
    }
}