@procedures/client 0.1.0 → 0.2.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/CHANGELOG.md +32 -0
- package/README.md +5 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapters/http/server/transport.d.ts.map +1 -1
- package/dist/adapters/http/server/transport.js +1 -0
- package/dist/adapters/http/server/transport.js.map +1 -1
- package/dist/browser.d.ts +6 -3
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +4 -1
- package/dist/browser.js.map +1 -1
- package/dist/client/client.d.ts +9 -1
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +27 -6
- package/dist/client/client.js.map +1 -1
- package/dist/client/types.d.ts +9 -0
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/types.js.map +1 -1
- package/dist/procedures/capability.d.ts +111 -0
- package/dist/procedures/capability.d.ts.map +1 -0
- package/dist/procedures/capability.js +269 -0
- package/dist/procedures/capability.js.map +1 -0
- package/dist/procedures/cli/discover.d.ts +3 -3
- package/dist/procedures/cli/discover.js +3 -3
- package/dist/procedures/cli/index.js +5 -3
- package/dist/procedures/cli/index.js.map +1 -1
- package/dist/procedures/cli/init.js +1 -1
- package/dist/procedures/collection/procedures.d.ts.map +1 -1
- package/dist/procedures/collection/procedures.js +12 -0
- package/dist/procedures/collection/procedures.js.map +1 -1
- package/dist/procedures/core/array.d.ts.map +1 -1
- package/dist/procedures/core/array.js +3 -1
- package/dist/procedures/core/array.js.map +1 -1
- package/dist/procedures/core/comparison.d.ts.map +1 -1
- package/dist/procedures/core/comparison.js +3 -1
- package/dist/procedures/core/comparison.js.map +1 -1
- package/dist/procedures/core/index.d.ts.map +1 -1
- package/dist/procedures/core/index.js +5 -2
- package/dist/procedures/core/index.js.map +1 -1
- package/dist/procedures/core/math.d.ts.map +1 -1
- package/dist/procedures/core/math.js +3 -1
- package/dist/procedures/core/math.js.map +1 -1
- package/dist/procedures/core/meta.d.ts.map +1 -1
- package/dist/procedures/core/meta.js +11 -6
- package/dist/procedures/core/meta.js.map +1 -1
- package/dist/procedures/core/object.d.ts.map +1 -1
- package/dist/procedures/core/object.js +3 -1
- package/dist/procedures/core/object.js.map +1 -1
- package/dist/procedures/core/string.d.ts.map +1 -1
- package/dist/procedures/core/string.js +3 -1
- package/dist/procedures/core/string.js.map +1 -1
- package/dist/procedures/core/type.d.ts.map +1 -1
- package/dist/procedures/core/type.js +3 -1
- package/dist/procedures/core/type.js.map +1 -1
- package/dist/procedures/define-procedure.d.ts.map +1 -1
- package/dist/procedures/define-procedure.js +5 -4
- package/dist/procedures/define-procedure.js.map +1 -1
- package/dist/procedures/effects.d.ts +15 -0
- package/dist/procedures/effects.d.ts.map +1 -0
- package/dist/procedures/effects.js +18 -0
- package/dist/procedures/effects.js.map +1 -0
- package/dist/procedures/index.d.ts +5 -2
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +8 -23
- package/dist/procedures/index.js.map +1 -1
- package/dist/procedures/interceptors.d.ts.map +1 -1
- package/dist/procedures/interceptors.js +2 -1
- package/dist/procedures/interceptors.js.map +1 -1
- package/dist/procedures/invoke.d.ts +36 -3
- package/dist/procedures/invoke.d.ts.map +1 -1
- package/dist/procedures/invoke.js +78 -12
- package/dist/procedures/invoke.js.map +1 -1
- package/dist/procedures/module.d.ts +13 -8
- package/dist/procedures/module.d.ts.map +1 -1
- package/dist/procedures/module.js +19 -9
- package/dist/procedures/module.js.map +1 -1
- package/dist/procedures/types.d.ts +100 -2
- package/dist/procedures/types.d.ts.map +1 -1
- package/dist/procedures/types.js.map +1 -1
- package/dist/register.d.ts +8 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +9 -0
- package/dist/register.js.map +1 -0
- package/dist/server/host-policy.d.ts +24 -2
- package/dist/server/host-policy.d.ts.map +1 -1
- package/dist/server/host-policy.js +22 -0
- package/dist/server/host-policy.js.map +1 -1
- package/dist/server/procedure-server.d.ts +16 -2
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +26 -1
- package/dist/server/procedure-server.js.map +1 -1
- package/package.json +6 -4
- package/src/adapters/http/server/transport.ts +1 -0
- package/src/browser.ts +29 -0
- package/src/client/client.ts +33 -6
- package/src/client/types.ts +11 -0
- package/src/procedures/capability.ts +333 -0
- package/src/procedures/cli/discover.ts +3 -3
- package/src/procedures/cli/index.ts +5 -3
- package/src/procedures/cli/init.ts +1 -1
- package/src/procedures/collection/procedures.ts +12 -0
- package/src/procedures/core/array.ts +3 -1
- package/src/procedures/core/comparison.ts +3 -1
- package/src/procedures/core/index.ts +5 -2
- package/src/procedures/core/math.ts +3 -1
- package/src/procedures/core/meta.ts +11 -6
- package/src/procedures/core/object.ts +3 -1
- package/src/procedures/core/string.ts +3 -1
- package/src/procedures/core/type.ts +3 -1
- package/src/procedures/define-procedure.ts +5 -4
- package/src/procedures/effects.ts +24 -0
- package/src/procedures/index.ts +33 -22
- package/src/procedures/interceptors.ts +2 -1
- package/src/procedures/invoke.ts +112 -13
- package/src/procedures/module.ts +19 -9
- package/src/procedures/types.ts +113 -2
- package/src/register.ts +10 -0
- package/src/server/host-policy.ts +44 -2
- package/src/server/procedure-server.ts +44 -2
- package/dist/client/middleware/batching.d.ts +0 -102
- package/dist/client/middleware/batching.d.ts.map +0 -1
- package/dist/client/middleware/batching.js +0 -245
- package/dist/client/middleware/batching.js.map +0 -1
- package/dist/collections/async/async-queue.d.ts +0 -144
- package/dist/collections/async/async-queue.d.ts.map +0 -1
- package/dist/collections/async/async-queue.js +0 -347
- package/dist/collections/async/async-queue.js.map +0 -1
- package/dist/collections/async/channels.d.ts +0 -188
- package/dist/collections/async/channels.d.ts.map +0 -1
- package/dist/collections/async/channels.js +0 -312
- package/dist/collections/async/channels.js.map +0 -1
- package/dist/collections/behaviors/bounded.d.ts +0 -79
- package/dist/collections/behaviors/bounded.d.ts.map +0 -1
- package/dist/collections/behaviors/bounded.js +0 -384
- package/dist/collections/behaviors/bounded.js.map +0 -1
- package/dist/collections/behaviors/evented.d.ts +0 -154
- package/dist/collections/behaviors/evented.d.ts.map +0 -1
- package/dist/collections/behaviors/evented.js +0 -299
- package/dist/collections/behaviors/evented.js.map +0 -1
- package/dist/collections/behaviors/lru.d.ts +0 -84
- package/dist/collections/behaviors/lru.d.ts.map +0 -1
- package/dist/collections/behaviors/lru.js +0 -330
- package/dist/collections/behaviors/lru.js.map +0 -1
- package/dist/collections/behaviors/readonly.d.ts +0 -73
- package/dist/collections/behaviors/readonly.d.ts.map +0 -1
- package/dist/collections/behaviors/readonly.js +0 -158
- package/dist/collections/behaviors/readonly.js.map +0 -1
- package/dist/collections/behaviors/safe.d.ts +0 -163
- package/dist/collections/behaviors/safe.d.ts.map +0 -1
- package/dist/collections/behaviors/safe.js +0 -172
- package/dist/collections/behaviors/safe.js.map +0 -1
- package/dist/collections/behaviors/synchronized.d.ts +0 -88
- package/dist/collections/behaviors/synchronized.d.ts.map +0 -1
- package/dist/collections/behaviors/synchronized.js +0 -294
- package/dist/collections/behaviors/synchronized.js.map +0 -1
- package/dist/collections/behaviors/ttl.d.ts +0 -118
- package/dist/collections/behaviors/ttl.d.ts.map +0 -1
- package/dist/collections/behaviors/ttl.js +0 -338
- package/dist/collections/behaviors/ttl.js.map +0 -1
- package/dist/collections/core/effects.d.ts +0 -176
- package/dist/collections/core/effects.d.ts.map +0 -1
- package/dist/collections/core/effects.js +0 -180
- package/dist/collections/core/effects.js.map +0 -1
- package/dist/collections/core/events.d.ts +0 -192
- package/dist/collections/core/events.d.ts.map +0 -1
- package/dist/collections/core/events.js +0 -291
- package/dist/collections/core/events.js.map +0 -1
- package/dist/collections/core/middleware.d.ts +0 -231
- package/dist/collections/core/middleware.d.ts.map +0 -1
- package/dist/collections/core/middleware.js +0 -161
- package/dist/collections/core/middleware.js.map +0 -1
- package/dist/collections/core/policies.d.ts +0 -220
- package/dist/collections/core/policies.d.ts.map +0 -1
- package/dist/collections/core/policies.js +0 -48
- package/dist/collections/core/policies.js.map +0 -1
- package/dist/collections/core/traits.d.ts +0 -142
- package/dist/collections/core/traits.d.ts.map +0 -1
- package/dist/collections/core/traits.js +0 -8
- package/dist/collections/core/traits.js.map +0 -1
- package/dist/collections/examples.d.ts +0 -13
- package/dist/collections/examples.d.ts.map +0 -1
- package/dist/collections/examples.js +0 -302
- package/dist/collections/examples.js.map +0 -1
- package/dist/collections/fx/collectors.d.ts +0 -190
- package/dist/collections/fx/collectors.d.ts.map +0 -1
- package/dist/collections/fx/collectors.js +0 -369
- package/dist/collections/fx/collectors.js.map +0 -1
- package/dist/collections/fx/iter.d.ts +0 -166
- package/dist/collections/fx/iter.d.ts.map +0 -1
- package/dist/collections/fx/iter.js +0 -421
- package/dist/collections/fx/iter.js.map +0 -1
- package/dist/collections/impl/array-deque.d.ts +0 -107
- package/dist/collections/impl/array-deque.d.ts.map +0 -1
- package/dist/collections/impl/array-deque.js +0 -381
- package/dist/collections/impl/array-deque.js.map +0 -1
- package/dist/collections/impl/array-list.d.ts +0 -101
- package/dist/collections/impl/array-list.d.ts.map +0 -1
- package/dist/collections/impl/array-list.js +0 -387
- package/dist/collections/impl/array-list.js.map +0 -1
- package/dist/collections/impl/hash-map.d.ts +0 -141
- package/dist/collections/impl/hash-map.d.ts.map +0 -1
- package/dist/collections/impl/hash-map.js +0 -450
- package/dist/collections/impl/hash-map.js.map +0 -1
- package/dist/collections/impl/hash-set.d.ts +0 -125
- package/dist/collections/impl/hash-set.d.ts.map +0 -1
- package/dist/collections/impl/hash-set.js +0 -264
- package/dist/collections/impl/hash-set.js.map +0 -1
- package/dist/collections/impl/linked-hash-map.d.ts +0 -141
- package/dist/collections/impl/linked-hash-map.d.ts.map +0 -1
- package/dist/collections/impl/linked-hash-map.js +0 -472
- package/dist/collections/impl/linked-hash-map.js.map +0 -1
- package/dist/collections/impl/linked-list.d.ts +0 -109
- package/dist/collections/impl/linked-list.d.ts.map +0 -1
- package/dist/collections/impl/linked-list.js +0 -491
- package/dist/collections/impl/linked-list.js.map +0 -1
- package/dist/collections/impl/priority-queue.d.ts +0 -134
- package/dist/collections/impl/priority-queue.d.ts.map +0 -1
- package/dist/collections/impl/priority-queue.js +0 -336
- package/dist/collections/impl/priority-queue.js.map +0 -1
- package/dist/collections/impl/tree-map.d.ts +0 -146
- package/dist/collections/impl/tree-map.d.ts.map +0 -1
- package/dist/collections/impl/tree-map.js +0 -781
- package/dist/collections/impl/tree-map.js.map +0 -1
- package/dist/collections/impl/tree-set.d.ts +0 -112
- package/dist/collections/impl/tree-set.d.ts.map +0 -1
- package/dist/collections/impl/tree-set.js +0 -374
- package/dist/collections/impl/tree-set.js.map +0 -1
- package/dist/collections/index.d.ts +0 -105
- package/dist/collections/index.d.ts.map +0 -1
- package/dist/collections/index.js +0 -135
- package/dist/collections/index.js.map +0 -1
- package/dist/collections/interfaces/collection.d.ts +0 -119
- package/dist/collections/interfaces/collection.d.ts.map +0 -1
- package/dist/collections/interfaces/collection.js +0 -8
- package/dist/collections/interfaces/collection.js.map +0 -1
- package/dist/collections/interfaces/list.d.ts +0 -247
- package/dist/collections/interfaces/list.d.ts.map +0 -1
- package/dist/collections/interfaces/list.js +0 -8
- package/dist/collections/interfaces/list.js.map +0 -1
- package/dist/collections/interfaces/map.d.ts +0 -344
- package/dist/collections/interfaces/map.d.ts.map +0 -1
- package/dist/collections/interfaces/map.js +0 -8
- package/dist/collections/interfaces/map.js.map +0 -1
- package/dist/collections/interfaces/queue.d.ts +0 -304
- package/dist/collections/interfaces/queue.d.ts.map +0 -1
- package/dist/collections/interfaces/queue.js +0 -8
- package/dist/collections/interfaces/queue.js.map +0 -1
- package/dist/collections/interfaces/set.d.ts +0 -137
- package/dist/collections/interfaces/set.d.ts.map +0 -1
- package/dist/collections/interfaces/set.js +0 -8
- package/dist/collections/interfaces/set.js.map +0 -1
- package/dist/collections/storage/api.d.ts +0 -109
- package/dist/collections/storage/api.d.ts.map +0 -1
- package/dist/collections/storage/api.js +0 -182
- package/dist/collections/storage/api.js.map +0 -1
- package/dist/collections/storage/hybrid.d.ts +0 -164
- package/dist/collections/storage/hybrid.d.ts.map +0 -1
- package/dist/collections/storage/hybrid.js +0 -416
- package/dist/collections/storage/hybrid.js.map +0 -1
- package/dist/collections/storage/index.d.ts +0 -16
- package/dist/collections/storage/index.d.ts.map +0 -1
- package/dist/collections/storage/index.js +0 -13
- package/dist/collections/storage/index.js.map +0 -1
- package/dist/collections/storage/interface.d.ts +0 -141
- package/dist/collections/storage/interface.d.ts.map +0 -1
- package/dist/collections/storage/interface.js +0 -23
- package/dist/collections/storage/interface.js.map +0 -1
- package/dist/collections/storage/memory.d.ts +0 -68
- package/dist/collections/storage/memory.d.ts.map +0 -1
- package/dist/collections/storage/memory.js +0 -154
- package/dist/collections/storage/memory.js.map +0 -1
- package/dist/collections/utils/defaults.d.ts +0 -135
- package/dist/collections/utils/defaults.d.ts.map +0 -1
- package/dist/collections/utils/defaults.js +0 -322
- package/dist/collections/utils/defaults.js.map +0 -1
- package/dist/collections/utils/factories.d.ts +0 -144
- package/dist/collections/utils/factories.d.ts.map +0 -1
- package/dist/collections/utils/factories.js +0 -293
- package/dist/collections/utils/factories.js.map +0 -1
- package/dist/collections/utils/helpers.d.ts +0 -137
- package/dist/collections/utils/helpers.d.ts.map +0 -1
- package/dist/collections/utils/helpers.js +0 -221
- package/dist/collections/utils/helpers.js.map +0 -1
- package/dist/collections.d.ts +0 -12
- package/dist/collections.d.ts.map +0 -1
- package/dist/collections.js +0 -10
- package/dist/collections.js.map +0 -1
- package/dist/components/define.d.ts +0 -134
- package/dist/components/define.d.ts.map +0 -1
- package/dist/components/define.js +0 -486
- package/dist/components/define.js.map +0 -1
- package/dist/components/index.d.ts +0 -39
- package/dist/components/index.d.ts.map +0 -1
- package/dist/components/index.js +0 -38
- package/dist/components/index.js.map +0 -1
- package/dist/components/types.d.ts +0 -264
- package/dist/components/types.d.ts.map +0 -1
- package/dist/components/types.js +0 -46
- package/dist/components/types.js.map +0 -1
- package/dist/procedures/storage/adapter.d.ts +0 -148
- package/dist/procedures/storage/adapter.d.ts.map +0 -1
- package/dist/procedures/storage/adapter.js +0 -214
- package/dist/procedures/storage/adapter.js.map +0 -1
- package/dist/procedures/storage/api.d.ts +0 -125
- package/dist/procedures/storage/api.d.ts.map +0 -1
- package/dist/procedures/storage/api.js +0 -220
- package/dist/procedures/storage/api.js.map +0 -1
- package/dist/procedures/storage/factory.d.ts +0 -134
- package/dist/procedures/storage/factory.d.ts.map +0 -1
- package/dist/procedures/storage/factory.js +0 -166
- package/dist/procedures/storage/factory.js.map +0 -1
- package/dist/procedures/storage/hybrid.d.ts +0 -193
- package/dist/procedures/storage/hybrid.d.ts.map +0 -1
- package/dist/procedures/storage/hybrid.js +0 -519
- package/dist/procedures/storage/hybrid.js.map +0 -1
- package/dist/procedures/storage/index.d.ts +0 -17
- package/dist/procedures/storage/index.d.ts.map +0 -1
- package/dist/procedures/storage/index.js +0 -17
- package/dist/procedures/storage/index.js.map +0 -1
- package/dist/procedures/storage/procedures.d.ts +0 -39
- package/dist/procedures/storage/procedures.d.ts.map +0 -1
- package/dist/procedures/storage/procedures.js +0 -261
- package/dist/procedures/storage/procedures.js.map +0 -1
- package/dist/procedures/storage/serialization.d.ts +0 -123
- package/dist/procedures/storage/serialization.d.ts.map +0 -1
- package/dist/procedures/storage/serialization.js +0 -216
- package/dist/procedures/storage/serialization.js.map +0 -1
- package/dist/procedures/storage/synced-registry.d.ts +0 -246
- package/dist/procedures/storage/synced-registry.d.ts.map +0 -1
- package/dist/procedures/storage/synced-registry.js +0 -575
- package/dist/procedures/storage/synced-registry.js.map +0 -1
- package/dist/procedures/storage/types.d.ts +0 -130
- package/dist/procedures/storage/types.d.ts.map +0 -1
- package/dist/procedures/storage/types.js +0 -8
- package/dist/procedures/storage/types.js.map +0 -1
|
@@ -17,7 +17,9 @@ import type {
|
|
|
17
17
|
import { pathToKey } from "../procedures/types.js";
|
|
18
18
|
import { ProcedureRegistry, PROCEDURE_REGISTRY } from "../procedures/registry.js";
|
|
19
19
|
import { createCollectionProcedures } from "../procedures/collection/procedures.js";
|
|
20
|
-
import { InvocationError, invokeProcedure, type Interceptor } from "../procedures/invoke.js";
|
|
20
|
+
import { InvocationError, invokeProcedure, type AdmitItem, type Interceptor, type Invocation } from "../procedures/invoke.js";
|
|
21
|
+
import type { Capability, ClearanceOrder, ResourcesOf } from "../procedures/types.js";
|
|
22
|
+
import { TIMEOUT_METADATA_KEY, narrowCapability } from "../procedures/capability.js";
|
|
21
23
|
import type { CollectionStorage } from "../procedures/collection/storage.js";
|
|
22
24
|
|
|
23
25
|
// =============================================================================
|
|
@@ -68,6 +70,29 @@ export interface ProcedureServerOptions extends ServerOptions {
|
|
|
68
70
|
* tracing, timeouts, metrics (see `procedures/interceptors.ts`).
|
|
69
71
|
*/
|
|
70
72
|
interceptors?: readonly Interceptor[] | undefined;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The capability of each request (CAPABILITIES-2026-10): one value, or a function of the
|
|
76
|
+
* request. The function reads only what the transport authenticated. Without it: no limits.
|
|
77
|
+
*/
|
|
78
|
+
capability?: Capability | ((request: ServerRequest<unknown>) => Capability | undefined) | undefined;
|
|
79
|
+
/** The order of the clearance values. */
|
|
80
|
+
clearanceOrder?: ClearanceOrder | undefined;
|
|
81
|
+
/** The resources of each invocation for its capability (`ctx.resources`). */
|
|
82
|
+
resources?: ((invocation: Invocation) => ResourcesOf) | undefined;
|
|
83
|
+
/** The filter of the output that leaves the server. */
|
|
84
|
+
admitItem?: AdmitItem | undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The capability of a request with the remaining time of its caller, when it sends one. */
|
|
88
|
+
function withRequestTimeout(
|
|
89
|
+
capability: Capability | undefined,
|
|
90
|
+
request: ServerRequest<unknown>,
|
|
91
|
+
order: ClearanceOrder | undefined
|
|
92
|
+
): Capability | undefined {
|
|
93
|
+
const timeoutMs = request.metadata[TIMEOUT_METADATA_KEY];
|
|
94
|
+
if (typeof timeoutMs !== "number" || !Number.isFinite(timeoutMs) || timeoutMs < 0) return capability;
|
|
95
|
+
return narrowCapability(capability, { timeoutMs }, order);
|
|
71
96
|
}
|
|
72
97
|
|
|
73
98
|
/**
|
|
@@ -78,6 +103,8 @@ export function errorResponse(id: string, error: unknown): ServerResponse<unknow
|
|
|
78
103
|
// An error that says it is retryable stays retryable: before, every error lost the flag
|
|
79
104
|
// (deep dive TRN-7)
|
|
80
105
|
const retryable = (error as { retryable?: unknown } | undefined)?.retryable;
|
|
106
|
+
// A refusal of the capability says why (CAPABILITIES-2026-10)
|
|
107
|
+
const reason = error instanceof InvocationError ? error.reason : undefined;
|
|
81
108
|
return {
|
|
82
109
|
id,
|
|
83
110
|
status: {
|
|
@@ -86,7 +113,7 @@ export function errorResponse(id: string, error: unknown): ServerResponse<unknow
|
|
|
86
113
|
message: error instanceof Error ? error.message : String(error),
|
|
87
114
|
retryable: typeof retryable === "boolean" ? retryable : false,
|
|
88
115
|
},
|
|
89
|
-
metadata: {},
|
|
116
|
+
metadata: reason ? { reason } : {},
|
|
90
117
|
};
|
|
91
118
|
}
|
|
92
119
|
|
|
@@ -136,6 +163,8 @@ export class ProcedureServer extends Server implements RepositoryProvider {
|
|
|
136
163
|
private readonly expose: ((path: ProcedurePath) => boolean) | undefined;
|
|
137
164
|
private readonly strictCalls: boolean;
|
|
138
165
|
private readonly interceptors: readonly Interceptor[];
|
|
166
|
+
/** The capability options of the requests (CAPABILITIES-2026-10). */
|
|
167
|
+
private readonly capabilityOptions: Pick<ProcedureServerOptions, "capability" | "clearanceOrder" | "resources" | "admitItem">;
|
|
139
168
|
|
|
140
169
|
constructor(options: ProcedureServerOptions = {}) {
|
|
141
170
|
super(options);
|
|
@@ -144,6 +173,12 @@ export class ProcedureServer extends Server implements RepositoryProvider {
|
|
|
144
173
|
this.expose = options.expose;
|
|
145
174
|
this.strictCalls = options.strictCalls ?? false;
|
|
146
175
|
this.interceptors = options.interceptors ?? [];
|
|
176
|
+
this.capabilityOptions = {
|
|
177
|
+
capability: options.capability,
|
|
178
|
+
clearanceOrder: options.clearanceOrder,
|
|
179
|
+
resources: options.resources,
|
|
180
|
+
admitItem: options.admitItem,
|
|
181
|
+
};
|
|
147
182
|
|
|
148
183
|
// Configure collections
|
|
149
184
|
if (options.collections) {
|
|
@@ -303,6 +338,7 @@ export class ProcedureServer extends Server implements RepositoryProvider {
|
|
|
303
338
|
try {
|
|
304
339
|
// One invocation path for every host (ARCHITECTURE-PROPOSALS P1): input validation, the
|
|
305
340
|
// context, the expose rule for nested calls (BUGS-2026-07 H18) and output validation
|
|
341
|
+
const { capability, clearanceOrder, resources, admitItem } = this.capabilityOptions;
|
|
306
342
|
const output = await invokeProcedure(procedure, request.payload, {
|
|
307
343
|
registry: this.procedureRegistry,
|
|
308
344
|
metadata: request.metadata,
|
|
@@ -311,6 +347,12 @@ export class ProcedureServer extends Server implements RepositoryProvider {
|
|
|
311
347
|
expose: this.expose,
|
|
312
348
|
strictCalls: this.strictCalls,
|
|
313
349
|
interceptors: this.interceptors,
|
|
350
|
+
// The capability comes from the server, never from the request. The remaining time of
|
|
351
|
+
// the caller can only make the deadline earlier (CAPABILITIES-2026-10, decision 9).
|
|
352
|
+
capability: withRequestTimeout(typeof capability === "function" ? capability(request) : capability, request, clearanceOrder),
|
|
353
|
+
clearanceOrder,
|
|
354
|
+
resources,
|
|
355
|
+
admitItem,
|
|
314
356
|
});
|
|
315
357
|
if (output.kind === "stream") {
|
|
316
358
|
return { id: request.id, status: { type: "success", code: 200 }, stream: output.items, metadata: {} };
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Batching Middleware
|
|
3
|
-
*
|
|
4
|
-
* Combines multiple requests into a single batch for efficiency.
|
|
5
|
-
* Reduces network overhead and improves throughput.
|
|
6
|
-
*
|
|
7
|
-
* Works with both client and server!
|
|
8
|
-
*/
|
|
9
|
-
import type { ClientMiddleware, Message, TypedClientMiddleware } from "../types.js";
|
|
10
|
-
import type { BatchingContext } from "./contexts.js";
|
|
11
|
-
/**
|
|
12
|
-
* Batching options.
|
|
13
|
-
*/
|
|
14
|
-
export interface BatchingOptions {
|
|
15
|
-
/**
|
|
16
|
-
* Maximum batch size (number of requests).
|
|
17
|
-
* @default 10
|
|
18
|
-
*/
|
|
19
|
-
maxBatchSize?: number;
|
|
20
|
-
/**
|
|
21
|
-
* Maximum wait time before sending batch (in milliseconds).
|
|
22
|
-
* @default 10
|
|
23
|
-
*/
|
|
24
|
-
maxWaitTime?: number;
|
|
25
|
-
/**
|
|
26
|
-
* Only batch requests to the same service.
|
|
27
|
-
* @default true
|
|
28
|
-
*/
|
|
29
|
-
sameServiceOnly?: boolean;
|
|
30
|
-
/**
|
|
31
|
-
* Custom batch key function.
|
|
32
|
-
* Requests with the same key will be batched together.
|
|
33
|
-
* @default Group by service name
|
|
34
|
-
*/
|
|
35
|
-
getBatchKey?: (message: Message) => string;
|
|
36
|
-
/**
|
|
37
|
-
* Callback when batch is sent.
|
|
38
|
-
*/
|
|
39
|
-
onBatchSent?: (batchSize: number, batchKey: string) => void;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Batching statistics.
|
|
43
|
-
*/
|
|
44
|
-
export interface BatchingStats {
|
|
45
|
-
totalRequests: number;
|
|
46
|
-
totalBatches: number;
|
|
47
|
-
averageBatchSize: number;
|
|
48
|
-
pendingRequests: number;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Create batching middleware.
|
|
52
|
-
*
|
|
53
|
-
* Automatically combines multiple requests into batches for efficiency.
|
|
54
|
-
*
|
|
55
|
-
* @param options - Batching configuration
|
|
56
|
-
* @returns Middleware function
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* client.use(createBatchingMiddleware({
|
|
61
|
-
* maxBatchSize: 10, // Combine up to 10 requests
|
|
62
|
-
* maxWaitTime: 10, // Wait max 10ms before sending
|
|
63
|
-
* sameServiceOnly: true // Only batch requests to same service
|
|
64
|
-
* }));
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* // Custom batching by operation
|
|
70
|
-
* client.use(createBatchingMiddleware({
|
|
71
|
-
* getBatchKey: (msg) => `${msg.method.service}:${msg.method.operation}`
|
|
72
|
-
* }));
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
export declare function createBatchingMiddleware(options?: BatchingOptions): TypedClientMiddleware<BatchingContext, {}>;
|
|
76
|
-
/**
|
|
77
|
-
* Get batching statistics.
|
|
78
|
-
*
|
|
79
|
-
* Note: This is a placeholder - proper implementation would
|
|
80
|
-
* require exposing the queue through the middleware instance.
|
|
81
|
-
*/
|
|
82
|
-
export declare function getBatchingStats(_middleware: ClientMiddleware): BatchingStats | null;
|
|
83
|
-
/**
|
|
84
|
-
* Create adaptive batching middleware.
|
|
85
|
-
*
|
|
86
|
-
* Automatically adjusts batch size based on request patterns.
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```typescript
|
|
90
|
-
* client.use(createAdaptiveBatchingMiddleware({
|
|
91
|
-
* minBatchSize: 2,
|
|
92
|
-
* maxBatchSize: 50,
|
|
93
|
-
* targetLatency: 20 // Target 20ms latency
|
|
94
|
-
* }));
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
export declare function createAdaptiveBatchingMiddleware(options?: {
|
|
98
|
-
minBatchSize?: number;
|
|
99
|
-
maxBatchSize?: number;
|
|
100
|
-
targetLatency?: number;
|
|
101
|
-
}): ClientMiddleware;
|
|
102
|
-
//# sourceMappingURL=batching.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"batching.d.ts","sourceRoot":"","sources":["../../../src/client/middleware/batching.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAA+B,OAAO,EAAgB,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC/H,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC;IAE3C;;OAEG;IACH,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7D;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;CACzB;AA8HD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,eAAoB,GAC5B,qBAAqB,CAAC,eAAe,EAAE,EAAE,CAAC,CAgE5C;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,gBAAgB,GAC5B,aAAa,GAAG,IAAI,CAEtB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gCAAgC,CAAC,OAAO,GAAE;IACxD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;CACnB,GAAG,gBAAgB,CAoDxB"}
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Batching Middleware
|
|
3
|
-
*
|
|
4
|
-
* Combines multiple requests into a single batch for efficiency.
|
|
5
|
-
* Reduces network overhead and improves throughput.
|
|
6
|
-
*
|
|
7
|
-
* Works with both client and server!
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Batch queue for collecting requests.
|
|
11
|
-
*/
|
|
12
|
-
class BatchQueue {
|
|
13
|
-
options;
|
|
14
|
-
processBatch;
|
|
15
|
-
requests = new Map();
|
|
16
|
-
timers = new Map();
|
|
17
|
-
stats = {
|
|
18
|
-
totalRequests: 0,
|
|
19
|
-
totalBatches: 0,
|
|
20
|
-
};
|
|
21
|
-
constructor(options, processBatch) {
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.processBatch = processBatch;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Add request to batch queue.
|
|
27
|
-
*/
|
|
28
|
-
add(key, request) {
|
|
29
|
-
this.stats.totalRequests++;
|
|
30
|
-
// Get or create batch for this key
|
|
31
|
-
let batch = this.requests.get(key);
|
|
32
|
-
if (!batch) {
|
|
33
|
-
batch = [];
|
|
34
|
-
this.requests.set(key, batch);
|
|
35
|
-
}
|
|
36
|
-
batch.push(request);
|
|
37
|
-
// Check if batch is full
|
|
38
|
-
if (batch.length >= this.options.maxBatchSize) {
|
|
39
|
-
this.flush(key);
|
|
40
|
-
}
|
|
41
|
-
else if (batch.length === 1) {
|
|
42
|
-
// First request in batch - start timer
|
|
43
|
-
this.startTimer(key);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Start timer for batch.
|
|
48
|
-
*/
|
|
49
|
-
startTimer(key) {
|
|
50
|
-
const timer = setTimeout(() => {
|
|
51
|
-
this.flush(key);
|
|
52
|
-
}, this.options.maxWaitTime);
|
|
53
|
-
this.timers.set(key, timer);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Flush batch (send it).
|
|
57
|
-
*/
|
|
58
|
-
flush(key) {
|
|
59
|
-
const batch = this.requests.get(key);
|
|
60
|
-
if (!batch || batch.length === 0) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
// Clear timer
|
|
64
|
-
const timer = this.timers.get(key);
|
|
65
|
-
if (timer) {
|
|
66
|
-
clearTimeout(timer);
|
|
67
|
-
this.timers.delete(key);
|
|
68
|
-
}
|
|
69
|
-
// Remove from queue
|
|
70
|
-
this.requests.delete(key);
|
|
71
|
-
// Update stats
|
|
72
|
-
this.stats.totalBatches++;
|
|
73
|
-
// Notify callback
|
|
74
|
-
if (this.options.onBatchSent) {
|
|
75
|
-
this.options.onBatchSent(batch.length, key);
|
|
76
|
-
}
|
|
77
|
-
// Process batch
|
|
78
|
-
this.processBatch(key, batch);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Get statistics.
|
|
82
|
-
*/
|
|
83
|
-
getStats() {
|
|
84
|
-
let pendingRequests = 0;
|
|
85
|
-
for (const batch of this.requests.values()) {
|
|
86
|
-
pendingRequests += batch.length;
|
|
87
|
-
}
|
|
88
|
-
const averageBatchSize = this.stats.totalBatches > 0
|
|
89
|
-
? this.stats.totalRequests / this.stats.totalBatches
|
|
90
|
-
: 0;
|
|
91
|
-
return {
|
|
92
|
-
totalRequests: this.stats.totalRequests,
|
|
93
|
-
totalBatches: this.stats.totalBatches,
|
|
94
|
-
averageBatchSize,
|
|
95
|
-
pendingRequests,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Create batching middleware.
|
|
101
|
-
*
|
|
102
|
-
* Automatically combines multiple requests into batches for efficiency.
|
|
103
|
-
*
|
|
104
|
-
* @param options - Batching configuration
|
|
105
|
-
* @returns Middleware function
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```typescript
|
|
109
|
-
* client.use(createBatchingMiddleware({
|
|
110
|
-
* maxBatchSize: 10, // Combine up to 10 requests
|
|
111
|
-
* maxWaitTime: 10, // Wait max 10ms before sending
|
|
112
|
-
* sameServiceOnly: true // Only batch requests to same service
|
|
113
|
-
* }));
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```typescript
|
|
118
|
-
* // Custom batching by operation
|
|
119
|
-
* client.use(createBatchingMiddleware({
|
|
120
|
-
* getBatchKey: (msg) => `${msg.method.service}:${msg.method.operation}`
|
|
121
|
-
* }));
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
124
|
-
export function createBatchingMiddleware(options = {}) {
|
|
125
|
-
const sameServiceOnly = options.sameServiceOnly ?? true;
|
|
126
|
-
const opts = {
|
|
127
|
-
maxBatchSize: options.maxBatchSize ?? 10,
|
|
128
|
-
maxWaitTime: options.maxWaitTime ?? 10,
|
|
129
|
-
sameServiceOnly,
|
|
130
|
-
getBatchKey: options.getBatchKey ??
|
|
131
|
-
((msg) => sameServiceOnly
|
|
132
|
-
? msg.method.service
|
|
133
|
-
: "__all__"),
|
|
134
|
-
...(options.onBatchSent !== undefined && { onBatchSent: options.onBatchSent }),
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Process a batch of requests.
|
|
138
|
-
*/
|
|
139
|
-
async function processBatch(_key, requests) {
|
|
140
|
-
// Note: This is a simplified implementation.
|
|
141
|
-
// In practice, you'd need transport-level support for batching.
|
|
142
|
-
// The transport would need to:
|
|
143
|
-
// 1. Accept multiple messages
|
|
144
|
-
// 2. Send them in a single network request
|
|
145
|
-
// 3. Return multiple responses
|
|
146
|
-
// 4. Match responses to requests by ID
|
|
147
|
-
// For now, we execute requests individually
|
|
148
|
-
// Real batching requires transport cooperation
|
|
149
|
-
for (const request of requests) {
|
|
150
|
-
try {
|
|
151
|
-
const response = request.next(request.context);
|
|
152
|
-
request.resolve(response);
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
request.reject(error);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
// Create shared batch queue
|
|
160
|
-
const queue = new BatchQueue(opts, processBatch);
|
|
161
|
-
return (next) => {
|
|
162
|
-
return async function* (context) {
|
|
163
|
-
// Get batch key for this request
|
|
164
|
-
const key = opts.getBatchKey(context.message);
|
|
165
|
-
// Create promise for batched response
|
|
166
|
-
const responsePromise = new Promise((resolve, reject) => {
|
|
167
|
-
queue.add(key, {
|
|
168
|
-
context,
|
|
169
|
-
next: next,
|
|
170
|
-
resolve: resolve,
|
|
171
|
-
reject,
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
// Wait for batch to complete and yield response
|
|
175
|
-
const response = await responsePromise;
|
|
176
|
-
yield* response;
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Get batching statistics.
|
|
182
|
-
*
|
|
183
|
-
* Note: This is a placeholder - proper implementation would
|
|
184
|
-
* require exposing the queue through the middleware instance.
|
|
185
|
-
*/
|
|
186
|
-
export function getBatchingStats(_middleware) {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Create adaptive batching middleware.
|
|
191
|
-
*
|
|
192
|
-
* Automatically adjusts batch size based on request patterns.
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```typescript
|
|
196
|
-
* client.use(createAdaptiveBatchingMiddleware({
|
|
197
|
-
* minBatchSize: 2,
|
|
198
|
-
* maxBatchSize: 50,
|
|
199
|
-
* targetLatency: 20 // Target 20ms latency
|
|
200
|
-
* }));
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
export function createAdaptiveBatchingMiddleware(options = {}) {
|
|
204
|
-
const { minBatchSize = 2, maxBatchSize = 50, targetLatency = 20, } = options;
|
|
205
|
-
let currentBatchSize = minBatchSize;
|
|
206
|
-
let recentLatencies = [];
|
|
207
|
-
/**
|
|
208
|
-
* Adjust batch size based on latency.
|
|
209
|
-
*/
|
|
210
|
-
function adjustBatchSize() {
|
|
211
|
-
if (recentLatencies.length < 5) {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
const avgLatency = recentLatencies.reduce((sum, l) => sum + l, 0) / recentLatencies.length;
|
|
215
|
-
if (avgLatency > targetLatency && currentBatchSize > minBatchSize) {
|
|
216
|
-
// Too slow - reduce batch size
|
|
217
|
-
currentBatchSize = Math.max(minBatchSize, currentBatchSize - 1);
|
|
218
|
-
}
|
|
219
|
-
else if (avgLatency < targetLatency * 0.5 && currentBatchSize < maxBatchSize) {
|
|
220
|
-
// Too fast - increase batch size
|
|
221
|
-
currentBatchSize = Math.min(maxBatchSize, currentBatchSize + 1);
|
|
222
|
-
}
|
|
223
|
-
// Keep only recent latencies
|
|
224
|
-
recentLatencies = recentLatencies.slice(-10);
|
|
225
|
-
}
|
|
226
|
-
return (next) => {
|
|
227
|
-
const batchingMiddleware = createBatchingMiddleware({
|
|
228
|
-
maxBatchSize: currentBatchSize,
|
|
229
|
-
maxWaitTime: targetLatency,
|
|
230
|
-
});
|
|
231
|
-
return async function* (context) {
|
|
232
|
-
const start = Date.now();
|
|
233
|
-
try {
|
|
234
|
-
const batchedNext = batchingMiddleware(next);
|
|
235
|
-
yield* batchedNext(context);
|
|
236
|
-
}
|
|
237
|
-
finally {
|
|
238
|
-
const latency = Date.now() - start;
|
|
239
|
-
recentLatencies.push(latency);
|
|
240
|
-
adjustBatchSize();
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
//# sourceMappingURL=batching.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"batching.js","sourceRoot":"","sources":["../../../src/client/middleware/batching.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA4DH;;GAEG;AACH,MAAM,UAAU;IASJ;IAOA;IAfF,QAAQ,GAAkC,IAAI,GAAG,EAAE,CAAC;IACpD,MAAM,GAA+C,IAAI,GAAG,EAAE,CAAC;IAC/D,KAAK,GAAG;QACd,aAAa,EAAE,CAAC;QAChB,YAAY,EAAE,CAAC;KAChB,CAAC;IAEF,YACU,OAMP,EACO,YAA+D;QAP/D,YAAO,GAAP,OAAO,CAMd;QACO,iBAAY,GAAZ,YAAY,CAA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BAAgB,GAAG,YAAY,EAAE,CAAC;YAC/E,iCAAiC;YACjC,gBAAgB,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC;QAClE,CAAC;QAED,6BAA6B;QAC7B,eAAe,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,CAAa,IAA8B,EAA4B,EAAE;QAC9E,MAAM,kBAAkB,GAAG,wBAAwB,CAAC;YAClD,YAAY,EAAE,gBAAgB;YAC9B,WAAW,EAAE,aAAa;SAC3B,CAAiC,CAAC;QAEnC,OAAO,KAAK,SAAS,CAAC,EAAE,OAA4B;YAClD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YAEzB,IAAI,CAAC;gBACH,MAAM,WAAW,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC;gBAC7C,KAAK,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;YAC9B,CAAC;oBAAS,CAAC;gBACT,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;gBACnC,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAC9B,eAAe,EAAE,CAAC;YACpB,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AsyncQueue - An async iterable queue with backpressure support.
|
|
3
|
-
*
|
|
4
|
-
* Designed for producer-consumer patterns where producers can wait when
|
|
5
|
-
* the queue is full and consumers can wait when the queue is empty.
|
|
6
|
-
* Implements AsyncIterable for use with for-await-of loops.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* const queue = asyncQueue<number>({ capacity: 10 })
|
|
10
|
-
*
|
|
11
|
-
* // Producer
|
|
12
|
-
* await queue.put(42) // Waits if queue is full
|
|
13
|
-
*
|
|
14
|
-
* // Consumer
|
|
15
|
-
* const value = await queue.take() // Waits if queue is empty
|
|
16
|
-
*
|
|
17
|
-
* // Async iteration
|
|
18
|
-
* for await (const value of queue) {
|
|
19
|
-
* console.log(value)
|
|
20
|
-
* }
|
|
21
|
-
*/
|
|
22
|
-
import type { AsyncQueue as IAsyncQueue } from "../interfaces/queue.js";
|
|
23
|
-
/**
|
|
24
|
-
* Options for creating an AsyncQueue.
|
|
25
|
-
*/
|
|
26
|
-
export interface AsyncQueueOptions {
|
|
27
|
-
/**
|
|
28
|
-
* Maximum capacity. Infinity for unbounded queue.
|
|
29
|
-
* @default Infinity
|
|
30
|
-
*/
|
|
31
|
-
capacity?: number;
|
|
32
|
-
/**
|
|
33
|
-
* Timeout for put/take operations in milliseconds.
|
|
34
|
-
* @default undefined (no timeout)
|
|
35
|
-
*/
|
|
36
|
-
timeout?: number;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* AsyncQueue<T> - Thread-safe async queue with backpressure.
|
|
40
|
-
*
|
|
41
|
-
* Features:
|
|
42
|
-
* - Blocking put() when full (backpressure)
|
|
43
|
-
* - Blocking take() when empty
|
|
44
|
-
* - AsyncIterable support
|
|
45
|
-
* - Graceful closing
|
|
46
|
-
* - Timeouts
|
|
47
|
-
*
|
|
48
|
-
* Perfect for:
|
|
49
|
-
* - Producer-consumer patterns
|
|
50
|
-
* - Rate limiting
|
|
51
|
-
* - Task queues
|
|
52
|
-
* - Stream processing
|
|
53
|
-
*/
|
|
54
|
-
export declare class AsyncQueue<T> implements IAsyncQueue<T> {
|
|
55
|
-
private buffer;
|
|
56
|
-
private putters;
|
|
57
|
-
private takers;
|
|
58
|
-
private _isClosed;
|
|
59
|
-
private readonly capacity;
|
|
60
|
-
private readonly defaultTimeout?;
|
|
61
|
-
constructor(options?: AsyncQueueOptions);
|
|
62
|
-
get size(): number;
|
|
63
|
-
get isEmpty(): boolean;
|
|
64
|
-
get isFull(): boolean;
|
|
65
|
-
get isClosed(): boolean;
|
|
66
|
-
get remainingCapacity(): number;
|
|
67
|
-
/**
|
|
68
|
-
* Adds an element to the queue.
|
|
69
|
-
* If the queue is full, waits until space is available.
|
|
70
|
-
*
|
|
71
|
-
* @throws Error if queue is closed
|
|
72
|
-
* @throws Error if timeout expires
|
|
73
|
-
*/
|
|
74
|
-
put(element: T, timeout?: number): Promise<void>;
|
|
75
|
-
/**
|
|
76
|
-
* Removes and returns an element from the queue.
|
|
77
|
-
* If the queue is empty, waits until an element is available.
|
|
78
|
-
*
|
|
79
|
-
* @throws Error if queue is closed and empty
|
|
80
|
-
* @throws Error if timeout expires
|
|
81
|
-
*/
|
|
82
|
-
take(timeout?: number): Promise<T>;
|
|
83
|
-
/**
|
|
84
|
-
* Attempts to add an element without waiting.
|
|
85
|
-
* Returns false if queue is full.
|
|
86
|
-
*/
|
|
87
|
-
tryPut(element: T): boolean;
|
|
88
|
-
/**
|
|
89
|
-
* Attempts to remove an element without waiting.
|
|
90
|
-
* Returns undefined if queue is empty.
|
|
91
|
-
*/
|
|
92
|
-
tryTake(): T | undefined;
|
|
93
|
-
/**
|
|
94
|
-
* Returns the next element without removing it.
|
|
95
|
-
* Waits if queue is empty.
|
|
96
|
-
*/
|
|
97
|
-
peek(): Promise<T>;
|
|
98
|
-
/**
|
|
99
|
-
* Returns the next element without waiting.
|
|
100
|
-
* Returns undefined if queue is empty.
|
|
101
|
-
*/
|
|
102
|
-
tryPeek(): T | undefined;
|
|
103
|
-
/**
|
|
104
|
-
* Closes the queue. No more elements can be added.
|
|
105
|
-
* Existing elements can still be consumed.
|
|
106
|
-
*/
|
|
107
|
-
close(): void;
|
|
108
|
-
/**
|
|
109
|
-
* Drains all elements into an array.
|
|
110
|
-
* Returns immediately with current elements.
|
|
111
|
-
*/
|
|
112
|
-
drain(): T[];
|
|
113
|
-
/**
|
|
114
|
-
* Async iterator that yields elements as they become available.
|
|
115
|
-
* Stops when queue is closed and empty.
|
|
116
|
-
*/
|
|
117
|
-
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
118
|
-
private fulfillPutter;
|
|
119
|
-
private clearTimeout;
|
|
120
|
-
/**
|
|
121
|
-
* Returns statistics about the queue.
|
|
122
|
-
*/
|
|
123
|
-
getStats(): {
|
|
124
|
-
size: number;
|
|
125
|
-
capacity: number;
|
|
126
|
-
isEmpty: boolean;
|
|
127
|
-
isFull: boolean;
|
|
128
|
-
isClosed: boolean;
|
|
129
|
-
waitingPutters: number;
|
|
130
|
-
waitingTakers: number;
|
|
131
|
-
remainingCapacity: number;
|
|
132
|
-
};
|
|
133
|
-
toString(): string;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Factory function to create an AsyncQueue.
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* const queue = asyncQueue<number>({ capacity: 100 })
|
|
140
|
-
* await queue.put(42)
|
|
141
|
-
* const value = await queue.take()
|
|
142
|
-
*/
|
|
143
|
-
export declare function asyncQueue<T>(options?: AsyncQueueOptions): AsyncQueue<T>;
|
|
144
|
-
//# sourceMappingURL=async-queue.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"async-queue.d.ts","sourceRoot":"","sources":["../../../src/collections/async/async-queue.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,UAAU,IAAI,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAExE;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAWD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,UAAU,CAAC,CAAC,CAAE,YAAW,WAAW,CAAC,CAAC,CAAC;IAClD,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,SAA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