Repository navigation
polite-crawler 0.1.0
The first version. It needs Node.js 22.19 or later, and it is ESM only.
- The library.
openCrawler({ dir })givesfetch,find,read,verify,versionsandreindexover one folder. Each method gives a promise. The root entry point also exports the parts of the runtime for an application. Examples arecreateRuntime,Crawler,DocIndex,SqliteHostStateStore,fetchAndIndex,reindex,readDocument,verifyQuoteand the rule for a held-back page.- A result of
fetchgivescapture,fetched_at,from_cacheandheld_back. A result ofreadgives thecaptureand thefetched_atof its text. readandverifytakecapture, the text of one capture.verifytells the capture, the blob and the fetch time of the text that holds a quote.staleis true when only an earlier capture holds it.openCrawlertakesegress,politeness,hostState,clock,scope,onEventandextractors.
- A result of
- The command line.
polite-crawler fetch,find,read,verify,reindexandmcp. The--jsonoption gives output for programs.- The exit codes are 0 (done), 1 (not done: a page not fetched, no match, a quote not found, a document unknown or held back, or an error) and 2 (a wrong command line).
- The command checks the command line before it opens the store.
--versionand--helpload no part of the crawler. --dirandPOLITE_CRAWLER_DIRexpand~and resolve a relative path.read --user-providedreads a page that refuses AI input, for a URL that you gave yourself.reindexkeeps the documents of the recipes of another application.
- The MCP server.
polite-crawler mcpgives four tools over standard input and output:crawl_fetch,crawl_find,crawl_readandcrawl_verify. Its entry point ispolite-crawler/mcp, so the root entry point does not load the MCP SDK.- The text of each page comes in an
<untrusted-web-content>envelope with the URL, the capture id and the fetch date. A page cannot close its envelope. user_providedreleases held-back text only when the user confirms it (MCP elicitation), or when the operator permits it (POLITE_CRAWLER_ALLOW_USER_PROVIDED=1).- Each tool has descriptions of its parameters, an output schema and structured content. A result gives the age of each copy.
crawl_readgives the page metadata with the first range only, and the position of the next range.- A bad URL in
crawl_fetchis an error of that URL only, and a URL given twice is fetched one time. The server sends progress for each URL. - With no browser installed, the server turns rendering off at start and writes one note.
- The text of each page comes in an
- Politeness. The crawler obeys robots.txt (RFC 9309), and the rules for
Claude-Userwhen an assistant asks. It sends one request at a time to each host and obeysRetry-After. TheUser-Agentnames the crawler and links to a page for site owners.- The
contactoption adds an address to theUser-Agent. Only an email address goes into theFromheader (RFC 9110). - A 401 or a 403 stops the host for 1 hour, a challenge or a 402 for 24 hours, and five failures in sequence for 1 hour. A longer
Retry-Afterstops the host for that time, up to 30 days. - A client error, a 429 and a 503 do not count toward a stop. The backoff starts at 5 seconds and stops at 300 seconds, with random jitter.
- When robots.txt cannot be read, the crawler fetches nothing from the site and tries again after some minutes. A good copy from the last 30 days applies in that time. A 401 or a 403 for robots.txt means that the site has no file.
CrawlerOptions.politenesschanges the limits for each host or for some hosts, andhostStatekeeps the state of each host.crawler.resetHosts()removes the stops.
- The
- Egress. By default, the crawler fetches only
http:andhttps:URLs on public addresses, and it checks the address after DNS. In the headless browser, it checks each request of the page and each step of its redirects, up to the limit of a plain fetch.CrawlerOptions.egressand the flag--allow-privatepermit private addresses. - The store. Each fetch decision goes into a capture log, and each body goes into a store by SHA-256 hash. A SQLite FTS5 index answers later questions with no request. A quote check finds the exact span of a quote, with the capture id to cite.
- The index keeps each capture of a document as a version: the latest capture is the current text. A page that asks not to be AI input stops being held back when a later capture does not ask it. The earlier versions that asked it stay held back, also from a quote check of the earlier texts.
- The search folds case, accents and Unicode forms, reads phrases and prefixes, and finds Chinese, Japanese and Thai words.
- The quote check ignores soft hyphens, zero-width characters and the differences of NFKC, NFC and NFD.
reindexbuilds a new index beside the old one, so a rebuild that fails keeps the old index. It counts damaged lines, missing bodies and readers that fail, and it does not stop at them.- At open, the crawler rebuilds a stale index and gives an index that is behind the log the captures that it missed.
catchUpIndexdoes the same aftercreateRuntime. A runtime takes anyReadingIndex. - The log and the bodies go to the disk before a fetch finishes. Two processes can use one store: the index waits for a lock, and
hosts.sqlitekeeps one pace for each host. - A capture shows
REDACTEDin place of a user name, a password and the values of secret query parameters. - The package publishes the JSON Schema of a capture record:
polite-crawler/record.schema.json. - A body longer than the limit keeps its first part, and the capture says
truncated. A body that does not download or decode gives the outcomeerror. FetchRequest.maxAgeMscan be"auto", the freshness that the server gave.
- Reading. The crawler reads JSON-LD, the visible text, the links and PDF files with no browser. With the optional peer
playwright-core, it opens a headless browser for a page that shows almost nothing without JavaScript.- The text and the links leave out hidden text:
display: nonein astyleattribute,hidden,aria-hidden="true"and a font size of zero. decodeBodyandbodyEncodingdecode a body in the encoding that it declares.PageExtractgivescanonical,redirect,blockedandnotes.Hoursgivespartialandseason.PdfOptionssets the limits of the PDF reader.- A renderer throws
RenderRefusedfor a page that it did not render, and the crawler does not count it as a failure of the host. maxRenders(default 2) limits the pages that the browser renders at one time.- The default renderer checks one time that the browser is installed. With no browser, each page that needs JavaScript gets a note at once, with no wait and no error capture.
- The text and the links leave out hidden text:
- Usage signals. When a page asks not to be AI input (
ai-input=no), the crawler keeps the page but holds back its text from the AI reader. The read gives no title, no text and only the structural metadata, the quote check gives no match, and the search leaves the page out.- The capture records the RSL
Licenselines of robots.txt and the TDMReptdm-reservationandtdm-policyheaders and<meta>tags. They do not change what the crawler does.
- The capture records the RSL
- The parts. The entry points
polite-crawler/robots,/hours,/html,/classify,/politenessand/signalshave no Node.js dependency, so a browser can import them.