@private.me/xbind 3.0.0 → 3.0.2
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 +55 -7
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
- package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
- package/dist-standalone/_deps/shared/cjs/index.js +463 -1
- package/dist-standalone/_deps/shared/cjs/types.js +315 -1
- package/dist-standalone/_deps/shared/errors.js +244 -1
- package/dist-standalone/_deps/shared/index.js +72 -1
- package/dist-standalone/_deps/shared/types.js +86 -1
- 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 +659 -1
- package/dist-standalone/agent-sdk.js +328 -1
- package/dist-standalone/agent.js +1800 -1
- package/dist-standalone/approval.js +193 -1
- package/dist-standalone/async-iterators.js +382 -1
- package/dist-standalone/auth.js +219 -1
- package/dist-standalone/auto-accept.js +229 -1
- package/dist-standalone/backup-config.js +201 -1
- package/dist-standalone/backup.js +326 -1
- package/dist-standalone/batch-operations.js +388 -1
- package/dist-standalone/cancellation.js +477 -1
- package/dist-standalone/checkpoint.js +186 -1
- package/dist-standalone/circuit-breaker.js +468 -1
- package/dist-standalone/cjs/agent-call.js +701 -1
- package/dist-standalone/cjs/agent-sdk.js +332 -1
- package/dist-standalone/cjs/agent.js +1837 -1
- package/dist-standalone/cjs/approval.js +199 -1
- package/dist-standalone/cjs/async-iterators.js +392 -1
- package/dist-standalone/cjs/auth.js +225 -1
- package/dist-standalone/cjs/auto-accept.js +233 -1
- package/dist-standalone/cjs/backup-config.js +207 -1
- package/dist-standalone/cjs/backup.js +330 -1
- package/dist-standalone/cjs/batch-operations.js +397 -1
- package/dist-standalone/cjs/cancellation.js +490 -1
- package/dist-standalone/cjs/checkpoint.js +193 -1
- package/dist-standalone/cjs/circuit-breaker.js +476 -1
- package/dist-standalone/cjs/cli/init.js +492 -1
- package/dist-standalone/cjs/config-validation.js +522 -1
- package/dist-standalone/cjs/connect.js +312 -1
- package/dist-standalone/cjs/connection-pool.js +506 -1
- package/dist-standalone/cjs/correlation-id.js +339 -1
- package/dist-standalone/cjs/crypto-utils.js +176 -1
- package/dist-standalone/cjs/debug-mode.js +534 -1
- package/dist-standalone/cjs/did-document.js +101 -1
- package/dist-standalone/cjs/did-privateme.js +130 -1
- package/dist-standalone/cjs/did-web.js +201 -1
- package/dist-standalone/cjs/discovery.js +462 -1
- package/dist-standalone/cjs/dual-mode.js +251 -1
- package/dist-standalone/cjs/email-templates.js +313 -1
- package/dist-standalone/cjs/email-transport.js +239 -1
- package/dist-standalone/cjs/envelope.js +538 -1
- package/dist-standalone/cjs/errors.js +913 -1
- package/dist-standalone/cjs/event-emitter.js +461 -1
- package/dist-standalone/cjs/gateway-state.js +55 -1
- package/dist-standalone/cjs/gateway-transport.js +120 -1
- package/dist-standalone/cjs/graceful-degradation.js +403 -1
- package/dist-standalone/cjs/guardrails.js +223 -1
- package/dist-standalone/cjs/health-check.js +336 -1
- package/dist-standalone/cjs/http-compat.js +272 -1
- package/dist-standalone/cjs/http-status-map.js +571 -1
- package/dist-standalone/cjs/identity.js +645 -1
- package/dist-standalone/cjs/index.js +406 -1
- package/dist-standalone/cjs/invitation.js +421 -1
- package/dist-standalone/cjs/invite.js +328 -1
- package/dist-standalone/cjs/key-agreement.js +335 -1
- package/dist-standalone/cjs/lazy-init.js +300 -1
- package/dist-standalone/cjs/logger.js +291 -1
- package/dist-standalone/cjs/mdns-discovery.js +202 -1
- package/dist-standalone/cjs/nonce-store.js +80 -1
- package/dist-standalone/cjs/pairing-manager.js +223 -1
- package/dist-standalone/cjs/plugin-system.js +264 -1
- package/dist-standalone/cjs/plugins/logging.js +168 -1
- package/dist-standalone/cjs/plugins/metrics.js +181 -1
- package/dist-standalone/cjs/plugins/validation.js +302 -1
- package/dist-standalone/cjs/policy.js +320 -1
- package/dist-standalone/cjs/progress-callbacks.js +583 -1
- package/dist-standalone/cjs/redis-nonce-store.js +76 -1
- package/dist-standalone/cjs/registry-middleware.js +50 -1
- package/dist-standalone/cjs/retry-strategies.js +544 -1
- package/dist-standalone/cjs/retry-transport.js +102 -1
- package/dist-standalone/cjs/runtime/browser.js +533 -1
- package/dist-standalone/cjs/runtime/edge.js +526 -1
- package/dist-standalone/cjs/runtime/react-native.js +394 -1
- package/dist-standalone/cjs/security-policy.js +245 -1
- package/dist-standalone/cjs/serialization.js +1040 -1
- package/dist-standalone/cjs/split-channel.js +225 -1
- package/dist-standalone/cjs/subscription-proof.js +230 -1
- package/dist-standalone/cjs/succession.js +148 -1
- package/dist-standalone/cjs/timeouts.js +412 -1
- package/dist-standalone/cjs/trace-context.js +424 -1
- package/dist-standalone/cjs/trace-spans.js +495 -1
- package/dist-standalone/cjs/transport.js +63 -1
- package/dist-standalone/cjs/trust-registry.js +991 -1
- package/dist-standalone/cjs/types/error-response.js +56 -1
- package/dist-standalone/cjs/vault-auth.js +178 -1
- package/dist-standalone/cjs/vault-store-loader.js +194 -1
- package/dist-standalone/cjs/verify.js +25 -1
- package/dist-standalone/cjs/version-info.js +543 -1
- package/dist-standalone/cjs/xfetch.js +340 -1
- package/dist-standalone/cli/init.js +455 -1
- package/dist-standalone/cli/setup.js +514 -1
- package/dist-standalone/cli/types.js +27 -1
- package/dist-standalone/cli/xbind.js +148 -1
- package/dist-standalone/config-validation.js +513 -1
- package/dist-standalone/connect.js +274 -1
- package/dist-standalone/connection-pool.js +500 -1
- package/dist-standalone/correlation-id.js +326 -1
- package/dist-standalone/crypto-utils.js +157 -1
- package/dist-standalone/debug-mode.js +510 -1
- package/dist-standalone/did-document.js +96 -1
- package/dist-standalone/did-privateme.js +121 -1
- package/dist-standalone/did-web.js +196 -1
- package/dist-standalone/discovery.js +458 -1
- package/dist-standalone/dual-mode.js +247 -1
- package/dist-standalone/email-templates.js +309 -1
- package/dist-standalone/email-transport.js +232 -1
- package/dist-standalone/envelope.js +525 -1
- package/dist-standalone/errors.js +896 -1
- package/dist-standalone/event-emitter.js +456 -1
- package/dist-standalone/gateway-state.js +51 -1
- package/dist-standalone/gateway-transport.js +116 -1
- package/dist-standalone/graceful-degradation.js +396 -1
- package/dist-standalone/guardrails.js +216 -1
- package/dist-standalone/health-check.js +332 -1
- package/dist-standalone/http-compat.js +267 -1
- package/dist-standalone/http-status-map.js +561 -1
- package/dist-standalone/identity.js +619 -1
- package/dist-standalone/index.js +78 -1
- package/dist-standalone/invitation.js +415 -1
- package/dist-standalone/invite.js +324 -1
- package/dist-standalone/key-agreement.js +325 -1
- package/dist-standalone/lazy-init.js +295 -1
- package/dist-standalone/logger.js +285 -1
- package/dist-standalone/mdns-discovery.js +195 -1
- package/dist-standalone/nonce-store.js +76 -1
- package/dist-standalone/pairing-manager.js +219 -1
- package/dist-standalone/plugin-system.js +257 -1
- package/dist-standalone/plugins/logging.d.ts +84 -0
- package/dist-standalone/plugins/logging.js +163 -0
- package/dist-standalone/plugins/metrics.d.ts +111 -0
- package/dist-standalone/plugins/metrics.js +176 -0
- package/dist-standalone/plugins/validation.d.ts +104 -0
- package/dist-standalone/plugins/validation.js +297 -0
- package/dist-standalone/policy.js +315 -1
- package/dist-standalone/progress-callbacks.js +576 -1
- package/dist-standalone/redis-nonce-store.js +72 -1
- package/dist-standalone/registry-middleware.js +47 -1
- package/dist-standalone/retry-strategies.js +534 -1
- package/dist-standalone/retry-transport.js +98 -1
- package/dist-standalone/runtime/browser.d.ts +311 -0
- package/dist-standalone/runtime/browser.js +516 -0
- package/dist-standalone/runtime/edge.d.ts +282 -0
- package/dist-standalone/runtime/edge.js +511 -0
- package/dist-standalone/runtime/react-native.d.ts +157 -0
- package/dist-standalone/runtime/react-native.js +383 -0
- package/dist-standalone/security-policy.js +239 -1
- package/dist-standalone/serialization.js +1031 -1
- package/dist-standalone/split-channel.js +219 -1
- package/dist-standalone/subscription-proof.js +224 -1
- package/dist-standalone/succession.js +142 -1
- package/dist-standalone/timeouts.js +398 -1
- package/dist-standalone/trace-context.js +414 -1
- package/dist-standalone/trace-spans.js +488 -1
- package/dist-standalone/transport.js +59 -1
- package/dist-standalone/trust-registry.js +950 -1
- package/dist-standalone/types/error-response.d.ts +209 -0
- package/dist-standalone/types/error-response.js +52 -0
- package/dist-standalone/vault-auth.js +174 -1
- package/dist-standalone/vault-store-loader.js +187 -1
- package/dist-standalone/verify.js +16 -1
- package/dist-standalone/version-info.js +530 -1
- package/dist-standalone/xfetch.js +335 -1
- package/package.json +4 -10
- package/share1.dat +0 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
- package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
- package/dist-standalone/_deps/shared/cjs/package.json +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
- package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
- package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
- package/dist-standalone/cjs/package.json +0 -3
- package/dist-standalone/package.json +0 -10
|
@@ -1 +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
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
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 type { Result } from '@private.me/shared';
|
|
8
|
+
import type { Plugin, PluginContext, HookResult } from '../plugin-system.js';
|
|
9
|
+
import type { AnyTransportEnvelope } from '../envelope.js';
|
|
10
|
+
/**
|
|
11
|
+
* Log level enum.
|
|
12
|
+
*/
|
|
13
|
+
export declare enum LogLevel {
|
|
14
|
+
DEBUG = "debug",
|
|
15
|
+
INFO = "info",
|
|
16
|
+
WARN = "warn",
|
|
17
|
+
ERROR = "error"
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Log entry structure.
|
|
21
|
+
*/
|
|
22
|
+
export interface LogEntry {
|
|
23
|
+
timestamp: string;
|
|
24
|
+
level: LogLevel;
|
|
25
|
+
event: 'send' | 'receive' | 'encrypt' | 'decrypt';
|
|
26
|
+
agent: string;
|
|
27
|
+
recipient?: string;
|
|
28
|
+
scope?: string;
|
|
29
|
+
payloadSize?: number;
|
|
30
|
+
metadata?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Logging plugin options.
|
|
34
|
+
*/
|
|
35
|
+
export interface LoggingPluginOptions {
|
|
36
|
+
/** Log level threshold (default: INFO) */
|
|
37
|
+
level?: LogLevel;
|
|
38
|
+
/** Include payload sizes in logs (default: true) */
|
|
39
|
+
includeSize?: boolean;
|
|
40
|
+
/** Include metadata in logs (default: false) */
|
|
41
|
+
includeMetadata?: boolean;
|
|
42
|
+
/** Custom log handler (default: console.log) */
|
|
43
|
+
logHandler?: (entry: LogEntry) => void;
|
|
44
|
+
/** Filter function to exclude certain logs */
|
|
45
|
+
filter?: (entry: LogEntry) => boolean;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Logging plugin implementation.
|
|
49
|
+
* Captures all message processing events and outputs structured logs.
|
|
50
|
+
*/
|
|
51
|
+
export declare class LoggingPlugin implements Plugin {
|
|
52
|
+
readonly name = "logging";
|
|
53
|
+
readonly version = "1.0.0";
|
|
54
|
+
readonly description = "Captures message processing events for debugging and audit trails";
|
|
55
|
+
readonly priority = 10;
|
|
56
|
+
private options;
|
|
57
|
+
private logs;
|
|
58
|
+
constructor(options?: LoggingPluginOptions);
|
|
59
|
+
onInit(): Result<void, string>;
|
|
60
|
+
onDestroy(): Result<void, string>;
|
|
61
|
+
onSend(payload: unknown, context: PluginContext): Result<HookResult<unknown>, string>;
|
|
62
|
+
onReceive(envelope: AnyTransportEnvelope, context: PluginContext): Result<HookResult<AnyTransportEnvelope>, string>;
|
|
63
|
+
onEncrypt(plaintext: Uint8Array, context: PluginContext): Result<HookResult<Uint8Array>, string>;
|
|
64
|
+
onDecrypt(plaintext: Uint8Array, context: PluginContext): Result<HookResult<Uint8Array>, string>;
|
|
65
|
+
/**
|
|
66
|
+
* Get all captured logs.
|
|
67
|
+
*/
|
|
68
|
+
getLogs(): readonly LogEntry[];
|
|
69
|
+
/**
|
|
70
|
+
* Clear all logs.
|
|
71
|
+
*/
|
|
72
|
+
clearLogs(): void;
|
|
73
|
+
/**
|
|
74
|
+
* Get logs filtered by criteria.
|
|
75
|
+
*/
|
|
76
|
+
getFilteredLogs(filter: (entry: LogEntry) => boolean): LogEntry[];
|
|
77
|
+
private log;
|
|
78
|
+
private shouldLog;
|
|
79
|
+
private estimateSize;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create a logging plugin with options.
|
|
83
|
+
*/
|
|
84
|
+
export declare function createLoggingPlugin(options?: LoggingPluginOptions): LoggingPlugin;
|
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
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 type { Result } from '@private.me/shared';
|
|
8
|
+
import type { Plugin, PluginContext, HookResult } from '../plugin-system.js';
|
|
9
|
+
import type { AnyTransportEnvelope } from '../envelope.js';
|
|
10
|
+
/**
|
|
11
|
+
* Metric entry structure.
|
|
12
|
+
*/
|
|
13
|
+
export interface MetricEntry {
|
|
14
|
+
timestamp: number;
|
|
15
|
+
event: 'send' | 'receive' | 'encrypt' | 'decrypt';
|
|
16
|
+
duration: number;
|
|
17
|
+
success: boolean;
|
|
18
|
+
error?: string;
|
|
19
|
+
metadata?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Aggregated metrics.
|
|
23
|
+
*/
|
|
24
|
+
export interface AggregatedMetrics {
|
|
25
|
+
/** Total number of send operations */
|
|
26
|
+
totalSends: number;
|
|
27
|
+
/** Total number of receive operations */
|
|
28
|
+
totalReceives: number;
|
|
29
|
+
/** Total number of encrypt operations */
|
|
30
|
+
totalEncrypts: number;
|
|
31
|
+
/** Total number of decrypt operations */
|
|
32
|
+
totalDecrypts: number;
|
|
33
|
+
/** Average send duration (ms) */
|
|
34
|
+
avgSendDuration: number;
|
|
35
|
+
/** Average receive duration (ms) */
|
|
36
|
+
avgReceiveDuration: number;
|
|
37
|
+
/** Average encrypt duration (ms) */
|
|
38
|
+
avgEncryptDuration: number;
|
|
39
|
+
/** Average decrypt duration (ms) */
|
|
40
|
+
avgDecryptDuration: number;
|
|
41
|
+
/** Total errors */
|
|
42
|
+
totalErrors: number;
|
|
43
|
+
/** Error rate (0-1) */
|
|
44
|
+
errorRate: number;
|
|
45
|
+
/** Uptime (ms) */
|
|
46
|
+
uptime: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Metrics plugin options.
|
|
50
|
+
*/
|
|
51
|
+
export interface MetricsPluginOptions {
|
|
52
|
+
/** Enable high-resolution timing (default: true) */
|
|
53
|
+
highResolution?: boolean;
|
|
54
|
+
/** Maximum metrics to store in memory (default: 10000) */
|
|
55
|
+
maxMetrics?: number;
|
|
56
|
+
/** Metrics export callback */
|
|
57
|
+
onExport?: (metrics: MetricEntry[]) => void;
|
|
58
|
+
/** Export interval in milliseconds (default: 60000 - 1 minute) */
|
|
59
|
+
exportInterval?: number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Metrics plugin implementation.
|
|
63
|
+
* Tracks performance and usage statistics for all operations.
|
|
64
|
+
*/
|
|
65
|
+
export declare class MetricsPlugin implements Plugin {
|
|
66
|
+
readonly name = "metrics";
|
|
67
|
+
readonly version = "1.0.0";
|
|
68
|
+
readonly description = "Tracks performance metrics, message counts, and error rates";
|
|
69
|
+
readonly priority = 20;
|
|
70
|
+
private options;
|
|
71
|
+
private metrics;
|
|
72
|
+
private startTime;
|
|
73
|
+
private exportTimer?;
|
|
74
|
+
constructor(options?: MetricsPluginOptions);
|
|
75
|
+
onInit(): Result<void, string>;
|
|
76
|
+
onDestroy(): Result<void, string>;
|
|
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
|
+
/**
|
|
82
|
+
* Get all captured metrics.
|
|
83
|
+
*/
|
|
84
|
+
getMetrics(): readonly MetricEntry[];
|
|
85
|
+
/**
|
|
86
|
+
* Get aggregated metrics summary.
|
|
87
|
+
*/
|
|
88
|
+
getAggregatedMetrics(): AggregatedMetrics;
|
|
89
|
+
/**
|
|
90
|
+
* Clear all metrics.
|
|
91
|
+
*/
|
|
92
|
+
clearMetrics(): void;
|
|
93
|
+
/**
|
|
94
|
+
* Export metrics using the configured callback.
|
|
95
|
+
*/
|
|
96
|
+
export(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Get metrics for a specific time range.
|
|
99
|
+
*/
|
|
100
|
+
getMetricsInRange(startTime: number, endTime: number): MetricEntry[];
|
|
101
|
+
/**
|
|
102
|
+
* Get metrics for a specific event type.
|
|
103
|
+
*/
|
|
104
|
+
getMetricsByEvent(event: 'send' | 'receive' | 'encrypt' | 'decrypt'): MetricEntry[];
|
|
105
|
+
private record;
|
|
106
|
+
private now;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create a metrics plugin with options.
|
|
110
|
+
*/
|
|
111
|
+
export declare function createMetricsPlugin(options?: MetricsPluginOptions): MetricsPlugin;
|