polite-crawler 0.7.10 → 0.7.12

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 (33) hide show
  1. package/README.md +36 -0
  2. package/build/api.d.mts +32 -1
  3. package/build/api.mjs +47 -1
  4. package/build/api.mjs.map +1 -1
  5. package/build/{cli-daemon-11H9DrcR.mjs → cli-daemon-CfJaQ_vG.mjs} +2 -2
  6. package/build/{cli-daemon-11H9DrcR.mjs.map → cli-daemon-CfJaQ_vG.mjs.map} +1 -1
  7. package/build/{cli-recipes-CELsPlba.mjs → cli-recipes-Bp_LMzOR.mjs} +2 -2
  8. package/build/{cli-recipes-CELsPlba.mjs.map → cli-recipes-Bp_LMzOR.mjs.map} +1 -1
  9. package/build/cli.mjs +7 -7
  10. package/build/{daemon-DazTlNoE.mjs → daemon-B7SbxQS2.mjs} +3 -3
  11. package/build/{daemon-DazTlNoE.mjs.map → daemon-B7SbxQS2.mjs.map} +1 -1
  12. package/build/{diff-BFVYtIvz.mjs → diff-D0fCuqm-.mjs} +2 -2
  13. package/build/{diff-BFVYtIvz.mjs.map → diff-D0fCuqm-.mjs.map} +1 -1
  14. package/build/{extract-CI-8rcx5.mjs → extract-BQLuBe6Z.mjs} +2 -2
  15. package/build/{extract-CI-8rcx5.mjs.map → extract-BQLuBe6Z.mjs.map} +1 -1
  16. package/build/index.d.mts +39 -2
  17. package/build/index.mjs +11 -11
  18. package/build/{open-CQO3gf6C.mjs → open-D5ET6_f4.mjs} +10 -6
  19. package/build/{open-CQO3gf6C.mjs.map → open-D5ET6_f4.mjs.map} +1 -1
  20. package/build/{recipe-C5iXikn3.mjs → recipe-DglmR882.mjs} +3 -3
  21. package/build/{recipe-C5iXikn3.mjs.map → recipe-DglmR882.mjs.map} +1 -1
  22. package/build/{recipe-product-BuejBcxL.mjs → recipe-product-H05CkiNF.mjs} +4 -4
  23. package/build/{recipe-product-BuejBcxL.mjs.map → recipe-product-H05CkiNF.mjs.map} +1 -1
  24. package/build/{runtime-CIuB8bcr.mjs → runtime-B6bhQ0zb.mjs} +43 -3
  25. package/build/runtime-B6bhQ0zb.mjs.map +1 -0
  26. package/build/{version-BGzjMFfh.mjs → version-C_fke4iU.mjs} +2 -2
  27. package/build/{version-BGzjMFfh.mjs.map → version-C_fke4iU.mjs.map} +1 -1
  28. package/build/{wacz-BQ_TbYhu.mjs → wacz-D2Klun_5.mjs} +3 -3
  29. package/build/{wacz-BQ_TbYhu.mjs.map → wacz-D2Klun_5.mjs.map} +1 -1
  30. package/build/{warc-dri2CGIB.mjs → warc-Co_sZew8.mjs} +2 -2
  31. package/build/{warc-dri2CGIB.mjs.map → warc-Co_sZew8.mjs.map} +1 -1
  32. package/package.json +1 -1
  33. package/build/runtime-CIuB8bcr.mjs.map +0 -1
package/README.md CHANGED
@@ -1312,6 +1312,28 @@ 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 replies of a poll
1316
+
1317
+ A 304 has no body. A client that reads each body into many values keeps those values until the next changed body, so its memory grows with the data. `keptReplies` keeps the last full reply of each URL instead, with its body as compressed bytes. At a 304, the client reads the reply again.
1318
+
1319
+ ```ts
1320
+ import { keptReplies, poll } from "polite-crawler/api";
1321
+
1322
+ const replies = keptReplies<{ fetched: number }>();
1323
+ const polled = await poll(pace, transport, conditions, input);
1324
+ if (polled.reply.status === 304) {
1325
+ const kept = await replies.get(input.exchange.url);
1326
+ // read kept.reply.body again, with kept.extra.fetched
1327
+ } else {
1328
+ replies.keep(input.exchange.url, polled.reply, { fetched: Date.now() });
1329
+ }
1330
+ ```
1331
+
1332
+ - **The value of the client.** Each reply keeps a value of the client, for example the time of the fetch. Thus a new read gives the same result.
1333
+ - **Only the text gets the compression.** A reply with `bytes` keeps its bytes as they are.
1334
+ - **A failure gives no reply.** A failed compression or expansion rejects the `get`. Then the client has no earlier full answer: delete the condition of the URL, and the next poll asks for the full answer.
1335
+ - **One run only.** The store is in memory. `keptStore` keeps answers between two runs.
1336
+
1315
1337
  ### Keep the answers of an API
1316
1338
 
1317
1339
  `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.
@@ -1459,6 +1481,20 @@ A server examines `If-None-Match` first, and it ignores `If-Modified-Since` when
1459
1481
  3. When the date alone also fails 3 times in sequence, the crawler sends the two validators again, because they cost nothing. The memo records that neither validator works.
1460
1482
  4. A changed body tells nothing about a validator. The robots.txt of a host is not part of its memo, because it is often a static file with validators of its own.
1461
1483
 
1484
+ ### The body of a capture, and an unchanged page
1485
+
1486
+ The result of `fetch` tells if the page changed. `unchanged` is true for a 304 and for an answer of the stored copy (`from_cache`). It is also true when the new body is the same as the previous body of the document. `blob` is the SHA-256 hash of the body that the document shows. Thus a caller does not keep its own hash of each page.
1487
+
1488
+ `body(capture)` gives the bytes of the body of a capture, for example the `capture` of a page. It sends no request. It gives `undefined` for an unknown capture, for a capture with no body, and for a page that asked not to be AI input. Over a runtime of your own, `captureBody(runtime, capture)` does the same.
1489
+
1490
+ ```ts
1491
+ const page = await crawler.fetch("https://example.com/list", { maxAge: "0s" });
1492
+ if (!page.unchanged && page.capture !== null) {
1493
+ const bytes = await crawler.body(page.capture);
1494
+ // parse the bytes
1495
+ }
1496
+ ```
1497
+
1462
1498
  The memo is in the host state store. `openCrawler` keeps it in `hosts.sqlite`, so the processes on one store share it, and it stays after a restart. `MemoryHostStateStore` keeps it in memory. A `HostStateStore` of your own gets a memo when it has the optional methods `validators` and `noteValidators`. `nextValidatorMemo` and `VALIDATOR_MEMO` give the rules and the limits.
1463
1499
 
1464
1500
  ## The store
package/build/api.d.mts CHANGED
@@ -209,6 +209,37 @@ export declare const isApiError: (value: unknown) => value is ApiError;
209
209
  * @beta
210
210
  */
211
211
  export declare function freshUntil(header: (name: string) => string | undefined, status: number, fetchedAt: number, lastModified: string | undefined, capMs: number): number | undefined;
212
+ /**
213
+ * One reply of a store of kept replies, with the value that the caller kept with it.
214
+ *
215
+ * @beta
216
+ */
217
+ interface KeptReply<Extra> {
218
+ /** This value is the reply, with its body. */
219
+ readonly reply: Reply;
220
+ /** This value is the value that the caller kept with the reply. */
221
+ readonly extra: Extra;
222
+ }
223
+ /**
224
+ * A store of the last full reply of each URL.
225
+ *
226
+ * @beta
227
+ */
228
+ interface KeptReplies<Extra> {
229
+ /** This method keeps the full reply of a URL in place of the last one. */
230
+ keep(url: string, reply: Reply, extra: Extra): void;
231
+ /** This method gives the last full reply of a URL, or `undefined` when the store has none. */
232
+ get(url: string): Promise<KeptReply<Extra> | undefined>;
233
+ /** This method removes the reply of a URL. */
234
+ delete(url: string): void;
235
+ }
236
+ /**
237
+ * This function makes a store of the last full reply of each URL of a poll, with each text body
238
+ * as compressed bytes. The store is in memory.
239
+ *
240
+ * @beta
241
+ */
242
+ export declare function keptReplies<Extra>(): KeptReplies<Extra>;
212
243
  /**
213
244
  * The name and the version of the sealed format. It is the first line of the sealed bytes.
214
245
  *
@@ -916,4 +947,4 @@ export interface Polled extends Answer {
916
947
  * @beta
917
948
  */
918
949
  export declare function poll<S extends Signal = AbortSignal>(pace: Pace<S>, transport: Transport<S>, conditions: Conditions, input: Send, signal?: S): Promise<Polled>;
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 };
950
+ export type { Classified, HostCounts, HostReaction, HostResult, HostStateStore, HostStop, KeepOptions, Kept, KeptAnswer, KeptMemory, KeptReplies, KeptReply, KeptStore, OpenedKept, Outcome, OutcomeFacts, Paced, PolicyOptions, PolitenessPolicy, Quota, QuotaGate, QuotaGateOptions, QuotaHeaders, QuotaReading, QuotaRecord, QuotaState, QuotaStore, QuotaWindow, ReactionContext, SealOptions, ValidatorAnswer, ValidatorMemo, ValidatorReport, ValidatorVerdict, WithQuotaOptions };
package/build/api.mjs CHANGED
@@ -563,6 +563,50 @@ function readKept(data) {
563
563
  }
564
564
  return out;
565
565
  }
566
+ // src/kept-replies.ts
567
+ /** This function gives the compressed bytes of a text. */
568
+ const deflate = async (text) => {
569
+ const stream = new Blob([text]).stream().pipeThrough(new CompressionStream("deflate-raw"));
570
+ return new Uint8Array(await new Response(stream).arrayBuffer());
571
+ };
572
+ /** This function gives the text of compressed bytes. */
573
+ const inflate = async (bytes) => new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream("deflate-raw"))).text();
574
+ /**
575
+ * This function makes a store of the last full reply of each URL of a poll, with each text body
576
+ * as compressed bytes. The store is in memory.
577
+ *
578
+ * @beta
579
+ */
580
+ function keptReplies() {
581
+ const entries = /* @__PURE__ */ new Map();
582
+ return {
583
+ keep(url, reply, extra) {
584
+ const { body, ...rest } = reply;
585
+ const entry = {
586
+ reply: rest,
587
+ body: deflate(body),
588
+ extra
589
+ };
590
+ entry.body.catch(() => void 0);
591
+ entries.set(url, entry);
592
+ },
593
+ async get(url) {
594
+ const entry = entries.get(url);
595
+ if (entry === void 0) return void 0;
596
+ const body = await inflate(await entry.body);
597
+ return {
598
+ reply: {
599
+ ...entry.reply,
600
+ body
601
+ },
602
+ extra: entry.extra
603
+ };
604
+ },
605
+ delete(url) {
606
+ entries.delete(url);
607
+ }
608
+ };
609
+ }
566
610
  // src/kept-seal.ts
567
611
  /**
568
612
  * The sealed form of a store of kept answers. Use it for a place that other people can read, for
@@ -738,6 +782,8 @@ async function openKept(sealed, secret, options = {}) {
738
782
  * `keepAnswer` keep the last full answer of a GET with its body, so a 304 gives the kept answer. The
739
783
  * client decides where the store is: a store is data. Each export is beta: a minor version can
740
784
  * change it.
785
+ *
786
+ * `keptReplies` keeps the last full reply of each URL of a poll, with the body compressed.
741
787
  */
742
788
  const unchanged = (text) => text;
743
789
  const messageOf = (error) => error instanceof Error ? error.message : String(error);
@@ -1116,6 +1162,6 @@ async function poll(pace, transport, conditions, input, signal) {
1116
1162
  changed
1117
1163
  };
1118
1164
  }
1119
- export { API_TIMEOUT_MS, ApiError, KEPT_BODY_MAX, KEPT_HEADERS, KEPT_SEAL_FORMAT, KEPT_SEAL_SECRET_MIN, KEPT_STATUSES, QuotaError, TOKEN_MARGIN_MS, fetchTransport, freshUntil, hasValidator, isApiError, isQuotaError, isTextType, keepAnswer, keptFetch, keptKey, keptRequestHeaders, keptStore, memoryQuotaStore, openKept, outcomeResult, poll, quotaGate, reactToOutcome, readGrant, readKept, readQuotaHeaders, sealKept, send, sendWithToken, statusLine, tokenStore, withQuota };
1165
+ export { API_TIMEOUT_MS, ApiError, KEPT_BODY_MAX, KEPT_HEADERS, KEPT_SEAL_FORMAT, KEPT_SEAL_SECRET_MIN, KEPT_STATUSES, QuotaError, TOKEN_MARGIN_MS, fetchTransport, freshUntil, hasValidator, isApiError, isQuotaError, isTextType, keepAnswer, keptFetch, keptKey, keptReplies, keptRequestHeaders, keptStore, memoryQuotaStore, openKept, outcomeResult, poll, quotaGate, reactToOutcome, readGrant, readKept, readQuotaHeaders, sealKept, send, sendWithToken, statusLine, tokenStore, withQuota };
1120
1166
 
1121
1167
  //# sourceMappingURL=api.mjs.map