Metreeca Gear
    Preparing search index...

    Function fetch

    • Creates a resource exchange task.

      The generated task reads a stream of requests as a stream of byte chunks, reporting the body of each response as its chunks are received; requests are given as accepted by the standard fetch() function, that is as URL strings, URL objects or Request objects.

      Chunks are pulled from the response as they are asked for, so resources of any size are handled without holding them in memory and a consumer that stops early cancels the response; a response reporting no body is read as an empty stream.

      Exchanges are routed through the fetch client resolved from the enclosing execution, so that the transport is chosen when the task is run rather than by the task itself: binding createFetch routes every exchange through a throttled, cached or stubbed client, without altering the task, while an unbound execution falls back on the standard fetch function.

      Exchanges are sent under desktop browser User-Agent and Accept fields, so that sites serving unattended clients differently, or refusing them altogether, are scraped as a browser would be; a request already stating one of these fields keeps its own value, as do the ones stated by middlewares.

      Content coding is left to the transport, which states the codings it decodes and decodes the body before it reaches the consumer: no runtime API reports what an implementation handles, so a field stated here would be a guess, and a coding guessed wrong would hand over a body still compressed.

      Warning

      Responses reporting an unsuccessful status are skipped, leaving the stream to run to completion; a request stating a URL that is not absolute or is otherwise malformed brings the stream down instead, unless a middleware screening it, such as monitor() from @metreeca/http/monitor, is layered over the client.

      Parameters

      • ...middlewares: readonly Middleware[]

        The middlewares to be layered over the resolved fetch client, in request processing order

      Returns Task<string | Request | URL, Uint8Array<ArrayBufferLike>>

      A task converting a stream of requests into a stream of response byte chunks

      Error While iterating the returned stream, if no execution is running, as the fetch client is resolved from the enclosing one

      Error While iterating the returned stream, whatever the exchange reports while connecting to a resource or receiving its response