/** * What went wrong on the page in the Studio's preview: the assistant's * `read_page_log`, and the counts on every `read_page`. * * Looking at a page does not say that it threw. A section that rendered * nothing because its component crashed looks exactly like a section with * nothing in it, and an image that 404'd is an empty box. So the page keeps a * short log of what it said went wrong, and the Studio can ask for it. * * Plain DOM and in `shared` for the reason `pageInspection.ts` is: the canvas * bridge (`@valbuild/next`, `@valbuild/tanstack`) runs it inside the page, * which is the only place any of this can be heard. Only what `readPageLog` * returns crosses the frame (`valCanvasProtocol.ts`). * * ## What is caught * * - `console.error` and `console.warn`, wrapped: the page's own console gets * the same call with the same arguments, so it prints exactly what it did * (one frame of this module is in the stack devtools shows for the line). * - Uncaught exceptions (`error` on the window) and rejected promises nobody * handled (`unhandledrejection`). * - Elements whose resource did not load -- `img`, `script`, `link`, `video`, * `audio`, `source`, `iframe` -- through a capture-phase `error` listener, * since those events do not bubble. * - `fetch` calls that failed outright or answered 400 or more. * * ## What is not * * - Anything before {@link installPageLog} ran. The bindings call it when * their provider's module is evaluated in the canvas frame, which is before * React hydrates -- so hydration errors are caught -- but after the app's * own modules that were evaluated first, and after any inline script. Two * things are looked for after the fact: `` elements already broken, * and requests the browser's resource timing says answered 400 or more * (`responseStatus`, where the browser has it). An exception thrown before * that is gone. * - What happened on the SERVER. A route that threw while rendering on the * server sends its error page, which `read_page` reads; its stack is in the * server's own log. * - `XMLHttpRequest`, `sendBeacon`, WebSocket and EventSource traffic: only * `fetch` is wrapped. * - `console.log`, `info` and `debug`: chatter, and the page's own business. * - Val's own: lines Val's logger printed (`[val]`) and requests to Val's API * (`/api/val`), which are the Studio talking to its server and not the page. */ /** How many entries the log keeps: older ones make room for new ones. */ export declare const PAGE_LOG_SIZE = 200; /** How long one entry's text may be, before it is shortened. */ export declare const PAGE_LOG_TEXT_LENGTH = 600; export type PageLogLevel = "error" | "warn"; /** Where an entry came from. */ export type PageLogSource = /** `console.error` / `console.warn`. */ "console" /** An uncaught exception. */ | "exception" /** A rejected promise nobody handled. */ | "rejection" /** An element whose image, script, stylesheet or media did not load. */ | "resource" /** A `fetch` that failed, or answered 400 or more. */ | "request"; export type PageLogEntry = { level: PageLogLevel; source: PageLogSource; /** What was said, shortened to {@link PAGE_LOG_TEXT_LENGTH}. */ text: string; /** The page's path when it happened: a client-side navigation moves it. */ path: string; /** Milliseconds after the document started loading. */ at: number; /** The URL that did not load: a resource or a request. */ url?: string; /** The status a request answered with. Absent when it never answered. */ status?: number; /** The tag of the element whose resource did not load. */ tag?: string; /** How many times the same thing happened in a row, when more than once. */ repeated?: number; }; /** How many of each, as `read_page` reports them. */ export type PageLogCounts = { errors: number; warnings: number; failedRequests: number; }; /** What `readPageLog` answers. */ export type PageLogSnapshot = { /** The page's path now. */ path: string; /** Oldest first. */ entries: PageLogEntry[]; /** * Whether `entries` is only what happened since the page last navigated * (client-side: a new document starts a new log anyway). */ sinceNavigation: boolean; /** How many entries were pushed out of the log to make room, ever. */ dropped: number; /** * When the log started listening, in milliseconds after the document * started loading: what happened before is not in it. */ startedAt: number; }; export type PageLog = { /** What is in it, filtered. */ read: (options?: { sinceNavigation?: boolean; levels?: readonly PageLogLevel[]; /** * Only what was recorded after this {@link PageLog.mark}: what an * interaction caused (`pageInteraction.ts`). Overrides * `sinceNavigation`, since a mark is later than any navigation before * it. An entry that repeated across the mark counts only its repeats * after it. */ since?: number; }) => PageLogSnapshot; /** * Where the log is now, to read from later with `since`: how many times * it has recorded anything. */ mark: () => number; /** * How many of the page's `fetch` calls have not answered yet (Val's own * excluded): what an interaction waits on before it calls the page * settled. */ inFlight: () => number; /** How many of each since the page last navigated. */ counts: () => PageLogCounts; /** * The page may have navigated: what the bridge calls when it sees the * address move. The log also looks for itself whenever it records or is * read, so this only makes a move with nothing logged during it count. */ noteLocation: () => void; /** Stop listening and put the page's own functions back. */ uninstall: () => void; }; export type PageLogOptions = { /** Requests that are not the page's own (Val's API, by default). */ ignoreUrl?: (url: URL) => boolean; size?: number; }; /** The window's log, if one is installed. */ export declare function pageLogOf(win: Window): PageLog | null; /** * Start keeping the page's log, or return the one already kept: a second * call (the provider's module, then the bridge) changes nothing. */ export declare function installPageLog(win: Window & typeof globalThis, options?: PageLogOptions): PageLog; /** * Start the log if this document is in the Studio's canvas, and only then: * what the bindings call when their provider's module is evaluated, which is * the earliest moment Val's code runs in the page -- before React hydrates. * A visitor's page (not in a frame, or framed by anything but the Studio) * is left exactly as it was. Does nothing on the server. */ export declare function installPageLogInCanvasFrame( /** * What else the bridge needs before it mounts that can be known this early: * the bindings pass whether Val is enabled in this browser (the * `val_enable` cookie), which is what mounts the overlay -- and so the * bridge -- outside the demo. What can only be known later (draft mode, * the demo prop) is left to the bridge, which starts the log itself when * it mounts in a frame. */ isEnabled?: () => boolean): PageLog | null; /** * Whether this document is the Studio's canvas AND framed. The canvas * parameter alone is not enough: a visitor can open `?val_canvas=1` in a tab * of its own -- "open frame in new tab", a link someone shared -- and that is * a visitor's page, which the log never touches. */ export declare function isInStudioFrame(win: Window): boolean; /** How many of each, in a list of entries. */ export declare function countEntries(entries: readonly PageLogEntry[]): PageLogCounts; /** * The arguments of a console call as one line, the way the console would * print them: `%s`-style substitutions applied, and `%c` (a style) dropped * along with its argument. Bounded: it stops describing once the line is * {@link PAGE_LOG_TEXT_LENGTH} long, however much the page logged. */ export declare function formatConsoleArgs(args: readonly unknown[]): string; /** * One value as text, in at most about `budget` characters: an error with the * top of its stack, an object as its own data properties. * * It never calls the page's code to do it -- no `toJSON`, no getters, no * `toString` of the page's own -- because describing a value must not change * it, and a value logged in a render loop is described on every render: it * reads only own DATA properties, a few levels down, and stops at the budget. */ export declare function describeValue(value: unknown, budget?: number): string;