polite-crawler 0.7.12 → 0.7.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/api.mjs +1 -1
- package/build/{browser-C_dFMvO3.mjs → browser-d8YE87vN.mjs} +3 -3
- package/build/{browser-C_dFMvO3.mjs.map → browser-d8YE87vN.mjs.map} +1 -1
- package/build/{cli-daemon-CfJaQ_vG.mjs → cli-daemon-BO4KheGn.mjs} +2 -2
- package/build/{cli-daemon-CfJaQ_vG.mjs.map → cli-daemon-BO4KheGn.mjs.map} +1 -1
- package/build/{cli-recipes-Bp_LMzOR.mjs → cli-recipes-BzNwc3gz.mjs} +2 -2
- package/build/{cli-recipes-Bp_LMzOR.mjs.map → cli-recipes-BzNwc3gz.mjs.map} +1 -1
- package/build/cli.mjs +15 -15
- package/build/{daemon-B7SbxQS2.mjs → daemon-Cf5kfakp.mjs} +8 -8
- package/build/{daemon-B7SbxQS2.mjs.map → daemon-Cf5kfakp.mjs.map} +1 -1
- package/build/{diff-D0fCuqm-.mjs → diff-CfHnyfQt.mjs} +2 -2
- package/build/{diff-D0fCuqm-.mjs.map → diff-CfHnyfQt.mjs.map} +1 -1
- package/build/{extract-BQLuBe6Z.mjs → extract-CConywz8.mjs} +2 -2
- package/build/{extract-BQLuBe6Z.mjs.map → extract-CConywz8.mjs.map} +1 -1
- package/build/html.mjs +2 -2
- package/build/index.d.mts +5 -5
- package/build/index.mjs +16 -16
- package/build/{log-CB5bADmd.mjs → log-BCu-Evbd.mjs} +1 -1
- package/build/{log-CB5bADmd.mjs.map → log-BCu-Evbd.mjs.map} +1 -1
- package/build/{mcp-package-D9ACY0Ux.mjs → mcp-package-BfWefRka.mjs} +1 -1
- package/build/{mcp-package-D9ACY0Ux.mjs.map → mcp-package-BfWefRka.mjs.map} +1 -1
- package/build/{ollama-B7eNmGhl.mjs → ollama-HOhb74X_.mjs} +1 -1
- package/build/{ollama-B7eNmGhl.mjs.map → ollama-HOhb74X_.mjs.map} +1 -1
- package/build/{open-D5ET6_f4.mjs → open-BlCyswse.mjs} +15 -15
- package/build/{open-D5ET6_f4.mjs.map → open-BlCyswse.mjs.map} +1 -1
- package/build/pinned.mjs +1 -1
- package/build/{reaction-Cqeo5kNs.mjs → reaction-CGDzx3sJ.mjs} +1 -1
- package/build/{reaction-Cqeo5kNs.mjs.map → reaction-CGDzx3sJ.mjs.map} +1 -1
- package/build/{recipe-DglmR882.mjs → recipe-lfg0UUxc.mjs} +3 -3
- package/build/{recipe-DglmR882.mjs.map → recipe-lfg0UUxc.mjs.map} +1 -1
- package/build/{recipe-product-H05CkiNF.mjs → recipe-product-pLJuBblR.mjs} +4 -4
- package/build/{recipe-product-H05CkiNF.mjs.map → recipe-product-pLJuBblR.mjs.map} +1 -1
- package/build/{render-B0NrFDkJ.mjs → render-DF3qFn1u.mjs} +1 -1
- package/build/{render-B0NrFDkJ.mjs.map → render-DF3qFn1u.mjs.map} +1 -1
- package/build/{runtime-B6bhQ0zb.mjs → runtime-DjchTCwx.mjs} +13 -13
- package/build/{runtime-B6bhQ0zb.mjs.map → runtime-DjchTCwx.mjs.map} +1 -1
- package/build/{transformers-Cb-KaPge.mjs → transformers-aT7aCYft.mjs} +1 -1
- package/build/{transformers-Cb-KaPge.mjs.map → transformers-aT7aCYft.mjs.map} +1 -1
- package/build/{version-C_fke4iU.mjs → version-Hnv1EkvR.mjs} +2 -2
- package/build/{version-C_fke4iU.mjs.map → version-Hnv1EkvR.mjs.map} +1 -1
- package/build/{wacz-D2Klun_5.mjs → wacz-B2ujEnnX.mjs} +4 -4
- package/build/{wacz-D2Klun_5.mjs.map → wacz-B2ujEnnX.mjs.map} +1 -1
- package/build/{warc-Co_sZew8.mjs → warc-NkPLiB_Y.mjs} +11 -11
- package/build/{warc-Co_sZew8.mjs.map → warc-NkPLiB_Y.mjs.map} +1 -1
- package/build/{webbotauth-DbiWmibz.mjs → webbotauth-kOH80wBp.mjs} +1 -1
- package/build/{webbotauth-DbiWmibz.mjs.map → webbotauth-kOH80wBp.mjs.map} +1 -1
- package/package.json +1 -1
package/build/pinned.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { join } from "node:path";
|
|
3
2
|
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
4
|
import { fetchTransport, isApiError, send, statusLine } from "polite-crawler/api";
|
|
5
5
|
import { HostScheduler, realClock } from "polite-crawler/politeness";
|
|
6
6
|
// src/pinned.ts
|
|
@@ -163,4 +163,4 @@ const reactToOutcome = (facts, counts, context) => {
|
|
|
163
163
|
};
|
|
164
164
|
export { freshUntil as i, reactToOutcome as n, PERMANENT_REDIRECTS as r, outcomeResult as t };
|
|
165
165
|
|
|
166
|
-
//# sourceMappingURL=reaction-
|
|
166
|
+
//# sourceMappingURL=reaction-CGDzx3sJ.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reaction-Cqeo5kNs.mjs","names":[],"sources":["../src/freshness.ts","../src/reaction.ts"],"sourcesContent":["/**\n * The freshness of an HTTP response, as RFC 9111 §4.2 calculates it. The crawler uses it for the\n * end of the freshness of each capture. `polite-crawler/api` gives `freshUntil` to API clients. The\n * module imports no Node.js module.\n */\nimport { parseHttpDate } from \"./classify.ts\";\n\n/** These redirect statuses are permanent. A cache can keep them with no explicit freshness. */\nexport const PERMANENT_REDIRECTS: ReadonlySet<number> = new Set([301, 308]);\n\n/** A response with one of these statuses can get a heuristic freshness (RFC 9110 §15.1). */\nconst HEURISTIC_STATUSES: ReadonlySet<number> = new Set([\n 200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501,\n]);\n\n/** This value is the maximum freshness lifetime of a response: ten years. */\nconst TEN_YEARS_MS = 10 * 365 * 24 * 60 * 60 * 1000;\n\nconst DIGITS = /^\\d+$/;\n\n/** This function gives the directives of a `Cache-Control` header. Each name is in lowercase. */\nconst directives = (value: string | undefined): Map<string, string> => {\n const out = new Map<string, string>();\n for (const part of (value ?? \"\").split(\",\")) {\n const equals = part.indexOf(\"=\");\n const name = (equals === -1 ? part : part.slice(0, equals)).trim().toLowerCase();\n if (name.length === 0) continue;\n const raw = equals === -1 ? \"\" : part.slice(equals + 1).trim();\n out.set(\n name,\n raw.length >= 2 && raw.startsWith('\"') && raw.endsWith('\"') ? raw.slice(1, -1) : raw,\n );\n }\n return out;\n};\n\n/**\n * This function gives the end of the freshness of an HTTP response, as RFC 9111 §4.2 calculates\n * it. A client that keeps the answers of `poll` and `send` asks again at that time.\n *\n * The sources in sequence are `max-age` of `Cache-Control`, `Expires` minus `Date`, and 10\n * percent of the age of `Last-Modified`. Only a heuristic status (RFC 9110 §15.1) uses the last\n * source, and `capMs` is its maximum. A permanent redirect with no `Last-Modified` before `Date`\n * gets `capMs`. A header that is not a date counts as no header. The function subtracts the `Age`\n * of the response. Ten years is the maximum lifetime.\n *\n * A response with `no-store` or `no-cache` is stale at once, so the function gives `fetchedAt`.\n * An `Expires` that is not a date also gives `fetchedAt`.\n *\n * For a `Reply`, a call is\n * `freshUntil((name) => reply.headers[name], reply.status, fetchedAt, reply.headers[\"last-modified\"], capMs)`.\n *\n * @param header - This function gives the value of one header. Each name is in lowercase.\n * @param status - This value is the status of the response.\n * @param fetchedAt - This value is the time of the response, in milliseconds since the epoch.\n * @param lastModified - This value is the `Last-Modified` header of the response, or undefined.\n * @param capMs - This value is the maximum of the heuristic freshness, in milliseconds.\n * @returns the end of the freshness in milliseconds since the epoch, or undefined when the\n * response has no freshness.\n * @beta\n */\nexport function freshUntil(\n header: (name: string) => string | undefined,\n status: number,\n fetchedAt: number,\n lastModified: string | undefined,\n capMs: number,\n): number | undefined {\n const control = directives(header(\"cache-control\"));\n if (control.has(\"no-store\") || control.has(\"no-cache\")) return fetchedAt;\n const ageText = header(\"age\")?.trim() ?? \"\";\n const age = DIGITS.test(ageText) ? Number(ageText) * 1000 : 0;\n const date = parseHttpDate(header(\"date\")) ?? fetchedAt;\n let lifetime: number | undefined;\n const maxAge = control.get(\"max-age\");\n const expires = header(\"expires\");\n if (maxAge !== undefined && DIGITS.test(maxAge)) lifetime = Number(maxAge) * 1000;\n else if (expires !== undefined) {\n // an Expires that is not a date means that the response is already stale\n const at = parseHttpDate(expires);\n lifetime = at === undefined ? 0 : at - date;\n } else if (HEURISTIC_STATUSES.has(status)) {\n const modified = parseHttpDate(lastModified);\n if (modified !== undefined && modified < date)\n lifetime = Math.min(capMs, (date - modified) / 10);\n else if (PERMANENT_REDIRECTS.has(status)) lifetime = capMs;\n }\n if (lifetime === undefined) return undefined;\n return fetchedAt + Math.max(0, Math.min(lifetime, TEN_YEARS_MS) - age);\n}\n","/**\n * The reaction of a host to one answer: the result that its counts get, its rest and its stops.\n * The `Crawler` and the send of `polite-crawler/api` use the same pure functions. This module is not\n * in `polite-crawler/politeness`, thus the size of that entry for a browser does not change.\n */\nimport type { Outcome } from \"./classify.ts\";\nimport { isClientError } from \"./classify.ts\";\nimport type { HostCounts, HostResult, HostStop, PolitenessPolicy } from \"./politeness.ts\";\nimport { MAX_STOP_MS, equalJitterMs } from \"./politeness.ts\";\n\n/**\n * These are the facts of one answer that set the reaction of its host. `classify` gives the\n * outcome and the wait that the server asked for.\n *\n * @beta\n */\nexport interface OutcomeFacts {\n /** This value is the outcome of the answer. */\n readonly outcome: Outcome;\n /** This value is the status of the answer. The value 0 is a request with no answer. */\n readonly status: number;\n /** This value is the wait that the server asked for (`Retry-After`). */\n readonly retryAfterMs?: number | undefined;\n /**\n * A refusal (`blocked`) with one of these statuses does not stop the host. The crawler spares\n * 401 and 403 for robots.txt (RFC 9309). An API client spares a 401 before it gets a new token.\n */\n readonly spare?: readonly number[] | undefined;\n /** This result replaces the result of the outcome, and the host gets only its rest. A cancelled request is `neutral`. */\n readonly result?: HostResult | undefined;\n}\n\n/**\n * These values set the reaction to one answer.\n *\n * @beta\n */\nexport interface ReactionContext {\n /** This value is the policy of the host. */\n readonly policy: PolitenessPolicy;\n /** This value is the time of the answer, in milliseconds since the epoch. */\n readonly now: number;\n /** This value is the rest of the host that `restAfter` gives for the request. */\n readonly restMs: number;\n /** This function gives a number in [0, 1) for the jitter of a backoff. */\n readonly random: () => number;\n}\n\n/**\n * This value tells how one answer changes its host, after its counts changed.\n *\n * @beta\n */\nexport interface HostReaction {\n /** This value is the rest of the host before its next request, in milliseconds. */\n readonly restMs: number;\n /**\n * This value tells why the rest is longer than the rest of `restAfter`: a backoff, or the\n * `Retry-After` of the server. A longer rest parks the host.\n */\n readonly longer: \"backoff\" | \"retry-after\" | undefined;\n /** This value is the stop of the host, when the answer stops it. */\n readonly stop: HostStop | undefined;\n}\n\n/**\n * This function tells how one answer changes the counts of its host. Give the result to\n * `HostStateStore.record`. These are the rules:\n *\n * - A 429, 502, 503 or 504 is a wait.\n * - A different error and a request with no answer are failures. A client error (`isClientError`) is neutral.\n * - Each other outcome is a success. A refusal is a success too, because its stop is the reaction.\n *\n * @beta\n */\nexport const outcomeResult = (facts: OutcomeFacts): HostResult => {\n if (facts.result !== undefined) return facts.result;\n if (facts.outcome === \"rate-limited\" || facts.outcome === \"unavailable\") return \"wait\";\n if (facts.outcome === \"error\") return isClientError(facts.status) ? \"neutral\" : \"failure\";\n return \"success\";\n};\n\n/**\n * This function gives the reaction of a host to one answer, with the counts that `record` gave for\n * the result of `outcomeResult`. The `Crawler` and the send of `polite-crawler/api` use it. These\n * are the rules, with the times of the policy:\n *\n * - A 429, 502, 503 or 504 parks the host for its `Retry-After`, or for a backoff with jitter. A\n * `Retry-After` longer than `maxRetryAfterMs` stops the host for that time, up to 30 days.\n * - A failure parks the host for a backoff with jitter. After `maxFailures` failures in sequence, the host also stops.\n * - A 401 or a 403 stops the host for `blockedStopMs`. A 402 and a bot challenge stop it longer.\n * - Each other answer gives the rest of `restAfter`.\n *\n * The function is pure, so a simulation and a study can use the reactions of the crawler.\n *\n * @beta\n */\nexport const reactToOutcome = (\n facts: OutcomeFacts,\n counts: HostCounts,\n context: ReactionContext,\n): HostReaction => {\n const { policy, now, restMs, random } = context;\n const rest: HostReaction = { restMs, longer: undefined, stop: undefined };\n // only a rest longer than the pace has a reason\n const longer = (ms: number, reason: \"backoff\" | \"retry-after\"): HostReaction =>\n ms > restMs ? { restMs: ms, longer: reason, stop: undefined } : rest;\n const stop = (ms: number, reason: string): HostReaction => ({\n ...rest,\n stop: { until: now + ms, reason },\n });\n if (facts.result !== undefined) return rest;\n switch (facts.outcome) {\n case \"rate-limited\":\n case \"unavailable\": {\n const asked = facts.retryAfterMs;\n // a wait longer than the budget stops the host, so no request sleeps for days\n if (asked !== undefined && asked > policy.maxRetryAfterMs)\n return stop(\n Math.min(asked, MAX_STOP_MS),\n `asked to wait ${String(Math.round(asked / 1000))} s`,\n );\n return asked === undefined\n ? longer(\n equalJitterMs(counts.waits, policy.backoffBaseMs, policy.backoffCapMs, random),\n \"backoff\",\n )\n : longer(asked, \"retry-after\");\n }\n case \"error\": {\n if (isClientError(facts.status)) return rest;\n const backoff = longer(\n equalJitterMs(counts.failures, policy.backoffBaseMs, policy.backoffCapMs, random),\n \"backoff\",\n );\n if (counts.failures < policy.maxFailures) return backoff;\n return {\n ...backoff,\n stop: {\n until: now + policy.failureStopMs,\n reason: `${String(counts.failures)} failures in a row`,\n },\n };\n }\n case \"challenge\":\n return stop(policy.challengeStopMs, \"challenge\");\n case \"payment-required\":\n return stop(policy.paymentStopMs, \"payment-required\");\n case \"blocked\":\n return facts.spare?.includes(facts.status) === true\n ? rest\n : stop(policy.blockedStopMs, \"blocked\");\n default:\n return rest;\n }\n};\n"],"mappings":";;;;;;;;;AAQA,MAAa,sCAA2C,IAAI,IAAI,CAAC,KAAK,GAAG,CAAC;;AAG1E,MAAM,qCAA0C,IAAI,IAAI;CACtD;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AACzD,CAAC;;AAGD,MAAM,eAAe;AAErB,MAAM,SAA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GACZ,IAAI,cAAc,MAAM,MAAM,GAAG,OAAO;GACxC,MAAM,UAAU,OACd,cAAc,OAAO,UAAU,OAAO,eAAe,OAAO,cAAc,MAAM,GAChF,SACF;GACA,IAAI,OAAO,WAAW,OAAO,aAAa,OAAO;GACjD,OAAO;IACL,GAAG;IACH,MAAM;KACJ,OAAO,MAAM,OAAO;KACpB,QAAQ,GAAG,OAAO,OAAO,QAAQ,EAAE;IACrC;GACF;EACF;EACA,KAAK,aACH,OAAO,KAAK,OAAO,iBAAiB,WAAW;EACjD,KAAK,oBACH,OAAO,KAAK,OAAO,eAAe,kBAAkB;EACtD,KAAK,WACH,OAAO,MAAM,OAAO,SAAS,MAAM,MAAM,MAAM,OAC3C,OACA,KAAK,OAAO,eAAe,SAAS;EAC1C,SACE,OAAO;CACX;AACF"}
|
|
1
|
+
{"version":3,"file":"reaction-CGDzx3sJ.mjs","names":[],"sources":["../src/freshness.ts","../src/reaction.ts"],"sourcesContent":["/**\n * The freshness of an HTTP response, as RFC 9111 §4.2 calculates it. The crawler uses it for the\n * end of the freshness of each capture. `polite-crawler/api` gives `freshUntil` to API clients. The\n * module imports no Node.js module.\n */\nimport { parseHttpDate } from \"./classify.ts\";\n\n/** These redirect statuses are permanent. A cache can keep them with no explicit freshness. */\nexport const PERMANENT_REDIRECTS: ReadonlySet<number> = new Set([301, 308]);\n\n/** A response with one of these statuses can get a heuristic freshness (RFC 9110 §15.1). */\nconst HEURISTIC_STATUSES: ReadonlySet<number> = new Set([\n 200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501,\n]);\n\n/** This value is the maximum freshness lifetime of a response: ten years. */\nconst TEN_YEARS_MS = 10 * 365 * 24 * 60 * 60 * 1000;\n\nconst DIGITS = /^\\d+$/;\n\n/** This function gives the directives of a `Cache-Control` header. Each name is in lowercase. */\nconst directives = (value: string | undefined): Map<string, string> => {\n const out = new Map<string, string>();\n for (const part of (value ?? \"\").split(\",\")) {\n const equals = part.indexOf(\"=\");\n const name = (equals === -1 ? part : part.slice(0, equals)).trim().toLowerCase();\n if (name.length === 0) continue;\n const raw = equals === -1 ? \"\" : part.slice(equals + 1).trim();\n out.set(\n name,\n raw.length >= 2 && raw.startsWith('\"') && raw.endsWith('\"') ? raw.slice(1, -1) : raw,\n );\n }\n return out;\n};\n\n/**\n * This function gives the end of the freshness of an HTTP response, as RFC 9111 §4.2 calculates\n * it. A client that keeps the answers of `poll` and `send` asks again at that time.\n *\n * The sources in sequence are `max-age` of `Cache-Control`, `Expires` minus `Date`, and 10\n * percent of the age of `Last-Modified`. Only a heuristic status (RFC 9110 §15.1) uses the last\n * source, and `capMs` is its maximum. A permanent redirect with no `Last-Modified` before `Date`\n * gets `capMs`. A header that is not a date counts as no header. The function subtracts the `Age`\n * of the response. Ten years is the maximum lifetime.\n *\n * A response with `no-store` or `no-cache` is stale at once, so the function gives `fetchedAt`.\n * An `Expires` that is not a date also gives `fetchedAt`.\n *\n * For a `Reply`, a call is\n * `freshUntil((name) => reply.headers[name], reply.status, fetchedAt, reply.headers[\"last-modified\"], capMs)`.\n *\n * @param header - This function gives the value of one header. Each name is in lowercase.\n * @param status - This value is the status of the response.\n * @param fetchedAt - This value is the time of the response, in milliseconds since the epoch.\n * @param lastModified - This value is the `Last-Modified` header of the response, or undefined.\n * @param capMs - This value is the maximum of the heuristic freshness, in milliseconds.\n * @returns the end of the freshness in milliseconds since the epoch, or undefined when the\n * response has no freshness.\n * @beta\n */\nexport function freshUntil(\n header: (name: string) => string | undefined,\n status: number,\n fetchedAt: number,\n lastModified: string | undefined,\n capMs: number,\n): number | undefined {\n const control = directives(header(\"cache-control\"));\n if (control.has(\"no-store\") || control.has(\"no-cache\")) return fetchedAt;\n const ageText = header(\"age\")?.trim() ?? \"\";\n const age = DIGITS.test(ageText) ? Number(ageText) * 1000 : 0;\n const date = parseHttpDate(header(\"date\")) ?? fetchedAt;\n let lifetime: number | undefined;\n const maxAge = control.get(\"max-age\");\n const expires = header(\"expires\");\n if (maxAge !== undefined && DIGITS.test(maxAge)) lifetime = Number(maxAge) * 1000;\n else if (expires !== undefined) {\n // an Expires that is not a date means that the response is already stale\n const at = parseHttpDate(expires);\n lifetime = at === undefined ? 0 : at - date;\n } else if (HEURISTIC_STATUSES.has(status)) {\n const modified = parseHttpDate(lastModified);\n if (modified !== undefined && modified < date)\n lifetime = Math.min(capMs, (date - modified) / 10);\n else if (PERMANENT_REDIRECTS.has(status)) lifetime = capMs;\n }\n if (lifetime === undefined) return undefined;\n return fetchedAt + Math.max(0, Math.min(lifetime, TEN_YEARS_MS) - age);\n}\n","/**\n * The reaction of a host to one answer: the result that its counts get, its rest and its stops.\n * The `Crawler` and the send of `polite-crawler/api` use the same pure functions. This module is not\n * in `polite-crawler/politeness`, thus the size of that entry for a browser does not change.\n */\nimport type { Outcome } from \"./classify.ts\";\nimport { isClientError } from \"./classify.ts\";\nimport type { HostCounts, HostResult, HostStop, PolitenessPolicy } from \"./politeness.ts\";\nimport { MAX_STOP_MS, equalJitterMs } from \"./politeness.ts\";\n\n/**\n * These are the facts of one answer that set the reaction of its host. `classify` gives the\n * outcome and the wait that the server asked for.\n *\n * @beta\n */\nexport interface OutcomeFacts {\n /** This value is the outcome of the answer. */\n readonly outcome: Outcome;\n /** This value is the status of the answer. The value 0 is a request with no answer. */\n readonly status: number;\n /** This value is the wait that the server asked for (`Retry-After`). */\n readonly retryAfterMs?: number | undefined;\n /**\n * A refusal (`blocked`) with one of these statuses does not stop the host. The crawler spares\n * 401 and 403 for robots.txt (RFC 9309). An API client spares a 401 before it gets a new token.\n */\n readonly spare?: readonly number[] | undefined;\n /** This result replaces the result of the outcome, and the host gets only its rest. A cancelled request is `neutral`. */\n readonly result?: HostResult | undefined;\n}\n\n/**\n * These values set the reaction to one answer.\n *\n * @beta\n */\nexport interface ReactionContext {\n /** This value is the policy of the host. */\n readonly policy: PolitenessPolicy;\n /** This value is the time of the answer, in milliseconds since the epoch. */\n readonly now: number;\n /** This value is the rest of the host that `restAfter` gives for the request. */\n readonly restMs: number;\n /** This function gives a number in [0, 1) for the jitter of a backoff. */\n readonly random: () => number;\n}\n\n/**\n * This value tells how one answer changes its host, after its counts changed.\n *\n * @beta\n */\nexport interface HostReaction {\n /** This value is the rest of the host before its next request, in milliseconds. */\n readonly restMs: number;\n /**\n * This value tells why the rest is longer than the rest of `restAfter`: a backoff, or the\n * `Retry-After` of the server. A longer rest parks the host.\n */\n readonly longer: \"backoff\" | \"retry-after\" | undefined;\n /** This value is the stop of the host, when the answer stops it. */\n readonly stop: HostStop | undefined;\n}\n\n/**\n * This function tells how one answer changes the counts of its host. Give the result to\n * `HostStateStore.record`. These are the rules:\n *\n * - A 429, 502, 503 or 504 is a wait.\n * - A different error and a request with no answer are failures. A client error (`isClientError`) is neutral.\n * - Each other outcome is a success. A refusal is a success too, because its stop is the reaction.\n *\n * @beta\n */\nexport const outcomeResult = (facts: OutcomeFacts): HostResult => {\n if (facts.result !== undefined) return facts.result;\n if (facts.outcome === \"rate-limited\" || facts.outcome === \"unavailable\") return \"wait\";\n if (facts.outcome === \"error\") return isClientError(facts.status) ? \"neutral\" : \"failure\";\n return \"success\";\n};\n\n/**\n * This function gives the reaction of a host to one answer, with the counts that `record` gave for\n * the result of `outcomeResult`. The `Crawler` and the send of `polite-crawler/api` use it. These\n * are the rules, with the times of the policy:\n *\n * - A 429, 502, 503 or 504 parks the host for its `Retry-After`, or for a backoff with jitter. A\n * `Retry-After` longer than `maxRetryAfterMs` stops the host for that time, up to 30 days.\n * - A failure parks the host for a backoff with jitter. After `maxFailures` failures in sequence, the host also stops.\n * - A 401 or a 403 stops the host for `blockedStopMs`. A 402 and a bot challenge stop it longer.\n * - Each other answer gives the rest of `restAfter`.\n *\n * The function is pure, so a simulation and a study can use the reactions of the crawler.\n *\n * @beta\n */\nexport const reactToOutcome = (\n facts: OutcomeFacts,\n counts: HostCounts,\n context: ReactionContext,\n): HostReaction => {\n const { policy, now, restMs, random } = context;\n const rest: HostReaction = { restMs, longer: undefined, stop: undefined };\n // only a rest longer than the pace has a reason\n const longer = (ms: number, reason: \"backoff\" | \"retry-after\"): HostReaction =>\n ms > restMs ? { restMs: ms, longer: reason, stop: undefined } : rest;\n const stop = (ms: number, reason: string): HostReaction => ({\n ...rest,\n stop: { until: now + ms, reason },\n });\n if (facts.result !== undefined) return rest;\n switch (facts.outcome) {\n case \"rate-limited\":\n case \"unavailable\": {\n const asked = facts.retryAfterMs;\n // a wait longer than the budget stops the host, so no request sleeps for days\n if (asked !== undefined && asked > policy.maxRetryAfterMs)\n return stop(\n Math.min(asked, MAX_STOP_MS),\n `asked to wait ${String(Math.round(asked / 1000))} s`,\n );\n return asked === undefined\n ? longer(\n equalJitterMs(counts.waits, policy.backoffBaseMs, policy.backoffCapMs, random),\n \"backoff\",\n )\n : longer(asked, \"retry-after\");\n }\n case \"error\": {\n if (isClientError(facts.status)) return rest;\n const backoff = longer(\n equalJitterMs(counts.failures, policy.backoffBaseMs, policy.backoffCapMs, random),\n \"backoff\",\n );\n if (counts.failures < policy.maxFailures) return backoff;\n return {\n ...backoff,\n stop: {\n until: now + policy.failureStopMs,\n reason: `${String(counts.failures)} failures in a row`,\n },\n };\n }\n case \"challenge\":\n return stop(policy.challengeStopMs, \"challenge\");\n case \"payment-required\":\n return stop(policy.paymentStopMs, \"payment-required\");\n case \"blocked\":\n return facts.spare?.includes(facts.status) === true\n ? rest\n : stop(policy.blockedStopMs, \"blocked\");\n default:\n return rest;\n }\n};\n"],"mappings":";;;;;;;;;AAQA,MAAa,sCAA2C,IAAI,IAAI,CAAC,KAAK,GAAG,CAAC;;AAG1E,MAAM,qCAA0C,IAAI,IAAI;CACtD;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AACzD,CAAC;;AAGD,MAAM,eAAe;AAErB,MAAM,SAAS;;AAGf,MAAM,cAAc,UAAmD;CACrE,MAAM,sBAAM,IAAI,IAAoB;CACpC,KAAK,MAAM,SAAS,SAAS,GAAA,CAAI,MAA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MAAM,GAChF,SACF;GACA,IAAI,OAAO,WAAW,OAAO,aAAa,OAAO;GACjD,OAAO;IACL,GAAG;IACH,MAAM;KACJ,OAAO,MAAM,OAAO;KACpB,QAAQ,GAAG,OAAO,OAAO,QAAQ,EAAE;IACrC;GACF;EACF;EACA,KAAK,aACH,OAAO,KAAK,OAAO,iBAAiB,WAAW;EACjD,KAAK,oBACH,OAAO,KAAK,OAAO,eAAe,kBAAkB;EACtD,KAAK,WACH,OAAO,MAAM,OAAO,SAAS,MAAM,MAAM,MAAM,OAC3C,OACA,KAAK,OAAO,eAAe,SAAS;EAC1C,SACE,OAAO;CACX;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as durationMs } from "./version-
|
|
2
|
-
import { _ as archivedCopy, a as fetchAndIndex, b as archivedOf, c as heldBackMeta, f as recordOverride, l as isHeldBack, n as HELD_BACK, x as fallbackReason } from "./runtime-
|
|
1
|
+
import { i as durationMs } from "./version-Hnv1EkvR.mjs";
|
|
2
|
+
import { _ as archivedCopy, a as fetchAndIndex, b as archivedOf, c as heldBackMeta, f as recordOverride, l as isHeldBack, n as HELD_BACK, x as fallbackReason } from "./runtime-DjchTCwx.mjs";
|
|
3
3
|
import { decodeBody } from "./text.mjs";
|
|
4
4
|
// src/recipe.ts
|
|
5
5
|
/**
|
|
@@ -281,4 +281,4 @@ const runRecipe = async (target, recipe, input, options = {}) => {
|
|
|
281
281
|
};
|
|
282
282
|
export { readJson as a, runtimeOf as c, defineRecipe as i, RESERVED_RECIPES as n, registerRecipe as o, deadlineOf as r, runRecipe as s, RECIPE_LIMITS as t };
|
|
283
283
|
|
|
284
|
-
//# sourceMappingURL=recipe-
|
|
284
|
+
//# sourceMappingURL=recipe-lfg0UUxc.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recipe-DglmR882.mjs","names":[],"sources":["../src/recipe.ts"],"sourcesContent":["/*\n * The recipe SDK. A recipe tells the crawler what to fetch and how to read it into documents.\n *\n * - `defineRecipe` declares a recipe: its name, the request for an input, and its extractor.\n * - `runRecipe` fetches one input through the crawler and gives the documents of the capture.\n * - `registerRecipe` gives the extractor to the extractors of a runtime. `openCrawler` and\n * `createRuntime` take `recipes` and register them at open, so `reindex` reads their captures.\n *\n * Each fetch of a recipe is a fetch of the crawler. Thus robots.txt (for a page), the pace of each\n * host, the stops, the egress policy and the capture log apply. The documents are versions in the\n * index, so `find`, `read`, `verify`, `extract`, `diff` and the time travel use them.\n */\nimport type { FetchRequest } from \"./crawler.ts\";\nimport type { Doc, ExtractedDoc, VersionText } from \"./docs.ts\";\nimport type { Duration } from \"./duration.ts\";\nimport { durationMs } from \"./duration.ts\";\nimport type { Archived } from \"./fallback.ts\";\nimport { archivedCopy, archivedOf, fallbackReason } from \"./fallback.ts\";\nimport type { CrawlRuntime, Extractor, Extractors, Indexed, PageOptions } from \"./runtime.ts\";\nimport { HELD_BACK, fetchAndIndex, heldBackMeta, isHeldBack, recordOverride } from \"./runtime.ts\";\nimport type { AiInput } from \"./signals.ts\";\nimport { decodeBody } from \"./text.ts\";\n\n/** The capture that an extractor reads: the final URL of the response and its content type. */\nexport interface RecipeCapture {\n readonly url: string;\n readonly contentType: string | null;\n}\n\n/**\n * This function reads one captured body into the documents that it holds. It is pure: the same\n * body and capture always give the same documents. It sends no request. It can answer later.\n */\nexport type RecipeExtract = (\n body: Uint8Array,\n capture: RecipeCapture,\n) => ReadonlyArray<ExtractedDoc> | Promise<ReadonlyArray<ExtractedDoc>>;\n\n/** The request of a recipe for one input: the URL, and the parts that change with the input. */\nexport interface RecipeRequest {\n readonly url: string;\n /** This value replaces the `accept` of the recipe for this request. */\n readonly accept?: string | undefined;\n /** These headers go with the request. The crawler does not write them into the capture. */\n readonly headers?: Readonly<Record<string, string>> | undefined;\n /** These query parameters hold secrets. The capture shows `REDACTED` in their place. */\n readonly secretParams?: readonly string[] | undefined;\n}\n\n/** These values declare a recipe. Only `name` and `extract` are necessary. */\nexport interface RecipeDefinition<Input = string> {\n /**\n * This name goes into each capture record of the recipe, so it is part of each capture id.\n * `reindex` finds the extractor by it. Do not change it after the first capture.\n */\n readonly name: string;\n /** This text tells what the recipe reads. */\n readonly description?: string | undefined;\n /**\n * The value `page` (the default) is a web page: robots.txt applies. The value `api` is a\n * documented API with terms of its own: robots.txt does not apply, and the pace still does.\n */\n readonly mode?: \"api\" | \"page\" | undefined;\n /** This value is the `Accept` header. The defaults are HTML for a page and JSON for an API. */\n readonly accept?: string | undefined;\n /** This value is the documented pace of the source: the shortest time between two of its requests. */\n readonly minInterval?: Duration | undefined;\n /** A stored copy younger than this age answers with no request. The default is `24h`. */\n readonly maxAge?: Duration | undefined;\n /** This value is the most bytes of a body that the crawler keeps. */\n readonly maxBytes?: number | undefined;\n /** This function gives the request for an input. With no function, the input is the URL. */\n readonly fetch?: ((input: Input) => string | RecipeRequest) | undefined;\n /** This function reads a captured body into documents. It is pure, and it sends no request. */\n readonly extract: RecipeExtract;\n}\n\n/** These options build the request of a recipe for one input (`Recipe.request`). */\nexport interface RecipeRequestOptions {\n /** This value replaces the `maxAge` of the recipe. The value `0` asks the source again. */\n readonly maxAge?: Duration | undefined;\n /** These agents get their robots.txt rules applied too, for a recipe of the mode `page`. */\n readonly onBehalfOf?: readonly string[] | undefined;\n /** This name connects the captures to the job that asked for them. */\n readonly job?: string | undefined;\n /** When this signal aborts, the crawler stops the fetch. */\n readonly signal?: AbortSignal | undefined;\n /** This label goes into each capture record of the request. */\n readonly scope?: string | undefined;\n /** These labels go into each capture record of the request. They are not part of the capture id. */\n readonly labels?: Readonly<Record<string, string>> | undefined;\n /** No request starts at or after this time (milliseconds since the epoch, on the clock of the crawler). */\n readonly deadline?: number | undefined;\n}\n\n/** A recipe that `defineRecipe` made. Give it to `runRecipe`, to `registerRecipe` or to `openCrawler`. */\nexport interface Recipe<Input = string> {\n readonly name: string;\n readonly description: string | null;\n readonly mode: \"api\" | \"page\";\n readonly accept: string;\n /**\n * This extractor reads the captures of the recipe. It checks the documents of the definition.\n *\n * @beta\n */\n readonly extractor: Extractor;\n /**\n * This method gives the request of the crawler for one input. A value that is not a URL throws a `TypeError`.\n *\n * @beta\n */\n request(input: Input, options?: RecipeRequestOptions): FetchRequest;\n}\n\n/**\n * This function reads a body as JSON, in the encoding that the response declares. A body that is\n * not JSON (a page about maintenance, or a login page) throws an error that names the URL and the\n * content type. In an extractor, the runtime then reports that error.\n */\nexport const readJson = (body: Uint8Array, capture: RecipeCapture): unknown => {\n const text = decodeBody(body, capture.contentType);\n try {\n return JSON.parse(text) as unknown;\n } catch {\n throw new Error(\n `${capture.url} answered ${capture.contentType ?? \"a body of no stated type\"}, not JSON`,\n );\n }\n};\n\n/**\n * These recipe names belong to the crawler: the page reader, the archived fallback, robots.txt,\n * and the sitemaps and feeds of `discover`. A recipe cannot use them.\n */\nexport const RESERVED_RECIPES: readonly string[] = [\n \"page\",\n \"archive-copy\",\n \"archive-index\",\n \"robots.txt\",\n \"sitemap\",\n \"feed\",\n];\n\n/**\n * These are the limits of a recipe.\n *\n * @beta\n */\nexport const RECIPE_LIMITS: {\n /** The longest recipe name. */\n readonly nameChars: number;\n /** The longest document id. */\n readonly docIdChars: number;\n} = { nameChars: 64, docIdChars: 2048 };\n\nconst NAME = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;\nconst PAGE_ACCEPT = \"text/html,application/xhtml+xml;q=0.9,*/*;q=0.5\";\nconst API_ACCEPT = \"application/json\";\nconst AI_INPUTS: readonly AiInput[] = [\"yes\", \"no\", \"unset\"];\n\n/** This function tells what is wrong with one document of an extractor, or gives undefined. */\nconst faultOf = (value: unknown, seen: Set<string>): string | undefined => {\n if (value === null || typeof value !== \"object\" || Array.isArray(value))\n return \"a document must be an object\";\n const doc = value as Record<string, unknown>;\n const id = doc[\"doc\"];\n if (typeof id !== \"string\" || id.length === 0 || id.length > RECIPE_LIMITS.docIdChars)\n return `doc must be a text of 1 to ${String(RECIPE_LIMITS.docIdChars)} characters`;\n // the archived fallback labels the copies of third parties, and only it makes these\n if (id.startsWith(\"archive:\"))\n return `the id ${id} starts with archive:, which is for the archived copies`;\n if (seen.has(id)) return `two documents have the id ${id}`;\n seen.add(id);\n const kind = doc[\"kind\"];\n if (typeof kind !== \"string\" || kind.length === 0) return \"kind must be a text that is not empty\";\n if (kind === \"archived\") return \"the kind archived is for the archived copies\";\n if (typeof doc[\"title\"] !== \"string\") return \"title must be a text\";\n if (typeof doc[\"text\"] !== \"string\") return \"text must be a text\";\n if (doc[\"url\"] !== null && typeof doc[\"url\"] !== \"string\") return \"url must be a text or null\";\n const meta = doc[\"meta\"];\n if (meta === null || typeof meta !== \"object\" || Array.isArray(meta))\n return \"meta must be an object\";\n try {\n JSON.stringify(meta);\n } catch {\n return \"meta must be JSON: no cycle and no BigInt\";\n }\n const merge = doc[\"merge\"];\n if (merge !== undefined && merge !== \"latest\" && merge !== \"richest\")\n return 'merge must be \"latest\" or \"richest\"';\n if (doc[\"full_text\"] !== undefined && typeof doc[\"full_text\"] !== \"string\")\n return \"full_text must be a text\";\n const aiInput = doc[\"ai_input\"];\n if (aiInput !== undefined && !AI_INPUTS.includes(aiInput as AiInput))\n return 'ai_input must be \"yes\", \"no\" or \"unset\"';\n return undefined;\n};\n\n/** This function gives the documents of an extractor, or throws an error that names the first fault. */\nconst checkedDocs = (name: string, docs: unknown): ReadonlyArray<ExtractedDoc> => {\n if (!Array.isArray(docs))\n throw new TypeError(`the recipe ${name} gave ${typeof docs}, not an array of documents`);\n const seen = new Set<string>();\n for (const [index, doc] of docs.entries()) {\n const fault = faultOf(doc, seen);\n if (fault !== undefined)\n throw new TypeError(\n `the recipe ${name} gave a document that is not valid (${String(index)}): ${fault}`,\n );\n }\n return docs as ReadonlyArray<ExtractedDoc>;\n};\n\n/**\n * This function declares a recipe. It checks the definition at once: a name that is not valid, a\n * reserved name or a duration that it cannot read throws an error. The extractor of the recipe\n * checks each document of `extract`. A document that is not valid makes the extractor throw, and\n * the runtime reports it as an error of the extractor.\n */\nexport const defineRecipe = <Input = string>(\n definition: RecipeDefinition<Input>,\n): Recipe<Input> => {\n const name = definition.name;\n if (typeof name !== \"string\" || name.length > RECIPE_LIMITS.nameChars || !NAME.test(name)) {\n throw new RangeError(\n `a recipe name is 1 to ${String(RECIPE_LIMITS.nameChars)} letters, digits and . _ : -, and it starts with a letter or a digit, not ${JSON.stringify(name) ?? \"undefined\"}`,\n );\n }\n if (RESERVED_RECIPES.includes(name)) {\n throw new RangeError(\n `the recipe name ${name} belongs to the crawler: give the recipe a name of its own`,\n );\n }\n if (typeof definition.extract !== \"function\") {\n throw new TypeError(\n `the recipe ${name} needs extract, a function that reads a body into documents`,\n );\n }\n const mode = definition.mode ?? \"page\";\n if (mode !== \"page\" && mode !== \"api\") {\n throw new RangeError(\n `the mode of the recipe ${name} must be \"page\" or \"api\", not ${String(mode)}`,\n );\n }\n const accept = definition.accept ?? (mode === \"api\" ? API_ACCEPT : PAGE_ACCEPT);\n const minIntervalMs =\n definition.minInterval === undefined ? undefined : durationMs(definition.minInterval);\n const maxAgeOf = (value: Duration): number | \"auto\" =>\n value === \"auto\" ? \"auto\" : durationMs(value);\n const maxAge = maxAgeOf(definition.maxAge ?? \"24h\");\n const maxBytes = definition.maxBytes;\n if (maxBytes !== undefined && (!Number.isSafeInteger(maxBytes) || maxBytes < 1)) {\n throw new RangeError(`maxBytes of the recipe ${name} must be a whole number of 1 or more`);\n }\n const extract = definition.extract;\n const extractor: Extractor = async (body, capture) =>\n checkedDocs(name, await extract(body, { url: capture.url, contentType: capture.contentType }));\n const fetch = definition.fetch;\n return {\n name,\n description: definition.description ?? null,\n mode,\n accept,\n extractor,\n request(input, options = {}) {\n const asked: unknown = fetch === undefined ? input : fetch(input);\n const target = typeof asked === \"string\" ? { url: asked } : (asked as RecipeRequest | null);\n const url = target?.url;\n let parsed: URL | undefined;\n try {\n parsed = typeof url === \"string\" ? new URL(url) : undefined;\n } catch {\n parsed = undefined;\n }\n if (parsed === undefined || (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\")) {\n throw new TypeError(\n `the recipe ${name} needs an http or https URL for its input, not ${JSON.stringify(url) ?? String(url)}`,\n );\n }\n return {\n url: parsed.href,\n mode,\n recipe: name,\n accept: target?.accept ?? accept,\n headers: target?.headers,\n secretParams: target?.secretParams,\n minIntervalMs,\n maxAgeMs: options.maxAge === undefined ? maxAge : maxAgeOf(options.maxAge),\n maxBytes,\n onBehalfOf: options.onBehalfOf,\n job: options.job,\n signal: options.signal,\n scope: options.scope,\n labels: options.labels,\n deadline: options.deadline,\n };\n },\n };\n};\n\n/**\n * This function gives the extractor of a recipe to the extractors of a runtime. A second\n * registration of the same recipe does nothing. A different extractor with the same name throws\n * an error, because `reindex` must read each capture with one extractor.\n *\n * @beta\n */\nexport const registerRecipe = <Input>(extractors: Extractors, recipe: Recipe<Input>): void => {\n const known = extractors.get(recipe.name);\n if (known === recipe.extractor) return;\n if (known !== undefined) {\n throw new Error(\n `the recipe ${recipe.name} has a different extractor in this runtime: give each recipe a name of its own`,\n );\n }\n extractors.register(recipe.name, recipe.extractor);\n};\n\n/** These options control one run of a recipe. */\nexport interface RecipeRunOptions extends Omit<RecipeRequestOptions, \"deadline\"> {\n /** No request starts after this time from the start of the run, for example `\"30s\"`. */\n readonly budget?: Duration | undefined;\n /** This value is true when a person gave this input directly. It is the only way to see a held-back document. The crawler logs it. */\n readonly userProvided?: boolean | undefined;\n /**\n * The value `archive` gives a copy from a public web archive when the site refuses the page or is\n * down. Only a recipe of the mode `page` takes it. The page reader reads the copy.\n */\n readonly fallback?: \"archive\" | \"none\" | undefined;\n /** The value true also gives an archived copy of a page that the current robots.txt disallows. */\n readonly archiveDespiteRobots?: boolean | undefined;\n}\n\n/** One document of a run of a recipe: the version of the capture of the run. */\nexport interface RecipeDoc {\n readonly doc: string;\n readonly kind: string;\n /** This value is the title. A held-back document shows no title. */\n readonly title: string;\n readonly url: string | null;\n /** This value is the metadata. A held-back document shows only the structural fields. */\n readonly meta: Readonly<Record<string, unknown>>;\n /** This value is the length of the text. `read` gives the text. */\n readonly chars: number;\n /** This id is the capture of this version. A citation names it. */\n readonly capture: string;\n readonly fetched_at: string;\n readonly held_back: boolean;\n /** This value tells that the document is an archived copy, with the archive and the date of the copy. */\n readonly archived?: Archived | undefined;\n}\n\n/** The result of `runRecipe`. */\nexport interface RecipeResult {\n readonly recipe: string;\n /** This URL is the URL of the capture. Its secrets show as `REDACTED`. */\n readonly url: string;\n readonly outcome: string;\n readonly status: number;\n /** These documents came from the capture of the run, in the order of the extractor. */\n readonly docs: readonly RecipeDoc[];\n /** This id is the capture of the documents. */\n readonly capture: string | null;\n readonly fetched_at: string | null;\n /** This value is true when the stored copy answered, and the crawler sent no request. */\n readonly from_cache: boolean;\n /** This value is true when a document is held back. */\n readonly held_back: boolean;\n /** This text is the error of the extractor, when it threw for the capture. */\n readonly error: string | null;\n /** This value tells that the documents are an archived copy, not a fresh fetch. */\n readonly archived?: Archived | undefined;\n readonly note: string | null;\n}\n\n/** A recipe operates on a runtime, or on a crawler that has one (`crawler.runtime`). */\nexport type RecipeTarget = CrawlRuntime | { readonly runtime: CrawlRuntime };\n\n/** This function gives the runtime of a target. */\nexport const runtimeOf = (target: RecipeTarget): CrawlRuntime =>\n \"crawler\" in target ? target : target.runtime;\n\n/** This function gives the deadline of a budget, on the clock of the runtime. */\nexport const deadlineOf = (\n runtime: CrawlRuntime,\n budget: Duration | undefined,\n): number | undefined =>\n budget === undefined ? undefined : Date.parse(runtime.now()) + durationMs(budget);\n\n/** This function gives one document of a run, with the rule for a held-back document. */\nconst recipeDoc = async (\n runtime: CrawlRuntime,\n doc: Doc,\n capture: string | undefined,\n userProvided: boolean,\n action: string,\n): Promise<RecipeDoc> => {\n const current = runtime.index.get(doc.doc) ?? doc;\n const version: VersionText | Doc =\n (capture === undefined ? undefined : runtime.index.versionText(doc.doc, capture)) ?? current;\n const aiInput: AiInput =\n current.ai_input === \"no\" || version.ai_input === \"no\" ? \"no\" : version.ai_input;\n const held = isHeldBack({ ai_input: aiInput }, userProvided);\n if (aiInput === \"no\" && userProvided) await recordOverride(runtime, current, action);\n const archived = archivedOf(version.meta);\n return {\n doc: doc.doc,\n kind: version.kind,\n title: held ? \"\" : version.title,\n url: version.url,\n meta: held ? heldBackMeta(version.meta) : version.meta,\n chars: version.text.length,\n capture: version.capture,\n fetched_at: version.fetched_at,\n held_back: held,\n ...(archived === undefined ? {} : { archived }),\n };\n};\n\nconst noteOf = (parts: ReadonlyArray<string | null | undefined>): string | null => {\n const kept = parts.filter((part): part is string => typeof part === \"string\" && part.length > 0);\n return kept.length > 0 ? kept.join(\"; \") : null;\n};\n\n/**\n * This function starts a recipe for one input. It fetches the request through the crawler, and it\n * reads the capture with the extractor of the recipe. It gives the documents of that capture.\n * These are its rules:\n *\n * - The runtime registers the recipe the first time that it gets it. Give the recipe to\n * `openCrawler` too, so that `reindex` reads its captures before the first fetch.\n * - A stored copy younger than `maxAge` answers with no request. When the extractor cannot read a\n * stored copy, the crawler asks the source one more time.\n * - An extractor that throws gives no document, and `error` tells why. The fetch still succeeds.\n * - A document that asked not to be AI input shows no title and only its structural metadata,\n * unless `userProvided`. The crawler logs each `userProvided` read of a held-back document.\n * - With `fallback: \"archive\"`, a page that the site refuses gets an archived copy, with\n * `archived`. The page reader reads the copy. The extractor of the recipe reads only live\n * captures. Thus its documents do not mix a live answer and an old copy of a third party.\n */\nexport const runRecipe = async <Input>(\n target: RecipeTarget,\n recipe: Recipe<Input>,\n input: Input,\n options: RecipeRunOptions = {},\n): Promise<RecipeResult> => {\n const runtime = runtimeOf(target);\n if (options.fallback === \"archive\" && recipe.mode !== \"page\") {\n throw new RangeError(\n `the recipe ${recipe.name} reads an API: an archived copy is for a page, so give fallback only to a recipe of the mode page`,\n );\n }\n registerRecipe(runtime.extractors, recipe);\n const deadline = deadlineOf(runtime, options.budget);\n const request = recipe.request(input, { ...options, deadline });\n let indexed: Indexed = await fetchAndIndex(runtime, request);\n // a stored answer that the extractor cannot read is no answer: it would give the same error until maxAge\n if (indexed.error !== undefined && indexed.blocked === undefined && indexed.fetched.fromCache) {\n indexed = await fetchAndIndex(runtime, { ...request, maxAgeMs: 0 });\n }\n const userProvided = options.userProvided ?? false;\n const action = options.job ?? `recipe ${recipe.name}`;\n const fetched = indexed.fetched;\n const notes: Array<string | undefined> = [fetched.note, indexed.error];\n if (indexed.docs.length === 0 && options.fallback === \"archive\") {\n const reason = fallbackReason(indexed);\n if (reason !== undefined) {\n const page: PageOptions = {\n render: \"never\",\n maxAgeMs: request.maxAgeMs,\n onBehalfOf: request.onBehalfOf,\n job: request.job,\n signal: request.signal,\n scope: request.scope,\n labels: request.labels,\n deadline,\n fallback: \"archive\",\n archiveDespiteRobots: options.archiveDespiteRobots,\n };\n const answer = await archivedCopy(runtime, request.url, reason, page);\n if (answer.doc !== undefined && answer.archived !== undefined) {\n const doc = await recipeDoc(runtime, answer.doc, answer.doc.capture, userProvided, action);\n return {\n recipe: recipe.name,\n url: fetched.url,\n outcome: fetched.outcome,\n status: fetched.status,\n docs: [doc],\n capture: doc.capture,\n fetched_at: doc.fetched_at,\n from_cache: answer.fromCache,\n held_back: doc.held_back,\n error: indexed.error ?? null,\n archived: answer.archived,\n note: noteOf([\n ...notes,\n ...answer.notes,\n \"the page reader read the archived copy, not the extractor of the recipe\",\n doc.held_back ? HELD_BACK : undefined,\n ]),\n };\n }\n notes.push(...answer.notes);\n }\n }\n const capture = fetched.capture;\n const docs: RecipeDoc[] = [];\n for (const doc of indexed.docs)\n docs.push(await recipeDoc(runtime, doc, capture, userProvided, action));\n const held = docs.some((doc) => doc.held_back);\n return {\n recipe: recipe.name,\n url: fetched.url,\n outcome: fetched.outcome,\n status: fetched.status,\n docs,\n capture: docs[0]?.capture ?? capture ?? null,\n fetched_at: docs[0]?.fetched_at ?? fetched.fetchedAt ?? null,\n from_cache: fetched.fromCache,\n held_back: held,\n error: indexed.error ?? null,\n note: noteOf([...notes, held ? HELD_BACK : undefined]),\n };\n};\n"],"mappings":";;;;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AAM,UAAU,SAAS,OAAO,KAAK,OAAO,IAAI,SAAS,cAAc,MAAM;IACzF,OAAO;KACL,QAAQ,OAAO;KACf,KAAK,QAAQ;KACb,SAAS,QAAQ;KACjB,QAAQ,QAAQ;KAChB,MAAM,CAAC,GAAG;KACV,SAAS,IAAI;KACb,YAAY,IAAI;KAChB,YAAY,OAAO;KACnB,WAAW,IAAI;KACf,OAAO,QAAQ,SAAS;KACxB,UAAU,OAAO;KACjB,MAAM,OAAO;MACX,GAAG;MACH,GAAG,OAAO;MACV;MACA,IAAI,YAAY,YAAY,KAAA;KAC9B,CAAC;IACH;GACF;GACA,MAAM,KAAK,GAAG,OAAO,KAAK;EAC5B;CACF;CACA,MAAM,UAAU,QAAQ;CACxB,MAAM,OAAoB,CAAC;CAC3B,KAAK,MAAM,OAAO,QAAQ,MACxB,KAAK,KAAK,MAAM,UAAU,SAAS,KAAK,SAAS,cAAc,MAAM,CAAC;CACxE,MAAM,OAAO,KAAK,MAAM,QAAQ,IAAI,SAAS;CAC7C,OAAO;EACL,QAAQ,OAAO;EACf,KAAK,QAAQ;EACb,SAAS,QAAQ;EACjB,QAAQ,QAAQ;EAChB;EACA,SAAS,KAAK,EAAE,EAAE,WAAW,WAAW;EACxC,YAAY,KAAK,EAAE,EAAE,cAAc,QAAQ,aAAa;EACxD,YAAY,QAAQ;EACpB,WAAW;EACX,OAAO,QAAQ,SAAS;EACxB,MAAM,OAAO,CAAC,GAAG,OAAO,OAAO,YAAY,KAAA,CAAS,CAAC;CACvD;AACF"}
|
|
1
|
+
{"version":3,"file":"recipe-lfg0UUxc.mjs","names":[],"sources":["../src/recipe.ts"],"sourcesContent":["/*\n * The recipe SDK. A recipe tells the crawler what to fetch and how to read it into documents.\n *\n * - `defineRecipe` declares a recipe: its name, the request for an input, and its extractor.\n * - `runRecipe` fetches one input through the crawler and gives the documents of the capture.\n * - `registerRecipe` gives the extractor to the extractors of a runtime. `openCrawler` and\n * `createRuntime` take `recipes` and register them at open, so `reindex` reads their captures.\n *\n * Each fetch of a recipe is a fetch of the crawler. Thus robots.txt (for a page), the pace of each\n * host, the stops, the egress policy and the capture log apply. The documents are versions in the\n * index, so `find`, `read`, `verify`, `extract`, `diff` and the time travel use them.\n */\nimport type { FetchRequest } from \"./crawler.ts\";\nimport type { Doc, ExtractedDoc, VersionText } from \"./docs.ts\";\nimport type { Duration } from \"./duration.ts\";\nimport { durationMs } from \"./duration.ts\";\nimport type { Archived } from \"./fallback.ts\";\nimport { archivedCopy, archivedOf, fallbackReason } from \"./fallback.ts\";\nimport type { CrawlRuntime, Extractor, Extractors, Indexed, PageOptions } from \"./runtime.ts\";\nimport { HELD_BACK, fetchAndIndex, heldBackMeta, isHeldBack, recordOverride } from \"./runtime.ts\";\nimport type { AiInput } from \"./signals.ts\";\nimport { decodeBody } from \"./text.ts\";\n\n/** The capture that an extractor reads: the final URL of the response and its content type. */\nexport interface RecipeCapture {\n readonly url: string;\n readonly contentType: string | null;\n}\n\n/**\n * This function reads one captured body into the documents that it holds. It is pure: the same\n * body and capture always give the same documents. It sends no request. It can answer later.\n */\nexport type RecipeExtract = (\n body: Uint8Array,\n capture: RecipeCapture,\n) => ReadonlyArray<ExtractedDoc> | Promise<ReadonlyArray<ExtractedDoc>>;\n\n/** The request of a recipe for one input: the URL, and the parts that change with the input. */\nexport interface RecipeRequest {\n readonly url: string;\n /** This value replaces the `accept` of the recipe for this request. */\n readonly accept?: string | undefined;\n /** These headers go with the request. The crawler does not write them into the capture. */\n readonly headers?: Readonly<Record<string, string>> | undefined;\n /** These query parameters hold secrets. The capture shows `REDACTED` in their place. */\n readonly secretParams?: readonly string[] | undefined;\n}\n\n/** These values declare a recipe. Only `name` and `extract` are necessary. */\nexport interface RecipeDefinition<Input = string> {\n /**\n * This name goes into each capture record of the recipe, so it is part of each capture id.\n * `reindex` finds the extractor by it. Do not change it after the first capture.\n */\n readonly name: string;\n /** This text tells what the recipe reads. */\n readonly description?: string | undefined;\n /**\n * The value `page` (the default) is a web page: robots.txt applies. The value `api` is a\n * documented API with terms of its own: robots.txt does not apply, and the pace still does.\n */\n readonly mode?: \"api\" | \"page\" | undefined;\n /** This value is the `Accept` header. The defaults are HTML for a page and JSON for an API. */\n readonly accept?: string | undefined;\n /** This value is the documented pace of the source: the shortest time between two of its requests. */\n readonly minInterval?: Duration | undefined;\n /** A stored copy younger than this age answers with no request. The default is `24h`. */\n readonly maxAge?: Duration | undefined;\n /** This value is the most bytes of a body that the crawler keeps. */\n readonly maxBytes?: number | undefined;\n /** This function gives the request for an input. With no function, the input is the URL. */\n readonly fetch?: ((input: Input) => string | RecipeRequest) | undefined;\n /** This function reads a captured body into documents. It is pure, and it sends no request. */\n readonly extract: RecipeExtract;\n}\n\n/** These options build the request of a recipe for one input (`Recipe.request`). */\nexport interface RecipeRequestOptions {\n /** This value replaces the `maxAge` of the recipe. The value `0` asks the source again. */\n readonly maxAge?: Duration | undefined;\n /** These agents get their robots.txt rules applied too, for a recipe of the mode `page`. */\n readonly onBehalfOf?: readonly string[] | undefined;\n /** This name connects the captures to the job that asked for them. */\n readonly job?: string | undefined;\n /** When this signal aborts, the crawler stops the fetch. */\n readonly signal?: AbortSignal | undefined;\n /** This label goes into each capture record of the request. */\n readonly scope?: string | undefined;\n /** These labels go into each capture record of the request. They are not part of the capture id. */\n readonly labels?: Readonly<Record<string, string>> | undefined;\n /** No request starts at or after this time (milliseconds since the epoch, on the clock of the crawler). */\n readonly deadline?: number | undefined;\n}\n\n/** A recipe that `defineRecipe` made. Give it to `runRecipe`, to `registerRecipe` or to `openCrawler`. */\nexport interface Recipe<Input = string> {\n readonly name: string;\n readonly description: string | null;\n readonly mode: \"api\" | \"page\";\n readonly accept: string;\n /**\n * This extractor reads the captures of the recipe. It checks the documents of the definition.\n *\n * @beta\n */\n readonly extractor: Extractor;\n /**\n * This method gives the request of the crawler for one input. A value that is not a URL throws a `TypeError`.\n *\n * @beta\n */\n request(input: Input, options?: RecipeRequestOptions): FetchRequest;\n}\n\n/**\n * This function reads a body as JSON, in the encoding that the response declares. A body that is\n * not JSON (a page about maintenance, or a login page) throws an error that names the URL and the\n * content type. In an extractor, the runtime then reports that error.\n */\nexport const readJson = (body: Uint8Array, capture: RecipeCapture): unknown => {\n const text = decodeBody(body, capture.contentType);\n try {\n return JSON.parse(text) as unknown;\n } catch {\n throw new Error(\n `${capture.url} answered ${capture.contentType ?? \"a body of no stated type\"}, not JSON`,\n );\n }\n};\n\n/**\n * These recipe names belong to the crawler: the page reader, the archived fallback, robots.txt,\n * and the sitemaps and feeds of `discover`. A recipe cannot use them.\n */\nexport const RESERVED_RECIPES: readonly string[] = [\n \"page\",\n \"archive-copy\",\n \"archive-index\",\n \"robots.txt\",\n \"sitemap\",\n \"feed\",\n];\n\n/**\n * These are the limits of a recipe.\n *\n * @beta\n */\nexport const RECIPE_LIMITS: {\n /** The longest recipe name. */\n readonly nameChars: number;\n /** The longest document id. */\n readonly docIdChars: number;\n} = { nameChars: 64, docIdChars: 2048 };\n\nconst NAME = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;\nconst PAGE_ACCEPT = \"text/html,application/xhtml+xml;q=0.9,*/*;q=0.5\";\nconst API_ACCEPT = \"application/json\";\nconst AI_INPUTS: readonly AiInput[] = [\"yes\", \"no\", \"unset\"];\n\n/** This function tells what is wrong with one document of an extractor, or gives undefined. */\nconst faultOf = (value: unknown, seen: Set<string>): string | undefined => {\n if (value === null || typeof value !== \"object\" || Array.isArray(value))\n return \"a document must be an object\";\n const doc = value as Record<string, unknown>;\n const id = doc[\"doc\"];\n if (typeof id !== \"string\" || id.length === 0 || id.length > RECIPE_LIMITS.docIdChars)\n return `doc must be a text of 1 to ${String(RECIPE_LIMITS.docIdChars)} characters`;\n // the archived fallback labels the copies of third parties, and only it makes these\n if (id.startsWith(\"archive:\"))\n return `the id ${id} starts with archive:, which is for the archived copies`;\n if (seen.has(id)) return `two documents have the id ${id}`;\n seen.add(id);\n const kind = doc[\"kind\"];\n if (typeof kind !== \"string\" || kind.length === 0) return \"kind must be a text that is not empty\";\n if (kind === \"archived\") return \"the kind archived is for the archived copies\";\n if (typeof doc[\"title\"] !== \"string\") return \"title must be a text\";\n if (typeof doc[\"text\"] !== \"string\") return \"text must be a text\";\n if (doc[\"url\"] !== null && typeof doc[\"url\"] !== \"string\") return \"url must be a text or null\";\n const meta = doc[\"meta\"];\n if (meta === null || typeof meta !== \"object\" || Array.isArray(meta))\n return \"meta must be an object\";\n try {\n JSON.stringify(meta);\n } catch {\n return \"meta must be JSON: no cycle and no BigInt\";\n }\n const merge = doc[\"merge\"];\n if (merge !== undefined && merge !== \"latest\" && merge !== \"richest\")\n return 'merge must be \"latest\" or \"richest\"';\n if (doc[\"full_text\"] !== undefined && typeof doc[\"full_text\"] !== \"string\")\n return \"full_text must be a text\";\n const aiInput = doc[\"ai_input\"];\n if (aiInput !== undefined && !AI_INPUTS.includes(aiInput as AiInput))\n return 'ai_input must be \"yes\", \"no\" or \"unset\"';\n return undefined;\n};\n\n/** This function gives the documents of an extractor, or throws an error that names the first fault. */\nconst checkedDocs = (name: string, docs: unknown): ReadonlyArray<ExtractedDoc> => {\n if (!Array.isArray(docs))\n throw new TypeError(`the recipe ${name} gave ${typeof docs}, not an array of documents`);\n const seen = new Set<string>();\n for (const [index, doc] of docs.entries()) {\n const fault = faultOf(doc, seen);\n if (fault !== undefined)\n throw new TypeError(\n `the recipe ${name} gave a document that is not valid (${String(index)}): ${fault}`,\n );\n }\n return docs as ReadonlyArray<ExtractedDoc>;\n};\n\n/**\n * This function declares a recipe. It checks the definition at once: a name that is not valid, a\n * reserved name or a duration that it cannot read throws an error. The extractor of the recipe\n * checks each document of `extract`. A document that is not valid makes the extractor throw, and\n * the runtime reports it as an error of the extractor.\n */\nexport const defineRecipe = <Input = string>(\n definition: RecipeDefinition<Input>,\n): Recipe<Input> => {\n const name = definition.name;\n if (typeof name !== \"string\" || name.length > RECIPE_LIMITS.nameChars || !NAME.test(name)) {\n throw new RangeError(\n `a recipe name is 1 to ${String(RECIPE_LIMITS.nameChars)} letters, digits and . _ : -, and it starts with a letter or a digit, not ${JSON.stringify(name) ?? \"undefined\"}`,\n );\n }\n if (RESERVED_RECIPES.includes(name)) {\n throw new RangeError(\n `the recipe name ${name} belongs to the crawler: give the recipe a name of its own`,\n );\n }\n if (typeof definition.extract !== \"function\") {\n throw new TypeError(\n `the recipe ${name} needs extract, a function that reads a body into documents`,\n );\n }\n const mode = definition.mode ?? \"page\";\n if (mode !== \"page\" && mode !== \"api\") {\n throw new RangeError(\n `the mode of the recipe ${name} must be \"page\" or \"api\", not ${String(mode)}`,\n );\n }\n const accept = definition.accept ?? (mode === \"api\" ? API_ACCEPT : PAGE_ACCEPT);\n const minIntervalMs =\n definition.minInterval === undefined ? undefined : durationMs(definition.minInterval);\n const maxAgeOf = (value: Duration): number | \"auto\" =>\n value === \"auto\" ? \"auto\" : durationMs(value);\n const maxAge = maxAgeOf(definition.maxAge ?? \"24h\");\n const maxBytes = definition.maxBytes;\n if (maxBytes !== undefined && (!Number.isSafeInteger(maxBytes) || maxBytes < 1)) {\n throw new RangeError(`maxBytes of the recipe ${name} must be a whole number of 1 or more`);\n }\n const extract = definition.extract;\n const extractor: Extractor = async (body, capture) =>\n checkedDocs(name, await extract(body, { url: capture.url, contentType: capture.contentType }));\n const fetch = definition.fetch;\n return {\n name,\n description: definition.description ?? null,\n mode,\n accept,\n extractor,\n request(input, options = {}) {\n const asked: unknown = fetch === undefined ? input : fetch(input);\n const target = typeof asked === \"string\" ? { url: asked } : (asked as RecipeRequest | null);\n const url = target?.url;\n let parsed: URL | undefined;\n try {\n parsed = typeof url === \"string\" ? new URL(url) : undefined;\n } catch {\n parsed = undefined;\n }\n if (parsed === undefined || (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\")) {\n throw new TypeError(\n `the recipe ${name} needs an http or https URL for its input, not ${JSON.stringify(url) ?? String(url)}`,\n );\n }\n return {\n url: parsed.href,\n mode,\n recipe: name,\n accept: target?.accept ?? accept,\n headers: target?.headers,\n secretParams: target?.secretParams,\n minIntervalMs,\n maxAgeMs: options.maxAge === undefined ? maxAge : maxAgeOf(options.maxAge),\n maxBytes,\n onBehalfOf: options.onBehalfOf,\n job: options.job,\n signal: options.signal,\n scope: options.scope,\n labels: options.labels,\n deadline: options.deadline,\n };\n },\n };\n};\n\n/**\n * This function gives the extractor of a recipe to the extractors of a runtime. A second\n * registration of the same recipe does nothing. A different extractor with the same name throws\n * an error, because `reindex` must read each capture with one extractor.\n *\n * @beta\n */\nexport const registerRecipe = <Input>(extractors: Extractors, recipe: Recipe<Input>): void => {\n const known = extractors.get(recipe.name);\n if (known === recipe.extractor) return;\n if (known !== undefined) {\n throw new Error(\n `the recipe ${recipe.name} has a different extractor in this runtime: give each recipe a name of its own`,\n );\n }\n extractors.register(recipe.name, recipe.extractor);\n};\n\n/** These options control one run of a recipe. */\nexport interface RecipeRunOptions extends Omit<RecipeRequestOptions, \"deadline\"> {\n /** No request starts after this time from the start of the run, for example `\"30s\"`. */\n readonly budget?: Duration | undefined;\n /** This value is true when a person gave this input directly. It is the only way to see a held-back document. The crawler logs it. */\n readonly userProvided?: boolean | undefined;\n /**\n * The value `archive` gives a copy from a public web archive when the site refuses the page or is\n * down. Only a recipe of the mode `page` takes it. The page reader reads the copy.\n */\n readonly fallback?: \"archive\" | \"none\" | undefined;\n /** The value true also gives an archived copy of a page that the current robots.txt disallows. */\n readonly archiveDespiteRobots?: boolean | undefined;\n}\n\n/** One document of a run of a recipe: the version of the capture of the run. */\nexport interface RecipeDoc {\n readonly doc: string;\n readonly kind: string;\n /** This value is the title. A held-back document shows no title. */\n readonly title: string;\n readonly url: string | null;\n /** This value is the metadata. A held-back document shows only the structural fields. */\n readonly meta: Readonly<Record<string, unknown>>;\n /** This value is the length of the text. `read` gives the text. */\n readonly chars: number;\n /** This id is the capture of this version. A citation names it. */\n readonly capture: string;\n readonly fetched_at: string;\n readonly held_back: boolean;\n /** This value tells that the document is an archived copy, with the archive and the date of the copy. */\n readonly archived?: Archived | undefined;\n}\n\n/** The result of `runRecipe`. */\nexport interface RecipeResult {\n readonly recipe: string;\n /** This URL is the URL of the capture. Its secrets show as `REDACTED`. */\n readonly url: string;\n readonly outcome: string;\n readonly status: number;\n /** These documents came from the capture of the run, in the order of the extractor. */\n readonly docs: readonly RecipeDoc[];\n /** This id is the capture of the documents. */\n readonly capture: string | null;\n readonly fetched_at: string | null;\n /** This value is true when the stored copy answered, and the crawler sent no request. */\n readonly from_cache: boolean;\n /** This value is true when a document is held back. */\n readonly held_back: boolean;\n /** This text is the error of the extractor, when it threw for the capture. */\n readonly error: string | null;\n /** This value tells that the documents are an archived copy, not a fresh fetch. */\n readonly archived?: Archived | undefined;\n readonly note: string | null;\n}\n\n/** A recipe operates on a runtime, or on a crawler that has one (`crawler.runtime`). */\nexport type RecipeTarget = CrawlRuntime | { readonly runtime: CrawlRuntime };\n\n/** This function gives the runtime of a target. */\nexport const runtimeOf = (target: RecipeTarget): CrawlRuntime =>\n \"crawler\" in target ? target : target.runtime;\n\n/** This function gives the deadline of a budget, on the clock of the runtime. */\nexport const deadlineOf = (\n runtime: CrawlRuntime,\n budget: Duration | undefined,\n): number | undefined =>\n budget === undefined ? undefined : Date.parse(runtime.now()) + durationMs(budget);\n\n/** This function gives one document of a run, with the rule for a held-back document. */\nconst recipeDoc = async (\n runtime: CrawlRuntime,\n doc: Doc,\n capture: string | undefined,\n userProvided: boolean,\n action: string,\n): Promise<RecipeDoc> => {\n const current = runtime.index.get(doc.doc) ?? doc;\n const version: VersionText | Doc =\n (capture === undefined ? undefined : runtime.index.versionText(doc.doc, capture)) ?? current;\n const aiInput: AiInput =\n current.ai_input === \"no\" || version.ai_input === \"no\" ? \"no\" : version.ai_input;\n const held = isHeldBack({ ai_input: aiInput }, userProvided);\n if (aiInput === \"no\" && userProvided) await recordOverride(runtime, current, action);\n const archived = archivedOf(version.meta);\n return {\n doc: doc.doc,\n kind: version.kind,\n title: held ? \"\" : version.title,\n url: version.url,\n meta: held ? heldBackMeta(version.meta) : version.meta,\n chars: version.text.length,\n capture: version.capture,\n fetched_at: version.fetched_at,\n held_back: held,\n ...(archived === undefined ? {} : { archived }),\n };\n};\n\nconst noteOf = (parts: ReadonlyArray<string | null | undefined>): string | null => {\n const kept = parts.filter((part): part is string => typeof part === \"string\" && part.length > 0);\n return kept.length > 0 ? kept.join(\"; \") : null;\n};\n\n/**\n * This function starts a recipe for one input. It fetches the request through the crawler, and it\n * reads the capture with the extractor of the recipe. It gives the documents of that capture.\n * These are its rules:\n *\n * - The runtime registers the recipe the first time that it gets it. Give the recipe to\n * `openCrawler` too, so that `reindex` reads its captures before the first fetch.\n * - A stored copy younger than `maxAge` answers with no request. When the extractor cannot read a\n * stored copy, the crawler asks the source one more time.\n * - An extractor that throws gives no document, and `error` tells why. The fetch still succeeds.\n * - A document that asked not to be AI input shows no title and only its structural metadata,\n * unless `userProvided`. The crawler logs each `userProvided` read of a held-back document.\n * - With `fallback: \"archive\"`, a page that the site refuses gets an archived copy, with\n * `archived`. The page reader reads the copy. The extractor of the recipe reads only live\n * captures. Thus its documents do not mix a live answer and an old copy of a third party.\n */\nexport const runRecipe = async <Input>(\n target: RecipeTarget,\n recipe: Recipe<Input>,\n input: Input,\n options: RecipeRunOptions = {},\n): Promise<RecipeResult> => {\n const runtime = runtimeOf(target);\n if (options.fallback === \"archive\" && recipe.mode !== \"page\") {\n throw new RangeError(\n `the recipe ${recipe.name} reads an API: an archived copy is for a page, so give fallback only to a recipe of the mode page`,\n );\n }\n registerRecipe(runtime.extractors, recipe);\n const deadline = deadlineOf(runtime, options.budget);\n const request = recipe.request(input, { ...options, deadline });\n let indexed: Indexed = await fetchAndIndex(runtime, request);\n // a stored answer that the extractor cannot read is no answer: it would give the same error until maxAge\n if (indexed.error !== undefined && indexed.blocked === undefined && indexed.fetched.fromCache) {\n indexed = await fetchAndIndex(runtime, { ...request, maxAgeMs: 0 });\n }\n const userProvided = options.userProvided ?? false;\n const action = options.job ?? `recipe ${recipe.name}`;\n const fetched = indexed.fetched;\n const notes: Array<string | undefined> = [fetched.note, indexed.error];\n if (indexed.docs.length === 0 && options.fallback === \"archive\") {\n const reason = fallbackReason(indexed);\n if (reason !== undefined) {\n const page: PageOptions = {\n render: \"never\",\n maxAgeMs: request.maxAgeMs,\n onBehalfOf: request.onBehalfOf,\n job: request.job,\n signal: request.signal,\n scope: request.scope,\n labels: request.labels,\n deadline,\n fallback: \"archive\",\n archiveDespiteRobots: options.archiveDespiteRobots,\n };\n const answer = await archivedCopy(runtime, request.url, reason, page);\n if (answer.doc !== undefined && answer.archived !== undefined) {\n const doc = await recipeDoc(runtime, answer.doc, answer.doc.capture, userProvided, action);\n return {\n recipe: recipe.name,\n url: fetched.url,\n outcome: fetched.outcome,\n status: fetched.status,\n docs: [doc],\n capture: doc.capture,\n fetched_at: doc.fetched_at,\n from_cache: answer.fromCache,\n held_back: doc.held_back,\n error: indexed.error ?? null,\n archived: answer.archived,\n note: noteOf([\n ...notes,\n ...answer.notes,\n \"the page reader read the archived copy, not the extractor of the recipe\",\n doc.held_back ? HELD_BACK : undefined,\n ]),\n };\n }\n notes.push(...answer.notes);\n }\n }\n const capture = fetched.capture;\n const docs: RecipeDoc[] = [];\n for (const doc of indexed.docs)\n docs.push(await recipeDoc(runtime, doc, capture, userProvided, action));\n const held = docs.some((doc) => doc.held_back);\n return {\n recipe: recipe.name,\n url: fetched.url,\n outcome: fetched.outcome,\n status: fetched.status,\n docs,\n capture: docs[0]?.capture ?? capture ?? null,\n fetched_at: docs[0]?.fetched_at ?? fetched.fetchedAt ?? null,\n from_cache: fetched.fromCache,\n held_back: held,\n error: indexed.error ?? null,\n note: noteOf([...notes, held ? HELD_BACK : undefined]),\n };\n};\n"],"mappings":";;;;;;;;;AAwHA,MAAa,YAAY,MAAkB,YAAoC;CAC7E,MAAM,OAAO,WAAW,MAAM,QAAQ,WAAW;CACjD,IAAI;EACF,OAAO,KAAK,MAAM,IAAI;CACxB,QAAQ;EACN,MAAM,IAAI,MACR,GA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,GAAG;KACV,SAAS,IAAI;KACb,YAAY,IAAI;KAChB,YAAY,OAAO;KACnB,WAAW,IAAI;KACf,OAAO,QAAQ,SAAS;KACxB,UAAU,OAAO;KACjB,MAAM,OAAO;MACX,GAAG;MACH,GAAG,OAAO;MACV;MACA,IAAI,YAAY,YAAY,KAAA;KAC9B,CAAC;IACH;GACF;GACA,MAAM,KAAK,GAAG,OAAO,KAAK;EAC5B;CACF;CACA,MAAM,UAAU,QAAQ;CACxB,MAAM,OAAoB,CAAC;CAC3B,KAAK,MAAM,OAAO,QAAQ,MACxB,KAAK,KAAK,MAAM,UAAU,SAAS,KAAK,SAAS,cAAc,MAAM,CAAC;CACxE,MAAM,OAAO,KAAK,MAAM,QAAQ,IAAI,SAAS;CAC7C,OAAO;EACL,QAAQ,OAAO;EACf,KAAK,QAAQ;EACb,SAAS,QAAQ;EACjB,QAAQ,QAAQ;EAChB;EACA,SAAS,KAAK,EAAE,EAAE,WAAW,WAAW;EACxC,YAAY,KAAK,EAAE,EAAE,cAAc,QAAQ,aAAa;EACxD,YAAY,QAAQ;EACpB,WAAW;EACX,OAAO,QAAQ,SAAS;EACxB,MAAM,OAAO,CAAC,GAAG,OAAO,OAAO,YAAY,KAAA,CAAS,CAAC;CACvD;AACF"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { i as durationMs } from "./version-
|
|
2
|
-
import { b as archivedOf, f as recordOverride, l as isHeldBack, n as HELD_BACK, o as fetchPage } from "./runtime-
|
|
1
|
+
import { i as durationMs } from "./version-Hnv1EkvR.mjs";
|
|
2
|
+
import { b as archivedOf, f as recordOverride, l as isHeldBack, n as HELD_BACK, o as fetchPage } from "./runtime-DjchTCwx.mjs";
|
|
3
3
|
import { decodeBody } from "./text.mjs";
|
|
4
4
|
import { extractPage, typesOf } from "./html.mjs";
|
|
5
|
-
import { c as runtimeOf, r as deadlineOf } from "./recipe-
|
|
5
|
+
import { c as runtimeOf, r as deadlineOf } from "./recipe-lfg0UUxc.mjs";
|
|
6
6
|
import { DAYS, describeWeek, disagreements, fromCompact, fromSchemaOrg, fromText } from "./hours.mjs";
|
|
7
7
|
// src/recipe-venue.ts
|
|
8
8
|
/**
|
|
@@ -958,4 +958,4 @@ const readProduct = async (target, url, options = {}) => {
|
|
|
958
958
|
};
|
|
959
959
|
export { readAmount as a, chooseVenueReadings as c, rankHoursLinks as d, readVenue as f, productOf as i, locationMatches as l, pickProduct as n, readProduct as o, venueReadingsOf as p, pickVariant as r, VENUE_LIMITS as s, PRODUCT_LIMITS as t, namesVenue as u };
|
|
960
960
|
|
|
961
|
-
//# sourceMappingURL=recipe-product-
|
|
961
|
+
//# sourceMappingURL=recipe-product-pLJuBblR.mjs.map
|