@cratestack/link-batch 0.5.2 → 0.6.1
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 +24 -5
- package/dist/correlate.d.ts +10 -0
- package/dist/correlate.d.ts.map +1 -0
- package/dist/correlate.js +79 -0
- package/dist/correlate.js.map +1 -0
- package/dist/dispatch.d.ts +8 -0
- package/dist/dispatch.d.ts.map +1 -0
- package/dist/dispatch.js +46 -0
- package/dist/dispatch.js.map +1 -0
- package/dist/index.d.ts +18 -42
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -126
- package/dist/index.js.map +1 -1
- package/dist/signature.d.ts +25 -0
- package/dist/signature.d.ts.map +1 -0
- package/dist/signature.js +100 -0
- package/dist/signature.js.map +1 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -33,21 +33,40 @@ const [a, b, c] = await Promise.all([
|
|
|
33
33
|
|
|
34
34
|
- **Window**: defaults to `queueMicrotask` — calls fired synchronously in the same tick (e.g.
|
|
35
35
|
inside `Promise.all`) collapse. Pass `windowMs` to widen the window across ticks.
|
|
36
|
+
- **Partitioning** (fixed in [#273](https://github.com/cratestack/cratestack/issues/273); versions
|
|
37
|
+
before this carried a bug here — see "Known limitations" below): each flush is split into
|
|
38
|
+
partitions by transport envelope — headers (excluding `Idempotency-Key`, which is carried
|
|
39
|
+
per-frame, not per-request), `fetch` reference, codec reference, and the resolved batch URL —
|
|
40
|
+
and every partition is sent as its own `POST /rpc/batch`. Calls that share an envelope, the
|
|
41
|
+
overwhelmingly common case, still collapse into exactly one request; calls that don't (e.g. two
|
|
42
|
+
different `Authorization` headers) each keep their own, in a separate request, rather than one
|
|
43
|
+
silently overwriting the other's.
|
|
44
|
+
- **Aggregate headers**: pass `headers` to `createBatchLink({ headers })` to declare headers that
|
|
45
|
+
every synthesized `/rpc/batch` request carries, merged **over** each partition's own — a
|
|
46
|
+
same-named per-call header is overridden by this value, not the other way around. Use this for
|
|
47
|
+
service-level headers (e.g. an API key) rather than per-tenant auth, which should come from
|
|
48
|
+
per-call headers that drive partitioning.
|
|
49
|
+
- **`maxBatchSize`** is enforced **per partition**, not globally across the flush — a single
|
|
50
|
+
oversized partition chunks into several concurrent requests; it never borrows headroom from a
|
|
51
|
+
different partition.
|
|
36
52
|
- **Dedup**: only calls sharing an explicit `idempotencyKey` are collapsed into one request frame
|
|
37
53
|
by default — that's already the caller's own signal that the call is a safe repeat. Calls with
|
|
38
54
|
no idempotency key are never auto-collapsed, since the server does no dedup of its own and
|
|
39
55
|
silently merging two textually-identical but unmarked mutations would be unsafe. Pass a custom
|
|
40
|
-
`dedupe` for full value-based collapsing, or `() => null` to disable dedup and only batch.
|
|
56
|
+
`dedupe` for full value-based collapsing, or `() => null` to disable dedup and only batch. Dedup
|
|
57
|
+
runs *within* a partition — it never collapses calls across two different envelopes.
|
|
41
58
|
- **Correlation**: results are fanned back out by matching each response frame's `id`, not array
|
|
42
59
|
position — the server's `/rpc/batch` contract already guarantees order (see
|
|
43
60
|
`docs/design/rpc-transport.md` §3.2), but id-based matching is strictly more robust and mirrors
|
|
44
61
|
the repo's own Rust client-side batch debouncer (`examples/rpc-batch-debounce`).
|
|
62
|
+
- **Failure isolation**: a partition that fails (network error, non-OK response) only rejects the
|
|
63
|
+
callers queued in *that* partition — it never affects a concurrently in-flight partition.
|
|
45
64
|
|
|
46
65
|
### Known limitations
|
|
47
66
|
|
|
48
|
-
- The synthesized `/rpc/batch` request reuses the **first** queued call's `headers`/`fetchFn`/
|
|
49
|
-
`codec` for the whole flush — per-call custom headers on later calls in the same window are not
|
|
50
|
-
applied to the aggregate request. Pass shared headers via the runtime's own `headers` option
|
|
51
|
-
rather than per-call `CratestackRpcCallOptions.headers` when using this link.
|
|
52
67
|
- Aborting an individual call's `AbortSignal` only cancels it if its batch hasn't been sent yet —
|
|
53
68
|
it does not cancel an in-flight `/rpc/batch` request.
|
|
69
|
+
- **(Fixed in #273, kept here for anyone reading an older version's docs)** Before #273, the
|
|
70
|
+
synthesized `/rpc/batch` request reused the *first* queued call's `headers`/`fetchFn`/`codec` for
|
|
71
|
+
the whole flush — per-call custom headers on later calls in the same window were silently dropped
|
|
72
|
+
from the aggregate request instead of applied. This is why partitioning exists now.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RpcLinkRequest, RpcResponseFrame } from "@cratestack/ts-types";
|
|
2
|
+
import type { Group, QueueEntry } from "./types.js";
|
|
3
|
+
/** Collapses entries that share a non-null dedupe key into one frame.
|
|
4
|
+
* Runs *within* a partition — this is frame-level dedup, distinct from
|
|
5
|
+
* the request-level split in `./signature`. */
|
|
6
|
+
export declare function groupByDedupeKey(entries: QueueEntry[], dedupe: (request: RpcLinkRequest) => string | null): Group[];
|
|
7
|
+
/** Fans a decoded `/rpc/batch` response back out to each queued caller. */
|
|
8
|
+
export declare function resolveGroups(groups: Group[], frames: RpcResponseFrame[]): void;
|
|
9
|
+
export declare function errorStatus(code: string): number;
|
|
10
|
+
//# sourceMappingURL=correlate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlate.d.ts","sourceRoot":"","sources":["../src/correlate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,cAAc,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3F,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEpD;;gDAEgD;AAChD,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,UAAU,EAAE,EACrB,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,MAAM,GAAG,IAAI,GACjD,KAAK,EAAE,CAkBT;AAED,2EAA2E;AAC3E,wBAAgB,aAAa,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,gBAAgB,EAAE,GAAG,IAAI,CAqC/E;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAgBhD"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** Collapses entries that share a non-null dedupe key into one frame.
|
|
2
|
+
* Runs *within* a partition — this is frame-level dedup, distinct from
|
|
3
|
+
* the request-level split in `./signature`. */
|
|
4
|
+
export function groupByDedupeKey(entries, dedupe) {
|
|
5
|
+
const groups = [];
|
|
6
|
+
const indexByKey = new Map();
|
|
7
|
+
for (const entry of entries) {
|
|
8
|
+
const key = dedupe(entry.request);
|
|
9
|
+
if (key === null) {
|
|
10
|
+
groups.push({ key: null, entries: [entry] });
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const existingIndex = indexByKey.get(key);
|
|
14
|
+
if (existingIndex === undefined) {
|
|
15
|
+
indexByKey.set(key, groups.length);
|
|
16
|
+
groups.push({ key, entries: [entry] });
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
groups[existingIndex].entries.push(entry);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return groups;
|
|
23
|
+
}
|
|
24
|
+
/** Fans a decoded `/rpc/batch` response back out to each queued caller. */
|
|
25
|
+
export function resolveGroups(groups, frames) {
|
|
26
|
+
// Correlate by `id`, not array position — the server contract
|
|
27
|
+
// guarantees order (docs/design/rpc-transport.md §3.2), but matching
|
|
28
|
+
// by id is strictly more robust and is the pattern the repo's own
|
|
29
|
+
// Rust client-side batch debouncer already establishes
|
|
30
|
+
// (examples/rpc-batch-debounce/src/lib.rs's `responders` map).
|
|
31
|
+
const frameById = new Map(frames.map((frame) => [frame.id, frame]));
|
|
32
|
+
groups.forEach((group, id) => {
|
|
33
|
+
const frame = frameById.get(id);
|
|
34
|
+
if (!frame) {
|
|
35
|
+
const error = new Error(`batch response is missing frame id ${id}`);
|
|
36
|
+
for (const entry of group.entries) {
|
|
37
|
+
entry.reject(error);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const hasError = frame.error !== undefined;
|
|
42
|
+
for (const entry of group.entries) {
|
|
43
|
+
if (!hasError && frame.output === undefined) {
|
|
44
|
+
// Mirrors the unary path's `response.status === 204` shortcut
|
|
45
|
+
// (void-returning calls resolve `undefined`, not a decoded null).
|
|
46
|
+
entry.resolve({ response: new Response(null, { status: 204 }) });
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const body = hasError ? frame.error : frame.output;
|
|
50
|
+
// Re-encoded with the *caller's own* codec, not the batch's — the
|
|
51
|
+
// caller's generated runtime decodes this synthetic response with
|
|
52
|
+
// the codec it was constructed with, which an `options.codec`
|
|
53
|
+
// override on this link must not change.
|
|
54
|
+
entry.resolve({
|
|
55
|
+
response: new Response(entry.request.codec.encode(body), {
|
|
56
|
+
status: hasError ? errorStatus(frame.error.code) : 200,
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export function errorStatus(code) {
|
|
63
|
+
switch (code) {
|
|
64
|
+
case "invalid_argument":
|
|
65
|
+
case "failed_precondition":
|
|
66
|
+
return 400;
|
|
67
|
+
case "unauthenticated":
|
|
68
|
+
return 401;
|
|
69
|
+
case "permission_denied":
|
|
70
|
+
return 403;
|
|
71
|
+
case "not_found":
|
|
72
|
+
return 404;
|
|
73
|
+
case "conflict":
|
|
74
|
+
return 409;
|
|
75
|
+
default:
|
|
76
|
+
return 500;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=correlate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlate.js","sourceRoot":"","sources":["../src/correlate.ts"],"names":[],"mappings":"AAGA;;gDAEgD;AAChD,MAAM,UAAU,gBAAgB,CAC9B,OAAqB,EACrB,MAAkD;IAElD,MAAM,MAAM,GAAY,EAAE,CAAC;IAC3B,MAAM,UAAU,GAAG,IAAI,GAAG,EAAk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OAAO,GAAG,CAAC;QACb,KAAK,mBAAmB;YACtB,OAAO,GAAG,CAAC;QACb,KAAK,WAAW;YACd,OAAO,GAAG,CAAC;QACb,KAAK,UAAU;YACb,OAAO,GAAG,CAAC;QACb;YACE,OAAO,GAAG,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RpcLinkRequest } from "@cratestack/ts-types";
|
|
2
|
+
import type { EffectiveConfig, QueueEntry } from "./types.js";
|
|
3
|
+
/** Sends one partition as a single `POST /rpc/batch` and settles every
|
|
4
|
+
* caller in it. A partition's entries share `config` by construction
|
|
5
|
+
* (see `./signature`), so nothing here is inherited from an arbitrary
|
|
6
|
+
* "first" call — the pre-partitioning behavior this replaces. */
|
|
7
|
+
export declare function dispatchPartition(entries: QueueEntry[], config: EffectiveConfig, dedupe: (request: RpcLinkRequest) => string | null): Promise<void>;
|
|
8
|
+
//# sourceMappingURL=dispatch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../src/dispatch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAgC,MAAM,sBAAsB,CAAC;AAEzF,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE9D;;;kEAGkE;AAClE,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,UAAU,EAAE,EACrB,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,MAAM,GAAG,IAAI,GACjD,OAAO,CAAC,IAAI,CAAC,CA0Cf"}
|
package/dist/dispatch.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { groupByDedupeKey, resolveGroups } from "./correlate.js";
|
|
2
|
+
/** Sends one partition as a single `POST /rpc/batch` and settles every
|
|
3
|
+
* caller in it. A partition's entries share `config` by construction
|
|
4
|
+
* (see `./signature`), so nothing here is inherited from an arbitrary
|
|
5
|
+
* "first" call — the pre-partitioning behavior this replaces. */
|
|
6
|
+
export async function dispatchPartition(entries, config, dedupe) {
|
|
7
|
+
const groups = groupByDedupeKey(entries, dedupe);
|
|
8
|
+
const requests = groups.map((group, id) => {
|
|
9
|
+
const request = group.entries[0].request;
|
|
10
|
+
const frame = { id, op: request.opId, input: request.input };
|
|
11
|
+
if (request.idempotencyKey !== undefined) {
|
|
12
|
+
frame.idem = request.idempotencyKey;
|
|
13
|
+
}
|
|
14
|
+
return frame;
|
|
15
|
+
});
|
|
16
|
+
try {
|
|
17
|
+
const response = await config.fetchFn(config.batchUrl, {
|
|
18
|
+
method: "POST",
|
|
19
|
+
headers: config.headers,
|
|
20
|
+
body: config.codec.encode(requests),
|
|
21
|
+
signal: null,
|
|
22
|
+
});
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
const bytes = new Uint8Array(await response.arrayBuffer().catch(() => new ArrayBuffer(0)));
|
|
25
|
+
for (const group of groups) {
|
|
26
|
+
for (const entry of group.entries) {
|
|
27
|
+
entry.resolve({ response: new Response(bytes, { status: response.status }) });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
33
|
+
const frames = config.codec.decode(bytes);
|
|
34
|
+
resolveGroups(groups, frames);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
// Scoped to this partition only — a failure here never settles a
|
|
38
|
+
// caller queued under a different transport config.
|
|
39
|
+
for (const group of groups) {
|
|
40
|
+
for (const entry of group.entries) {
|
|
41
|
+
entry.reject(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=dispatch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch.js","sourceRoot":"","sources":["../src/dispatch.ts"],"names":[],"mappings":"AACA,OA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K,CAAC,OAAO,EAAE,CAAC;oBAClC,KAAK,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,IAAI,QAAQ,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;gBAChF,CAAC;YACH,CAAC;YACD,OAAO;QACT,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;QAC3D,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAuB,CAAC;QAChE,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,iEAAiE;QACjE,oDAAoD;QACpD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;gBAClC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,51 +1,27 @@
|
|
|
1
|
-
import type { RpcLink
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* collapse into one `/rpc/batch` request. Pass a millisecond value to
|
|
6
|
-
* widen the window across ticks (e.g. debounce bursts from unrelated
|
|
7
|
-
* components). */
|
|
8
|
-
windowMs?: number;
|
|
9
|
-
/** Maximum requests per flushed `/rpc/batch` call. Default: unbounded
|
|
10
|
-
* — the server enforces no size limit either (see
|
|
11
|
-
* `docs/design/rpc-transport.md` §3.2), but a huge fan-in may still
|
|
12
|
-
* be worth capping client-side. */
|
|
13
|
-
maxBatchSize?: number;
|
|
14
|
-
/** Returns a dedupe key for a queued call, or `null` to never
|
|
15
|
-
* collapse it with anything else. Two calls in the same flush that
|
|
16
|
-
* return the same non-null key become ONE `/rpc/batch` frame, and
|
|
17
|
-
* every caller resolves from that single result.
|
|
18
|
-
*
|
|
19
|
-
* Default: only collapse calls that share an explicit
|
|
20
|
-
* `idempotencyKey` — that's already the caller's own signal that the
|
|
21
|
-
* call is safe to treat as a repeat. Calls with no idempotency key
|
|
22
|
-
* are never auto-collapsed, since the server does no dedup of its
|
|
23
|
-
* own and silently merging two textually-identical but unmarked
|
|
24
|
-
* mutations would be unsafe. Pass a custom `dedupe` for full
|
|
25
|
-
* value-based batshit-style collapsing (e.g.
|
|
26
|
-
* `req => \`${req.opId}:${JSON.stringify(req.input)}\``), or
|
|
27
|
-
* `() => null` to disable dedup entirely and only batch.
|
|
28
|
-
*/
|
|
29
|
-
dedupe?: (request: RpcLinkRequest) => string | null;
|
|
30
|
-
}
|
|
1
|
+
import type { RpcLink } from "@cratestack/ts-types";
|
|
2
|
+
import type { BatchLinkOptions } from "./types.js";
|
|
3
|
+
export { batchSignature, effectiveConfig } from "./signature.js";
|
|
4
|
+
export type { BatchLinkOptions } from "./types.js";
|
|
31
5
|
/** A batshit-style (github.com/yornaath/batshit) automatic batch
|
|
32
6
|
* scheduler, shipped as an `RpcLink` (issue #182's composition
|
|
33
7
|
* mechanism) rather than a `fetch` override — so it composes with a
|
|
34
8
|
* logger, retry, or auth-refresh link instead of clobbering them.
|
|
35
9
|
*
|
|
36
10
|
* Terminal in practice: for `kind: "unary"` calls it never invokes
|
|
37
|
-
* `next` — it queues the call and later performs its own
|
|
38
|
-
* `POST /rpc/batch
|
|
39
|
-
* "batch"`) passes straight through via `next`, since it's
|
|
40
|
-
* the shape this link would otherwise build.
|
|
11
|
+
* `next` — it queues the call and later performs its own
|
|
12
|
+
* `POST /rpc/batch` requests. An explicit `runtime.batch()` call
|
|
13
|
+
* (`kind: "batch"`) passes straight through via `next`, since it's
|
|
14
|
+
* already the shape this link would otherwise build.
|
|
41
15
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
16
|
+
* Each flush is split into partitions by transport config — headers,
|
|
17
|
+
* `fetch`, codec, batch URL (see `batchSignature`) — and every
|
|
18
|
+
* partition is sent as its own request, so no call's headers are ever
|
|
19
|
+
* dropped in favor of another's (issue #273). Calls sharing a config,
|
|
20
|
+
* which is the overwhelmingly common case, still collapse into exactly
|
|
21
|
+
* one request.
|
|
22
|
+
*
|
|
23
|
+
* Known limitation: aborting an individual call's `AbortSignal` only
|
|
24
|
+
* cancels it if the flush hasn't been sent yet — it does not cancel an
|
|
25
|
+
* in-flight batch. */
|
|
50
26
|
export declare function createBatchLink(options?: BatchLinkOptions): RpcLink;
|
|
51
27
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAmC,MAAM,sBAAsB,CAAC;AAGrF,OAAO,KAAK,EAAE,gBAAgB,EAAc,MAAM,YAAY,CAAC;AAE/D,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAKnD;;;;;;;;;;;;;;;;;;;;uBAoBuB;AACvB,wBAAgB,eAAe,CAAC,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAkEvE"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { dispatchPartition } from "./dispatch.js";
|
|
2
|
+
import { partition } from "./signature.js";
|
|
3
|
+
export { batchSignature, effectiveConfig } from "./signature.js";
|
|
1
4
|
const defaultDedupe = (request) => request.idempotencyKey !== undefined ? `idem:${request.idempotencyKey}` : null;
|
|
2
5
|
/** A batshit-style (github.com/yornaath/batshit) automatic batch
|
|
3
6
|
* scheduler, shipped as an `RpcLink` (issue #182's composition
|
|
@@ -5,21 +8,23 @@ const defaultDedupe = (request) => request.idempotencyKey !== undefined ? `idem:
|
|
|
5
8
|
* logger, retry, or auth-refresh link instead of clobbering them.
|
|
6
9
|
*
|
|
7
10
|
* Terminal in practice: for `kind: "unary"` calls it never invokes
|
|
8
|
-
* `next` — it queues the call and later performs its own
|
|
9
|
-
* `POST /rpc/batch
|
|
10
|
-
* "batch"`) passes straight through via `next`, since it's
|
|
11
|
-
* the shape this link would otherwise build.
|
|
11
|
+
* `next` — it queues the call and later performs its own
|
|
12
|
+
* `POST /rpc/batch` requests. An explicit `runtime.batch()` call
|
|
13
|
+
* (`kind: "batch"`) passes straight through via `next`, since it's
|
|
14
|
+
* already the shape this link would otherwise build.
|
|
12
15
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
16
|
+
* Each flush is split into partitions by transport config — headers,
|
|
17
|
+
* `fetch`, codec, batch URL (see `batchSignature`) — and every
|
|
18
|
+
* partition is sent as its own request, so no call's headers are ever
|
|
19
|
+
* dropped in favor of another's (issue #273). Calls sharing a config,
|
|
20
|
+
* which is the overwhelmingly common case, still collapse into exactly
|
|
21
|
+
* one request.
|
|
22
|
+
*
|
|
23
|
+
* Known limitation: aborting an individual call's `AbortSignal` only
|
|
24
|
+
* cancels it if the flush hasn't been sent yet — it does not cancel an
|
|
25
|
+
* in-flight batch. */
|
|
21
26
|
export function createBatchLink(options = {}) {
|
|
22
|
-
const maxBatchSize = options.maxBatchSize ?? Number.POSITIVE_INFINITY;
|
|
27
|
+
const maxBatchSize = Math.max(1, options.maxBatchSize ?? Number.POSITIVE_INFINITY);
|
|
23
28
|
const dedupe = options.dedupe ?? defaultDedupe;
|
|
24
29
|
const windowMs = options.windowMs;
|
|
25
30
|
const queue = [];
|
|
@@ -44,49 +49,14 @@ export function createBatchLink(options = {}) {
|
|
|
44
49
|
if (queue.length === 0) {
|
|
45
50
|
return;
|
|
46
51
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const groups = groupByDedupeKey(entries, dedupe);
|
|
56
|
-
const leader = entries[0].request;
|
|
57
|
-
const requests = groups.map((group, id) => {
|
|
58
|
-
const request = group.entries[0].request;
|
|
59
|
-
const frame = { id, op: request.opId, input: request.input };
|
|
60
|
-
if (request.idempotencyKey !== undefined) {
|
|
61
|
-
frame.idem = request.idempotencyKey;
|
|
62
|
-
}
|
|
63
|
-
return frame;
|
|
64
|
-
});
|
|
65
|
-
try {
|
|
66
|
-
const response = await leader.fetchFn(leader.urls.batch(), {
|
|
67
|
-
method: "POST",
|
|
68
|
-
headers: leader.headers,
|
|
69
|
-
body: leader.codec.encode(requests),
|
|
70
|
-
signal: null,
|
|
71
|
-
});
|
|
72
|
-
if (!response.ok) {
|
|
73
|
-
const bytes = new Uint8Array(await response.arrayBuffer().catch(() => new ArrayBuffer(0)));
|
|
74
|
-
for (const group of groups) {
|
|
75
|
-
for (const entry of group.entries) {
|
|
76
|
-
entry.resolve({ response: new Response(bytes, { status: response.status }) });
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
82
|
-
const frames = leader.codec.decode(bytes);
|
|
83
|
-
resolveGroups(groups, frames);
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
for (const group of groups) {
|
|
87
|
-
for (const entry of group.entries) {
|
|
88
|
-
entry.reject(error);
|
|
89
|
-
}
|
|
52
|
+
// Drain the whole queue, then split it. Chunks are dispatched
|
|
53
|
+
// concurrently rather than rescheduled onto later ticks: the set of
|
|
54
|
+
// requests issued is identical either way, and firing them together
|
|
55
|
+
// avoids serializing an oversized fan-in behind its own window.
|
|
56
|
+
const pending = queue.splice(0, queue.length);
|
|
57
|
+
for (const part of partition(pending, options)) {
|
|
58
|
+
for (let i = 0; i < part.entries.length; i += maxBatchSize) {
|
|
59
|
+
void dispatchPartition(part.entries.slice(i, i + maxBatchSize), part.config, dedupe);
|
|
90
60
|
}
|
|
91
61
|
}
|
|
92
62
|
}
|
|
@@ -114,74 +84,4 @@ export function createBatchLink(options = {}) {
|
|
|
114
84
|
});
|
|
115
85
|
};
|
|
116
86
|
}
|
|
117
|
-
function groupByDedupeKey(entries, dedupe) {
|
|
118
|
-
const groups = [];
|
|
119
|
-
const indexByKey = new Map();
|
|
120
|
-
for (const entry of entries) {
|
|
121
|
-
const key = dedupe(entry.request);
|
|
122
|
-
if (key === null) {
|
|
123
|
-
groups.push({ key: null, entries: [entry] });
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
const existingIndex = indexByKey.get(key);
|
|
127
|
-
if (existingIndex === undefined) {
|
|
128
|
-
indexByKey.set(key, groups.length);
|
|
129
|
-
groups.push({ key, entries: [entry] });
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
groups[existingIndex].entries.push(entry);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return groups;
|
|
136
|
-
}
|
|
137
|
-
function resolveGroups(groups, frames) {
|
|
138
|
-
// Correlate by `id`, not array position — the server contract
|
|
139
|
-
// guarantees order (docs/design/rpc-transport.md §3.2), but matching
|
|
140
|
-
// by id is strictly more robust and is the pattern the repo's own
|
|
141
|
-
// Rust client-side batch debouncer already establishes
|
|
142
|
-
// (examples/rpc-batch-debounce/src/lib.rs's `responders` map).
|
|
143
|
-
const frameById = new Map(frames.map((frame) => [frame.id, frame]));
|
|
144
|
-
groups.forEach((group, id) => {
|
|
145
|
-
const frame = frameById.get(id);
|
|
146
|
-
if (!frame) {
|
|
147
|
-
const error = new Error(`batch response is missing frame id ${id}`);
|
|
148
|
-
for (const entry of group.entries) {
|
|
149
|
-
entry.reject(error);
|
|
150
|
-
}
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
const hasError = frame.error !== undefined;
|
|
154
|
-
for (const entry of group.entries) {
|
|
155
|
-
if (!hasError && frame.output === undefined) {
|
|
156
|
-
// Mirrors the unary path's `response.status === 204` shortcut
|
|
157
|
-
// (void-returning calls resolve `undefined`, not a decoded null).
|
|
158
|
-
entry.resolve({ response: new Response(null, { status: 204 }) });
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
const body = hasError ? frame.error : frame.output;
|
|
162
|
-
entry.resolve({
|
|
163
|
-
response: new Response(entry.request.codec.encode(body), {
|
|
164
|
-
status: hasError ? errorStatus(frame.error.code) : 200,
|
|
165
|
-
}),
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
function errorStatus(code) {
|
|
171
|
-
switch (code) {
|
|
172
|
-
case "invalid_argument":
|
|
173
|
-
case "failed_precondition":
|
|
174
|
-
return 400;
|
|
175
|
-
case "unauthenticated":
|
|
176
|
-
return 401;
|
|
177
|
-
case "permission_denied":
|
|
178
|
-
return 403;
|
|
179
|
-
case "not_found":
|
|
180
|
-
return 404;
|
|
181
|
-
case "conflict":
|
|
182
|
-
return 409;
|
|
183
|
-
default:
|
|
184
|
-
return 500;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
87
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,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C,SAAS,CAAC,CAAC,CAAC;gBACzD,CAAC;YACH,CAAC,CAAC,CAAC;YAEH,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAClB,aAAa,EAAE,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { RpcLinkRequest } from "@cratestack/ts-types";
|
|
2
|
+
import type { BatchLinkOptions, EffectiveConfig, QueueEntry } from "./types.js";
|
|
3
|
+
/** Resolves the transport config a request would actually be sent with,
|
|
4
|
+
* applying any link-level overrides on top of the request's own. */
|
|
5
|
+
export declare function effectiveConfig(request: RpcLinkRequest, options: BatchLinkOptions): EffectiveConfig;
|
|
6
|
+
/** A stable key identifying which queued calls may share one
|
|
7
|
+
* `/rpc/batch` request. Two calls batch together only when every part
|
|
8
|
+
* of their transport envelope matches: headers, `fetch`, codec, and
|
|
9
|
+
* the resolved batch URL.
|
|
10
|
+
*
|
|
11
|
+
* The URL is included because nothing stops one `createBatchLink()`
|
|
12
|
+
* instance from being passed to two runtimes pointing at different
|
|
13
|
+
* origins; without it, calls to one service would be merged into a
|
|
14
|
+
* request sent to the other. */
|
|
15
|
+
export declare function batchSignature(config: EffectiveConfig): string;
|
|
16
|
+
/** Splits a flush into partitions that may each be sent as one
|
|
17
|
+
* `/rpc/batch` request. Insertion order is preserved within every
|
|
18
|
+
* partition, and partitions are returned in first-seen order, so a
|
|
19
|
+
* flush whose calls all share a config behaves exactly as it did
|
|
20
|
+
* before partitioning existed. */
|
|
21
|
+
export declare function partition(entries: QueueEntry[], options: BatchLinkOptions): {
|
|
22
|
+
config: EffectiveConfig;
|
|
23
|
+
entries: QueueEntry[];
|
|
24
|
+
}[];
|
|
25
|
+
//# sourceMappingURL=signature.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAgBhF;qEACqE;AACrE,wBAAgB,eAAe,CAC7B,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,gBAAgB,GACxB,eAAe,CAcjB;AAED;;;;;;;;iCAQiC;AACjC,wBAAgB,cAAc,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAO9D;AAoCD;;;;mCAImC;AACnC,wBAAgB,SAAS,CACvB,OAAO,EAAE,UAAU,EAAE,EACrB,OAAO,EAAE,gBAAgB,GACxB;IAAE,MAAM,EAAE,eAAe,CAAC;IAAC,OAAO,EAAE,UAAU,EAAE,CAAA;CAAE,EAAE,CAiBtD"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Carried per *frame* in the `/rpc/batch` payload (`RpcRequest.idem`),
|
|
2
|
+
// not per request — the generated runtime writes the caller's
|
|
3
|
+
// `idempotencyKey` into BOTH the per-call `Idempotency-Key` header and
|
|
4
|
+
// the frame, so including it in the signature would put every distinct
|
|
5
|
+
// key in its own partition and defeat batching entirely for exactly the
|
|
6
|
+
// calls the dedup feature exists to serve. See
|
|
7
|
+
// `rpc-runtime.ts.j2`'s `call()`.
|
|
8
|
+
const FRAME_LEVEL_HEADERS = new Set(["idempotency-key"]);
|
|
9
|
+
// Field separators that cannot appear in a header name or value, so
|
|
10
|
+
// distinct configs cannot collide into one signature string.
|
|
11
|
+
const FIELD = "\u0000";
|
|
12
|
+
const ENTRY = "\u0001";
|
|
13
|
+
/** Resolves the transport config a request would actually be sent with,
|
|
14
|
+
* applying any link-level overrides on top of the request's own. */
|
|
15
|
+
export function effectiveConfig(request, options) {
|
|
16
|
+
const headers = new Headers(request.headers);
|
|
17
|
+
if (options.headers) {
|
|
18
|
+
// `options.headers` OVERRIDES same-named request headers, not the
|
|
19
|
+
// other way around — `.set()` here, applied after seeding `headers`
|
|
20
|
+
// from the request, is what makes link-level config win.
|
|
21
|
+
new Headers(options.headers).forEach((value, key) => headers.set(key, value));
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
headers,
|
|
25
|
+
fetchFn: options.fetchFn ?? request.fetchFn,
|
|
26
|
+
codec: options.codec ?? request.codec,
|
|
27
|
+
batchUrl: request.urls.batch(),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** A stable key identifying which queued calls may share one
|
|
31
|
+
* `/rpc/batch` request. Two calls batch together only when every part
|
|
32
|
+
* of their transport envelope matches: headers, `fetch`, codec, and
|
|
33
|
+
* the resolved batch URL.
|
|
34
|
+
*
|
|
35
|
+
* The URL is included because nothing stops one `createBatchLink()`
|
|
36
|
+
* instance from being passed to two runtimes pointing at different
|
|
37
|
+
* origins; without it, calls to one service would be merged into a
|
|
38
|
+
* request sent to the other. */
|
|
39
|
+
export function batchSignature(config) {
|
|
40
|
+
return [
|
|
41
|
+
refId(config.fetchFn),
|
|
42
|
+
refId(config.codec),
|
|
43
|
+
config.batchUrl,
|
|
44
|
+
headerSignature(config.headers),
|
|
45
|
+
].join(FIELD);
|
|
46
|
+
}
|
|
47
|
+
function headerSignature(headers) {
|
|
48
|
+
const parts = [];
|
|
49
|
+
// `.forEach()` rather than `.entries()`/`for...of`: it is the one
|
|
50
|
+
// iteration style declared consistently across the DOM `Headers` lib
|
|
51
|
+
// type and the Node/undici one. `key` is already lowercase — the
|
|
52
|
+
// WHATWG `Headers` spec normalizes names on the way in, so no
|
|
53
|
+
// `.toLowerCase()` is needed here.
|
|
54
|
+
headers.forEach((value, key) => {
|
|
55
|
+
if (!FRAME_LEVEL_HEADERS.has(key)) {
|
|
56
|
+
parts.push(`${key}:${value}`);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
// `Headers` iteration order is not guaranteed stable across
|
|
60
|
+
// implementations — sort so two identical header sets always produce
|
|
61
|
+
// one signature instead of needlessly splitting a batch.
|
|
62
|
+
parts.sort();
|
|
63
|
+
return parts.join(ENTRY);
|
|
64
|
+
}
|
|
65
|
+
// Identity (not structural) comparison for `fetch`/codec: two distinct
|
|
66
|
+
// codec objects with equal `contentType` may still encode differently,
|
|
67
|
+
// so only the same reference is safe to batch together.
|
|
68
|
+
const refIds = new WeakMap();
|
|
69
|
+
let nextRefId = 0;
|
|
70
|
+
function refId(ref) {
|
|
71
|
+
let id = refIds.get(ref);
|
|
72
|
+
if (id === undefined) {
|
|
73
|
+
id = nextRefId++;
|
|
74
|
+
refIds.set(ref, id);
|
|
75
|
+
}
|
|
76
|
+
return String(id);
|
|
77
|
+
}
|
|
78
|
+
/** Splits a flush into partitions that may each be sent as one
|
|
79
|
+
* `/rpc/batch` request. Insertion order is preserved within every
|
|
80
|
+
* partition, and partitions are returned in first-seen order, so a
|
|
81
|
+
* flush whose calls all share a config behaves exactly as it did
|
|
82
|
+
* before partitioning existed. */
|
|
83
|
+
export function partition(entries, options) {
|
|
84
|
+
const partitions = [];
|
|
85
|
+
const indexBySignature = new Map();
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
const config = effectiveConfig(entry.request, options);
|
|
88
|
+
const signature = batchSignature(config);
|
|
89
|
+
const existing = indexBySignature.get(signature);
|
|
90
|
+
if (existing === undefined) {
|
|
91
|
+
indexBySignature.set(signature, partitions.length);
|
|
92
|
+
partitions.push({ config, entries: [entry] });
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
partitions[existing].entries.push(entry);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return partitions;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=signature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAGA,uEAAuE;AACvE,8DAA8D;AAC9D,uEAAuE;AACvE,uEAAuE;AACvE,wEAAwE;AACxE,+CAA+C;AAC/C,kCAAkC;AAClC,MAAM,m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K,SAAS,EAAE,CAAC;YAC3B,gBAAgB,CAAC,GAAG,CAAC,SAAS,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;YACnD,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAChD,CAAC;aAAM,CAAC;YACN,UAAU,CAAC,QAAQ,CAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { CratestackRpcCodec, RpcLinkRequest, RpcLinkResponse } from "@cratestack/ts-types";
|
|
2
|
+
export interface BatchLinkOptions {
|
|
3
|
+
/** Scheduling window. Omitted (default) uses `queueMicrotask` — calls
|
|
4
|
+
* fired synchronously in the same tick (e.g. inside `Promise.all`)
|
|
5
|
+
* collapse into one `/rpc/batch` request. Pass a millisecond value to
|
|
6
|
+
* widen the window across ticks (e.g. debounce bursts from unrelated
|
|
7
|
+
* components). */
|
|
8
|
+
windowMs?: number;
|
|
9
|
+
/** Maximum requests per flushed `/rpc/batch` call. Applied **per
|
|
10
|
+
* partition** (see {@link batchSignature}), not across the whole
|
|
11
|
+
* flush — a partition larger than this splits into several
|
|
12
|
+
* concurrent requests. Default: unbounded; the server enforces no
|
|
13
|
+
* size limit either (see `docs/design/rpc-transport.md` §3.2), but a
|
|
14
|
+
* huge fan-in may still be worth capping client-side. */
|
|
15
|
+
maxBatchSize?: number;
|
|
16
|
+
/** Returns a dedupe key for a queued call, or `null` to never
|
|
17
|
+
* collapse it with anything else. Two calls in the same partition
|
|
18
|
+
* that return the same non-null key become ONE `/rpc/batch` frame,
|
|
19
|
+
* and every caller resolves from that single result.
|
|
20
|
+
*
|
|
21
|
+
* Default: only collapse calls that share an explicit
|
|
22
|
+
* `idempotencyKey` — that's already the caller's own signal that the
|
|
23
|
+
* call is safe to treat as a repeat. Calls with no idempotency key
|
|
24
|
+
* are never auto-collapsed, since the server does no dedup of its
|
|
25
|
+
* own and silently merging two textually-identical but unmarked
|
|
26
|
+
* mutations would be unsafe. Pass a custom `dedupe` for full
|
|
27
|
+
* value-based collapsing, or `() => null` to disable dedup entirely
|
|
28
|
+
* and only batch. */
|
|
29
|
+
dedupe?: (request: RpcLinkRequest) => string | null;
|
|
30
|
+
/** Headers merged **over** the shared headers of each partition when
|
|
31
|
+
* synthesizing its `/rpc/batch` request. Use this to declare the
|
|
32
|
+
* aggregate request's own headers rather than inheriting whatever
|
|
33
|
+
* the queued calls happened to carry. */
|
|
34
|
+
headers?: HeadersInit;
|
|
35
|
+
/** `fetch` used for synthesized `/rpc/batch` requests, overriding the
|
|
36
|
+
* queued calls' own. */
|
|
37
|
+
fetchFn?: typeof fetch;
|
|
38
|
+
/** Codec used to encode the `/rpc/batch` body and decode its response
|
|
39
|
+
* frames, overriding the queued calls' own. Note each caller's
|
|
40
|
+
* individual result is still re-encoded with *its own* codec, so a
|
|
41
|
+
* generated runtime always decodes what it expects. */
|
|
42
|
+
codec?: CratestackRpcCodec;
|
|
43
|
+
}
|
|
44
|
+
/** One queued call awaiting a flush. */
|
|
45
|
+
export interface QueueEntry {
|
|
46
|
+
readonly request: RpcLinkRequest;
|
|
47
|
+
readonly resolve: (value: RpcLinkResponse) => void;
|
|
48
|
+
readonly reject: (reason: unknown) => void;
|
|
49
|
+
}
|
|
50
|
+
/** Calls collapsed into a single `/rpc/batch` frame by `dedupe`. */
|
|
51
|
+
export interface Group {
|
|
52
|
+
readonly key: string | null;
|
|
53
|
+
readonly entries: QueueEntry[];
|
|
54
|
+
}
|
|
55
|
+
/** The transport config a synthesized `/rpc/batch` request is issued
|
|
56
|
+
* with, after applying any {@link BatchLinkOptions} overrides. Every
|
|
57
|
+
* entry within a partition resolves to an equal config by
|
|
58
|
+
* construction — that is what the partition *is*. */
|
|
59
|
+
export interface EffectiveConfig {
|
|
60
|
+
readonly headers: Headers;
|
|
61
|
+
readonly fetchFn: typeof fetch;
|
|
62
|
+
readonly codec: CratestackRpcCodec;
|
|
63
|
+
readonly batchUrl: string;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEhG,MAAM,WAAW,gBAAgB;IAC/B;;;;uBAImB;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;8DAK0D;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;;;;;;;;0BAYsB;IACtB,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,MAAM,GAAG,IAAI,CAAC;IACpD;;;8CAG0C;IAC1C,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB;6BACyB;IACzB,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;IACvB;;;4DAGwD;IACxD,KAAK,CAAC,EAAE,kBAAkB,CAAC;CAC5B;AAED,wCAAwC;AACxC,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IACnD,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;CAC5C;AAED,oEAAoE;AACpE,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,UAAU,EAAE,CAAC;CAChC;AAED;;;sDAGsD;AACtD,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,OAAO,KAAK,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE,kBAAkB,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cratestack/link-batch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "A batshit-style automatic batch scheduler for CrateStack's generated TypeScript RPC client, shipped as an RpcLink.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"lint": "biome check ."
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@cratestack/ts-types": "0.
|
|
31
|
+
"@cratestack/ts-types": "0.6.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"typescript": "^5.7.0",
|