polite-crawler 0.7.8 → 0.7.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +43 -0
  2. package/build/api.d.mts +227 -1
  3. package/build/api.mjs +424 -3
  4. package/build/api.mjs.map +1 -1
  5. package/build/{cli-daemon-f2xHraCa.mjs → cli-daemon-11H9DrcR.mjs} +2 -2
  6. package/build/{cli-daemon-f2xHraCa.mjs.map → cli-daemon-11H9DrcR.mjs.map} +1 -1
  7. package/build/{cli-recipes-ZFyc0wTa.mjs → cli-recipes-CELsPlba.mjs} +2 -2
  8. package/build/{cli-recipes-ZFyc0wTa.mjs.map → cli-recipes-CELsPlba.mjs.map} +1 -1
  9. package/build/cli.mjs +7 -7
  10. package/build/{daemon-CqQmcYh9.mjs → daemon-DazTlNoE.mjs} +3 -3
  11. package/build/{daemon-CqQmcYh9.mjs.map → daemon-DazTlNoE.mjs.map} +1 -1
  12. package/build/{diff-BwrS5qLG.mjs → diff-BFVYtIvz.mjs} +2 -2
  13. package/build/{diff-BwrS5qLG.mjs.map → diff-BFVYtIvz.mjs.map} +1 -1
  14. package/build/{extract-Dt-zWC9M.mjs → extract-CI-8rcx5.mjs} +2 -2
  15. package/build/{extract-Dt-zWC9M.mjs.map → extract-CI-8rcx5.mjs.map} +1 -1
  16. package/build/index.d.mts +1 -1
  17. package/build/index.mjs +10 -10
  18. package/build/{open-D2aPACL_.mjs → open-CQO3gf6C.mjs} +6 -6
  19. package/build/{open-D2aPACL_.mjs.map → open-CQO3gf6C.mjs.map} +1 -1
  20. package/build/{recipe-BZCG1pOy.mjs → recipe-C5iXikn3.mjs} +3 -3
  21. package/build/{recipe-BZCG1pOy.mjs.map → recipe-C5iXikn3.mjs.map} +1 -1
  22. package/build/{recipe-product-gd-6XJ4C.mjs → recipe-product-BuejBcxL.mjs} +4 -4
  23. package/build/{recipe-product-gd-6XJ4C.mjs.map → recipe-product-BuejBcxL.mjs.map} +1 -1
  24. package/build/{runtime-CD32YZv5.mjs → runtime-CIuB8bcr.mjs} +2 -2
  25. package/build/{runtime-CD32YZv5.mjs.map → runtime-CIuB8bcr.mjs.map} +1 -1
  26. package/build/{version-ACvb6zmT.mjs → version-BGzjMFfh.mjs} +2 -2
  27. package/build/{version-ACvb6zmT.mjs.map → version-BGzjMFfh.mjs.map} +1 -1
  28. package/build/{wacz-C2egGCSb.mjs → wacz-BQ_TbYhu.mjs} +3 -3
  29. package/build/{wacz-C2egGCSb.mjs.map → wacz-BQ_TbYhu.mjs.map} +1 -1
  30. package/build/{warc-DshKROY0.mjs → warc-dri2CGIB.mjs} +2 -2
  31. package/build/{warc-DshKROY0.mjs.map → warc-dri2CGIB.mjs.map} +1 -1
  32. package/package.json +1 -1
package/README.md CHANGED
@@ -1312,6 +1312,49 @@ if (polled.changed) {
1312
1312
  - **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.
1313
1313
  - **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.
1314
1314
 
1315
+ ### Keep the answers of an API
1316
+
1317
+ `poll` tells only if the data changed. A client that needs the data at each request uses `keptFetch`. It keeps the last full answer of each GET with its body. The next GET of the same URL sends the validators. A 304 then gives the kept answer: the status, the body and the kept headers, with the new fields of the 304. Some APIs do not count a 304 in the rate limit, for example the REST API of GitHub. A run that reads the same files again then counts fewer requests.
1318
+
1319
+ ```ts
1320
+ import { fetchTransport, keptFetch, keptStore, readKept } from "polite-crawler/api";
1321
+
1322
+ // The entries of the last run, for example from a file of the cache of a CI job
1323
+ const store = keptStore(readKept(JSON.parse(lastRun)));
1324
+ const transport = fetchTransport({
1325
+ userAgent: "example-app/1.0 (+https://example.com/app)",
1326
+ fetch: keptFetch((url, init) => fetch(url, init), store),
1327
+ });
1328
+ // ... the run sends its requests through the transport ...
1329
+ const nextRun = JSON.stringify(store.used());
1330
+ ```
1331
+
1332
+ - **The server decides.** A kept answer goes to the client only after a 304 of the server. The key of an answer is its URL and its `accept` header. The server examines the access of each request, so a 304 comes only for a request that can read the answer.
1333
+ - **A store keeps text only.** It keeps an answer 200, 203 or 204 with an `ETag` or a `Last-Modified` date. The answer needs a text, JSON or XML type in UTF-8, and a body of at most `KEPT_BODY_MAX` UTF-16 code units. Each other answer goes to the client as it is. A binary body is not read, so an exchange with `read: "bytes"` gets its bytes.
1334
+ - **Your request stays yours.** A request with its own `If-None-Match`, `If-Modified-Since`, other `If-` header or `Range` goes to the server as it is. So does each request that is not a GET with no body.
1335
+ - **The store stays small.** `used()` gives only the entries that the run read or wrote. Save these, and an answer that no run asks for again goes away. `readKept` drops each entry with a bad shape, so a damaged file gives fewer kept answers and no error.
1336
+ - **A file of answers holds data.** A file of the cache of a public repository can be read by others. Seal the file when the answers hold private data, as the next section tells.
1337
+
1338
+ #### Seal a store
1339
+
1340
+ `sealKept` encrypts the entries of a store, and `openKept` gives them back. Use them for a place that others can read, for example the cache of a public repository in GitHub Actions.
1341
+
1342
+ ```ts
1343
+ import { keptStore, openKept, sealKept } from "polite-crawler/api";
1344
+
1345
+ // The secret is a random key of 16 bytes or more, for example a secret of the repository
1346
+ const opened = await openKept(sealedBytes, process.env.KEPT_KEY!, { label: "example-app" });
1347
+ const store = keptStore(opened.entries);
1348
+ // ... the run sends its requests ...
1349
+ const nextSeal = await sealKept(store.used(), process.env.KEPT_KEY!, { label: "example-app" });
1350
+ ```
1351
+
1352
+ - **The cipher.** AES-256-GCM. The key comes from the secret by HKDF-SHA-256, with a new random salt for each seal and the label as the info. Two labels give two keys from one secret. The format name `KEPT_SEAL_FORMAT` is the first line of the bytes and the associated data of the cipher.
1353
+ - **The secret must be a key.** HKDF does not slow a guess, so give a random key, not a password. A secret of fewer than `KEPT_SEAL_SECRET_MIN` bytes (16) throws a `RangeError`.
1354
+ - **A bad seal gives an empty store.** Another format, another secret, another label or damaged bytes give no entries and a `note`, and no error. The client then goes on with an empty store, and the server sends full answers.
1355
+ - **One compression for each entry.** Each entry is compressed alone before the encryption. So the size of the seal does not tell how the text of one answer matches the text of another answer.
1356
+ - **Web APIs only.** The functions use `crypto.subtle` and `CompressionStream`, so they run in Node.js and in a browser.
1357
+
1315
1358
  ### Quotas of a key
1316
1359
 
1317
1360
  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.
package/build/api.d.mts CHANGED
@@ -1,5 +1,165 @@
1
1
  import { Classified, Outcome } from "./classify.mjs";
2
2
  import { HostCounts, HostResult, HostStateStore, HostStop, Paced, PolicyOptions, PolitenessPolicy, QuotaRecord, QuotaStore, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict } from "./politeness.mjs";
3
+ /**
4
+ * Kept answers. A client keeps the last full answer of a GET with its validators. The next GET of
5
+ * the same key is then a conditional request (RFC 9110 §13.1). An answer 304 then gives the kept
6
+ * answer with the fields of the 304 (RFC 9111 §4.3.4). Some APIs with a rate limit do not count a
7
+ * 304, for example the REST API of GitHub. The store is data, so a client can keep it between two
8
+ * runs. The server examines each validator, so a kept answer goes to the client only after the
9
+ * server tells that it is current. The module imports no Node.js module.
10
+ */
11
+ /**
12
+ * The kept answer of one key.
13
+ *
14
+ * @beta
15
+ */
16
+ interface Kept {
17
+ /** This value is the `ETag` header of the answer, when it had one. */
18
+ readonly etag?: string | undefined;
19
+ /** This value is the `Last-Modified` header of the answer, when it had one. */
20
+ readonly lastModified?: string | undefined;
21
+ /** This value is the status of the answer: 200, 203 or 204 (`KEPT_STATUSES`). */
22
+ readonly status: number;
23
+ /** These values are the headers that the store keeps, by name in lowercase. */
24
+ readonly headers: Readonly<Record<string, string>>;
25
+ /** This value is the body of the answer, as text. */
26
+ readonly body: string;
27
+ }
28
+ /**
29
+ * A store of kept answers, by key. A `Map` is a store in memory. `keptStore` also records the
30
+ * entries that a run uses.
31
+ *
32
+ * @beta
33
+ */
34
+ interface KeptStore {
35
+ /** This function gives the kept answer of the key, or undefined when the store has none. */
36
+ get(key: string): Kept | undefined;
37
+ /** This function keeps the answer under the key, in place of an earlier one. */
38
+ set(key: string, kept: Kept): void;
39
+ }
40
+ /**
41
+ * An answer as a client reads it: its status, its headers by name in lowercase, and its body.
42
+ *
43
+ * @beta
44
+ */
45
+ interface KeptAnswer {
46
+ /** The status of the answer. After a 304, it is the status of the kept answer. */
47
+ readonly status: number;
48
+ /** The headers of the answer, by name in lowercase. */
49
+ readonly headers: Readonly<Record<string, string>>;
50
+ /** The body of the answer as text. After a 304, it is the kept body. */
51
+ readonly body: string;
52
+ /** This value is true when the server answered 304 and the answer is the kept answer. */
53
+ readonly notModified: boolean;
54
+ }
55
+ /**
56
+ * The options of `keepAnswer`.
57
+ *
58
+ * @beta
59
+ */
60
+ interface KeepOptions {
61
+ /** These names are the headers that a kept answer keeps. The default is `KEPT_HEADERS`. */
62
+ readonly headers?: readonly string[] | undefined;
63
+ /**
64
+ * This value is the largest body that the store keeps, in UTF-16 code units. The default is
65
+ * `KEPT_BODY_MAX`.
66
+ */
67
+ readonly maxBody?: number | undefined;
68
+ }
69
+ /**
70
+ * The headers that a kept answer keeps when the options give no list. `link` keeps the next page
71
+ * of a list.
72
+ *
73
+ * @beta
74
+ */
75
+ export declare const KEPT_HEADERS: readonly string[];
76
+ /**
77
+ * The largest body that a store keeps when the options give no limit: 1,048,576 UTF-16 code units.
78
+ *
79
+ * @beta
80
+ */
81
+ export declare const KEPT_BODY_MAX: number;
82
+ /**
83
+ * The statuses that a store keeps. The body of each is the full answer. A 206 has only a part of
84
+ * the body, so a store does not keep it.
85
+ *
86
+ * @beta
87
+ */
88
+ export declare const KEPT_STATUSES: readonly number[];
89
+ /**
90
+ * This function tells if the headers of a request make it conditional or partial on their own:
91
+ * `If-None-Match`, `If-Modified-Since`, `If-Match`, `If-Unmodified-Since`, `If-Range` or
92
+ * `Range`. The client of such a request reads the answer itself, so a store does not answer for
93
+ * it.
94
+ *
95
+ * @beta
96
+ */
97
+ export declare function hasValidator(headers: Readonly<Record<string, string>>): boolean;
98
+ /**
99
+ * This function tells if a body of a media type is text in UTF-8. That is a text, JSON or XML type
100
+ * with no charset or with the charset UTF-8. A store keeps only such a body, because `text()` of
101
+ * `fetch` reads a body as UTF-8.
102
+ *
103
+ * @beta
104
+ */
105
+ export declare function isTextType(contentType: string | undefined): boolean;
106
+ /**
107
+ * This function gives the key of a request in a store: its `accept` header and its URL. A
108
+ * different `accept` header can give a different body, so it gets a different key.
109
+ *
110
+ * @beta
111
+ */
112
+ export declare function keptKey(url: string, headers: Readonly<Record<string, string>>): string;
113
+ /**
114
+ * This function gives the headers of a conditional request: the headers of the request, and the
115
+ * validators of the kept answer. `If-None-Match` sends its ETag, and `If-Modified-Since` sends its
116
+ * date. The function adds no header to a request that has a validator of its own.
117
+ *
118
+ * @beta
119
+ */
120
+ export declare function keptRequestHeaders(kept: Kept | undefined, headers: Readonly<Record<string, string>>): Record<string, string>;
121
+ /**
122
+ * This function gives the answer that a client reads, and changes the store.
123
+ *
124
+ * - A 304 for a key with a kept answer gives the kept answer. Its headers get the fields of the
125
+ * 304, as RFC 9111 §4.3.4 tells, and the store keeps the new values of its kept headers.
126
+ * - An answer with a status of `KEPT_STATUSES` and an ETag or a date goes into the store. It needs
127
+ * a text type in UTF-8 and a body in the limit.
128
+ * - Each other answer is as it is, and the store does not change.
129
+ *
130
+ * @beta
131
+ */
132
+ export declare function keepAnswer(store: KeptStore, key: string, answer: {
133
+ readonly status: number;
134
+ readonly headers: Readonly<Record<string, string>>;
135
+ readonly body: string;
136
+ }, options?: KeepOptions): KeptAnswer;
137
+ /**
138
+ * A store of kept answers in memory that records which entries a run uses.
139
+ *
140
+ * @beta
141
+ */
142
+ interface KeptMemory extends KeptStore {
143
+ /**
144
+ * This function gives each entry that a `get` found or a `set` wrote, in the order of the first
145
+ * use. A client that saves only these entries keeps no answer that it does not ask for again.
146
+ */
147
+ used(): [string, Kept][];
148
+ }
149
+ /**
150
+ * This function makes a store of kept answers in memory, with the entries of an earlier run.
151
+ *
152
+ * @beta
153
+ */
154
+ export declare function keptStore(entries?: Iterable<readonly [string, Kept]>): KeptMemory;
155
+ /**
156
+ * This function reads the entries of a store from data, for example from the JSON of the `used`
157
+ * entries of an earlier run. It keeps each entry with a correct shape and drops each other entry.
158
+ * Thus damaged data gives fewer kept answers and no error.
159
+ *
160
+ * @beta
161
+ */
162
+ export declare function readKept(data: unknown): [string, Kept][];
3
163
  /**
4
164
  * The error of `polite-crawler/api`. It is in a module of its own, so `api.ts` and `quota.ts` import
5
165
  * it and do not import each other.
@@ -49,6 +209,60 @@ export declare const isApiError: (value: unknown) => value is ApiError;
49
209
  * @beta
50
210
  */
51
211
  export declare function freshUntil(header: (name: string) => string | undefined, status: number, fetchedAt: number, lastModified: string | undefined, capMs: number): number | undefined;
212
+ /**
213
+ * The name and the version of the sealed format. It is the first line of the sealed bytes.
214
+ *
215
+ * @beta
216
+ */
217
+ export declare const KEPT_SEAL_FORMAT = "polite-crawler-kept-sealed/1";
218
+ /**
219
+ * The smallest secret that `sealKept` and `openKept` take, in bytes. HKDF does not slow a guess, so
220
+ * the secret must be a random key, not a password.
221
+ *
222
+ * @beta
223
+ */
224
+ export declare const KEPT_SEAL_SECRET_MIN = 16;
225
+ /**
226
+ * The options of `sealKept` and `openKept`.
227
+ *
228
+ * @beta
229
+ */
230
+ interface SealOptions {
231
+ /**
232
+ * This value is the label of the use, for example the name of the client. A store opens only with
233
+ * the label that sealed it. Give a label when the secret has a second use. The default is "".
234
+ */
235
+ readonly label?: string | undefined;
236
+ }
237
+ /**
238
+ * The result of `openKept`.
239
+ *
240
+ * @beta
241
+ */
242
+ interface OpenedKept {
243
+ /** These values are the entries of the store. They are empty when the store does not open. */
244
+ readonly entries: [string, Kept][];
245
+ /** This value tells why the store gave fewer entries or none, when that occurs. */
246
+ readonly note?: string | undefined;
247
+ }
248
+ /**
249
+ * This function seals the entries of a store of kept answers, for example the `used` entries of a
250
+ * run. It compresses each entry alone, and it encrypts all of them with a key from the secret. It
251
+ * throws a `RangeError` when the secret has fewer than `KEPT_SEAL_SECRET_MIN` bytes.
252
+ *
253
+ * @beta
254
+ */
255
+ export declare function sealKept(entries: Iterable<readonly [string, Kept]>, secret: string | Uint8Array, options?: SealOptions): Promise<Uint8Array<ArrayBuffer>>;
256
+ /**
257
+ * This function opens a store that `sealKept` sealed, and gives its entries. Data that does not open
258
+ * (another format, another secret, another label, damaged bytes) gives no entries and a note, and no
259
+ * error, so a client goes on with an empty store. It throws a `RangeError` when the secret has fewer
260
+ * than `KEPT_SEAL_SECRET_MIN` bytes, as `sealKept` does. As `readKept` does, it drops each entry that
261
+ * does not have the shape of a kept answer.
262
+ *
263
+ * @beta
264
+ */
265
+ export declare function openKept(sealed: Uint8Array, secret: string | Uint8Array, options?: SealOptions): Promise<OpenedKept>;
52
266
  /**
53
267
  * These are the facts of one answer that set the reaction of its host. `classify` gives the
54
268
  * outcome and the wait that the server asked for.
@@ -641,6 +855,18 @@ export interface FetchTransportOptions {
641
855
  * @beta
642
856
  */
643
857
  export declare function fetchTransport(options: FetchTransportOptions): Transport;
858
+ /**
859
+ * This function makes a `fetch` with kept answers, for example for `fetchTransport`. A GET with no
860
+ * body and no conditional or range header of its own (`hasValidator`) sends the validators of its
861
+ * kept answer. The key of the answer is `keptKey`. A 304 then gives the kept answer, as
862
+ * `keepAnswer` tells, and a full answer with a text body in UTF-8 goes into the store. The function does not read a body
863
+ * of a different media type, so an exchange that reads `bytes` gets the bytes. Each other request
864
+ * goes to `fetch` as it is. The key does not hold the `authorization` header: the server examines
865
+ * the access of each request before it answers 304.
866
+ *
867
+ * @beta
868
+ */
869
+ export declare function keptFetch(fetch: FetchLike, store: KeptStore, options?: KeepOptions): FetchLike;
644
870
  /**
645
871
  * What a poll keeps of the last answer 2xx of one URL.
646
872
  *
@@ -690,4 +916,4 @@ export interface Polled extends Answer {
690
916
  * @beta
691
917
  */
692
918
  export declare function poll<S extends Signal = AbortSignal>(pace: Pace<S>, transport: Transport<S>, conditions: Conditions, input: Send, signal?: S): Promise<Polled>;
693
- 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 };
919
+ export type { Classified, HostCounts, HostReaction, HostResult, HostStateStore, HostStop, KeepOptions, Kept, KeptAnswer, KeptMemory, KeptStore, OpenedKept, Outcome, OutcomeFacts, Paced, PolicyOptions, PolitenessPolicy, Quota, QuotaGate, QuotaGateOptions, QuotaHeaders, QuotaReading, QuotaRecord, QuotaState, QuotaStore, QuotaWindow, ReactionContext, SealOptions, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict, WithQuotaOptions };