@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,424 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module trace-context
|
|
4
|
+
* W3C Trace Context implementation for distributed tracing
|
|
5
|
+
*
|
|
6
|
+
* Implements the W3C Trace Context specification for distributed tracing across
|
|
7
|
+
* xBind agent operations, enabling correlation of requests across service boundaries.
|
|
8
|
+
*
|
|
9
|
+
* Standard: https://www.w3.org/TR/trace-context/
|
|
10
|
+
*
|
|
11
|
+
* Headers:
|
|
12
|
+
* - `traceparent`: Required header with version, trace-id, parent-id, and trace-flags
|
|
13
|
+
* - `tracestate`: Optional vendor-specific state
|
|
14
|
+
*
|
|
15
|
+
* Format: `{version}-{trace-id}-{parent-id}-{trace-flags}`
|
|
16
|
+
* Example: `00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01`
|
|
17
|
+
*
|
|
18
|
+
* Usage:
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { TraceContext } from '@private.me/xbind';
|
|
21
|
+
*
|
|
22
|
+
* // Create a new trace context
|
|
23
|
+
* const ctx = TraceContext.create();
|
|
24
|
+
*
|
|
25
|
+
* // Attach to request headers
|
|
26
|
+
* const headers = new Headers();
|
|
27
|
+
* ctx.inject(headers);
|
|
28
|
+
*
|
|
29
|
+
* // Extract from incoming headers
|
|
30
|
+
* const incomingCtx = TraceContext.extract(headers);
|
|
31
|
+
*
|
|
32
|
+
* // Create child span
|
|
33
|
+
* const childCtx = incomingCtx.createChild();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.TraceContext = exports.TraceFlags = exports.TRACE_VERSION = exports.TRACESTATE_HEADER = exports.TRACEPARENT_HEADER = void 0;
|
|
38
|
+
exports.generateTraceId = generateTraceId;
|
|
39
|
+
exports.generateSpanId = generateSpanId;
|
|
40
|
+
exports.validateTraceId = validateTraceId;
|
|
41
|
+
exports.validateSpanId = validateSpanId;
|
|
42
|
+
exports.parseTraceparent = parseTraceparent;
|
|
43
|
+
exports.parseTracestate = parseTracestate;
|
|
44
|
+
/**
|
|
45
|
+
* W3C Trace Context specification constants
|
|
46
|
+
*/
|
|
47
|
+
exports.TRACEPARENT_HEADER = 'traceparent';
|
|
48
|
+
exports.TRACESTATE_HEADER = 'tracestate';
|
|
49
|
+
/**
|
|
50
|
+
* Trace context version (currently only 00 is supported)
|
|
51
|
+
*/
|
|
52
|
+
exports.TRACE_VERSION = '00';
|
|
53
|
+
/**
|
|
54
|
+
* Trace flags
|
|
55
|
+
*/
|
|
56
|
+
var TraceFlags;
|
|
57
|
+
(function (TraceFlags) {
|
|
58
|
+
/** Not sampled */
|
|
59
|
+
TraceFlags[TraceFlags["NONE"] = 0] = "NONE";
|
|
60
|
+
/** Sampled */
|
|
61
|
+
TraceFlags[TraceFlags["SAMPLED"] = 1] = "SAMPLED";
|
|
62
|
+
})(TraceFlags || (exports.TraceFlags = TraceFlags = {}));
|
|
63
|
+
/**
|
|
64
|
+
* W3C Trace Context implementation
|
|
65
|
+
*
|
|
66
|
+
* Represents a single point in a distributed trace with parent-child relationships.
|
|
67
|
+
*/
|
|
68
|
+
class TraceContext {
|
|
69
|
+
/** W3C version (always '00' for now) */
|
|
70
|
+
version;
|
|
71
|
+
/** Trace ID (32 hex characters, 16 bytes) */
|
|
72
|
+
traceId;
|
|
73
|
+
/** Parent span ID (16 hex characters, 8 bytes) */
|
|
74
|
+
parentId;
|
|
75
|
+
/** Trace flags (8-bit field) */
|
|
76
|
+
traceFlags;
|
|
77
|
+
/** Vendor-specific trace state */
|
|
78
|
+
traceState;
|
|
79
|
+
/**
|
|
80
|
+
* Create a trace context
|
|
81
|
+
*
|
|
82
|
+
* @param traceId - 32-character hex trace ID
|
|
83
|
+
* @param parentId - 16-character hex parent span ID
|
|
84
|
+
* @param traceFlags - 8-bit trace flags
|
|
85
|
+
* @param traceState - Optional vendor-specific state
|
|
86
|
+
*/
|
|
87
|
+
constructor(traceId, parentId, traceFlags = TraceFlags.SAMPLED, traceState = []) {
|
|
88
|
+
if (!validateTraceId(traceId)) {
|
|
89
|
+
throw new Error(`Invalid trace ID: ${traceId}. Must be 32 hex characters.`);
|
|
90
|
+
}
|
|
91
|
+
if (!validateSpanId(parentId)) {
|
|
92
|
+
throw new Error(`Invalid parent ID: ${parentId}. Must be 16 hex characters.`);
|
|
93
|
+
}
|
|
94
|
+
if (traceFlags < 0 || traceFlags > 255) {
|
|
95
|
+
throw new Error(`Invalid trace flags: ${traceFlags}. Must be 0-255.`);
|
|
96
|
+
}
|
|
97
|
+
this.version = exports.TRACE_VERSION;
|
|
98
|
+
this.traceId = traceId;
|
|
99
|
+
this.parentId = parentId;
|
|
100
|
+
this.traceFlags = traceFlags;
|
|
101
|
+
this.traceState = traceState;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Create a new root trace context
|
|
105
|
+
*
|
|
106
|
+
* Generates a new trace ID and parent ID for a new trace.
|
|
107
|
+
*
|
|
108
|
+
* @param sampled - Whether this trace should be sampled
|
|
109
|
+
* @returns New trace context
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const ctx = TraceContext.create();
|
|
114
|
+
* console.log(ctx.traceId); // => "4bf92f3577b34da6a3ce929d0e0e4736"
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
static create(sampled = true) {
|
|
118
|
+
const traceId = generateTraceId();
|
|
119
|
+
const parentId = generateSpanId();
|
|
120
|
+
const flags = sampled ? TraceFlags.SAMPLED : TraceFlags.NONE;
|
|
121
|
+
return new TraceContext(traceId, parentId, flags);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Extract trace context from headers
|
|
125
|
+
*
|
|
126
|
+
* Parses the traceparent and tracestate headers according to W3C spec.
|
|
127
|
+
*
|
|
128
|
+
* @param headers - Headers object or plain object
|
|
129
|
+
* @returns Trace context if found, null otherwise
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const headers = new Headers({
|
|
134
|
+
* traceparent: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
|
|
135
|
+
* });
|
|
136
|
+
* const ctx = TraceContext.extract(headers);
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
static extract(headers) {
|
|
140
|
+
const traceparent = headers instanceof Headers
|
|
141
|
+
? headers.get(exports.TRACEPARENT_HEADER)
|
|
142
|
+
: headers[exports.TRACEPARENT_HEADER];
|
|
143
|
+
if (!traceparent)
|
|
144
|
+
return null;
|
|
145
|
+
const spec = parseTraceparent(traceparent);
|
|
146
|
+
if (!spec)
|
|
147
|
+
return null;
|
|
148
|
+
// Extract tracestate
|
|
149
|
+
const tracestateValue = headers instanceof Headers
|
|
150
|
+
? headers.get(exports.TRACESTATE_HEADER)
|
|
151
|
+
: headers[exports.TRACESTATE_HEADER];
|
|
152
|
+
const traceState = tracestateValue ? parseTracestate(tracestateValue) : [];
|
|
153
|
+
return new TraceContext(spec.traceId, spec.parentId, spec.traceFlags, traceState);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Inject trace context into headers
|
|
157
|
+
*
|
|
158
|
+
* Adds traceparent and tracestate headers to the provided headers object.
|
|
159
|
+
*
|
|
160
|
+
* @param headers - Headers to modify
|
|
161
|
+
* @returns Modified headers
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const ctx = TraceContext.create();
|
|
166
|
+
* const headers = new Headers();
|
|
167
|
+
* ctx.inject(headers);
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
inject(headers) {
|
|
171
|
+
const traceparent = this.toTraceparent();
|
|
172
|
+
if (headers instanceof Headers) {
|
|
173
|
+
headers.set(exports.TRACEPARENT_HEADER, traceparent);
|
|
174
|
+
if (this.traceState.length > 0) {
|
|
175
|
+
headers.set(exports.TRACESTATE_HEADER, this.toTracestate());
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
headers[exports.TRACEPARENT_HEADER] = traceparent;
|
|
180
|
+
if (this.traceState.length > 0) {
|
|
181
|
+
headers[exports.TRACESTATE_HEADER] = this.toTracestate();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return headers;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Create a child trace context
|
|
188
|
+
*
|
|
189
|
+
* Generates a new parent ID (span ID) while keeping the same trace ID.
|
|
190
|
+
* This represents a new span in the same trace.
|
|
191
|
+
*
|
|
192
|
+
* @param sampled - Override sampling decision (defaults to parent's decision)
|
|
193
|
+
* @returns New child trace context
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* const parent = TraceContext.create();
|
|
198
|
+
* const child = parent.createChild();
|
|
199
|
+
* console.log(parent.traceId === child.traceId); // => true
|
|
200
|
+
* console.log(parent.parentId === child.parentId); // => false
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
createChild(sampled) {
|
|
204
|
+
const newParentId = generateSpanId();
|
|
205
|
+
const flags = sampled !== undefined
|
|
206
|
+
? (sampled ? TraceFlags.SAMPLED : TraceFlags.NONE)
|
|
207
|
+
: this.traceFlags;
|
|
208
|
+
return new TraceContext(this.traceId, newParentId, flags, this.traceState);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Format as traceparent header value
|
|
212
|
+
*
|
|
213
|
+
* @returns traceparent string
|
|
214
|
+
*/
|
|
215
|
+
toTraceparent() {
|
|
216
|
+
const flags = this.traceFlags.toString(16).padStart(2, '0');
|
|
217
|
+
return `${this.version}-${this.traceId}-${this.parentId}-${flags}`;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Format as tracestate header value
|
|
221
|
+
*
|
|
222
|
+
* @returns tracestate string
|
|
223
|
+
*/
|
|
224
|
+
toTracestate() {
|
|
225
|
+
return this.traceState
|
|
226
|
+
.map(({ key, value }) => `${key}=${value}`)
|
|
227
|
+
.join(',');
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Check if this trace is sampled
|
|
231
|
+
*
|
|
232
|
+
* @returns True if sampled
|
|
233
|
+
*/
|
|
234
|
+
isSampled() {
|
|
235
|
+
return (this.traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Add or update tracestate entry
|
|
239
|
+
*
|
|
240
|
+
* @param key - Vendor key (format: vendor[@system])
|
|
241
|
+
* @param value - State value
|
|
242
|
+
* @returns New trace context with updated state
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* const ctx = TraceContext.create();
|
|
247
|
+
* const updated = ctx.withTracestate('private.me', 'did:key:z6Mk...');
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
withTracestate(key, value) {
|
|
251
|
+
const newState = this.traceState.filter((e) => e.key !== key);
|
|
252
|
+
newState.unshift({ key, value });
|
|
253
|
+
return new TraceContext(this.traceId, this.parentId, this.traceFlags, newState);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Get tracestate value by key
|
|
257
|
+
*
|
|
258
|
+
* @param key - Vendor key
|
|
259
|
+
* @returns State value or undefined
|
|
260
|
+
*/
|
|
261
|
+
getTracestate(key) {
|
|
262
|
+
const entry = this.traceState.find((e) => e.key === key);
|
|
263
|
+
return entry?.value;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Convert to plain object
|
|
267
|
+
*
|
|
268
|
+
* @returns Plain object representation
|
|
269
|
+
*/
|
|
270
|
+
toObject() {
|
|
271
|
+
return {
|
|
272
|
+
version: this.version,
|
|
273
|
+
traceId: this.traceId,
|
|
274
|
+
parentId: this.parentId,
|
|
275
|
+
traceFlags: this.traceFlags,
|
|
276
|
+
traceState: [...this.traceState],
|
|
277
|
+
sampled: this.isSampled(),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
exports.TraceContext = TraceContext;
|
|
282
|
+
/**
|
|
283
|
+
* Generate a random trace ID (32 hex characters)
|
|
284
|
+
*
|
|
285
|
+
* @returns 32-character hex string
|
|
286
|
+
*/
|
|
287
|
+
function generateTraceId() {
|
|
288
|
+
return generateRandomHex(32);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Generate a random span ID (16 hex characters)
|
|
292
|
+
*
|
|
293
|
+
* @returns 16-character hex string
|
|
294
|
+
*/
|
|
295
|
+
function generateSpanId() {
|
|
296
|
+
return generateRandomHex(16);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Validate trace ID format
|
|
300
|
+
*
|
|
301
|
+
* @param traceId - Trace ID to validate
|
|
302
|
+
* @returns True if valid
|
|
303
|
+
*/
|
|
304
|
+
function validateTraceId(traceId) {
|
|
305
|
+
if (typeof traceId !== 'string')
|
|
306
|
+
return false;
|
|
307
|
+
if (traceId.length !== 32)
|
|
308
|
+
return false;
|
|
309
|
+
if (!/^[a-f0-9]{32}$/.test(traceId))
|
|
310
|
+
return false;
|
|
311
|
+
// Trace ID must not be all zeros
|
|
312
|
+
if (traceId === '00000000000000000000000000000000')
|
|
313
|
+
return false;
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Validate span ID format
|
|
318
|
+
*
|
|
319
|
+
* @param spanId - Span ID to validate
|
|
320
|
+
* @returns True if valid
|
|
321
|
+
*/
|
|
322
|
+
function validateSpanId(spanId) {
|
|
323
|
+
if (typeof spanId !== 'string')
|
|
324
|
+
return false;
|
|
325
|
+
if (spanId.length !== 16)
|
|
326
|
+
return false;
|
|
327
|
+
if (!/^[a-f0-9]{16}$/.test(spanId))
|
|
328
|
+
return false;
|
|
329
|
+
// Span ID must not be all zeros
|
|
330
|
+
if (spanId === '0000000000000000')
|
|
331
|
+
return false;
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Parse traceparent header value
|
|
336
|
+
*
|
|
337
|
+
* @param traceparent - Header value
|
|
338
|
+
* @returns Parsed components or null if invalid
|
|
339
|
+
*/
|
|
340
|
+
function parseTraceparent(traceparent) {
|
|
341
|
+
if (typeof traceparent !== 'string')
|
|
342
|
+
return null;
|
|
343
|
+
const parts = traceparent.split('-');
|
|
344
|
+
if (parts.length !== 4)
|
|
345
|
+
return null;
|
|
346
|
+
const [version, traceId, parentId, flagsStr] = parts;
|
|
347
|
+
// Version validation
|
|
348
|
+
if (version !== '00')
|
|
349
|
+
return null;
|
|
350
|
+
// Trace ID validation
|
|
351
|
+
if (!validateTraceId(traceId ?? ''))
|
|
352
|
+
return null;
|
|
353
|
+
// Parent ID validation
|
|
354
|
+
if (!validateSpanId(parentId ?? ''))
|
|
355
|
+
return null;
|
|
356
|
+
// Flags validation
|
|
357
|
+
if (!flagsStr || flagsStr.length !== 2)
|
|
358
|
+
return null;
|
|
359
|
+
if (!/^[a-f0-9]{2}$/.test(flagsStr))
|
|
360
|
+
return null;
|
|
361
|
+
const traceFlags = parseInt(flagsStr, 16);
|
|
362
|
+
return {
|
|
363
|
+
version: version ?? '00',
|
|
364
|
+
traceId: traceId ?? '',
|
|
365
|
+
parentId: parentId ?? '',
|
|
366
|
+
traceFlags,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Parse tracestate header value
|
|
371
|
+
*
|
|
372
|
+
* @param tracestate - Header value
|
|
373
|
+
* @returns Array of key-value pairs
|
|
374
|
+
*/
|
|
375
|
+
function parseTracestate(tracestate) {
|
|
376
|
+
if (!tracestate)
|
|
377
|
+
return [];
|
|
378
|
+
const entries = [];
|
|
379
|
+
const parts = tracestate.split(',');
|
|
380
|
+
for (const part of parts) {
|
|
381
|
+
const trimmed = part.trim();
|
|
382
|
+
const eqIndex = trimmed.indexOf('=');
|
|
383
|
+
if (eqIndex === -1)
|
|
384
|
+
continue;
|
|
385
|
+
const key = trimmed.substring(0, eqIndex).trim();
|
|
386
|
+
const value = trimmed.substring(eqIndex + 1).trim();
|
|
387
|
+
if (key && value) {
|
|
388
|
+
entries.push({ key, value });
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return entries;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Generate random hex string
|
|
395
|
+
*
|
|
396
|
+
* Uses Web Crypto API in browser, Node.js crypto in Node.
|
|
397
|
+
*
|
|
398
|
+
* @param length - Number of hex characters to generate
|
|
399
|
+
* @returns Random hex string
|
|
400
|
+
*
|
|
401
|
+
* @internal
|
|
402
|
+
*/
|
|
403
|
+
function generateRandomHex(length) {
|
|
404
|
+
const bytes = Math.ceil(length / 2);
|
|
405
|
+
// Try Web Crypto API (browser)
|
|
406
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
407
|
+
const buffer = new Uint8Array(bytes);
|
|
408
|
+
crypto.getRandomValues(buffer);
|
|
409
|
+
return Array.from(buffer)
|
|
410
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
411
|
+
.join('')
|
|
412
|
+
.substring(0, length);
|
|
413
|
+
}
|
|
414
|
+
// Try Node.js crypto
|
|
415
|
+
try {
|
|
416
|
+
const nodeCrypto = require('node:crypto');
|
|
417
|
+
return nodeCrypto.randomBytes(bytes).toString('hex').substring(0, length);
|
|
418
|
+
}
|
|
419
|
+
catch {
|
|
420
|
+
// SECURITY: Never fall back to Math.random() in production (OWASP violation)
|
|
421
|
+
throw new Error('Cryptographic random generation unavailable. ' +
|
|
422
|
+
'Install crypto polyfill or use environment with crypto support.');
|
|
423
|
+
}
|
|
424
|
+
}
|