polite-crawler 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/build/api.d.mts +396 -0
- package/build/api.mjs +325 -0
- package/build/api.mjs.map +1 -0
- package/build/{browser-DCd_d0oU.mjs → browser-C_dFMvO3.mjs} +2 -2
- package/build/{browser-DCd_d0oU.mjs.map → browser-C_dFMvO3.mjs.map} +1 -1
- package/build/classify.d.mts +1 -2
- package/build/{cli-daemon-BTT_6ec8.mjs → cli-daemon-DBvq5TU3.mjs} +2 -2
- package/build/{cli-daemon-BTT_6ec8.mjs.map → cli-daemon-DBvq5TU3.mjs.map} +1 -1
- package/build/{cli-recipes-m_vX7Fu-.mjs → cli-recipes-C--_YyuP.mjs} +2 -2
- package/build/{cli-recipes-m_vX7Fu-.mjs.map → cli-recipes-C--_YyuP.mjs.map} +1 -1
- package/build/cli.mjs +19 -19
- package/build/cli.mjs.map +1 -1
- package/build/{daemon-CWpToMtq.mjs → daemon-BkJp3wXR.mjs} +5 -5
- package/build/{daemon-CWpToMtq.mjs.map → daemon-BkJp3wXR.mjs.map} +1 -1
- package/build/{diff-9UN0TNNe.mjs → diff-DcbpHrxv.mjs} +3 -3
- package/build/{diff-9UN0TNNe.mjs.map → diff-DcbpHrxv.mjs.map} +1 -1
- package/build/{extract-CW-Pzo2l.mjs → extract-DpycIF0M.mjs} +3 -3
- package/build/{extract-CW-Pzo2l.mjs.map → extract-DpycIF0M.mjs.map} +1 -1
- package/build/hours.d.mts +1 -2
- package/build/hours.mjs +1 -1
- package/build/hours.mjs.map +1 -1
- package/build/html-oS_ueu9U.d.mts +1 -2
- package/build/index.d.mts +2 -3
- package/build/index.mjs +14 -14
- package/build/log-CB5bADmd.mjs.map +1 -1
- package/build/{ollama-BDZjLDOK.mjs → ollama-B7eNmGhl.mjs} +2 -2
- package/build/{ollama-BDZjLDOK.mjs.map → ollama-B7eNmGhl.mjs.map} +1 -1
- package/build/{open-D-PoX1Lt.mjs → open-BIvV3pHE.mjs} +11 -11
- package/build/{open-D-PoX1Lt.mjs.map → open-BIvV3pHE.mjs.map} +1 -1
- package/build/politeness.d.mts +1 -2
- package/build/politeness.mjs.map +1 -1
- package/build/reaction-BWTIe95J.mjs +82 -0
- package/build/reaction-BWTIe95J.mjs.map +1 -0
- package/build/{recipe-product-DbuYUSYG.mjs → recipe-product-tVKjy4xu.mjs} +4 -4
- package/build/{recipe-product-DbuYUSYG.mjs.map → recipe-product-tVKjy4xu.mjs.map} +1 -1
- package/build/{recipe-CJrxYoRL.mjs → recipe-rECpzNa-.mjs} +3 -3
- package/build/{recipe-CJrxYoRL.mjs.map → recipe-rECpzNa-.mjs.map} +1 -1
- package/build/render-B0NrFDkJ.mjs.map +1 -1
- package/build/robots.d.mts +1 -2
- package/build/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/build/{runtime-jYXBNvyC.mjs → runtime-Hyr9qD0-.mjs} +30 -46
- package/build/runtime-Hyr9qD0-.mjs.map +1 -0
- package/build/signals.d.mts +1 -2
- package/build/{transformers-C_ziG8yf.mjs → transformers-Cb-KaPge.mjs} +2 -2
- package/build/{transformers-C_ziG8yf.mjs.map → transformers-Cb-KaPge.mjs.map} +1 -1
- package/build/urls.d.mts +1 -2
- package/build/{version-D6jptm-z.mjs → version-D1VudHdM.mjs} +2 -2
- package/build/{version-D6jptm-z.mjs.map → version-D1VudHdM.mjs.map} +1 -1
- package/build/{wacz-e3aVS4rN.mjs → wacz-D_Onr46C.mjs} +4 -4
- package/build/{wacz-e3aVS4rN.mjs.map → wacz-D_Onr46C.mjs.map} +1 -1
- package/build/{warc-Cn9IXXKK.mjs → warc-l7lECYMZ.mjs} +3 -3
- package/build/{warc-Cn9IXXKK.mjs.map → warc-l7lECYMZ.mjs.map} +1 -1
- package/build/{webbotauth-W3GXY3dy.mjs → webbotauth-DbiWmibz.mjs} +2 -2
- package/build/{webbotauth-W3GXY3dy.mjs.map → webbotauth-DbiWmibz.mjs.map} +1 -1
- package/package.json +5 -1
- package/build/rolldown-runtime-BJohB4LM.mjs +0 -13
- package/build/runtime-jYXBNvyC.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -49,6 +49,7 @@ The package is ESM only, and it includes its TypeScript declarations. It has the
|
|
|
49
49
|
| `polite-crawler/politeness` | The queue that keeps one request in progress for each host | Yes |
|
|
50
50
|
| `polite-crawler/signals` | The reader of usage signals (`Content-Signal`) | Yes |
|
|
51
51
|
| `polite-crawler/urls` | The canonical form of a URL, its cache key and its site. It imports no module. | Yes |
|
|
52
|
+
| `polite-crawler/api` | The polite send of a documented API, with conditional polls and the OAuth 2.0 client credentials grant. [Send to a documented API](#send-to-a-documented-api) tells how. | Yes |
|
|
52
53
|
|
|
53
54
|
The entry points that work in a browser import no Node.js module. The package loads no MCP SDK and no `zod`: the MCP server is the package `polite-crawler-mcp`.
|
|
54
55
|
|
|
@@ -295,6 +296,8 @@ The engine of the recipes is `registerRecipe`, `rankHoursLinks`, `namesVenue`, `
|
|
|
295
296
|
|
|
296
297
|
The engine of the archived fallback and of the signatures is `archiveExtractor`, `archivedDocId`, and the `fallback` and `archiveDespiteRobots` of `PageOptions`. It also includes `range`, `signalLines` and `originalHeaderPrefix` of `FetchRequest`, `ai_input` of `ExtractedDoc`, `sign` of `RenderOptions` and `signer` of `CrawlerOptions`.
|
|
297
298
|
|
|
299
|
+
Each export of `polite-crawler/api` is the engine too. This includes the reaction of the crawler: `outcomeResult` and `reactToOutcome`, with `OutcomeFacts`, `ReactionContext` and `HostReaction`.
|
|
300
|
+
|
|
298
301
|
Version 0.2.0 removed five exports from the root entry point: `checkedRenderer`, `renderedKey`, `pageDocId`, `renderAndIndex` and `pageExtractor`. No documented use needed them. `durationMs` stays in the engine, because `polite-crawler-mcp` uses it.
|
|
299
302
|
|
|
300
303
|
## Hybrid search
|
|
@@ -1248,6 +1251,64 @@ User-agent: PoliteCrawler
|
|
|
1248
1251
|
Disallow: /
|
|
1249
1252
|
```
|
|
1250
1253
|
|
|
1254
|
+
## Send to a documented API
|
|
1255
|
+
|
|
1256
|
+
The entry point `polite-crawler/api` sends the requests of an API client with the same politeness as the crawler. It stores no page. Give it the scheduler of the crawler, so a host has one pace for pages and API requests.
|
|
1257
|
+
|
|
1258
|
+
```ts
|
|
1259
|
+
import { fetchTransport, send } from "polite-crawler/api";
|
|
1260
|
+
import { HostScheduler, realClock } from "polite-crawler/politeness";
|
|
1261
|
+
|
|
1262
|
+
const scheduler = new HostScheduler(realClock, 4);
|
|
1263
|
+
const pace = { scheduler, now: realClock.now };
|
|
1264
|
+
const transport = fetchTransport({ userAgent: "example-app/1.0 (ops@example.com)" });
|
|
1265
|
+
|
|
1266
|
+
const answer = await send(pace, transport, {
|
|
1267
|
+
source: "Example API",
|
|
1268
|
+
host: "api.example.com",
|
|
1269
|
+
minIntervalMs: 500,
|
|
1270
|
+
what: "the product P-1",
|
|
1271
|
+
exchange: { method: "GET", url: "https://api.example.com/products/P-1", headers: {}, body: null },
|
|
1272
|
+
});
|
|
1273
|
+
```
|
|
1274
|
+
|
|
1275
|
+
- **The transport is a port.** `fetchTransport` sends with `fetch`. Give a different HTTP client, or a fake in a test. The send changes no part of the exchange.
|
|
1276
|
+
- **A User-Agent and a time limit.** `fetchTransport` needs a User-Agent that names the client and a contact. Some APIs refuse a request with no User-Agent. It stops a request and its body after `timeoutMs` (default 30 seconds), and the send counts that as a failure of the host. It does not follow a redirect.
|
|
1277
|
+
- **The reaction is the reaction of the crawler.** `reactToOutcome` sets the rest and the stops of the host after each answer. A 429 waits the `Retry-After`, a 403 stops the host, and five failures in sequence stop it.
|
|
1278
|
+
- **Each answer comes back.** The send gives each status to the caller. It throws an `ApiError` only when the API gives no answer, or when the host is stopped.
|
|
1279
|
+
- **Tokens.** `tokenStore` keeps one token of the OAuth 2.0 client credentials grant, and `readGrant` reads the answer of the token endpoint. `sendWithToken` gets a new token after a 401 and sends one more time. A second 401 stops the host.
|
|
1280
|
+
- **No secret in a message.** Give the keys of the exchange in `secrets`, and a `redact` function. The send gives the text of each error to `redact`. `sendWithToken` adds the token to the secrets. The default `redact` changes nothing, so give one that removes each form of a secret.
|
|
1281
|
+
|
|
1282
|
+
### Poll an API
|
|
1283
|
+
|
|
1284
|
+
`poll` sends a conditional GET at the pace of the host. It keeps the `ETag` and the `Last-Modified` date of the last answer 2xx of each URL. They go in a store that you give, for example a `Map`. The next poll sends them as `If-None-Match` and `If-Modified-Since`. A poll every 60 seconds then costs a 304 with no body while the data does not change.
|
|
1285
|
+
|
|
1286
|
+
```ts
|
|
1287
|
+
import { fetchTransport, poll, type Condition } from "polite-crawler/api";
|
|
1288
|
+
|
|
1289
|
+
const conditions = new Map<string, Condition>();
|
|
1290
|
+
const polled = await poll(pace, transport, conditions, {
|
|
1291
|
+
source: "Example alerts",
|
|
1292
|
+
host: "api.example.com",
|
|
1293
|
+
minIntervalMs: 30_000,
|
|
1294
|
+
what: "the active alerts",
|
|
1295
|
+
exchange: {
|
|
1296
|
+
method: "GET",
|
|
1297
|
+
url: "https://api.example.com/alerts/active",
|
|
1298
|
+
headers: { accept: "application/geo+json" },
|
|
1299
|
+
body: null,
|
|
1300
|
+
},
|
|
1301
|
+
});
|
|
1302
|
+
if (polled.changed) {
|
|
1303
|
+
// read polled.reply.body
|
|
1304
|
+
}
|
|
1305
|
+
```
|
|
1306
|
+
|
|
1307
|
+
- **`changed`** is true only for an answer 2xx with a body that is not the last body of the URL. A 304, the same body again and each other status give false. A 304 has no body: keep the result of the last changed body.
|
|
1308
|
+
- **The poll keeps no page.** The store holds the two validators and the SHA-256 of the last body.
|
|
1309
|
+
- **The validator memo of the crawler.** Some hosts give an `ETag` that changes at each answer. With a store of the scheduler that keeps the validator memo, the poll then sends only `If-Modified-Since`, as the crawler does.
|
|
1310
|
+
- **One store for one set of headers.** A store keeps one entry for each URL. Give a second store to a request with a different `accept` header.
|
|
1311
|
+
|
|
1251
1312
|
## Stored copies and conditional requests
|
|
1252
1313
|
|
|
1253
1314
|
A fetch uses the stored copy of a URL, with no request, while the copy is younger than `maxAge` (24 hours by default). With `maxAge: "auto"`, the crawler uses the freshness that the server gave: `Cache-Control`, `Expires`, or 10 percent of the age of `Last-Modified`. With `"auto"`, `no-store` and `no-cache` make a copy stale at once. A fixed `maxAge` ignores them. A response that varies on a request header (`Vary`) is used again only for a request with the same value of that header. The store keeps one copy for each URL, so a request with a different value replaces it.
|
package/build/api.d.mts
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The polite send of a documented API. It keeps the pace of each host with the scheduler of the
|
|
3
|
+
* crawler (`HostScheduler`). As for the `Crawler`, `reactToOutcome` gives its reaction to each
|
|
4
|
+
* answer. The send stores no page, because the terms of some APIs forbid a store.
|
|
5
|
+
*
|
|
6
|
+
* The transport is a port: a host gives `fetch` or a different HTTP client, and a test gives a fake.
|
|
7
|
+
* The module imports no Node.js module, and it uses no type of a platform except the signal.
|
|
8
|
+
*
|
|
9
|
+
* The module also has the OAuth 2.0 client credentials grant (RFC 6749, section 4.4): `readGrant`,
|
|
10
|
+
* `tokenStore` and `sendWithToken`. `fetchTransport` is a transport over `fetch` with a User-Agent
|
|
11
|
+
* and a time limit. `poll` sends conditional requests. Each export is beta: a minor version can
|
|
12
|
+
* change it.
|
|
13
|
+
*/
|
|
14
|
+
import { Classified, Outcome } from "./classify.mjs";
|
|
15
|
+
import { HostCounts, HostResult, HostStateStore, HostStop, Paced, PolicyOptions, PolitenessPolicy, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict } from "./politeness.mjs";
|
|
16
|
+
/**
|
|
17
|
+
* These are the facts of one answer that set the reaction of its host. `classify` gives the
|
|
18
|
+
* outcome and the wait that the server asked for.
|
|
19
|
+
*
|
|
20
|
+
* @beta
|
|
21
|
+
*/
|
|
22
|
+
interface OutcomeFacts {
|
|
23
|
+
/** This value is the outcome of the answer. */
|
|
24
|
+
readonly outcome: Outcome;
|
|
25
|
+
/** This value is the status of the answer. The value 0 is a request with no answer. */
|
|
26
|
+
readonly status: number;
|
|
27
|
+
/** This value is the wait that the server asked for (`Retry-After`). */
|
|
28
|
+
readonly retryAfterMs?: number | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* A refusal (`blocked`) with one of these statuses does not stop the host. The crawler spares
|
|
31
|
+
* 401 and 403 for robots.txt (RFC 9309). An API client spares a 401 before it gets a new token.
|
|
32
|
+
*/
|
|
33
|
+
readonly spare?: readonly number[] | undefined;
|
|
34
|
+
/** This result replaces the result of the outcome, and the host gets only its rest. A cancelled request is `neutral`. */
|
|
35
|
+
readonly result?: HostResult | undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* These values set the reaction to one answer.
|
|
39
|
+
*
|
|
40
|
+
* @beta
|
|
41
|
+
*/
|
|
42
|
+
interface ReactionContext {
|
|
43
|
+
/** This value is the policy of the host. */
|
|
44
|
+
readonly policy: PolitenessPolicy;
|
|
45
|
+
/** This value is the time of the answer, in milliseconds since the epoch. */
|
|
46
|
+
readonly now: number;
|
|
47
|
+
/** This value is the rest of the host that `restAfter` gives for the request. */
|
|
48
|
+
readonly restMs: number;
|
|
49
|
+
/** This function gives a number in [0, 1) for the jitter of a backoff. */
|
|
50
|
+
readonly random: () => number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* This value tells how one answer changes its host, after its counts changed.
|
|
54
|
+
*
|
|
55
|
+
* @beta
|
|
56
|
+
*/
|
|
57
|
+
interface HostReaction {
|
|
58
|
+
/** This value is the rest of the host before its next request, in milliseconds. */
|
|
59
|
+
readonly restMs: number;
|
|
60
|
+
/**
|
|
61
|
+
* This value tells why the rest is longer than the rest of `restAfter`: a backoff, or the
|
|
62
|
+
* `Retry-After` of the server. A longer rest parks the host.
|
|
63
|
+
*/
|
|
64
|
+
readonly longer: "backoff" | "retry-after" | undefined;
|
|
65
|
+
/** This value is the stop of the host, when the answer stops it. */
|
|
66
|
+
readonly stop: HostStop | undefined;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* This function tells how one answer changes the counts of its host. Give the result to
|
|
70
|
+
* `HostStateStore.record`. These are the rules:
|
|
71
|
+
*
|
|
72
|
+
* - A 429, 502, 503 or 504 is a wait.
|
|
73
|
+
* - A different error and a request with no answer are failures. A client error (`isClientError`) is neutral.
|
|
74
|
+
* - Each other outcome is a success. A refusal is a success too, because its stop is the reaction.
|
|
75
|
+
*
|
|
76
|
+
* @beta
|
|
77
|
+
*/
|
|
78
|
+
export declare const outcomeResult: (facts: OutcomeFacts) => HostResult;
|
|
79
|
+
/**
|
|
80
|
+
* This function gives the reaction of a host to one answer, with the counts that `record` gave for
|
|
81
|
+
* the result of `outcomeResult`. The `Crawler` and the send of `polite-crawler/api` use it. These
|
|
82
|
+
* are the rules, with the times of the policy:
|
|
83
|
+
*
|
|
84
|
+
* - A 429, 502, 503 or 504 parks the host for its `Retry-After`, or for a backoff with jitter. A
|
|
85
|
+
* `Retry-After` longer than `maxRetryAfterMs` stops the host for that time, up to 30 days.
|
|
86
|
+
* - A failure parks the host for a backoff with jitter. After `maxFailures` failures in sequence, the host also stops.
|
|
87
|
+
* - A 401 or a 403 stops the host for `blockedStopMs`. A 402 and a bot challenge stop it longer.
|
|
88
|
+
* - Each other answer gives the rest of `restAfter`.
|
|
89
|
+
*
|
|
90
|
+
* The function is pure, so a simulation and a study can use the reactions of the crawler.
|
|
91
|
+
*
|
|
92
|
+
* @beta
|
|
93
|
+
*/
|
|
94
|
+
export declare const reactToOutcome: (facts: OutcomeFacts, counts: HostCounts, context: ReactionContext) => HostReaction;
|
|
95
|
+
/**
|
|
96
|
+
* One request of a client to an API.
|
|
97
|
+
*
|
|
98
|
+
* @beta
|
|
99
|
+
*/
|
|
100
|
+
export interface Exchange {
|
|
101
|
+
/** This value is the method of the request, for example `GET` or `POST`. */
|
|
102
|
+
readonly method: string;
|
|
103
|
+
readonly url: string;
|
|
104
|
+
/** These are the headers of the request. Each name is in lowercase. */
|
|
105
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
106
|
+
/** This value is the body of the request, or null. */
|
|
107
|
+
readonly body: string | null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The answer of an API to one exchange.
|
|
111
|
+
*
|
|
112
|
+
* @beta
|
|
113
|
+
*/
|
|
114
|
+
export interface Reply {
|
|
115
|
+
readonly status: number;
|
|
116
|
+
/** This value is the reason phrase of the status line. HTTP/2 has no reason phrase, so it can be empty. */
|
|
117
|
+
readonly statusText: string;
|
|
118
|
+
/** These are the headers of the answer. Each name is in lowercase. */
|
|
119
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
120
|
+
readonly body: string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The part of a signal that the send reads. An `AbortSignal` has it.
|
|
124
|
+
*
|
|
125
|
+
* @beta
|
|
126
|
+
*/
|
|
127
|
+
export interface Signal {
|
|
128
|
+
readonly aborted: boolean;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A transport sends one exchange and gives the answer. It does not follow a redirect. It throws an
|
|
132
|
+
* error when the API gives no answer, and when the signal stops the request.
|
|
133
|
+
*
|
|
134
|
+
* @beta
|
|
135
|
+
*/
|
|
136
|
+
export type Transport<S extends Signal = AbortSignal> = (exchange: Exchange, signal: S | undefined) => Promise<Reply>;
|
|
137
|
+
/**
|
|
138
|
+
* The part of a `HostScheduler` that the send uses. A `HostScheduler` has each member.
|
|
139
|
+
*
|
|
140
|
+
* @beta
|
|
141
|
+
*/
|
|
142
|
+
export interface ApiScheduler<S extends Signal = AbortSignal> {
|
|
143
|
+
run<T>(host: string, task: () => Promise<Paced<T>>, signal?: S): Promise<T>;
|
|
144
|
+
/**
|
|
145
|
+
* This store keeps the counts and the stops of each host. When it has the validator memo
|
|
146
|
+
* (`validators` and `noteValidators`), `poll` uses it as the crawler does. The two members are optional, as in
|
|
147
|
+
* `HostStateStore`, so a store of `record` and `stop` is sufficient.
|
|
148
|
+
*/
|
|
149
|
+
readonly store: Pick<HostStateStore, "record" | "stop" | "validators" | "noteValidators">;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The pace of the API clients of one process. Give one pace to each client, so each host keeps one pace.
|
|
153
|
+
*
|
|
154
|
+
* @beta
|
|
155
|
+
*/
|
|
156
|
+
export interface Pace<S extends Signal = AbortSignal> {
|
|
157
|
+
/** This scheduler keeps one request in progress for each host. Give the scheduler of the crawler to share its pace. */
|
|
158
|
+
readonly scheduler: ApiScheduler<S>;
|
|
159
|
+
/** This function gives the time in milliseconds since the epoch. Give the clock of the scheduler. */
|
|
160
|
+
readonly now: () => number;
|
|
161
|
+
/** This value is the politeness policy. The default is `DEFAULT_POLICY`. */
|
|
162
|
+
readonly policy?: PolicyOptions | undefined;
|
|
163
|
+
/** This function gives a number in [0, 1) for the jitter of a backoff. The default is `Math.random`. */
|
|
164
|
+
readonly random?: (() => number) | undefined;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* This function removes each secret from the text of an error. `secrets` holds the secrets of the
|
|
168
|
+
* exchange: its keys and its token.
|
|
169
|
+
*
|
|
170
|
+
* @beta
|
|
171
|
+
*/
|
|
172
|
+
export type Redact = (text: string, secrets: readonly string[]) => string;
|
|
173
|
+
/**
|
|
174
|
+
* An error of the send or of the token. The `redact` function of the send examined its message.
|
|
175
|
+
*
|
|
176
|
+
* @beta
|
|
177
|
+
*/
|
|
178
|
+
export declare class ApiError extends Error {
|
|
179
|
+
override readonly name: string;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* One exchange of a client, and its context for the pace and the messages.
|
|
183
|
+
*
|
|
184
|
+
* @beta
|
|
185
|
+
*/
|
|
186
|
+
export interface Send {
|
|
187
|
+
/** This value is the name of the API in a message, for example `Example API`. */
|
|
188
|
+
readonly source: string;
|
|
189
|
+
/** This value is the host of the URL. The scheduler keeps one pace for each host. */
|
|
190
|
+
readonly host: string;
|
|
191
|
+
/** This value is the documented pace of the API: the shortest time between two requests. The default is `apiIntervalMs` of the policy. */
|
|
192
|
+
readonly minIntervalMs?: number | undefined;
|
|
193
|
+
/** This value tells what the exchange asks for, in a message, for example `the product P-1`. */
|
|
194
|
+
readonly what: string;
|
|
195
|
+
readonly exchange: Exchange;
|
|
196
|
+
/** These are the secrets of the exchange. The send gives them to `redact`. */
|
|
197
|
+
readonly secrets?: readonly string[] | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* This function removes the secrets from the text of each error. The default changes nothing.
|
|
200
|
+
* Give a function that removes each form of a secret, for example `redactSecrets` of `@mark1russell7/text`.
|
|
201
|
+
*/
|
|
202
|
+
readonly redact?: Redact | undefined;
|
|
203
|
+
/** With true, a 401 does not stop the host. The client then gets a new token one time. */
|
|
204
|
+
readonly tokenRetry?: boolean | undefined;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* An answer, and what it means for the host.
|
|
208
|
+
*
|
|
209
|
+
* @beta
|
|
210
|
+
*/
|
|
211
|
+
export interface Answer {
|
|
212
|
+
readonly reply: Reply;
|
|
213
|
+
readonly classified: Classified;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* This function gives the status line of an answer for a message, with no control character.
|
|
217
|
+
*
|
|
218
|
+
* @beta
|
|
219
|
+
*/
|
|
220
|
+
export declare function statusLine(reply: Reply): string;
|
|
221
|
+
/**
|
|
222
|
+
* This function sends one exchange at the pace of its host. The scheduler waits the rest of the
|
|
223
|
+
* host first. After the answer, `reactToOutcome` sets the rest of the host and its stops, as for
|
|
224
|
+
* the `Crawler`:
|
|
225
|
+
*
|
|
226
|
+
* - A 429, 502, 503 or 504 parks the host for its `Retry-After`, or for a backoff with jitter.
|
|
227
|
+
* - A 401 or a 403 stops the host for 1 hour. A 402 and a bot challenge stop it for 24 hours.
|
|
228
|
+
* - An error 5xx and an exchange with no answer are failures. After 5 failures in sequence, the host stops.
|
|
229
|
+
*
|
|
230
|
+
* The function gives each answer, with each status. The caller reads the status.
|
|
231
|
+
*
|
|
232
|
+
* @throws ApiError when the API gives no answer, or when the host is stopped. `redact` examined the message.
|
|
233
|
+
* @throws the reason of the signal, when the signal stops the exchange.
|
|
234
|
+
* @beta
|
|
235
|
+
*/
|
|
236
|
+
export declare function send<S extends Signal = AbortSignal>(pace: Pace<S>, transport: Transport<S>, input: Send, signal?: S): Promise<Answer>;
|
|
237
|
+
/**
|
|
238
|
+
* The token of a client credentials grant, and the time when the client stops its use.
|
|
239
|
+
*
|
|
240
|
+
* @beta
|
|
241
|
+
*/
|
|
242
|
+
export interface Grant {
|
|
243
|
+
readonly token: string;
|
|
244
|
+
/** The client uses the token before this time, in milliseconds since the epoch. */
|
|
245
|
+
readonly until: number;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* A client stops the use of a token this time before its end.
|
|
249
|
+
*
|
|
250
|
+
* @beta
|
|
251
|
+
*/
|
|
252
|
+
export declare const TOKEN_MARGIN_MS: number;
|
|
253
|
+
/**
|
|
254
|
+
* This function reads the answer of a token endpoint of the OAuth 2.0 client credentials grant
|
|
255
|
+
* (RFC 6749, section 4.4). The client uses the token until `TOKEN_MARGIN_MS` before its end, or
|
|
256
|
+
* for half of a short life.
|
|
257
|
+
*
|
|
258
|
+
* @throws ApiError when the answer is not a 2xx with an `access_token` and an `expires_in`. The
|
|
259
|
+
* message has no part of the body, and `redact` examined it.
|
|
260
|
+
* @beta
|
|
261
|
+
*/
|
|
262
|
+
export declare function readGrant(reply: Reply, source: string, issuedAt: number, secrets?: readonly string[], redact?: Redact): Grant;
|
|
263
|
+
/**
|
|
264
|
+
* A store of one token. The exchanges that wait for a token at one time share one grant.
|
|
265
|
+
*
|
|
266
|
+
* @beta
|
|
267
|
+
*/
|
|
268
|
+
export interface Tokens<S extends Signal = AbortSignal> {
|
|
269
|
+
/** This method gives a token that the client can use. With no such token, it gets a new token. */
|
|
270
|
+
get(signal?: S): Promise<string>;
|
|
271
|
+
/**
|
|
272
|
+
* This method forgets the token after an answer 401. With a token, it forgets only that token,
|
|
273
|
+
* so a second 401 of an old token does not drop a newer token.
|
|
274
|
+
*/
|
|
275
|
+
drop(token?: string): void;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* This function makes a store of one token. The store gets a new token from `grant` when the token
|
|
279
|
+
* ends. `now` gives the time in milliseconds since the epoch.
|
|
280
|
+
*
|
|
281
|
+
* @beta
|
|
282
|
+
*/
|
|
283
|
+
export declare function tokenStore<S extends Signal = AbortSignal>(grant: (signal: S | undefined) => Promise<Grant>, now: () => number): Tokens<S>;
|
|
284
|
+
/**
|
|
285
|
+
* This function sends an exchange with a token. `headers` gives the headers of the token, for
|
|
286
|
+
* example `authorization`. After an answer 401, the function forgets the token, gets a new token,
|
|
287
|
+
* and sends the exchange one more time. A second 401 stops the host. The token goes into the
|
|
288
|
+
* secrets of the messages.
|
|
289
|
+
*
|
|
290
|
+
* @throws ApiError as `send` does, and the error of the grant.
|
|
291
|
+
* @beta
|
|
292
|
+
*/
|
|
293
|
+
export declare function sendWithToken<S extends Signal = AbortSignal>(pace: Pace<S>, transport: Transport<S>, tokens: Tokens<S>, input: Send, headers: (token: string) => Readonly<Record<string, string>>, signal?: S): Promise<Answer>;
|
|
294
|
+
/**
|
|
295
|
+
* The part of a `fetch` function that `fetchTransport` uses. The `fetch` of Node.js and of a
|
|
296
|
+
* browser has it.
|
|
297
|
+
*
|
|
298
|
+
* @beta
|
|
299
|
+
*/
|
|
300
|
+
export type FetchLike = (url: string, init: {
|
|
301
|
+
readonly method: string;
|
|
302
|
+
readonly headers: Record<string, string>;
|
|
303
|
+
readonly body?: string;
|
|
304
|
+
readonly redirect: "manual";
|
|
305
|
+
readonly signal: AbortSignal;
|
|
306
|
+
}) => Promise<{
|
|
307
|
+
readonly status: number;
|
|
308
|
+
readonly statusText: string;
|
|
309
|
+
readonly headers: {
|
|
310
|
+
forEach(callback: (value: string, name: string) => void): void;
|
|
311
|
+
};
|
|
312
|
+
text(): Promise<string>;
|
|
313
|
+
}>;
|
|
314
|
+
/**
|
|
315
|
+
* The default time limit of `fetchTransport`: 30 seconds for the request and its body.
|
|
316
|
+
*
|
|
317
|
+
* @beta
|
|
318
|
+
*/
|
|
319
|
+
export declare const API_TIMEOUT_MS: number;
|
|
320
|
+
/**
|
|
321
|
+
* The options of `fetchTransport`.
|
|
322
|
+
*
|
|
323
|
+
* @beta
|
|
324
|
+
*/
|
|
325
|
+
export interface FetchTransportOptions {
|
|
326
|
+
/**
|
|
327
|
+
* This value names the client and a contact, for example `example-app/1.0 (ops@example.com)`.
|
|
328
|
+
* Each request sends it, except a request with its own `user-agent` header. Some APIs, for
|
|
329
|
+
* example `api.weather.gov`, refuse a request with no User-Agent.
|
|
330
|
+
*/
|
|
331
|
+
readonly userAgent: string;
|
|
332
|
+
/** This value is the time limit of one request and its body, in milliseconds. The default is `API_TIMEOUT_MS`. */
|
|
333
|
+
readonly timeoutMs?: number | undefined;
|
|
334
|
+
/** This function sends the request. The default is the global `fetch`. */
|
|
335
|
+
readonly fetch?: FetchLike | undefined;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* This function makes a transport over `fetch`. The transport sends the User-Agent, does not
|
|
339
|
+
* follow a redirect, and stops a request after the time limit. A request that the time limit
|
|
340
|
+
* stopped throws an error, and `send` counts it as a failure of the host. A request that the
|
|
341
|
+
* signal stopped throws the reason of the signal.
|
|
342
|
+
*
|
|
343
|
+
* @throws ApiError when the User-Agent is not printable ASCII, or when the time limit is not a positive number.
|
|
344
|
+
* @beta
|
|
345
|
+
*/
|
|
346
|
+
export declare function fetchTransport(options: FetchTransportOptions): Transport;
|
|
347
|
+
/**
|
|
348
|
+
* What a poll keeps of the last answer 2xx of one URL.
|
|
349
|
+
*
|
|
350
|
+
* @beta
|
|
351
|
+
*/
|
|
352
|
+
export interface Condition {
|
|
353
|
+
/** This value is the `ETag` header of the answer, when it had one. */
|
|
354
|
+
readonly etag?: string | undefined;
|
|
355
|
+
/** This value is the `Last-Modified` header of the answer, when it had one. */
|
|
356
|
+
readonly lastModified?: string | undefined;
|
|
357
|
+
/** This value is the SHA-256 of the body, in hexadecimal. */
|
|
358
|
+
readonly digest: string;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* A store of the conditions of a poll, one for each URL. A `Map` is a store in memory. One store
|
|
362
|
+
* goes with one set of request headers: give a second store to a second `accept` header.
|
|
363
|
+
*
|
|
364
|
+
* @beta
|
|
365
|
+
*/
|
|
366
|
+
export interface Conditions {
|
|
367
|
+
get(url: string): Condition | undefined;
|
|
368
|
+
set(url: string, condition: Condition): void;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* An answer of a poll.
|
|
372
|
+
*
|
|
373
|
+
* @beta
|
|
374
|
+
*/
|
|
375
|
+
export interface Polled extends Answer {
|
|
376
|
+
/**
|
|
377
|
+
* This value is true for an answer 2xx with a body that is not the body of the last answer 2xx
|
|
378
|
+
* of the URL. It is false for a 304, for the same body, and for each other status.
|
|
379
|
+
*/
|
|
380
|
+
readonly changed: boolean;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* This function sends one conditional GET at the pace of its host, as `send` does. It sends the
|
|
384
|
+
* validators of the last answer 2xx of the URL: `If-None-Match` with its ETag and
|
|
385
|
+
* `If-Modified-Since` with its date. The store of the scheduler can have a validator memo. Then
|
|
386
|
+
* the function sends only the date to a host whose ETags do not match, as the crawler does.
|
|
387
|
+
*
|
|
388
|
+
* A 304 gives no body: the caller keeps the result of the last changed body. A server that does
|
|
389
|
+
* not answer 304 can send the same body again. The function then gives `changed: false` too.
|
|
390
|
+
*
|
|
391
|
+
* @throws ApiError as `send` does, and when the method is not GET.
|
|
392
|
+
* @throws the reason of the signal, when the signal stops the exchange.
|
|
393
|
+
* @beta
|
|
394
|
+
*/
|
|
395
|
+
export declare function poll<S extends Signal = AbortSignal>(pace: Pace<S>, transport: Transport<S>, conditions: Conditions, input: Send, signal?: S): Promise<Polled>;
|
|
396
|
+
export type { Classified, HostCounts, HostReaction, HostResult, HostStateStore, HostStop, Outcome, OutcomeFacts, Paced, PolicyOptions, PolitenessPolicy, ReactionContext, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict };
|