import { FeedError, type FeedFormat, type FeedGame } from "./feeds.core.ts"; /** What a fetch of a feed can be told. */ export interface FeedFetchOptions { /** The format to read the answer in; `auto` (the default) tells the formats apart. */ format?: FeedFormat; /** The address a relative one is read against: the page's own by default. */ base?: string; /** The most an answer may be, in bytes: 20 MB by default. */ most?: number; /** Stop the request. */ signal?: AbortSignal; /** The function that fetches: the browser's own `fetch` by default (a test gives its own). */ fetch?: (input: string, init?: RequestInit) => Promise; } /** The most an answer may be, by default: 20 MB. */ export declare const FEED_MOST: number; /** An address the reader gave, as a URL: `http` or `https` only (a relative address is read against the page's). Throws a `FeedError` (`address`) for anything else. */ export declare function feedAddress(url: string, base?: string): URL; /** Fetch an address's answer as text, by the reader's browser: no cookie, no referrer, no cache. */ export declare function fetchFeedText(url: string, options?: Omit): Promise; /** * Fetch a feed from an address the reader gave and read it into a game. Throws a `FeedError` that says what was wrong: `address`, `cors`, `network`, `http` (with its `status`), `too-large`, `format` or `empty`. * @example * ```ts * import { loadFeed } from "@johnmorrisdotca/kyogijo-stadiums/feeds"; * const stub = async () => new Response(JSON.stringify({ frames: [{ t: 0, objects: [] }, { t: 5, objects: [] }] })); * const game = await loadFeed("https://example.com/feed.json", { fetch: stub }); * console.log(game.format, game.timeline.frames.length); * const refused = async () => { throw new TypeError("Failed to fetch"); }; * await loadFeed("https://example.com/feed.json", { fetch: refused }).catch((error) => console.log(error.kind)); * // kyogijo 2 * // cors * ``` */ export declare function loadFeed(url: string, options?: FeedFetchOptions): Promise; /** What a live follow can be told, beyond a fetch's options. */ export interface FeedFollowOptions extends FeedFetchOptions { /** Seconds between asks: 15 by default, never less than 10. */ every?: number; /** A new answer, read: only when it differs from the one before. */ onGame?: (game: FeedGame) => void; /** An answer that could not be had or read. The follow goes on, waiting longer, unless it is a 403 or a 429. */ onError?: (error: FeedError) => void; /** Told each time the follow's state changes. */ onState?: (state: FeedFollowState) => void; /** The document whose visibility it follows: the page's by default. */ doc?: Document; } /** What a follow is doing: `asking`, `waiting` for the next ask, `hidden` (the page is not shown: no ask until it is), `ended` (the game is over), `refused` (403 or 429) or `stopped`. */ export type FeedFollowState = "asking" | "waiting" | "hidden" | "ended" | "refused" | "stopped"; /** A live follow: its state, and a way to stop it. */ export interface FeedFollow { state(): FeedFollowState; stop(): void; } /** The least and the greatest wait between asks, in seconds. */ export declare const FOLLOW_EVERY: readonly [number, number]; /** * Follow a feed live: ask the address now and again every `every` seconds (15 by default, never less than 10) while the page is shown, and tell `onGame` of each answer that changed. Asking stops while the page is * hidden and starts again when it is shown; after an error the wait doubles, up to two minutes; a 403 or a 429 stops it (`refused`), and so does a game the feed says is over (`ended`). One timer at most at any time. */ export declare function followFeed(url: string, options?: FeedFollowOptions): FeedFollow;