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.
- package/README.md +43 -0
- package/build/api.d.mts +227 -1
- package/build/api.mjs +424 -3
- package/build/api.mjs.map +1 -1
- package/build/{cli-daemon-f2xHraCa.mjs → cli-daemon-11H9DrcR.mjs} +2 -2
- package/build/{cli-daemon-f2xHraCa.mjs.map → cli-daemon-11H9DrcR.mjs.map} +1 -1
- package/build/{cli-recipes-ZFyc0wTa.mjs → cli-recipes-CELsPlba.mjs} +2 -2
- package/build/{cli-recipes-ZFyc0wTa.mjs.map → cli-recipes-CELsPlba.mjs.map} +1 -1
- package/build/cli.mjs +7 -7
- package/build/{daemon-CqQmcYh9.mjs → daemon-DazTlNoE.mjs} +3 -3
- package/build/{daemon-CqQmcYh9.mjs.map → daemon-DazTlNoE.mjs.map} +1 -1
- package/build/{diff-BwrS5qLG.mjs → diff-BFVYtIvz.mjs} +2 -2
- package/build/{diff-BwrS5qLG.mjs.map → diff-BFVYtIvz.mjs.map} +1 -1
- package/build/{extract-Dt-zWC9M.mjs → extract-CI-8rcx5.mjs} +2 -2
- package/build/{extract-Dt-zWC9M.mjs.map → extract-CI-8rcx5.mjs.map} +1 -1
- package/build/index.d.mts +1 -1
- package/build/index.mjs +10 -10
- package/build/{open-D2aPACL_.mjs → open-CQO3gf6C.mjs} +6 -6
- package/build/{open-D2aPACL_.mjs.map → open-CQO3gf6C.mjs.map} +1 -1
- package/build/{recipe-BZCG1pOy.mjs → recipe-C5iXikn3.mjs} +3 -3
- package/build/{recipe-BZCG1pOy.mjs.map → recipe-C5iXikn3.mjs.map} +1 -1
- package/build/{recipe-product-gd-6XJ4C.mjs → recipe-product-BuejBcxL.mjs} +4 -4
- package/build/{recipe-product-gd-6XJ4C.mjs.map → recipe-product-BuejBcxL.mjs.map} +1 -1
- package/build/{runtime-CD32YZv5.mjs → runtime-CIuB8bcr.mjs} +2 -2
- package/build/{runtime-CD32YZv5.mjs.map → runtime-CIuB8bcr.mjs.map} +1 -1
- package/build/{version-ACvb6zmT.mjs → version-BGzjMFfh.mjs} +2 -2
- package/build/{version-ACvb6zmT.mjs.map → version-BGzjMFfh.mjs.map} +1 -1
- package/build/{wacz-C2egGCSb.mjs → wacz-BQ_TbYhu.mjs} +3 -3
- package/build/{wacz-C2egGCSb.mjs.map → wacz-BQ_TbYhu.mjs.map} +1 -1
- package/build/{warc-DshKROY0.mjs → warc-dri2CGIB.mjs} +2 -2
- package/build/{warc-DshKROY0.mjs.map → warc-dri2CGIB.mjs.map} +1 -1
- package/package.json +1 -1
package/build/api.mjs
CHANGED
|
@@ -347,6 +347,381 @@ function readQuotaHeaders(get, names, now) {
|
|
|
347
347
|
...resetAt === void 0 ? {} : { resetAt }
|
|
348
348
|
};
|
|
349
349
|
}
|
|
350
|
+
// src/kept.ts
|
|
351
|
+
/**
|
|
352
|
+
* The headers that a kept answer keeps when the options give no list. `link` keeps the next page
|
|
353
|
+
* of a list.
|
|
354
|
+
*
|
|
355
|
+
* @beta
|
|
356
|
+
*/
|
|
357
|
+
const KEPT_HEADERS = [
|
|
358
|
+
"content-type",
|
|
359
|
+
"etag",
|
|
360
|
+
"last-modified",
|
|
361
|
+
"link"
|
|
362
|
+
];
|
|
363
|
+
/**
|
|
364
|
+
* The largest body that a store keeps when the options give no limit: 1,048,576 UTF-16 code units.
|
|
365
|
+
*
|
|
366
|
+
* @beta
|
|
367
|
+
*/
|
|
368
|
+
const KEPT_BODY_MAX = 1048576;
|
|
369
|
+
/**
|
|
370
|
+
* The statuses that a store keeps. The body of each is the full answer. A 206 has only a part of
|
|
371
|
+
* the body, so a store does not keep it.
|
|
372
|
+
*
|
|
373
|
+
* @beta
|
|
374
|
+
*/
|
|
375
|
+
const KEPT_STATUSES = [
|
|
376
|
+
200,
|
|
377
|
+
203,
|
|
378
|
+
204
|
|
379
|
+
];
|
|
380
|
+
/** These fields of a 304 tell of its empty body, so they do not go into the kept answer. */
|
|
381
|
+
const BODY_FIELDS = /* @__PURE__ */ new Set([
|
|
382
|
+
"content-encoding",
|
|
383
|
+
"content-length",
|
|
384
|
+
"transfer-encoding"
|
|
385
|
+
]);
|
|
386
|
+
/** A body of one of these media types is text, so a store can keep it as text. */
|
|
387
|
+
const TEXT_TYPE = /^\s*(?:text\/[\w.+-]+|application\/(?:[\w.-]+\+)?(?:json|xml))\s*(?:;|$)/i;
|
|
388
|
+
/** This pattern finds the charset parameter of a media type. */
|
|
389
|
+
const CHARSET = /;\s*charset\s*=\s*"?([^";\s]+)"?/i;
|
|
390
|
+
/** This function gives the headers with each name in lowercase. */
|
|
391
|
+
const lower = (headers) => {
|
|
392
|
+
const out = {};
|
|
393
|
+
for (const [name, value] of Object.entries(headers)) out[name.toLowerCase()] = value;
|
|
394
|
+
return out;
|
|
395
|
+
};
|
|
396
|
+
/**
|
|
397
|
+
* This function tells if the headers of a request make it conditional or partial on their own:
|
|
398
|
+
* `If-None-Match`, `If-Modified-Since`, `If-Match`, `If-Unmodified-Since`, `If-Range` or
|
|
399
|
+
* `Range`. The client of such a request reads the answer itself, so a store does not answer for
|
|
400
|
+
* it.
|
|
401
|
+
*
|
|
402
|
+
* @beta
|
|
403
|
+
*/
|
|
404
|
+
function hasValidator(headers) {
|
|
405
|
+
return Object.keys(headers).some((name) => {
|
|
406
|
+
const n = name.toLowerCase();
|
|
407
|
+
return n === "range" || n.startsWith("if-");
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* This function tells if a body of a media type is text in UTF-8. That is a text, JSON or XML type
|
|
412
|
+
* with no charset or with the charset UTF-8. A store keeps only such a body, because `text()` of
|
|
413
|
+
* `fetch` reads a body as UTF-8.
|
|
414
|
+
*
|
|
415
|
+
* @beta
|
|
416
|
+
*/
|
|
417
|
+
function isTextType(contentType) {
|
|
418
|
+
const type = contentType ?? "";
|
|
419
|
+
if (!TEXT_TYPE.test(type)) return false;
|
|
420
|
+
const charset = CHARSET.exec(type)?.[1];
|
|
421
|
+
return charset === void 0 || /^utf-?8$/i.test(charset);
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* This function gives the key of a request in a store: its `accept` header and its URL. A
|
|
425
|
+
* different `accept` header can give a different body, so it gets a different key.
|
|
426
|
+
*
|
|
427
|
+
* @beta
|
|
428
|
+
*/
|
|
429
|
+
function keptKey(url, headers) {
|
|
430
|
+
return `${lower(headers)["accept"] ?? ""} ${url}`;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* This function gives the headers of a conditional request: the headers of the request, and the
|
|
434
|
+
* validators of the kept answer. `If-None-Match` sends its ETag, and `If-Modified-Since` sends its
|
|
435
|
+
* date. The function adds no header to a request that has a validator of its own.
|
|
436
|
+
*
|
|
437
|
+
* @beta
|
|
438
|
+
*/
|
|
439
|
+
function keptRequestHeaders(kept, headers) {
|
|
440
|
+
const out = { ...headers };
|
|
441
|
+
if (kept === void 0 || hasValidator(headers)) return out;
|
|
442
|
+
if (kept.etag !== void 0) out["if-none-match"] = kept.etag;
|
|
443
|
+
if (kept.lastModified !== void 0) out["if-modified-since"] = kept.lastModified;
|
|
444
|
+
return out;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* This function gives the answer that a client reads, and changes the store.
|
|
448
|
+
*
|
|
449
|
+
* - A 304 for a key with a kept answer gives the kept answer. Its headers get the fields of the
|
|
450
|
+
* 304, as RFC 9111 §4.3.4 tells, and the store keeps the new values of its kept headers.
|
|
451
|
+
* - An answer with a status of `KEPT_STATUSES` and an ETag or a date goes into the store. It needs
|
|
452
|
+
* a text type in UTF-8 and a body in the limit.
|
|
453
|
+
* - Each other answer is as it is, and the store does not change.
|
|
454
|
+
*
|
|
455
|
+
* @beta
|
|
456
|
+
*/
|
|
457
|
+
function keepAnswer(store, key, answer, options = {}) {
|
|
458
|
+
const names = new Set((options.headers ?? KEPT_HEADERS).map((name) => name.toLowerCase()));
|
|
459
|
+
const headers = lower(answer.headers);
|
|
460
|
+
const kept = store.get(key);
|
|
461
|
+
if (answer.status === 304 && kept !== void 0) {
|
|
462
|
+
const read = { ...kept.headers };
|
|
463
|
+
const keep = { ...kept.headers };
|
|
464
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
465
|
+
if (BODY_FIELDS.has(name)) continue;
|
|
466
|
+
read[name] = value;
|
|
467
|
+
if (names.has(name)) keep[name] = value;
|
|
468
|
+
}
|
|
469
|
+
store.set(key, {
|
|
470
|
+
...kept,
|
|
471
|
+
etag: headers["etag"] ?? kept.etag,
|
|
472
|
+
lastModified: headers["last-modified"] ?? kept.lastModified,
|
|
473
|
+
headers: keep
|
|
474
|
+
});
|
|
475
|
+
return {
|
|
476
|
+
status: kept.status,
|
|
477
|
+
headers: read,
|
|
478
|
+
body: kept.body,
|
|
479
|
+
notModified: true
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
const etag = headers["etag"];
|
|
483
|
+
const lastModified = headers["last-modified"];
|
|
484
|
+
if (KEPT_STATUSES.includes(answer.status) && (etag !== void 0 || lastModified !== void 0) && isTextType(headers["content-type"]) && answer.body.length <= (options.maxBody ?? 1048576)) {
|
|
485
|
+
const keep = {};
|
|
486
|
+
for (const [name, value] of Object.entries(headers)) if (names.has(name)) keep[name] = value;
|
|
487
|
+
store.set(key, {
|
|
488
|
+
etag,
|
|
489
|
+
lastModified,
|
|
490
|
+
status: answer.status,
|
|
491
|
+
headers: keep,
|
|
492
|
+
body: answer.body
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
return {
|
|
496
|
+
status: answer.status,
|
|
497
|
+
headers,
|
|
498
|
+
body: answer.body,
|
|
499
|
+
notModified: false
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* This function makes a store of kept answers in memory, with the entries of an earlier run.
|
|
504
|
+
*
|
|
505
|
+
* @beta
|
|
506
|
+
*/
|
|
507
|
+
function keptStore(entries = []) {
|
|
508
|
+
const all = new Map(entries);
|
|
509
|
+
const used = /* @__PURE__ */ new Set();
|
|
510
|
+
return {
|
|
511
|
+
get(key) {
|
|
512
|
+
const kept = all.get(key);
|
|
513
|
+
if (kept !== void 0) used.add(key);
|
|
514
|
+
return kept;
|
|
515
|
+
},
|
|
516
|
+
set(key, kept) {
|
|
517
|
+
all.set(key, kept);
|
|
518
|
+
used.add(key);
|
|
519
|
+
},
|
|
520
|
+
used: () => [...used].flatMap((key) => {
|
|
521
|
+
const kept = all.get(key);
|
|
522
|
+
return kept === void 0 ? [] : [[key, kept]];
|
|
523
|
+
})
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
/** This function gives a kept answer from data, or null when the data does not have its shape. */
|
|
527
|
+
const keptOf = (value) => {
|
|
528
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
529
|
+
const { etag, lastModified, status, headers, body } = value;
|
|
530
|
+
if (typeof status !== "number" || !KEPT_STATUSES.includes(status)) return null;
|
|
531
|
+
if (typeof body !== "string") return null;
|
|
532
|
+
if (etag !== void 0 && typeof etag !== "string") return null;
|
|
533
|
+
if (lastModified !== void 0 && typeof lastModified !== "string") return null;
|
|
534
|
+
if (etag === void 0 && lastModified === void 0) return null;
|
|
535
|
+
if (typeof headers !== "object" || headers === null || Array.isArray(headers)) return null;
|
|
536
|
+
const read = {};
|
|
537
|
+
for (const [name, field] of Object.entries(headers)) {
|
|
538
|
+
if (typeof field !== "string") return null;
|
|
539
|
+
read[name.toLowerCase()] = field;
|
|
540
|
+
}
|
|
541
|
+
return {
|
|
542
|
+
etag: typeof etag === "string" ? etag : void 0,
|
|
543
|
+
lastModified: typeof lastModified === "string" ? lastModified : void 0,
|
|
544
|
+
status,
|
|
545
|
+
headers: read,
|
|
546
|
+
body
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
/**
|
|
550
|
+
* This function reads the entries of a store from data, for example from the JSON of the `used`
|
|
551
|
+
* entries of an earlier run. It keeps each entry with a correct shape and drops each other entry.
|
|
552
|
+
* Thus damaged data gives fewer kept answers and no error.
|
|
553
|
+
*
|
|
554
|
+
* @beta
|
|
555
|
+
*/
|
|
556
|
+
function readKept(data) {
|
|
557
|
+
if (!Array.isArray(data)) return [];
|
|
558
|
+
const out = [];
|
|
559
|
+
for (const entry of data) {
|
|
560
|
+
if (!Array.isArray(entry) || entry.length !== 2 || typeof entry[0] !== "string") continue;
|
|
561
|
+
const kept = keptOf(entry[1]);
|
|
562
|
+
if (kept !== null) out.push([entry[0], kept]);
|
|
563
|
+
}
|
|
564
|
+
return out;
|
|
565
|
+
}
|
|
566
|
+
// src/kept-seal.ts
|
|
567
|
+
/**
|
|
568
|
+
* The sealed form of a store of kept answers. Use it for a place that other people can read, for
|
|
569
|
+
* example the cache of a public repository in GitHub Actions. A kept answer of a private resource
|
|
570
|
+
* holds its body as text. Thus a client that saves its store in such a place seals it first.
|
|
571
|
+
*
|
|
572
|
+
* `sealKept` encrypts the entries with AES-256-GCM. The key comes from a secret by HKDF-SHA-256
|
|
573
|
+
* (RFC 5869). Each seal has a new random salt, and the label of the use is the info. Thus two labels
|
|
574
|
+
* give two keys from one secret. The format name is the first line of the bytes. It is also the
|
|
575
|
+
* associated data of the cipher, so a change of the format cannot go unseen.
|
|
576
|
+
*
|
|
577
|
+
* Each entry is compressed alone (gzip) before the encryption. A compression of all entries together
|
|
578
|
+
* puts the text of one answer in the window of another answer. Then the size of the sealed bytes can
|
|
579
|
+
* tell something of a private answer to a person who controls a public answer. With one compression
|
|
580
|
+
* for each entry, the text of an answer meets only its own text.
|
|
581
|
+
*
|
|
582
|
+
* The functions use only Web APIs (`crypto.subtle`, `CompressionStream`). Thus they operate in
|
|
583
|
+
* Node.js and in a browser.
|
|
584
|
+
*/
|
|
585
|
+
/**
|
|
586
|
+
* The name and the version of the sealed format. It is the first line of the sealed bytes.
|
|
587
|
+
*
|
|
588
|
+
* @beta
|
|
589
|
+
*/
|
|
590
|
+
const KEPT_SEAL_FORMAT = "polite-crawler-kept-sealed/1";
|
|
591
|
+
/**
|
|
592
|
+
* The smallest secret that `sealKept` and `openKept` take, in bytes. HKDF does not slow a guess, so
|
|
593
|
+
* the secret must be a random key, not a password.
|
|
594
|
+
*
|
|
595
|
+
* @beta
|
|
596
|
+
*/
|
|
597
|
+
const KEPT_SEAL_SECRET_MIN = 16;
|
|
598
|
+
const SALT_BYTES = 16;
|
|
599
|
+
const IV_BYTES = 12;
|
|
600
|
+
const TAG_BYTES = 16;
|
|
601
|
+
const encoder = new TextEncoder();
|
|
602
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
603
|
+
const HEADER = encoder.encode(`${KEPT_SEAL_FORMAT}\n`);
|
|
604
|
+
/** This function gives the bytes of the secret, or throws when the secret is too short. */
|
|
605
|
+
const secretBytes = (secret) => {
|
|
606
|
+
const bytes = typeof secret === "string" ? encoder.encode(secret) : new Uint8Array(secret);
|
|
607
|
+
if (bytes.length < 16) throw new RangeError(`The secret of a sealed store needs 16 bytes or more, and it has ${bytes.length}. Give a random key.`);
|
|
608
|
+
return bytes;
|
|
609
|
+
};
|
|
610
|
+
/** This function gives the AES-GCM key of a seal from the secret, the salt and the label. */
|
|
611
|
+
const keyOf = async (secret, salt, label, use) => {
|
|
612
|
+
const base = await crypto.subtle.importKey("raw", secret, "HKDF", false, ["deriveKey"]);
|
|
613
|
+
const info = encoder.encode(`${KEPT_SEAL_FORMAT}\n${label}`);
|
|
614
|
+
return crypto.subtle.deriveKey({
|
|
615
|
+
name: "HKDF",
|
|
616
|
+
hash: "SHA-256",
|
|
617
|
+
salt,
|
|
618
|
+
info
|
|
619
|
+
}, base, {
|
|
620
|
+
name: "AES-GCM",
|
|
621
|
+
length: 256
|
|
622
|
+
}, false, [use]);
|
|
623
|
+
};
|
|
624
|
+
/** This function sends the bytes through a compression stream or a decompression stream. */
|
|
625
|
+
const through = async (bytes, stream) => new Uint8Array(await new Response(new Blob([bytes]).stream().pipeThrough(stream)).arrayBuffer());
|
|
626
|
+
/** This function joins the parts into one array of bytes. */
|
|
627
|
+
const join = (parts) => {
|
|
628
|
+
const out = new Uint8Array(parts.reduce((sum, part) => sum + part.length, 0));
|
|
629
|
+
let at = 0;
|
|
630
|
+
for (const part of parts) {
|
|
631
|
+
out.set(part, at);
|
|
632
|
+
at += part.length;
|
|
633
|
+
}
|
|
634
|
+
return out;
|
|
635
|
+
};
|
|
636
|
+
/**
|
|
637
|
+
* This function seals the entries of a store of kept answers, for example the `used` entries of a
|
|
638
|
+
* run. It compresses each entry alone, and it encrypts all of them with a key from the secret. It
|
|
639
|
+
* throws a `RangeError` when the secret has fewer than `KEPT_SEAL_SECRET_MIN` bytes.
|
|
640
|
+
*
|
|
641
|
+
* @beta
|
|
642
|
+
*/
|
|
643
|
+
async function sealKept(entries, secret, options = {}) {
|
|
644
|
+
const key = secretBytes(secret);
|
|
645
|
+
const parts = [];
|
|
646
|
+
for (const entry of entries) {
|
|
647
|
+
const packed = await through(encoder.encode(JSON.stringify(entry)), new CompressionStream("gzip"));
|
|
648
|
+
const length = /* @__PURE__ */ new Uint8Array(4);
|
|
649
|
+
new DataView(length.buffer).setUint32(0, packed.length);
|
|
650
|
+
parts.push(length, packed);
|
|
651
|
+
}
|
|
652
|
+
const salt = crypto.getRandomValues(new Uint8Array(SALT_BYTES));
|
|
653
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES));
|
|
654
|
+
const cipherKey = await keyOf(key, salt, options.label ?? "", "encrypt");
|
|
655
|
+
const sealed = await crypto.subtle.encrypt({
|
|
656
|
+
name: "AES-GCM",
|
|
657
|
+
iv,
|
|
658
|
+
additionalData: HEADER
|
|
659
|
+
}, cipherKey, join(parts));
|
|
660
|
+
return join([
|
|
661
|
+
HEADER,
|
|
662
|
+
salt,
|
|
663
|
+
iv,
|
|
664
|
+
new Uint8Array(sealed)
|
|
665
|
+
]);
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* This function opens a store that `sealKept` sealed, and gives its entries. Data that does not open
|
|
669
|
+
* (another format, another secret, another label, damaged bytes) gives no entries and a note, and no
|
|
670
|
+
* error, so a client goes on with an empty store. It throws a `RangeError` when the secret has fewer
|
|
671
|
+
* than `KEPT_SEAL_SECRET_MIN` bytes, as `sealKept` does. As `readKept` does, it drops each entry that
|
|
672
|
+
* does not have the shape of a kept answer.
|
|
673
|
+
*
|
|
674
|
+
* @beta
|
|
675
|
+
*/
|
|
676
|
+
async function openKept(sealed, secret, options = {}) {
|
|
677
|
+
const key = secretBytes(secret);
|
|
678
|
+
const bytes = new Uint8Array(sealed);
|
|
679
|
+
if (bytes.length < HEADER.length || HEADER.some((byte, index) => bytes[index] !== byte)) return {
|
|
680
|
+
entries: [],
|
|
681
|
+
note: `The data is not a sealed store of the format ${KEPT_SEAL_FORMAT}.`
|
|
682
|
+
};
|
|
683
|
+
const start = HEADER.length;
|
|
684
|
+
if (bytes.length < start + SALT_BYTES + IV_BYTES + TAG_BYTES) return {
|
|
685
|
+
entries: [],
|
|
686
|
+
note: "The sealed store is too short, so it is damaged."
|
|
687
|
+
};
|
|
688
|
+
const salt = bytes.slice(start, start + SALT_BYTES);
|
|
689
|
+
const iv = bytes.slice(start + SALT_BYTES, start + SALT_BYTES + IV_BYTES);
|
|
690
|
+
let plain;
|
|
691
|
+
try {
|
|
692
|
+
const cipherKey = await keyOf(key, salt, options.label ?? "", "decrypt");
|
|
693
|
+
const opened = await crypto.subtle.decrypt({
|
|
694
|
+
name: "AES-GCM",
|
|
695
|
+
iv,
|
|
696
|
+
additionalData: HEADER
|
|
697
|
+
}, cipherKey, bytes.slice(start + SALT_BYTES + IV_BYTES));
|
|
698
|
+
plain = new Uint8Array(opened);
|
|
699
|
+
} catch {
|
|
700
|
+
return {
|
|
701
|
+
entries: [],
|
|
702
|
+
note: "The sealed store does not open: another secret, another label, or damaged bytes."
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
const read = [];
|
|
706
|
+
const view = new DataView(plain.buffer);
|
|
707
|
+
let at = 0;
|
|
708
|
+
try {
|
|
709
|
+
while (at < plain.length) {
|
|
710
|
+
if (at + 4 > plain.length) throw new Error("a length is cut");
|
|
711
|
+
const length = view.getUint32(at);
|
|
712
|
+
at += 4;
|
|
713
|
+
if (at + length > plain.length) throw new Error("an entry is cut");
|
|
714
|
+
read.push(JSON.parse(decoder.decode(await through(plain.slice(at, at + length), new DecompressionStream("gzip")))));
|
|
715
|
+
at += length;
|
|
716
|
+
}
|
|
717
|
+
} catch {
|
|
718
|
+
return {
|
|
719
|
+
entries: readKept(read),
|
|
720
|
+
note: "An entry of the sealed store does not read, so the store gives only the entries before it."
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
return { entries: readKept(read) };
|
|
724
|
+
}
|
|
350
725
|
// src/api.ts
|
|
351
726
|
/**
|
|
352
727
|
* The polite send of a documented API. It keeps the pace of each host with the scheduler of the
|
|
@@ -359,8 +734,10 @@ function readQuotaHeaders(get, names, now) {
|
|
|
359
734
|
* The module also has the OAuth 2.0 client credentials grant (RFC 6749, section 4.4): `readGrant`,
|
|
360
735
|
* `tokenStore` and `sendWithToken`. `fetchTransport` is a transport over `fetch` with a User-Agent
|
|
361
736
|
* and a time limit. `poll` sends conditional requests. `freshUntil` gives the end of the freshness
|
|
362
|
-
* of an answer (RFC 9111), so a client that keeps answers knows when to ask again.
|
|
363
|
-
*
|
|
737
|
+
* of an answer (RFC 9111), so a client that keeps answers knows when to ask again. `keptFetch` and
|
|
738
|
+
* `keepAnswer` keep the last full answer of a GET with its body, so a 304 gives the kept answer. The
|
|
739
|
+
* client decides where the store is: a store is data. Each export is beta: a minor version can
|
|
740
|
+
* change it.
|
|
364
741
|
*/
|
|
365
742
|
const unchanged = (text) => text;
|
|
366
743
|
const messageOf = (error) => error instanceof Error ? error.message : String(error);
|
|
@@ -627,6 +1004,50 @@ function fetchTransport(options) {
|
|
|
627
1004
|
}
|
|
628
1005
|
};
|
|
629
1006
|
}
|
|
1007
|
+
/**
|
|
1008
|
+
* This function makes a `fetch` with kept answers, for example for `fetchTransport`. A GET with no
|
|
1009
|
+
* body and no conditional or range header of its own (`hasValidator`) sends the validators of its
|
|
1010
|
+
* kept answer. The key of the answer is `keptKey`. A 304 then gives the kept answer, as
|
|
1011
|
+
* `keepAnswer` tells, and a full answer with a text body in UTF-8 goes into the store. The function does not read a body
|
|
1012
|
+
* of a different media type, so an exchange that reads `bytes` gets the bytes. Each other request
|
|
1013
|
+
* goes to `fetch` as it is. The key does not hold the `authorization` header: the server examines
|
|
1014
|
+
* the access of each request before it answers 304.
|
|
1015
|
+
*
|
|
1016
|
+
* @beta
|
|
1017
|
+
*/
|
|
1018
|
+
function keptFetch(fetch, store, options = {}) {
|
|
1019
|
+
return async (url, init) => {
|
|
1020
|
+
if (!(init.method.toUpperCase() === "GET" && init.body === void 0) || hasValidator(init.headers)) return fetch(url, init);
|
|
1021
|
+
const key = keptKey(url, init.headers);
|
|
1022
|
+
const kept = store.get(key);
|
|
1023
|
+
const response = await fetch(url, {
|
|
1024
|
+
...init,
|
|
1025
|
+
headers: keptRequestHeaders(kept, init.headers)
|
|
1026
|
+
});
|
|
1027
|
+
const headers = {};
|
|
1028
|
+
response.headers.forEach((value, name) => {
|
|
1029
|
+
headers[name.toLowerCase()] = value;
|
|
1030
|
+
});
|
|
1031
|
+
const unchanged = response.status === 304 && kept !== void 0;
|
|
1032
|
+
const full = KEPT_STATUSES.includes(response.status) && isTextType(headers["content-type"]);
|
|
1033
|
+
if (!unchanged && !full) return response;
|
|
1034
|
+
const body = response.status === 304 ? "" : await response.text();
|
|
1035
|
+
const answer = keepAnswer(store, key, {
|
|
1036
|
+
status: response.status,
|
|
1037
|
+
headers,
|
|
1038
|
+
body
|
|
1039
|
+
}, options);
|
|
1040
|
+
return {
|
|
1041
|
+
status: answer.status,
|
|
1042
|
+
statusText: answer.notModified ? "" : response.statusText,
|
|
1043
|
+
headers: { forEach(callback) {
|
|
1044
|
+
for (const [name, value] of Object.entries(answer.headers)) callback(value, name);
|
|
1045
|
+
} },
|
|
1046
|
+
text: () => Promise.resolve(answer.body),
|
|
1047
|
+
arrayBuffer: () => Promise.resolve(new TextEncoder().encode(answer.body).slice().buffer)
|
|
1048
|
+
};
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
630
1051
|
const HEX = "0123456789abcdef";
|
|
631
1052
|
/** This function gives the SHA-256 of a text or of bytes, in hexadecimal. */
|
|
632
1053
|
const digestOf = async (body) => {
|
|
@@ -695,6 +1116,6 @@ async function poll(pace, transport, conditions, input, signal) {
|
|
|
695
1116
|
changed
|
|
696
1117
|
};
|
|
697
1118
|
}
|
|
698
|
-
export { API_TIMEOUT_MS, ApiError, QuotaError, TOKEN_MARGIN_MS, fetchTransport, freshUntil, isApiError, isQuotaError, memoryQuotaStore, outcomeResult, poll, quotaGate, reactToOutcome, readGrant, readQuotaHeaders, send, sendWithToken, statusLine, tokenStore, withQuota };
|
|
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 };
|
|
699
1120
|
|
|
700
1121
|
//# sourceMappingURL=api.mjs.map
|