@private.me/xbind 3.0.1 → 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 -14
- 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.js +163 -1
- package/dist-standalone/plugins/metrics.js +176 -1
- package/dist-standalone/plugins/validation.js +297 -1
- 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.js +516 -1
- package/dist-standalone/runtime/edge.js +511 -1
- package/dist-standalone/runtime/react-native.js +383 -1
- 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.js +52 -1
- 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 -13
- 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,495 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module trace-spans
|
|
4
|
+
* Distributed tracing spans with parent-child relationships
|
|
5
|
+
*
|
|
6
|
+
* Provides span tracking for distributed tracing with automatic timing,
|
|
7
|
+
* attribute collection, and integration with W3C Trace Context.
|
|
8
|
+
*
|
|
9
|
+
* Performance target: <1ms overhead per operation
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { TraceSpan, SpanRecorder } from '@private.me/xbind';
|
|
14
|
+
*
|
|
15
|
+
* // Create a span recorder
|
|
16
|
+
* const recorder = new SpanRecorder();
|
|
17
|
+
*
|
|
18
|
+
* // Start a span
|
|
19
|
+
* const span = recorder.startSpan('operation-name', { attr: 'value' });
|
|
20
|
+
*
|
|
21
|
+
* // Do work...
|
|
22
|
+
*
|
|
23
|
+
* // End the span
|
|
24
|
+
* span.end();
|
|
25
|
+
*
|
|
26
|
+
* // Get completed spans
|
|
27
|
+
* const spans = recorder.getCompletedSpans();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.SpanRecorder = exports.TraceSpan = exports.SpanKind = exports.SpanStatusCode = void 0;
|
|
32
|
+
exports.measureAsync = measureAsync;
|
|
33
|
+
exports.measureSync = measureSync;
|
|
34
|
+
const trace_context_js_1 = require("./trace-context.js");
|
|
35
|
+
/**
|
|
36
|
+
* Span status codes (aligned with OpenTelemetry)
|
|
37
|
+
*/
|
|
38
|
+
var SpanStatusCode;
|
|
39
|
+
(function (SpanStatusCode) {
|
|
40
|
+
/** Operation completed successfully */
|
|
41
|
+
SpanStatusCode[SpanStatusCode["OK"] = 0] = "OK";
|
|
42
|
+
/** Operation encountered an error */
|
|
43
|
+
SpanStatusCode[SpanStatusCode["ERROR"] = 1] = "ERROR";
|
|
44
|
+
/** Status is unset (default) */
|
|
45
|
+
SpanStatusCode[SpanStatusCode["UNSET"] = 2] = "UNSET";
|
|
46
|
+
})(SpanStatusCode || (exports.SpanStatusCode = SpanStatusCode = {}));
|
|
47
|
+
/**
|
|
48
|
+
* Span kind (aligned with OpenTelemetry)
|
|
49
|
+
*/
|
|
50
|
+
var SpanKind;
|
|
51
|
+
(function (SpanKind) {
|
|
52
|
+
/** Internal span (default) */
|
|
53
|
+
SpanKind[SpanKind["INTERNAL"] = 0] = "INTERNAL";
|
|
54
|
+
/** Server span (handling incoming request) */
|
|
55
|
+
SpanKind[SpanKind["SERVER"] = 1] = "SERVER";
|
|
56
|
+
/** Client span (outgoing request) */
|
|
57
|
+
SpanKind[SpanKind["CLIENT"] = 2] = "CLIENT";
|
|
58
|
+
/** Producer span (message queue producer) */
|
|
59
|
+
SpanKind[SpanKind["PRODUCER"] = 3] = "PRODUCER";
|
|
60
|
+
/** Consumer span (message queue consumer) */
|
|
61
|
+
SpanKind[SpanKind["CONSUMER"] = 4] = "CONSUMER";
|
|
62
|
+
})(SpanKind || (exports.SpanKind = SpanKind = {}));
|
|
63
|
+
/**
|
|
64
|
+
* Trace span for distributed tracing
|
|
65
|
+
*
|
|
66
|
+
* Represents a single unit of work in a distributed trace with automatic timing.
|
|
67
|
+
*/
|
|
68
|
+
class TraceSpan {
|
|
69
|
+
/** Span ID (unique within trace) */
|
|
70
|
+
spanId;
|
|
71
|
+
/** Trace context */
|
|
72
|
+
traceContext;
|
|
73
|
+
/** Span name */
|
|
74
|
+
name;
|
|
75
|
+
/** Span kind */
|
|
76
|
+
kind;
|
|
77
|
+
/** Parent span ID */
|
|
78
|
+
parentSpanId;
|
|
79
|
+
/** Start timestamp (milliseconds since epoch) */
|
|
80
|
+
startTime;
|
|
81
|
+
/** End timestamp (milliseconds since epoch, undefined if not ended) */
|
|
82
|
+
endTime;
|
|
83
|
+
/** Span status */
|
|
84
|
+
status;
|
|
85
|
+
/** Span attributes */
|
|
86
|
+
attributes;
|
|
87
|
+
/** Span events */
|
|
88
|
+
events;
|
|
89
|
+
/** Whether span has ended */
|
|
90
|
+
ended;
|
|
91
|
+
/**
|
|
92
|
+
* Create a trace span
|
|
93
|
+
*
|
|
94
|
+
* @param name - Span name
|
|
95
|
+
* @param traceContext - Trace context
|
|
96
|
+
* @param options - Span options
|
|
97
|
+
*/
|
|
98
|
+
constructor(name, traceContext, options = {}) {
|
|
99
|
+
this.spanId = (0, trace_context_js_1.generateSpanId)();
|
|
100
|
+
this.traceContext = traceContext;
|
|
101
|
+
this.name = name;
|
|
102
|
+
this.kind = options.kind ?? SpanKind.INTERNAL;
|
|
103
|
+
this.parentSpanId = options.parentSpanId;
|
|
104
|
+
this.startTime = Date.now();
|
|
105
|
+
this.status = { code: SpanStatusCode.UNSET };
|
|
106
|
+
this.attributes = options.attributes ?? {};
|
|
107
|
+
this.events = [];
|
|
108
|
+
this.ended = false;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Set span attribute
|
|
112
|
+
*
|
|
113
|
+
* @param key - Attribute key
|
|
114
|
+
* @param value - Attribute value
|
|
115
|
+
* @returns This span (for chaining)
|
|
116
|
+
*/
|
|
117
|
+
setAttribute(key, value) {
|
|
118
|
+
if (this.ended) {
|
|
119
|
+
throw new Error('Cannot set attribute on ended span');
|
|
120
|
+
}
|
|
121
|
+
this.attributes[key] = value;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Set multiple span attributes
|
|
126
|
+
*
|
|
127
|
+
* @param attributes - Attributes to set
|
|
128
|
+
* @returns This span (for chaining)
|
|
129
|
+
*/
|
|
130
|
+
setAttributes(attributes) {
|
|
131
|
+
if (this.ended) {
|
|
132
|
+
throw new Error('Cannot set attributes on ended span');
|
|
133
|
+
}
|
|
134
|
+
Object.assign(this.attributes, attributes);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Add an event to the span
|
|
139
|
+
*
|
|
140
|
+
* Events are timestamped log entries within a span.
|
|
141
|
+
*
|
|
142
|
+
* @param name - Event name
|
|
143
|
+
* @param attributes - Optional event attributes
|
|
144
|
+
* @returns This span (for chaining)
|
|
145
|
+
*/
|
|
146
|
+
addEvent(name, attributes) {
|
|
147
|
+
if (this.ended) {
|
|
148
|
+
throw new Error('Cannot add event to ended span');
|
|
149
|
+
}
|
|
150
|
+
this.events.push({
|
|
151
|
+
name,
|
|
152
|
+
timestamp: Date.now(),
|
|
153
|
+
attributes,
|
|
154
|
+
});
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Set span status
|
|
159
|
+
*
|
|
160
|
+
* @param code - Status code
|
|
161
|
+
* @param message - Optional status message
|
|
162
|
+
* @returns This span (for chaining)
|
|
163
|
+
*/
|
|
164
|
+
setStatus(code, message) {
|
|
165
|
+
if (this.ended) {
|
|
166
|
+
throw new Error('Cannot set status on ended span');
|
|
167
|
+
}
|
|
168
|
+
this.status = { code, message };
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Record an exception in the span
|
|
173
|
+
*
|
|
174
|
+
* Automatically sets status to ERROR and adds exception event.
|
|
175
|
+
*
|
|
176
|
+
* @param error - Error to record
|
|
177
|
+
* @returns This span (for chaining)
|
|
178
|
+
*/
|
|
179
|
+
recordException(error) {
|
|
180
|
+
if (this.ended) {
|
|
181
|
+
throw new Error('Cannot record exception on ended span');
|
|
182
|
+
}
|
|
183
|
+
this.setStatus(SpanStatusCode.ERROR);
|
|
184
|
+
const attributes = {};
|
|
185
|
+
if (error instanceof Error) {
|
|
186
|
+
attributes['exception.type'] = error.name;
|
|
187
|
+
attributes['exception.message'] = error.message;
|
|
188
|
+
if (error.stack) {
|
|
189
|
+
attributes['exception.stacktrace'] = error.stack;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
attributes['exception.message'] = String(error);
|
|
194
|
+
}
|
|
195
|
+
this.addEvent('exception', attributes);
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* End the span
|
|
200
|
+
*
|
|
201
|
+
* Records the end timestamp and marks the span as complete.
|
|
202
|
+
* If status is UNSET, automatically sets to OK.
|
|
203
|
+
*
|
|
204
|
+
* @param endTime - Optional end timestamp (defaults to now)
|
|
205
|
+
*/
|
|
206
|
+
end(endTime) {
|
|
207
|
+
if (this.ended) {
|
|
208
|
+
return; // Idempotent
|
|
209
|
+
}
|
|
210
|
+
this.endTime = endTime ?? Date.now();
|
|
211
|
+
this.ended = true;
|
|
212
|
+
// Auto-set status to OK if unset
|
|
213
|
+
if (this.status.code === SpanStatusCode.UNSET) {
|
|
214
|
+
this.status = { code: SpanStatusCode.OK };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Check if span has ended
|
|
219
|
+
*
|
|
220
|
+
* @returns True if ended
|
|
221
|
+
*/
|
|
222
|
+
isEnded() {
|
|
223
|
+
return this.ended;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Get span duration in milliseconds
|
|
227
|
+
*
|
|
228
|
+
* @returns Duration or null if not ended
|
|
229
|
+
*/
|
|
230
|
+
getDuration() {
|
|
231
|
+
if (!this.ended || !this.endTime)
|
|
232
|
+
return null;
|
|
233
|
+
return this.endTime - this.startTime;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Export span data
|
|
237
|
+
*
|
|
238
|
+
* @returns Span data (throws if span not ended)
|
|
239
|
+
*/
|
|
240
|
+
toData() {
|
|
241
|
+
if (!this.ended || !this.endTime) {
|
|
242
|
+
throw new Error('Cannot export data from active span. Call end() first.');
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
spanId: this.spanId,
|
|
246
|
+
traceId: this.traceContext.traceId,
|
|
247
|
+
parentSpanId: this.parentSpanId,
|
|
248
|
+
name: this.name,
|
|
249
|
+
kind: this.kind,
|
|
250
|
+
startTime: this.startTime,
|
|
251
|
+
endTime: this.endTime,
|
|
252
|
+
duration: this.endTime - this.startTime,
|
|
253
|
+
status: { ...this.status },
|
|
254
|
+
attributes: { ...this.attributes },
|
|
255
|
+
events: this.events.map((e) => ({
|
|
256
|
+
name: e.name,
|
|
257
|
+
timestamp: e.timestamp,
|
|
258
|
+
attributes: e.attributes ? { ...e.attributes } : undefined,
|
|
259
|
+
})),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Create a child span
|
|
264
|
+
*
|
|
265
|
+
* The child span will have the same trace ID but a new span ID.
|
|
266
|
+
*
|
|
267
|
+
* @param name - Child span name
|
|
268
|
+
* @param options - Span options
|
|
269
|
+
* @returns New child span
|
|
270
|
+
*/
|
|
271
|
+
createChild(name, options = {}) {
|
|
272
|
+
return new TraceSpan(name, this.traceContext, {
|
|
273
|
+
...options,
|
|
274
|
+
parentSpanId: this.spanId,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
exports.TraceSpan = TraceSpan;
|
|
279
|
+
/**
|
|
280
|
+
* Span recorder for collecting completed spans
|
|
281
|
+
*
|
|
282
|
+
* Manages span lifecycle and provides access to completed span data.
|
|
283
|
+
*/
|
|
284
|
+
class SpanRecorder {
|
|
285
|
+
/** Active spans (not yet ended) */
|
|
286
|
+
activeSpans;
|
|
287
|
+
/** Completed spans */
|
|
288
|
+
completedSpans;
|
|
289
|
+
/** Maximum number of completed spans to keep (prevent memory leak) */
|
|
290
|
+
maxSpans;
|
|
291
|
+
/** Trace context for root spans */
|
|
292
|
+
rootContext;
|
|
293
|
+
/**
|
|
294
|
+
* Create a span recorder
|
|
295
|
+
*
|
|
296
|
+
* @param options - Recorder options
|
|
297
|
+
*/
|
|
298
|
+
constructor(options = {}) {
|
|
299
|
+
this.activeSpans = new Map();
|
|
300
|
+
this.completedSpans = [];
|
|
301
|
+
this.maxSpans = options.maxSpans ?? 1000;
|
|
302
|
+
this.rootContext = options.traceContext;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Start a new span
|
|
306
|
+
*
|
|
307
|
+
* @param name - Span name
|
|
308
|
+
* @param options - Span options
|
|
309
|
+
* @returns New span
|
|
310
|
+
*/
|
|
311
|
+
startSpan(name, options = {}) {
|
|
312
|
+
const context = options.traceContext ??
|
|
313
|
+
options.parentSpan?.traceContext ??
|
|
314
|
+
this.rootContext ??
|
|
315
|
+
trace_context_js_1.TraceContext.create();
|
|
316
|
+
const span = new TraceSpan(name, context, {
|
|
317
|
+
kind: options.kind,
|
|
318
|
+
attributes: options.attributes,
|
|
319
|
+
parentSpanId: options.parentSpan?.spanId,
|
|
320
|
+
});
|
|
321
|
+
this.activeSpans.set(span.spanId, span);
|
|
322
|
+
// Auto-end on next tick if forgotten (safety measure)
|
|
323
|
+
// This prevents memory leaks from forgotten spans
|
|
324
|
+
const timeout = setTimeout(() => {
|
|
325
|
+
if (!span.isEnded()) {
|
|
326
|
+
span.setStatus(SpanStatusCode.ERROR, 'Span not ended (auto-ended by recorder)');
|
|
327
|
+
this.endSpan(span);
|
|
328
|
+
}
|
|
329
|
+
}, 60000); // 60 second timeout
|
|
330
|
+
// Clear timeout if span ends normally
|
|
331
|
+
const originalEnd = span.end.bind(span);
|
|
332
|
+
span.end = (endTime) => {
|
|
333
|
+
clearTimeout(timeout);
|
|
334
|
+
originalEnd(endTime);
|
|
335
|
+
this.endSpan(span);
|
|
336
|
+
};
|
|
337
|
+
return span;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* End a span and record its data
|
|
341
|
+
*
|
|
342
|
+
* @param span - Span to end
|
|
343
|
+
*/
|
|
344
|
+
endSpan(span) {
|
|
345
|
+
if (!span.isEnded()) {
|
|
346
|
+
span.end();
|
|
347
|
+
}
|
|
348
|
+
// Remove from active spans
|
|
349
|
+
this.activeSpans.delete(span.spanId);
|
|
350
|
+
// Add to completed spans
|
|
351
|
+
const data = span.toData();
|
|
352
|
+
this.completedSpans.push(data);
|
|
353
|
+
// Enforce max spans limit (FIFO)
|
|
354
|
+
if (this.completedSpans.length > this.maxSpans) {
|
|
355
|
+
this.completedSpans.shift();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Get all completed spans
|
|
360
|
+
*
|
|
361
|
+
* @returns Array of completed span data
|
|
362
|
+
*/
|
|
363
|
+
getCompletedSpans() {
|
|
364
|
+
return [...this.completedSpans];
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Get active spans
|
|
368
|
+
*
|
|
369
|
+
* @returns Array of active spans
|
|
370
|
+
*/
|
|
371
|
+
getActiveSpans() {
|
|
372
|
+
return Array.from(this.activeSpans.values());
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Clear all completed spans
|
|
376
|
+
*/
|
|
377
|
+
clearCompletedSpans() {
|
|
378
|
+
this.completedSpans = [];
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Get span count
|
|
382
|
+
*
|
|
383
|
+
* @returns Object with active and completed span counts
|
|
384
|
+
*/
|
|
385
|
+
getSpanCount() {
|
|
386
|
+
return {
|
|
387
|
+
active: this.activeSpans.size,
|
|
388
|
+
completed: this.completedSpans.length,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Find completed spans by trace ID
|
|
393
|
+
*
|
|
394
|
+
* @param traceId - Trace ID to search for
|
|
395
|
+
* @returns Array of matching spans
|
|
396
|
+
*/
|
|
397
|
+
findSpansByTraceId(traceId) {
|
|
398
|
+
return this.completedSpans.filter((span) => span.traceId === traceId);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Build trace tree from completed spans
|
|
402
|
+
*
|
|
403
|
+
* Groups spans by trace ID and builds parent-child relationships.
|
|
404
|
+
*
|
|
405
|
+
* @param traceId - Trace ID to build tree for
|
|
406
|
+
* @returns Tree structure
|
|
407
|
+
*/
|
|
408
|
+
buildTraceTree(traceId) {
|
|
409
|
+
const spans = this.findSpansByTraceId(traceId);
|
|
410
|
+
if (spans.length === 0)
|
|
411
|
+
return null;
|
|
412
|
+
// Find root span (no parent)
|
|
413
|
+
const root = spans.find((s) => !s.parentSpanId);
|
|
414
|
+
if (!root)
|
|
415
|
+
return null;
|
|
416
|
+
// Build tree recursively
|
|
417
|
+
return buildSpanTreeNode(root, spans);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
exports.SpanRecorder = SpanRecorder;
|
|
421
|
+
/**
|
|
422
|
+
* Build span tree node recursively
|
|
423
|
+
*
|
|
424
|
+
* @param span - Current span
|
|
425
|
+
* @param allSpans - All spans in trace
|
|
426
|
+
* @returns Tree node
|
|
427
|
+
*/
|
|
428
|
+
function buildSpanTreeNode(span, allSpans) {
|
|
429
|
+
const children = allSpans
|
|
430
|
+
.filter((s) => s.parentSpanId === span.spanId)
|
|
431
|
+
.map((child) => buildSpanTreeNode(child, allSpans));
|
|
432
|
+
return { span, children };
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Helper function to measure async operation with automatic span tracking
|
|
436
|
+
*
|
|
437
|
+
* @param recorder - Span recorder
|
|
438
|
+
* @param name - Span name
|
|
439
|
+
* @param fn - Async function to measure
|
|
440
|
+
* @param options - Span options
|
|
441
|
+
* @returns Result of async function
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* ```typescript
|
|
445
|
+
* const result = await measureAsync(recorder, 'database-query', async () => {
|
|
446
|
+
* return await db.query('SELECT * FROM users');
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
async function measureAsync(recorder, name, fn, options) {
|
|
451
|
+
const span = recorder.startSpan(name, options);
|
|
452
|
+
try {
|
|
453
|
+
const result = await fn();
|
|
454
|
+
span.setStatus(SpanStatusCode.OK);
|
|
455
|
+
return result;
|
|
456
|
+
}
|
|
457
|
+
catch (error) {
|
|
458
|
+
span.recordException(error);
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
finally {
|
|
462
|
+
span.end();
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Helper function to measure sync operation with automatic span tracking
|
|
467
|
+
*
|
|
468
|
+
* @param recorder - Span recorder
|
|
469
|
+
* @param name - Span name
|
|
470
|
+
* @param fn - Sync function to measure
|
|
471
|
+
* @param options - Span options
|
|
472
|
+
* @returns Result of sync function
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* const result = measureSync(recorder, 'encryption', () => {
|
|
477
|
+
* return encrypt(data, key);
|
|
478
|
+
* });
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
function measureSync(recorder, name, fn, options) {
|
|
482
|
+
const span = recorder.startSpan(name, options);
|
|
483
|
+
try {
|
|
484
|
+
const result = fn();
|
|
485
|
+
span.setStatus(SpanStatusCode.OK);
|
|
486
|
+
return result;
|
|
487
|
+
}
|
|
488
|
+
catch (error) {
|
|
489
|
+
span.recordException(error);
|
|
490
|
+
throw error;
|
|
491
|
+
}
|
|
492
|
+
finally {
|
|
493
|
+
span.end();
|
|
494
|
+
}
|
|
495
|
+
}
|
|
@@ -1 +1,63 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpsTransportAdapter = void 0;
|
|
4
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* HTTPS transport adapter — sends envelopes as JSON POST requests.
|
|
7
|
+
*
|
|
8
|
+
* Recipient URL resolved as: baseUrl/deliver/{recipientDid}
|
|
9
|
+
* This is the SDK's default transport for server-to-server delivery.
|
|
10
|
+
*/
|
|
11
|
+
class HttpsTransportAdapter {
|
|
12
|
+
baseUrl;
|
|
13
|
+
timeoutMs;
|
|
14
|
+
fetchFn;
|
|
15
|
+
handlers = [];
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
this.baseUrl = opts.baseUrl.replace(/\/$/, '');
|
|
18
|
+
this.timeoutMs = opts.timeoutMs ?? 10_000;
|
|
19
|
+
this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
20
|
+
}
|
|
21
|
+
async send(envelope, recipientDid) {
|
|
22
|
+
const url = `${this.baseUrl}/deliver/${encodeURIComponent(recipientDid)}`;
|
|
23
|
+
try {
|
|
24
|
+
const controller = new AbortController();
|
|
25
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
26
|
+
const response = await this.fetchFn(url, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: { 'Content-Type': 'application/json' },
|
|
29
|
+
body: JSON.stringify(envelope),
|
|
30
|
+
signal: controller.signal,
|
|
31
|
+
});
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
return (0, shared_1.err)(response.status === 404
|
|
35
|
+
? 'RECIPIENT_UNREACHABLE'
|
|
36
|
+
: 'SEND_FAILED');
|
|
37
|
+
}
|
|
38
|
+
return (0, shared_1.ok)(undefined);
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
if (e instanceof DOMException && e.name === 'AbortError') {
|
|
42
|
+
return (0, shared_1.err)('TIMEOUT');
|
|
43
|
+
}
|
|
44
|
+
return (0, shared_1.err)('NETWORK_ERROR');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
onReceive(handler) {
|
|
48
|
+
this.handlers.push(handler);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Dispatch a received envelope to all handlers.
|
|
52
|
+
* Called by the server when an incoming POST is received.
|
|
53
|
+
*/
|
|
54
|
+
dispatch(envelope) {
|
|
55
|
+
for (const handler of this.handlers) {
|
|
56
|
+
handler(envelope);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
dispose() {
|
|
60
|
+
this.handlers = [];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.HttpsTransportAdapter = HttpsTransportAdapter;
|