@fetchbrain.com/sdk 0.7.0 → 0.7.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 CHANGED
@@ -96,7 +96,7 @@ That's it. Your handler only runs when there's something new to learn.
96
96
  - **Auto-learning** — every successful scrape teaches the brain, free
97
97
  - **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))
98
98
  - **Built for scale** — request batching and deduping for high-concurrency crawlers
99
- - **Crawlee-native** — CheerioCrawler, PlaywrightCrawler, and friends
99
+ - **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)
100
100
  - **Transparent** — see exactly [what leaves your process](#what-leaves-your-process). Telemetry is **opt-in** and pseudonymous ([details](#telemetry-opt-in))
101
101
  - **TypeScript-first** — full types, ESM + CJS; an API key is the only required option
102
102
 
@@ -702,6 +702,10 @@ Nothing. Learning is never billed — you pay only for known recalls.
702
702
  That's what memory depth is for: pick `fresh` (1h) for volatile data, or
703
703
  set `refreshOnRebuild` to re-learn whenever you ship a new scraper build.
704
704
 
705
+ **Can I call `enhance()` twice on the same crawler?**
706
+ Don't — each call wraps the crawler again, so recalls and learns run twice.
707
+ Enhance a crawler once, right after you create it.
708
+
705
709
  ## Contributing
706
710
 
707
711
  Issues and PRs welcome — see [open issues](https://github.com/fetchbrain-com/sdk/issues).
package/dist/index.js CHANGED
@@ -1075,6 +1075,13 @@ function isIterableInputRejection(err) {
1075
1075
  const message = err instanceof Error ? err.message : String(err);
1076
1076
  return /iterable|\barray\b/i.test(message);
1077
1077
  }
1078
+ function resolveHandlerKey(crawler) {
1079
+ const c = crawler;
1080
+ for (const key of ["userProvidedRequestHandler", "requestHandler", "userDefinedRequestHandler"]) {
1081
+ if (typeof c[key] === "function") return key;
1082
+ }
1083
+ return null;
1084
+ }
1078
1085
  function buildRawRequest(request) {
1079
1086
  const r = request;
1080
1087
  let headers;
@@ -1185,7 +1192,14 @@ var FetchBrain = class {
1185
1192
  startTime: 0
1186
1193
  };
1187
1194
  const pendingLearns = /* @__PURE__ */ new Set();
1188
- const originalHandler = crawler.requestHandler || crawler.userDefinedRequestHandler;
1195
+ if ("adaptiveRequestHandler" in crawler) {
1196
+ logger.warn(
1197
+ "AdaptivePlaywrightCrawler is not supported yet \u2014 FetchBrain left this crawler unmodified"
1198
+ );
1199
+ return Object.assign(crawler, { fetchBrain: client });
1200
+ }
1201
+ const handlerKey = resolveHandlerKey(crawler);
1202
+ const originalHandler = handlerKey ? crawler[handlerKey] : void 0;
1189
1203
  if (!originalHandler) {
1190
1204
  logger.warn("No request handler found on crawler, returning unmodified");
1191
1205
  return Object.assign(crawler, { fetchBrain: client });
@@ -1432,12 +1446,8 @@ var FetchBrain = class {
1432
1446
  }
1433
1447
  };
1434
1448
  const crawlerAny = crawler;
1435
- if ("requestHandler" in crawlerAny) {
1436
- crawlerAny.requestHandler = wrappedHandler;
1437
- }
1438
- if ("userDefinedRequestHandler" in crawlerAny) {
1439
- crawlerAny.userDefinedRequestHandler = wrappedHandler;
1440
- }
1449
+ crawlerAny[handlerKey] = wrappedHandler;
1450
+ logger.debug(`Wrapped ${handlerKey}`);
1441
1451
  if (typeof crawlerAny.run === "function") {
1442
1452
  const originalRun = crawlerAny.run.bind(crawlerAny);
1443
1453
  crawlerAny.run = async (...args) => {