@fetchbrain.com/sdk 0.11.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -86
- package/dist/collector-KEC7N6EY.mjs +1035 -0
- package/dist/collector-KEC7N6EY.mjs.map +1 -0
- package/dist/index.d.ts +70 -109
- package/dist/index.js +1913 -337
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +824 -333
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.ts +2 -7
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/index.mjs.map +1 -1
- package/dist/{types-BgqTjfAN.d.mts → types-hqajSmWO.d.ts} +132 -68
- package/package.json +5 -3
- package/dist/index.d.mts +0 -449
- package/dist/mock/index.d.mts +0 -84
- package/dist/types-BgqTjfAN.d.ts +0 -396
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ That's it. Your handler only runs when there's something new to learn.
|
|
|
94
94
|
- **Never breaks your crawl** — the circuit breaker degrades gracefully; an outage costs you optimization, not your crawl (one deliberate exception: [expired memory](#recall-at-enqueue-time-prerecall))
|
|
95
95
|
- **Built for scale** — request batching and deduping for high-concurrency crawlers
|
|
96
96
|
- **Crawlee-native** — CheerioCrawler, PlaywrightCrawler, and friends (browser crawlers — Playwright, Puppeteer — from 0.7.1; earlier versions did not apply FetchBrain to them. `AdaptivePlaywrightCrawler` is not supported yet)
|
|
97
|
-
- **Transparent** — see exactly [what leaves your process](#what-leaves-your-process).
|
|
97
|
+
- **Transparent** — see exactly [what leaves your process](#what-leaves-your-process).
|
|
98
98
|
- **TypeScript-first** — full types, ESM + CJS; an API key is the only required option
|
|
99
99
|
|
|
100
100
|
## Who is it for?
|
|
@@ -243,6 +243,7 @@ interface FetchBrainConfig {
|
|
|
243
243
|
scraper?: string; // Name this scraper so it gets its own memory (or set FETCHBRAIN_SCRAPER_ID)
|
|
244
244
|
baseUrl?: string; // API URL (default: https://api.fetchbrain.com)
|
|
245
245
|
memory?: MemoryDepth; // How far back the brain recalls (default: "recent")
|
|
246
|
+
recall?: boolean; // Recall known pages and skip their fetches; false = fetch and learn everything (default: true)
|
|
246
247
|
learning?: boolean; // Teach the brain from scraped data (default: true)
|
|
247
248
|
alwaysRun?: boolean | string | string[]; // Handlers that still run when a request is known (default: false)
|
|
248
249
|
skipLabels?: string[]; // Labels that bypass FetchBrain entirely (default: none)
|
|
@@ -250,7 +251,7 @@ interface FetchBrainConfig {
|
|
|
250
251
|
onRecalled?: (event: RecalledEvent) => void | Promise<void>; // Per-record hook when recalled data is delivered
|
|
251
252
|
extractForLearning?: (data: unknown) => Record<string, unknown>; // Transform each record before it is learned
|
|
252
253
|
refreshOnRebuild?: boolean; // Scope learn, recall and ask to the current build (*_BUILD_ID env var) (default: false)
|
|
253
|
-
timeout?: number; // Handler-time recall
|
|
254
|
+
timeout?: number; // Handler-time recall and stats timeout in ms, 1-2147483647 (default: 1500)
|
|
254
255
|
askTimeout?: number; // ask() timeout in ms, 1-2147483647 (default: 60000); a per-call `timeout` wins
|
|
255
256
|
batch?: { maxSize?: number; maxWait?: number }; // Request batching (default: 50 items / 50ms)
|
|
256
257
|
circuitBreaker?: {
|
|
@@ -258,7 +259,6 @@ interface FetchBrainConfig {
|
|
|
258
259
|
resetTimeout?: number; // ms before a retry is allowed (default: 30000)
|
|
259
260
|
successThreshold?: number; // Successes to close it again (default: 1)
|
|
260
261
|
};
|
|
261
|
-
telemetry?: TelemetryConfig; // Opt-in telemetry (default: { enabled: false })
|
|
262
262
|
changeDetection?: { enabled?: boolean; ignoreFields?: string[] }; // Relearn known alwaysRun pages and report changes (default: off)
|
|
263
263
|
reportRuns?: boolean; // Send the end-of-run report: run id, timestamps, counts per label (default: true)
|
|
264
264
|
learnUrlField?: string; // Accepted but not currently applied
|
|
@@ -405,7 +405,7 @@ FetchBrain.enhance(crawler, {
|
|
|
405
405
|
For dynamic requests that must always run live and aren't worth remembering
|
|
406
406
|
(search results, pagination), use `skipLabels` instead: where `alwaysRun`
|
|
407
407
|
still recalls (and can learn), `skipLabels` bypasses FetchBrain entirely —
|
|
408
|
-
no recall round-trip, no learn
|
|
408
|
+
no recall round-trip, no learn — so those requests run at
|
|
409
409
|
native speed:
|
|
410
410
|
|
|
411
411
|
```typescript
|
|
@@ -459,6 +459,30 @@ its own models. If your provider fails, you get the `sources` without an
|
|
|
459
459
|
`BYOK_PROVIDER_FAILED`. The ask still costs 1 credit. Your question and the
|
|
460
460
|
matching sources are sent to your provider under your agreement with them.
|
|
461
461
|
|
|
462
|
+
### Choose what Ask reads
|
|
463
|
+
|
|
464
|
+
Each source says how old it is: `version` (1, then +1 each time the page
|
|
465
|
+
changed) and `learnedAt` (when it was last learned). Three options narrow
|
|
466
|
+
what an ask reads; leave them out to use the scraper's Ask settings from the
|
|
467
|
+
dashboard:
|
|
468
|
+
|
|
469
|
+
```typescript
|
|
470
|
+
const res = await client.ask("what changed on the blue widget?", {
|
|
471
|
+
answer: true,
|
|
472
|
+
memory: "recent", // skip pages last learned over 24h ago ("any" = no limit)
|
|
473
|
+
build: "latest", // each scraper's newest build only ("all" = every build, or an exact build id)
|
|
474
|
+
versions: "all", // also read past versions of pages that changed
|
|
475
|
+
});
|
|
476
|
+
console.log(res.skippedOld); // sources left out by memory or build
|
|
477
|
+
for (const s of res.sources) console.log(s.version, s.learnedAt, s.pastVersion ?? false);
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
Past versions are off by default. Turn them on for a key in the dashboard
|
|
481
|
+
(API keys, paid plans); from then on, when a page's content changes, its
|
|
482
|
+
earlier version is kept for 30 days. `versions: "all"` reads them with one
|
|
483
|
+
more query, so an answer can use one more Ask credit. On a key without past
|
|
484
|
+
versions, `versions: "all"` comes back `status: "rejected"`.
|
|
485
|
+
|
|
462
486
|
An answer can take tens of seconds, so `ask()` waits up to 60s by default.
|
|
463
487
|
Change that for every call with the `askTimeout` option, or for one call
|
|
464
488
|
with `timeout` (ms), which wins over `askTimeout`. Both accept 1 to 2147483647 ms
|
|
@@ -571,7 +595,7 @@ Timeouts per call type:
|
|
|
571
595
|
| Recall at enqueue (`preRecall`, bulk) | 3s per chunk of up to 100 requests |
|
|
572
596
|
| Learn (batched, sent in the background) | 5s |
|
|
573
597
|
| `ask()` | `askTimeout` option (default 60s), or `timeout` per call |
|
|
574
|
-
| `stats()`
|
|
598
|
+
| `stats()` | `timeout` option, default 1.5s |
|
|
575
599
|
|
|
576
600
|
A FetchBrain outage costs you *optimization*, not your crawl: SDK calls
|
|
577
601
|
degrade gracefully instead of throwing into it. The one deliberate
|
|
@@ -667,8 +691,8 @@ the fetch, so those pages can't skip it) or when `preRecall` is `false`. A page
|
|
|
667
691
|
through `context.pushData` or the SDK's `pushData` export (pages written
|
|
668
692
|
straight to Crawlee's static `Dataset.pushData` are counted that way too), and
|
|
669
693
|
retries count once per attempt. The report is sent with the same standard
|
|
670
|
-
headers as every other call (see [What leaves your process](#what-leaves-your-process)). It is what powers the unhealthy-run warnings above. It is
|
|
671
|
-
|
|
694
|
+
headers as every other call (see [What leaves your process](#what-leaves-your-process)). It is what powers the unhealthy-run warnings above. It is on by
|
|
695
|
+
default; set `reportRuns: false` to
|
|
672
696
|
turn it off. It is bounded to a few seconds at the end of a run and can never
|
|
673
697
|
fail your crawl.
|
|
674
698
|
|
|
@@ -677,7 +701,7 @@ You can browse what changed in the FetchBrain dashboard, or fetch it from
|
|
|
677
701
|
|
|
678
702
|
## What leaves your process
|
|
679
703
|
|
|
680
|
-
Every API call (recall, learn, ask, stats,
|
|
704
|
+
Every API call (recall, learn, ask, stats, run report) sends these headers:
|
|
681
705
|
|
|
682
706
|
| Header | Value |
|
|
683
707
|
| --- | --- |
|
|
@@ -717,86 +741,11 @@ Other headers (for example
|
|
|
717
741
|
forwarded as-is — keep secrets out of them on requests FetchBrain handles,
|
|
718
742
|
or route those requests through `skipLabels`, which sends nothing for them.
|
|
719
743
|
|
|
720
|
-
`ask()` sends your question and options.
|
|
721
|
-
[opt in](#telemetry-opt-in). At the end of a run the SDK also sends a small
|
|
744
|
+
`ask()` sends your question and options. At the end of a run the SDK also sends a small
|
|
722
745
|
[run report](#run-report): run id, start/end timestamps, scraped/learned/recalled
|
|
723
746
|
counts, requests skipped, late recalls and bypassed counts, per-label page counts, and a build id only with `refreshOnRebuild`
|
|
724
747
|
(`reportRuns: false` turns it off).
|
|
725
748
|
|
|
726
|
-
## Telemetry (opt-in)
|
|
727
|
-
|
|
728
|
-
Telemetry is **off by default.** Nothing in this section is collected or
|
|
729
|
-
sent unless you opt in:
|
|
730
|
-
|
|
731
|
-
```typescript
|
|
732
|
-
FetchBrain.enhance(crawler, {
|
|
733
|
-
apiKey: process.env.FETCHBRAIN_API_KEY,
|
|
734
|
-
telemetry: { enabled: true },
|
|
735
|
-
});
|
|
736
|
-
```
|
|
737
|
-
|
|
738
|
-
**It's pseudonymous, not anonymous.** Entries are sent to the FetchBrain
|
|
739
|
-
API with your API key and the [headers above](#what-leaves-your-process),
|
|
740
|
-
so they're linked to your account. They're used to improve FetchBrain's
|
|
741
|
-
recommendations (proxy recommendations, block detection, configuration
|
|
742
|
-
tuning); aggregated statistics may inform recommendations for other users.
|
|
743
|
-
|
|
744
|
-
**How long it's kept:** raw entries are deleted about 30 days after they're
|
|
745
|
-
received (a daily cleanup), and all of your entries are deleted when you
|
|
746
|
-
delete your account (a very large history may take the daily cleanup a few
|
|
747
|
-
days to finish, never longer than the 30 days above).
|
|
748
|
-
Per-domain aggregate counts (requests, successes, blocks and block rate for a
|
|
749
|
-
hostname, with no URLs and no account link) are kept. Per-domain statistics
|
|
750
|
-
are aggregated per domain, not per account: entries from every account that
|
|
751
|
-
scraped a hostname add to the same counts, and they are not deleted with your
|
|
752
|
-
account.
|
|
753
|
-
|
|
754
|
-
**When it's collected:** one entry per request FetchBrain handles
|
|
755
|
-
(`skipLabels` requests are excluded), after your handler finishes — or
|
|
756
|
-
whenever the request fails inside the SDK's wrapper, including failures
|
|
757
|
-
before your handler runs (e.g. [expired memory](#recall-at-enqueue-time-prerecall))
|
|
758
|
-
and on known requests. Known requests that are skipped successfully produce
|
|
759
|
-
no entry. A failure entry carries only the "always" fields (minus session),
|
|
760
|
-
`retryCount`, timing, and `blocked: true` with a `blockType` if one matches.
|
|
761
|
-
Entries are buffered in your process and sent in batches (every 50 entries,
|
|
762
|
-
every 30 seconds, and when the crawl finishes); send failures are ignored and
|
|
763
|
-
never affect your crawl.
|
|
764
|
-
|
|
765
|
-
**What's sent:**
|
|
766
|
-
|
|
767
|
-
| Field | Sent when | What it is |
|
|
768
|
-
| --- | --- | --- |
|
|
769
|
-
| `domain` | always | the URL's hostname, in plain text (e.g. `shop.example.com`) |
|
|
770
|
-
| `urlHash` | always | first 16 hex characters of an **unsalted** SHA-256 of the full URL, including the query string. Anyone with a candidate URL can compute the same hash, so it identifies URLs that are guessable. |
|
|
771
|
-
| `urlPattern` | always | the URL path with numeric segments, UUIDs and alphanumeric tokens of 20+ characters replaced by `*`; the query string is dropped. Everything else — slugs, usernames, words — is sent as-is (e.g. `/users/jane-doe/orders/*`). |
|
|
772
|
-
| `timestamp` | always | when the entry was collected |
|
|
773
|
-
| `crawlerType` | always | crawler class name, lowercased, without `Crawler` (e.g. `cheerio`) |
|
|
774
|
-
| `requestLabel` | always | the request's Crawlee label, if set |
|
|
775
|
-
| `sessionAge`, `sessionErrorScore` | on success, if the request has a Crawlee session | the session's usage count and error score |
|
|
776
|
-
| `statusCode` | `sharePerformance`, on success | HTTP status |
|
|
777
|
-
| `retryCount` | `sharePerformance` | Crawlee retry count |
|
|
778
|
-
| `responseTimeMs`, `totalTimeMs` | `sharePerformance` | time in ms from when the SDK starts handling the request (including recall) until your handler finishes or the request fails |
|
|
779
|
-
| `contentSize` | `sharePerformance`, on success | response body length, when the crawler exposes it |
|
|
780
|
-
| `blocked`, `blockType` | `shareSitePatterns` | `blocked` is true for status 403, 429 or 5xx, or when the request failed inside the SDK wrapper for any reason; `blockType` is the status code (4xx/5xx) or a category matched in the error message (`captcha`, `timeout`, `blocked`, `429` for "rate limit") — the message itself isn't sent |
|
|
781
|
-
| `proxyCountry`, `proxyType`, `proxySuccess` | `shareProxyInfo`, on success, if the request used a Crawlee proxy | the proxy's country code; `residential`, `mobile` or `datacenter`, guessed in your process from the proxy URL; and whether the status was below 400. The proxy URL, hostname, IP and credentials are not sent. |
|
|
782
|
-
|
|
783
|
-
**Sub-flags** — each defaults to `true` once telemetry is enabled; set one
|
|
784
|
-
to `false` to stop sending its fields. The "always" fields above are sent
|
|
785
|
-
whenever telemetry is enabled.
|
|
786
|
-
|
|
787
|
-
```typescript
|
|
788
|
-
telemetry: {
|
|
789
|
-
enabled: true, // default: false
|
|
790
|
-
sharePerformance: false, // default: true
|
|
791
|
-
shareSitePatterns: true, // default: true
|
|
792
|
-
shareProxyInfo: false, // default: true
|
|
793
|
-
}
|
|
794
|
-
```
|
|
795
|
-
|
|
796
|
-
**Never sent by telemetry:** the raw URL or query string, request or response
|
|
797
|
-
bodies, your scraped requests' headers or cookies, error messages, or proxy
|
|
798
|
-
URLs and IPs.
|
|
799
|
-
|
|
800
749
|
## Testing
|
|
801
750
|
|
|
802
751
|
`MockFetchBrain` ships with the SDK — seed it, run your tests, no network:
|
|
@@ -856,8 +805,7 @@ Your requests (minus `Authorization`, `Cookie` and `Set-Cookie` headers),
|
|
|
856
805
|
the data you choose to learn, your API key, and a small set of `X-FB-*`
|
|
857
806
|
headers including platform identifiers (env vars ending in `_ID`, plus
|
|
858
807
|
`NODE_ENV`/`REGION`/`CI`). The full list is in
|
|
859
|
-
[What leaves your process](#what-leaves-your-process).
|
|
860
|
-
telemetry, the fields in [Telemetry](#telemetry-opt-in) too.
|
|
808
|
+
[What leaves your process](#what-leaves-your-process).
|
|
861
809
|
|
|
862
810
|
**What does learning cost?**
|
|
863
811
|
Nothing. Learning is never billed — you pay only for known recalls.
|