/** * What `interact_page` lets the page send to the site's server while an * action runs. * * The preview is the real site, and its backend is real. The first line of * defence is the person: every click and every Enter or Space in the * preview is asked about BEFORE it is dispatched (`pageInteraction.ts`). * This is the second, for the actions that are not asked about -- typing, * other keys, opening a page, changing the width -- and for the off-site * rule, which holds for every action. * * ## `hold`: for the actions nobody approved * * From the moment the action is dispatched until the page has settled: * * - `fetch`, `XMLHttpRequest` and `navigator.sendBeacon` with a method that * sends data (anything but GET, HEAD and OPTIONS) are not sent. The page's * `fetch` gets a promise that has not settled yet; its XHR has not been * sent; its beacon was "queued". * - A form submitted with such a method -- natively, caught by a * capture-phase `submit` listener on the window, or by `form.submit()`, * which fires no event and is wrapped -- is not submitted. * * What was held is shown to the person -- method, the address the browser * resolved, and an excerpt of the body -- and Allow sends exactly that * (`releaseHold`), as it was SNAPSHOT when it was held: a `Request` copy, a * `FormData` of the form at that moment. Anything else, or * {@link HOLD_TIMEOUT_MS} with no answer, refuses it as an abort. What this * gate cannot read -- an address it cannot resolve, a method it cannot see * -- is held, not let through. * * ## `off-site`: for an action the person approved * * The action is the person's, so the page does exactly what it would for * them: nothing same-site is held, and no same-site submit is cancelled -- * React form actions and routers that bail on `defaultPrevented` work. Only * data sent to ANOTHER site, and a form that would take the frame to another * site or window, are refused. In both modes that is refused without * asking: the preview stays on the site. * * ## Not covered, in either mode * * Requests a page sends while it loads (a gate starts with an action, in a * document that is already there), requests sent after the page settled * (an autosave on a timer), WebSocket and EventSource traffic, workers, * embedded frames, a reference to the original `fetch` or * `XMLHttpRequest.prototype.send` taken before the action, an XHR opened * before the action, GET, HEAD and OPTIONS requests -- to other sites too, * where a query string can carry data -- image and script loads, and GET * navigations: links, which can have side effects of their own (a logout * link, an unsubscribe link), and script navigations and redirects, which * can leave the site. * * A body that cannot be copied as it is (a stream, a document) is not held * but refused, so what Allow sends is always what the person was shown. * * Every wrapper knows when its gate has stopped and then passes straight * through to what it wrapped, so a page or an SDK that wraps `fetch` again * during an action, or two gates that overlap, leave nothing stuck behind. * Nothing here runs outside the Studio's frame: only the canvas bridge * calls it. */ /** The page's window, as `window` is typed: with its own constructors. */ type Win = Window & typeof globalThis; /** A request the page tried to make during an action. */ export type PageRequest = { via: "fetch" | "xhr" | "beacon" | "form"; /** Upper case: `POST`. `UNKNOWN` when it could not be read. */ method: string; /** Absolute, as the browser resolved it; empty when it could not be. */ url: string; /** A short excerpt of what it sends: form fields, JSON, or its size. */ body?: string; }; /** * The most requests one hold keeps. More than this is refused outright * rather than shown in part: the person must see everything Allow sends. The * Studio checks the same number. */ export declare const MAX_HELD_REQUESTS = 10; /** How much of a request's body the person is shown. */ export declare const BODY_EXCERPT_LENGTH = 200; /** * A held request, and how to send or refuse it. `send` says why it could * not be sent, if it could not. */ type Held = PageRequest & { send: () => string | undefined; refuse: () => void; }; /** * How long a hold waits for the person -- a card can sit for a while -- * before what it holds is refused. */ export declare const HOLD_TIMEOUT_MS: number; export type GateMode = "hold" | "off-site"; export type RequestGate = { /** What was held so far. */ held: () => readonly PageRequest[]; /** What was refused for going to another site, or for leaving it. */ blocked: () => readonly PageRequest[]; /** XHRs let through and not finished: counted as in flight. */ inFlight: () => number; /** * Stop: every wrapper passes straight through from now on, and is put * back where nothing wrapped it since. What was held stays held, and is * returned: hand it to {@link holdRequests}, or refuse it. */ stop: () => HeldRequests; }; /** Requests held by a gate that stopped: opaque outside this module. */ export type HeldRequests = { readonly requests: readonly Held[]; }; /** Start the gate in `mode`: see the module doc. */ export declare function startRequestGate(win: Win, { mode }: { mode: GateMode; }): RequestGate; /** ``: where links and forms without a target of their own go. */ export declare function baseTargetOf(doc: Document): string | null; /** * Keep held requests for the person to decide on: the id the Studio sends * back with Allow or Don't allow. Refused by themselves after * {@link HOLD_TIMEOUT_MS}. */ export declare function holdRequests(win: Window, held: HeldRequests): string; /** * Send (`allow`) or refuse what a hold kept: what the person decided. `null` * when there is no such hold any more -- refused already, timed out, or the * page reloaded. */ export declare function releaseHold(win: Window, id: string, allow: boolean): { requests: readonly PageRequest[]; notSent: string[]; } | null; /** Refuse every hold of the window: a new action means they were not wanted. */ export declare function refuseAllHolds(win: Window): void; /** Refuse requests a gate held and nobody kept. */ export declare function refuseHeld(held: HeldRequests): void; export {};