using System.Text.Json;
namespace Febe.Core.Collection;
/// Which kind of host served a response (spec §14.5: only the site/CDN distinction is kept).
public enum HostKind
{
/// The site itself (Instagram pages and data, or the local test site).
Site,
/// The content delivery network that serves photos and videos.
Cdn,
/// Anything else the page happens to contact.
Other,
}
///
/// Sanitized description of a response seen by the page: never the query string, never headers, never the body.
///
/// Browser request identifier (opaque, only meaningful inside one browser session).
/// Site, CDN or other.
/// Scheme, host and path, without query string and fragment (see ).
/// For CDN responses it must not be logged: it identifies files (spec §13.2).
/// Host name, with the port when it is not the default one.
/// Path without query string and fragment.
/// HTTP status code.
/// MIME type declared by the response, lower case, without parameters.
/// Browser resource type (Document, XHR, Fetch, Image, ...), when known.
/// True for the document of the main frame (a page load).
/// When the response was seen.
///
/// Order in which the page started the request among the site's requests (from 1, Network.requestWillBeSent);
/// 0 when unknown. The profile search (issue #37) tells answers to texts typed before the last key from the answer to
/// the text in the box with it.
///
public sealed record ResponseDescriptor(
string RequestId,
HostKind HostKind,
string Url,
string Host,
string Path,
int Status,
string MimeType,
string? ResourceType,
bool IsMainFrameDocument,
DateTimeOffset At,
long RequestOrder = 0);
/// A main-frame navigation: a new document, or a route change inside a single-page site.
/// Site, CDN or other.
/// Address without query string and fragment.
/// Host name (with port when not default); empty for about:blank and similar.
/// Path without query string and fragment.
/// When the navigation was seen.
/// True for a route change without a new document (history.pushState).
public sealed record NavigationDescriptor(HostKind HostKind, string Url, string Host, string Path, DateTimeOffset At, bool SameDocument = false);
///
/// Receives the JSON bodies captured by the response tap (spec §6.2): readers and signal detection.
///
///
/// The is owned by the caller and disposed right after the call returns:
/// consumers must not keep references to it or to its elements. Bodies live only in memory and are never logged.
///
public interface IResponseConsumer
{
ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct);
}
/// Receives metadata of every response and every main-frame navigation (no bodies).
/// Called on the browser event thread: implementations must be quick and must not block.
public interface IResponseObserver
{
void OnResponse(ResponseDescriptor response);
void OnNavigation(NavigationDescriptor navigation);
///
/// The body of a data response announced by will not reach the consumers: it failed to
/// load, was too large, was not JSON after all (Instagram's «for (;;);» answers), or the page moved on. Observers
/// that wait for bodies stop waiting for this one.
///
void OnBodyDropped(string requestId)
{
}
///
/// The same as , with the reason (for the diagnostic report, issue #39). The tap
/// calls this one; by default it forwards to the overload without a reason.
///
void OnBodyDropped(string requestId, BodyDropReason reason) => OnBodyDropped(requestId);
///
/// A response announced by finished loading: is what
/// crossed the network (the browser's encodedDataLength). Called for every response, whether its body is read or not.
///
void OnLoadingFinished(string requestId, long encodedBytes)
{
}
///
/// The page started a request to the site: is its .
/// Nothing else of the request is passed (no address, no body).
///
void OnRequestStarted(long order)
{
}
}
/// Why the body of a response did not reach the consumers (closed list: a report may show it).
public enum BodyDropReason
{
/// Over the size the tap reads (memory guard).
TooLarge,
/// The browser says the request failed.
LoadingFailed,
/// The page moved to another document before the body finished.
FrameNavigated,
/// The browser was restarted or reconnected.
Reconnected,
/// Nobody was listening for bodies.
NoConsumers,
/// The browser no longer had the body (evicted from its buffer) or gave none.
Unavailable,
/// The body did not arrive in time.
Timeout,
/// Declared as data, but not JSON after all.
NotJson,
/// A page document without any embedded JSON block.
NoJsonBlock,
/// An unexpected error while reading it.
Unexpected,
}