@private.me/xbind 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2366 -204
- package/README.md.backup +2121 -0
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
- package/dist-standalone/_deps/shared/cjs/errors.js +1 -729
- package/dist-standalone/_deps/shared/cjs/index.js +1 -463
- package/dist-standalone/_deps/shared/cjs/types.js +1 -315
- package/dist-standalone/_deps/shared/errors.js +1 -244
- package/dist-standalone/_deps/shared/index.js +1 -72
- package/dist-standalone/_deps/shared/types.js +1 -86
- 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.d.ts +2 -2
- package/dist-standalone/agent-call.js +1 -659
- package/dist-standalone/agent-sdk.js +1 -328
- package/dist-standalone/agent.d.ts +2 -0
- package/dist-standalone/agent.js +1 -1800
- package/dist-standalone/approval.js +1 -193
- package/dist-standalone/async-iterators.d.ts +3 -3
- package/dist-standalone/async-iterators.js +1 -382
- 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.js +1 -326
- package/dist-standalone/batch-operations.js +1 -388
- package/dist-standalone/cancellation.js +1 -477
- package/dist-standalone/checkpoint.js +1 -186
- package/dist-standalone/circuit-breaker.js +1 -468
- package/dist-standalone/cjs/agent-call.js +1 -701
- package/dist-standalone/cjs/agent-sdk.js +1 -332
- package/dist-standalone/cjs/agent.js +1 -1837
- package/dist-standalone/cjs/approval.js +1 -199
- package/dist-standalone/cjs/async-iterators.js +1 -392
- 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 -330
- package/dist-standalone/cjs/batch-operations.js +1 -397
- package/dist-standalone/cjs/cancellation.js +1 -490
- package/dist-standalone/cjs/checkpoint.js +1 -193
- package/dist-standalone/cjs/circuit-breaker.js +1 -476
- package/dist-standalone/cjs/cli/init.js +1 -492
- package/dist-standalone/cjs/config-validation.js +1 -522
- package/dist-standalone/cjs/connect.js +1 -312
- package/dist-standalone/cjs/connection-pool.js +1 -506
- package/dist-standalone/cjs/correlation-id.js +1 -339
- package/dist-standalone/cjs/crypto-utils.js +1 -176
- package/dist-standalone/cjs/debug-mode.js +1 -534
- 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 -538
- package/dist-standalone/cjs/errors.js +1 -913
- package/dist-standalone/cjs/event-emitter.js +1 -461
- 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 -403
- package/dist-standalone/cjs/guardrails.js +1 -223
- package/dist-standalone/cjs/health-check.js +1 -336
- 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 -645
- package/dist-standalone/cjs/index.js +1 -406
- package/dist-standalone/cjs/invitation.js +1 -421
- package/dist-standalone/cjs/invite.js +1 -328
- package/dist-standalone/cjs/key-agreement.js +1 -335
- package/dist-standalone/cjs/lazy-init.js +1 -300
- package/dist-standalone/cjs/logger.js +1 -291
- package/dist-standalone/cjs/loopback-transport.js +1 -0
- package/dist-standalone/cjs/mdns-discovery.js +1 -202
- package/dist-standalone/cjs/nonce-store.js +1 -80
- package/dist-standalone/cjs/pairing-manager.js +1 -223
- package/dist-standalone/cjs/plugin-system.js +1 -264
- package/dist-standalone/cjs/plugins/logging.js +1 -168
- package/dist-standalone/cjs/plugins/metrics.js +1 -181
- package/dist-standalone/cjs/plugins/validation.js +1 -302
- package/dist-standalone/cjs/policy.js +1 -320
- package/dist-standalone/cjs/progress-callbacks.js +1 -583
- 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 -544
- package/dist-standalone/cjs/retry-transport.js +1 -102
- package/dist-standalone/cjs/runtime/browser.js +1 -533
- package/dist-standalone/cjs/runtime/edge.js +1 -526
- package/dist-standalone/cjs/runtime/react-native.js +1 -394
- package/dist-standalone/cjs/security-policy.js +1 -245
- package/dist-standalone/cjs/serialization.js +1 -1040
- package/dist-standalone/cjs/split-channel.js +1 -225
- package/dist-standalone/cjs/subscription-proof.js +1 -230
- package/dist-standalone/cjs/succession.js +1 -148
- package/dist-standalone/cjs/timeouts.js +1 -412
- package/dist-standalone/cjs/trace-context.js +1 -424
- package/dist-standalone/cjs/trace-spans.js +1 -495
- package/dist-standalone/cjs/transport.js +1 -63
- package/dist-standalone/cjs/trust-registry.js +1 -991
- package/dist-standalone/cjs/types/error-response.js +1 -56
- package/dist-standalone/cjs/vault-auth.js +1 -178
- package/dist-standalone/cjs/vault-store-loader.js +1 -194
- package/dist-standalone/cjs/verify.js +1 -25
- package/dist-standalone/cjs/version-info.js +1 -543
- package/dist-standalone/cjs/xfetch.js +1 -340
- package/dist-standalone/cli/init.js +1 -455
- 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.js +1 -513
- package/dist-standalone/connect.js +1 -274
- package/dist-standalone/connection-pool.js +1 -500
- package/dist-standalone/correlation-id.js +1 -326
- package/dist-standalone/crypto-utils.d.ts +2 -7
- package/dist-standalone/crypto-utils.js +1 -157
- package/dist-standalone/debug-mode.js +1 -510
- 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.d.ts +2 -2
- package/dist-standalone/email-transport.js +1 -232
- package/dist-standalone/envelope.js +1 -525
- package/dist-standalone/errors.d.ts +13 -3
- package/dist-standalone/errors.js +1 -896
- package/dist-standalone/event-emitter.js +1 -456
- package/dist-standalone/gateway-state.d.ts +1 -1
- package/dist-standalone/gateway-state.js +1 -51
- package/dist-standalone/gateway-transport.js +1 -116
- package/dist-standalone/graceful-degradation.js +1 -396
- package/dist-standalone/guardrails.js +1 -216
- package/dist-standalone/health-check.d.ts +5 -1
- package/dist-standalone/health-check.js +1 -332
- package/dist-standalone/http-compat.d.ts +1 -1
- package/dist-standalone/http-compat.js +1 -267
- package/dist-standalone/http-status-map.js +1 -561
- package/dist-standalone/identity.js +1 -619
- package/dist-standalone/index.d.ts +15 -4
- package/dist-standalone/index.js +1 -78
- package/dist-standalone/invitation.js +1 -415
- package/dist-standalone/invite.js +1 -324
- package/dist-standalone/key-agreement.js +1 -325
- package/dist-standalone/lazy-init.d.ts +11 -6
- package/dist-standalone/lazy-init.js +1 -295
- package/dist-standalone/logger.js +1 -285
- package/dist-standalone/loopback-transport.d.ts +87 -0
- package/dist-standalone/loopback-transport.js +1 -0
- package/dist-standalone/mdns-discovery.js +1 -195
- package/dist-standalone/nonce-store.js +1 -76
- package/dist-standalone/pairing-manager.js +1 -219
- package/dist-standalone/plugin-system.js +1 -257
- package/dist-standalone/plugins/logging.js +1 -163
- package/dist-standalone/plugins/metrics.d.ts +4 -4
- package/dist-standalone/plugins/metrics.js +1 -176
- package/dist-standalone/plugins/validation.js +1 -297
- package/dist-standalone/policy.js +1 -315
- package/dist-standalone/progress-callbacks.js +1 -576
- package/dist-standalone/redis-nonce-store.js +1 -72
- package/dist-standalone/registry-middleware.js +1 -47
- package/dist-standalone/retry-strategies.js +1 -534
- package/dist-standalone/retry-transport.js +1 -98
- package/dist-standalone/runtime/browser.js +1 -516
- package/dist-standalone/runtime/edge.js +1 -511
- package/dist-standalone/runtime/react-native.d.ts +1 -1
- package/dist-standalone/runtime/react-native.js +1 -383
- package/dist-standalone/security-policy.js +1 -239
- package/dist-standalone/serialization.js +1 -1031
- package/dist-standalone/split-channel.js +1 -219
- package/dist-standalone/subscription-proof.js +1 -224
- package/dist-standalone/succession.js +1 -142
- package/dist-standalone/timeouts.js +1 -398
- package/dist-standalone/trace-context.js +1 -414
- package/dist-standalone/trace-spans.js +1 -488
- package/dist-standalone/transport.js +1 -59
- package/dist-standalone/trust-registry.d.ts +3 -3
- package/dist-standalone/trust-registry.js +1 -950
- package/dist-standalone/types/error-response.js +1 -52
- package/dist-standalone/vault-auth.js +1 -174
- package/dist-standalone/vault-store-loader.d.ts +9 -0
- package/dist-standalone/vault-store-loader.js +1 -187
- package/dist-standalone/verify.js +1 -16
- package/dist-standalone/version-info.js +1 -530
- package/dist-standalone/xfetch.js +1 -335
- package/package.json +1 -1
- package/share1.dat +0 -0
|
@@ -1,257 +1 @@
|
|
|
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 { ok, err } from"./_deps/shared/index.js";
|
|
9
|
-
/**
|
|
10
|
-
* Middleware chain executor.
|
|
11
|
-
* Runs plugins in priority order and handles errors.
|
|
12
|
-
*/
|
|
13
|
-
export class MiddlewareChain {
|
|
14
|
-
plugins = [];
|
|
15
|
-
initialized = false;
|
|
16
|
-
constructor(plugins = []) {
|
|
17
|
-
this.plugins = [...plugins].sort((a, b) => (a.priority ?? 100) - (b.priority ?? 100));
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Register a plugin.
|
|
21
|
-
* Plugins are automatically sorted by priority after registration.
|
|
22
|
-
*/
|
|
23
|
-
register(plugin) {
|
|
24
|
-
if (this.plugins.some((p) => p.name === plugin.name)) {
|
|
25
|
-
return err(`PLUGIN_ALREADY_REGISTERED:${plugin.name}`);
|
|
26
|
-
}
|
|
27
|
-
this.plugins.push(plugin);
|
|
28
|
-
this.plugins.sort((a, b) => (a.priority ?? 100) - (b.priority ?? 100));
|
|
29
|
-
return ok(undefined);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Unregister a plugin by name.
|
|
33
|
-
*/
|
|
34
|
-
unregister(name) {
|
|
35
|
-
const index = this.plugins.findIndex((p) => p.name === name);
|
|
36
|
-
if (index === -1) {
|
|
37
|
-
return err(`PLUGIN_NOT_FOUND:${name}`);
|
|
38
|
-
}
|
|
39
|
-
this.plugins.splice(index, 1);
|
|
40
|
-
return ok(undefined);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Get all registered plugins.
|
|
44
|
-
*/
|
|
45
|
-
getPlugins() {
|
|
46
|
-
return [...this.plugins];
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Initialize all plugins.
|
|
50
|
-
* Must be called before executing hooks.
|
|
51
|
-
*/
|
|
52
|
-
async initialize() {
|
|
53
|
-
if (this.initialized) {
|
|
54
|
-
return ok(undefined);
|
|
55
|
-
}
|
|
56
|
-
for (const plugin of this.plugins) {
|
|
57
|
-
if (plugin.onInit) {
|
|
58
|
-
const result = await plugin.onInit();
|
|
59
|
-
if (result.ok === false) {
|
|
60
|
-
return err(`PLUGIN_INIT_FAILED:${plugin.name}:${result.error}`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
this.initialized = true;
|
|
65
|
-
return ok(undefined);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Destroy all plugins and cleanup resources.
|
|
69
|
-
*/
|
|
70
|
-
async destroy() {
|
|
71
|
-
const errors = [];
|
|
72
|
-
for (const plugin of this.plugins) {
|
|
73
|
-
if (plugin.onDestroy) {
|
|
74
|
-
const result = await plugin.onDestroy();
|
|
75
|
-
if (result.ok === false) {
|
|
76
|
-
errors.push(`${plugin.name}:${result.error}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
this.initialized = false;
|
|
81
|
-
if (errors.length > 0) {
|
|
82
|
-
return err(`PLUGIN_DESTROY_FAILED:${errors.join(',')}`);
|
|
83
|
-
}
|
|
84
|
-
return ok(undefined);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Execute onSend hooks.
|
|
88
|
-
*/
|
|
89
|
-
async executeSend(payload, context) {
|
|
90
|
-
if (!this.initialized) {
|
|
91
|
-
return err('MIDDLEWARE_NOT_INITIALIZED');
|
|
92
|
-
}
|
|
93
|
-
let currentPayload = payload;
|
|
94
|
-
for (const plugin of this.plugins) {
|
|
95
|
-
if (plugin.onSend) {
|
|
96
|
-
const result = await plugin.onSend(currentPayload, context);
|
|
97
|
-
if (result.ok === false) {
|
|
98
|
-
return err(`PLUGIN_HOOK_FAILED:${plugin.name}:${result.error}`);
|
|
99
|
-
}
|
|
100
|
-
const hookResult = result.value;
|
|
101
|
-
if (hookResult.abort) {
|
|
102
|
-
return err(`PLUGIN_ABORTED:${plugin.name}:${hookResult.reason ?? 'no reason'}`);
|
|
103
|
-
}
|
|
104
|
-
currentPayload = hookResult.payload;
|
|
105
|
-
if (hookResult.metadata) {
|
|
106
|
-
Object.assign(context.metadata, hookResult.metadata);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return ok(currentPayload);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Execute onReceive hooks.
|
|
114
|
-
*/
|
|
115
|
-
async executeReceive(envelope, context) {
|
|
116
|
-
if (!this.initialized) {
|
|
117
|
-
return err('MIDDLEWARE_NOT_INITIALIZED');
|
|
118
|
-
}
|
|
119
|
-
let currentEnvelope = envelope;
|
|
120
|
-
for (const plugin of this.plugins) {
|
|
121
|
-
if (plugin.onReceive) {
|
|
122
|
-
const result = await plugin.onReceive(currentEnvelope, context);
|
|
123
|
-
if (result.ok === false) {
|
|
124
|
-
return err(`PLUGIN_HOOK_FAILED:${plugin.name}:${result.error}`);
|
|
125
|
-
}
|
|
126
|
-
const hookResult = result.value;
|
|
127
|
-
if (hookResult.abort) {
|
|
128
|
-
return err(`PLUGIN_ABORTED:${plugin.name}:${hookResult.reason ?? 'no reason'}`);
|
|
129
|
-
}
|
|
130
|
-
currentEnvelope = hookResult.payload;
|
|
131
|
-
if (hookResult.metadata) {
|
|
132
|
-
Object.assign(context.metadata, hookResult.metadata);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return ok(currentEnvelope);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Execute onEncrypt hooks.
|
|
140
|
-
*/
|
|
141
|
-
async executeEncrypt(plaintext, context) {
|
|
142
|
-
if (!this.initialized) {
|
|
143
|
-
return err('MIDDLEWARE_NOT_INITIALIZED');
|
|
144
|
-
}
|
|
145
|
-
let currentPlaintext = plaintext;
|
|
146
|
-
for (const plugin of this.plugins) {
|
|
147
|
-
if (plugin.onEncrypt) {
|
|
148
|
-
const result = await plugin.onEncrypt(currentPlaintext, context);
|
|
149
|
-
if (result.ok === false) {
|
|
150
|
-
return err(`PLUGIN_HOOK_FAILED:${plugin.name}:${result.error}`);
|
|
151
|
-
}
|
|
152
|
-
const hookResult = result.value;
|
|
153
|
-
if (hookResult.abort) {
|
|
154
|
-
return err(`PLUGIN_ABORTED:${plugin.name}:${hookResult.reason ?? 'no reason'}`);
|
|
155
|
-
}
|
|
156
|
-
currentPlaintext = hookResult.payload;
|
|
157
|
-
if (hookResult.metadata) {
|
|
158
|
-
Object.assign(context.metadata, hookResult.metadata);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return ok(currentPlaintext);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Execute onDecrypt hooks.
|
|
166
|
-
*/
|
|
167
|
-
async executeDecrypt(plaintext, context) {
|
|
168
|
-
if (!this.initialized) {
|
|
169
|
-
return err('MIDDLEWARE_NOT_INITIALIZED');
|
|
170
|
-
}
|
|
171
|
-
let currentPlaintext = plaintext;
|
|
172
|
-
for (const plugin of this.plugins) {
|
|
173
|
-
if (plugin.onDecrypt) {
|
|
174
|
-
const result = await plugin.onDecrypt(currentPlaintext, context);
|
|
175
|
-
if (result.ok === false) {
|
|
176
|
-
return err(`PLUGIN_HOOK_FAILED:${plugin.name}:${result.error}`);
|
|
177
|
-
}
|
|
178
|
-
const hookResult = result.value;
|
|
179
|
-
if (hookResult.abort) {
|
|
180
|
-
return err(`PLUGIN_ABORTED:${plugin.name}:${hookResult.reason ?? 'no reason'}`);
|
|
181
|
-
}
|
|
182
|
-
currentPlaintext = hookResult.payload;
|
|
183
|
-
if (hookResult.metadata) {
|
|
184
|
-
Object.assign(context.metadata, hookResult.metadata);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return ok(currentPlaintext);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Create a plugin context.
|
|
193
|
-
*/
|
|
194
|
-
export function createPluginContext(agent, options = {}) {
|
|
195
|
-
return {
|
|
196
|
-
agent,
|
|
197
|
-
recipient: options.recipient,
|
|
198
|
-
scope: options.scope,
|
|
199
|
-
timestamp: options.timestamp ?? Date.now(),
|
|
200
|
-
state: options.state ?? new Map(),
|
|
201
|
-
metadata: options.metadata ?? {},
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Plugin builder for creating plugins with a fluent API.
|
|
206
|
-
*/
|
|
207
|
-
export class PluginBuilder {
|
|
208
|
-
plugin = {};
|
|
209
|
-
constructor(name, version) {
|
|
210
|
-
this.plugin.name = name;
|
|
211
|
-
this.plugin.version = version;
|
|
212
|
-
}
|
|
213
|
-
description(desc) {
|
|
214
|
-
this.plugin.description = desc;
|
|
215
|
-
return this;
|
|
216
|
-
}
|
|
217
|
-
priority(p) {
|
|
218
|
-
this.plugin.priority = p;
|
|
219
|
-
return this;
|
|
220
|
-
}
|
|
221
|
-
onSend(hook) {
|
|
222
|
-
this.plugin.onSend = hook;
|
|
223
|
-
return this;
|
|
224
|
-
}
|
|
225
|
-
onReceive(hook) {
|
|
226
|
-
this.plugin.onReceive = hook;
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
onEncrypt(hook) {
|
|
230
|
-
this.plugin.onEncrypt = hook;
|
|
231
|
-
return this;
|
|
232
|
-
}
|
|
233
|
-
onDecrypt(hook) {
|
|
234
|
-
this.plugin.onDecrypt = hook;
|
|
235
|
-
return this;
|
|
236
|
-
}
|
|
237
|
-
onInit(hook) {
|
|
238
|
-
this.plugin.onInit = hook;
|
|
239
|
-
return this;
|
|
240
|
-
}
|
|
241
|
-
onDestroy(hook) {
|
|
242
|
-
this.plugin.onDestroy = hook;
|
|
243
|
-
return this;
|
|
244
|
-
}
|
|
245
|
-
build() {
|
|
246
|
-
if (!this.plugin.name || !this.plugin.version) {
|
|
247
|
-
throw new Error('Plugin name and version are required');
|
|
248
|
-
}
|
|
249
|
-
return this.plugin;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Create a plugin builder.
|
|
254
|
-
*/
|
|
255
|
-
export function createPlugin(name, version) {
|
|
256
|
-
return new PluginBuilder(name, version);
|
|
257
|
-
}
|
|
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)}
|
|
@@ -1,163 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @module plugins/logging
|
|
3
|
-
* Logging plugin for xBind plugin system
|
|
4
|
-
*
|
|
5
|
-
* Captures send, receive, encrypt, and decrypt events for debugging and audit trails.
|
|
6
|
-
*/
|
|
7
|
-
import { ok } from"../_deps/shared/index.js";
|
|
8
|
-
/**
|
|
9
|
-
* Log level enum.
|
|
10
|
-
*/
|
|
11
|
-
export var LogLevel;
|
|
12
|
-
(function (LogLevel) {
|
|
13
|
-
LogLevel["DEBUG"] = "debug";
|
|
14
|
-
LogLevel["INFO"] = "info";
|
|
15
|
-
LogLevel["WARN"] = "warn";
|
|
16
|
-
LogLevel["ERROR"] = "error";
|
|
17
|
-
})(LogLevel || (LogLevel = {}));
|
|
18
|
-
/**
|
|
19
|
-
* Logging plugin implementation.
|
|
20
|
-
* Captures all message processing events and outputs structured logs.
|
|
21
|
-
*/
|
|
22
|
-
export class LoggingPlugin {
|
|
23
|
-
name = 'logging';
|
|
24
|
-
version = '1.0.0';
|
|
25
|
-
description = 'Captures message processing events for debugging and audit trails';
|
|
26
|
-
priority = 10; // Execute early
|
|
27
|
-
options;
|
|
28
|
-
logs = [];
|
|
29
|
-
constructor(options = {}) {
|
|
30
|
-
this.options = {
|
|
31
|
-
level: options.level ?? LogLevel.INFO,
|
|
32
|
-
includeSize: options.includeSize ?? true,
|
|
33
|
-
includeMetadata: options.includeMetadata ?? false,
|
|
34
|
-
logHandler: options.logHandler ?? ((entry) => console.log(JSON.stringify(entry))),
|
|
35
|
-
filter: options.filter ?? (() => true),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
onInit() {
|
|
39
|
-
this.log({
|
|
40
|
-
timestamp: new Date().toISOString(),
|
|
41
|
-
level: LogLevel.INFO,
|
|
42
|
-
event: 'send',
|
|
43
|
-
agent: 'system',
|
|
44
|
-
metadata: { message: 'Logging plugin initialized' },
|
|
45
|
-
});
|
|
46
|
-
return ok(undefined);
|
|
47
|
-
}
|
|
48
|
-
onDestroy() {
|
|
49
|
-
this.log({
|
|
50
|
-
timestamp: new Date().toISOString(),
|
|
51
|
-
level: LogLevel.INFO,
|
|
52
|
-
event: 'send',
|
|
53
|
-
agent: 'system',
|
|
54
|
-
metadata: { message: 'Logging plugin destroyed', totalLogs: this.logs.length },
|
|
55
|
-
});
|
|
56
|
-
this.logs = [];
|
|
57
|
-
return ok(undefined);
|
|
58
|
-
}
|
|
59
|
-
onSend(payload, context) {
|
|
60
|
-
const size = this.options.includeSize ? this.estimateSize(payload) : undefined;
|
|
61
|
-
this.log({
|
|
62
|
-
timestamp: new Date().toISOString(),
|
|
63
|
-
level: LogLevel.INFO,
|
|
64
|
-
event: 'send',
|
|
65
|
-
agent: context.agent.did,
|
|
66
|
-
recipient: context.recipient,
|
|
67
|
-
scope: context.scope,
|
|
68
|
-
payloadSize: size,
|
|
69
|
-
metadata: this.options.includeMetadata ? context.metadata : undefined,
|
|
70
|
-
});
|
|
71
|
-
return ok({ payload });
|
|
72
|
-
}
|
|
73
|
-
onReceive(envelope, context) {
|
|
74
|
-
const size = this.options.includeSize ? this.estimateSize(envelope) : undefined;
|
|
75
|
-
this.log({
|
|
76
|
-
timestamp: new Date().toISOString(),
|
|
77
|
-
level: LogLevel.INFO,
|
|
78
|
-
event: 'receive',
|
|
79
|
-
agent: context.agent.did,
|
|
80
|
-
recipient: envelope.sender,
|
|
81
|
-
scope: context.scope,
|
|
82
|
-
payloadSize: size,
|
|
83
|
-
metadata: this.options.includeMetadata ? context.metadata : undefined,
|
|
84
|
-
});
|
|
85
|
-
return ok({ payload: envelope });
|
|
86
|
-
}
|
|
87
|
-
onEncrypt(plaintext, context) {
|
|
88
|
-
this.log({
|
|
89
|
-
timestamp: new Date().toISOString(),
|
|
90
|
-
level: LogLevel.DEBUG,
|
|
91
|
-
event: 'encrypt',
|
|
92
|
-
agent: context.agent.did,
|
|
93
|
-
recipient: context.recipient,
|
|
94
|
-
scope: context.scope,
|
|
95
|
-
payloadSize: this.options.includeSize ? plaintext.length : undefined,
|
|
96
|
-
metadata: this.options.includeMetadata ? context.metadata : undefined,
|
|
97
|
-
});
|
|
98
|
-
return ok({ payload: plaintext });
|
|
99
|
-
}
|
|
100
|
-
onDecrypt(plaintext, context) {
|
|
101
|
-
this.log({
|
|
102
|
-
timestamp: new Date().toISOString(),
|
|
103
|
-
level: LogLevel.DEBUG,
|
|
104
|
-
event: 'decrypt',
|
|
105
|
-
agent: context.agent.did,
|
|
106
|
-
scope: context.scope,
|
|
107
|
-
payloadSize: this.options.includeSize ? plaintext.length : undefined,
|
|
108
|
-
metadata: this.options.includeMetadata ? context.metadata : undefined,
|
|
109
|
-
});
|
|
110
|
-
return ok({ payload: plaintext });
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Get all captured logs.
|
|
114
|
-
*/
|
|
115
|
-
getLogs() {
|
|
116
|
-
return [...this.logs];
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Clear all logs.
|
|
120
|
-
*/
|
|
121
|
-
clearLogs() {
|
|
122
|
-
this.logs = [];
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Get logs filtered by criteria.
|
|
126
|
-
*/
|
|
127
|
-
getFilteredLogs(filter) {
|
|
128
|
-
return this.logs.filter(filter);
|
|
129
|
-
}
|
|
130
|
-
log(entry) {
|
|
131
|
-
if (!this.shouldLog(entry.level)) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (!this.options.filter(entry)) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
this.logs.push(entry);
|
|
138
|
-
this.options.logHandler(entry);
|
|
139
|
-
}
|
|
140
|
-
shouldLog(level) {
|
|
141
|
-
const levels = [LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARN, LogLevel.ERROR];
|
|
142
|
-
const thresholdIndex = levels.indexOf(this.options.level);
|
|
143
|
-
const currentIndex = levels.indexOf(level);
|
|
144
|
-
return currentIndex >= thresholdIndex;
|
|
145
|
-
}
|
|
146
|
-
estimateSize(value) {
|
|
147
|
-
if (value instanceof Uint8Array) {
|
|
148
|
-
return value.length;
|
|
149
|
-
}
|
|
150
|
-
try {
|
|
151
|
-
return JSON.stringify(value).length;
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
return 0;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Create a logging plugin with options.
|
|
160
|
-
*/
|
|
161
|
-
export function createLoggingPlugin(options) {
|
|
162
|
-
return new LoggingPlugin(options);
|
|
163
|
-
}
|
|
1
|
+
import{ok}from"../_deps/shared/index.js";export var LogLevel;!function(e){e.DEBUG="debug",e.INFO="info",e.WARN="warn",e.ERROR="error"}(LogLevel||(LogLevel={}));export class LoggingPlugin{name="logging";version="1.0.0";description="Captures message processing events for debugging and audit trails";priority=10;options;logs=[];constructor(e={}){this.options={level:e.level??LogLevel.INFO,includeSize:e.includeSize??!0,includeMetadata:e.includeMetadata??!1,logHandler:e.logHandler??(e=>{console.log(JSON.stringify(e))}),filter:e.filter??(()=>!0)}}onInit(){return this.log({timestamp:(new Date).toISOString(),level:LogLevel.INFO,event:"send",agent:"system",metadata:{message:"Logging plugin initialized"}}),ok(void 0)}onDestroy(){return this.log({timestamp:(new Date).toISOString(),level:LogLevel.INFO,event:"send",agent:"system",metadata:{message:"Logging plugin destroyed",totalLogs:this.logs.length}}),this.logs=[],ok(void 0)}onSend(e,t){const i=this.options.includeSize?this.estimateSize(e):void 0;return this.log({timestamp:(new Date).toISOString(),level:LogLevel.INFO,event:"send",agent:t.agent.did,recipient:t.recipient,scope:t.scope,payloadSize:i,metadata:this.options.includeMetadata?t.metadata:void 0}),ok({payload:e})}onReceive(e,t){const i=this.options.includeSize?this.estimateSize(e):void 0;return this.log({timestamp:(new Date).toISOString(),level:LogLevel.INFO,event:"receive",agent:t.agent.did,recipient:e.sender,scope:t.scope,payloadSize:i,metadata:this.options.includeMetadata?t.metadata:void 0}),ok({payload:e})}onEncrypt(e,t){return this.log({timestamp:(new Date).toISOString(),level:LogLevel.DEBUG,event:"encrypt",agent:t.agent.did,recipient:t.recipient,scope:t.scope,payloadSize:this.options.includeSize?e.length:void 0,metadata:this.options.includeMetadata?t.metadata:void 0}),ok({payload:e})}onDecrypt(e,t){return this.log({timestamp:(new Date).toISOString(),level:LogLevel.DEBUG,event:"decrypt",agent:t.agent.did,scope:t.scope,payloadSize:this.options.includeSize?e.length:void 0,metadata:this.options.includeMetadata?t.metadata:void 0}),ok({payload:e})}getLogs(){return[...this.logs]}clearLogs(){this.logs=[]}getFilteredLogs(e){return this.logs.filter(e)}log(e){this.shouldLog(e.level)&&this.options.filter(e)&&(this.logs.push(e),this.options.logHandler(e))}shouldLog(e){const t=[LogLevel.DEBUG,LogLevel.INFO,LogLevel.WARN,LogLevel.ERROR],i=t.indexOf(this.options.level);return t.indexOf(e)>=i}estimateSize(e){if(e instanceof Uint8Array)return e.length;try{return JSON.stringify(e).length}catch{return 0}}}export function createLoggingPlugin(e){return new LoggingPlugin(e)}
|
|
@@ -74,10 +74,10 @@ export declare class MetricsPlugin implements Plugin {
|
|
|
74
74
|
constructor(options?: MetricsPluginOptions);
|
|
75
75
|
onInit(): Result<void, string>;
|
|
76
76
|
onDestroy(): Result<void, string>;
|
|
77
|
-
onSend(payload: unknown,
|
|
78
|
-
onReceive(envelope: AnyTransportEnvelope,
|
|
79
|
-
onEncrypt(plaintext: Uint8Array,
|
|
80
|
-
onDecrypt(plaintext: Uint8Array,
|
|
77
|
+
onSend(payload: unknown, _context: PluginContext): Result<HookResult<unknown>, string>;
|
|
78
|
+
onReceive(envelope: AnyTransportEnvelope, _context: PluginContext): Result<HookResult<AnyTransportEnvelope>, string>;
|
|
79
|
+
onEncrypt(plaintext: Uint8Array, _context: PluginContext): Result<HookResult<Uint8Array>, string>;
|
|
80
|
+
onDecrypt(plaintext: Uint8Array, _context: PluginContext): Result<HookResult<Uint8Array>, string>;
|
|
81
81
|
/**
|
|
82
82
|
* Get all captured metrics.
|
|
83
83
|
*/
|
|
@@ -1,176 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @module plugins/metrics
|
|
3
|
-
* Metrics plugin for xBind plugin system
|
|
4
|
-
*
|
|
5
|
-
* Tracks performance metrics, message counts, and error rates.
|
|
6
|
-
*/
|
|
7
|
-
import { ok } from"../_deps/shared/index.js";
|
|
8
|
-
/**
|
|
9
|
-
* Metrics plugin implementation.
|
|
10
|
-
* Tracks performance and usage statistics for all operations.
|
|
11
|
-
*/
|
|
12
|
-
export class MetricsPlugin {
|
|
13
|
-
name = 'metrics';
|
|
14
|
-
version = '1.0.0';
|
|
15
|
-
description = 'Tracks performance metrics, message counts, and error rates';
|
|
16
|
-
priority = 20; // Execute after logging
|
|
17
|
-
options;
|
|
18
|
-
metrics = [];
|
|
19
|
-
startTime = Date.now();
|
|
20
|
-
exportTimer;
|
|
21
|
-
constructor(options = {}) {
|
|
22
|
-
this.options = {
|
|
23
|
-
highResolution: options.highResolution ?? true,
|
|
24
|
-
maxMetrics: options.maxMetrics ?? 10000,
|
|
25
|
-
onExport: options.onExport ?? (() => { }),
|
|
26
|
-
exportInterval: options.exportInterval ?? 60000,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
onInit() {
|
|
30
|
-
this.startTime = Date.now();
|
|
31
|
-
if (this.options.exportInterval > 0) {
|
|
32
|
-
this.exportTimer = setInterval(() => {
|
|
33
|
-
this.export();
|
|
34
|
-
}, this.options.exportInterval);
|
|
35
|
-
}
|
|
36
|
-
return ok(undefined);
|
|
37
|
-
}
|
|
38
|
-
onDestroy() {
|
|
39
|
-
if (this.exportTimer) {
|
|
40
|
-
clearInterval(this.exportTimer);
|
|
41
|
-
this.exportTimer = undefined;
|
|
42
|
-
}
|
|
43
|
-
this.export();
|
|
44
|
-
this.metrics = [];
|
|
45
|
-
return ok(undefined);
|
|
46
|
-
}
|
|
47
|
-
onSend(payload, context) {
|
|
48
|
-
const start = this.now();
|
|
49
|
-
// Simulate processing
|
|
50
|
-
const end = this.now();
|
|
51
|
-
this.record({
|
|
52
|
-
timestamp: Date.now(),
|
|
53
|
-
event: 'send',
|
|
54
|
-
duration: end - start,
|
|
55
|
-
success: true,
|
|
56
|
-
});
|
|
57
|
-
return ok({ payload });
|
|
58
|
-
}
|
|
59
|
-
onReceive(envelope, context) {
|
|
60
|
-
const start = this.now();
|
|
61
|
-
// Simulate processing
|
|
62
|
-
const end = this.now();
|
|
63
|
-
this.record({
|
|
64
|
-
timestamp: Date.now(),
|
|
65
|
-
event: 'receive',
|
|
66
|
-
duration: end - start,
|
|
67
|
-
success: true,
|
|
68
|
-
});
|
|
69
|
-
return ok({ payload: envelope });
|
|
70
|
-
}
|
|
71
|
-
onEncrypt(plaintext, context) {
|
|
72
|
-
const start = this.now();
|
|
73
|
-
// Simulate processing
|
|
74
|
-
const end = this.now();
|
|
75
|
-
this.record({
|
|
76
|
-
timestamp: Date.now(),
|
|
77
|
-
event: 'encrypt',
|
|
78
|
-
duration: end - start,
|
|
79
|
-
success: true,
|
|
80
|
-
});
|
|
81
|
-
return ok({ payload: plaintext });
|
|
82
|
-
}
|
|
83
|
-
onDecrypt(plaintext, context) {
|
|
84
|
-
const start = this.now();
|
|
85
|
-
// Simulate processing
|
|
86
|
-
const end = this.now();
|
|
87
|
-
this.record({
|
|
88
|
-
timestamp: Date.now(),
|
|
89
|
-
event: 'decrypt',
|
|
90
|
-
duration: end - start,
|
|
91
|
-
success: true,
|
|
92
|
-
});
|
|
93
|
-
return ok({ payload: plaintext });
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Get all captured metrics.
|
|
97
|
-
*/
|
|
98
|
-
getMetrics() {
|
|
99
|
-
return [...this.metrics];
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Get aggregated metrics summary.
|
|
103
|
-
*/
|
|
104
|
-
getAggregatedMetrics() {
|
|
105
|
-
const sends = this.metrics.filter((m) => m.event === 'send');
|
|
106
|
-
const receives = this.metrics.filter((m) => m.event === 'receive');
|
|
107
|
-
const encrypts = this.metrics.filter((m) => m.event === 'encrypt');
|
|
108
|
-
const decrypts = this.metrics.filter((m) => m.event === 'decrypt');
|
|
109
|
-
const errors = this.metrics.filter((m) => !m.success);
|
|
110
|
-
const avgDuration = (items) => {
|
|
111
|
-
if (items.length === 0)
|
|
112
|
-
return 0;
|
|
113
|
-
return items.reduce((sum, m) => sum + m.duration, 0) / items.length;
|
|
114
|
-
};
|
|
115
|
-
const total = this.metrics.length;
|
|
116
|
-
return {
|
|
117
|
-
totalSends: sends.length,
|
|
118
|
-
totalReceives: receives.length,
|
|
119
|
-
totalEncrypts: encrypts.length,
|
|
120
|
-
totalDecrypts: decrypts.length,
|
|
121
|
-
avgSendDuration: avgDuration(sends),
|
|
122
|
-
avgReceiveDuration: avgDuration(receives),
|
|
123
|
-
avgEncryptDuration: avgDuration(encrypts),
|
|
124
|
-
avgDecryptDuration: avgDuration(decrypts),
|
|
125
|
-
totalErrors: errors.length,
|
|
126
|
-
errorRate: total > 0 ? errors.length / total : 0,
|
|
127
|
-
uptime: Date.now() - this.startTime,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Clear all metrics.
|
|
132
|
-
*/
|
|
133
|
-
clearMetrics() {
|
|
134
|
-
this.metrics = [];
|
|
135
|
-
this.startTime = Date.now();
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Export metrics using the configured callback.
|
|
139
|
-
*/
|
|
140
|
-
export() {
|
|
141
|
-
if (this.metrics.length > 0) {
|
|
142
|
-
this.options.onExport([...this.metrics]);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Get metrics for a specific time range.
|
|
147
|
-
*/
|
|
148
|
-
getMetricsInRange(startTime, endTime) {
|
|
149
|
-
return this.metrics.filter((m) => m.timestamp >= startTime && m.timestamp <= endTime);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Get metrics for a specific event type.
|
|
153
|
-
*/
|
|
154
|
-
getMetricsByEvent(event) {
|
|
155
|
-
return this.metrics.filter((m) => m.event === event);
|
|
156
|
-
}
|
|
157
|
-
record(entry) {
|
|
158
|
-
this.metrics.push(entry);
|
|
159
|
-
// Enforce max metrics limit
|
|
160
|
-
if (this.metrics.length > this.options.maxMetrics) {
|
|
161
|
-
this.metrics.shift(); // Remove oldest
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
now() {
|
|
165
|
-
if (this.options.highResolution && typeof performance !== 'undefined') {
|
|
166
|
-
return performance.now();
|
|
167
|
-
}
|
|
168
|
-
return Date.now();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Create a metrics plugin with options.
|
|
173
|
-
*/
|
|
174
|
-
export function createMetricsPlugin(options) {
|
|
175
|
-
return new MetricsPlugin(options);
|
|
176
|
-
}
|
|
1
|
+
import{ok}from"../_deps/shared/index.js";export class MetricsPlugin{name="metrics";version="1.0.0";description="Tracks performance metrics, message counts, and error rates";priority=20;options;metrics=[];startTime=Date.now();exportTimer;constructor(t={}){this.options={highResolution:t.highResolution??!0,maxMetrics:t.maxMetrics??1e4,onExport:t.onExport??(()=>{}),exportInterval:t.exportInterval??6e4}}onInit(){return this.startTime=Date.now(),this.options.exportInterval>0&&(this.exportTimer=setInterval(()=>{this.export()},this.options.exportInterval)),ok(void 0)}onDestroy(){return this.exportTimer&&(clearInterval(this.exportTimer),this.exportTimer=void 0),this.export(),this.metrics=[],ok(void 0)}onSend(t,e){const r=this.now(),i=this.now();return this.record({timestamp:Date.now(),event:"send",duration:i-r,success:!0}),ok({payload:t})}onReceive(t,e){const r=this.now(),i=this.now();return this.record({timestamp:Date.now(),event:"receive",duration:i-r,success:!0}),ok({payload:t})}onEncrypt(t,e){const r=this.now(),i=this.now();return this.record({timestamp:Date.now(),event:"encrypt",duration:i-r,success:!0}),ok({payload:t})}onDecrypt(t,e){const r=this.now(),i=this.now();return this.record({timestamp:Date.now(),event:"decrypt",duration:i-r,success:!0}),ok({payload:t})}getMetrics(){return[...this.metrics]}getAggregatedMetrics(){const t=this.metrics.filter(t=>"send"===t.event),e=this.metrics.filter(t=>"receive"===t.event),r=this.metrics.filter(t=>"encrypt"===t.event),i=this.metrics.filter(t=>"decrypt"===t.event),s=this.metrics.filter(t=>!t.success),n=t=>0===t.length?0:t.reduce((t,e)=>t+e.duration,0)/t.length,o=this.metrics.length;return{totalSends:t.length,totalReceives:e.length,totalEncrypts:r.length,totalDecrypts:i.length,avgSendDuration:n(t),avgReceiveDuration:n(e),avgEncryptDuration:n(r),avgDecryptDuration:n(i),totalErrors:s.length,errorRate:o>0?s.length/o:0,uptime:Date.now()-this.startTime}}clearMetrics(){this.metrics=[],this.startTime=Date.now()}export(){this.metrics.length>0&&this.options.onExport([...this.metrics])}getMetricsInRange(t,e){return this.metrics.filter(r=>r.timestamp>=t&&r.timestamp<=e)}getMetricsByEvent(t){return this.metrics.filter(e=>e.event===t)}record(t){this.metrics.push(t),this.metrics.length>this.options.maxMetrics&&this.metrics.shift()}now(){return this.options.highResolution&&"undefined"!=typeof performance?performance.now():Date.now()}}export function createMetricsPlugin(t){return new MetricsPlugin(t)}
|