@mailwoman/core 8.3.0 → 8.5.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/api/APIClient.ts +207 -42
- package/api/clock.ts +43 -0
- package/api/disk-storage.ts +239 -0
- package/api/index.ts +7 -0
- package/api/pacer.ts +98 -0
- package/api/responses.ts +227 -44
- package/api/retry.ts +227 -0
- package/api/test-clocks.ts +234 -0
- package/data/libpostal/README.md +62 -0
- package/env/schema.ts +7 -0
- package/out/api/APIClient.d.ts +60 -8
- package/out/api/APIClient.d.ts.map +1 -1
- package/out/api/APIClient.js +149 -35
- package/out/api/APIClient.js.map +1 -1
- package/out/api/clock.d.ts +36 -0
- package/out/api/clock.d.ts.map +1 -0
- package/out/api/clock.js +27 -0
- package/out/api/clock.js.map +1 -0
- package/out/api/disk-storage.d.ts +64 -0
- package/out/api/disk-storage.d.ts.map +1 -0
- package/out/api/disk-storage.js +185 -0
- package/out/api/disk-storage.js.map +1 -0
- package/out/api/index.d.ts +3 -0
- package/out/api/index.d.ts.map +1 -1
- package/out/api/index.js +6 -0
- package/out/api/index.js.map +1 -1
- package/out/api/pacer.d.ts +68 -0
- package/out/api/pacer.d.ts.map +1 -0
- package/out/api/pacer.js +88 -0
- package/out/api/pacer.js.map +1 -0
- package/out/api/responses.d.ts +71 -2
- package/out/api/responses.d.ts.map +1 -1
- package/out/api/responses.js +160 -34
- package/out/api/responses.js.map +1 -1
- package/out/api/retry.d.ts +119 -0
- package/out/api/retry.d.ts.map +1 -0
- package/out/api/retry.js +158 -0
- package/out/api/retry.js.map +1 -0
- package/out/api/test-clocks.d.ts +87 -0
- package/out/api/test-clocks.d.ts.map +1 -0
- package/out/api/test-clocks.js +175 -0
- package/out/api/test-clocks.js.map +1 -0
- package/out/env/index.d.ts +3 -0
- package/out/env/index.d.ts.map +1 -1
- package/out/env/schema.d.ts +3 -0
- package/out/env/schema.d.ts.map +1 -1
- package/out/env/schema.js +5 -0
- package/out/env/schema.js.map +1 -1
- package/out/vitest.config.d.ts.map +1 -1
- package/out/vitest.config.js +2 -0
- package/out/vitest.config.js.map +1 -1
- package/package.json +18 -2
- package/vitest.config.ts +2 -0
package/out/api/APIClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"APIClient.js","sourceRoot":"","sources":["../../api/APIClient.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"APIClient.js","sourceRoot":"","sources":["../../api/APIClient.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,EAA6F,MAAM,OAAO,CAAA;AACxH,OAAO,EAIN,UAAU,GACV,MAAM,yBAAyB,CAAA;AAEhC,OAAO,EAAE,aAAa,EAAuB,MAAM,qBAAqB,CAAA;AACxE,OAAO,EAAkB,WAAW,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,EACN,oBAAoB,EAEpB,kBAAkB,EAClB,YAAY,GAEZ,MAAM,YAAY,CAAA;AAEnB,OAAO,EAAuB,MAAM,qBAAqB,CAAA;AAEzD;;GAEG;AACH,MAAM,aAAa,GAAG,MAAM,CAAA;AAyD5B;;;GAGG;AACH,MAAM,OAAO,SAAuD,SAAQ,WAAW;IACtE,MAAM,CAAG;IAEzB,sBAAsB,GAAsC,IAAI,CAAA;IAChE,2BAA2B,GAAG,CAAC,CAAA;IAC/B;;;OAGG;IACH,gBAAgB,GAAG,CAAC,CAAA;IAEX,MAAM,CAAW;IACjB,MAAM,CAAqB;IAC3B,YAAY,CAAqB;IAE1C,IAAW,SAAS;QACnB,OAAO,IAAI,CAAC,sBAAsB,EAAE,OAAO,IAAI,OAAO,CAAC,OAAO,EAAE,CAAA;IACjE,CAAC;IAED;;O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AK,yBAAyB,EAAE,CAAC;gBAC/D,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAA;YACnC,CAAC;YAED,yBAAyB,CAAC,OAAO,EAAE,CAAA;YAEnC,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC,CAAA;QAC9C,CAAC,CAAC,CAAA;IACH,CAAC,CAAA;IAEM,KAAK,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC;QACjC,MAAM,OAAO,GAAG,IAAI,CAAC,sBAAsB,CAAA;QAE3C,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAA;QAClC,OAAO,EAAE,OAAO,EAAE,CAAA;QAElB,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAA;QAEhD,IAAI,iBAAiB,CAAC,WAAW,CAAC,EAAE,CAAC;YACpC,MAAM,WAAW,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAA;QACzC,CAAC;IACF,CAAC;IAEe,QAAQ;QACvB,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,aAAa,CAAA;IAC/C,CAAC;CACD"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file The injectable time source every timing-sensitive part of {@linkcode APIClient} reads through.
|
|
6
|
+
*
|
|
7
|
+
* Lifted from `filer/sdk/sec-client.ts` (3b task 5) when the SEC client's bespoke pacing/retry
|
|
8
|
+
* machinery moved down into `@mailwoman/core/api`. Keeping the seam is the whole reason the pacing
|
|
9
|
+
* and backoff suites are deterministic and finish in milliseconds instead of sleeping on the wall
|
|
10
|
+
* clock — a rate-limit test that actually waits 100ms per grant costs 4s for a 40-call fan-out and
|
|
11
|
+
* is flaky under CI load; the same test against an injected clock is exact and instant.
|
|
12
|
+
*
|
|
13
|
+
* Deliberately NOT `node:timers/promises`: `core/api` reaches a browser bundle (`docs`'s
|
|
14
|
+
* `DashboardMap` imports `@mailwoman/cartographer`, which barrels `tiles/api.ts`, which imports
|
|
15
|
+
* `@mailwoman/core/api`), and webpack 5 refuses to resolve a `node:`-prefixed specifier for the
|
|
16
|
+
* web target. The global `setTimeout` is available on every runtime this package ships to.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* A minimal, injectable time source. Anything in this package that would otherwise call `Date.now()` or `setTimeout`
|
|
20
|
+
* directly reads through this instead, so tests can drive time deterministically.
|
|
21
|
+
*/
|
|
22
|
+
export interface ClockLike {
|
|
23
|
+
/**
|
|
24
|
+
* The current time, in milliseconds. Same contract as `Date.now()`.
|
|
25
|
+
*/
|
|
26
|
+
now(): number;
|
|
27
|
+
/**
|
|
28
|
+
* Resolve after at least `ms` milliseconds of this clock's time.
|
|
29
|
+
*/
|
|
30
|
+
sleep(ms: number): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The real-time {@linkcode ClockLike}, used whenever a caller doesn't inject one.
|
|
34
|
+
*/
|
|
35
|
+
export declare const systemClock: ClockLike;
|
|
36
|
+
//# sourceMappingURL=clock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clock.d.ts","sourceRoot":"","sources":["../../api/clock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;GAGG;AACH,MAAM,WAAW,SAAS;IACzB;;OAEG;IACH,GAAG,IAAI,MAAM,CAAA;IACb;;OAEG;IACH,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAChC;AAED;;GAEG;AACH,eAAO,MAAM,WAAW,EAAE,SAMzB,CAAA"}
|
package/out/api/clock.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file The injectable time source every timing-sensitive part of {@linkcode APIClient} reads through.
|
|
6
|
+
*
|
|
7
|
+
* Lifted from `filer/sdk/sec-client.ts` (3b task 5) when the SEC client's bespoke pacing/retry
|
|
8
|
+
* machinery moved down into `@mailwoman/core/api`. Keeping the seam is the whole reason the pacing
|
|
9
|
+
* and backoff suites are deterministic and finish in milliseconds instead of sleeping on the wall
|
|
10
|
+
* clock — a rate-limit test that actually waits 100ms per grant costs 4s for a 40-call fan-out and
|
|
11
|
+
* is flaky under CI load; the same test against an injected clock is exact and instant.
|
|
12
|
+
*
|
|
13
|
+
* Deliberately NOT `node:timers/promises`: `core/api` reaches a browser bundle (`docs`'s
|
|
14
|
+
* `DashboardMap` imports `@mailwoman/cartographer`, which barrels `tiles/api.ts`, which imports
|
|
15
|
+
* `@mailwoman/core/api`), and webpack 5 refuses to resolve a `node:`-prefixed specifier for the
|
|
16
|
+
* web target. The global `setTimeout` is available on every runtime this package ships to.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* The real-time {@linkcode ClockLike}, used whenever a caller doesn't inject one.
|
|
20
|
+
*/
|
|
21
|
+
export const systemClock = {
|
|
22
|
+
now: () => Date.now(),
|
|
23
|
+
sleep: (ms) => new Promise((resolve) => {
|
|
24
|
+
setTimeout(resolve, ms);
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=clock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../../api/clock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAiBH;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAc;IACrC,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE;IACrB,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,CACb,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QAC7B,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IACxB,CAAC,CAAC;CACH,CAAA"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file An on-disk `axios-cache-interceptor` storage adapter, so a client gets a durable HTTP cache by
|
|
6
|
+
* CONFIGURATION rather than by hand-rolling one (3b task 5).
|
|
7
|
+
*
|
|
8
|
+
* NODE ONLY, and deliberately NOT re-exported from `./index.ts`: `core/api` reaches a browser bundle
|
|
9
|
+
* (`docs`'s `DashboardMap` → `@mailwoman/cartographer` → `tiles/api.ts` → `@mailwoman/core/api`),
|
|
10
|
+
* and webpack refuses to resolve `node:fs/promises` for the web target. Import this through its own
|
|
11
|
+
* `@mailwoman/core/api/disk-storage` subpath.
|
|
12
|
+
*
|
|
13
|
+
* Two rules here are load-bearing, both carried over from the bespoke cache this replaces
|
|
14
|
+
* (`98c4dda1:filer/sdk/sec-client.ts`), both learned the hard way:
|
|
15
|
+
*
|
|
16
|
+
* 1. VALIDATE BEFORE WRITING. A response that can't be read back — an unparseable body, a
|
|
17
|
+
* non-finite TTL — must never reach disk. A permanently-cached entry has no self-healing path
|
|
18
|
+
* short of hand-deleting a hash-named file.
|
|
19
|
+
* 2. ATOMIC WRITE, UNIQUE TEMP NAME. Write-then-rename, with a temp name unique per write. A
|
|
20
|
+
* DETERMINISTIC temp name (`${final}.building`) made two clients writing one URL collide: the
|
|
21
|
+
* first `rename()` moved the shared temp file away and the second got a raw `ENOENT` for a
|
|
22
|
+
* response that had already succeeded (reproduced 6/6), and at multi-MB bodies the two writers'
|
|
23
|
+
* bytes interleaved into a corrupt-but-parseable entry.
|
|
24
|
+
*/
|
|
25
|
+
import { type AxiosStorage, type NotEmptyStorageValue } from "axios-cache-interceptor";
|
|
26
|
+
import { type IRuntimeLogger } from "../logging/index.ts";
|
|
27
|
+
/**
|
|
28
|
+
* Options for {@linkcode buildDiskStorage}.
|
|
29
|
+
*/
|
|
30
|
+
export interface DiskStorageOptions {
|
|
31
|
+
/**
|
|
32
|
+
* The directory cache entries live in. Created on first write (recursively).
|
|
33
|
+
*/
|
|
34
|
+
directory: string;
|
|
35
|
+
/**
|
|
36
|
+
* An additional, domain-specific gate run against every entry BEFORE it is written. Return `false` (or throw) to drop
|
|
37
|
+
* the write; the entry is removed rather than persisted, so the next request re-fetches.
|
|
38
|
+
*
|
|
39
|
+
* This is the seam for "a 200 whose body isn't what this API is supposed to return". Some upstreams (SEC EDGAR among
|
|
40
|
+
* them) serve an HTML error page with a 200 status; persisting one under a permanent TTL poisons that URL forever.
|
|
41
|
+
* The structural checks below (serializable, finite `createdAt`/`ttl`) always run regardless.
|
|
42
|
+
*/
|
|
43
|
+
validate?: (value: NotEmptyStorageValue) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Where rejected writes and unreadable entries are reported. Defaults to a `disk-storage`-prefixed console logger.
|
|
46
|
+
*/
|
|
47
|
+
logger?: IRuntimeLogger;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create an on-disk {@linkcode AxiosStorage}, keyed by the SHA-256 of the interceptor's cache key (which already folds
|
|
51
|
+
* in method, URL, params and body), so a filename is always a fixed-length, filesystem-safe hex digest.
|
|
52
|
+
*
|
|
53
|
+
* An in-process overlay Map sits in front of the files, and it is load-bearing for two reasons:
|
|
54
|
+
*
|
|
55
|
+
* 1. `loading` markers live there INSTEAD of on disk. That keeps the interceptor's stampede guard working (a concurrent
|
|
56
|
+
* second request for the same key sees `loading` and waits on the first) without a file write per request, and
|
|
57
|
+
* without an interrupted process leaving a `loading` marker on disk forever.
|
|
58
|
+
* 2. A value being WRITTEN stays there until its `rename` lands. Without that, `set()` clearing the `loading` marker
|
|
59
|
+
* before the file exists opens a window where the key is in neither place, and a concurrent reader gets `empty` for
|
|
60
|
+
* a response that is already in hand — measured as 3 dispatches for 3 concurrent requests to one URL, i.e. the
|
|
61
|
+
* stampede guard fully defeated.
|
|
62
|
+
*/
|
|
63
|
+
export declare function buildDiskStorage(options: DiskStorageOptions): AxiosStorage;
|
|
64
|
+
//# sourceMappingURL=disk-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disk-storage.d.ts","sourceRoot":"","sources":["../../api/disk-storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAMH,OAAO,EAAE,KAAK,YAAY,EAAgB,KAAK,oBAAoB,EAAqB,MAAM,yBAAyB,CAAA;AAEvH,OAAO,EAAiB,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGxE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,OAAO,CAAA;IACnD;;OAEG;IACH,MAAM,CAAC,EAAE,cAAc,CAAA;CACvB;AA0BD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,kBAAkB,GAAG,YAAY,CA+I1E"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file An on-disk `axios-cache-interceptor` storage adapter, so a client gets a durable HTTP cache by
|
|
6
|
+
* CONFIGURATION rather than by hand-rolling one (3b task 5).
|
|
7
|
+
*
|
|
8
|
+
* NODE ONLY, and deliberately NOT re-exported from `./index.ts`: `core/api` reaches a browser bundle
|
|
9
|
+
* (`docs`'s `DashboardMap` → `@mailwoman/cartographer` → `tiles/api.ts` → `@mailwoman/core/api`),
|
|
10
|
+
* and webpack refuses to resolve `node:fs/promises` for the web target. Import this through its own
|
|
11
|
+
* `@mailwoman/core/api/disk-storage` subpath.
|
|
12
|
+
*
|
|
13
|
+
* Two rules here are load-bearing, both carried over from the bespoke cache this replaces
|
|
14
|
+
* (`98c4dda1:filer/sdk/sec-client.ts`), both learned the hard way:
|
|
15
|
+
*
|
|
16
|
+
* 1. VALIDATE BEFORE WRITING. A response that can't be read back — an unparseable body, a
|
|
17
|
+
* non-finite TTL — must never reach disk. A permanently-cached entry has no self-healing path
|
|
18
|
+
* short of hand-deleting a hash-named file.
|
|
19
|
+
* 2. ATOMIC WRITE, UNIQUE TEMP NAME. Write-then-rename, with a temp name unique per write. A
|
|
20
|
+
* DETERMINISTIC temp name (`${final}.building`) made two clients writing one URL collide: the
|
|
21
|
+
* first `rename()` moved the shared temp file away and the second got a raw `ENOENT` for a
|
|
22
|
+
* response that had already succeeded (reproduced 6/6), and at multi-MB bodies the two writers'
|
|
23
|
+
* bytes interleaved into a corrupt-but-parseable entry.
|
|
24
|
+
*/
|
|
25
|
+
import { randomUUID } from "node:crypto";
|
|
26
|
+
import { mkdir, readFile, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
import { buildStorage } from "axios-cache-interceptor";
|
|
29
|
+
import { ConsoleLogger } from "../logging/index.js";
|
|
30
|
+
import { sha256Hex } from "../utils/hash.js";
|
|
31
|
+
/**
|
|
32
|
+
* Whether a storage value is one worth persisting. `loading` is an in-flight marker with no reusable body — it belongs
|
|
33
|
+
* in memory (see {@linkcode buildDiskStorage}'s in-process overlay), not in a file that would outlive the process that
|
|
34
|
+
* wrote it and block every later request for that key.
|
|
35
|
+
*/
|
|
36
|
+
function isPersistableState(value) {
|
|
37
|
+
return value.state !== "loading";
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The structural half of the validate-before-write rule: an entry must survive a JSON round trip WITH ITS MEANING
|
|
41
|
+
* INTACT.
|
|
42
|
+
*
|
|
43
|
+
* `createdAt` and `ttl` get an explicit finite check because `JSON.stringify(Infinity)` is the string `null`, and
|
|
44
|
+
* `null` reads back as `0` in the interceptor's `createdAt + ttl < Date.now()` expiry test. An `Infinity` TTL — the
|
|
45
|
+
* obvious way to spell "cache this immutable document forever" — would therefore round-trip into an entry that is
|
|
46
|
+
* expired the instant it is read. Rejecting it loudly beats silently caching nothing.
|
|
47
|
+
*/
|
|
48
|
+
function hasFiniteTiming(value) {
|
|
49
|
+
if (value.createdAt !== undefined && !Number.isFinite(value.createdAt))
|
|
50
|
+
return false;
|
|
51
|
+
return value.ttl === undefined || Number.isFinite(value.ttl);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create an on-disk {@linkcode AxiosStorage}, keyed by the SHA-256 of the interceptor's cache key (which already folds
|
|
55
|
+
* in method, URL, params and body), so a filename is always a fixed-length, filesystem-safe hex digest.
|
|
56
|
+
*
|
|
57
|
+
* An in-process overlay Map sits in front of the files, and it is load-bearing for two reasons:
|
|
58
|
+
*
|
|
59
|
+
* 1. `loading` markers live there INSTEAD of on disk. That keeps the interceptor's stampede guard working (a concurrent
|
|
60
|
+
* second request for the same key sees `loading` and waits on the first) without a file write per request, and
|
|
61
|
+
* without an interrupted process leaving a `loading` marker on disk forever.
|
|
62
|
+
* 2. A value being WRITTEN stays there until its `rename` lands. Without that, `set()` clearing the `loading` marker
|
|
63
|
+
* before the file exists opens a window where the key is in neither place, and a concurrent reader gets `empty` for
|
|
64
|
+
* a response that is already in hand — measured as 3 dispatches for 3 concurrent requests to one URL, i.e. the
|
|
65
|
+
* stampede guard fully defeated.
|
|
66
|
+
*/
|
|
67
|
+
export function buildDiskStorage(options) {
|
|
68
|
+
const { directory, validate } = options;
|
|
69
|
+
const logger = options.logger ?? ConsoleLogger.prefix("disk-storage");
|
|
70
|
+
/**
|
|
71
|
+
* Values visible to this process ahead of (or instead of) the files: `loading` markers, which are never persisted,
|
|
72
|
+
* and entries mid-write, which are dropped once their `rename` lands. See the function docstring.
|
|
73
|
+
*/
|
|
74
|
+
const overlay = new Map();
|
|
75
|
+
function entryPath(key) {
|
|
76
|
+
return join(directory, `${sha256Hex(key)}.json`);
|
|
77
|
+
}
|
|
78
|
+
async function removeEntry(key) {
|
|
79
|
+
overlay.delete(key);
|
|
80
|
+
try {
|
|
81
|
+
await unlink(entryPath(key));
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// A miss is the expected case for a key that was never persisted (or was already evicted).
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Serialize `value`, or return `null` when it must not reach disk. Every rejection path is logged with the key so a
|
|
89
|
+
* maintainer seeing a cache that never fills has something to grep for.
|
|
90
|
+
*/
|
|
91
|
+
function serializeIfValid(key, value) {
|
|
92
|
+
if (!hasFiniteTiming(value)) {
|
|
93
|
+
logger.warn(`Refusing to cache ${key}: createdAt/ttl must be finite (received ttl=${String(value.ttl)}).`);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
if (validate && !validate(value)) {
|
|
98
|
+
logger.warn(`Refusing to cache ${key}: rejected by the configured validate() predicate.`);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
return JSON.stringify(value);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
logger.warn(`Refusing to cache ${key}: ${error instanceof Error ? error.message : String(error)}`);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return buildStorage({
|
|
109
|
+
find: async (key) => {
|
|
110
|
+
const pending = overlay.get(key);
|
|
111
|
+
if (pending)
|
|
112
|
+
return pending;
|
|
113
|
+
let raw;
|
|
114
|
+
try {
|
|
115
|
+
raw = await readFile(entryPath(key), "utf8");
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(raw);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// A truncated or hand-edited entry is a miss, not a crash — re-fetching is always safe.
|
|
125
|
+
logger.warn(`Discarding an unreadable cache entry for ${key}.`);
|
|
126
|
+
await removeEntry(key);
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
set: async (key, value) => {
|
|
131
|
+
if (!isPersistableState(value)) {
|
|
132
|
+
overlay.set(key, value);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const serialized = serializeIfValid(key, value);
|
|
136
|
+
if (serialized === null) {
|
|
137
|
+
// Drop any older entry too: the interceptor is telling us this key's content just changed,
|
|
138
|
+
// and keeping a superseded body would be worse than a miss.
|
|
139
|
+
await removeEntry(key);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// Publish to the overlay BEFORE the write and clear it only once the rename has landed, so the
|
|
143
|
+
// key is continuously visible: the `loading` marker is replaced by the real value in the same
|
|
144
|
+
// synchronous step, never by a gap.
|
|
145
|
+
overlay.set(key, value);
|
|
146
|
+
const finalPath = entryPath(key);
|
|
147
|
+
// Unique per write — `process.pid` separates processes, `randomUUID()` separates concurrent
|
|
148
|
+
// writes inside one. A deterministic name here is the ENOENT/interleaving bug in the file header.
|
|
149
|
+
const buildingPath = `${finalPath}.${process.pid}.${randomUUID()}.building`;
|
|
150
|
+
try {
|
|
151
|
+
await mkdir(directory, { recursive: true });
|
|
152
|
+
await writeFile(buildingPath, serialized);
|
|
153
|
+
await rename(buildingPath, finalPath);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
// A CACHE WRITE IS A SIDE EFFECT OF A SUCCESSFUL REQUEST, AND A FAILED SIDE EFFECT MUST NOT
|
|
157
|
+
// FAIL THE REQUEST.
|
|
158
|
+
//
|
|
159
|
+
// `axios-cache-interceptor` awaits `set()` inside its response `onFulfilled`, so throwing
|
|
160
|
+
// from here rejects a request whose HTTP response ALREADY SUCCEEDED — the body is discarded.
|
|
161
|
+
// Worse, it escapes as a bare `Error`: no `status`, so `isTransientResourceError` reads it as
|
|
162
|
+
// FALSE and a caller following the documented contract is told never to retry. ANY
|
|
163
|
+
// filesystem failure does this: `EACCES` on a directory whose mode changed (reproduced with
|
|
164
|
+
// a `0o500` parent, which also showed three concurrent gets yielding one rejection and two
|
|
165
|
+
// successes for the SAME response), `EMFILE` under a concurrent crawl, a rename race, a
|
|
166
|
+
// transient I/O error. Not being able to cache is a cache miss.
|
|
167
|
+
logger.warn(`Could not persist ${key} (continuing as a cache miss): ${error instanceof Error ? error.message : String(error)}`);
|
|
168
|
+
// Best-effort cleanup of the temp file, if the failure came after it was created.
|
|
169
|
+
await unlink(buildingPath).catch(() => undefined);
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
overlay.delete(key);
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
remove: removeEntry,
|
|
176
|
+
clear: async () => {
|
|
177
|
+
overlay.clear();
|
|
178
|
+
// Recreated rather than left absent: "the cache is empty" and "the cache directory vanished"
|
|
179
|
+
// are different states to anything inspecting the data root, and only the first is intended.
|
|
180
|
+
await rm(directory, { recursive: true, force: true });
|
|
181
|
+
await mkdir(directory, { recursive: true });
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=disk-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disk-storage.js","sourceRoot":"","sources":["../../api/disk-storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACjF,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAqB,YAAY,EAAgD,MAAM,yBAAyB,CAAA;AAEvH,OAAO,EAAE,aAAa,EAAuB,MAAM,qBAAqB,CAAA;AACxE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AC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CACtD,EAAE,CACF,CAAA;gBAED,kFAAkF;gBAClF,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;YAClD,CAAC;oBAAS,CAAC;gBACV,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACpB,CAAC;QACF,CAAC;QAED,MAAM,EAAE,WAAW;QAEnB,KAAK,EAAE,KAAK,IAAI,EAAE;YACjB,OAAO,CAAC,KAAK,EAAE,CAAA;YAEf,6FAA6F;YAC7F,6FAA6F;YAC7F,MAAM,EAAE,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;YACrD,MAAM,KAAK,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;QAC5C,CAAC;KACD,CAAC,CAAA;AACH,CAAC"}
|
package/out/api/index.d.ts
CHANGED
package/out/api/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA"}
|
package/out/api/index.js
CHANGED
|
@@ -6,6 +6,12 @@
|
|
|
6
6
|
* Core API utilities.
|
|
7
7
|
*/
|
|
8
8
|
export * from "./APIClient.js";
|
|
9
|
+
export * from "./clock.js";
|
|
9
10
|
export * from "./headless.js";
|
|
11
|
+
export * from "./pacer.js";
|
|
10
12
|
export * from "./responses.js";
|
|
13
|
+
export * from "./retry.js";
|
|
14
|
+
// NOT `./disk-storage.ts` — it imports `node:fs/promises`, and this barrel reaches a browser bundle
|
|
15
|
+
// (docs' DashboardMap → @mailwoman/cartographer → tiles/api.ts → @mailwoman/core/api). Import it from
|
|
16
|
+
// its own `@mailwoman/core/api/disk-storage` subpath.
|
|
11
17
|
//# sourceMappingURL=index.js.map
|
package/out/api/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAE1B,oGAAoG;AACpG,sGAAsG;AACtG,sDAAsD"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file Strict minimum-interval request pacing for {@linkcode APIClient}.
|
|
6
|
+
*
|
|
7
|
+
* Lifted verbatim (modulo the SEC-specific rate clamp, which now lives at the SEC client's own call
|
|
8
|
+
* site) from `98c4dda1:filer/sdk/sec-client.ts`, where the design was settled over two review rounds
|
|
9
|
+
* and verified across eight arrival shapes — fan-out, interleaved, serial-plus-fanout, multi-burst,
|
|
10
|
+
* mid-refill, idle-then-burst, and a 60-call run at 50/s — under an injected clock.
|
|
11
|
+
*/
|
|
12
|
+
import { type ClockLike } from "./clock.ts";
|
|
13
|
+
/**
|
|
14
|
+
* A strict-interval request pacer: grants are spaced AT LEAST `intervalMs` apart, with NO burst allowance beyond the
|
|
15
|
+
* very first grant. {@linkcode RequestPacer.acquire} resolves immediately for the first call after construction (or
|
|
16
|
+
* after any idle gap), and every call within one interval of the previous grant waits out the remainder of it.
|
|
17
|
+
*
|
|
18
|
+
* WHY NOT A TOKEN BUCKET: a token bucket with capacity C admits up to `C + rate * 1s` requests within any sliding
|
|
19
|
+
* one-second window — there is NO non-zero capacity that honors a FLAT rate cap, only an average one. The bucket this
|
|
20
|
+
* replaced started full (a one-time startup burst, by design) but ALSO refilled to full after any idle gap, so a fresh
|
|
21
|
+
* burst recurred every time a crawl paused and resumed — measured at 20 grants inside one 1000ms window against a 10/s
|
|
22
|
+
* ceiling (2x). Upstreams that publish a flat rate (SEC EDGAR's 10 req/s, verified enforced) and actively police it
|
|
23
|
+
* need the FLAT guarantee, at the cost of the first-call-of-a-burst latency a bucket would have avoided.
|
|
24
|
+
*
|
|
25
|
+
* FAIRNESS (deliberately not addressed): under concurrent contention (N callers racing `acquire()` in the same
|
|
26
|
+
* synchronous turn), grants are issued in whatever order the synchronous reservation happens to run in. This never
|
|
27
|
+
* affects the RATE — every grant is still exactly `intervalMs` after the last — only which specific caller waits how
|
|
28
|
+
* long under backlog. A caller needing FIFO fairness would need a real queue on top of this.
|
|
29
|
+
*/
|
|
30
|
+
export declare class RequestPacer {
|
|
31
|
+
#private;
|
|
32
|
+
/**
|
|
33
|
+
* @param intervalMs Minimum milliseconds between two grants. Values `<= 0` are rejected — a zero-interval pacer would
|
|
34
|
+
* never actually pace, and silently accepting one would make a misconfigured caller look throttled when it isn't.
|
|
35
|
+
* @param clock Time source. Defaults to {@linkcode systemClock}.
|
|
36
|
+
*/
|
|
37
|
+
constructor(intervalMs: number, clock?: ClockLike);
|
|
38
|
+
/**
|
|
39
|
+
* The configured minimum spacing between grants, in milliseconds.
|
|
40
|
+
*/
|
|
41
|
+
get intervalMs(): number;
|
|
42
|
+
/**
|
|
43
|
+
* Resolve once this call's turn comes up: immediately for the first call (or after any idle gap), or exactly
|
|
44
|
+
* `intervalMs` after the previous grant otherwise.
|
|
45
|
+
*
|
|
46
|
+
* The grant time is reserved SYNCHRONOUSLY, before any `await` — `#nextGrantAt` is read AND bumped in the same
|
|
47
|
+
* synchronous step that computes this call's own wait. This is load-bearing, not cosmetic: moving the `#nextGrantAt`
|
|
48
|
+
* update to after the `await` (i.e. "compute the wait, sleep, then update state") reopens the concurrency bug this
|
|
49
|
+
* pacer exists to close — N callers invoked in the same synchronous turn would all read the SAME stale `#nextGrantAt`
|
|
50
|
+
* before any of them updates it, compute the SAME wait, and all be released together instead of one interval apart.
|
|
51
|
+
*
|
|
52
|
+
* `Math.max(#nextGrantAt, now)` is equally load-bearing: without it, a long idle gap leaves `#nextGrantAt` stuck in
|
|
53
|
+
* the past, and every call after the idle would compute a negative/zero wait forever (the increment-by-one-interval
|
|
54
|
+
* never catches up to a `now` that's run far ahead) — pacing would silently stop working after any idle period.
|
|
55
|
+
*
|
|
56
|
+
* Both are mutation-proved in `pacer.test.ts`.
|
|
57
|
+
*
|
|
58
|
+
* REAL-CLOCK CAVEAT, measured rather than assumed: the guarantee is on the CLOCK's timeline. Grant instants are
|
|
59
|
+
* scheduled exactly `intervalMs` apart, but the continuation that actually issues the request runs whenever the event
|
|
60
|
+
* loop gets to it, which on real timers is 0-2ms after the deadline. A grant that lands 1ms late shifts toward the
|
|
61
|
+
* following window, so a sliding-second count over the OBSERVED dispatch times reads 11 rather than 10 at a 100ms
|
|
62
|
+
* interval (measured 3/3 runs of a 40-call fan-out; the preceding second correspondingly holds 9, and the long-run
|
|
63
|
+
* rate is exactly at the cap). A caller that needs a hard sliding-window ceiling with no jitter headroom should pace
|
|
64
|
+
* fractionally under the published rate rather than exactly at it.
|
|
65
|
+
*/
|
|
66
|
+
acquire(): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=pacer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pacer.d.ts","sourceRoot":"","sources":["../../api/pacer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAe,MAAM,YAAY,CAAA;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,YAAY;;IAKxB;;;;OAIG;gBACS,UAAU,EAAE,MAAM,EAAE,KAAK,GAAE,SAAuB;IAa9D;;OAEG;IACH,IAAW,UAAU,IAAI,MAAM,CAE9B;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAYrC"}
|
package/out/api/pacer.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file Strict minimum-interval request pacing for {@linkcode APIClient}.
|
|
6
|
+
*
|
|
7
|
+
* Lifted verbatim (modulo the SEC-specific rate clamp, which now lives at the SEC client's own call
|
|
8
|
+
* site) from `98c4dda1:filer/sdk/sec-client.ts`, where the design was settled over two review rounds
|
|
9
|
+
* and verified across eight arrival shapes — fan-out, interleaved, serial-plus-fanout, multi-burst,
|
|
10
|
+
* mid-refill, idle-then-burst, and a 60-call run at 50/s — under an injected clock.
|
|
11
|
+
*/
|
|
12
|
+
import { systemClock } from "./clock.js";
|
|
13
|
+
/**
|
|
14
|
+
* A strict-interval request pacer: grants are spaced AT LEAST `intervalMs` apart, with NO burst allowance beyond the
|
|
15
|
+
* very first grant. {@linkcode RequestPacer.acquire} resolves immediately for the first call after construction (or
|
|
16
|
+
* after any idle gap), and every call within one interval of the previous grant waits out the remainder of it.
|
|
17
|
+
*
|
|
18
|
+
* WHY NOT A TOKEN BUCKET: a token bucket with capacity C admits up to `C + rate * 1s` requests within any sliding
|
|
19
|
+
* one-second window — there is NO non-zero capacity that honors a FLAT rate cap, only an average one. The bucket this
|
|
20
|
+
* replaced started full (a one-time startup burst, by design) but ALSO refilled to full after any idle gap, so a fresh
|
|
21
|
+
* burst recurred every time a crawl paused and resumed — measured at 20 grants inside one 1000ms window against a 10/s
|
|
22
|
+
* ceiling (2x). Upstreams that publish a flat rate (SEC EDGAR's 10 req/s, verified enforced) and actively police it
|
|
23
|
+
* need the FLAT guarantee, at the cost of the first-call-of-a-burst latency a bucket would have avoided.
|
|
24
|
+
*
|
|
25
|
+
* FAIRNESS (deliberately not addressed): under concurrent contention (N callers racing `acquire()` in the same
|
|
26
|
+
* synchronous turn), grants are issued in whatever order the synchronous reservation happens to run in. This never
|
|
27
|
+
* affects the RATE — every grant is still exactly `intervalMs` after the last — only which specific caller waits how
|
|
28
|
+
* long under backlog. A caller needing FIFO fairness would need a real queue on top of this.
|
|
29
|
+
*/
|
|
30
|
+
export class RequestPacer {
|
|
31
|
+
#intervalMs;
|
|
32
|
+
#clock;
|
|
33
|
+
#nextGrantAt;
|
|
34
|
+
/**
|
|
35
|
+
* @param intervalMs Minimum milliseconds between two grants. Values `<= 0` are rejected — a zero-interval pacer would
|
|
36
|
+
* never actually pace, and silently accepting one would make a misconfigured caller look throttled when it isn't.
|
|
37
|
+
* @param clock Time source. Defaults to {@linkcode systemClock}.
|
|
38
|
+
*/
|
|
39
|
+
constructor(intervalMs, clock = systemClock) {
|
|
40
|
+
if (!Number.isFinite(intervalMs) || intervalMs <= 0) {
|
|
41
|
+
throw new RangeError(`RequestPacer: intervalMs must be a finite number greater than 0 (received ${intervalMs}). A pacer with a ` +
|
|
42
|
+
"zero or non-finite interval cannot enforce a rate; construct one only when you actually want pacing.");
|
|
43
|
+
}
|
|
44
|
+
this.#intervalMs = intervalMs;
|
|
45
|
+
this.#clock = clock;
|
|
46
|
+
this.#nextGrantAt = clock.now();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The configured minimum spacing between grants, in milliseconds.
|
|
50
|
+
*/
|
|
51
|
+
get intervalMs() {
|
|
52
|
+
return this.#intervalMs;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Resolve once this call's turn comes up: immediately for the first call (or after any idle gap), or exactly
|
|
56
|
+
* `intervalMs` after the previous grant otherwise.
|
|
57
|
+
*
|
|
58
|
+
* The grant time is reserved SYNCHRONOUSLY, before any `await` — `#nextGrantAt` is read AND bumped in the same
|
|
59
|
+
* synchronous step that computes this call's own wait. This is load-bearing, not cosmetic: moving the `#nextGrantAt`
|
|
60
|
+
* update to after the `await` (i.e. "compute the wait, sleep, then update state") reopens the concurrency bug this
|
|
61
|
+
* pacer exists to close — N callers invoked in the same synchronous turn would all read the SAME stale `#nextGrantAt`
|
|
62
|
+
* before any of them updates it, compute the SAME wait, and all be released together instead of one interval apart.
|
|
63
|
+
*
|
|
64
|
+
* `Math.max(#nextGrantAt, now)` is equally load-bearing: without it, a long idle gap leaves `#nextGrantAt` stuck in
|
|
65
|
+
* the past, and every call after the idle would compute a negative/zero wait forever (the increment-by-one-interval
|
|
66
|
+
* never catches up to a `now` that's run far ahead) — pacing would silently stop working after any idle period.
|
|
67
|
+
*
|
|
68
|
+
* Both are mutation-proved in `pacer.test.ts`.
|
|
69
|
+
*
|
|
70
|
+
* REAL-CLOCK CAVEAT, measured rather than assumed: the guarantee is on the CLOCK's timeline. Grant instants are
|
|
71
|
+
* scheduled exactly `intervalMs` apart, but the continuation that actually issues the request runs whenever the event
|
|
72
|
+
* loop gets to it, which on real timers is 0-2ms after the deadline. A grant that lands 1ms late shifts toward the
|
|
73
|
+
* following window, so a sliding-second count over the OBSERVED dispatch times reads 11 rather than 10 at a 100ms
|
|
74
|
+
* interval (measured 3/3 runs of a 40-call fan-out; the preceding second correspondingly holds 9, and the long-run
|
|
75
|
+
* rate is exactly at the cap). A caller that needs a hard sliding-window ceiling with no jitter headroom should pace
|
|
76
|
+
* fractionally under the published rate rather than exactly at it.
|
|
77
|
+
*/
|
|
78
|
+
async acquire() {
|
|
79
|
+
const now = this.#clock.now();
|
|
80
|
+
const grantAt = Math.max(this.#nextGrantAt, now);
|
|
81
|
+
this.#nextGrantAt = grantAt + this.#intervalMs;
|
|
82
|
+
const waitMs = grantAt - now;
|
|
83
|
+
if (waitMs > 0) {
|
|
84
|
+
await this.#clock.sleep(Math.ceil(waitMs));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=pacer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pacer.js","sourceRoot":"","sources":["../../api/pacer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAkB,WAAW,EAAE,MAAM,YAAY,CAAA;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,YAAY;IACf,WAAW,CAAQ;IACnB,MAAM,CAAW;IAC1B,YAAY,CAAQ;IAEpB;;;;OAIG;IACH,YAAY,UAAkB,EAAE,QAAmB,WAAW;QAC7D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;YACrD,MAAM,IAAI,UAAU,CACnB,6EAA6E,UAAU,oBAAoB;gBAC1G,sGAAsG,CACvG,CAAA;QACF,CAAC;QAED,IAAI,CAAC,WAAW,GAAG,UAAU,CAAA;QAC7B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE,CAAA;IAChC,CAAC;IAED;;OAEG;IACH,IAAW,UAAU;QACpB,OAAO,IAAI,CAAC,WAAW,CAAA;IACxB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACI,KAAK,CAAC,OAAO;QACnB,MAAM,GAAG,GAAG,IA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