Documentation
¶
Overview ¶
Package crawl turns any web page into clean markdown a model can read.
The engine is three orthogonal steps, each in its own file and each testable without the others:
Fetch (this file) URL → HTML bytes, over guarded HTTP extract (extract.go) HTML → the readable subtree + metadata render (markdown.go) node → markdown
Read wraps Fetch with an archive (archive.go) and, for the pages a static read cannot see, a headless browser (browser.go).
Fetching untrusted URLs is the security boundary ¶
The caller supplies the URL, so this is a server-side request forgery primitive by construction. Run inside a network, a naive fetcher reaches private services and a cloud metadata endpoint on 169.254.169.254 that hands out credentials to anyone who asks — a credential exfiltration hole, not a bug. So this package carries the guard itself.
So: only http/https, and every address actually dialed must be a public unicast address. The check is in the DIALER, not on the hostname, because checking a hostname up front and then letting the transport resolve it again is a time-of-check/time-of-use gap — DNS rebinding walks straight through it. Dialing is the only moment the real destination is known, and that is where it is enforced. Redirects are followed but re-enter the same dialer, so a public URL that 302s to 169.254.169.254 is refused at the hop that matters.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBlocked = errors.New("crawl: refused to dial a non-public address")
ErrBlocked is returned when a URL resolves to an address this package refuses to dial. It is deliberately distinguishable: a caller may want to report a blocked fetch differently from a site that was merely down, and a log line that cannot tell them apart makes an attack look like an outage.
Functions ¶
func Bind ¶
func Bind(vfs Store)
Bind installs the process-wide archive. A nil store leaves crawling fully functional and unarchived, which is the correct posture for a deployment with no object store configured — fail soft, never fail closed, since nothing about answering a fetch depends on keeping it.
func BindMeter ¶
func BindMeter(m Meter)
BindMeter installs the process-wide meter. A nil meter renders for free.
func BindRenderer ¶
func BindRenderer(r Renderer)
BindRenderer installs the process-wide renderer. An empty URL turns rendering off.
Types ¶
type Archive ¶
type Archive struct {
// contains filtered or unexported fields
}
Archive keeps pages under a scope.
func (*Archive) Get ¶
Get returns a previously archived page.
Any failure — missing, unreachable, corrupt — is reported the same way: not found. A caller's only sensible response to each is to fetch, so distinguishing them at this client would create a decision nobody makes differently.
func (*Archive) Put ¶
Put files a page under the scope, keyed by the url that was REQUESTED.
requested is a separate argument from p.URL on purpose: Fetch returns where the page landed after redirects, and keying by that would file every redirecting page under an address no caller ever asks for. Get would then miss it forever — a cache that silently never hits for exactly the pages that redirect, which is a large share of the real web.
type Charge ¶
type Charge interface {
// Debit bills the render. It is called at most once, after the render succeeded.
Debit()
// Release returns whatever Debit did not take. It is always called.
Release()
}
Charge is one reserved render.
type Meter ¶
Meter pays for a render.
Fetching a page is one HTTP GET and costs nothing worth metering. Rendering one boots a browser, runs the page's scripts and holds it for seconds, so a host that sells crawling prices the render and only the render.
Reserve is asked BEFORE the browser is, so a caller who cannot cover a render is refused having spent nothing — and the refusal only costs the render: the static page still answers. The Charge is debited once a page came back and released on every exit, so failed work is never billed.
type Page ¶
type Page struct {
// URL is the FINAL url after redirects, not the one requested. Callers cite it,
// so it must be where the content actually came from.
URL string
// Title is the document title, best-effort from <title> or og:title.
Title string
// Markdown is the readable content. Empty is a legitimate result for a page
// that is genuinely all chrome; it is not an error.
Markdown string
// Metadata carries what the document said about itself (description, og:*,
// language, status). Map-shaped because it is passed through to a JSON contract
// that predates this package and accepts arbitrary keys.
Metadata map[string]any
}
Page is one crawled document. It is what every surface in front of this package renders, so it holds the extracted content and the provenance needed to cite it — not the raw HTML, which no caller has wanted and which would keep the whole response alive in memory.
func Fetch ¶
Fetch retrieves one URL and returns its readable content.
It returns an error only when there is nothing to read — a refused address, an unreachable host, a non-2xx status, a body that is not a document. A page that is reachable but yields little content is a Page with short Markdown, because "this page is mostly navigation" is a true answer and a caller that treats it as a failure would retry forever.
func Read ¶
Read is the entry point: return the archived page if we have one, otherwise fetch it and keep it.
A hit skips the network entirely, which is the point — the same URL is read repeatedly across a research loop, a re-ask, and a re-index, and paying for it once is both faster and politer to the origin.
type Renderer ¶
Renderer is where a page the static read cannot see gets rendered: a headless browser answering POST {URL}/crawl with {success, results:[{url, success, markdown, metadata}]}. Token, when set, is sent as a bearer.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
crawl
command
Command crawl reads web pages as markdown, or serves the crawl API.
|
Command crawl reads web pages as markdown, or serves the crawl API. |
|
Package serve is the crawl engine as an API: POST /v1/crawl on a zip app, which is also an MCP tool (read_page), a CLI command and an OpenAPI operation.
|
Package serve is the crawl engine as an API: POST /v1/crawl on a zip app, which is also an MCP tool (read_page), a CLI command and an OpenAPI operation. |