@private.me/xbind 1.3.5 → 3.0.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/LICENSES.md +212 -0
- package/README.md +388 -6
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
- package/dist-standalone/_deps/shared/cjs/errors.js +1 -639
- package/dist-standalone/_deps/shared/cjs/index.js +1 -496
- package/dist-standalone/_deps/shared/cjs/types.js +1 -317
- package/dist-standalone/_deps/shared/errors.js +1 -255
- package/dist-standalone/_deps/shared/index.js +1 -74
- package/dist-standalone/_deps/shared/types.js +1 -90
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +1 -642
- package/dist-standalone/agent-sdk.js +1 -328
- package/dist-standalone/agent.d.ts +95 -5
- package/dist-standalone/agent.js +1 -1545
- package/dist-standalone/approval.js +1 -193
- package/dist-standalone/async-iterators.d.ts +275 -0
- package/dist-standalone/async-iterators.js +1 -0
- package/dist-standalone/auth.js +1 -219
- package/dist-standalone/auto-accept.js +1 -229
- package/dist-standalone/backup-config.js +1 -201
- package/dist-standalone/backup.d.ts +114 -0
- package/dist-standalone/backup.js +1 -0
- package/dist-standalone/batch-operations.d.ts +297 -0
- package/dist-standalone/batch-operations.js +1 -0
- package/dist-standalone/cancellation.d.ts +301 -0
- package/dist-standalone/cancellation.js +1 -0
- package/dist-standalone/checkpoint.js +1 -186
- package/dist-standalone/circuit-breaker.d.ts +351 -0
- package/dist-standalone/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/agent-call.js +1 -651
- package/dist-standalone/cjs/agent-sdk.js +1 -332
- package/dist-standalone/cjs/agent.js +1 -1582
- package/dist-standalone/cjs/approval.js +1 -199
- package/dist-standalone/cjs/async-iterators.js +1 -0
- package/dist-standalone/cjs/auth.js +1 -225
- package/dist-standalone/cjs/auto-accept.js +1 -233
- package/dist-standalone/cjs/backup-config.js +1 -207
- package/dist-standalone/cjs/backup.js +1 -0
- package/dist-standalone/cjs/batch-operations.js +1 -0
- package/dist-standalone/cjs/cancellation.js +1 -0
- package/dist-standalone/cjs/checkpoint.js +1 -193
- package/dist-standalone/cjs/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/cli/init.js +1 -486
- package/dist-standalone/cjs/config-validation.js +1 -0
- package/dist-standalone/cjs/connect.js +1 -312
- package/dist-standalone/cjs/connection-pool.js +1 -0
- package/dist-standalone/cjs/correlation-id.js +1 -339
- package/dist-standalone/cjs/crypto-utils.js +1 -0
- package/dist-standalone/cjs/debug-mode.js +1 -0
- package/dist-standalone/cjs/did-document.js +1 -101
- package/dist-standalone/cjs/did-privateme.js +1 -130
- package/dist-standalone/cjs/did-web.js +1 -201
- package/dist-standalone/cjs/discovery.js +1 -462
- package/dist-standalone/cjs/dual-mode.js +1 -251
- package/dist-standalone/cjs/email-templates.js +1 -313
- package/dist-standalone/cjs/email-transport.js +1 -239
- package/dist-standalone/cjs/envelope.js +1 -510
- package/dist-standalone/cjs/errors.js +1 -826
- package/dist-standalone/cjs/event-emitter.js +1 -0
- package/dist-standalone/cjs/gateway-state.js +1 -55
- package/dist-standalone/cjs/gateway-transport.js +1 -120
- package/dist-standalone/cjs/graceful-degradation.js +1 -0
- package/dist-standalone/cjs/guardrails.js +1 -223
- package/dist-standalone/cjs/health-check.js +1 -0
- package/dist-standalone/cjs/http-compat.js +1 -272
- package/dist-standalone/cjs/http-status-map.js +1 -571
- package/dist-standalone/cjs/identity.js +1 -540
- package/dist-standalone/cjs/index.js +1 -237
- package/dist-standalone/cjs/invitation.js +1 -421
- package/dist-standalone/cjs/invite.js +1 -328
- package/dist-standalone/cjs/key-agreement.js +1 -246
- package/dist-standalone/cjs/lazy-init.js +1 -300
- package/dist-standalone/cjs/logger.js +1 -0
- package/dist-standalone/cjs/mdns-discovery.js +1 -202
- package/dist-standalone/cjs/nonce-store.js +1 -66
- package/dist-standalone/cjs/pairing-manager.js +1 -223
- package/dist-standalone/cjs/plugin-system.js +1 -0
- package/dist-standalone/cjs/plugins/logging.js +1 -0
- package/dist-standalone/cjs/plugins/metrics.js +1 -0
- package/dist-standalone/cjs/plugins/validation.js +1 -0
- package/dist-standalone/cjs/policy.js +1 -320
- package/dist-standalone/cjs/progress-callbacks.js +1 -0
- package/dist-standalone/cjs/redis-nonce-store.js +1 -76
- package/dist-standalone/cjs/registry-middleware.js +1 -50
- package/dist-standalone/cjs/retry-strategies.js +1 -0
- package/dist-standalone/cjs/retry-transport.js +1 -102
- package/dist-standalone/cjs/runtime/browser.js +1 -0
- package/dist-standalone/cjs/runtime/edge.js +1 -0
- package/dist-standalone/cjs/runtime/react-native.js +1 -0
- package/dist-standalone/cjs/security-policy.js +1 -245
- package/dist-standalone/cjs/serialization.js +1 -0
- package/dist-standalone/cjs/split-channel.js +1 -177
- package/dist-standalone/cjs/subscription-proof.js +1 -230
- package/dist-standalone/cjs/succession.js +1 -148
- package/dist-standalone/cjs/timeouts.js +1 -0
- package/dist-standalone/cjs/trace-context.js +1 -0
- package/dist-standalone/cjs/trace-spans.js +1 -0
- package/dist-standalone/cjs/transport.js +1 -63
- package/dist-standalone/cjs/trust-registry.js +1 -742
- package/dist-standalone/cjs/types/error-response.js +1 -56
- package/dist-standalone/cjs/vault-auth.js +1 -0
- package/dist-standalone/cjs/vault-store-loader.js +1 -0
- package/dist-standalone/cjs/verify.js +1 -25
- package/dist-standalone/cjs/version-info.js +1 -0
- package/dist-standalone/cjs/xfetch.js +1 -252
- package/dist-standalone/cli/init.js +1 -449
- package/dist-standalone/cli/setup.js +1 -514
- package/dist-standalone/cli/types.js +1 -27
- package/dist-standalone/cli/xbind.js +1 -148
- package/dist-standalone/config-validation.d.ts +185 -0
- package/dist-standalone/config-validation.js +1 -0
- package/dist-standalone/connect.js +1 -274
- package/dist-standalone/connection-pool.d.ts +251 -0
- package/dist-standalone/connection-pool.js +1 -0
- package/dist-standalone/correlation-id.js +1 -326
- package/dist-standalone/crypto-utils.d.ts +60 -0
- package/dist-standalone/crypto-utils.js +1 -0
- package/dist-standalone/debug-mode.d.ts +286 -0
- package/dist-standalone/debug-mode.js +1 -0
- package/dist-standalone/did-document.js +1 -96
- package/dist-standalone/did-privateme.js +1 -121
- package/dist-standalone/did-web.js +1 -196
- package/dist-standalone/discovery.js +1 -458
- package/dist-standalone/dual-mode.js +1 -247
- package/dist-standalone/email-templates.js +1 -309
- package/dist-standalone/email-transport.js +1 -232
- package/dist-standalone/envelope.d.ts +29 -1
- package/dist-standalone/envelope.js +1 -497
- package/dist-standalone/errors.d.ts +10 -0
- package/dist-standalone/errors.js +1 -811
- package/dist-standalone/event-emitter.d.ts +395 -0
- package/dist-standalone/event-emitter.js +1 -0
- package/dist-standalone/gateway-state.js +1 -51
- package/dist-standalone/gateway-transport.js +1 -116
- package/dist-standalone/graceful-degradation.d.ts +246 -0
- package/dist-standalone/graceful-degradation.js +1 -0
- package/dist-standalone/guardrails.js +1 -216
- package/dist-standalone/health-check.d.ts +150 -0
- package/dist-standalone/health-check.js +1 -0
- package/dist-standalone/http-compat.js +1 -267
- package/dist-standalone/http-status-map.js +1 -561
- package/dist-standalone/identity.d.ts +64 -1
- package/dist-standalone/identity.js +1 -515
- package/dist-standalone/index.d.ts +45 -3
- package/dist-standalone/index.js +1 -52
- package/dist-standalone/invitation.js +1 -415
- package/dist-standalone/invite.js +1 -324
- package/dist-standalone/key-agreement.d.ts +61 -13
- package/dist-standalone/key-agreement.js +1 -236
- package/dist-standalone/lazy-init.js +1 -295
- package/dist-standalone/logger.d.ts +77 -0
- package/dist-standalone/logger.js +1 -0
- package/dist-standalone/mdns-discovery.js +1 -195
- package/dist-standalone/nonce-store.d.ts +16 -3
- package/dist-standalone/nonce-store.js +1 -62
- package/dist-standalone/package.json +0 -1
- package/dist-standalone/pairing-manager.js +1 -219
- package/dist-standalone/plugin-system.d.ts +145 -0
- package/dist-standalone/plugin-system.js +1 -0
- package/dist-standalone/policy.js +1 -315
- package/dist-standalone/progress-callbacks.d.ts +394 -0
- package/dist-standalone/progress-callbacks.js +1 -0
- package/dist-standalone/redis-nonce-store.js +1 -72
- package/dist-standalone/registry-middleware.js +1 -47
- package/dist-standalone/retry-strategies.d.ts +382 -0
- package/dist-standalone/retry-strategies.js +1 -0
- package/dist-standalone/retry-transport.js +1 -98
- package/dist-standalone/security-policy.js +1 -239
- package/dist-standalone/serialization.d.ts +244 -0
- package/dist-standalone/serialization.js +1 -0
- package/dist-standalone/split-channel.d.ts +49 -1
- package/dist-standalone/split-channel.js +1 -171
- package/dist-standalone/subscription-proof.js +1 -224
- package/dist-standalone/succession.js +1 -142
- package/dist-standalone/timeouts.d.ts +275 -0
- package/dist-standalone/timeouts.js +1 -0
- package/dist-standalone/trace-context.d.ts +252 -0
- package/dist-standalone/trace-context.js +1 -0
- package/dist-standalone/trace-spans.d.ts +360 -0
- package/dist-standalone/trace-spans.js +1 -0
- package/dist-standalone/transport.js +1 -59
- package/dist-standalone/trust-registry.d.ts +106 -5
- package/dist-standalone/trust-registry.js +1 -702
- package/dist-standalone/vault-auth.d.ts +91 -0
- package/dist-standalone/vault-auth.js +1 -0
- package/dist-standalone/vault-store-loader.d.ts +110 -0
- package/dist-standalone/vault-store-loader.js +1 -0
- package/dist-standalone/verify.js +1 -16
- package/dist-standalone/version-info.d.ts +259 -0
- package/dist-standalone/version-info.js +1 -0
- package/dist-standalone/xfetch.js +1 -247
- package/llms.txt +1 -0
- package/package.json +65 -5
- package/share1.dat +0 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +0 -29
- package/dist-standalone/_deps/crypto/base64.js +0 -222
- package/dist-standalone/_deps/crypto/cjs/base64.js +0 -665
- package/dist-standalone/_deps/crypto/cjs/errors.js +0 -675
- package/dist-standalone/_deps/crypto/cjs/hmac.js +0 -473
- package/dist-standalone/_deps/crypto/cjs/index.js +0 -852
- package/dist-standalone/_deps/crypto/cjs/package.json +0 -1
- package/dist-standalone/_deps/crypto/cjs/padding.js +0 -511
- package/dist-standalone/_deps/crypto/cjs/share-header.js +0 -372
- package/dist-standalone/_deps/crypto/cjs/shares.js +0 -874
- package/dist-standalone/_deps/crypto/cjs/tlv.js +0 -1021
- package/dist-standalone/_deps/crypto/cjs/uuid.js +0 -443
- package/dist-standalone/_deps/crypto/cjs/verify.js +0 -414
- package/dist-standalone/_deps/crypto/cjs/xorida.js +0 -923
- package/dist-standalone/_deps/crypto/errors.d.ts +0 -51
- package/dist-standalone/_deps/crypto/errors.js +0 -199
- package/dist-standalone/_deps/crypto/hmac.d.ts +0 -39
- package/dist-standalone/_deps/crypto/hmac.js +0 -134
- package/dist-standalone/_deps/crypto/index.d.ts +0 -20
- package/dist-standalone/_deps/crypto/index.js +0 -145
- package/dist-standalone/_deps/crypto/padding.d.ts +0 -19
- package/dist-standalone/_deps/crypto/padding.js +0 -159
- package/dist-standalone/_deps/crypto/share-header.d.ts +0 -44
- package/dist-standalone/_deps/crypto/share-header.js +0 -92
- package/dist-standalone/_deps/crypto/shares.d.ts +0 -27
- package/dist-standalone/_deps/crypto/shares.js +0 -295
- package/dist-standalone/_deps/crypto/tlv.d.ts +0 -26
- package/dist-standalone/_deps/crypto/tlv.js +0 -364
- package/dist-standalone/_deps/crypto/uuid.d.ts +0 -22
- package/dist-standalone/_deps/crypto/uuid.js +0 -136
- package/dist-standalone/_deps/crypto/verify.d.ts +0 -15
- package/dist-standalone/_deps/crypto/verify.js +0 -71
- package/dist-standalone/_deps/crypto/xorida.d.ts +0 -44
- package/dist-standalone/_deps/crypto/xorida.js +0 -366
- package/dist-standalone/_deps/shared/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/errors.js.map +0 -1
- package/dist-standalone/_deps/shared/index.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/index.js.map +0 -1
- package/dist-standalone/_deps/shared/types.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.js.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.js.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.js.map +0 -1
- package/dist-standalone/_deps/xregistry/index.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/index.js.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.js.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.js.map +0 -1
- package/dist-standalone/_deps/xregistry/types.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/types.js.map +0 -1
|
@@ -1,193 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @module approval
|
|
3
|
-
* OAuth-style approval flow for agents
|
|
4
|
-
*
|
|
5
|
-
* Enterprise agents require explicit user consent before performing
|
|
6
|
-
* sensitive operations. This module implements OAuth-style consent
|
|
7
|
-
* screens and approval tokens.
|
|
8
|
-
*/
|
|
9
|
-
import { ok, err } from"./_deps/shared/index.js";
|
|
10
|
-
/**
|
|
11
|
-
* Approval error codes
|
|
12
|
-
*/
|
|
13
|
-
export var ApprovalErrorCode;
|
|
14
|
-
(function (ApprovalErrorCode) {
|
|
15
|
-
ApprovalErrorCode["USER_DENIED"] = "APPROVAL_USER_DENIED";
|
|
16
|
-
ApprovalErrorCode["TIMEOUT"] = "APPROVAL_TIMEOUT";
|
|
17
|
-
ApprovalErrorCode["INVALID_DURATION"] = "APPROVAL_INVALID_DURATION";
|
|
18
|
-
ApprovalErrorCode["SIGNATURE_FAILED"] = "APPROVAL_SIGNATURE_FAILED";
|
|
19
|
-
ApprovalErrorCode["TOKEN_EXPIRED"] = "APPROVAL_TOKEN_EXPIRED";
|
|
20
|
-
ApprovalErrorCode["TOKEN_REVOKED"] = "APPROVAL_TOKEN_REVOKED";
|
|
21
|
-
ApprovalErrorCode["INVALID_TOKEN"] = "APPROVAL_INVALID_TOKEN";
|
|
22
|
-
})(ApprovalErrorCode || (ApprovalErrorCode = {}));
|
|
23
|
-
/**
|
|
24
|
-
* Approval error
|
|
25
|
-
*/
|
|
26
|
-
export class ApprovalError extends Error {
|
|
27
|
-
code;
|
|
28
|
-
details;
|
|
29
|
-
constructor(code, message, details) {
|
|
30
|
-
super(message);
|
|
31
|
-
this.code = code;
|
|
32
|
-
this.details = details;
|
|
33
|
-
this.name = 'ApprovalError';
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* CLI approval presenter (prints to console, reads stdin)
|
|
38
|
-
* This is a legitimate CLI interface - console output is intentional
|
|
39
|
-
*/
|
|
40
|
-
export class CLIApprovalPresenter {
|
|
41
|
-
/* eslint-disable no-console */
|
|
42
|
-
async present(options) {
|
|
43
|
-
console.log('\n=== AGENT APPROVAL REQUEST ===');
|
|
44
|
-
console.log(`Agent: ${options.agentDid}`);
|
|
45
|
-
console.log(`Duration: ${options.duration}`);
|
|
46
|
-
console.log('\nRequested Scopes:');
|
|
47
|
-
for (const scope of options.scopes) {
|
|
48
|
-
console.log(` - ${scope}`);
|
|
49
|
-
}
|
|
50
|
-
if (options.limits) {
|
|
51
|
-
console.log('\nPolicy Limits:');
|
|
52
|
-
if (options.limits.amountPerTxn) {
|
|
53
|
-
console.log(` - Max per transaction: $${options.limits.amountPerTxn.toLocaleString()}`);
|
|
54
|
-
}
|
|
55
|
-
if (options.limits.dailyAmount) {
|
|
56
|
-
console.log(` - Daily limit: $${options.limits.dailyAmount.toLocaleString()}`);
|
|
57
|
-
}
|
|
58
|
-
if (options.limits.callsPerMinute) {
|
|
59
|
-
console.log(` - Rate limit: ${options.limits.callsPerMinute} calls/minute`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (options.description) {
|
|
63
|
-
console.log(`\nDescription: ${options.description}`);
|
|
64
|
-
}
|
|
65
|
-
console.log('\n[This is a mock presenter - auto-approving for development]');
|
|
66
|
-
console.log('In production, this would prompt for user input.\n');
|
|
67
|
-
// Auto-approve for development (production would prompt for y/n)
|
|
68
|
-
return ok({
|
|
69
|
-
approved: true,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Approval flow coordinator
|
|
75
|
-
*
|
|
76
|
-
* Orchestrates the consent flow: present to user → sign token → track expiry
|
|
77
|
-
*/
|
|
78
|
-
export class ApprovalFlow {
|
|
79
|
-
presenter;
|
|
80
|
-
tokens = new Map();
|
|
81
|
-
constructor(options = {}) {
|
|
82
|
-
this.presenter = options.presenter ?? new CLIApprovalPresenter();
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Request approval from user
|
|
86
|
-
*
|
|
87
|
-
* @param options - Approval request options
|
|
88
|
-
* @returns Approval result with token (if approved)
|
|
89
|
-
*/
|
|
90
|
-
async requestApproval(options) {
|
|
91
|
-
// Validate duration
|
|
92
|
-
const durationMs = this.parseDuration(options.duration);
|
|
93
|
-
if (durationMs <= 0) {
|
|
94
|
-
return err(new ApprovalError(ApprovalErrorCode.INVALID_DURATION, `Invalid duration: ${options.duration}`));
|
|
95
|
-
}
|
|
96
|
-
// Present to user
|
|
97
|
-
const result = await this.presenter.present(options);
|
|
98
|
-
if (!result.ok)
|
|
99
|
-
return result;
|
|
100
|
-
if (!result.value.approved) {
|
|
101
|
-
return ok({
|
|
102
|
-
approved: false,
|
|
103
|
-
reason: result.value.reason ?? 'User denied approval',
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
// Generate approval token
|
|
107
|
-
const now = Date.now();
|
|
108
|
-
const token = {
|
|
109
|
-
id: this.generateTokenId(),
|
|
110
|
-
agentDid: options.agentDid,
|
|
111
|
-
scopes: options.scopes,
|
|
112
|
-
limits: options.limits,
|
|
113
|
-
expiresAt: now + durationMs,
|
|
114
|
-
createdAt: now,
|
|
115
|
-
valid: true,
|
|
116
|
-
signature: await this.signToken(options.agentDid, options.scopes, now + durationMs),
|
|
117
|
-
};
|
|
118
|
-
// Store token
|
|
119
|
-
this.tokens.set(token.id, token);
|
|
120
|
-
return ok({
|
|
121
|
-
approved: true,
|
|
122
|
-
token,
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Verify an approval token
|
|
127
|
-
*
|
|
128
|
-
* @param tokenId - Token ID to verify
|
|
129
|
-
* @returns Token if valid, error otherwise
|
|
130
|
-
*/
|
|
131
|
-
verifyToken(tokenId) {
|
|
132
|
-
const token = this.tokens.get(tokenId);
|
|
133
|
-
if (!token) {
|
|
134
|
-
return err(new ApprovalError(ApprovalErrorCode.INVALID_TOKEN, `Token ${tokenId} not found`));
|
|
135
|
-
}
|
|
136
|
-
if (!token.valid) {
|
|
137
|
-
return err(new ApprovalError(ApprovalErrorCode.TOKEN_REVOKED, `Token ${tokenId} has been revoked`));
|
|
138
|
-
}
|
|
139
|
-
if (Date.now() > token.expiresAt) {
|
|
140
|
-
return err(new ApprovalError(ApprovalErrorCode.TOKEN_EXPIRED, `Token ${tokenId} expired at ${new Date(token.expiresAt).toISOString()}`));
|
|
141
|
-
}
|
|
142
|
-
return ok(token);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Revoke an approval token
|
|
146
|
-
*
|
|
147
|
-
* @param tokenId - Token ID to revoke
|
|
148
|
-
*/
|
|
149
|
-
revokeToken(tokenId) {
|
|
150
|
-
const token = this.tokens.get(tokenId);
|
|
151
|
-
if (token) {
|
|
152
|
-
this.tokens.set(tokenId, { ...token, valid: false });
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Parse duration string to milliseconds
|
|
157
|
-
*/
|
|
158
|
-
parseDuration(duration) {
|
|
159
|
-
if (typeof duration === 'number')
|
|
160
|
-
return duration;
|
|
161
|
-
// Parse ISO 8601 duration or simple format
|
|
162
|
-
const matches = duration.match(/^(\d+)([smhd])$/);
|
|
163
|
-
if (!matches)
|
|
164
|
-
return -1;
|
|
165
|
-
const value = parseInt(matches[1] ?? '0', 10);
|
|
166
|
-
const unit = matches[2];
|
|
167
|
-
switch (unit) {
|
|
168
|
-
case 's': return value * 1000;
|
|
169
|
-
case 'm': return value * 60 * 1000;
|
|
170
|
-
case 'h': return value * 60 * 60 * 1000;
|
|
171
|
-
case 'd': return value * 24 * 60 * 60 * 1000;
|
|
172
|
-
default: return -1;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Generate a unique token ID
|
|
177
|
-
*/
|
|
178
|
-
generateTokenId() {
|
|
179
|
-
const randomBytes = new Uint8Array(12);
|
|
180
|
-
crypto.getRandomValues(randomBytes);
|
|
181
|
-
const randomString = Array.from(randomBytes, b => b.toString(16).padStart(2, '0')).join('');
|
|
182
|
-
return `appr_${Date.now()}_${randomString}`;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Sign an approval token (stub - production would use Ed25519)
|
|
186
|
-
*/
|
|
187
|
-
async signToken(agentDid, scopes, expiresAt) {
|
|
188
|
-
// Production would use Ed25519 signature over JSON.stringify({ agentDid, scopes, expiresAt })
|
|
189
|
-
// For now, return a mock signature
|
|
190
|
-
const payload = JSON.stringify({ agentDid, scopes, expiresAt });
|
|
191
|
-
return btoa(payload).substring(0, 32);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
1
|
+
import{ok,err}from"./_deps/shared/index.js";export var ApprovalErrorCode;!function(e){e.USER_DENIED="APPROVAL_USER_DENIED",e.TIMEOUT="APPROVAL_TIMEOUT",e.INVALID_DURATION="APPROVAL_INVALID_DURATION",e.SIGNATURE_FAILED="APPROVAL_SIGNATURE_FAILED",e.TOKEN_EXPIRED="APPROVAL_TOKEN_EXPIRED",e.TOKEN_REVOKED="APPROVAL_TOKEN_REVOKED",e.INVALID_TOKEN="APPROVAL_INVALID_TOKEN"}(ApprovalErrorCode||(ApprovalErrorCode={}));export class ApprovalError extends Error{code;details;constructor(e,r,o){super(r),this.code=e,this.details=o,this.name="ApprovalError"}}export class CLIApprovalPresenter{async present(e){console.log("\n=== AGENT APPROVAL REQUEST ==="),console.log(`Agent: ${e.agentDid}`),console.log(`Duration: ${e.duration}`),console.log("\nRequested Scopes:");for(const r of e.scopes)console.log(` - ${r}`);return e.limits&&(console.log("\nPolicy Limits:"),e.limits.amountPerTxn&&console.log(` - Max per transaction: $${e.limits.amountPerTxn.toLocaleString()}`),e.limits.dailyAmount&&console.log(` - Daily limit: $${e.limits.dailyAmount.toLocaleString()}`),e.limits.callsPerMinute&&console.log(` - Rate limit: ${e.limits.callsPerMinute} calls/minute`)),e.description&&console.log(`\nDescription: ${e.description}`),console.log("\n[This is a mock presenter - auto-approving for development]"),console.log("In production, this would prompt for user input.\n"),ok({approved:!0})}}export class ApprovalFlow{presenter;tokens=new Map;constructor(e={}){this.presenter=e.presenter??new CLIApprovalPresenter}async requestApproval(e){const r=this.parseDuration(e.duration);if(r<=0)return err(new ApprovalError(ApprovalErrorCode.INVALID_DURATION,`Invalid duration: ${e.duration}`));const o=await this.presenter.present(e);if(!o.ok)return o;if(!o.value.approved)return ok({approved:!1,reason:o.value.reason??"User denied approval"});const t=Date.now(),n={id:this.generateTokenId(),agentDid:e.agentDid,scopes:e.scopes,limits:e.limits,expiresAt:t+r,createdAt:t,valid:!0,signature:await this.signToken(e.agentDid,e.scopes,t+r)};return this.tokens.set(n.id,n),ok({approved:!0,token:n})}verifyToken(e){const r=this.tokens.get(e);return r?r.valid?Date.now()>r.expiresAt?err(new ApprovalError(ApprovalErrorCode.TOKEN_EXPIRED,`Token ${e} expired at ${new Date(r.expiresAt).toISOString()}`)):ok(r):err(new ApprovalError(ApprovalErrorCode.TOKEN_REVOKED,`Token ${e} has been revoked`)):err(new ApprovalError(ApprovalErrorCode.INVALID_TOKEN,`Token ${e} not found`))}revokeToken(e){const r=this.tokens.get(e);r&&this.tokens.set(e,{...r,valid:!1})}parseDuration(e){if("number"==typeof e)return e;const r=e.match(/^(\d+)([smhd])$/);if(!r)return-1;const o=parseInt(r[1]??"0",10);switch(r[2]){case"s":return 1e3*o;case"m":return 60*o*1e3;case"h":return 60*o*60*1e3;case"d":return 24*o*60*60*1e3;default:return-1}}generateTokenId(){const e=new Uint8Array(12);crypto.getRandomValues(e);const r=Array.from(e,e=>e.toString(16).padStart(2,"0")).join("");return`appr_${Date.now()}_${r}`}async signToken(e,r,o){const t=JSON.stringify({agentDid:e,scopes:r,expiresAt:o});return btoa(t).substring(0,32)}}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Async iterator API for xBind message streams.
|
|
3
|
+
*
|
|
4
|
+
* Provides modern async iteration patterns for consuming message streams
|
|
5
|
+
* with backpressure handling, stream cancellation, and error propagation.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Subscribe to messages with async iteration
|
|
10
|
+
* for await (const message of agent.subscribe({ from: 'did:key:alice' })) {
|
|
11
|
+
* console.log('Received:', message.payload);
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module async-iterators
|
|
16
|
+
*/
|
|
17
|
+
import type { Agent, AgentMessage } from './agent.js';
|
|
18
|
+
/**
|
|
19
|
+
* Options for message stream subscription.
|
|
20
|
+
*/
|
|
21
|
+
export interface MessageStreamOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Filter messages by sender DID.
|
|
24
|
+
* If undefined, accepts messages from all senders.
|
|
25
|
+
*/
|
|
26
|
+
readonly from?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Filter messages by scope.
|
|
29
|
+
* If undefined, accepts messages with any scope.
|
|
30
|
+
*/
|
|
31
|
+
readonly scope?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Maximum number of buffered messages before backpressure.
|
|
34
|
+
* When buffer is full, new messages are dropped until consumed.
|
|
35
|
+
* Default: 100
|
|
36
|
+
*/
|
|
37
|
+
readonly bufferSize?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Signal to abort the stream.
|
|
40
|
+
* When aborted, the iterator completes and cleans up resources.
|
|
41
|
+
*/
|
|
42
|
+
readonly signal?: AbortSignal;
|
|
43
|
+
/**
|
|
44
|
+
* Allow cleartext messages (signed but not encrypted).
|
|
45
|
+
* Default: false
|
|
46
|
+
*/
|
|
47
|
+
readonly allowCleartext?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Async iterable message stream.
|
|
51
|
+
*
|
|
52
|
+
* Implements AsyncIterableIterator for use with `for await...of` syntax.
|
|
53
|
+
* Handles backpressure, cancellation, and error propagation.
|
|
54
|
+
*/
|
|
55
|
+
export declare class MessageStream implements AsyncIterableIterator<AgentMessage> {
|
|
56
|
+
private readonly agent;
|
|
57
|
+
private readonly options;
|
|
58
|
+
private readonly state;
|
|
59
|
+
constructor(agent: Agent, options?: MessageStreamOptions);
|
|
60
|
+
/**
|
|
61
|
+
* Handle incoming envelope from transport.
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
private handleEnvelope;
|
|
65
|
+
/**
|
|
66
|
+
* Close the stream with optional error.
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
private close;
|
|
70
|
+
/**
|
|
71
|
+
* Get next message from the stream.
|
|
72
|
+
*
|
|
73
|
+
* Returns a promise that resolves when:
|
|
74
|
+
* - A message is available in the buffer
|
|
75
|
+
* - A new message arrives from the transport
|
|
76
|
+
* - The stream is closed
|
|
77
|
+
*
|
|
78
|
+
* @returns Promise resolving to iterator result
|
|
79
|
+
*/
|
|
80
|
+
next(): Promise<IteratorResult<AgentMessage>>;
|
|
81
|
+
/**
|
|
82
|
+
* Return method for iterator protocol.
|
|
83
|
+
* Closes the stream and completes iteration.
|
|
84
|
+
*
|
|
85
|
+
* @param value - Optional value (ignored)
|
|
86
|
+
* @returns Promise resolving to done result
|
|
87
|
+
*/
|
|
88
|
+
return(value?: any): Promise<IteratorResult<AgentMessage>>;
|
|
89
|
+
/**
|
|
90
|
+
* Throw method for iterator protocol.
|
|
91
|
+
* Closes the stream with error.
|
|
92
|
+
*
|
|
93
|
+
* @param error - Error to propagate
|
|
94
|
+
* @returns Promise resolving to done result
|
|
95
|
+
*/
|
|
96
|
+
throw(error?: any): Promise<IteratorResult<AgentMessage>>;
|
|
97
|
+
/**
|
|
98
|
+
* Make this object async iterable.
|
|
99
|
+
* Enables `for await...of` syntax.
|
|
100
|
+
*
|
|
101
|
+
* @returns This stream instance
|
|
102
|
+
*/
|
|
103
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<AgentMessage>;
|
|
104
|
+
/**
|
|
105
|
+
* Get number of buffered messages.
|
|
106
|
+
* Useful for monitoring backpressure.
|
|
107
|
+
*/
|
|
108
|
+
get bufferedCount(): number;
|
|
109
|
+
/**
|
|
110
|
+
* Check if stream is closed.
|
|
111
|
+
*/
|
|
112
|
+
get closed(): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Get stream error if any.
|
|
115
|
+
*/
|
|
116
|
+
get error(): Error | undefined;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Options for batch message collection.
|
|
120
|
+
*/
|
|
121
|
+
export interface CollectOptions extends MessageStreamOptions {
|
|
122
|
+
/**
|
|
123
|
+
* Maximum number of messages to collect.
|
|
124
|
+
* Stream completes after collecting this many messages.
|
|
125
|
+
* If undefined, collects indefinitely until stream ends.
|
|
126
|
+
*/
|
|
127
|
+
readonly limit?: number;
|
|
128
|
+
/**
|
|
129
|
+
* Timeout in milliseconds.
|
|
130
|
+
* Stream completes after this duration regardless of message count.
|
|
131
|
+
* If undefined, no timeout (waits indefinitely).
|
|
132
|
+
*/
|
|
133
|
+
readonly timeout?: number;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Collect messages from a stream into an array.
|
|
137
|
+
*
|
|
138
|
+
* Useful for batch processing or testing. Stream completes when:
|
|
139
|
+
* - Limit is reached
|
|
140
|
+
* - Timeout expires
|
|
141
|
+
* - Stream is aborted via signal
|
|
142
|
+
*
|
|
143
|
+
* @param agent - Agent instance to collect messages from
|
|
144
|
+
* @param options - Collection options
|
|
145
|
+
* @returns Promise resolving to array of messages
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // Collect up to 10 messages with 5 second timeout
|
|
150
|
+
* const messages = await collectMessages(agent, {
|
|
151
|
+
* from: 'did:key:alice',
|
|
152
|
+
* limit: 10,
|
|
153
|
+
* timeout: 5000
|
|
154
|
+
* });
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export declare function collectMessages(agent: Agent, options?: CollectOptions): Promise<AgentMessage[]>;
|
|
158
|
+
/**
|
|
159
|
+
* Transform message stream with async mapping function.
|
|
160
|
+
*
|
|
161
|
+
* Enables functional-style stream processing with backpressure preservation.
|
|
162
|
+
*
|
|
163
|
+
* @param stream - Source message stream
|
|
164
|
+
* @param fn - Async transform function
|
|
165
|
+
* @returns Async iterable of transformed values
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* const stream = agent.subscribe({ from: 'did:key:alice' });
|
|
170
|
+
* const payloads = mapStream(stream, async (msg) => msg.payload);
|
|
171
|
+
*
|
|
172
|
+
* for await (const payload of payloads) {
|
|
173
|
+
* console.log('Payload:', payload);
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export declare function mapStream<T, U>(stream: AsyncIterable<T>, fn: (value: T) => Promise<U> | U): AsyncIterableIterator<U>;
|
|
178
|
+
/**
|
|
179
|
+
* Filter message stream with async predicate.
|
|
180
|
+
*
|
|
181
|
+
* Only yields messages that satisfy the predicate.
|
|
182
|
+
*
|
|
183
|
+
* @param stream - Source message stream
|
|
184
|
+
* @param predicate - Async filter predicate
|
|
185
|
+
* @returns Async iterable of filtered messages
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* const stream = agent.subscribe({ scope: 'notifications' });
|
|
190
|
+
* const urgent = filterStream(stream, async (msg) =>
|
|
191
|
+
* msg.payload.priority === 'high'
|
|
192
|
+
* );
|
|
193
|
+
*
|
|
194
|
+
* for await (const message of urgent) {
|
|
195
|
+
* await handleUrgent(message);
|
|
196
|
+
* }
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export declare function filterStream<T>(stream: AsyncIterable<T>, predicate: (value: T) => Promise<boolean> | boolean): AsyncIterableIterator<T>;
|
|
200
|
+
/**
|
|
201
|
+
* Take first N messages from stream.
|
|
202
|
+
*
|
|
203
|
+
* Stream completes after yielding N messages.
|
|
204
|
+
*
|
|
205
|
+
* @param stream - Source message stream
|
|
206
|
+
* @param count - Maximum messages to take
|
|
207
|
+
* @returns Async iterable with at most count messages
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* const stream = agent.subscribe();
|
|
212
|
+
* const first5 = takeStream(stream, 5);
|
|
213
|
+
*
|
|
214
|
+
* for await (const message of first5) {
|
|
215
|
+
* console.log('Message:', message);
|
|
216
|
+
* }
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
export declare function takeStream<T>(stream: AsyncIterable<T>, count: number): AsyncIterableIterator<T>;
|
|
220
|
+
/**
|
|
221
|
+
* Merge multiple message streams into one.
|
|
222
|
+
*
|
|
223
|
+
* Yields messages from all streams as they arrive.
|
|
224
|
+
* Completes when all source streams complete.
|
|
225
|
+
*
|
|
226
|
+
* @param streams - Array of source streams
|
|
227
|
+
* @returns Async iterable of merged messages
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const stream1 = agent.subscribe({ from: 'did:key:alice' });
|
|
232
|
+
* const stream2 = agent.subscribe({ from: 'did:key:bob' });
|
|
233
|
+
* const merged = mergeStreams([stream1, stream2]);
|
|
234
|
+
*
|
|
235
|
+
* for await (const message of merged) {
|
|
236
|
+
* console.log('From:', message.sender);
|
|
237
|
+
* }
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
export declare function mergeStreams<T>(streams: AsyncIterable<T>[]): AsyncIterableIterator<T>;
|
|
241
|
+
declare module './agent.js' {
|
|
242
|
+
interface Agent {
|
|
243
|
+
/**
|
|
244
|
+
* Subscribe to incoming messages as an async iterable stream.
|
|
245
|
+
*
|
|
246
|
+
* Enables modern async iteration patterns with `for await...of` syntax.
|
|
247
|
+
* Handles backpressure, cancellation, and error propagation.
|
|
248
|
+
*
|
|
249
|
+
* @param options - Stream subscription options
|
|
250
|
+
* @returns Async iterable message stream
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* // Subscribe to all messages from Alice
|
|
255
|
+
* for await (const message of agent.subscribe({ from: 'did:key:alice' })) {
|
|
256
|
+
* console.log('Received:', message.payload);
|
|
257
|
+
* }
|
|
258
|
+
*
|
|
259
|
+
* // With abort signal
|
|
260
|
+
* const controller = new AbortController();
|
|
261
|
+
* setTimeout(() => controller.abort(), 5000); // 5 second timeout
|
|
262
|
+
*
|
|
263
|
+
* for await (const message of agent.subscribe({ signal: controller.signal })) {
|
|
264
|
+
* console.log('Message:', message);
|
|
265
|
+
* }
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
subscribe(options?: MessageStreamOptions): MessageStream;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Add subscribe method to Agent prototype.
|
|
273
|
+
* @internal
|
|
274
|
+
*/
|
|
275
|
+
export declare function installAsyncIterators(AgentClass: typeof Agent): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class MessageStream{agent;options;state;constructor(t,e={}){this.agent=t,this.options={from:e.from,scope:e.scope,bufferSize:e.bufferSize??100,signal:e.signal,allowCleartext:e.allowCleartext??!1},this.state={buffer:[],pending:[],done:!1},this.options.signal&&this.options.signal.addEventListener("abort",()=>{this.close(new Error("Stream aborted"))});const s=t=>{this.handleEnvelope(t)},n=this.agent.getTransports()[0];n&&(n.onReceive(s),this.state.cleanup=()=>{})}async handleEnvelope(t){if(this.state.done)return;const e=await this.agent.receive(t,{allowCleartext:this.options.allowCleartext});if(!e.ok)return;const s=e.value;if(!(this.options.from&&s.sender!==this.options.from||this.options.scope&&s.scope!==this.options.scope))if(this.state.pending.length>0){this.state.pending.shift()({value:s,done:!1})}else this.state.buffer.length<this.options.bufferSize&&this.state.buffer.push(s)}close(t){if(!this.state.done){this.state.done=!0,this.state.error=t;for(const t of this.state.pending)t({value:void 0,done:!0});this.state.pending=[],this.state.cleanup&&this.state.cleanup()}}async next(){if(this.state.done)return{value:void 0,done:!0};if(this.options.signal?.aborted)return this.close(new Error("Stream aborted")),{value:void 0,done:!0};if(this.state.buffer.length>0){return{value:this.state.buffer.shift(),done:!1}}return new Promise(t=>{this.state.pending.push(t)})}async return(t){return this.close(),{value:void 0,done:!0}}async throw(t){return this.close(t instanceof Error?t:new Error(String(t))),{value:void 0,done:!0}}[Symbol.asyncIterator](){return this}get bufferedCount(){return this.state.buffer.length}get closed(){return this.state.done}get error(){return this.state.error}}export async function collectMessages(t,e={}){const s=[],n=new MessageStream(t,e);let o;e.timeout&&(o=setTimeout(()=>{n.return()},e.timeout));try{for await(const t of n)if(s.push(t),e.limit&&s.length>=e.limit)break}finally{o&&clearTimeout(o),n.closed||await n.return()}return s}export async function*mapStream(t,e){for await(const s of t)yield await e(s)}export async function*filterStream(t,e){for await(const s of t)await e(s)&&(yield s)}export async function*takeStream(t,e){let s=0;for await(const n of t){if(s>=e)break;yield n,s++}}export async function*mergeStreams(t){const e=t.map(t=>t[Symbol.asyncIterator]()),s=new Map;for(let t=0;t<e.length;t++)s.set(t,e[t].next());for(;s.size>0;){const t=Array.from(s.entries()),n=await Promise.race(t.map(async([t,e])=>({idx:t,result:await e})));s.delete(n.idx),n.result.done||(yield n.result.value,s.set(n.idx,e[n.idx].next()))}}export function installAsyncIterators(t){"subscribe"in t.prototype||(t.prototype.subscribe=function(t){return new MessageStream(this,t)})}
|