@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.
Files changed (210) hide show
  1. package/README.md +55 -14
  2. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
  3. package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
  4. package/dist-standalone/_deps/shared/cjs/index.js +463 -1
  5. package/dist-standalone/_deps/shared/cjs/types.js +315 -1
  6. package/dist-standalone/_deps/shared/errors.js +244 -1
  7. package/dist-standalone/_deps/shared/index.js +72 -1
  8. package/dist-standalone/_deps/shared/types.js +86 -1
  9. package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
  10. package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
  11. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
  12. package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
  13. package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
  14. package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
  15. package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
  16. package/dist-standalone/_deps/ux-helpers/index.js +1 -1
  17. package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
  18. package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
  19. package/dist-standalone/_deps/ux-helpers/search.js +1 -1
  20. package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
  21. package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
  22. package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
  23. package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
  24. package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
  25. package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
  26. package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
  27. package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
  28. package/dist-standalone/_deps/xchange/errors.js +1 -1
  29. package/dist-standalone/_deps/xchange/index.js +1 -1
  30. package/dist-standalone/_deps/xchange/invite-client.js +1 -1
  31. package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
  32. package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
  33. package/dist-standalone/_deps/xchange/xchange.js +1 -1
  34. package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
  35. package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
  36. package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
  37. package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
  38. package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
  39. package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
  40. package/dist-standalone/_deps/xregistry/discovery.js +1 -1
  41. package/dist-standalone/_deps/xregistry/errors.js +1 -1
  42. package/dist-standalone/_deps/xregistry/index.js +1 -1
  43. package/dist-standalone/_deps/xregistry/registry.js +1 -1
  44. package/dist-standalone/_deps/xregistry/schema.js +1 -1
  45. package/dist-standalone/_deps/xregistry/types.js +1 -1
  46. package/dist-standalone/agent-call.js +659 -1
  47. package/dist-standalone/agent-sdk.js +328 -1
  48. package/dist-standalone/agent.js +1800 -1
  49. package/dist-standalone/approval.js +193 -1
  50. package/dist-standalone/async-iterators.js +382 -1
  51. package/dist-standalone/auth.js +219 -1
  52. package/dist-standalone/auto-accept.js +229 -1
  53. package/dist-standalone/backup-config.js +201 -1
  54. package/dist-standalone/backup.js +326 -1
  55. package/dist-standalone/batch-operations.js +388 -1
  56. package/dist-standalone/cancellation.js +477 -1
  57. package/dist-standalone/checkpoint.js +186 -1
  58. package/dist-standalone/circuit-breaker.js +468 -1
  59. package/dist-standalone/cjs/agent-call.js +701 -1
  60. package/dist-standalone/cjs/agent-sdk.js +332 -1
  61. package/dist-standalone/cjs/agent.js +1837 -1
  62. package/dist-standalone/cjs/approval.js +199 -1
  63. package/dist-standalone/cjs/async-iterators.js +392 -1
  64. package/dist-standalone/cjs/auth.js +225 -1
  65. package/dist-standalone/cjs/auto-accept.js +233 -1
  66. package/dist-standalone/cjs/backup-config.js +207 -1
  67. package/dist-standalone/cjs/backup.js +330 -1
  68. package/dist-standalone/cjs/batch-operations.js +397 -1
  69. package/dist-standalone/cjs/cancellation.js +490 -1
  70. package/dist-standalone/cjs/checkpoint.js +193 -1
  71. package/dist-standalone/cjs/circuit-breaker.js +476 -1
  72. package/dist-standalone/cjs/cli/init.js +492 -1
  73. package/dist-standalone/cjs/config-validation.js +522 -1
  74. package/dist-standalone/cjs/connect.js +312 -1
  75. package/dist-standalone/cjs/connection-pool.js +506 -1
  76. package/dist-standalone/cjs/correlation-id.js +339 -1
  77. package/dist-standalone/cjs/crypto-utils.js +176 -1
  78. package/dist-standalone/cjs/debug-mode.js +534 -1
  79. package/dist-standalone/cjs/did-document.js +101 -1
  80. package/dist-standalone/cjs/did-privateme.js +130 -1
  81. package/dist-standalone/cjs/did-web.js +201 -1
  82. package/dist-standalone/cjs/discovery.js +462 -1
  83. package/dist-standalone/cjs/dual-mode.js +251 -1
  84. package/dist-standalone/cjs/email-templates.js +313 -1
  85. package/dist-standalone/cjs/email-transport.js +239 -1
  86. package/dist-standalone/cjs/envelope.js +538 -1
  87. package/dist-standalone/cjs/errors.js +913 -1
  88. package/dist-standalone/cjs/event-emitter.js +461 -1
  89. package/dist-standalone/cjs/gateway-state.js +55 -1
  90. package/dist-standalone/cjs/gateway-transport.js +120 -1
  91. package/dist-standalone/cjs/graceful-degradation.js +403 -1
  92. package/dist-standalone/cjs/guardrails.js +223 -1
  93. package/dist-standalone/cjs/health-check.js +336 -1
  94. package/dist-standalone/cjs/http-compat.js +272 -1
  95. package/dist-standalone/cjs/http-status-map.js +571 -1
  96. package/dist-standalone/cjs/identity.js +645 -1
  97. package/dist-standalone/cjs/index.js +406 -1
  98. package/dist-standalone/cjs/invitation.js +421 -1
  99. package/dist-standalone/cjs/invite.js +328 -1
  100. package/dist-standalone/cjs/key-agreement.js +335 -1
  101. package/dist-standalone/cjs/lazy-init.js +300 -1
  102. package/dist-standalone/cjs/logger.js +291 -1
  103. package/dist-standalone/cjs/mdns-discovery.js +202 -1
  104. package/dist-standalone/cjs/nonce-store.js +80 -1
  105. package/dist-standalone/cjs/pairing-manager.js +223 -1
  106. package/dist-standalone/cjs/plugin-system.js +264 -1
  107. package/dist-standalone/cjs/plugins/logging.js +168 -1
  108. package/dist-standalone/cjs/plugins/metrics.js +181 -1
  109. package/dist-standalone/cjs/plugins/validation.js +302 -1
  110. package/dist-standalone/cjs/policy.js +320 -1
  111. package/dist-standalone/cjs/progress-callbacks.js +583 -1
  112. package/dist-standalone/cjs/redis-nonce-store.js +76 -1
  113. package/dist-standalone/cjs/registry-middleware.js +50 -1
  114. package/dist-standalone/cjs/retry-strategies.js +544 -1
  115. package/dist-standalone/cjs/retry-transport.js +102 -1
  116. package/dist-standalone/cjs/runtime/browser.js +533 -1
  117. package/dist-standalone/cjs/runtime/edge.js +526 -1
  118. package/dist-standalone/cjs/runtime/react-native.js +394 -1
  119. package/dist-standalone/cjs/security-policy.js +245 -1
  120. package/dist-standalone/cjs/serialization.js +1040 -1
  121. package/dist-standalone/cjs/split-channel.js +225 -1
  122. package/dist-standalone/cjs/subscription-proof.js +230 -1
  123. package/dist-standalone/cjs/succession.js +148 -1
  124. package/dist-standalone/cjs/timeouts.js +412 -1
  125. package/dist-standalone/cjs/trace-context.js +424 -1
  126. package/dist-standalone/cjs/trace-spans.js +495 -1
  127. package/dist-standalone/cjs/transport.js +63 -1
  128. package/dist-standalone/cjs/trust-registry.js +991 -1
  129. package/dist-standalone/cjs/types/error-response.js +56 -1
  130. package/dist-standalone/cjs/vault-auth.js +178 -1
  131. package/dist-standalone/cjs/vault-store-loader.js +194 -1
  132. package/dist-standalone/cjs/verify.js +25 -1
  133. package/dist-standalone/cjs/version-info.js +543 -1
  134. package/dist-standalone/cjs/xfetch.js +340 -1
  135. package/dist-standalone/cli/init.js +455 -1
  136. package/dist-standalone/cli/setup.js +514 -1
  137. package/dist-standalone/cli/types.js +27 -1
  138. package/dist-standalone/cli/xbind.js +148 -1
  139. package/dist-standalone/config-validation.js +513 -1
  140. package/dist-standalone/connect.js +274 -1
  141. package/dist-standalone/connection-pool.js +500 -1
  142. package/dist-standalone/correlation-id.js +326 -1
  143. package/dist-standalone/crypto-utils.js +157 -1
  144. package/dist-standalone/debug-mode.js +510 -1
  145. package/dist-standalone/did-document.js +96 -1
  146. package/dist-standalone/did-privateme.js +121 -1
  147. package/dist-standalone/did-web.js +196 -1
  148. package/dist-standalone/discovery.js +458 -1
  149. package/dist-standalone/dual-mode.js +247 -1
  150. package/dist-standalone/email-templates.js +309 -1
  151. package/dist-standalone/email-transport.js +232 -1
  152. package/dist-standalone/envelope.js +525 -1
  153. package/dist-standalone/errors.js +896 -1
  154. package/dist-standalone/event-emitter.js +456 -1
  155. package/dist-standalone/gateway-state.js +51 -1
  156. package/dist-standalone/gateway-transport.js +116 -1
  157. package/dist-standalone/graceful-degradation.js +396 -1
  158. package/dist-standalone/guardrails.js +216 -1
  159. package/dist-standalone/health-check.js +332 -1
  160. package/dist-standalone/http-compat.js +267 -1
  161. package/dist-standalone/http-status-map.js +561 -1
  162. package/dist-standalone/identity.js +619 -1
  163. package/dist-standalone/index.js +78 -1
  164. package/dist-standalone/invitation.js +415 -1
  165. package/dist-standalone/invite.js +324 -1
  166. package/dist-standalone/key-agreement.js +325 -1
  167. package/dist-standalone/lazy-init.js +295 -1
  168. package/dist-standalone/logger.js +285 -1
  169. package/dist-standalone/mdns-discovery.js +195 -1
  170. package/dist-standalone/nonce-store.js +76 -1
  171. package/dist-standalone/pairing-manager.js +219 -1
  172. package/dist-standalone/plugin-system.js +257 -1
  173. package/dist-standalone/plugins/logging.js +163 -1
  174. package/dist-standalone/plugins/metrics.js +176 -1
  175. package/dist-standalone/plugins/validation.js +297 -1
  176. package/dist-standalone/policy.js +315 -1
  177. package/dist-standalone/progress-callbacks.js +576 -1
  178. package/dist-standalone/redis-nonce-store.js +72 -1
  179. package/dist-standalone/registry-middleware.js +47 -1
  180. package/dist-standalone/retry-strategies.js +534 -1
  181. package/dist-standalone/retry-transport.js +98 -1
  182. package/dist-standalone/runtime/browser.js +516 -1
  183. package/dist-standalone/runtime/edge.js +511 -1
  184. package/dist-standalone/runtime/react-native.js +383 -1
  185. package/dist-standalone/security-policy.js +239 -1
  186. package/dist-standalone/serialization.js +1031 -1
  187. package/dist-standalone/split-channel.js +219 -1
  188. package/dist-standalone/subscription-proof.js +224 -1
  189. package/dist-standalone/succession.js +142 -1
  190. package/dist-standalone/timeouts.js +398 -1
  191. package/dist-standalone/trace-context.js +414 -1
  192. package/dist-standalone/trace-spans.js +488 -1
  193. package/dist-standalone/transport.js +59 -1
  194. package/dist-standalone/trust-registry.js +950 -1
  195. package/dist-standalone/types/error-response.js +52 -1
  196. package/dist-standalone/vault-auth.js +174 -1
  197. package/dist-standalone/vault-store-loader.js +187 -1
  198. package/dist-standalone/verify.js +16 -1
  199. package/dist-standalone/version-info.js +530 -1
  200. package/dist-standalone/xfetch.js +335 -1
  201. package/package.json +4 -13
  202. package/share1.dat +0 -0
  203. package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
  204. package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
  205. package/dist-standalone/_deps/shared/cjs/package.json +0 -1
  206. package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
  207. package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
  208. package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
  209. package/dist-standalone/cjs/package.json +0 -3
  210. package/dist-standalone/package.json +0 -10
@@ -1 +1,495 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SpanRecorder=exports.TraceSpan=exports.SpanKind=exports.SpanStatusCode=void 0,exports.measureAsync=measureAsync,exports.measureSync=measureSync;const trace_context_js_1=require("./trace-context.js");var SpanStatusCode,SpanKind;!function(t){t[t.OK=0]="OK",t[t.ERROR=1]="ERROR",t[t.UNSET=2]="UNSET"}(SpanStatusCode||(exports.SpanStatusCode=SpanStatusCode={})),function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"}(SpanKind||(exports.SpanKind=SpanKind={}));class TraceSpan{spanId;traceContext;name;kind;parentSpanId;startTime;endTime;status;attributes;events;ended;constructor(t,e,n={}){this.spanId=(0,trace_context_js_1.generateSpanId)(),this.traceContext=e,this.name=t,this.kind=n.kind??SpanKind.INTERNAL,this.parentSpanId=n.parentSpanId,this.startTime=Date.now(),this.status={code:SpanStatusCode.UNSET},this.attributes=n.attributes??{},this.events=[],this.ended=!1}setAttribute(t,e){if(this.ended)throw new Error("Cannot set attribute on ended span");return this.attributes[t]=e,this}setAttributes(t){if(this.ended)throw new Error("Cannot set attributes on ended span");return Object.assign(this.attributes,t),this}addEvent(t,e){if(this.ended)throw new Error("Cannot add event to ended span");return this.events.push({name:t,timestamp:Date.now(),attributes:e}),this}setStatus(t,e){if(this.ended)throw new Error("Cannot set status on ended span");return this.status={code:t,message:e},this}recordException(t){if(this.ended)throw new Error("Cannot record exception on ended span");this.setStatus(SpanStatusCode.ERROR);const e={};return t instanceof Error?(e["exception.type"]=t.name,e["exception.message"]=t.message,t.stack&&(e["exception.stacktrace"]=t.stack)):e["exception.message"]=String(t),this.addEvent("exception",e),this}end(t){this.ended||(this.endTime=t??Date.now(),this.ended=!0,this.status.code===SpanStatusCode.UNSET&&(this.status={code:SpanStatusCode.OK}))}isEnded(){return this.ended}getDuration(){return this.ended&&this.endTime?this.endTime-this.startTime:null}toData(){if(!this.ended||!this.endTime)throw new Error("Cannot export data from active span. Call end() first.");return{spanId:this.spanId,traceId:this.traceContext.traceId,parentSpanId:this.parentSpanId,name:this.name,kind:this.kind,startTime:this.startTime,endTime:this.endTime,duration:this.endTime-this.startTime,status:{...this.status},attributes:{...this.attributes},events:this.events.map(t=>({name:t.name,timestamp:t.timestamp,attributes:t.attributes?{...t.attributes}:void 0}))}}createChild(t,e={}){return new TraceSpan(t,this.traceContext,{...e,parentSpanId:this.spanId})}}exports.TraceSpan=TraceSpan;class SpanRecorder{activeSpans;completedSpans;maxSpans;rootContext;constructor(t={}){this.activeSpans=new Map,this.completedSpans=[],this.maxSpans=t.maxSpans??1e3,this.rootContext=t.traceContext}startSpan(t,e={}){const n=e.traceContext??e.parentSpan?.traceContext??this.rootContext??trace_context_js_1.TraceContext.create(),s=new TraceSpan(t,n,{kind:e.kind,attributes:e.attributes,parentSpanId:e.parentSpan?.spanId});this.activeSpans.set(s.spanId,s);const a=setTimeout(()=>{s.isEnded()||(s.setStatus(SpanStatusCode.ERROR,"Span not ended (auto-ended by recorder)"),this.endSpan(s))},6e4),r=s.end.bind(s);return s.end=t=>{clearTimeout(a),r(t),this.endSpan(s)},s}endSpan(t){t.isEnded()||t.end(),this.activeSpans.delete(t.spanId);const e=t.toData();this.completedSpans.push(e),this.completedSpans.length>this.maxSpans&&this.completedSpans.shift()}getCompletedSpans(){return[...this.completedSpans]}getActiveSpans(){return Array.from(this.activeSpans.values())}clearCompletedSpans(){this.completedSpans=[]}getSpanCount(){return{active:this.activeSpans.size,completed:this.completedSpans.length}}findSpansByTraceId(t){return this.completedSpans.filter(e=>e.traceId===t)}buildTraceTree(t){const e=this.findSpansByTraceId(t);if(0===e.length)return null;const n=e.find(t=>!t.parentSpanId);return n?buildSpanTreeNode(n,e):null}}function buildSpanTreeNode(t,e){const n=e.filter(e=>e.parentSpanId===t.spanId).map(t=>buildSpanTreeNode(t,e));return{span:t,children:n}}async function measureAsync(t,e,n,s){const a=t.startSpan(e,s);try{const t=await n();return a.setStatus(SpanStatusCode.OK),t}catch(t){throw a.recordException(t),t}finally{a.end()}}function measureSync(t,e,n,s){const a=t.startSpan(e,s);try{const t=n();return a.setStatus(SpanStatusCode.OK),t}catch(t){throw a.recordException(t),t}finally{a.end()}}exports.SpanRecorder=SpanRecorder;
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";Object.defineProperty(exports,"__esModule",{value:!0}),exports.HttpsTransportAdapter=void 0;const shared_1=require("../_deps/shared/index.js");class HttpsTransportAdapter{baseUrl;timeoutMs;fetchFn;handlers=[];constructor(e){this.baseUrl=e.baseUrl.replace(/\/$/,""),this.timeoutMs=e.timeoutMs??1e4,this.fetchFn=e.fetch??globalThis.fetch.bind(globalThis)}async send(e,t){const s=`${this.baseUrl}/deliver/${encodeURIComponent(t)}`;try{const t=new AbortController,r=setTimeout(()=>t.abort(),this.timeoutMs),o=await this.fetchFn(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),signal:t.signal});return clearTimeout(r),o.ok?(0,shared_1.ok)(void 0):(0,shared_1.err)(404===o.status?"RECIPIENT_UNREACHABLE":"SEND_FAILED")}catch(e){return e instanceof DOMException&&"AbortError"===e.name?(0,shared_1.err)("TIMEOUT"):(0,shared_1.err)("NETWORK_ERROR")}}onReceive(e){this.handlers.push(e)}dispatch(e){for(const t of this.handlers)t(e)}dispose(){this.handlers=[]}}exports.HttpsTransportAdapter=HttpsTransportAdapter;
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;