crawl

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2026 License: Apache-2.0, MIT Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

crawl

Read any web page as clean markdown. Pure Go, no browser needed.

go install github.com/hanzoai/crawl/cmd/crawl@latest
crawl https://go.dev/doc/effective_go

In your app

page, err := crawl.Fetch(ctx, "https://example.com")
fmt.Println(page.Title, page.Markdown)

crawl.Read does the same and keeps every page in an archive you bind with crawl.Bind(store) — anything with Get and Put. A page already read under a scope is answered without touching the network.

As a service

crawl serve 127.0.0.1:8787
curl -s localhost:8787/v1/crawl -d '{"url":"https://example.com"}'

The same server is an MCP endpoint at /mcp (tool read_page) and publishes its OpenAPI document. serve.Mount(app, serve.Options{...}) puts the endpoint on your own zip app, with your own admission and scope. Hanzo Cloud serves it at https://api.hanzo.ai/v1/crawl.

crawl serve admits every caller. Keep it on loopback, or mount it behind your own authentication.

Safe by default

A crawler fetches addresses its callers choose. crawl dials only public unicast addresses — never loopback, private, link-local or cloud metadata — and checks at the dialer, so neither DNS rebinding nor a redirect can walk around it.

Pages that need a browser

A single-page app answers a plain GET with an empty shell. Bind a headless browser service with crawl.BindRenderer(crawl.Renderer{URL: ..., Token: ...}) and crawl asks it for the rendered page whenever the static read comes back too thin, keeping whichever is richer. crawl.BindMeter bills those renders.

License

Apache-2.0 OR MIT.

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

View Source
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

func (a *Archive) Get(ctx context.Context, s Scope, url string) (*Page, bool)

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

func (a *Archive) Put(ctx context.Context, s Scope, requested string, p *Page) error

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

type Meter interface {
	Reserve(ctx context.Context) (Charge, error)
}

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

func Fetch(ctx context.Context, raw string) (*Page, error)

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

func Read(ctx context.Context, s Scope, url string) (*Page, error)

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

type Renderer struct {
	URL   string
	Token string
}

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.

type Scope

type Scope struct {
	Org     string
	Project string
}

Scope is who a page belongs to. Both fields come from the VERIFIED principal — never from the request body — because they select the key prefix, and a caller-supplied prefix is a caller reading another tenant's corpus.

type Store

type Store interface {
	Put(ctx context.Context, key string, payload []byte) error
	Get(ctx context.Context, key string) ([]byte, error)
}

Store is the object store pages are kept in: keys in, bytes out.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL