Function guardedFetch

  • Reads address, an http or https address a person gave a server, under options. Each hop asks the caller's allowHost, when given, about the host before anything is resolved, then resolves the host and refuses it when any of its addresses is not public (isPublicAddress) and not in allowAddresses, then connects to a checked address on a connection of its own, trying the next one when the first cannot be reached; a redirect is followed only to an address that passes the same checks, at most maxRedirects times. The answer must be a 2xx of a type in accept, and its body, decoded from gzip, deflate or br, is refused once it passes maxBytes on the wire or decoded. The whole read stops at deadlineMs.

    Parameters

    Returns Promise<GuardedResponse>

    Example

    const page = await guardedFetch('https://example.com/notes', { maxBytes: 2 * 1024 * 1024, deadlineMs: 10_000 });
    page.contentType; // 'text/html'

    Throws

    When maxBytes or deadlineMs is not a positive number (or deadlineMs is longer than Node's longest timer), or maxRedirects is not a whole number of 0 or more, before any lookup.

    Throws

    When allowHost is given and is not a function, before any lookup.

    Throws

    When the address, a redirect or the answer is refused, or the read fails or passes its deadline; reason says which, and a host rule's own failure is the error's cause.

    Throws

    The reason of options.signal when the caller aborts it.