polite-crawler 0.1.4 → 0.2.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.
Files changed (76) hide show
  1. package/README.md +28 -121
  2. package/build/{browser-CKRNbIDT.mjs → browser-56dBBLec.mjs} +7 -3
  3. package/build/browser-56dBBLec.mjs.map +1 -0
  4. package/build/classify.d.mts +50 -2
  5. package/build/{cli-daemon-ZYRdy6EW.mjs → cli-daemon-qS4PH2bM.mjs} +2 -2
  6. package/build/{cli-daemon-ZYRdy6EW.mjs.map → cli-daemon-qS4PH2bM.mjs.map} +1 -1
  7. package/build/{cli-recipes-Bci6HREA.mjs → cli-recipes-qbLRVThF.mjs} +2 -2
  8. package/build/{cli-recipes-Bci6HREA.mjs.map → cli-recipes-qbLRVThF.mjs.map} +1 -1
  9. package/build/cli.mjs +41 -16
  10. package/build/cli.mjs.map +1 -1
  11. package/build/{daemon-DK6U51_v.mjs → daemon-BXBbUrni.mjs} +11 -7
  12. package/build/daemon-BXBbUrni.mjs.map +1 -0
  13. package/build/{diff-BUM91Rev.mjs → diff-CpKN0Jdc.mjs} +7 -3
  14. package/build/{diff-BUM91Rev.mjs.map → diff-CpKN0Jdc.mjs.map} +1 -1
  15. package/build/{extract-CefB5Vfn.mjs → extract-DqeqiNHb.mjs} +12 -4
  16. package/build/extract-DqeqiNHb.mjs.map +1 -0
  17. package/build/{html-BACljlkf.d.mts → html-DMgIeQfJ.d.mts} +16 -2
  18. package/build/html.d.mts +1 -2
  19. package/build/index.d.mts +4227 -40
  20. package/build/index.mjs +17 -17
  21. package/build/{log-Dx-wXjJl.mjs → log-CmBWcyQm.mjs} +5 -1
  22. package/build/log-CmBWcyQm.mjs.map +1 -0
  23. package/build/mcp-package-Y0bREnOS.mjs +40 -0
  24. package/build/mcp-package-Y0bREnOS.mjs.map +1 -0
  25. package/build/{ollama-BJxkjny4.mjs → ollama-D1xRh3hY.mjs} +7 -3
  26. package/build/ollama-D1xRh3hY.mjs.map +1 -0
  27. package/build/{open-CbpSCEZc.mjs → open-CEo22Hpu.mjs} +78 -18
  28. package/build/open-CEo22Hpu.mjs.map +1 -0
  29. package/build/politeness.d.mts +366 -2
  30. package/build/{recipe-GiFQojmo.mjs → recipe-B2SkepDZ.mjs} +10 -4
  31. package/build/recipe-B2SkepDZ.mjs.map +1 -0
  32. package/build/{recipe-product-Dellqudu.mjs → recipe-product-CyZE3Ukd.mjs} +29 -7
  33. package/build/recipe-product-CyZE3Ukd.mjs.map +1 -0
  34. package/build/{render-C1CUraWF.mjs → render-CKE2LILH.mjs} +10 -3
  35. package/build/render-CKE2LILH.mjs.map +1 -0
  36. package/build/robots.d.mts +143 -2
  37. package/build/robots.mjs +1 -1
  38. package/build/robots.mjs.map +1 -1
  39. package/build/{rolldown-runtime-D7D4PA-g.mjs → rolldown-runtime-8H4AJuhK.mjs} +1 -0
  40. package/build/{runtime-D_r2SYge.mjs → runtime-C_2BQibH.mjs} +84 -18
  41. package/build/runtime-C_2BQibH.mjs.map +1 -0
  42. package/build/signals.d.mts +45 -2
  43. package/build/{transformers-BQMlaGj4.mjs → transformers-BNiuiLxT.mjs} +3 -3
  44. package/build/transformers-BNiuiLxT.mjs.map +1 -0
  45. package/build/{version-BPem-XZd.mjs → version-Djva7JkN.mjs} +7 -2
  46. package/build/version-Djva7JkN.mjs.map +1 -0
  47. package/build/{wacz-QyzLp4mC.mjs → wacz-DKk2HBmx.mjs} +4 -4
  48. package/build/{wacz-QyzLp4mC.mjs.map → wacz-DKk2HBmx.mjs.map} +1 -1
  49. package/build/{warc-BHcaRKHw.mjs → warc-C_ew76bV.mjs} +4 -4
  50. package/build/{warc-BHcaRKHw.mjs.map → warc-C_ew76bV.mjs.map} +1 -1
  51. package/build/{webbotauth-CjJdTAfE.mjs → webbotauth-Dq_dA_u3.mjs} +2 -2
  52. package/build/{webbotauth-CjJdTAfE.mjs.map → webbotauth-Dq_dA_u3.mjs.map} +1 -1
  53. package/package.json +5 -11
  54. package/build/browser-CKRNbIDT.mjs.map +0 -1
  55. package/build/classify-CrfyXcwo.d.mts +0 -51
  56. package/build/daemon-DK6U51_v.mjs.map +0 -1
  57. package/build/daemon-wwIWsUmU.d.mts +0 -3777
  58. package/build/extract-CefB5Vfn.mjs.map +0 -1
  59. package/build/log-Dx-wXjJl.mjs.map +0 -1
  60. package/build/loopback-C_QYowSf.mjs +0 -22
  61. package/build/loopback-C_QYowSf.mjs.map +0 -1
  62. package/build/mcp.d.mts +0 -199
  63. package/build/mcp.mjs +0 -1555
  64. package/build/mcp.mjs.map +0 -1
  65. package/build/ollama-BJxkjny4.mjs.map +0 -1
  66. package/build/open-CbpSCEZc.mjs.map +0 -1
  67. package/build/politeness-yI3xBEx9.d.mts +0 -367
  68. package/build/recipe-GiFQojmo.mjs.map +0 -1
  69. package/build/recipe-product-Dellqudu.mjs.map +0 -1
  70. package/build/render-C1CUraWF.mjs.map +0 -1
  71. package/build/robots-R-qaEBOb.d.mts +0 -144
  72. package/build/runtime-D_r2SYge.mjs.map +0 -1
  73. package/build/signals-BHpy2xb2.d.mts +0 -46
  74. package/build/transformers-BQMlaGj4.mjs.map +0 -1
  75. package/build/urls-BTDSgDLC.d.mts +0 -16
  76. package/build/version-BPem-XZd.mjs.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"recipe-product-Dellqudu.mjs","names":["answered"],"sources":["../src/recipe-venue.ts","../src/recipe-product.ts"],"sourcesContent":["/*\n * The venue recipe reads the opening hours of a venue (a restaurant, a shop, a museum) from its own\n * site. It reads the cheap sources first:\n *\n * 1. The home page: its JSON-LD (`openingHoursSpecification` and `openingHours`) and its text.\n * 2. The few pages of the site that most likely hold hours or a menu, by the words of their links.\n * 3. An OpenStreetMap `opening_hours` value, when the caller gives one. The recipe sends no\n * request to OpenStreetMap.\n *\n * Each reading names the page, the document and the capture that it came from. The recipe gives\n * readings only. To compare them with the hours that an application keeps is the work of the\n * application, and `compareHours` is the pure helper for it.\n */\nimport type { Doc } from \"./docs.ts\";\nimport type { Duration } from \"./duration.ts\";\nimport { durationMs } from \"./duration.ts\";\nimport type { Archived } from \"./fallback.ts\";\nimport { archivedOf } from \"./fallback.ts\";\nimport type { Day, Hours, Week } from \"./hours.ts\";\nimport {\n DAYS,\n describeWeek,\n disagreements,\n fromCompact,\n fromSchemaOrg,\n fromText,\n} from \"./hours.ts\";\nimport type { PageLink } from \"./html.ts\";\nimport { typesOf } from \"./html.ts\";\nimport type { RecipeTarget } from \"./recipe.ts\";\nimport { deadlineOf, runtimeOf } from \"./recipe.ts\";\nimport type { PageOptions, PageResult } from \"./runtime.ts\";\nimport { HELD_BACK, fetchPage, isHeldBack, recordOverride } from \"./runtime.ts\";\n\n/** These are the limits of the venue recipe. */\nexport const VENUE_LIMITS: {\n /** The pages of the site that a read fetches after the home page, by default. */\n readonly pages: number;\n /** The most pages after the home page. */\n readonly maxPages: number;\n /** The most menu links of a result. */\n readonly menus: number;\n /** The most readings of one page. A page with more is a list of something else. */\n readonly readingsPerPage: number;\n /** The most links of one page that the recipe ranks. */\n readonly links: number;\n /** The distance in metres within which a JSON-LD location is the venue. */\n readonly nearMetres: number;\n} = { pages: 3, maxPages: 8, menus: 8, readingsPerPage: 20, links: 200, nearMetres: 150 };\n\n// ── words ───────────────────────────────────────────────────────────────────\n\nconst STOP_WORDS: ReadonlySet<string> = new Set([\"the\", \"a\", \"an\", \"and\", \"of\", \"at\"]);\n\n/** This function gives the words of a text in one form: lower case, no accents, and `&` as `and`. */\nconst wordsOf = (text: string): string[] =>\n text\n .toLowerCase()\n .normalize(\"NFKD\")\n .replace(/\\p{M}/gu, \"\")\n .replace(/&/g, \" and \")\n .replace(/[^\\p{L}\\p{N} ]/gu, \" \")\n .split(/\\s+/)\n .filter((word) => word.length > 0 && !STOP_WORDS.has(word));\n\n/**\n * This function tells if a page names a venue: the page has the first word of the name and 60\n * percent of its words. A domain that a venue let go can show a different business. It gives\n * undefined for a name with no word.\n */\nexport const namesVenue = (\n name: string,\n page: { readonly title: string; readonly text: string },\n): boolean | undefined => {\n const words = wordsOf(name);\n if (words.length === 0) return undefined;\n const onPage = new Set(wordsOf(`${page.title} ${page.text}`));\n const named = new Set(words);\n const found = [...named].filter((word) => onPage.has(word)).length;\n return onPage.has(words[0]!) && found / named.size >= 0.6;\n};\n\n// ── the links of the site ───────────────────────────────────────────────────\n\n/** The words of a link that tell what its page holds, with their weights. */\nconst LINK_WORDS: ReadonlyArray<readonly [RegExp, number]> = [\n [/hours|horario|horaires|opening/, 6],\n [/menu|food|drink|wine|cocktail/, 4],\n [/contact|location|visit|find.?us|directions/, 3],\n [/about|info/, 2],\n [/lunch|dinner|brunch|breakfast/, 2],\n];\n\n/** The hosts of review and social sites: a menu there is a different page about the venue. */\nconst SOCIAL =\n /(?:^|\\.)(?:yelp|tripadvisor|facebook|instagram|twitter|x|tiktok|google|apple|spotify)\\.[a-z.]+$/i;\n\n/** The files that hold no hours as text. */\nconst FILE = /\\.(?:pdf|jpe?g|png|gif|webp|svg|mp4)(?:[?#]|$)/i;\n\nconst hostOf = (url: string): string | undefined => {\n try {\n return new URL(url).hostname.toLowerCase().replace(/^www\\./, \"\");\n } catch {\n return undefined;\n }\n};\n\nconst withoutHash = (url: string): string => url.replace(/#.*$/s, \"\").replace(/\\/$/, \"\");\n\n/**\n * This function ranks the links of a page by the words of their text and path. The links to\n * the hours or a menu come first. It keeps only the links on the site of `home`,\n * and it leaves out the files (a PDF, an image) and the home page itself.\n */\nexport const rankHoursLinks = (links: readonly PageLink[], home: string): PageLink[] => {\n const site = hostOf(home);\n const self = withoutHash(home);\n const seen = new Set<string>();\n const scored: Array<{ readonly link: PageLink; readonly score: number }> = [];\n for (const link of links.slice(0, VENUE_LIMITS.links)) {\n if (typeof link?.href !== \"string\" || hostOf(link.href) !== site || FILE.test(link.href))\n continue;\n const key = withoutHash(link.href);\n if (key === self || seen.has(key)) continue;\n let path = \"\";\n try {\n path = new URL(link.href).pathname;\n } catch {\n continue;\n }\n const words = `${typeof link.text === \"string\" ? link.text : \"\"} ${path}`.toLowerCase();\n const score = LINK_WORDS.reduce(\n (sum, [pattern, weight]) => sum + (pattern.test(words) ? weight : 0),\n 0,\n );\n if (score <= 0) continue;\n seen.add(key);\n scored.push({ link, score });\n }\n // a stable sort: links of equal score keep the order of the page\n return scored.sort((a, b) => b.score - a.score).map((entry) => entry.link);\n};\n\n/** A link to a menu. */\nexport interface VenueLink {\n readonly url: string;\n readonly text: string;\n}\n\n// ── the location of a venue ─────────────────────────────────────────────────\n\n/** The place of a venue, to choose its hours on a page that lists more than one location. */\nexport interface VenueLocation {\n /** This text is the street address, for example `12 Via Esempio`. The number and the street count. */\n readonly address?: string | undefined;\n readonly lat?: number | undefined;\n readonly lon?: number | undefined;\n}\n\ninterface Point {\n readonly lat: number;\n readonly lon: number;\n}\n\nconst EARTH_METRES = 6_371_000;\n\n/** This function gives the distance in metres between two points on the earth (the haversine formula). */\nconst metresBetween = (a: Point, b: Point): number => {\n const rad = Math.PI / 180;\n const dLat = (b.lat - a.lat) * rad;\n const dLon = (b.lon - a.lon) * rad;\n const h =\n Math.sin(dLat / 2) ** 2 +\n Math.cos(a.lat * rad) * Math.cos(b.lat * rad) * Math.sin(dLon / 2) ** 2;\n return 2 * EARTH_METRES * Math.asin(Math.min(1, Math.sqrt(h)));\n};\n\nconst pointOf = (lat: unknown, lon: unknown): Point | undefined => {\n const la = typeof lat === \"string\" ? Number.parseFloat(lat) : lat;\n const lo = typeof lon === \"string\" ? Number.parseFloat(lon) : lon;\n return typeof la === \"number\" &&\n typeof lo === \"number\" &&\n Number.isFinite(la) &&\n Number.isFinite(lo) &&\n Math.abs(la) <= 90 &&\n Math.abs(lo) <= 180\n ? { lat: la, lon: lo }\n : undefined;\n};\n\n/** The words of a street that tell nothing about which street: directions and kinds of road. */\nconst STREET_NOISE: ReadonlySet<string> = new Set([\n \"n\",\n \"s\",\n \"e\",\n \"w\",\n \"north\",\n \"south\",\n \"east\",\n \"west\",\n \"st\",\n \"street\",\n \"ave\",\n \"avenue\",\n \"rd\",\n \"road\",\n \"blvd\",\n \"boulevard\",\n \"dr\",\n \"drive\",\n \"ln\",\n \"lane\",\n \"ct\",\n \"court\",\n \"pl\",\n \"place\",\n \"sq\",\n \"square\",\n \"hwy\",\n \"highway\",\n \"pkwy\",\n \"parkway\",\n \"way\",\n \"ter\",\n \"terrace\",\n \"suite\",\n \"ste\",\n \"unit\",\n]);\n\n/** This function gives the number of a street address and the words that name its street. */\nconst streetOf = (\n address: string,\n): { readonly number: string; readonly words: ReadonlySet<string> } | undefined => {\n const words = wordsOf((address.split(\",\")[0] ?? address).slice(0, 200));\n const number = words.find((word) => /^\\d+[a-z]?$/.test(word));\n if (number === undefined) return undefined;\n return {\n number,\n words: new Set(words.filter((word) => !/^\\d/.test(word) && !STREET_NOISE.has(word))),\n };\n};\n\nconst addressText = (value: unknown): string | undefined => {\n if (typeof value === \"string\") return value;\n if (Array.isArray(value)) return addressText(value[0]);\n if (typeof value === \"object\" && value !== null) {\n const street = (value as Record<string, unknown>)[\"streetAddress\"];\n return typeof street === \"string\" ? street : undefined;\n }\n return undefined;\n};\n\n/**\n * This function tells if a JSON-LD location record is the venue. The record must have the same\n * number on a street that shares a word, or a position near the venue (`VENUE_LIMITS.nearMetres`).\n */\nexport const locationMatches = (\n record: Readonly<Record<string, unknown>>,\n location: VenueLocation,\n): boolean => {\n const geo = record[\"geo\"];\n const at =\n typeof geo === \"object\" && geo !== null\n ? pointOf(\n (geo as Record<string, unknown>)[\"latitude\"],\n (geo as Record<string, unknown>)[\"longitude\"],\n )\n : undefined;\n const ours = pointOf(location.lat, location.lon);\n if (at !== undefined && ours !== undefined && metresBetween(at, ours) <= VENUE_LIMITS.nearMetres)\n return true;\n const theirs = addressText(record[\"address\"]);\n if (theirs === undefined || location.address === undefined) return false;\n const a = streetOf(theirs);\n const b = streetOf(location.address);\n return (\n a !== undefined &&\n b !== undefined &&\n a.number === b.number &&\n [...a.words].some((word) => b.words.has(word))\n );\n};\n\nconst located = (record: Readonly<Record<string, unknown>>): boolean =>\n record[\"address\"] !== undefined || record[\"geo\"] !== undefined;\n\n// ── the readings ────────────────────────────────────────────────────────────\n\n/** One reading of the hours, with the source that it came from. */\nexport interface VenueReading {\n /** The value `jsonld` is the structured data of a page, `text` the text of a page, and `osm` the value of the caller. */\n readonly from: \"jsonld\" | \"osm\" | \"text\";\n /**\n * This value orders the readings, 0 first. JSON-LD is 0, a text under an hours heading is 1 and\n * OpenStreetMap is 2. A text with no label of a different schedule is 3. A different schedule is\n * 4, for example a brunch, a kitchen, a happy hour or an office.\n */\n readonly rank: number;\n /** The value `opening` is the opening hours of the venue (a rank of 3 or less). Else it is `other`. */\n readonly schedule: \"opening\" | \"other\";\n /** This text is the line above a reading of the text, for example `Hours` or `Happy hour`. */\n readonly label: string | null;\n /** This text is the week as words, from `describeWeek`. */\n readonly hours: string;\n readonly week: Week;\n /** This value is true when the reader guessed an am or a pm. */\n readonly inferred: boolean;\n /** This value is true when the reader left out a rule or a line (a holiday, a date). */\n readonly partial: boolean;\n /** This text names the season or the dates of the hours. */\n readonly season: string | null;\n /** This text is the words that the hours came from. A reading of the text can be a quote for `verify`. */\n readonly raw: string;\n /** This path names the JSON-LD record of the reading in the metadata of the document, for example `jsonld[2]`. */\n readonly path: string | null;\n /** This URL is the page of the reading. */\n readonly url: string | null;\n readonly doc: string | null;\n /** This id is the capture of the text that the reading came from. A citation names it. */\n readonly capture: string | null;\n readonly fetched_at: string | null;\n /** This value tells that the page is an archived copy, with the archive and the date of the copy. */\n readonly archived?: Archived | undefined;\n}\n\n/** A stored document, or a version of one: what `venueReadingsOf` reads. */\nexport interface VenueSource {\n readonly doc?: string | null | undefined;\n readonly url: string | null;\n readonly text: string;\n readonly meta: Readonly<Record<string, unknown>>;\n readonly capture?: string | null | undefined;\n readonly fetched_at?: string | null | undefined;\n}\n\n/** The result of `venueReadingsOf`: what one document says. */\nexport interface VenueRead {\n readonly readings: readonly VenueReading[];\n /** This value is the first `telephone` of the JSON-LD records of the venue. */\n readonly phone: string | null;\n readonly menus: readonly VenueLink[];\n readonly notes: readonly string[];\n /** This value counts the work of the reader. The tests use it, because it does not change with the computer. */\n readonly steps: number;\n}\n\n/** The types of JSON-LD records that have the hours of a venue. */\nconst HOURS_TYPES =\n /Restaurant|LocalBusiness|FoodEstablishment|BarOrPub|CafeOrCoffeeShop|Bakery|Store|Place|Winery|Brewery|NightClub|Museum|TouristAttraction|Library|EntertainmentBusiness/;\n\n/**\n * A label that names a schedule that is not the opening hours. The line above a schedule is often a\n * different thing, for example a phone number, and such a line tells nothing.\n */\nconst OTHER_SCHEDULE =\n /brunch|lunch|dinner|breakfast|happy|kitchen|\bbar\b|deliver|take.?out|pick.?up|patio|holiday|season|summer|winter|special|late|menu/i;\n\n/**\n * A label of a service beside the opening hours, or of an office. Its hours are a different\n * schedule, also when the label says \"hours\" (\"Happy hour\", \"Kitchen hours\", \"Office hours\").\n */\nconst SERVICE =\n /happy|kitchen|deliver|take.?out|pick.?up|brunch|holiday|special|patio|\bbar\b|late.?night|office/i;\n\nconst rankOf = (from: VenueReading[\"from\"], label: string | null): number => {\n if (from === \"jsonld\") return 0;\n if (from === \"osm\") return 2;\n const text = label ?? \"\";\n if (SERVICE.test(text)) return 4;\n if (/hour|open/i.test(text)) return 1;\n return OTHER_SCHEDULE.test(text) ? 4 : 3;\n};\n\nconst reading = (\n from: VenueReading[\"from\"],\n hours: Hours,\n label: string | null,\n source: {\n readonly url: string | null;\n readonly doc: string | null;\n readonly capture: string | null;\n readonly fetched_at: string | null;\n readonly path: string | null;\n readonly archived?: Archived | undefined;\n },\n): VenueReading => {\n const rank = rankOf(from, label);\n return {\n from,\n rank,\n schedule: rank <= 3 ? \"opening\" : \"other\",\n label,\n hours: describeWeek(hours.week),\n week: hours.week,\n inferred: hours.inferred,\n partial: hours.partial === true,\n season: hours.season ?? null,\n raw: hours.raw,\n path: source.path,\n url: source.url,\n doc: source.doc,\n capture: source.capture,\n fetched_at: source.fetched_at,\n ...(source.archived === undefined ? {} : { archived: source.archived }),\n };\n};\n\nconst recordsOf = (meta: Readonly<Record<string, unknown>>): Record<string, unknown>[] => {\n const jsonld = meta[\"jsonld\"];\n return Array.isArray(jsonld)\n ? jsonld.filter(\n (record): record is Record<string, unknown> =>\n record !== null && typeof record === \"object\" && !Array.isArray(record),\n )\n : [];\n};\n\nconst linksOf = (meta: Readonly<Record<string, unknown>>): PageLink[] => {\n const links = meta[\"links\"];\n return Array.isArray(links)\n ? links.filter(\n (link): link is PageLink =>\n link !== null && typeof link === \"object\" && typeof (link as PageLink).href === \"string\",\n )\n : [];\n};\n\n/**\n * This function reads the hours of one stored document, or of one version of it. It is pure, and\n * it sends no request. Thus it also reads a version at a time (`versionAtTime`) or an archived copy.\n *\n * - It reads each JSON-LD record of a venue type with `fromSchemaOrg`, and the text with `fromText`.\n * - On a page that lists more than one location, it reads only the location that matches\n * `location`. With no `location`, it reads none of them, and a note says so.\n * - It gives the first `telephone` and the menu links that are not on a review or social site.\n */\nexport const venueReadingsOf = (\n source: VenueSource,\n options: { readonly location?: VenueLocation | undefined } = {},\n): VenueRead => {\n let steps = 0;\n const notes: string[] = [];\n const archived = archivedOf(source.meta);\n const where = {\n url: archived?.original ?? source.url,\n doc: source.doc ?? null,\n capture: source.capture ?? null,\n fetched_at: source.fetched_at ?? null,\n ...(archived === undefined ? {} : { archived }),\n };\n const all = recordsOf(source.meta);\n steps += all.length;\n const venues = all\n .map((record, index) => ({ record, index }))\n .filter(({ record }) => typesOf(record).some((type) => HOURS_TYPES.test(type)));\n // a page of a group lists each of its locations: only the hours of this venue are its hours\n let usable = venues;\n const placed = venues.filter(({ record }) => located(record));\n if (placed.length > 1) {\n const location = options.location;\n const ours =\n location === undefined\n ? []\n : placed.filter(({ record }) => locationMatches(record, location));\n if (ours.length === 0) {\n notes.push(\n location === undefined\n ? `${where.url ?? \"the page\"} lists ${String(placed.length)} locations: give location to read the hours of one`\n : `${where.url ?? \"the page\"} lists ${String(placed.length)} locations, and none matched the location: their hours were not read`,\n );\n }\n usable = [...ours, ...venues.filter(({ record }) => !located(record))];\n }\n const readings: VenueReading[] = [];\n let phone: string | null = null;\n for (const { record, index } of usable) {\n steps += 1;\n const hours = fromSchemaOrg(record);\n if (hours !== undefined && readings.length < VENUE_LIMITS.readingsPerPage) {\n readings.push(reading(\"jsonld\", hours, null, { ...where, path: `jsonld[${String(index)}]` }));\n }\n const telephone = record[\"telephone\"];\n if (phone === null && typeof telephone === \"string\" && telephone.trim().length > 0)\n phone = telephone.trim().slice(0, 100);\n }\n steps += source.text.length;\n for (const hours of fromText(source.text)) {\n if (readings.length >= VENUE_LIMITS.readingsPerPage) {\n notes.push(\n `${where.url ?? \"the page\"} gives more than ${String(VENUE_LIMITS.readingsPerPage)} readings: the rest were not read`,\n );\n break;\n }\n readings.push(reading(\"text\", hours, hours.label, { ...where, path: null }));\n }\n const menus: VenueLink[] = [];\n for (const link of linksOf(source.meta).slice(0, VENUE_LIMITS.links)) {\n steps += 1;\n if (menus.length >= VENUE_LIMITS.menus) break;\n const text = typeof link.text === \"string\" ? link.text.slice(0, 200) : \"\";\n const host = hostOf(link.href);\n if (host === undefined || SOCIAL.test(host) || !/menu/i.test(`${text} ${link.href}`)) continue;\n if (menus.some((menu) => menu.url === link.href)) continue;\n menus.push({ url: link.href, text });\n }\n return { readings, phone, menus, notes, steps };\n};\n\n// ── the recipe ──────────────────────────────────────────────────────────────\n\n/** An OpenStreetMap `opening_hours` value, with where the caller got it. */\nexport interface VenueOsm {\n /** This text is the value of the tag, for example `Mo-Fr 08:00-18:00; Sa 09:00-13:00`. */\n readonly value: string;\n readonly url?: string | null | undefined;\n readonly doc?: string | null | undefined;\n readonly capture?: string | null | undefined;\n readonly fetched_at?: string | null | undefined;\n}\n\n/** These options control `readVenue`. */\nexport interface VenueOptions {\n /** This value is the number of pages after the home page, from 0 to `VENUE_LIMITS.maxPages`. The default is 3. */\n readonly pages?: number | undefined;\n /** This text is the name of the venue. The result tells if the home page names it (`names_venue`). */\n readonly name?: string | undefined;\n /** With true, a home page that does not name the venue gives no reading, and the recipe fetches no more pages. */\n readonly requireName?: boolean | undefined;\n /** This value is the place of the venue, for a page that lists more than one location. */\n readonly location?: VenueLocation | undefined;\n /** This value is an OpenStreetMap `opening_hours` value. The recipe reads it, and sends no request for it. */\n readonly osm?: string | VenueOsm | undefined;\n /** The value `auto` renders a page only when it shows almost nothing without JavaScript. */\n readonly render?: \"auto\" | \"never\" | \"always\" | undefined;\n /** A stored copy younger than this age answers with no request. The default is `24h`. */\n readonly maxAge?: Duration | undefined;\n /** These agents get their robots.txt rules applied too, for example `Claude-User`. */\n readonly onBehalfOf?: readonly string[] | undefined;\n readonly signal?: AbortSignal | undefined;\n /** No request starts after this time from the start of the read, for example `\"2m\"`. */\n readonly budget?: Duration | undefined;\n readonly scope?: string | undefined;\n readonly labels?: Readonly<Record<string, string>> | undefined;\n /** This name connects the captures to the job that asked for them. The default is `venue`. */\n readonly job?: string | undefined;\n /** This value is true when a person gave this URL directly. It is the only way to read a held-back page. The crawler logs it. */\n readonly userProvided?: boolean | undefined;\n /** The value `archive` reads an archived copy of a page that the site refuses, or when the site is down. */\n readonly fallback?: \"archive\" | \"none\" | undefined;\n readonly archiveDespiteRobots?: boolean | undefined;\n}\n\n/** One page that the venue recipe fetched. */\nexport interface VenuePage {\n readonly url: string;\n readonly doc: string | null;\n readonly outcome: string;\n readonly status: number;\n readonly rendered: boolean;\n readonly capture: string | null;\n readonly fetched_at: string | null;\n readonly from_cache: boolean;\n readonly held_back: boolean;\n readonly archived?: Archived | undefined;\n readonly note: string | null;\n}\n\n/** The result of `readVenue`. */\nexport interface VenueResult {\n /** This URL is the site that the caller gave. */\n readonly url: string;\n /** This URL is where the home page landed, after the redirects. */\n readonly home: string | null;\n /** This URL is the new site, when the home page landed on a different host. */\n readonly moved: string | null;\n /** This value tells if the home page names the venue. It is null with no `name`. */\n readonly names_venue: boolean | null;\n readonly pages: readonly VenuePage[];\n /** These are the readings, one for each source, label and week: the best first. */\n readonly readings: readonly VenueReading[];\n /** This reading is the best reading of the opening hours, from any source. */\n readonly best: VenueReading | null;\n /** This reading is the best reading of the site itself (not OpenStreetMap) with no guess. A check for a change compares it. */\n readonly from_site: VenueReading | null;\n /** These days are the days where two readings of the whole schedule (a rank of 2 or less) disagree. */\n readonly disagree: readonly Day[];\n readonly menus: readonly VenueLink[];\n readonly phone: string | null;\n /** These ids are the captures of the pages that the recipe read. */\n readonly captures: readonly string[];\n /** This value is true when a page asked not to be AI input, so the recipe read nothing from it. */\n readonly held_back: boolean;\n readonly note: string | null;\n}\n\nconst answered = (page: PageResult): boolean =>\n page.outcome === \"ok\" || page.outcome === \"not-modified\" || page.archived !== undefined;\n\n/**\n * This function reads the hours of a venue from its site. It fetches the home page and the pages\n * that most likely hold hours or a menu. Each one is a fetch of the crawler. Thus robots.txt, the\n * pace, the stops, the capture log and the rule for a held-back page apply. It renders a page only\n * when the page needs JavaScript, as `fetch` does.\n *\n * It gives each reading with its source, the best reading, and the days where the readings\n * disagree. It writes nothing but the capture log. To choose the true hours is the decision of\n * the application.\n */\nexport const readVenue = async (\n target: RecipeTarget,\n url: string,\n options: VenueOptions = {},\n): Promise<VenueResult> => {\n const runtime = runtimeOf(target);\n const extra = options.pages ?? VENUE_LIMITS.pages;\n if (!Number.isInteger(extra) || extra < 0 || extra > VENUE_LIMITS.maxPages) {\n throw new RangeError(\n `pages must be a whole number from 0 to ${String(VENUE_LIMITS.maxPages)}, not ${String(extra)}`,\n );\n }\n let site: URL;\n try {\n site = new URL(url);\n } catch {\n throw new TypeError(`readVenue needs the URL of a site, not ${JSON.stringify(url)}`);\n }\n if (site.protocol !== \"http:\" && site.protocol !== \"https:\")\n throw new TypeError(`readVenue needs an http or https URL, not ${url}`);\n const maxAge = options.maxAge ?? \"24h\";\n const job = options.job ?? \"venue\";\n const userProvided = options.userProvided ?? false;\n const pageOptions: PageOptions = {\n render: options.render ?? \"auto\",\n maxAgeMs: maxAge === \"auto\" ? \"auto\" : durationMs(maxAge),\n onBehalfOf: options.onBehalfOf,\n job,\n signal: options.signal,\n scope: options.scope,\n labels: options.labels,\n deadline: deadlineOf(runtime, options.budget),\n fallback: options.fallback,\n archiveDespiteRobots: options.archiveDespiteRobots,\n };\n const notes: string[] = [];\n const readings: VenueReading[] = [];\n const pages: VenuePage[] = [];\n const menus: VenueLink[] = [];\n const captures = new Set<string>();\n let phone: string | null = null;\n let heldBack = false;\n\n if (options.osm !== undefined) {\n const osm: VenueOsm = typeof options.osm === \"string\" ? { value: options.osm } : options.osm;\n const hours = typeof osm.value === \"string\" ? fromCompact(osm.value, \"osm\") : undefined;\n if (hours === undefined) {\n notes.push(\n `the OpenStreetMap hours cannot be read: ${JSON.stringify(String(osm.value).slice(0, 200))}`,\n );\n } else {\n readings.push(\n reading(\"osm\", hours, null, {\n url: osm.url ?? null,\n doc: osm.doc ?? null,\n capture: osm.capture ?? null,\n fetched_at: osm.fetched_at ?? null,\n path: null,\n }),\n );\n if (typeof osm.capture === \"string\") captures.add(osm.capture);\n }\n }\n\n /** This function fetches one page, and gives its document when the recipe can read it. */\n const readPage = async (pageUrl: string): Promise<Doc | undefined> => {\n const page = await fetchPage(runtime, pageUrl, pageOptions);\n const doc = page.doc === null ? undefined : runtime.index.get(page.doc);\n const held = doc !== undefined && isHeldBack(doc, userProvided);\n pages.push({\n url: pageUrl,\n doc: page.doc,\n outcome: page.outcome,\n status: page.status,\n rendered: page.rendered,\n capture: page.capture,\n fetched_at: page.fetched_at,\n from_cache: page.from_cache,\n held_back: held,\n ...(page.archived === undefined ? {} : { archived: page.archived }),\n note: page.note,\n });\n if (!answered(page)) {\n notes.push(`${pageUrl}: ${page.outcome}${page.note === null ? \"\" : ` (${page.note})`}`);\n }\n if (doc === undefined) return undefined;\n if (held) {\n heldBack = true;\n return undefined;\n }\n if (doc.ai_input === \"no\") await recordOverride(runtime, doc, job);\n captures.add(doc.capture);\n return doc;\n };\n\n const readDoc = (doc: Doc): void => {\n const read = venueReadingsOf(doc, { location: options.location });\n readings.push(...read.readings);\n notes.push(...read.notes);\n phone ??= read.phone;\n for (const menu of read.menus) {\n if (menus.length < VENUE_LIMITS.menus && !menus.some((known) => known.url === menu.url))\n menus.push(menu);\n }\n };\n\n let homeUrl: string | null = null;\n let namesVenueResult: boolean | null = null;\n const home = await readPage(site.href);\n if (home !== undefined) {\n homeUrl = archivedOf(home.meta)?.original ?? home.url ?? site.href;\n if (options.name !== undefined) {\n namesVenueResult = namesVenue(options.name, home) ?? null;\n if (namesVenueResult === false) {\n notes.push(\n `${homeUrl} does not name ${options.name}: the domain can belong to a different business now`,\n );\n }\n }\n if (namesVenueResult === false && options.requireName === true) {\n notes.push(\"requireName: the recipe read no hours from a site that does not name the venue\");\n } else {\n readDoc(home);\n for (const link of rankHoursLinks(linksOf(home.meta), homeUrl).slice(0, extra)) {\n if (options.signal?.aborted === true) break;\n const doc = await readPage(link.href);\n if (doc !== undefined) readDoc(doc);\n }\n }\n }\n const moved = homeUrl !== null && hostOf(homeUrl) !== hostOf(site.href) ? homeUrl : null;\n if (moved !== null) notes.push(`${site.href} now goes to ${moved}`);\n if (heldBack) notes.push(HELD_BACK);\n\n // one reading for each source, label and week: a site that repeats its JSON-LD on each page said it once\n const firstOf = new Map<string, VenueReading>();\n for (const one of readings) {\n const key = `${one.from}|${one.label ?? \"\"}|${one.hours}|${one.season ?? \"\"}`;\n if (!firstOf.has(key)) firstOf.set(key, one);\n }\n // the best first; of two equal readings, the reading as written before a guess\n const unique = [...firstOf.values()].sort(\n (a, b) => a.rank - b.rank || Number(a.inferred) - Number(b.inferred),\n );\n const disagree = new Set<Day>();\n const whole = unique.filter((one) => one.rank <= 2);\n for (let i = 0; i < whole.length; i += 1) {\n for (let j = i + 1; j < whole.length; j += 1) {\n for (const day of disagreements(whole[i]!.week, whole[j]!.week)) disagree.add(day);\n }\n }\n const best = unique.find((one) => one.schedule === \"opening\") ?? null;\n const fromSite =\n unique.find((one) => one.from !== \"osm\" && one.schedule === \"opening\" && !one.inferred) ?? null;\n return {\n url: site.href,\n home: homeUrl,\n moved,\n names_venue: namesVenueResult,\n pages,\n readings: unique,\n best,\n from_site: fromSite,\n disagree: DAYS.filter((day) => disagree.has(day)),\n menus,\n phone,\n captures: [...captures],\n held_back: heldBack,\n note: notes.length > 0 ? notes.join(\"; \") : null,\n };\n};\n","/*\n * The product recipe reads a product page: its schema.org Product data. Most shop systems write\n * this data on each product page. The recipe gives the name, the brand, the codes (SKU and GTIN),\n * the price as an amount and a currency, a range of prices across the variants, the availability,\n * the seller, the image, and the variant that the URL names.\n *\n * A shop that refuses the crawler is a clean stop: the result says `wall`, with a note, and the\n * crawler does not try a different identity.\n */\nimport type { Doc } from \"./docs.ts\";\nimport type { Duration } from \"./duration.ts\";\nimport { durationMs } from \"./duration.ts\";\nimport type { Archived } from \"./fallback.ts\";\nimport { archivedOf } from \"./fallback.ts\";\nimport { extractPage, typesOf } from \"./html.ts\";\nimport type { RecipeTarget } from \"./recipe.ts\";\nimport { deadlineOf, runtimeOf } from \"./recipe.ts\";\nimport type { CrawlRuntime, PageOptions, PageResult } from \"./runtime.ts\";\nimport { HELD_BACK, fetchPage, isHeldBack, recordOverride } from \"./runtime.ts\";\nimport { decodeBody } from \"./text.ts\";\n\ntype Json = Readonly<Record<string, unknown>>;\n\n/** These are the limits of the product reader. A page with more is hostile or broken, and the reader stops there. */\nexport const PRODUCT_LIMITS: {\n /** The most JSON-LD records that the reader examines. */\n readonly records: number;\n /** The most offers of one product or variant. */\n readonly offers: number;\n /** The most variants of one product. */\n readonly variants: number;\n /** The levels of `hasVariant` and of nested offers that the reader follows. */\n readonly depth: number;\n /** The longest text of a price. */\n readonly priceChars: number;\n} = { records: 500, offers: 200, variants: 200, depth: 2, priceChars: 64 };\n\n/** An amount of money, as schema.org Product data gives it. */\nexport interface Money {\n /** This value is the amount in the unit of the currency, for example 12.5. */\n readonly amount: number;\n /** This value is the ISO 4217 code of the currency, for example `EUR`. */\n readonly currency: string;\n /** This text is the price as the page wrote it, for a check with `extract`. */\n readonly raw: string;\n}\n\n/** The variant of a product that the URL names. */\nexport interface ProductVariant {\n readonly name: string | null;\n readonly sku: string | null;\n readonly price: Money | null;\n readonly availability: string | null;\n}\n\n/** The result of `productOf`: what the Product data of a page says. */\nexport interface ProductRead {\n /** This value is true when the page has a Product record. */\n readonly found: boolean;\n readonly name: string | null;\n readonly brand: string | null;\n readonly sku: string | null;\n readonly gtin: string | null;\n /** This value is the lowest price of the offers, in the first currency of the page. */\n readonly price: Money | null;\n /** This value is the highest price, when the offers give a range (sizes, colors, variants). */\n readonly price_to: Money | null;\n /**\n * This value is the schema.org name of the availability, for example `InStock`. When the offers\n * do not agree, it is `InStock` if one offer is in stock, and `availabilities` gives each value.\n */\n readonly availability: string | null;\n readonly availabilities: readonly string[];\n /** This value is the variant that the URL names (`?variant=`, `sku` or `gtin`), read alone. */\n readonly variant: ProductVariant | null;\n readonly seller: string | null;\n readonly image: string | null;\n readonly notes: readonly string[];\n /** This value counts the work of the reader. The tests use it, because it does not change with the computer. */\n readonly steps: number;\n}\n\nconst PRODUCT_TYPES = /^(?:Product|ProductGroup|IndividualProduct|ProductModel|SomeProducts)$/;\n\n/** A list price or a struck-through price is the old price, not the price. */\nconst WAS_PRICE = /ListPrice|StrikethroughPrice|MSRP|SRP/i;\n\nconst GTIN_KEYS = [\"gtin13\", \"gtin12\", \"gtin14\", \"gtin8\", \"gtin\"] as const;\n\n/** Two symbols that name one currency only. The other symbols (`$`, `¥`) name more than one. */\nconst SYMBOLS: Readonly<Record<string, string>> = { \"€\": \"EUR\", \"£\": \"GBP\" };\n\nlet knownCurrencies: ReadonlySet<string> | undefined;\n/** This function tells if a code is an ISO 4217 currency that the runtime knows. */\nconst isCurrency = (code: string): boolean => {\n if (!/^[A-Z]{3}$/.test(code)) return false;\n if (knownCurrencies === undefined) {\n try {\n knownCurrencies = new Set(Intl.supportedValuesOf(\"currency\"));\n } catch {\n // a runtime with no list: each code of three capital letters counts\n knownCurrencies = new Set();\n }\n }\n return knownCurrencies.size === 0 || knownCurrencies.has(code);\n};\n\n/** This function gives the number of digits after the decimal point of a currency: 2 for EUR, 0 for JPY. */\nconst fractionDigits = (currency: string | undefined): number | undefined => {\n if (currency === undefined) return undefined;\n try {\n return new Intl.NumberFormat(\"en\", { style: \"currency\", currency }).resolvedOptions()\n .maximumFractionDigits;\n } catch {\n return undefined;\n }\n};\n\n/**\n * This function reads an amount as a shop writes it: `19.99`, `1,299.00`, `1.299,00`, `$19.99` or\n * the number 19.99. The last separator is the decimal point, with one exception. One kind of\n * separator, each followed by three digits, separates thousands: `1.299` is 1299. For a\n * currency with three decimal digits, one point with three digits is the decimal point. A text\n * with two numbers (a range), a negative amount or zero gives undefined.\n */\nexport const readAmount = (value: unknown, currency?: string): number | undefined => {\n if (typeof value === \"number\") return Number.isFinite(value) && value > 0 ? value : undefined;\n if (typeof value !== \"string\" || value.length > PRODUCT_LIMITS.priceChars) return undefined;\n // one number, with the currency and the spaces around it\n const match = /^[^\\d-]*?(\\d(?:[\\d.,'   ]*\\d)?)[^\\d]*$/.exec(value.trim());\n if (match === null) return undefined;\n let digits = match[1]!.replace(/['   ]/g, \"\");\n const last = Math.max(digits.lastIndexOf(\".\"), digits.lastIndexOf(\",\"));\n if (last !== -1) {\n const kinds = new Set(digits.match(/[.,]/g));\n const groups = kinds.size === 1 && /^[1-9]\\d{0,2}(?:[.,]\\d{3})+$/.test(digits);\n const decimalThree = groups && fractionDigits(currency) === 3 && /^\\d+\\.\\d{3}$/.test(digits);\n digits =\n groups && !decimalThree\n ? digits.replace(/[.,]/g, \"\")\n : `${digits.slice(0, last).replace(/[.,]/g, \"\")}.${digits.slice(last + 1)}`;\n }\n const amount = Number(digits);\n return Number.isFinite(amount) && amount > 0 ? amount : undefined;\n};\n\nconst list = (value: unknown, limit: number): unknown[] =>\n (Array.isArray(value) ? value : value === undefined || value === null ? [] : [value]).slice(\n 0,\n limit,\n );\n\nconst objects = (value: unknown, limit: number): Json[] =>\n list(value, limit).filter(\n (item): item is Json => typeof item === \"object\" && item !== null && !Array.isArray(item),\n );\n\n/** This function gives a name, written as a text or as an object that has one. */\nconst nameOf = (value: unknown): string | null => {\n for (const item of list(value, 20)) {\n if (typeof item === \"string\" && item.trim().length > 0) return item.trim().slice(0, 500);\n if (typeof item === \"object\" && item !== null) {\n const named = (item as Json)[\"name\"];\n if (typeof named === \"string\" && named.trim().length > 0) return named.trim().slice(0, 500);\n }\n }\n return null;\n};\n\nconst textOf = (value: unknown): string | null =>\n typeof value === \"string\" && value.trim().length > 0\n ? value.trim().slice(0, 200)\n : typeof value === \"number\" && Number.isFinite(value)\n ? String(value)\n : null;\n\nconst imageOf = (value: unknown): string | null => {\n for (const item of list(value, 20)) {\n if (typeof item === \"string\" && /^https?:/i.test(item)) return item;\n const url = typeof item === \"object\" && item !== null ? (item as Json)[\"url\"] : undefined;\n if (typeof url === \"string\" && /^https?:/i.test(url)) return url;\n }\n return null;\n};\n\ninterface Offers {\n readonly prices: Money[];\n readonly unknownCurrencies: Set<string>;\n readonly availability: Set<string>;\n readonly sellers: Set<string>;\n /** An amount with no currency, which is not a price. */\n unstated: boolean;\n steps: number;\n}\n\nconst emptyOffers = (): Offers => ({\n prices: [],\n unknownCurrencies: new Set(),\n availability: new Set(),\n sellers: new Set(),\n unstated: false,\n steps: 0,\n});\n\n/** This function reads the offers of a product into `read`, and the offers of its variants to `depth` levels. */\nconst readOffers = (product: Json, read: Offers, depth: number): void => {\n const money = (value: unknown, currency: unknown): void => {\n const written = typeof currency === \"string\" ? currency.trim() : \"\";\n const code = SYMBOLS[written] ?? written.toUpperCase();\n const amount = readAmount(value, code.length > 0 ? code : undefined);\n if (amount === undefined) return;\n if (code.length === 0) {\n read.unstated = true;\n return;\n }\n if (!isCurrency(code)) {\n read.unknownCurrencies.add(written.slice(0, 20));\n return;\n }\n read.prices.push({\n amount,\n currency: code,\n raw: String(value).slice(0, PRODUCT_LIMITS.priceChars),\n });\n };\n const offer = (o: Json, level: number): void => {\n read.steps += 1;\n const spec = objects(o[\"priceSpecification\"], PRODUCT_LIMITS.offers).find((entry) => {\n const kind = entry[\"priceType\"];\n return !(typeof kind === \"string\" && WAS_PRICE.test(kind));\n });\n const currency = o[\"priceCurrency\"] ?? spec?.[\"priceCurrency\"];\n money(o[\"price\"] ?? spec?.[\"price\"], currency);\n money(o[\"lowPrice\"], currency);\n money(o[\"highPrice\"], currency);\n const availability =\n typeof o[\"availability\"] === \"string\"\n ? o[\"availability\"]\n .trim()\n .replace(/^.*[/#:]/, \"\")\n .slice(0, 60)\n : undefined;\n if (availability !== undefined && availability.length > 0) read.availability.add(availability);\n const seller = nameOf(o[\"seller\"]);\n if (seller !== null) read.sellers.add(seller);\n // an AggregateOffer can list its offers\n if (level < PRODUCT_LIMITS.depth) {\n for (const inner of objects(o[\"offers\"], PRODUCT_LIMITS.offers)) offer(inner, level + 1);\n }\n };\n for (const o of objects(product[\"offers\"], PRODUCT_LIMITS.offers)) offer(o, 0);\n if (depth <= 0) return;\n for (const variant of objects(product[\"hasVariant\"], PRODUCT_LIMITS.variants)) {\n read.steps += 1;\n readOffers(variant, read, depth - 1);\n }\n};\n\nconst availabilityOf = (read: Offers): string | null =>\n read.availability.size === 0\n ? null\n : read.availability.size === 1 || !read.availability.has(\"InStock\")\n ? [...read.availability][0]!\n : \"InStock\";\n\nconst pathKey = (value: string, page: string): string | undefined => {\n // \"#product\" names a part of this page: a related product can have such an id too\n if (value.startsWith(\"#\")) return undefined;\n try {\n const url = new URL(value, page);\n return `${url.hostname.toLowerCase().replace(/^www\\./, \"\")}${url.pathname.replace(/\\/$/, \"\")}`;\n } catch {\n return undefined;\n }\n};\n\n/** This function gives the size of a record as JSON, or 0 for a record that JSON cannot write (a nesting too deep). */\nconst sizeOf = (record: Json): number => {\n try {\n return JSON.stringify(record).length;\n } catch {\n return 0;\n }\n};\n\n/**\n * This function picks the product of a page from its JSON-LD records. A page often has more\n * records of products: the related products, the products that others bought. It picks:\n *\n * 1. The product whose `url` or `@id` is the page (the path, with no query).\n * 2. Else the largest product with offers or variants.\n * 3. Else the largest product.\n *\n * It also reads the `mainEntity` of a record (a `WebPage` or an `ItemPage` that names its product).\n */\nexport const pickProduct = (\n records: readonly Json[],\n page: string,\n): { readonly product: Json | undefined; readonly steps: number } => {\n let steps = 0;\n const products: Json[] = [];\n for (const record of records.slice(0, PRODUCT_LIMITS.records)) {\n steps += 1;\n if (typesOf(record as Record<string, unknown>).some((type) => PRODUCT_TYPES.test(type)))\n products.push(record);\n for (const inner of objects(record[\"mainEntity\"], 20)) {\n steps += 1;\n if (typesOf(inner as Record<string, unknown>).some((type) => PRODUCT_TYPES.test(type)))\n products.push(inner);\n }\n }\n const here = pathKey(page, page);\n const own = products.find((product) =>\n [product[\"url\"], product[\"@id\"]].some(\n (value) => typeof value === \"string\" && here !== undefined && pathKey(value, page) === here,\n ),\n );\n if (own !== undefined) return { product: own, steps };\n const selling = products.filter(\n (product) => list(product[\"offers\"], 1).length > 0 || list(product[\"hasVariant\"], 1).length > 0,\n );\n const pool = selling.length > 0 ? selling : products;\n let best: Json | undefined;\n let size = -1;\n for (const product of pool) {\n const length = sizeOf(product);\n steps += length;\n if (length > size) {\n best = product;\n size = length;\n }\n }\n return { product: best, steps };\n};\n\n/** This function gives the variant that a URL names: the `variant` parameter of Shopify, or a `sku` or `gtin` parameter. */\nexport const pickVariant = (product: Json, page: string): Json | undefined => {\n let params: URLSearchParams;\n try {\n params = new URL(page).searchParams;\n } catch {\n return undefined;\n }\n const variantId = params.get(\"variant\");\n const code = params.get(\"sku\") ?? params.get(\"gtin\");\n if (variantId === null && code === null) return undefined;\n const names = (value: unknown): boolean => {\n if (typeof value !== \"string\" || variantId === null) return false;\n try {\n return (\n new URL(value, page).searchParams.get(\"variant\") === variantId ||\n value.endsWith(`/${variantId}`)\n );\n } catch {\n return false;\n }\n };\n return objects(product[\"hasVariant\"], PRODUCT_LIMITS.variants).find((variant) => {\n const offers = objects(variant[\"offers\"], PRODUCT_LIMITS.offers);\n const codes = [\n variant[\"sku\"],\n variant[\"gtin\"],\n variant[\"gtin13\"],\n variant[\"gtin12\"],\n variant[\"productID\"],\n ]\n .map(textOf)\n .filter((one): one is string => one !== null);\n return (\n (code !== null && codes.includes(code)) ||\n [variant[\"@id\"], variant[\"url\"], ...offers.map((offer) => offer[\"url\"])].some(names) ||\n (variantId !== null && codes.includes(variantId))\n );\n });\n};\n\nconst byAmount = (a: Money, b: Money): number => a.amount - b.amount;\n\n/**\n * This function reads the Product data of a page from its JSON-LD records. It is pure, and it sends\n * no request. Thus it also reads a version at a time, or the stored JSON-LD of a document.\n *\n * - The prices are in the first currency of the offers. A price with no currency is not a price.\n * - A list price or a struck-through price is not the price.\n * - A currency must be an ISO 4217 code (`€` and `£` count too). The notes name each other one.\n */\nexport const productOf = (records: readonly Json[], page: string): ProductRead => {\n const picked = pickProduct(records, page);\n const product = picked.product;\n if (product === undefined) {\n return {\n found: false,\n name: null,\n brand: null,\n sku: null,\n gtin: null,\n price: null,\n price_to: null,\n availability: null,\n availabilities: [],\n variant: null,\n seller: null,\n image: null,\n notes: [\n \"no schema.org Product data on the page: a list of products, or a shop that does not publish it\",\n ],\n steps: picked.steps,\n };\n }\n const read = emptyOffers();\n readOffers(product, read, PRODUCT_LIMITS.depth);\n const currency = read.prices[0]?.currency;\n const sorted = read.prices.filter((price) => price.currency === currency).sort(byAmount);\n const low = sorted[0] ?? null;\n const high = sorted.at(-1) ?? null;\n const gtin = GTIN_KEYS.map((key) => textOf(product[key])).find((value) => value !== null);\n const chosen = pickVariant(product, page);\n let variant: ProductVariant | null = null;\n let steps = picked.steps + read.steps;\n if (chosen !== undefined) {\n const own = emptyOffers();\n readOffers(chosen, own, 0);\n steps += own.steps;\n const prices = own.prices.filter((price) => price.currency === currency).sort(byAmount);\n variant = {\n name: nameOf(chosen[\"name\"]),\n sku: textOf(chosen[\"sku\"]),\n price: prices[0] ?? null,\n availability: availabilityOf(own),\n };\n }\n const notes = [\n read.unknownCurrencies.size > 0\n ? `a currency that is not an ISO 4217 code: ${[...read.unknownCurrencies].join(\", \")}`\n : null,\n read.unstated && sorted.length === 0\n ? \"the page gives a price with no currency, so the price is not read as money\"\n : sorted.length === 0\n ? \"the page names the product and gives no price\"\n : null,\n new Set(read.prices.map((price) => price.currency)).size > 1\n ? `the offers have more than one currency: the prices are in ${currency ?? \"\"}`\n : null,\n /[?&](?:variant|sku|gtin)=/.test(page) && chosen === undefined\n ? \"the URL names a variant that the Product data does not list: the price is the whole range\"\n : null,\n ].filter((note): note is string => note !== null);\n return {\n found: true,\n name: nameOf(product[\"name\"]),\n brand: nameOf(product[\"brand\"]),\n sku: textOf(product[\"sku\"]),\n gtin: gtin ?? null,\n price: low,\n price_to: high !== null && low !== null && high.amount > low.amount ? high : null,\n availability: availabilityOf(read),\n availabilities: [...read.availability],\n variant,\n seller: read.sellers.size === 0 ? null : [...read.sellers].join(\", \"),\n image: imageOf(product[\"image\"]),\n notes,\n steps,\n };\n};\n\n// ── the recipe ──────────────────────────────────────────────────────────────\n\n/** These options control `readProduct`. */\nexport interface ProductOptions {\n /** The value `auto` (the default) renders once more when the plain page has no Product data. `never` does not render. */\n readonly render?: \"auto\" | \"never\" | \"always\" | undefined;\n /** A stored copy younger than this age answers with no request. The default is `24h`. */\n readonly maxAge?: Duration | undefined;\n /** These agents get their robots.txt rules applied too, for example `Claude-User`. */\n readonly onBehalfOf?: readonly string[] | undefined;\n readonly signal?: AbortSignal | undefined;\n /** No request starts after this time from the start of the read, for example `\"1m\"`. */\n readonly budget?: Duration | undefined;\n readonly scope?: string | undefined;\n readonly labels?: Readonly<Record<string, string>> | undefined;\n /** This name connects the captures to the job that asked for them. The default is `product`. */\n readonly job?: string | undefined;\n /** This value is true when a person gave this URL directly. It is the only way to read a held-back page. The crawler logs it. */\n readonly userProvided?: boolean | undefined;\n /** The value `archive` reads an archived copy of a page that the shop refuses. Its prices are of the date of the copy. */\n readonly fallback?: \"archive\" | \"none\" | undefined;\n readonly archiveDespiteRobots?: boolean | undefined;\n}\n\n/** The result of `readProduct`. */\nexport interface ProductResult extends Omit<ProductRead, \"steps\" | \"notes\"> {\n readonly url: string;\n /** This value is true when the page says what it sells, in structured data: the Product has a name. */\n readonly verified: boolean;\n /**\n * This value is true when the site refused the crawler. The causes are a 401, a 402 or a 403, a\n * challenge, a bot check, a stop of the host or robots.txt. The recipe stops there, and the note\n * says why. With `fallback: \"archive\"`, it stays true when an archived copy answers.\n */\n readonly wall: boolean;\n readonly outcome: string;\n readonly status: number;\n readonly rendered: boolean;\n readonly doc: string | null;\n /** This id is the capture of the text that the recipe read. A citation names it. */\n readonly capture: string | null;\n readonly fetched_at: string | null;\n readonly captures: readonly string[];\n readonly from_cache: boolean;\n /** This value is true when the page asked not to be AI input, so the recipe read nothing from it. */\n readonly held_back: boolean;\n /** This value tells that the page is an archived copy, with the archive and the date of the copy. */\n readonly archived?: Archived | undefined;\n readonly note: string | null;\n}\n\nconst WALLS: ReadonlySet<string> = new Set([\"blocked\", \"challenge\", \"payment-required\"]);\n\n/** This function tells if the site refused the crawler. */\nconst wallOf = (page: PageResult): boolean =>\n WALLS.has(page.outcome) ||\n (page.outcome === \"skipped\" && /^(?:robots\\.txt disallows|host stopped)/.test(page.note ?? \"\")) ||\n (page.doc === null && /\\bbot check\\b/.test(page.note ?? \"\"));\n\nconst answered = (page: PageResult): boolean =>\n page.doc !== null &&\n (page.outcome === \"ok\" || page.outcome === \"not-modified\" || page.archived !== undefined);\n\n/**\n * This function gives the JSON-LD records of a document from its stored body. The index keeps a\n * part of the JSON-LD of a large page only, and a product with many variants can be larger. An\n * archived copy gives the JSON-LD that the index keeps.\n */\nconst recordsOfDoc = async (runtime: CrawlRuntime, doc: Doc): Promise<readonly Json[]> => {\n const kept = Array.isArray(doc.meta[\"jsonld\"]) ? (doc.meta[\"jsonld\"] as Json[]) : [];\n if (doc.blob === null || archivedOf(doc.meta) !== undefined) return kept;\n const body = await runtime.blobs.get(doc.blob);\n if (body === undefined) return kept;\n const at = doc.url ?? \"\";\n const type = doc.rendered\n ? \"text/html; charset=utf-8\"\n : (runtime.index.latest(at, \"plain\")?.content_type ?? null);\n try {\n return extractPage(decodeBody(body, type), at).jsonld;\n } catch {\n return kept;\n }\n};\n\n/**\n * This function reads a product page. It fetches the page through the crawler: robots.txt, the\n * pace, the stops, the capture log and the rule for a held-back page apply. When the plain page has\n * no Product data, it renders the page once, because some shops write that data with JavaScript.\n *\n * A shop that refuses the crawler gives `wall: true` and a note. The recipe writes nothing but the\n * capture log. `extract` can check each value against the stored page.\n */\nexport const readProduct = async (\n target: RecipeTarget,\n url: string,\n options: ProductOptions = {},\n): Promise<ProductResult> => {\n const runtime = runtimeOf(target);\n try {\n const parsed = new URL(url);\n if (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\") throw new TypeError(url);\n } catch {\n throw new TypeError(`readProduct needs an http or https URL, not ${JSON.stringify(url)}`);\n }\n const maxAge = options.maxAge ?? \"24h\";\n const job = options.job ?? \"product\";\n const userProvided = options.userProvided ?? false;\n const render = options.render ?? \"auto\";\n const deadline = deadlineOf(runtime, options.budget);\n const pageOptions = (mode: \"auto\" | \"never\" | \"always\"): PageOptions => ({\n render: mode,\n maxAgeMs: maxAge === \"auto\" ? \"auto\" : durationMs(maxAge),\n onBehalfOf: options.onBehalfOf,\n job,\n signal: options.signal,\n scope: options.scope,\n labels: options.labels,\n deadline,\n fallback: options.fallback,\n archiveDespiteRobots: options.archiveDespiteRobots,\n });\n let page = await fetchPage(runtime, url, pageOptions(render));\n const notes: string[] = [];\n const result = (\n from: PageResult,\n read: Omit<ProductRead, \"steps\"> | undefined,\n more: { readonly held_back?: boolean } = {},\n ): ProductResult => {\n const all = [from.note, ...notes, ...(read?.notes ?? [])].filter(\n (part): part is string => part !== null && part !== undefined && part.length > 0,\n );\n return {\n url,\n found: read?.found ?? false,\n verified: read?.found === true && read.name !== null,\n name: read?.name ?? null,\n brand: read?.brand ?? null,\n sku: read?.sku ?? null,\n gtin: read?.gtin ?? null,\n price: read?.price ?? null,\n price_to: read?.price_to ?? null,\n availability: read?.availability ?? null,\n availabilities: read?.availabilities ?? [],\n variant: read?.variant ?? null,\n seller: read?.seller ?? null,\n image: read?.image ?? null,\n wall: wallOf(from),\n outcome: from.outcome,\n status: from.status,\n rendered: from.rendered,\n doc: from.doc,\n capture: from.capture,\n fetched_at: from.fetched_at,\n captures: [...from.captures],\n from_cache: from.from_cache,\n held_back: more.held_back ?? false,\n ...(from.archived === undefined ? {} : { archived: from.archived }),\n note: all.length > 0 ? [...new Set(all)].join(\"; \") : null,\n };\n };\n if (!answered(page)) {\n const wall = wallOf(page);\n notes.push(\n wall\n ? `the shop refused the crawler (${page.outcome}): the recipe read nothing, and it does not try a different identity`\n : `the page gave no document (${page.outcome})`,\n );\n return result(page, undefined);\n }\n const held = (from: PageResult): Doc | undefined => {\n const doc = from.doc === null ? undefined : runtime.index.get(from.doc);\n return doc !== undefined && isHeldBack(doc, userProvided) ? doc : undefined;\n };\n if (held(page) !== undefined) {\n notes.push(HELD_BACK);\n return result(page, undefined, { held_back: true });\n }\n const docOf = (from: PageResult): Doc | undefined =>\n from.doc === null ? undefined : runtime.index.get(from.doc);\n let doc = docOf(page);\n let read = doc === undefined ? undefined : productOf(await recordsOfDoc(runtime, doc), url);\n // some shops write their Product data with JavaScript: one render before the recipe says that the page has none\n if (read?.found !== true && !page.rendered && render === \"auto\" && page.archived === undefined) {\n const rendered = await fetchPage(runtime, url, pageOptions(\"always\"));\n const shown = docOf(rendered);\n if (\n rendered.rendered &&\n answered(rendered) &&\n shown !== undefined &&\n held(rendered) === undefined\n ) {\n page = rendered;\n doc = shown;\n read = productOf(await recordsOfDoc(runtime, shown), url);\n } else if (rendered.note !== null && !rendered.rendered) {\n notes.push(`the render gave nothing more: ${rendered.note}`);\n }\n }\n if (userProvided && doc?.ai_input === \"no\") await recordOverride(runtime, doc, job);\n return result(page, read);\n};\n"],"mappings":";;;;;;;AAmCA,MAAa,eAaT;CAAE,O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AA;CACpE;CACA,IAAI,KAAK,IAAI,MAAM,KAAA,GAAW;EAC5B,MAAM,KAAK,SAAS;EACpB,OAAO,OAAO,MAAM,KAAA,GAAW,EAAE,WAAW,KAAK,CAAC;CACpD;CACA,MAAM,SAAS,SACb,KAAK,QAAQ,OAAO,KAAA,IAAY,QAAQ,MAAM,IAAI,KAAK,GAAG;CAC5D,IAAI,MAAM,MAAM,IAAI;CACpB,IAAI,OAAO,QAAQ,KAAA,IAAY,KAAA,IAAY,UAAU,MAAM,aAAa,SAAS,GAAG,GAAG,GAAG;CAE1F,IAAI,MAAM,UAAU,QAAQ,CAAC,KAAK,YAAY,WAAW,UAAU,KAAK,aAAa,KAAA,GAAW;EAC9F,MAAM,WAAW,MAAM,UAAU,SAAS,KAAK,YAAY,QAAQ,CAAC;EACpE,MAAM,QAAQ,MAAM,QAAQ;EAC5B,IACE,SAAS,YACT,SAAS,QAAQ,KACjB,UAAU,KAAA,KACV,KAAK,QAAQ,MAAM,KAAA,GACnB;GACA,OAAO;GACP,MAAM;GACN,OAAO,UAAU,MAAM,aAAa,SAAS,KAAK,GAAG,GAAG;EAC1D,OAAO,IAAI,SAAS,SAAS,QAAQ,CAAC,SAAS,UAC7C,MAAM,KAAK,iCAAiC,SAAS,MAAM;CAE/D;CACA,IAAI,gBAAgB,KAAK,aAAa,MAAM,MAAM,eAAe,SAAS,KAAK,GAAG;CAClF,OAAO,OAAO,MAAM,IAAI;AAC1B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"render-C1CUraWF.mjs","names":[],"sources":["../src/render.ts"],"sourcesContent":["import type { APIResponse, Browser, BrowserContext, Page, Request, Route } from \"playwright-core\";\n\n/**\n * The renderer opens the headless browser only for a page that shows almost nothing without\n * JavaScript. The settings:\n *\n * - The default `chromium-headless-shell` of Playwright, the lighter build.\n * - One browser for many pages, and a new context for each page. Thus no cookie or storage goes\n * from one site to the next. A context has its own HTTP cache, so routing each request costs\n * no cache across sites.\n * - Each request of the page goes through a route, the first navigation too. The route refuses\n * images, fonts and media by their resource type. It asks `allowRequest` about each other\n * request, and it sends `headers` with it.\n * - Playwright routes only the first request of a redirect chain. Thus with `allowRequest`, the\n * route sends each request itself, follows each redirect itself and asks about each step. The\n * browser then gets each body whole, not as a stream.\n * - No download, no popup, no service worker and no dialog.\n * - A wait until the visible text stops growing, with a limit. Playwright discourages\n * `networkidle`. The text includes open shadow roots and the frames of the same origin.\n * - One deadline for the whole render: the navigation, the wait and the reading of the page.\n * - After a crash or a disconnection, the next page launches a new browser.\n * - The browser closes after two idle minutes. An idle Chromium holds hundreds of megabytes, so\n * it stays open only while pages need it. The open browser keeps the process alive, so a\n * script uses `close()` when it is done.\n */\n\nexport interface Rendered {\n readonly html: string;\n readonly finalUrl: string;\n readonly status: number | null;\n readonly headers: Readonly<Record<string, string>>;\n}\n\n/** These are the options of one render. */\nexport interface RenderOptions {\n readonly userAgent: string;\n /** This value is the deadline of the whole render, in milliseconds. */\n readonly timeoutMs: number;\n /** These headers go with each request of the page, for example the `From` header. */\n readonly headers?: Readonly<Record<string, string>> | undefined;\n /**\n * This function decides each request that the page makes, the first navigation included. When\n * it gives false, the browser does not send the request. The crawler uses it to apply the\n * egress policy and robots.txt to the requests of the page.\n */\n readonly allowRequest?: ((url: string) => Promise<boolean> | boolean) | undefined;\n /** This value is the maximum number of redirects of one request of the page. The default is 5, as for a plain fetch. */\n readonly maxRedirects?: number | undefined;\n /** When this signal aborts, the renderer stops the page: it closes the context, and the render rejects. */\n readonly signal?: AbortSignal | undefined;\n /**\n * This function gives the signature headers of one request of the page (Web Bot Auth). The route\n * adds them to the request and to each step of its redirects. When the function gives undefined,\n * the browser does not send the request.\n */\n readonly sign?: ((url: string) => Readonly<Record<string, string>> | undefined) | undefined;\n}\n\nexport interface Renderer {\n render(url: string, options: RenderOptions): Promise<Rendered>;\n close(): Promise<void>;\n}\n\n/** The reason of a `RenderRefused`. */\nexport type RefusedReason = \"refused\" | \"download\" | \"scheme\" | \"browser\";\n\n/**\n * This error tells that the renderer did not render the page, and that the page or the site is\n * not at fault. The reason is one of these:\n *\n * - `refused`: `allowRequest` refused the first navigation, a step of its redirects, or a later\n * navigation of the page that left it on an error page. `url` names the refused URL.\n * - `download`: the URL gives a file, not a page.\n * - `scheme`: the URL is not an http or https URL.\n * - `browser`: the renderer has no browser (`playwright-core` or its browser is not installed). The\n * crawler then renders no more pages, and gives each one the note at once.\n *\n * A crawler does not count this error as a failure of the host.\n */\nexport class RenderRefused extends Error {\n readonly reason: RefusedReason;\n /** This URL is the request that `allowRequest` or the limit of redirects refused, when the reason is `refused`. */\n readonly url: string | undefined;\n constructor(reason: RefusedReason, message: string, url?: string) {\n super(message);\n this.name = \"RenderRefused\";\n this.reason = reason;\n this.url = url;\n }\n}\n\n/** The browser does not ask for these. The text of a page needs none of them. */\nconst BLOCKED_TYPES = new Set([\"image\", \"font\", \"media\"]);\n/** These statuses send the request on to the URL in `Location` (the Fetch standard). */\nconst REDIRECTS = new Set([301, 302, 303, 307, 308]);\nconst MAX_REDIRECTS = 5;\n\n/** The text is stable when it does not grow for this time. */\nconst SETTLED_MS = 1000;\nconst POLL_MS = 250;\nconst SETTLE_CAP_MS = 12_000;\n/** A page with no text after this time has nothing more to show. */\nconst EMPTY_SETTLE_MS = 3000;\n/** The part of the deadline that is kept to read the page after the wait. */\nconst READ_RESERVE_MS = 2000;\n/** A context that does not close in this time takes its browser with it. */\nconst CLOSE_MS = 5000;\n/** A browser that does not open a context in this time is replaced. */\nconst OPEN_MS = 5000;\n\n/** The text of the error when the optional package `playwright-core` is not installed. */\nexport const NO_PLAYWRIGHT =\n \"rendering needs the package playwright-core and a browser: npm install playwright-core, then npx playwright-core install chromium-headless-shell. For the MCP server with npx, start it with npx -y -p polite-crawler -p playwright-core polite-crawler mcp\";\n\n// playwright-core is an optional peer: it loads only when a page first needs a browser\nconst launch = async (): Promise<Browser> => {\n let chromium: typeof import(\"playwright-core\").chromium;\n try {\n ({ chromium } = await import(\"playwright-core\"));\n } catch (cause) {\n throw new Error(NO_PLAYWRIGHT, { cause });\n }\n return chromium.launch({ headless: true });\n};\n\n/** This script gives the length of the text that the page shows: its body, its open shadow roots and its frames of the same origin. */\nconst MEASURE = `(() => {\n let total = document.body ? document.body.innerText.length : 0;\n const visit = (root) => {\n for (const element of root.querySelectorAll(\"*\")) {\n if (element.shadowRoot) {\n total += (element.shadowRoot.textContent || \"\").length;\n visit(element.shadowRoot);\n }\n }\n };\n visit(document);\n for (const frame of document.querySelectorAll(\"iframe, frame\")) {\n try {\n const inner = frame.contentDocument;\n if (inner && inner.body) total += inner.body.innerText.length;\n } catch (error) {}\n }\n return total;\n})()`;\n\n/** This script gives the HTML of the page with its open shadow roots, and the body of each frame of the same origin. */\nconst SERIALIZE = `(() => {\n const roots = [];\n const visit = (root) => {\n for (const element of root.querySelectorAll(\"*\")) {\n if (element.shadowRoot) {\n roots.push(element.shadowRoot);\n visit(element.shadowRoot);\n }\n }\n };\n visit(document);\n const root = document.documentElement;\n if (!root) return { html: \"\", frames: [] };\n const quote = (value) => value.replace(/&/g, \"&amp;\").replace(/\"/g, \"&quot;\");\n const open = \"<html\" + Array.from(root.attributes, (item) => \" \" + item.name + '=\"' + quote(item.value) + '\"').join(\"\") + \">\";\n const inner =\n roots.length > 0 && typeof root.getHTML === \"function\"\n ? root.getHTML({ serializableShadowRoots: true, shadowRoots: roots })\n : root.innerHTML;\n const frames = [];\n for (const frame of document.querySelectorAll(\"iframe, frame\")) {\n try {\n const doc = frame.contentDocument;\n if (doc && doc.body) frames.push({ src: frame.src || \"\", html: doc.body.innerHTML });\n } catch (error) {}\n }\n return { html: \"<!DOCTYPE html>\" + open + inner + \"</html>\", frames };\n})()`;\n\ninterface Serialized {\n readonly html: string;\n readonly frames: ReadonlyArray<{ readonly src: string; readonly html: string }>;\n}\n\n/** This function puts the body of each frame at the end of the page, in a section that names the frame. */\nconst withFrames = ({ html, frames }: Serialized): string => {\n if (frames.length === 0) return html;\n const sections = frames\n .map(\n ({ src, html: body }) =>\n `<section data-polite-frame=\"${src.replace(/&/g, \"&amp;\").replace(/\"/g, \"&quot;\")}\">${body}</section>`,\n )\n .join(\"\");\n const end = html.toLowerCase().lastIndexOf(\"</body>\");\n return end === -1 ? `${html}${sections}` : `${html.slice(0, end)}${sections}${html.slice(end)}`;\n};\n\nconst messageOf = (error: unknown): string =>\n error instanceof Error ? error.message : String(error);\n\n/** This function gives the headers with their names in lowercase, as the route sends them. */\nconst lowerKeys = (headers: Readonly<Record<string, string>>): Record<string, string> =>\n Object.fromEntries(Object.entries(headers).map(([name, value]) => [name.toLowerCase(), value]));\n\nconst sleep = (ms: number): Promise<void> =>\n new Promise((resolve) => {\n setTimeout(resolve, Math.max(0, ms)).unref();\n });\n\n/** This function gives the value of a promise, or undefined when the time ends first. */\nconst within = async <T>(promise: Promise<T>, ms: number): Promise<T | undefined> => {\n let timer: ReturnType<typeof setTimeout> | undefined;\n const late = new Promise<undefined>((resolve) => {\n timer = setTimeout(() => resolve(undefined), Math.max(0, ms));\n });\n try {\n return await Promise.race([promise, late]);\n } finally {\n clearTimeout(timer);\n }\n};\n\nexport interface PlaywrightRenderer extends Renderer {\n /** This function tells if a browser is open. */\n readonly open: () => boolean;\n}\n\nexport const playwrightRenderer = (\n options: {\n readonly idleMs?: number;\n /**\n * This function starts the browser: it gives a `Browser` of `playwright-core`. The default is\n * the headless shell of Playwright. The type names no type of `playwright-core`, so a program\n * with no `playwright-core` can check its types.\n */\n readonly launch?: (() => Promise<object>) | undefined;\n } = {},\n): PlaywrightRenderer => {\n const idleMs = options.idleMs ?? 120_000;\n const start = (options.launch ?? launch) as () => Promise<Browser>;\n let browser: Promise<Browser> | undefined;\n let inFlight = 0;\n let idle: ReturnType<typeof setTimeout> | undefined;\n const shutSoon = (): void => {\n if (idle !== undefined) clearTimeout(idle);\n idle = setTimeout(() => {\n idle = undefined;\n if (inFlight > 0 || browser === undefined) return;\n const closing = browser;\n browser = undefined;\n void closing.then((running) => running.close()).catch(() => undefined);\n }, idleMs);\n // the timer alone does not keep the process alive; the open browser does\n idle.unref();\n };\n const acquire = async (): Promise<Browser> => {\n for (let attempt = 0; ; attempt += 1) {\n if (browser === undefined) {\n const launching: Promise<Browser> = start().then((running) => {\n // a crash, a kill or a closed pipe: the next page launches a new browser\n running.on(\"disconnected\", () => {\n if (browser === launching) browser = undefined;\n });\n return running;\n });\n // a failed launch is not kept, so the next page tries again; a newer\n // launch that replaced this one is not touched\n launching.catch(() => {\n if (browser === launching) browser = undefined;\n });\n browser = launching;\n }\n const held = browser;\n const running = await held;\n if (running.isConnected() || attempt > 0) return running;\n if (browser === held) browser = undefined;\n }\n };\n /** This function gives up on a browser that does not answer. The next page launches a new one. */\n const forget = (running: Browser): void => {\n const held = browser;\n if (held !== undefined) {\n void held.then(\n (current) => {\n if (current === running && browser === held) browser = undefined;\n },\n () => undefined,\n );\n }\n void running.close().catch(() => undefined);\n };\n\n const renderPage = async (\n url: string,\n {\n userAgent,\n timeoutMs,\n headers,\n allowRequest,\n maxRedirects = MAX_REDIRECTS,\n signal,\n sign,\n }: RenderOptions,\n ): Promise<Rendered> => {\n if (signal?.aborted === true)\n throw signal.reason instanceof Error ? signal.reason : new Error(\"the render was cancelled\");\n const deadline = Date.now() + timeoutMs;\n const left = (): number => Math.max(1, deadline - Date.now());\n let running: Browser | undefined;\n let context: BrowserContext | undefined;\n let shownPage: Page | undefined;\n /** The last navigation of the page that the route refused: the reason, and the refused URL. */\n let refused: { readonly message: string; readonly url: string } | undefined;\n /** The last navigation of the page that the route answered: the URL that the browser asked for, and the URL of the answer. */\n let navigated: { readonly asked: string; readonly final: string } | undefined;\n const extra = Object.fromEntries(\n Object.entries(headers ?? {}).map(([name, value]) => [name.toLowerCase(), value]),\n );\n const contextOptions = {\n userAgent,\n serviceWorkers: \"block\" as const,\n acceptDownloads: false,\n viewport: { width: 1280, height: 900 },\n };\n const ask = async (url: string): Promise<boolean> =>\n allowRequest === undefined\n ? true\n : Promise.resolve()\n .then(() => allowRequest(url))\n .catch(() => false);\n /**\n * This function sends one request of the page and follows its redirects. Each step is a new\n * decision of `allowRequest`, and a step past `maxRedirects` is refused. It gives the answer,\n * or the reason of a refusal.\n *\n * These rules are the rules of a browser. Each step gets the cookies of its own URL from the\n * context. A credential does not go to a different origin. A 303, or a 301 or 302 after a POST,\n * changes the request to a GET.\n */\n const follow = async (\n route: Route,\n sent: Record<string, string> | undefined,\n ): Promise<\n | { readonly response: APIResponse; readonly url: string }\n | { readonly refusal: string; readonly url: string }\n > => {\n const request = route.request();\n let url = request.url();\n let method = request.method();\n let headers: Record<string, string> = { ...request.headers(), ...extra };\n let response = await route.fetch({\n maxRedirects: 0,\n timeout: left(),\n ...(sent === undefined ? {} : { headers: sent }),\n });\n for (let hop = 0; ; hop += 1) {\n const location = REDIRECTS.has(response.status())\n ? response.headers()[\"location\"]\n : undefined;\n if (location === undefined) return { response, url };\n let next: URL;\n try {\n next = new URL(location, url);\n } catch {\n return { refusal: `${url} redirects to a URL that does not parse`, url };\n }\n if (hop >= maxRedirects) {\n return {\n refusal: `${request.url()} redirects more than ${String(maxRedirects)} times`,\n url: next.href,\n };\n }\n if (!(await ask(next.href))) {\n return {\n refusal: `the request for ${next.href}, a redirect of ${url}, was refused`,\n url: next.href,\n };\n }\n const status = response.status();\n const get = status === 303 || ((status === 301 || status === 302) && method === \"POST\");\n const crossOrigin = next.origin !== new URL(url).origin;\n headers = Object.fromEntries(\n Object.entries(headers).filter(\n ([name]) =>\n name !== \"cookie\" &&\n !(crossOrigin && name === \"authorization\") &&\n !(get && (name === \"content-type\" || name === \"content-length\")),\n ),\n );\n if (get) method = \"GET\";\n // ── signed requests: each step of the redirects gets a signature for its own URL ──\n if (sign !== undefined) {\n const signed = sign(next.href);\n if (signed === undefined)\n return {\n refusal: `the signer did not sign the request for ${next.href}`,\n url: next.href,\n };\n headers = { ...headers, ...lowerKeys(signed) };\n }\n const body = get ? undefined : (request.postDataBuffer() ?? undefined);\n response = await context!.request.fetch(next.href, {\n method,\n headers,\n maxRedirects: 0,\n timeout: left(),\n ...(body === undefined ? {} : { data: body }),\n });\n url = next.href;\n }\n };\n /** This function tells if a request is a navigation of the page itself, not of a frame or a popup. */\n const ofPage = (request: Request): boolean => {\n if (!request.isNavigationRequest()) return false;\n try {\n return request.frame().parentFrame() === null && request.frame().page() === shownPage;\n } catch {\n // a popup asks for its first page before it has a frame\n return false;\n }\n };\n const decide = async (route: Route): Promise<void> => {\n const request = route.request();\n const main = ofPage(request);\n try {\n if (BLOCKED_TYPES.has(request.resourceType())) {\n await route.abort(\"blockedbyclient\");\n return;\n }\n if (!(await ask(request.url()))) {\n if (main)\n refused = {\n message: `the request for ${request.url()} was refused`,\n url: request.url(),\n };\n await route.abort(\"blockedbyclient\");\n return;\n }\n // ── signed requests: a request that the signer does not sign does not go out ──\n const signed = sign === undefined ? {} : sign(request.url());\n if (signed === undefined) {\n if (main)\n refused = {\n message: `the signer did not sign the request for ${request.url()}`,\n url: request.url(),\n };\n await route.abort(\"blockedbyclient\");\n return;\n }\n const added = { ...extra, ...lowerKeys(signed) };\n const sent =\n Object.keys(added).length === 0\n ? undefined\n : {\n ...Object.fromEntries(\n Object.entries(await request.allHeaders()).filter(\n ([name]) => !name.startsWith(\":\") && name !== \"host\",\n ),\n ),\n ...added,\n };\n if (allowRequest === undefined) {\n await route.continue(sent === undefined ? undefined : { headers: sent });\n return;\n }\n const answer = await follow(route, sent);\n if (\"refusal\" in answer) {\n if (main) refused = { message: answer.refusal, url: answer.url };\n await route.abort(\"blockedbyclient\");\n return;\n }\n if (main) navigated = { asked: request.url(), final: answer.url };\n await route.fulfill({ response: answer.response });\n } catch {\n // the page closed while the request waited: nothing to answer\n }\n };\n const work = (async (): Promise<Rendered> => {\n running = await acquire();\n // a browser that was killed can keep its pipe open and answer nothing:\n // it gets a few seconds, and then a new browser takes its place\n const opened = await within(\n running.newContext(contextOptions).catch((error: unknown) => {\n if (!/closed|disconnected/i.test(messageOf(error))) throw error;\n return undefined;\n }),\n Math.min(OPEN_MS, left()),\n );\n if (opened === undefined) {\n forget(running);\n running = await acquire();\n context = await running.newContext(contextOptions);\n } else {\n context = opened;\n }\n await context.route(\"**/*\", decide);\n if (allowRequest !== undefined && typeof context.routeWebSocket === \"function\") {\n await context.routeWebSocket(/.*/, async (socket) => {\n const allowed = await Promise.resolve()\n .then(() => allowRequest(socket.url().replace(/^ws/i, \"http\")))\n .catch(() => false);\n if (allowed) socket.connectToServer();\n else await socket.close({ code: 1008, reason: \"refused\" }).catch(() => undefined);\n });\n }\n const page: Page = await context.newPage();\n shownPage = page;\n // a popup is not the page, and a dialog waits for no one\n context.on(\"page\", (other) => {\n if (other !== page) void other.close().catch(() => undefined);\n });\n page.on(\"dialog\", (dialog) => {\n void dialog.dismiss().catch(() => undefined);\n });\n let response;\n try {\n response = await page.goto(url, {\n waitUntil: \"domcontentloaded\",\n timeout: Math.max(1, left() - READ_RESERVE_MS),\n });\n } catch (error) {\n if (refused !== undefined) throw new RenderRefused(\"refused\", refused.message, refused.url);\n if (/Download is starting/i.test(messageOf(error))) {\n throw new RenderRefused(\"download\", `${url} gives a file, not a page`);\n }\n throw error;\n }\n const settleEnd = Math.min(deadline - READ_RESERVE_MS, Date.now() + SETTLE_CAP_MS);\n const settleStart = Date.now();\n let last = -1;\n let since = Date.now();\n while (Date.now() < settleEnd) {\n const measured = (page.evaluate(MEASURE) as Promise<number>).catch(\n async (error: unknown) => {\n // a script of the page can start a navigation while the text is measured: the next poll measures the new document\n if (!/destroyed|navigat/i.test(messageOf(error))) throw error;\n await page\n .waitForLoadState(\"domcontentloaded\", { timeout: left() })\n .catch(() => undefined);\n // no length equals NaN, so the wait goes on\n return Number.NaN;\n },\n );\n const length = await within(measured, settleEnd - Date.now());\n // a page that keeps its thread busy does not answer: the reading below decides\n if (length === undefined) break;\n if (length !== last) {\n last = length;\n since = Date.now();\n } else if (\n Date.now() - since >= SETTLED_MS &&\n (length > 0 || Date.now() - settleStart >= EMPTY_SETTLE_MS)\n ) {\n break;\n }\n await sleep(Math.min(POLL_MS, settleEnd - Date.now()));\n }\n // a navigation can replace the document while it is read: the read waits for the new document\n // and tries again, until the deadline (a page can navigate many times in a row)\n let serialized: Serialized | undefined;\n while (serialized === undefined) {\n try {\n serialized = (await page.evaluate(SERIALIZE)) as Serialized;\n } catch (error) {\n if (!/destroyed|navigat/i.test(messageOf(error)) || Date.now() >= deadline) throw error;\n await page\n .waitForLoadState(\"domcontentloaded\", { timeout: left() })\n .catch(() => undefined);\n }\n }\n // a later navigation that the route refused leaves the browser on its error page, which is not the page\n if (refused !== undefined && page.url().startsWith(\"chrome-error:\")) {\n throw new RenderRefused(\"refused\", `the page moved: ${refused.message}`, refused.url);\n }\n // the route followed the redirects of the navigation, so the browser shows the first URL\n const shown = page.url().replace(/#.*$/s, \"\");\n return {\n html: withFrames(serialized),\n finalUrl:\n navigated !== undefined && shown === navigated.asked ? navigated.final : page.url(),\n status: response?.status() ?? null,\n headers: response?.headers() ?? {},\n };\n })();\n // the deadline can win the race; the work then ends when its context closes\n work.catch(() => undefined);\n let timer: ReturnType<typeof setTimeout> | undefined;\n let cancel: (() => void) | undefined;\n const expired = new Promise<never>((_, reject) => {\n timer = setTimeout(\n () => reject(new Error(`render timed out after ${String(timeoutMs)} ms`)),\n timeoutMs,\n );\n // a cancelled render ends now; the context closes below, and that stops the page\n if (signal !== undefined) {\n cancel = () =>\n reject(\n signal.reason instanceof Error ? signal.reason : new Error(\"the render was cancelled\"),\n );\n signal.addEventListener(\"abort\", cancel, { once: true });\n }\n });\n const closeContext = async (): Promise<void> => {\n const open = context;\n if (open === undefined) return;\n const closed = await within(\n open.close().then(\n () => true,\n () => true,\n ),\n CLOSE_MS,\n );\n if (closed === undefined && running !== undefined) forget(running);\n };\n try {\n return await Promise.race([work, expired]);\n } finally {\n clearTimeout(timer);\n if (cancel !== undefined) signal?.removeEventListener(\"abort\", cancel);\n if (context !== undefined) await closeContext();\n // a context made after the deadline is closed when it arrives\n else void work.then(closeContext, closeContext).catch(() => undefined);\n }\n };\n\n return {\n open: () => browser !== undefined,\n async render(url, renderOptions) {\n let target: URL;\n try {\n target = new URL(url);\n } catch {\n throw new RenderRefused(\"scheme\", `${url} is not a URL`);\n }\n if (target.protocol !== \"http:\" && target.protocol !== \"https:\") {\n throw new RenderRefused(\n \"scheme\",\n `the renderer reads http and https pages, not ${target.protocol}`,\n );\n }\n if (idle !== undefined) {\n clearTimeout(idle);\n idle = undefined;\n }\n inFlight += 1;\n try {\n return await renderPage(target.href, renderOptions);\n } finally {\n inFlight -= 1;\n if (inFlight === 0) shutSoon();\n }\n },\n async close() {\n if (idle !== undefined) clearTimeout(idle);\n idle = undefined;\n const launched = browser;\n browser = undefined;\n // a launch that failed has nothing to close, and closing never throws:\n // the idle timer calls this with no one to catch it\n if (launched !== undefined)\n await launched.then(\n (open) => open.close(),\n () => undefined,\n );\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;AA+EA,IAAa,gBAAb,cAAmC,MAAM;CACvC;;CAEA;CACA,YAAY,QAAuB,SAAiB,KAAc;EAChE,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,SAAS;EACd,KAAK,MAAM;CACb;AACF;;AAGA,MAAM,gCAAgB,IAAI,IAAI;CAAC;CAAS;CAAQ;AAAO,CAAC;;AAExD,MAAM,4BAAY,IAAI,IAAI;CAAC;CAAK;CAAK;CAAK;CAAK;AAAG,CAAC;AACnD,MAAM,gBAAgB;;AAGtB,MAAM,aAAa;AACnB,MAAM,UAAU;AAChB,MAAM,gBAAgB;;AAEtB,MAAM,kBAAkB;;AAExB,MAAM,kBAAkB;;AAExB,MAAM,WAAW;;AAEjB,MAAM,UAAU;;AAGhB,MAAa,gBACX;AAGF,MAAM,SAAS,YAA8B;CAC3C,IAAI;CACJ,IAAI;EACF,CAAC,CAAE,YAAa,MAAM,OAAO;CAC/B,SAAS,OAAO;EACd,MAAM,IAAI,MAAM,eAAe,EAAE,MAAM,CAAC;CAC1C;CACA,OAAO,SAAS,OAAO,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CAEA,OAAO;EACL,YAAY,YAAY,KAAA;EACxB,MAAM,OAAO,KAAK,eAAe;GAC/B,IAAI;GACJ,IAAI;IACF,SAAS,IAAI,IAAI,GAAG;GACtB,QAAQ;IACN,MAAM,IAAI,cAAc,UAAU,GAAG,IAAI,cAAc;GACzD;GACA,IAAI,OAAO,aAAa,WAAW,OAAO,aAAa,UACrD,MAAM,IAAI,cACR,UACA,gDAAgD,OAAO,UACzD;GAEF,IAAI,SAAS,KAAA,GAAW;IACtB,aAAa,IAAI;IACjB,OAAO,KAAA;GACT;GACA,YAAY;GACZ,IAAI;IACF,OAAO,MAAM,WAAW,OAAO,MAAM,aAAa;GACpD,UAAU;IACR,YAAY;IACZ,IAAI,aAAa,GAAG,SAAS;GAC/B;EACF;EACA,MAAM,QAAQ;GACZ,IAAI,SAAS,KAAA,GAAW,aAAa,IAAI;GACzC,OAAO,KAAA;GACP,MAAM,WAAW;GACjB,UAAU,KAAA;GAGV,IAAI,aAAa,KAAA,GACf,MAAM,SAAS,MACZ,SAAS,KAAK,MAAM,SACf,KAAA,CACR;EACJ;CACF;AACF"}
@@ -1,144 +0,0 @@
1
- //#region src/robots.d.ts
2
- /**
3
- * This module reads robots.txt as RFC 9309 (September 2022) specifies it. It is for a crawler that
4
- * tells who it is and obeys what a site says. The important rules, each with a test:
5
- *
6
- * - A group is one or more `user-agent` lines and the rules after them. A `user-agent` line after
7
- * an `allow` or `disallow` line starts a new group, as the parser of Google does. A different
8
- * line (`crawl-delay`) between two `user-agent` lines does not split the group. A rule before
9
- * the first `user-agent` line is in no group.
10
- * - The product token matches with no attention to case. The matching groups merge. The group
11
- * `*` applies only when no group names the crawler.
12
- * - The longest matching rule wins, and `allow` wins a tie. A `*` matches any sequence, and a `$`
13
- * at the end anchors the end. The matcher uses no regular expression, and its time is linear.
14
- * - `/robots.txt` is always permitted, and an empty `disallow` matches nothing.
15
- * - The parser reads at least the first 500 KiB.
16
- * - A usage line (`content-signal`, `content-usage`) belongs to its group, with an optional path
17
- * before its value. A line before the first group applies to each crawler.
18
- *
19
- * The function `robotsFromStatus` gives the meaning of the status of the fetch. A 4xx is
20
- * "unavailable", so all paths are permitted. A 5xx or no response is "unreachable", so no path is
21
- * permitted. A 429 is unreachable too, as Google does it. A site that limits the rate of its robots
22
- * file does not ask for a crawl.
23
- */
24
- declare const ROBOTS_MAX_BYTES: number;
25
- interface RobotsRule {
26
- readonly allow: boolean;
27
- /** This value is the path pattern, normalized for comparison. */
28
- readonly pattern: string;
29
- /** This value is the number of the line in robots.txt, from 1. A rule that the code makes has none. */
30
- readonly line?: number | undefined;
31
- }
32
- /** A usage line states a preference about the use of the content (`content-signal`, `content-usage`). */
33
- interface RobotsSignal {
34
- /** This value is the name of the line, in lowercase. */
35
- readonly key: string;
36
- /** This text is the value of the line, after the path. */
37
- readonly value: string;
38
- /** This value is the normalized path pattern of the line. A line with no path applies to each path. */
39
- readonly path: string | undefined;
40
- /** This value is the number of the line in robots.txt, from 1. */
41
- readonly line: number;
42
- }
43
- interface RobotsGroup {
44
- /** These values are the user agents of the group, in lowercase. */
45
- readonly agents: readonly string[];
46
- /** This value is the number of the first `user-agent` line of the group, from 1. */
47
- readonly line?: number | undefined;
48
- readonly rules: readonly RobotsRule[];
49
- /** This value is the nonstandard `crawl-delay` in seconds, when the group sets one. */
50
- readonly crawlDelaySeconds: number | undefined;
51
- /** These usage lines are in the group. They apply when the group applies. */
52
- readonly signals?: readonly RobotsSignal[] | undefined;
53
- }
54
- interface Robots {
55
- readonly groups: readonly RobotsGroup[];
56
- readonly sitemaps: readonly string[];
57
- /** These lines state usage preferences (`content-signal: …`, `content-usage: …`), as written, from each group. */
58
- readonly signals: readonly string[];
59
- /** These usage lines come before the first group, so they apply to each crawler. */
60
- readonly fileSignals?: readonly RobotsSignal[] | undefined;
61
- /**
62
- * This text tells why the crawler could not read robots.txt: a dead domain, a broken certificate
63
- * or a server error. Then no path is permitted, and the note of a skip gives this reason. It does
64
- * not name a rule of the site.
65
- */
66
- readonly unreachable?: string | undefined;
67
- }
68
- declare const ALLOW_ALL: Robots;
69
- declare const DISALLOW_ALL: Robots;
70
- /**
71
- * This function normalizes a path, or the pattern of a rule, for comparison, as §2.2.2 asks. It
72
- * encodes the characters outside US-ASCII, and the ASCII characters that the URL parser encodes
73
- * (a space, `"`, `<`, `{`). It decodes an escape of an unreserved character, so `/%7Efoo` and
74
- * `/~foo` are equal. The other escapes stay, with uppercase hexadecimal digits.
75
- */
76
- declare const normalizePath: (value: string) => string;
77
- /** This function tells if a text is a product token: letters, `_` and `-` only, and one or more (RFC 9309, section 2.2.1). */
78
- declare const isProductToken: (value: string) => boolean;
79
- /**
80
- * This function gives the product token that robots.txt rules match, in lowercase. `MyBot/1.0`
81
- * gives `mybot`. The full header `Mozilla/5.0 (compatible; MyBot/1.0; +https://…)` also gives
82
- * `mybot`. The token is the name after `compatible;`, or else the first product that is not a part
83
- * of a browser.
84
- */
85
- declare const productToken: (value: string) => string;
86
- declare const parseRobots: (input: string) => Robots;
87
- /**
88
- * This function tells if a normalized path matches the pattern of a rule. A `*` matches any
89
- * sequence, and a `$` at the end anchors the end. The function finds the parts between the stars
90
- * in sequence, each at its first position. No pattern makes it slow: its time is linear in the
91
- * length of the path for each part, and it uses no regular expression.
92
- */
93
- declare const matchesPattern: (pattern: string, path: string) => boolean;
94
- interface RobotsDecision {
95
- readonly allowed: boolean;
96
- /** This value is the product token whose rules refused the path. */
97
- readonly by: string | undefined;
98
- readonly crawlDelaySeconds: number | undefined;
99
- /** This rule decided. When no rule matches the path, the path is permitted and this value is empty. */
100
- readonly rule?: RobotsRule | undefined;
101
- /** These groups applied to the decision: the groups that name the token, or the group `*`. */
102
- readonly groups?: readonly RobotsGroup[] | undefined;
103
- }
104
- /**
105
- * This function tells if these tokens can fetch `pathAndQuery`. Each token becomes its product
106
- * token (`productToken`), so `MyBot/1.0` matches the group `mybot`. The first token is the token of
107
- * the crawler, and `*` applies when no group names it. A further token, for example `Claude-User`,
108
- * applies only when the site names it. A site that writes rules for an agent means them for the
109
- * fetches of that agent.
110
- */
111
- declare const decide: (robots: Robots, tokens: readonly string[], pathAndQuery: string) => RobotsDecision;
112
- /**
113
- * This function gives the usage lines that apply to these tokens and this path, as
114
- * `key: value` texts. The lines come from three places:
115
- *
116
- * - the lines before the first group
117
- * - the groups of the first token, or `*`
118
- * - the groups that name a further token
119
- *
120
- * In one group, the line with the longest matching path wins for each key.
121
- */
122
- declare const signalsFor: (robots: Robots, tokens: readonly string[], pathAndQuery: string) => string[];
123
- /** This function gives the robots of a site whose robots.txt the crawler cannot read. No path is permitted, and the reason stays. */
124
- declare const unreachableRobots: (reason: string) => Robots;
125
- /** This text is the reason when a robots.txt that answered 200 is a web page, not a robots.txt file. */
126
- declare const HTML_ROBOTS = "robots.txt is a web page, not a robots.txt file (a login or challenge page)";
127
- /**
128
- * This function tells if the body of a robots.txt is a web page. A server can send a challenge, a
129
- * login or an error page with a 200. Such a body starts with `<!doctype`, `<html`, `<head` or
130
- * `<body`. With the content type `text/html`, a tag of a page in the first 4 KB is enough. A plain
131
- * robots.txt that a server sends as `text/html` stays a file.
132
- */
133
- declare const isHtmlRobots: (contentType: string | null | undefined, text: string) => boolean;
134
- /**
135
- * This function reads the answer of a robots.txt fetch: the status, the content type and the body.
136
- * A 2xx with a web page in place of the file is unreadable. Thus no path is permitted (fail
137
- * closed), as for a body that does not decode. The other statuses follow `robotsFromStatus`.
138
- */
139
- declare const robotsFromResponse: (status: number, contentType: string | null | undefined, body: Uint8Array | string) => Robots;
140
- /** This function gives the meaning of the status of a robots.txt fetch (RFC 9309 §2.3.1). */
141
- declare const robotsFromStatus: (status: number, body: string | undefined) => Robots;
142
- //#endregion
143
- export { robotsFromResponse as _, Robots as a, unreachableRobots as b, RobotsRule as c, isHtmlRobots as d, isProductToken as f, productToken as g, parseRobots as h, ROBOTS_MAX_BYTES as i, RobotsSignal as l, normalizePath as m, DISALLOW_ALL as n, RobotsDecision as o, matchesPattern as p, HTML_ROBOTS as r, RobotsGroup as s, ALLOW_ALL as t, decide as u, robotsFromStatus as v, signalsFor as y };
144
- //# sourceMappingURL=robots-R-qaEBOb.d.mts.map