# andris9/fetch architecture

> Architecture diagram of the GitHub repository andris9/fetch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andris9/fetch
- Repository: https://github.com/andris9/fetch

## Overview

This is a Node.js library for fetching URL contents as either a readable stream or a completed response body. The main workflow is: a caller starts a fetch, FetchStream prepares and issues HTTP(S) requests, follows redirects while managing cookies, then emits response metadata and content; fetchUrl collects that content and optionally decodes its charset before invoking the callback. The README also documents gzip/deflate handling and piping streams to files. The excerpts show the core request, cookie, and decoding relationships; other documented capabilities are included where their implementation paths are known.

Main entry points: [`lib/fetch.js`](https://github.com/andris9/fetch/blob/master/lib/fetch.js) (FetchStream), [`lib/cookiejar.js`](https://github.com/andris9/fetch/blob/master/lib/cookiejar.js) (CookieJar)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_request["Request and streaming"]
  node_fetchstream["FetchStream<br/>[fetch.js]"]
  node_redirects["Redirect handling<br/>[fetch.js]"]
end

subgraph group_response["Response processing"]
  node_gzip["Gzip/deflate decode<br/>[fetch.js]"]
  node_fetchurl["fetchUrl<br/>[fetch.js]"]
  node_charset["Charset decoding<br/>[fetch.js]"]
end

subgraph group_cookies["Cookie management"]
  node_cookiejar["CookieJar<br/>[cookiejar.js]"]
end

node_caller(("Library caller"))
node_httpserver(("HTTP&#40;S&#41; server"))
node_biskviit[("biskviit store")]
node_streamconsumer(("Stream consumer"))
node_callback(("Result callback"))
node_iconv["iconv-lite"]
node_file[("File output")]

node_caller -->|"starts stream"| node_fetchstream
node_caller -->|"requests body"| node_fetchurl
node_fetchstream -->|"issues requests"| node_httpserver
node_fetchstream -->|"gets and sets cookies"| node_cookiejar
node_cookiejar -->|"delegates storage"| node_biskviit
node_fetchstream -->|"handles redirects"| node_redirects
node_fetchstream -->|"decodes content"| node_gzip
node_fetchstream -->|"emits stream"| node_streamconsumer
node_streamconsumer -->|"pipes content"| node_file
node_fetchurl -->|"creates stream"| node_fetchstream
node_fetchurl -->|"decodes body"| node_charset
node_charset -->|"uses decoder"| node_iconv
node_fetchurl -->|"returns body and metadata"| node_callback

click node_fetchstream "https://github.com/andris9/fetch/blob/master/lib/fetch.js"
click node_redirects "https://github.com/andris9/fetch/blob/master/lib/fetch.js"
click node_cookiejar "https://github.com/andris9/fetch/blob/master/lib/cookiejar.js"
click node_gzip "https://github.com/andris9/fetch/blob/master/lib/fetch.js"
click node_fetchurl "https://github.com/andris9/fetch/blob/master/lib/fetch.js"
click node_charset "https://github.com/andris9/fetch/blob/master/lib/fetch.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_fetchstream,node_redirects toneBlue
class node_gzip,node_fetchurl,node_charset,node_biskviit,node_file toneAmber
class node_cookiejar,node_httpserver toneMint
class node_caller,node_streamconsumer,node_callback,node_iconv toneIndigo
```

## Components

### Request and streaming

- [FetchStream](https://github.com/andris9/fetch/blob/master/lib/fetch.js): `lib/fetch.js`
- [Redirect handling](https://github.com/andris9/fetch/blob/master/lib/fetch.js): `lib/fetch.js`

### Response processing

- [Gzip/deflate decode](https://github.com/andris9/fetch/blob/master/lib/fetch.js): `lib/fetch.js`
- [fetchUrl](https://github.com/andris9/fetch/blob/master/lib/fetch.js): `lib/fetch.js`
- [Charset decoding](https://github.com/andris9/fetch/blob/master/lib/fetch.js): `lib/fetch.js`

### Cookie management

- [CookieJar](https://github.com/andris9/fetch/blob/master/lib/cookiejar.js): `lib/cookiejar.js`

### Other components

- **Library caller**
- **HTTP(S) server**
- **biskviit store**
- **Stream consumer**
- **Result callback**
- **iconv-lite**
- **File output**

## Connections

- Library caller → FetchStream: starts stream (evidence: `README.md`)
- Library caller → fetchUrl: requests body (evidence: `README.md`)
- FetchStream → HTTP(S) server: issues requests (evidence: `lib/fetch.js`)
- FetchStream → CookieJar: gets and sets cookies (evidence: `lib/fetch.js`)
- CookieJar → biskviit store: delegates storage (evidence: `lib/cookiejar.js`)
- FetchStream → Redirect handling: handles redirects (evidence: `lib/fetch.js`)
- FetchStream → Gzip/deflate decode: decodes content (evidence: `README.md`)
- FetchStream → Stream consumer: emits stream (evidence: `README.md`)
- Stream consumer → File output: pipes content (evidence: `README.md`)
- fetchUrl → FetchStream: creates stream (evidence: `lib/fetch.js`)
- fetchUrl → Charset decoding: decodes body (evidence: `lib/fetch.js`)
- Charset decoding → iconv-lite: uses decoder (evidence: `lib/fetch.js`)
- fetchUrl → Result callback: returns body and metadata (evidence: `README.md`)
