polite-crawler 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -1
- package/build/api.d.mts +274 -20
- package/build/api.mjs +455 -4
- package/build/api.mjs.map +1 -1
- package/build/{cli-daemon-DBvq5TU3.mjs → cli-daemon-Bke7kU1e.mjs} +2 -2
- package/build/{cli-daemon-DBvq5TU3.mjs.map → cli-daemon-Bke7kU1e.mjs.map} +1 -1
- package/build/{cli-recipes-C--_YyuP.mjs → cli-recipes-CUFd9ORK.mjs} +2 -2
- package/build/{cli-recipes-C--_YyuP.mjs.map → cli-recipes-CUFd9ORK.mjs.map} +1 -1
- package/build/cli.mjs +7 -7
- package/build/{daemon-BkJp3wXR.mjs → daemon-Dh32XlZq.mjs} +3 -3
- package/build/{daemon-BkJp3wXR.mjs.map → daemon-Dh32XlZq.mjs.map} +1 -1
- package/build/{diff-DcbpHrxv.mjs → diff-ZEIt8xv3.mjs} +2 -2
- package/build/{diff-DcbpHrxv.mjs.map → diff-ZEIt8xv3.mjs.map} +1 -1
- package/build/{extract-DpycIF0M.mjs → extract-B_Tsv1UC.mjs} +3 -3
- package/build/{extract-DpycIF0M.mjs.map → extract-B_Tsv1UC.mjs.map} +1 -1
- package/build/html.d.mts +83 -2
- package/build/html.mjs +844 -1
- package/build/html.mjs.map +1 -0
- package/build/index.d.mts +15 -10
- package/build/index.mjs +12 -11
- package/build/{open-BIvV3pHE.mjs → open-Dx1gCR6l.mjs} +77 -55
- package/build/open-Dx1gCR6l.mjs.map +1 -0
- package/build/politeness.d.mts +33 -0
- package/build/politeness.mjs.map +1 -1
- package/build/{recipe-rECpzNa-.mjs → recipe-BO3V_1Kr.mjs} +4 -4
- package/build/{recipe-rECpzNa-.mjs.map → recipe-BO3V_1Kr.mjs.map} +1 -1
- package/build/{recipe-product-tVKjy4xu.mjs → recipe-product-BOQMCTGh.mjs} +6 -5
- package/build/{recipe-product-tVKjy4xu.mjs.map → recipe-product-BOQMCTGh.mjs.map} +1 -1
- package/build/{runtime-Hyr9qD0-.mjs → runtime-BVC7iOG4.mjs} +4 -3
- package/build/{runtime-Hyr9qD0-.mjs.map → runtime-BVC7iOG4.mjs.map} +1 -1
- package/build/text.d.mts +10 -0
- package/build/text.mjs +188 -0
- package/build/text.mjs.map +1 -0
- package/build/{version-D1VudHdM.mjs → version-DxUGcHQr.mjs} +2 -2
- package/build/{version-D1VudHdM.mjs.map → version-DxUGcHQr.mjs.map} +1 -1
- package/build/{wacz-D_Onr46C.mjs → wacz-DjgklRod.mjs} +3 -3
- package/build/{wacz-D_Onr46C.mjs.map → wacz-DjgklRod.mjs.map} +1 -1
- package/build/{warc-l7lECYMZ.mjs → warc-CgkNdXyG.mjs} +2 -2
- package/build/{warc-l7lECYMZ.mjs.map → warc-CgkNdXyG.mjs.map} +1 -1
- package/package.json +7 -1
- package/build/html-CawWTgMQ.mjs +0 -1030
- package/build/html-CawWTgMQ.mjs.map +0 -1
- package/build/html-oS_ueu9U.d.mts +0 -92
- package/build/open-BIvV3pHE.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/text` | The decoder of a body in its declared encoding. It imports no module. | Yes |
|
|
52
53
|
| `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 |
|
|
53
54
|
|
|
54
55
|
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`.
|
|
@@ -269,7 +270,7 @@ These parts are the stable API:
|
|
|
269
270
|
- The watch list: `WatchList`, `WatchOptions`, `WatchEntry`, `WatchPending`, `WatchKind`, `WatchRun`, `WatchRunOptions`, `WatchEvent`, `WatchSummary`, `WatchStop`, `WATCH_LIMITS` and the file `watch.sqlite` (`WATCH_SCHEMA`).
|
|
270
271
|
- The daemon: `startDaemon`, `scheduleWatches`, `changeCommand`, `readDaemonState`, `daemonStateFile`, `DAEMON_DEFAULTS`, their options and results, and the state file (`DAEMON_STATE_SCHEMA`). The MCP seam of the daemon is the engine: `DaemonMcpHooks`, `DaemonMcpHost`, `DaemonMcpOptions`, `DaemonMcpService`, the options `mcp` and `mcpHost`, and `Daemon.mcp`. The MCP server of the daemon is `mcpHttpHost` of `polite-crawler-mcp`.
|
|
271
272
|
- The options and the results of `discover` and `crawl`: `DiscoverOptions`, `DiscoverResult`, `DiscoveredUrl`, `DiscoverSource`, `DiscoverStop`, `CrawlOptions`, `CrawlRun`, `CrawlPage`, `CrawlSummary`, `CrawlStop`, `UrlPattern` and `ScopeOptions`.
|
|
272
|
-
- The readers: `polite-crawler/robots`, `/hours`, `/html`, `/classify`, `/politeness` and `/
|
|
273
|
+
- The readers: `polite-crawler/robots`, `/hours`, `/html`, `/classify`, `/politeness`, `/signals`, `/urls` and `/text`. The site and its tools use them in the browser.
|
|
273
274
|
- The command line, its exit codes, the store on disk and `polite-crawler/record.schema.json`.
|
|
274
275
|
- The export: `exportWarc` and `exportWacz`, their options (`ArchiveOptions`, `ArchiveFilter`, `ArchiveSource`), their results (`WarcResult`, `WaczResult`, `ArchiveCounts`) and the records of the archive.
|
|
275
276
|
- The archived fallback: `fallback` and `archiveDespiteRobots` of `FetchOptions`, the field `archived` of the results, and the types `Archived` and `WebArchive`.
|
|
@@ -1309,6 +1310,70 @@ if (polled.changed) {
|
|
|
1309
1310
|
- **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
1311
|
- **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
1312
|
|
|
1313
|
+
### Quotas of a key
|
|
1314
|
+
|
|
1315
|
+
Many free API keys have a quota: a count of calls or messages in each day, month or window. The pace of the host does not count it. `quotaGate` counts the calls of one key, and it refuses a call over the quota before the provider does. `withQuota` puts a gate on an async function and keeps its type. So it fits each path of a call: a `fetch` that a client takes, a transport of `send` and `poll`, or a different client.
|
|
1316
|
+
|
|
1317
|
+
```ts
|
|
1318
|
+
import { quotaGate, readQuotaHeaders, withQuota } from "polite-crawler/api";
|
|
1319
|
+
import { SqliteHostStateStore } from "polite-crawler";
|
|
1320
|
+
|
|
1321
|
+
// Pushover: 10,000 messages each month for each user account, reset at 00:00 Central Time on the 1st
|
|
1322
|
+
const gate = quotaGate(
|
|
1323
|
+
{
|
|
1324
|
+
name: "Pushover messages",
|
|
1325
|
+
limit: 10_000,
|
|
1326
|
+
reserve: 100,
|
|
1327
|
+
window: { kind: "calendar", unit: "month", timeZone: "America/Chicago" },
|
|
1328
|
+
},
|
|
1329
|
+
{ key: "pushover:account-1", store: new SqliteHostStateStore("state/hosts.sqlite") },
|
|
1330
|
+
);
|
|
1331
|
+
|
|
1332
|
+
// a fetch for a client that takes one; only a message counts, not a read of a receipt
|
|
1333
|
+
const fetchWithQuota = withQuota(gate, fetch, {
|
|
1334
|
+
cost: (input) =>
|
|
1335
|
+
new URL(input instanceof Request ? input.url : input).pathname.endsWith("/messages.json") ? 1 : 0,
|
|
1336
|
+
read: (response) =>
|
|
1337
|
+
readQuotaHeaders(
|
|
1338
|
+
(name) => response.headers.get(name),
|
|
1339
|
+
{ remaining: "X-Limit-App-Remaining", resetAt: "X-Limit-App-Reset" },
|
|
1340
|
+
Date.now(),
|
|
1341
|
+
),
|
|
1342
|
+
});
|
|
1343
|
+
```
|
|
1344
|
+
|
|
1345
|
+
- **A refusal before the call.** A call over the quota throws `QuotaError` with `resetAt`, the time when the gate gives units again. The wrapped function does not start, so no request goes out. `send` and `poll` give the `QuotaError` as it is. The host keeps its state, because a refusal of the quota is not a failure of the host.
|
|
1346
|
+
- **A refusal of the provider.** While the provider refuses the key, `state().left` is 0. Then `resetAt` is the end of the refusal, or a later time when the window has the units of the call.
|
|
1347
|
+
- **The window.** A `calendar` window is a day or a month in an IANA time zone, from the hour `hour`. It follows the clock changes of the zone. A `rolling` window is the last `ms` milliseconds. When the provider does not tell when its count starts again, use a rolling window.
|
|
1348
|
+
- **The length of a rolling window.** A rolling window counts at least as much as a calendar window only when it is as long as the longest calendar window. In a zone with clock changes, a day can have 25 hours, and a month can have 31 days and 1 hour (745 hours). In UTC, a day always has 24 hours.
|
|
1349
|
+
- **The count is not lower than the count of the provider.** A rolling window puts each call in a time bucket (60 in a window by default). A bucket counts until all of it is out of the window. The answer of the provider (`read`) can make the count larger, but not smaller. A count of 0 left, or a reset time with no count, stops the key until that time.
|
|
1350
|
+
- **A reset header with no count stops the key, also after a 200.** A reset time with no count tells that the provider refuses the key until that time. So the gate stops the key until then, also when the answer was successful. Does the provider send a reset header on each answer? Then name its `remaining` header too, and make sure that the provider sends a number in it. A missing count, or a count that is not a number, stops the key until the reset.
|
|
1351
|
+
- **The forms of a reset header.** `readQuotaHeaders` reads the value of `resetAt` in three forms, and it tells them apart by their size:
|
|
1352
|
+
- A value under 10^9 is the seconds until the reset, as `RateLimit-Reset` of the IETF draft gives them.
|
|
1353
|
+
- A value from 10^9 to under 10^11 is seconds since the epoch.
|
|
1354
|
+
- A value from 10^11 is milliseconds since the epoch.
|
|
1355
|
+
|
|
1356
|
+
The bound 10^9 is 2001-09-09T01:46:40Z in seconds since the epoch. No provider gives a reset before that time, so a smaller value is the seconds until the reset. The bound 10^11 is a time in the year 5138 as seconds, and a time in 1973 as milliseconds. So neither form is near it. The value of `resetAfter` is the seconds until the reset, or the HTTP date of the reset, as `Retry-After` gives them. When the function can read it, it does not read `resetAt`.
|
|
1357
|
+
- **The longest block.** A block ends at the latest one window after the reading, when the units of a call at the reading go free. Thus a reset header in a wrong form cannot stop a key for much longer than the window of the quota. When the provider still refuses the key, it refuses the next call, and the gate reads the new reset. `send` also parks the host for the `Retry-After` of a 429. The option `maxBlockMs` of `quotaGate` sets a different bound, which also ends a longer block of an earlier version in a store.
|
|
1358
|
+
- **The reserve.** The gate keeps `reserve` units unused, for a different program on the same key or for a call that you must make.
|
|
1359
|
+
- **The cost.** A call counts 1 unit, or `cost` units. A cost of 0 counts nothing and refuses nothing. A cost of more than the quota gives in one window is a fault of the caller: the gate throws an `ApiError`.
|
|
1360
|
+
- **The store.** `memoryQuotaStore` keeps the counts in memory. `SqliteHostStateStore` keeps them in its file, so the count of a key stays after a restart and two processes share it. Each change of a count is one transaction. The key names the account that the provider counts. It is not the secret of the key: the store keeps it as text.
|
|
1361
|
+
- **An error after the call.** When `read` or the store fails after the call, the request went out. So `withQuota` gives the result, and it gives the error to `onError`. It does not throw, because a retry after a throw sends the request again. `onError` also gets an error of a give-back of a list, and the caller still gets the refusal. An error that `onError` throws is lost: the caller gets the result or the refusal, and the other gates still give back their units.
|
|
1362
|
+
- **Two quotas on one key.** Give a list of gates, for example `withQuota([perMinute, perDay], fetch)`. The list counts a call in each gate or in none. When a gate refuses, the other gates give back the units of the call, because the call did not start. The `QuotaError` has the latest reset of the gates that refused. `read` gets each gate as its second argument, so it can read the headers of each quota.
|
|
1363
|
+
|
|
1364
|
+
Give each quota a different name. The store keeps one record for each name and key, and a list with two gates of one name and key is an error. Do not put one gate in the function of another. The outer gate cannot know if a request went out before the inner gate refused (`send` retries). So it keeps the units of the call.
|
|
1365
|
+
- **A give-back.** `giveBack(cost, at)` removes `cost` units that the gate counted at the time `at`, from the bucket of that time. The record keeps the time of the last reading of the provider (`readAt`). A give-back of units that the gate counted at that time or earlier changes nothing, because the reading can include them. So a give-back cannot make the count lower than the count of the provider. A gate of your own can be in a list of two or more gates only when it has `giveBack`.
|
|
1366
|
+
- **A refusal from another copy of the package.** `isQuotaError(error)` tells a `QuotaError` from another failure, and `isApiError(error)` tells each error of `polite-crawler/api`. Use them, not `instanceof`. A program with two versions of the package has two `QuotaError` classes, and `instanceof` is false for the class of the other copy. The guards also accept an error of the same name and fields. `send` and `poll` use the guard too.
|
|
1367
|
+
|
|
1368
|
+
These are the limits of the first providers, as their documents gave them on 2026-11-14:
|
|
1369
|
+
|
|
1370
|
+
| Provider | Limit | Window | Headers |
|
|
1371
|
+
| --- | --- | --- | --- |
|
|
1372
|
+
| [Pushover](https://pushover.net/api#limits) | 10,000 messages each month for each user account (25,000 for a team). The applications of an account share it. | Calendar month, `America/Chicago`, from 00:00 on the 1st | `X-Limit-App-Remaining`, `X-Limit-App-Reset` (seconds since the epoch) |
|
|
1373
|
+
| [NASA FIRMS](https://firms.modaps.eosdis.nasa.gov/api/map_key/) | 5,000 transactions for each MAP_KEY | Rolling, 10 minutes | none |
|
|
1374
|
+
| [OpenSky Network](https://openskynetwork.github.io/opensky-api/rest.html) | 400 credits a day with no account, 4,000 for a registered user, 8,000 for an active feeder. `/states`, `/tracks` and `/flights` each have a quota: give each one a key. | Rolling, 25 hours (the document gives no reset time or zone, and a day can have 25 hours) | `X-Rate-Limit-Remaining`, `X-Rate-Limit-Retry-After-Seconds` (seconds until the reset) |
|
|
1375
|
+
| [TomTom](https://docs.tomtom.com/pricing) | 200,000 tile requests and 20,000 other map requests each month, free | Rolling, 745 hours (the document gives no reset time or zone, and a month can have 31 days and 1 hour) | none |
|
|
1376
|
+
|
|
1312
1377
|
## Stored copies and conditional requests
|
|
1313
1378
|
|
|
1314
1379
|
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
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
|
+
import { Classified, Outcome } from "./classify.mjs";
|
|
2
|
+
import { HostCounts, HostResult, HostStateStore, HostStop, Paced, PolicyOptions, PolitenessPolicy, QuotaRecord, QuotaStore, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict } from "./politeness.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* The error of `polite-crawler/api`. It is in a module of its own, so `api.ts` and `quota.ts` import
|
|
5
|
+
* it and do not import each other.
|
|
6
|
+
*/
|
|
1
7
|
/**
|
|
2
|
-
*
|
|
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.
|
|
8
|
+
* An error of the send or of the token. The `redact` function of the send examined its message.
|
|
5
9
|
*
|
|
6
|
-
*
|
|
7
|
-
|
|
10
|
+
* @beta
|
|
11
|
+
*/
|
|
12
|
+
export declare class ApiError extends Error {
|
|
13
|
+
override readonly name: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* This function tells if a value is an error of `polite-crawler/api`: an `ApiError`, or a
|
|
17
|
+
* `QuotaError`, which extends it. The error can come from this copy of the package or from another
|
|
18
|
+
* copy, of this version or of an earlier one. `instanceof` is false across two copies, so the
|
|
19
|
+
* function also accepts an `Error` with one of their names.
|
|
8
20
|
*
|
|
9
|
-
*
|
|
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.
|
|
21
|
+
* @beta
|
|
13
22
|
*/
|
|
14
|
-
|
|
15
|
-
import { HostCounts, HostResult, HostStateStore, HostStop, Paced, PolicyOptions, PolitenessPolicy, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict } from "./politeness.mjs";
|
|
23
|
+
export declare const isApiError: (value: unknown) => value is ApiError;
|
|
16
24
|
/**
|
|
17
25
|
* These are the facts of one answer that set the reaction of its host. `classify` gives the
|
|
18
26
|
* outcome and the wait that the server asked for.
|
|
@@ -92,6 +100,258 @@ export declare const outcomeResult: (facts: OutcomeFacts) => HostResult;
|
|
|
92
100
|
* @beta
|
|
93
101
|
*/
|
|
94
102
|
export declare const reactToOutcome: (facts: OutcomeFacts, counts: HostCounts, context: ReactionContext) => HostReaction;
|
|
103
|
+
/**
|
|
104
|
+
* The window of a quota.
|
|
105
|
+
*
|
|
106
|
+
* - `rolling`: the units of the last `ms` milliseconds. Use it when the provider does not tell when
|
|
107
|
+
* its count starts again. It is stricter than each fixed window of the same length or shorter.
|
|
108
|
+
* - `calendar`: the units of a day or a month in the IANA time zone `timeZone`. A window starts at
|
|
109
|
+
* the hour `hour` (default 0) of the first day, in that zone. When the clock jumps over that hour,
|
|
110
|
+
* the window starts at the jump.
|
|
111
|
+
*
|
|
112
|
+
* @beta
|
|
113
|
+
*/
|
|
114
|
+
type QuotaWindow = {
|
|
115
|
+
readonly kind: "rolling";
|
|
116
|
+
readonly ms: number;
|
|
117
|
+
/** The window has this count of time buckets. The default is 60. Each call counts until all of its bucket is out of the window. */
|
|
118
|
+
readonly buckets?: number | undefined;
|
|
119
|
+
} | {
|
|
120
|
+
readonly kind: "calendar";
|
|
121
|
+
readonly unit: "day" | "month";
|
|
122
|
+
/** This value is an IANA time zone, for example `America/Chicago` or `UTC`. */
|
|
123
|
+
readonly timeZone: string;
|
|
124
|
+
/** This value is the hour of the start of a window, from 0 to 23. The default is 0. */
|
|
125
|
+
readonly hour?: number | undefined;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* A quota of one API key: at most `limit` units in each window.
|
|
129
|
+
*
|
|
130
|
+
* @beta
|
|
131
|
+
*/
|
|
132
|
+
interface Quota {
|
|
133
|
+
/**
|
|
134
|
+
* This value names the quota in a message, for example `Example API requests`. A store keeps a
|
|
135
|
+
* record for each name and key, so give each quota of a key a different name.
|
|
136
|
+
*/
|
|
137
|
+
readonly name: string;
|
|
138
|
+
/** This value is the count of units that the provider gives in one window. */
|
|
139
|
+
readonly limit: number;
|
|
140
|
+
/** The gate keeps this count of units unused. The default is 0. */
|
|
141
|
+
readonly reserve?: number | undefined;
|
|
142
|
+
readonly window: QuotaWindow;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* What an answer of the provider tells of its count.
|
|
146
|
+
*
|
|
147
|
+
* @beta
|
|
148
|
+
*/
|
|
149
|
+
interface QuotaReading {
|
|
150
|
+
/** This value is the count of units that the provider gives before its next reset. */
|
|
151
|
+
readonly remaining?: number | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* This value is the time of the next reset of the provider, in milliseconds since the epoch. When
|
|
154
|
+
* `remaining` is 0 or is not there, the gate refuses each call until this time.
|
|
155
|
+
*/
|
|
156
|
+
readonly resetAt?: number | undefined;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The state of a quota at one time.
|
|
160
|
+
*
|
|
161
|
+
* @beta
|
|
162
|
+
*/
|
|
163
|
+
interface QuotaState {
|
|
164
|
+
/** This value is the count of units in the window. */
|
|
165
|
+
readonly used: number;
|
|
166
|
+
/**
|
|
167
|
+
* This value is the count of units that the gate gives: the limit, less the reserve and `used`.
|
|
168
|
+
* It is 0 while the provider refuses the key.
|
|
169
|
+
*/
|
|
170
|
+
readonly left: number;
|
|
171
|
+
/** The units come back from this time, in milliseconds since the epoch. With no unit in the window, it is not there. */
|
|
172
|
+
readonly resetAt?: number | undefined;
|
|
173
|
+
/** The provider refused the key until this time, in milliseconds since the epoch. */
|
|
174
|
+
readonly blockedUntil?: number | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* This value is the time of the state, in milliseconds since the epoch. For the state that `take`
|
|
177
|
+
* gives, it is the time of the units of the call, which `giveBack` needs.
|
|
178
|
+
*/
|
|
179
|
+
readonly at?: number | undefined;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The refusal of a call over its quota. The gate refused the call before the call started.
|
|
183
|
+
*
|
|
184
|
+
* @beta
|
|
185
|
+
*/
|
|
186
|
+
export declare class QuotaError extends ApiError {
|
|
187
|
+
override readonly name: string;
|
|
188
|
+
/** This value is the key of the quota. */
|
|
189
|
+
readonly key: string;
|
|
190
|
+
/** The gate gives units again from this time, in milliseconds since the epoch. */
|
|
191
|
+
readonly resetAt: number;
|
|
192
|
+
constructor(message: string, key: string, resetAt: number);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* This function tells if a value is a `QuotaError`. The error can come from this copy of the package
|
|
196
|
+
* or from another copy, of this version or of an earlier one. `instanceof` is false across two copies.
|
|
197
|
+
* So the function also accepts an `Error` named `QuotaError`, with a string `key` and a finite number
|
|
198
|
+
* `resetAt`. A package that gets a transport of `withQuota` uses it to tell a refusal of the quota from
|
|
199
|
+
* another failure. That package needs no dependency on this package.
|
|
200
|
+
*
|
|
201
|
+
* @beta
|
|
202
|
+
*/
|
|
203
|
+
export declare const isQuotaError: (value: unknown) => value is QuotaError;
|
|
204
|
+
/**
|
|
205
|
+
* The gate of one key. It counts the units of the calls, and it refuses a call over the quota.
|
|
206
|
+
*
|
|
207
|
+
* @beta
|
|
208
|
+
*/
|
|
209
|
+
interface QuotaGate {
|
|
210
|
+
readonly quota: Quota;
|
|
211
|
+
readonly key: string;
|
|
212
|
+
/**
|
|
213
|
+
* This method counts `cost` units (default 1) for a call that starts at this time, and gives the
|
|
214
|
+
* state. A cost of 0 counts nothing and refuses nothing.
|
|
215
|
+
*
|
|
216
|
+
* @throws QuotaError when the units are more than `left`, or when the provider refused the key.
|
|
217
|
+
* The gate then counts nothing. Its `resetAt` is the end of the refusal of the provider, or a later
|
|
218
|
+
* time when the window has the units of the call.
|
|
219
|
+
* @throws ApiError when the cost is negative or not finite, or when it is more than the quota
|
|
220
|
+
* gives in one window.
|
|
221
|
+
*/
|
|
222
|
+
take(cost?: number): QuotaState;
|
|
223
|
+
/** This method adds what an answer of the provider tells. The count can only become larger. */
|
|
224
|
+
note(reading: QuotaReading): QuotaState;
|
|
225
|
+
/** This method gives the state at this time, and changes nothing. */
|
|
226
|
+
state(): QuotaState;
|
|
227
|
+
/**
|
|
228
|
+
* This method gives back `cost` units that `take` counted at the time `at` (the `at` of its
|
|
229
|
+
* state), for a call that did not start. `withQuota` uses it for a list of gates. When the gate
|
|
230
|
+
* read the count of the provider at the time `at` or later, the method changes nothing. That
|
|
231
|
+
* reading can include the units. So the count does not become lower than the count of the provider
|
|
232
|
+
* at any time. A gate made by `quotaGate` has this method.
|
|
233
|
+
*
|
|
234
|
+
* @throws ApiError when the cost is negative or not finite, or when `at` is not a time.
|
|
235
|
+
*/
|
|
236
|
+
giveBack?(cost: number, at: number): QuotaState;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The options of `quotaGate`.
|
|
240
|
+
*
|
|
241
|
+
* @beta
|
|
242
|
+
*/
|
|
243
|
+
interface QuotaGateOptions {
|
|
244
|
+
/**
|
|
245
|
+
* This value names the key, for example `example:account-1`. A store keeps it as text, so do not
|
|
246
|
+
* give the secret of the key. Give one name to each account that the provider counts.
|
|
247
|
+
*/
|
|
248
|
+
readonly key: string;
|
|
249
|
+
/** This store keeps the records. The default is a new `memoryQuotaStore`. */
|
|
250
|
+
readonly store?: QuotaStore | undefined;
|
|
251
|
+
/** This function gives the time in milliseconds since the epoch. The default is `Date.now`. */
|
|
252
|
+
readonly now?: (() => number) | undefined;
|
|
253
|
+
/**
|
|
254
|
+
* This value is the longest block that a reset of the provider can set, in milliseconds. A reading
|
|
255
|
+
* with a later reset blocks the key until the time of the reading plus this value. The default is
|
|
256
|
+
* one window. Then the block ends at the latest when the units of a call at the time of the
|
|
257
|
+
* reading go free. Thus a reset header in a wrong form cannot stop a key for much longer than the
|
|
258
|
+
* window of the quota.
|
|
259
|
+
*/
|
|
260
|
+
readonly maxBlockMs?: number | undefined;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* This function makes the gate of one key.
|
|
264
|
+
*
|
|
265
|
+
* @throws ApiError when the quota or its window is not valid.
|
|
266
|
+
* @beta
|
|
267
|
+
*/
|
|
268
|
+
export declare function quotaGate(quota: Quota, options: QuotaGateOptions): QuotaGate;
|
|
269
|
+
/**
|
|
270
|
+
* This function makes a store of records in memory. Each gate of one process can share it.
|
|
271
|
+
*
|
|
272
|
+
* @beta
|
|
273
|
+
*/
|
|
274
|
+
export declare function memoryQuotaStore(): QuotaStore;
|
|
275
|
+
/**
|
|
276
|
+
* The options of `withQuota`.
|
|
277
|
+
*
|
|
278
|
+
* @beta
|
|
279
|
+
*/
|
|
280
|
+
interface WithQuotaOptions<A extends readonly unknown[], R> {
|
|
281
|
+
/**
|
|
282
|
+
* This value is the count of units of a call, or a function of the arguments that gives it. The
|
|
283
|
+
* default is 1. Give 0 for a call that the provider does not count: the gate then does not refuse it.
|
|
284
|
+
*/
|
|
285
|
+
readonly cost?: number | ((...args: A) => number) | undefined;
|
|
286
|
+
/**
|
|
287
|
+
* This function reads what the result of a call tells of the count of the provider, for example
|
|
288
|
+
* with `readQuotaHeaders`. With a list of gates, it reads once for each gate. A provider often
|
|
289
|
+
* tells the count of one quota only: then give its reading for that gate, and undefined for the
|
|
290
|
+
* other gates.
|
|
291
|
+
*/
|
|
292
|
+
readonly read?: ((result: R, gate: QuotaGate) => QuotaReading | undefined) | undefined;
|
|
293
|
+
/**
|
|
294
|
+
* This function gets an error of `read` or of the store after the call. The call went out, so
|
|
295
|
+
* `withQuota` gives its result. It does not throw, because a retry after a throw sends the call
|
|
296
|
+
* again. It also gets an error of a give-back of a list, and the caller still gets the refusal.
|
|
297
|
+
* With no function, the error is lost. An error that this function throws is lost too.
|
|
298
|
+
*/
|
|
299
|
+
readonly onError?: ((error: unknown) => void) | undefined;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* This function puts a gate, or a list of gates, on an async function, and keeps the type of the
|
|
303
|
+
* function. Each call counts its units first. A call over the quota throws `QuotaError`, and the
|
|
304
|
+
* function does not start.
|
|
305
|
+
*
|
|
306
|
+
* Give the result to a client that takes a function of this type. For example, `withQuota(gate,
|
|
307
|
+
* fetch)` is a `fetch`, and `withQuota(gate, transport)` is a transport of `send` and `poll`.
|
|
308
|
+
*
|
|
309
|
+
* For two quotas of one key, give a list: `withQuota([perMinute, perDay], fetch)`. The list counts
|
|
310
|
+
* the call in each gate or in none. Do not put one gate in the function of another. The outer gate
|
|
311
|
+
* cannot know if a request went out before the inner gate refused (`send` retries). So it keeps
|
|
312
|
+
* the units of the call.
|
|
313
|
+
*
|
|
314
|
+
* @throws ApiError when the list is empty, or when a gate of a list of two or more has no
|
|
315
|
+
* `giveBack`. Also when two gates of the list have one quota name and key.
|
|
316
|
+
* @beta
|
|
317
|
+
*/
|
|
318
|
+
export declare function withQuota<A extends readonly unknown[], R>(gate: QuotaGate | readonly QuotaGate[], call: (...args: A) => Promise<R>, options?: WithQuotaOptions<A, R>): (...args: A) => Promise<R>;
|
|
319
|
+
/**
|
|
320
|
+
* The names of the headers of an answer that tell the count of a provider. Each name is optional.
|
|
321
|
+
*
|
|
322
|
+
* @beta
|
|
323
|
+
*/
|
|
324
|
+
interface QuotaHeaders {
|
|
325
|
+
/** This header has the count of units that the provider gives before its reset, for example `x-ratelimit-remaining`. */
|
|
326
|
+
readonly remaining?: string | undefined;
|
|
327
|
+
/**
|
|
328
|
+
* This header has the time of the reset, for example `x-ratelimit-reset`. The function reads its
|
|
329
|
+
* form from its size. A value under 10^9 is the seconds until the reset, as `RateLimit-Reset` of
|
|
330
|
+
* the IETF draft gives them. A value from 10^9 to under 10^11 is seconds since the epoch. A value
|
|
331
|
+
* from 10^11 is milliseconds since the epoch.
|
|
332
|
+
*/
|
|
333
|
+
readonly resetAt?: string | undefined;
|
|
334
|
+
/**
|
|
335
|
+
* This header has the seconds until the reset, or the HTTP date of the reset, as `retry-after`
|
|
336
|
+
* gives them. When it has a value that the function can read, the function does not read `resetAt`.
|
|
337
|
+
*/
|
|
338
|
+
readonly resetAfter?: string | undefined;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* This function reads the count of a provider from the headers of an answer. `get` gives the value
|
|
342
|
+
* of one header, for example `(name) => response.headers.get(name)` for a `fetch` answer, or
|
|
343
|
+
* `(name) => reply.headers[name]` for a `Reply`. A value that is not a number is not read. A value of
|
|
344
|
+
* `resetAfter` can also be an HTTP date, as in `Retry-After`. The value of `resetAt` can be in each
|
|
345
|
+
* of three forms, which the function tells apart by their size.
|
|
346
|
+
*
|
|
347
|
+
* A reset with no count tells that the provider refuses the key until that time. The answer can
|
|
348
|
+
* have a reset header, and no `remaining` header with a number. Then the gate stops the key until
|
|
349
|
+
* the reset, also after a successful answer.
|
|
350
|
+
*
|
|
351
|
+
* @returns the reading, or undefined when no header has a value.
|
|
352
|
+
* @beta
|
|
353
|
+
*/
|
|
354
|
+
export declare function readQuotaHeaders(get: (name: string) => string | null | undefined, names: QuotaHeaders, now: number): QuotaReading | undefined;
|
|
95
355
|
/**
|
|
96
356
|
* One request of a client to an API.
|
|
97
357
|
*
|
|
@@ -170,14 +430,6 @@ export interface Pace<S extends Signal = AbortSignal> {
|
|
|
170
430
|
* @beta
|
|
171
431
|
*/
|
|
172
432
|
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
433
|
/**
|
|
182
434
|
* One exchange of a client, and its context for the pace and the messages.
|
|
183
435
|
*
|
|
@@ -226,10 +478,12 @@ export declare function statusLine(reply: Reply): string;
|
|
|
226
478
|
* - A 429, 502, 503 or 504 parks the host for its `Retry-After`, or for a backoff with jitter.
|
|
227
479
|
* - A 401 or a 403 stops the host for 1 hour. A 402 and a bot challenge stop it for 24 hours.
|
|
228
480
|
* - An error 5xx and an exchange with no answer are failures. After 5 failures in sequence, the host stops.
|
|
481
|
+
* - A `QuotaError` of the transport (`withQuota`) does not change the host.
|
|
229
482
|
*
|
|
230
483
|
* The function gives each answer, with each status. The caller reads the status.
|
|
231
484
|
*
|
|
232
485
|
* @throws ApiError when the API gives no answer, or when the host is stopped. `redact` examined the message.
|
|
486
|
+
* @throws QuotaError of the transport, as it is, when a quota refuses the exchange.
|
|
233
487
|
* @throws the reason of the signal, when the signal stops the exchange.
|
|
234
488
|
* @beta
|
|
235
489
|
*/
|
|
@@ -393,4 +647,4 @@ export interface Polled extends Answer {
|
|
|
393
647
|
* @beta
|
|
394
648
|
*/
|
|
395
649
|
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 };
|
|
650
|
+
export type { Classified, HostCounts, HostReaction, HostResult, HostStateStore, HostStop, Outcome, OutcomeFacts, Paced, PolicyOptions, PolitenessPolicy, Quota, QuotaGate, QuotaGateOptions, QuotaHeaders, QuotaReading, QuotaRecord, QuotaState, QuotaStore, QuotaWindow, ReactionContext, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict, WithQuotaOptions };
|