@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,62 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/** Default TTL for nonce entries: 10 minutes. */
|
|
3
|
-
const DEFAULT_TTL_MS = 10 * 60 * 1000;
|
|
4
|
-
/** Default cleanup interval: 60 seconds. */
|
|
5
|
-
const DEFAULT_CLEANUP_INTERVAL_MS = 60 * 1000;
|
|
6
|
-
/**
|
|
7
|
-
* In-memory nonce store for development and single-process deployments.
|
|
8
|
-
*
|
|
9
|
-
* Stores nonces in a Map keyed by "senderDid:nonce" with expiry timestamps.
|
|
10
|
-
* Periodic cleanup removes expired entries to prevent unbounded growth.
|
|
11
|
-
*/
|
|
12
|
-
export class MemoryNonceStore {
|
|
13
|
-
seen = new Map();
|
|
14
|
-
ttlMs;
|
|
15
|
-
cleanupIntervalMs;
|
|
16
|
-
timer = null;
|
|
17
|
-
constructor(opts) {
|
|
18
|
-
this.ttlMs = opts?.ttlMs ?? DEFAULT_TTL_MS;
|
|
19
|
-
this.cleanupIntervalMs = opts?.cleanupIntervalMs ?? DEFAULT_CLEANUP_INTERVAL_MS;
|
|
20
|
-
// Timer starts lazily on first check() to avoid blocking process exit in tests
|
|
21
|
-
}
|
|
22
|
-
/** Start cleanup timer on first use. */
|
|
23
|
-
ensureTimer() {
|
|
24
|
-
if (this.timer !== null)
|
|
25
|
-
return;
|
|
26
|
-
const t = setInterval(() => this.cleanup(), this.cleanupIntervalMs);
|
|
27
|
-
// Unref so the timer doesn't block Node.js process exit
|
|
28
|
-
if (typeof t === 'object' && 'unref' in t) {
|
|
29
|
-
t.unref(); // SAFETY: runtime check guards access
|
|
30
|
-
}
|
|
31
|
-
this.timer = t;
|
|
32
|
-
}
|
|
33
|
-
async check(nonce, senderDid) {
|
|
34
|
-
this.ensureTimer();
|
|
35
|
-
const key = `${senderDid}:${nonce}`;
|
|
36
|
-
const existing = this.seen.get(key);
|
|
37
|
-
const now = Date.now();
|
|
38
|
-
if (existing !== undefined && existing > now) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
this.seen.set(key, now + this.ttlMs);
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
cleanup() {
|
|
45
|
-
const now = Date.now();
|
|
46
|
-
for (const [key, expiry] of this.seen) {
|
|
47
|
-
if (expiry <= now)
|
|
48
|
-
this.seen.delete(key);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
dispose() {
|
|
52
|
-
if (this.timer !== null) {
|
|
53
|
-
clearInterval(this.timer);
|
|
54
|
-
this.timer = null;
|
|
55
|
-
}
|
|
56
|
-
this.seen.clear();
|
|
57
|
-
}
|
|
58
|
-
/** Number of active (non-expired) entries. Useful for testing. */
|
|
59
|
-
get size() {
|
|
60
|
-
return this.seen.size;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
1
|
+
const DEFAULT_TTL_MS=6e5,DEFAULT_CLEANUP_INTERVAL_MS=6e4;export class MemoryNonceStore{seen=new Map;ttlMs;cleanupIntervalMs;timer=null;constructor(e){this.ttlMs=e?.ttlMs??6e5,this.cleanupIntervalMs=e?.cleanupIntervalMs??6e4}ensureTimer(){if(null!==this.timer)return;const e=setInterval(()=>this.cleanup(),this.cleanupIntervalMs);"object"==typeof e&&"unref"in e&&e.unref(),this.timer=e}buildKey(e,t,s){if(null==s)return`${t}:${e}`;return`${t}:${e}:${"string"==typeof s.shareGroupId?s.shareGroupId:""}:${"number"==typeof s.shareIndex?String(s.shareIndex):""}`}async check(e,t,s){this.ensureTimer();const n=this.buildKey(e,t,s),r=this.seen.get(n),i=Date.now();return!(void 0!==r&&r>i)&&(this.seen.set(n,i+this.ttlMs),!0)}cleanup(){const e=Date.now(),t=Array.from(this.seen.keys());for(const s of t){const t=this.seen.get(s);void 0!==t&&t<=e&&this.seen.delete(s)}}dispose(){null!==this.timer&&(clearInterval(this.timer),this.timer=null),this.seen.clear()}get size(){return this.seen.size}}
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
"type": "module",
|
|
3
3
|
"imports": {
|
|
4
4
|
"@private.me/shared": "./_deps/shared/index.js",
|
|
5
|
-
"@private.me/crypto": "./_deps/crypto/index.js",
|
|
6
5
|
"@private.me/xchange": "./_deps/xchange/index.js",
|
|
7
6
|
"@private.me/ux-helpers": "./_deps/ux-helpers/index.js",
|
|
8
7
|
"@private.me/xregistry": "./_deps/xregistry/index.js",
|
|
@@ -1,219 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @module pairing-manager
|
|
3
|
-
* Security validation for peer-to-peer device pairing via mDNS.
|
|
4
|
-
*
|
|
5
|
-
* Security properties:
|
|
6
|
-
* - 16-byte cryptographically secure nonce (prevents replay attacks)
|
|
7
|
-
* - 60-second timeout (limits attack window)
|
|
8
|
-
* - User confirmation required on BOTH devices
|
|
9
|
-
* - Nonce echo in response (prevents MITM substitution)
|
|
10
|
-
* - Trust registry integration (stores paired DIDs with scopes)
|
|
11
|
-
* - Gold Standard compliant (GS-CONNECT requirement #26, GS-SEC-RNG compliant)
|
|
12
|
-
*/
|
|
13
|
-
import { ok, err } from"./_deps/shared/index.js";
|
|
14
|
-
/**
|
|
15
|
-
* Pairing manager for secure peer-to-peer device pairing.
|
|
16
|
-
*
|
|
17
|
-
* Implements challenge-response protocol with user confirmation.
|
|
18
|
-
* Integrates with trust registry for persistent pairing storage.
|
|
19
|
-
*/
|
|
20
|
-
export class PairingManager {
|
|
21
|
-
pendingNonces = new Map(); // nonce → timestamp
|
|
22
|
-
registry;
|
|
23
|
-
deviceDid;
|
|
24
|
-
/**
|
|
25
|
-
* Create pairing manager.
|
|
26
|
-
*
|
|
27
|
-
* @param deviceDid - DID of this device
|
|
28
|
-
* @param registry - Trust registry instance
|
|
29
|
-
*/
|
|
30
|
-
constructor(deviceDid, registry) {
|
|
31
|
-
this.deviceDid = deviceDid;
|
|
32
|
-
this.registry = registry;
|
|
33
|
-
// Clean up expired nonces every 10 seconds
|
|
34
|
-
setInterval(() => {
|
|
35
|
-
const now = Date.now();
|
|
36
|
-
for (const [nonce, timestamp] of this.pendingNonces.entries()) {
|
|
37
|
-
if (now - timestamp > 60_000) {
|
|
38
|
-
this.pendingNonces.delete(nonce);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}, 10_000);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Create pairing request.
|
|
45
|
-
*
|
|
46
|
-
* Generates cryptographically secure nonce and stores it for validation.
|
|
47
|
-
* Device A calls this before sending request to Device B.
|
|
48
|
-
*
|
|
49
|
-
* @returns Pairing request to send to remote device
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```typescript
|
|
53
|
-
* const manager = new PairingManager('did:key:z6MkA...', registry);
|
|
54
|
-
* const request = manager.createRequest();
|
|
55
|
-
* // Send request to Device B via HTTP POST
|
|
56
|
-
* const response = await fetch(`${deviceB.endpoint}/pair`, {
|
|
57
|
-
* method: 'POST',
|
|
58
|
-
* body: JSON.stringify(request),
|
|
59
|
-
* });
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
createRequest() {
|
|
63
|
-
// Generate cryptographically secure nonce (GS-SEC-RNG: NEVER use Math.random)
|
|
64
|
-
const nonceBytes = new Uint8Array(16);
|
|
65
|
-
if (typeof globalThis.crypto !== 'undefined' && globalThis.crypto.getRandomValues) {
|
|
66
|
-
globalThis.crypto.getRandomValues(nonceBytes);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
// Node.js environment
|
|
70
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
71
|
-
const crypto = require('node:crypto');
|
|
72
|
-
crypto.getRandomValues(nonceBytes);
|
|
73
|
-
}
|
|
74
|
-
const nonce = Array.from(nonceBytes)
|
|
75
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
76
|
-
.join('');
|
|
77
|
-
const timestamp = Date.now();
|
|
78
|
-
// Store nonce for validation (60-second TTL)
|
|
79
|
-
this.pendingNonces.set(nonce, timestamp);
|
|
80
|
-
return {
|
|
81
|
-
device_a_did: this.deviceDid,
|
|
82
|
-
nonce,
|
|
83
|
-
timestamp,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Validate pairing request.
|
|
88
|
-
*
|
|
89
|
-
* Checks timeout and nonce uniqueness.
|
|
90
|
-
* Device B calls this when receiving request from Device A.
|
|
91
|
-
*
|
|
92
|
-
* @param request - Pairing request from remote device
|
|
93
|
-
* @returns Ok if valid, Err with reason if invalid
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* ```typescript
|
|
97
|
-
* // Device B receives request
|
|
98
|
-
* const validation = manager.validateRequest(request);
|
|
99
|
-
* if (!validation.ok) {
|
|
100
|
-
* return res.status(400).json({ error: validation.error });
|
|
101
|
-
* }
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
validateRequest(request) {
|
|
105
|
-
// Check timeout (60-second window)
|
|
106
|
-
const age = Date.now() - request.timestamp;
|
|
107
|
-
if (age > 60_000) {
|
|
108
|
-
return err(`Request expired (${Math.floor(age / 1000)}s old, max 60s)`);
|
|
109
|
-
}
|
|
110
|
-
if (age < 0) {
|
|
111
|
-
return err('Request timestamp in the future (clock skew detected)');
|
|
112
|
-
}
|
|
113
|
-
// Check required fields
|
|
114
|
-
if (!request.device_a_did || !request.nonce || !request.timestamp) {
|
|
115
|
-
return err('Missing required fields (device_a_did, nonce, timestamp)');
|
|
116
|
-
}
|
|
117
|
-
return ok(undefined);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Accept pairing request.
|
|
121
|
-
*
|
|
122
|
-
* Shows UI prompt for user confirmation, then stores in trust registry.
|
|
123
|
-
* Device B calls this after validating request.
|
|
124
|
-
*
|
|
125
|
-
* @param request - Validated pairing request
|
|
126
|
-
* @param promptCallback - Async function to show UI prompt (returns true if accepted)
|
|
127
|
-
* @returns Pairing response to send back to Device A
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* const response = await manager.acceptPairing(request, async () => {
|
|
132
|
-
* // Show modal: "Device {name} wants to pair. Accept?"
|
|
133
|
-
* return await showConfirmationDialog({
|
|
134
|
-
* title: 'Pair with Device?',
|
|
135
|
-
* message: `DID: ${request.device_a_did.slice(0, 20)}...`,
|
|
136
|
-
* });
|
|
137
|
-
* });
|
|
138
|
-
*
|
|
139
|
-
* if (response.ok && response.value.accepted) {
|
|
140
|
-
* // Send response back to Device A
|
|
141
|
-
* }
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
async acceptPairing(request, promptCallback) {
|
|
145
|
-
// Validate request first
|
|
146
|
-
const validation = this.validateRequest(request);
|
|
147
|
-
if (!validation.ok) {
|
|
148
|
-
return err(validation.error);
|
|
149
|
-
}
|
|
150
|
-
// Prompt user for confirmation
|
|
151
|
-
const accepted = await promptCallback();
|
|
152
|
-
if (accepted) {
|
|
153
|
-
// Store in trust registry (Device A → Device B)
|
|
154
|
-
const registerResult = await this.registry.register(request.device_a_did, new Uint8Array(32), 'Paired Device', ['pairing']);
|
|
155
|
-
if (!registerResult.ok) {
|
|
156
|
-
const errorMsg = typeof registerResult.error === 'string'
|
|
157
|
-
? registerResult.error
|
|
158
|
-
: 'Unknown error';
|
|
159
|
-
return err(`Failed to register pairing: ${errorMsg}`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
// Return response (echo nonce to prevent MITM)
|
|
163
|
-
return ok({
|
|
164
|
-
device_b_did: this.deviceDid,
|
|
165
|
-
accepted,
|
|
166
|
-
nonce: request.nonce,
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Finalize pairing.
|
|
171
|
-
*
|
|
172
|
-
* Verifies response nonce and stores remote DID in trust registry.
|
|
173
|
-
* Device A calls this after receiving response from Device B.
|
|
174
|
-
*
|
|
175
|
-
* @param response - Pairing response from Device B
|
|
176
|
-
* @param expectedNonce - Nonce from original request (for verification)
|
|
177
|
-
* @returns Ok if successful, Err if nonce mismatch or storage failed
|
|
178
|
-
*
|
|
179
|
-
* @example
|
|
180
|
-
* ```typescript
|
|
181
|
-
* const request = manager.createRequest();
|
|
182
|
-
* const response = await sendPairingRequest(deviceB.endpoint, request);
|
|
183
|
-
*
|
|
184
|
-
* const result = await manager.finalizePairing(response, request.nonce);
|
|
185
|
-
* if (result.ok) {
|
|
186
|
-
* console.log('Pairing successful!');
|
|
187
|
-
* } else {
|
|
188
|
-
* console.error('Pairing failed:', result.error);
|
|
189
|
-
* }
|
|
190
|
-
* ```
|
|
191
|
-
*/
|
|
192
|
-
async finalizePairing(response, expectedNonce) {
|
|
193
|
-
// Verify nonce echo (prevents MITM substitution)
|
|
194
|
-
if (response.nonce !== expectedNonce) {
|
|
195
|
-
return err('Nonce mismatch (possible MITM attack)');
|
|
196
|
-
}
|
|
197
|
-
// Check if pairing was accepted
|
|
198
|
-
if (!response.accepted) {
|
|
199
|
-
return err('Pairing rejected by remote device');
|
|
200
|
-
}
|
|
201
|
-
// Remove nonce from pending set
|
|
202
|
-
this.pendingNonces.delete(expectedNonce);
|
|
203
|
-
// Store in trust registry (Device B → Device A)
|
|
204
|
-
const registerResult = await this.registry.register(response.device_b_did, new Uint8Array(32), 'Paired Device', ['pairing']);
|
|
205
|
-
if (!registerResult.ok) {
|
|
206
|
-
const errorMsg = typeof registerResult.error === 'string'
|
|
207
|
-
? registerResult.error
|
|
208
|
-
: 'Unknown error';
|
|
209
|
-
return err(`Failed to register pairing: ${errorMsg}`);
|
|
210
|
-
}
|
|
211
|
-
return ok(undefined);
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Get pending nonce count (for debugging/monitoring).
|
|
215
|
-
*/
|
|
216
|
-
getPendingNonceCount() {
|
|
217
|
-
return this.pendingNonces.size;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
1
|
+
import{ok,err}from"./_deps/shared/index.js";export class PairingManager{pendingNonces=new Map;registry;deviceDid;constructor(e,r){this.deviceDid=e,this.registry=r,setInterval(()=>{const e=Date.now();for(const[r,i]of this.pendingNonces.entries())e-i>6e4&&this.pendingNonces.delete(r)},1e4)}createRequest(){const e=new Uint8Array(16);if(void 0!==globalThis.crypto&&globalThis.crypto.getRandomValues)globalThis.crypto.getRandomValues(e);else{require("node:crypto").getRandomValues(e)}const r=Array.from(e).map(e=>e.toString(16).padStart(2,"0")).join(""),i=Date.now();return this.pendingNonces.set(r,i),{device_a_did:this.deviceDid,nonce:r,timestamp:i}}validateRequest(e){const r=Date.now()-e.timestamp;return r>6e4?err(`Request expired (${Math.floor(r/1e3)}s old, max 60s)`):r<0?err("Request timestamp in the future (clock skew detected)"):e.device_a_did&&e.nonce&&e.timestamp?ok(void 0):err("Missing required fields (device_a_did, nonce, timestamp)")}async acceptPairing(e,r){const i=this.validateRequest(e);if(!i.ok)return err(i.error);const t=await r();if(t){const r=await this.registry.register(e.device_a_did,new Uint8Array(32),"Paired Device",["pairing"]);if(!r.ok){const e="string"==typeof r.error?r.error:"Unknown error";return err(`Failed to register pairing: ${e}`)}}return ok({device_b_did:this.deviceDid,accepted:t,nonce:e.nonce})}async finalizePairing(e,r){if(e.nonce!==r)return err("Nonce mismatch (possible MITM attack)");if(!e.accepted)return err("Pairing rejected by remote device");this.pendingNonces.delete(r);const i=await this.registry.register(e.device_b_did,new Uint8Array(32),"Paired Device",["pairing"]);if(!i.ok){const e="string"==typeof i.error?i.error:"Unknown error";return err(`Failed to register pairing: ${e}`)}return ok(void 0)}getPendingNonceCount(){return this.pendingNonces.size}}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module plugin-system
|
|
3
|
+
* Extensible plugin and middleware architecture for xBind
|
|
4
|
+
*
|
|
5
|
+
* Provides hooks for message processing, encryption, decryption, and custom logic.
|
|
6
|
+
* Supports both synchronous and asynchronous plugin execution.
|
|
7
|
+
*/
|
|
8
|
+
import type { Result } from '@private.me/shared';
|
|
9
|
+
import type { AnyTransportEnvelope } from './envelope.js';
|
|
10
|
+
import type { AgentIdentity } from './identity.js';
|
|
11
|
+
/**
|
|
12
|
+
* Plugin execution context provided to all hooks.
|
|
13
|
+
* Contains agent identity, message metadata, and shared state.
|
|
14
|
+
*/
|
|
15
|
+
export interface PluginContext {
|
|
16
|
+
/** Agent identity making the call */
|
|
17
|
+
readonly agent: AgentIdentity;
|
|
18
|
+
/** Recipient DID (if applicable) */
|
|
19
|
+
readonly recipient?: string;
|
|
20
|
+
/** Message scope */
|
|
21
|
+
readonly scope?: string;
|
|
22
|
+
/** Message timestamp */
|
|
23
|
+
readonly timestamp: number;
|
|
24
|
+
/** Shared state across plugins in the chain */
|
|
25
|
+
readonly state: Map<string, unknown>;
|
|
26
|
+
/** Plugin metadata */
|
|
27
|
+
readonly metadata: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Hook execution result.
|
|
31
|
+
* Plugins can modify the payload, add metadata, or abort execution.
|
|
32
|
+
*/
|
|
33
|
+
export interface HookResult<T> {
|
|
34
|
+
/** Modified payload (or original if unchanged) */
|
|
35
|
+
readonly payload: T;
|
|
36
|
+
/** Metadata to attach to context */
|
|
37
|
+
readonly metadata?: Record<string, unknown>;
|
|
38
|
+
/** Abort flag - if true, stops further processing */
|
|
39
|
+
readonly abort?: boolean;
|
|
40
|
+
/** Abort reason (if abort === true) */
|
|
41
|
+
readonly reason?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Plugin interface.
|
|
45
|
+
* All plugins must implement this interface.
|
|
46
|
+
*/
|
|
47
|
+
export interface Plugin {
|
|
48
|
+
/** Plugin name (must be unique) */
|
|
49
|
+
readonly name: string;
|
|
50
|
+
/** Plugin version */
|
|
51
|
+
readonly version: string;
|
|
52
|
+
/** Plugin description */
|
|
53
|
+
readonly description?: string;
|
|
54
|
+
/** Plugin priority (lower = earlier execution, default: 100) */
|
|
55
|
+
readonly priority?: number;
|
|
56
|
+
/** Called before sending a message */
|
|
57
|
+
onSend?(payload: unknown, context: PluginContext): Promise<Result<HookResult<unknown>, string>> | Result<HookResult<unknown>, string>;
|
|
58
|
+
/** Called after receiving a message */
|
|
59
|
+
onReceive?(envelope: AnyTransportEnvelope, context: PluginContext): Promise<Result<HookResult<AnyTransportEnvelope>, string>> | Result<HookResult<AnyTransportEnvelope>, string>;
|
|
60
|
+
/** Called before encryption */
|
|
61
|
+
onEncrypt?(plaintext: Uint8Array, context: PluginContext): Promise<Result<HookResult<Uint8Array>, string>> | Result<HookResult<Uint8Array>, string>;
|
|
62
|
+
/** Called after decryption */
|
|
63
|
+
onDecrypt?(plaintext: Uint8Array, context: PluginContext): Promise<Result<HookResult<Uint8Array>, string>> | Result<HookResult<Uint8Array>, string>;
|
|
64
|
+
/** Called on plugin initialization */
|
|
65
|
+
onInit?(): Promise<Result<void, string>> | Result<void, string>;
|
|
66
|
+
/** Called on plugin cleanup */
|
|
67
|
+
onDestroy?(): Promise<Result<void, string>> | Result<void, string>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Middleware chain executor.
|
|
71
|
+
* Runs plugins in priority order and handles errors.
|
|
72
|
+
*/
|
|
73
|
+
export declare class MiddlewareChain {
|
|
74
|
+
private plugins;
|
|
75
|
+
private initialized;
|
|
76
|
+
constructor(plugins?: Plugin[]);
|
|
77
|
+
/**
|
|
78
|
+
* Register a plugin.
|
|
79
|
+
* Plugins are automatically sorted by priority after registration.
|
|
80
|
+
*/
|
|
81
|
+
register(plugin: Plugin): Result<void, string>;
|
|
82
|
+
/**
|
|
83
|
+
* Unregister a plugin by name.
|
|
84
|
+
*/
|
|
85
|
+
unregister(name: string): Result<void, string>;
|
|
86
|
+
/**
|
|
87
|
+
* Get all registered plugins.
|
|
88
|
+
*/
|
|
89
|
+
getPlugins(): readonly Plugin[];
|
|
90
|
+
/**
|
|
91
|
+
* Initialize all plugins.
|
|
92
|
+
* Must be called before executing hooks.
|
|
93
|
+
*/
|
|
94
|
+
initialize(): Promise<Result<void, string>>;
|
|
95
|
+
/**
|
|
96
|
+
* Destroy all plugins and cleanup resources.
|
|
97
|
+
*/
|
|
98
|
+
destroy(): Promise<Result<void, string>>;
|
|
99
|
+
/**
|
|
100
|
+
* Execute onSend hooks.
|
|
101
|
+
*/
|
|
102
|
+
executeSend(payload: unknown, context: PluginContext): Promise<Result<unknown, string>>;
|
|
103
|
+
/**
|
|
104
|
+
* Execute onReceive hooks.
|
|
105
|
+
*/
|
|
106
|
+
executeReceive(envelope: AnyTransportEnvelope, context: PluginContext): Promise<Result<AnyTransportEnvelope, string>>;
|
|
107
|
+
/**
|
|
108
|
+
* Execute onEncrypt hooks.
|
|
109
|
+
*/
|
|
110
|
+
executeEncrypt(plaintext: Uint8Array, context: PluginContext): Promise<Result<Uint8Array, string>>;
|
|
111
|
+
/**
|
|
112
|
+
* Execute onDecrypt hooks.
|
|
113
|
+
*/
|
|
114
|
+
executeDecrypt(plaintext: Uint8Array, context: PluginContext): Promise<Result<Uint8Array, string>>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Create a plugin context.
|
|
118
|
+
*/
|
|
119
|
+
export declare function createPluginContext(agent: AgentIdentity, options?: {
|
|
120
|
+
recipient?: string;
|
|
121
|
+
scope?: string;
|
|
122
|
+
timestamp?: number;
|
|
123
|
+
state?: Map<string, unknown>;
|
|
124
|
+
metadata?: Record<string, unknown>;
|
|
125
|
+
}): PluginContext;
|
|
126
|
+
/**
|
|
127
|
+
* Plugin builder for creating plugins with a fluent API.
|
|
128
|
+
*/
|
|
129
|
+
export declare class PluginBuilder {
|
|
130
|
+
private plugin;
|
|
131
|
+
constructor(name: string, version: string);
|
|
132
|
+
description(desc: string): this;
|
|
133
|
+
priority(p: number): this;
|
|
134
|
+
onSend(hook: (payload: unknown, context: PluginContext) => Promise<Result<HookResult<unknown>, string>> | Result<HookResult<unknown>, string>): this;
|
|
135
|
+
onReceive(hook: (envelope: AnyTransportEnvelope, context: PluginContext) => Promise<Result<HookResult<AnyTransportEnvelope>, string>> | Result<HookResult<AnyTransportEnvelope>, string>): this;
|
|
136
|
+
onEncrypt(hook: (plaintext: Uint8Array, context: PluginContext) => Promise<Result<HookResult<Uint8Array>, string>> | Result<HookResult<Uint8Array>, string>): this;
|
|
137
|
+
onDecrypt(hook: (plaintext: Uint8Array, context: PluginContext) => Promise<Result<HookResult<Uint8Array>, string>> | Result<HookResult<Uint8Array>, string>): this;
|
|
138
|
+
onInit(hook: () => Promise<Result<void, string>> | Result<void, string>): this;
|
|
139
|
+
onDestroy(hook: () => Promise<Result<void, string>> | Result<void, string>): this;
|
|
140
|
+
build(): Plugin;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Create a plugin builder.
|
|
144
|
+
*/
|
|
145
|
+
export declare function createPlugin(name: string, version: string): PluginBuilder;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ok,err}from"./_deps/shared/index.js";export class MiddlewareChain{plugins=[];initialized=!1;constructor(t=[]){this.plugins=[...t].sort((t,e)=>(t.priority??100)-(e.priority??100))}register(t){return this.plugins.some(e=>e.name===t.name)?err(`PLUGIN_ALREADY_REGISTERED:${t.name}`):(this.plugins.push(t),this.plugins.sort((t,e)=>(t.priority??100)-(e.priority??100)),ok(void 0))}unregister(t){const e=this.plugins.findIndex(e=>e.name===t);return-1===e?err(`PLUGIN_NOT_FOUND:${t}`):(this.plugins.splice(e,1),ok(void 0))}getPlugins(){return[...this.plugins]}async initialize(){if(this.initialized)return ok(void 0);for(const t of this.plugins)if(t.onInit){const e=await t.onInit();if(!1===e.ok)return err(`PLUGIN_INIT_FAILED:${t.name}:${e.error}`)}return this.initialized=!0,ok(void 0)}async destroy(){const t=[];for(const e of this.plugins)if(e.onDestroy){const r=await e.onDestroy();!1===r.ok&&t.push(`${e.name}:${r.error}`)}return this.initialized=!1,t.length>0?err(`PLUGIN_DESTROY_FAILED:${t.join(",")}`):ok(void 0)}async executeSend(t,e){if(!this.initialized)return err("MIDDLEWARE_NOT_INITIALIZED");let r=t;for(const t of this.plugins)if(t.onSend){const n=await t.onSend(r,e);if(!1===n.ok)return err(`PLUGIN_HOOK_FAILED:${t.name}:${n.error}`);const i=n.value;if(i.abort)return err(`PLUGIN_ABORTED:${t.name}:${i.reason??"no reason"}`);r=i.payload,i.metadata&&Object.assign(e.metadata,i.metadata)}return ok(r)}async executeReceive(t,e){if(!this.initialized)return err("MIDDLEWARE_NOT_INITIALIZED");let r=t;for(const t of this.plugins)if(t.onReceive){const n=await t.onReceive(r,e);if(!1===n.ok)return err(`PLUGIN_HOOK_FAILED:${t.name}:${n.error}`);const i=n.value;if(i.abort)return err(`PLUGIN_ABORTED:${t.name}:${i.reason??"no reason"}`);r=i.payload,i.metadata&&Object.assign(e.metadata,i.metadata)}return ok(r)}async executeEncrypt(t,e){if(!this.initialized)return err("MIDDLEWARE_NOT_INITIALIZED");let r=t;for(const t of this.plugins)if(t.onEncrypt){const n=await t.onEncrypt(r,e);if(!1===n.ok)return err(`PLUGIN_HOOK_FAILED:${t.name}:${n.error}`);const i=n.value;if(i.abort)return err(`PLUGIN_ABORTED:${t.name}:${i.reason??"no reason"}`);r=i.payload,i.metadata&&Object.assign(e.metadata,i.metadata)}return ok(r)}async executeDecrypt(t,e){if(!this.initialized)return err("MIDDLEWARE_NOT_INITIALIZED");let r=t;for(const t of this.plugins)if(t.onDecrypt){const n=await t.onDecrypt(r,e);if(!1===n.ok)return err(`PLUGIN_HOOK_FAILED:${t.name}:${n.error}`);const i=n.value;if(i.abort)return err(`PLUGIN_ABORTED:${t.name}:${i.reason??"no reason"}`);r=i.payload,i.metadata&&Object.assign(e.metadata,i.metadata)}return ok(r)}}export function createPluginContext(t,e={}){return{agent:t,recipient:e.recipient,scope:e.scope,timestamp:e.timestamp??Date.now(),state:e.state??new Map,metadata:e.metadata??{}}}export class PluginBuilder{plugin={};constructor(t,e){this.plugin.name=t,this.plugin.version=e}description(t){return this.plugin.description=t,this}priority(t){return this.plugin.priority=t,this}onSend(t){return this.plugin.onSend=t,this}onReceive(t){return this.plugin.onReceive=t,this}onEncrypt(t){return this.plugin.onEncrypt=t,this}onDecrypt(t){return this.plugin.onDecrypt=t,this}onInit(t){return this.plugin.onInit=t,this}onDestroy(t){return this.plugin.onDestroy=t,this}build(){if(!this.plugin.name||!this.plugin.version)throw new Error("Plugin name and version are required");return this.plugin}}export function createPlugin(t,e){return new PluginBuilder(t,e)}
|