@crawlbrulee/sdk 1.4.0 → 1.6.0

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
@@ -160,17 +160,18 @@ notes:
160
160
  }
161
161
  ```
162
162
 
163
- billing follows the site's status: 2xx and 4xx pages are billed, except 403, 407, 408, 429 and 451; 5xx pages are
164
- never billed. an unbilled page reports `total_credit_cost: 0`. errors are thrown when we could not return the page
163
+ billing follows the site's status: 2xx and 4xx pages are billed, except 403, 407, 408, 429, 444 and 451; 5xx pages
164
+ are never billed. an unbilled page reports `total_credit_cost: 0`. errors are thrown when we could not return the page
165
165
  — see [errors](#errors).
166
166
 
167
- - **`proxy`**: defaults to `auto` when omitted — it starts at the basic tier and escalates to advanced on failure,
168
- billed at the delivered tier. pass `'basic'` or `'advanced'` to pin a tier. on the response,
167
+ - **`proxy`**: defaults to `auto` when omitted. we start with the lightest way to get the page and step up our effort
168
+ on our own when a site needs more: a stronger proxy, a real browser, or both. billed at the delivered tier. pass `'basic'` or `'advanced'` to pin a tier. on the response,
169
169
  `response_meta.usage.proxy` reports the tier we resolved and used — never `'auto'`. see
170
170
  [proxies & location](https://crawlbrulee.com/docs/proxies) for what each tier does.
171
171
  - **`screenshot`**: custom `viewport.width`/`height` are integers in `[16, 10000]` and `device_scale_factor` is in
172
172
  `[1, 3]`; out-of-range values are rejected with a `400`. the `url` of the screenshot and of every slice is a signed
173
- link that expires 24 hours after the scrape, so download the image and keep the file, not the link. full capture
173
+ link that expires 24 hours after the scrape, so download the image and keep the file, not the link. set
174
+ `color_scheme: 'dark'` to ask the site for its dark theme (see [dark mode](#dark-mode)). full capture
174
175
  options: [screenshots](https://crawlbrulee.com/docs/scrape/screenshots).
175
176
  - **`extract.images`**: urls preserve their query string and resolve document-relative `src`s against the full page url
176
177
  (browser parity) — the same rules as `links`. every extract field is documented under
@@ -178,10 +179,12 @@ notes:
178
179
  - **`warnings`**: when we complete a scrape but something is worth flagging, the codes land on `page.warnings`, in three
179
180
  groups. capped output: `screenshot_truncated` (a long page exceeded the scrolling-screenshot height cap),
180
181
  `links_truncated` (more than 30 000 links), `inline_images_truncated` (more than 10 000 inline images),
181
- `raw_html_truncated` (more than 10 000 000 characters of body html), and `elements_truncated` (an `elements` value
182
- hit a limit, see [elements](#elements)). failed extraction of one section: `links_unavailable`,
183
- `inline_images_unavailable`, `metadata_unavailable`, and `screenshot_unavailable` (a screenshot was asked for, but the
184
- page came back without one) — the field comes back omitted or empty while the rest of the scrape succeeds, which is
182
+ `raw_html_truncated` (more than 10 000 000 characters of body html), `elements_truncated` (an `elements` value
183
+ hit a limit, see [elements](#elements)), and `pdf_single_page_overflow` (a `single_page` pdf was taller than
184
+ 15 000 px, so it goes on over more pages). failed extraction of one section: `links_unavailable`,
185
+ `inline_images_unavailable`, `metadata_unavailable`, `screenshot_unavailable` (a screenshot was asked for, but the
186
+ page came back without one) and `pdf_unavailable` (the same for a pdf, see [pdf](#pdf)). the field comes back
187
+ omitted or empty while the rest of the scrape succeeds, which is
185
188
  how you tell "the page had none" from "we couldn't read them". and a request notice: `custom_user_agent` means the request set its own
186
189
  `User-Agent` header, which can lower the success rate (see [headers and cookies](#headers-and-cookies)). they're stable, so you can switch on them — the union
187
190
  is exported as `ScrapeWarningCode`. `metadata_truncated` is retired and no longer sent; only results stored before
@@ -226,6 +229,60 @@ page.elements?.books // [{ title: 'The Night Garden', price: '€18.90' }, …]
226
229
  the request types are `ScrapeElements` and `ScrapeElementSpec`; each value on `page.elements` is a
227
230
  `ScrapeElementValue`. selector rules and limits: see [targeted extraction](https://crawlbrulee.com/docs/scrape/targeted-extraction).
228
231
 
232
+ #### pdf
233
+
234
+ `extract.pdf` saves the page as a pdf: a real document with selectable text and working links, backgrounds printed,
235
+ and no header or footer added. `{}` takes every default. it costs what a screenshot costs, and a pdf and a screenshot
236
+ in one request are billed once.
237
+
238
+ ```ts
239
+ const page = await crawlbrulee.scrape({
240
+ url: 'https://example.com/report',
241
+ extract: {
242
+ markdown: false,
243
+ metadata: false,
244
+ pdf: { paper: 'letter', landscape: true }, // or {} for every default
245
+ },
246
+ })
247
+
248
+ page.pdf?.url // signed link, expires 24 hours after the scrape
249
+ page.pdf?.bytes // size of the file
250
+ ```
251
+
252
+ the options are `layout` (`'pages'`, the default, on sheets of paper, or `'single_page'`, one tall page up to
253
+ 15 000 px), `paper` (`'a4'`, the default, or `'letter'`) and `landscape` (both only with `'pages'`), and `media`
254
+ (`'screen'`, the default, or `'print'`, the site's own print style). the request type is `PdfRequest`; the result is
255
+ `PdfResult`.
256
+
257
+ - the `url` is a signed link that expires 24 hours after the scrape, like a screenshot link. download the file and
258
+ keep it, not the link.
259
+ - when the pdf can't be made, `page.pdf` is left out, `warnings` carries `pdf_unavailable`, and the rest of the scrape
260
+ still comes back. the request then costs what it costs without the pdf.
261
+ - a pdf-only request, like the one above, turns every other format off. if its content type can't be made into a
262
+ pdf, it raises `ValidationError` (`422` `unsupported_pdf_output`), not billed. an async pdf-only job in that case,
263
+ or one that got no pdf, ends `failed` and is not billed (see
264
+ [`getScrapeResult`](#crawlbruleegetscraperesultjobid-options)).
265
+ - a cached copy of the page answers only when it has a pdf made with the same settings. `cache: { max_age: 0 }` always
266
+ makes a new one.
267
+
268
+ #### dark mode
269
+
270
+ set `color_scheme: 'dark'` on `extract.screenshot` to ask the site for its dark theme. a site without one looks the
271
+ same as with `'light'`, the default. it costs no extra credits.
272
+
273
+ ```ts
274
+ const page = await crawlbrulee.scrape({
275
+ url: 'https://example.com',
276
+ extract: {
277
+ screenshot: { type: 'full_page', color_scheme: 'dark' },
278
+ pdf: {}, // the pdf shows the dark theme too
279
+ },
280
+ })
281
+ ```
282
+
283
+ a pdf in the same request is made from the same page, so it is dark too. a pdf on its own is made with the light theme: the pdf
284
+ has no color setting of its own.
285
+
229
286
  #### zero data retention
230
287
 
231
288
  pass `zero_data_retention: true` on `scrape`, `scrapeAsync` or `map` to keep the result out of the shared cache. anything stored to deliver it is kept for 24 hours, then deleted. it adds 1 credit and must be enabled for your organization. see [zero data retention](https://crawlbrulee.com/docs/zero-data-retention).
@@ -280,6 +337,11 @@ look up the current state of an async job — `pending`, `running`, `done`, or `
280
337
  fetch the result of a completed async job. throws if the job hasn't finished yet. like `scrape()`, a job whose page came
281
338
  back as a 404 (or any other status) is a completed job — the result carries `page_status_code`.
282
339
 
340
+ a job that asked only for a screenshot, only for a pdf, or for both and nothing else, and got no capture, ends `failed`
341
+ and is not billed. its status says `failed`, and `getScrapeResult()` throws: `ValidationError` (`422`
342
+ `unsupported_screenshot_output` or `unsupported_pdf_output`) when the page's content type can't be captured, or a
343
+ `CrawlbruleeError` with `errorName: 'job_failed'` (`400`) otherwise. a job that got its capture completes as usual.
344
+
283
345
  a job's status and result are available for 24 hours after you submit it. after that, `getScrapeStatus()` and
284
346
  `getScrapeResult()` throw `NotFoundError`, the same as for an unknown `jobId`. screenshot links in the result expire at
285
347
  the same moment, however late you fetch it.
@@ -305,75 +367,137 @@ under [async scrape](https://crawlbrulee.com/docs/scrape/async).
305
367
 
306
368
  #### `crawlbrulee.map(request, options?)`
307
369
 
308
- build (or return a cached) link map for a website. combines sitemap discovery with the freshest cached homepage scrape
309
- when available.
370
+ build (or return a cached) link map of a site from its sitemaps, plus the links on its home page. a plain map reads
371
+ the site's top sitemap files and two levels of nested ones, and returns its pages.
310
372
 
311
373
  ```ts
312
374
  const result = await crawlbrulee.map({
313
375
  url: 'https://example.com',
314
376
  sitemap_only: false,
315
377
  types: { internal: true, internal_subdomains: false, external: false },
316
- max_urls: 5_000,
378
+ max_results: 5_000,
317
379
  page: 1,
318
380
  limit: 1_000,
319
381
  })
320
382
 
321
- console.log(result.links.length, 'urls on page 1 of', result.response_meta.pagination.total_pages)
383
+ for (const link of result.links) {
384
+ console.log(link.url, link.type, link.lastmod) // 'page', 'sitemap' or 'sitemap_index'; lastmod is a UTC date and time, or null
385
+ }
386
+ console.log(result.response_meta.pagination.total_pages, 'pages of results')
322
387
  console.log(result.response_meta.usage.total_credit_cost, 'credits charged') // usage accounting, alongside pagination + truncation
323
388
  ```
324
389
 
325
- - **`max_urls`** defaults to `5_000` and maxes out at `100_000`. discovery _stops_ at this number, so a smaller value
326
- is a cheaper and faster crawl, not just a shorter answer.
390
+ - **`max_results`** defaults to `5_000` and maxes out at `100_000`. it counts only the kind you ask for in `entries`.
391
+ discovery _stops_ at this number, so a smaller value is a faster and lighter map, not just a shorter answer.
327
392
  - **`limit`** (urls per page) defaults to `5_000` and maxes out at `10_000`.
328
- - the sdk sends only the fields you pass — omit `max_urls` or `limit` and the server applies its own default.
393
+ - **`entries`**: `'pages'` (the default), `'sitemaps'` (sitemap files, of type `'sitemap'` or `'sitemap_index'`) or
394
+ `'all'` (both).
395
+ - **`sitemap_url`**: start from this sitemap file instead of the site's own top sitemap files. it must be on the same
396
+ site as `url`, or on a host the site's robots.txt names, such as a CDN.
397
+ - **`depth`**: how many levels of nested sitemaps to follow: `0` to `3`, default `3`. the sdk passes the number
398
+ through; a higher value raises `ValidationError`. `depth: 1` with `entries: 'sitemaps'` lists the site's top sitemap
399
+ files and every file they list, in one call. to go deeper, read a listed file with `sitemap_url`, or ask with a
400
+ higher depth.
401
+ - **`modified_since`**: a date such as `'2026-11-01'` (the start of that day in UTC), or an ISO 8601 date and time
402
+ with a zone, such as `'2026-11-01T08:00:00+01:00'`. only entries whose
403
+ `lastmod` is at or after it come back, so an entry with no `lastmod` is left out. every sitemap file is read
404
+ whatever its own `lastmod`, and each entry returned, a page or a sitemap file, is judged by its own `lastmod`: with
405
+ `entries: 'sitemaps'`, a file comes back when its own `lastmod` matches, not when a page in it does, so ask with
406
+ `entries: 'pages'` to find the pages that changed. such a map is always fresh, so `cache.max_age` does nothing. see
407
+ [find what changed since a date](https://crawlbrulee.com/docs/map/sitemaps#find-what-changed-since-a-date).
408
+ - the sdk sends only the fields you pass: omit `max_results` or `limit` and the server applies its own default.
409
+ - `max_urls` and `truncation.total_before_max_urls` are the old names of `max_results` and
410
+ `truncation.total_before_max_results`. they still work and always match the new ones, and the api removes them on
411
+ 2027-02-01. when you send both, only `max_results` is used.
412
+
413
+ every link is `{ url, type, lastmod }`. `type` is `'page'`, `'sitemap'` or `'sitemap_index'`. a `'sitemap_index'` is a
414
+ sitemap file that this map found to be a sitemap index (a file that lists other sitemap files), whether it read all of
415
+ it, part of it, or only its start; it can also be in `unread_sitemaps` when the map did not read all of it. any other
416
+ sitemap file is a `'sitemap'`: one that lists pages, or one whose kind the map did not learn (past `depth`, or it could
417
+ not be read). you can read either kind with `sitemap_url` in another call (on another site, only when the site's
418
+ robots.txt names its host). a link found on the home page is a `'page'` with `lastmod: null`. the site root is
419
+ listed only when a sitemap lists it. a map with `modified_since` or `sitemap_url` never adds home page links.
329
420
 
330
421
  `result.response_meta` carries `usage` alongside the map-specific `pagination` and `truncation` blocks. map usage has the
331
422
  same fields as scrape usage minus slicing, since a map makes no screenshots: `total_credit_cost` (=
332
423
  `engine_credit_cost × proxy_multiplier + zero_data_retention_credit_cost`), `engine_credit_cost` (1 `http`, 0 `cache`),
333
424
  `proxy_multiplier`, `zero_data_retention_credit_cost` (0 or 1), the billed
334
425
  `engine` and the resolved `proxy`. an empty map is free
335
- when the site answered only with statuses we don't bill (a `5xx`, for example) or not at all.
426
+ when the site answered only with statuses we don't bill (a `5xx`, for example) or not at all. when the site answers
427
+ `429` for the file in `sitemap_url`, for its robots.txt or for one of its top sitemap files, the map raises
428
+ `RateLimitError` and is not billed.
336
429
 
337
430
  a map has no `page_status_code`: it reads several pages (sitemaps and the home page), so one status would not describe
338
431
  it.
339
432
 
340
433
  #### did the map miss pages?
341
434
 
342
- a map that stopped at your own `max_urls` comes back with exactly `max_urls` links and `response_capped: false` — the
343
- signal that more exists is `discovery_cap_reason`, not the capped flags:
435
+ a map that stopped at your own `max_results` has `response_capped: false`. the signal that more exists is
436
+ `discovery_cap_reason`, not the capped flags:
344
437
 
345
438
  ```ts
346
439
  const { truncation } = result.response_meta
347
440
 
348
441
  if (truncation.discovery_capped) {
349
442
  // the site has more pages than this map lists
350
- // 'max_urls' | 'time' | 'file_budget' | 'depth' | 'file_size' | 'unread_files'
443
+ // 'max_results' | 'depth' | 'time' | 'file_budget' | 'file_size' | 'unread_files'
351
444
  console.log('stopped by:', truncation.discovery_cap_reason)
352
445
  console.log(truncation.sitemaps_skipped, 'sitemap files skipped or partly read')
446
+ console.log(truncation.unread_sitemaps) // sitemap files this call did not read whole
353
447
  }
354
448
 
355
- if (truncation.discovery_cap_reason === 'max_urls') {
356
- // the only reason you can fix from the request — ask again with a higher max_urls
449
+ if (truncation.discovery_cap_reason === 'max_results') {
450
+ // ask again with a higher max_results
357
451
  }
358
452
 
359
453
  if (truncation.discovery_cap_reason === 'unread_files') {
360
- // a sitemap file could not be read this time — try the same request again later
454
+ // a sitemap file could not be read whole this time; try the same request again later
361
455
  }
362
456
  ```
363
457
 
364
- `unread_files` means a sitemap file the site publishes could not be read at all this time: the request for it failed or
365
- was rate limited, or the file was not a readable sitemap. that is often temporary, so asking again later can return a
366
- fuller map.
458
+ - `max_results`: your own `max_results` was reached. ask again with a higher one.
459
+ - `depth`: there are sitemap files past the `depth` you asked for. that is the answer to your `depth`, not a fault.
460
+ read the files in `unread_sitemaps` with `sitemap_url`, or ask with a higher depth.
461
+ - `time`, `file_budget`: the site is big or slow. map it in several calls (below).
462
+ - `file_size`: a sitemap file was too large. a file of pages that turns out too large while it is read gives the
463
+ links in its first part. a file that says up front that it is over the size limit, or any other file that turns
464
+ out too large (such as a sitemap index), gives nothing and is listed in `unread_sitemaps`.
465
+ - `unread_files`: a sitemap file could not be read whole this time. when its download broke off, the links received
466
+ before the break are returned, and the file is listed in `unread_sitemaps`. asking again later can return more.
467
+
468
+ `truncation.unread_sitemaps` lists the sitemap files the map knew about but did not read whole (up to 1 000). read
469
+ each one with `sitemap_url` in another call, except a file that says it is over the size limit: no call reads that
470
+ one. it is empty when every file was read.
471
+
472
+ #### a large site, in several calls
473
+
474
+ a very large or deep site is mapped in several calls: list its sitemap files with `depth: 1` and
475
+ `entries: 'sitemaps'`. then read each link of type `'sitemap'` with `sitemap_url`, plus each `'sitemap_index'` link
476
+ that is also in `unread_sitemaps` (an index this map read only in part). the files of an index read whole are
477
+ already in your list. a listed file shows as `'sitemap'` until a map opens it, even when it
478
+ is a sitemap index itself; reading it then follows its own files, and its own `unread_sitemaps` names the files to
479
+ read next. the full walk-through is in
480
+ [map a very large or deep site in several calls](https://crawlbrulee.com/docs/map/sitemaps#map-a-very-large-or-deep-site-in-several-calls).
481
+
482
+ ```ts
483
+ const tree = await crawlbrulee.map({ url: 'https://example.com', depth: 1, entries: 'sitemaps' })
367
484
 
368
- every other reason (`time`, `file_budget`, `depth`, `file_size`) means the site itself is big, slow or deeply nested;
369
- re-asking with a higher `max_urls` will not return more, and neither will trying again later.
485
+ const unread = new Set(tree.response_meta.truncation.unread_sitemaps)
486
+ const toRead = tree.links.filter(
487
+ link => link.type === 'sitemap' || (link.type === 'sitemap_index' && unread.has(link.url))
488
+ )
489
+
490
+ for (const file of toRead) {
491
+ const part = await crawlbrulee.map({ url: 'https://example.com', sitemap_url: file.url })
492
+ console.log(part.links.length, part.response_meta.truncation.unread_sitemaps)
493
+ }
494
+ ```
370
495
 
371
496
  #### url shape and ordering
372
497
 
373
498
  returned urls come back normalised, the same way `/scrape` normalises the `url` it returns.
374
499
 
375
- ordering is stable, with the most useful links first,
376
- so a link item is always just `{ url }`.
500
+ ordering is stable, with the most useful links first.
377
501
 
378
502
  see the [map endpoint](https://crawlbrulee.com/docs/map) for discovery rules and pagination semantics.
379
503
 
@@ -552,6 +676,11 @@ answer in time, or its tls certificate was not valid. it raises `TargetUnreachab
552
676
  not reach the target site." and no `details`. you are not charged. the site may come back, so retrying later can help.
553
677
  both `/scrape` and `/map` can return it.
554
678
 
679
+ **a 422 `unsupported_screenshot_output` or `unsupported_pdf_output` is a `ValidationError`.** the request asked only for
680
+ a screenshot, or only for a pdf, and the content type can't be made into one. a request with only a screenshot and a pdf gets
681
+ `unsupported_screenshot_output`. ask for another format too to get the rest of the page. not billed. an async job
682
+ like that ends `failed`, and reading its result throws the same `422`.
683
+
555
684
  **a 422 `page_too_large` means the page, not the request.** the page's html was too large to process, so it raises
556
685
  `PageTooLargeError`, not `ValidationError`. it is terminal: the same url will fail the same way, so do not retry it —
557
686
  scrape a smaller page instead. `/scrape` returns it; `/map` does not.
package/dist/index.cjs CHANGED
@@ -12,7 +12,7 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 0;
12
12
  /** Environment variable read by `Crawlbrulee.fromEnv()` to source the API key. */
13
13
  const ENV_API_KEY = "CRAWLBRULEE_API_KEY";
14
14
  /** Identifies the SDK in the `User-Agent` header. Kept in one place for easy bumping. */
15
- const USER_AGENT = "@crawlbrulee/sdk/1.4.0 (node)";
15
+ const USER_AGENT = "@crawlbrulee/sdk/1.6.0 (node)";
16
16
 
17
17
  //#endregion
18
18
  //#region src/errors.ts
@@ -323,7 +323,8 @@ function createApiError(body, status) {
323
323
  case "url_credentials_not_supported":
324
324
  case "blocked_url":
325
325
  case "unsupported_content":
326
- case "unsupported_screenshot_output": return new ValidationError(message, {
326
+ case "unsupported_screenshot_output":
327
+ case "unsupported_pdf_output": return new ValidationError(message, {
327
328
  status,
328
329
  errorName: name,
329
330
  response
@@ -765,8 +766,17 @@ var Crawlbrulee = class Crawlbrulee {
765
766
  }
766
767
  }
767
768
  /**
768
- * Build (or return a cached) site link-map for a domain. Combines sitemap
769
- * discovery with the freshest cached homepage scrape when available.
769
+ * Build (or return a cached) link map of a site from its sitemaps, plus the
770
+ * links on its home page. Every link is `{ url, type, lastmod }`.
771
+ *
772
+ * By default the map reads the site's top sitemap files and two levels of
773
+ * nested ones, and returns pages. Use `sitemap_url` to read one sitemap file,
774
+ * so a very large site can be mapped in several calls. When the site answers
775
+ * `429` for the file in `sitemap_url`, for its robots.txt or for one of its
776
+ * top sitemap files, the map raises
777
+ * {@link RateLimitError} and is not billed.
778
+ *
779
+ * See https://crawlbrulee.com/docs/map/sitemaps#map-a-very-large-or-deep-site-in-several-calls.
770
780
  */
771
781
  map(request, options) {
772
782
  return this.http.post("/api/map", request, options);