@private.me/xbind 1.3.5 → 3.0.0
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/LICENSES.md +212 -0
- package/README.md +388 -6
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
- package/dist-standalone/_deps/shared/cjs/errors.js +1 -639
- package/dist-standalone/_deps/shared/cjs/index.js +1 -496
- package/dist-standalone/_deps/shared/cjs/types.js +1 -317
- package/dist-standalone/_deps/shared/errors.js +1 -255
- package/dist-standalone/_deps/shared/index.js +1 -74
- package/dist-standalone/_deps/shared/types.js +1 -90
- 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 +1 -642
- package/dist-standalone/agent-sdk.js +1 -328
- package/dist-standalone/agent.d.ts +95 -5
- package/dist-standalone/agent.js +1 -1545
- package/dist-standalone/approval.js +1 -193
- package/dist-standalone/async-iterators.d.ts +275 -0
- package/dist-standalone/async-iterators.js +1 -0
- package/dist-standalone/auth.js +1 -219
- package/dist-standalone/auto-accept.js +1 -229
- package/dist-standalone/backup-config.js +1 -201
- package/dist-standalone/backup.d.ts +114 -0
- package/dist-standalone/backup.js +1 -0
- package/dist-standalone/batch-operations.d.ts +297 -0
- package/dist-standalone/batch-operations.js +1 -0
- package/dist-standalone/cancellation.d.ts +301 -0
- package/dist-standalone/cancellation.js +1 -0
- package/dist-standalone/checkpoint.js +1 -186
- package/dist-standalone/circuit-breaker.d.ts +351 -0
- package/dist-standalone/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/agent-call.js +1 -651
- package/dist-standalone/cjs/agent-sdk.js +1 -332
- package/dist-standalone/cjs/agent.js +1 -1582
- package/dist-standalone/cjs/approval.js +1 -199
- package/dist-standalone/cjs/async-iterators.js +1 -0
- package/dist-standalone/cjs/auth.js +1 -225
- package/dist-standalone/cjs/auto-accept.js +1 -233
- package/dist-standalone/cjs/backup-config.js +1 -207
- package/dist-standalone/cjs/backup.js +1 -0
- package/dist-standalone/cjs/batch-operations.js +1 -0
- package/dist-standalone/cjs/cancellation.js +1 -0
- package/dist-standalone/cjs/checkpoint.js +1 -193
- package/dist-standalone/cjs/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/cli/init.js +1 -486
- package/dist-standalone/cjs/config-validation.js +1 -0
- package/dist-standalone/cjs/connect.js +1 -312
- package/dist-standalone/cjs/connection-pool.js +1 -0
- package/dist-standalone/cjs/correlation-id.js +1 -339
- package/dist-standalone/cjs/crypto-utils.js +1 -0
- package/dist-standalone/cjs/debug-mode.js +1 -0
- package/dist-standalone/cjs/did-document.js +1 -101
- package/dist-standalone/cjs/did-privateme.js +1 -130
- package/dist-standalone/cjs/did-web.js +1 -201
- package/dist-standalone/cjs/discovery.js +1 -462
- package/dist-standalone/cjs/dual-mode.js +1 -251
- package/dist-standalone/cjs/email-templates.js +1 -313
- package/dist-standalone/cjs/email-transport.js +1 -239
- package/dist-standalone/cjs/envelope.js +1 -510
- package/dist-standalone/cjs/errors.js +1 -826
- package/dist-standalone/cjs/event-emitter.js +1 -0
- package/dist-standalone/cjs/gateway-state.js +1 -55
- package/dist-standalone/cjs/gateway-transport.js +1 -120
- package/dist-standalone/cjs/graceful-degradation.js +1 -0
- package/dist-standalone/cjs/guardrails.js +1 -223
- package/dist-standalone/cjs/health-check.js +1 -0
- package/dist-standalone/cjs/http-compat.js +1 -272
- package/dist-standalone/cjs/http-status-map.js +1 -571
- package/dist-standalone/cjs/identity.js +1 -540
- package/dist-standalone/cjs/index.js +1 -237
- package/dist-standalone/cjs/invitation.js +1 -421
- package/dist-standalone/cjs/invite.js +1 -328
- package/dist-standalone/cjs/key-agreement.js +1 -246
- package/dist-standalone/cjs/lazy-init.js +1 -300
- package/dist-standalone/cjs/logger.js +1 -0
- package/dist-standalone/cjs/mdns-discovery.js +1 -202
- package/dist-standalone/cjs/nonce-store.js +1 -66
- package/dist-standalone/cjs/pairing-manager.js +1 -223
- package/dist-standalone/cjs/plugin-system.js +1 -0
- package/dist-standalone/cjs/plugins/logging.js +1 -0
- package/dist-standalone/cjs/plugins/metrics.js +1 -0
- package/dist-standalone/cjs/plugins/validation.js +1 -0
- package/dist-standalone/cjs/policy.js +1 -320
- package/dist-standalone/cjs/progress-callbacks.js +1 -0
- package/dist-standalone/cjs/redis-nonce-store.js +1 -76
- package/dist-standalone/cjs/registry-middleware.js +1 -50
- package/dist-standalone/cjs/retry-strategies.js +1 -0
- package/dist-standalone/cjs/retry-transport.js +1 -102
- package/dist-standalone/cjs/runtime/browser.js +1 -0
- package/dist-standalone/cjs/runtime/edge.js +1 -0
- package/dist-standalone/cjs/runtime/react-native.js +1 -0
- package/dist-standalone/cjs/security-policy.js +1 -245
- package/dist-standalone/cjs/serialization.js +1 -0
- package/dist-standalone/cjs/split-channel.js +1 -177
- package/dist-standalone/cjs/subscription-proof.js +1 -230
- package/dist-standalone/cjs/succession.js +1 -148
- package/dist-standalone/cjs/timeouts.js +1 -0
- package/dist-standalone/cjs/trace-context.js +1 -0
- package/dist-standalone/cjs/trace-spans.js +1 -0
- package/dist-standalone/cjs/transport.js +1 -63
- package/dist-standalone/cjs/trust-registry.js +1 -742
- package/dist-standalone/cjs/types/error-response.js +1 -56
- package/dist-standalone/cjs/vault-auth.js +1 -0
- package/dist-standalone/cjs/vault-store-loader.js +1 -0
- package/dist-standalone/cjs/verify.js +1 -25
- package/dist-standalone/cjs/version-info.js +1 -0
- package/dist-standalone/cjs/xfetch.js +1 -252
- package/dist-standalone/cli/init.js +1 -449
- package/dist-standalone/cli/setup.js +1 -514
- package/dist-standalone/cli/types.js +1 -27
- package/dist-standalone/cli/xbind.js +1 -148
- package/dist-standalone/config-validation.d.ts +185 -0
- package/dist-standalone/config-validation.js +1 -0
- package/dist-standalone/connect.js +1 -274
- package/dist-standalone/connection-pool.d.ts +251 -0
- package/dist-standalone/connection-pool.js +1 -0
- package/dist-standalone/correlation-id.js +1 -326
- package/dist-standalone/crypto-utils.d.ts +60 -0
- package/dist-standalone/crypto-utils.js +1 -0
- package/dist-standalone/debug-mode.d.ts +286 -0
- package/dist-standalone/debug-mode.js +1 -0
- package/dist-standalone/did-document.js +1 -96
- package/dist-standalone/did-privateme.js +1 -121
- package/dist-standalone/did-web.js +1 -196
- package/dist-standalone/discovery.js +1 -458
- package/dist-standalone/dual-mode.js +1 -247
- package/dist-standalone/email-templates.js +1 -309
- package/dist-standalone/email-transport.js +1 -232
- package/dist-standalone/envelope.d.ts +29 -1
- package/dist-standalone/envelope.js +1 -497
- package/dist-standalone/errors.d.ts +10 -0
- package/dist-standalone/errors.js +1 -811
- package/dist-standalone/event-emitter.d.ts +395 -0
- package/dist-standalone/event-emitter.js +1 -0
- package/dist-standalone/gateway-state.js +1 -51
- package/dist-standalone/gateway-transport.js +1 -116
- package/dist-standalone/graceful-degradation.d.ts +246 -0
- package/dist-standalone/graceful-degradation.js +1 -0
- package/dist-standalone/guardrails.js +1 -216
- package/dist-standalone/health-check.d.ts +150 -0
- package/dist-standalone/health-check.js +1 -0
- package/dist-standalone/http-compat.js +1 -267
- package/dist-standalone/http-status-map.js +1 -561
- package/dist-standalone/identity.d.ts +64 -1
- package/dist-standalone/identity.js +1 -515
- package/dist-standalone/index.d.ts +45 -3
- package/dist-standalone/index.js +1 -52
- package/dist-standalone/invitation.js +1 -415
- package/dist-standalone/invite.js +1 -324
- package/dist-standalone/key-agreement.d.ts +61 -13
- package/dist-standalone/key-agreement.js +1 -236
- package/dist-standalone/lazy-init.js +1 -295
- package/dist-standalone/logger.d.ts +77 -0
- package/dist-standalone/logger.js +1 -0
- package/dist-standalone/mdns-discovery.js +1 -195
- package/dist-standalone/nonce-store.d.ts +16 -3
- package/dist-standalone/nonce-store.js +1 -62
- package/dist-standalone/package.json +0 -1
- package/dist-standalone/pairing-manager.js +1 -219
- package/dist-standalone/plugin-system.d.ts +145 -0
- package/dist-standalone/plugin-system.js +1 -0
- package/dist-standalone/policy.js +1 -315
- package/dist-standalone/progress-callbacks.d.ts +394 -0
- package/dist-standalone/progress-callbacks.js +1 -0
- package/dist-standalone/redis-nonce-store.js +1 -72
- package/dist-standalone/registry-middleware.js +1 -47
- package/dist-standalone/retry-strategies.d.ts +382 -0
- package/dist-standalone/retry-strategies.js +1 -0
- package/dist-standalone/retry-transport.js +1 -98
- package/dist-standalone/security-policy.js +1 -239
- package/dist-standalone/serialization.d.ts +244 -0
- package/dist-standalone/serialization.js +1 -0
- package/dist-standalone/split-channel.d.ts +49 -1
- package/dist-standalone/split-channel.js +1 -171
- package/dist-standalone/subscription-proof.js +1 -224
- package/dist-standalone/succession.js +1 -142
- package/dist-standalone/timeouts.d.ts +275 -0
- package/dist-standalone/timeouts.js +1 -0
- package/dist-standalone/trace-context.d.ts +252 -0
- package/dist-standalone/trace-context.js +1 -0
- package/dist-standalone/trace-spans.d.ts +360 -0
- package/dist-standalone/trace-spans.js +1 -0
- package/dist-standalone/transport.js +1 -59
- package/dist-standalone/trust-registry.d.ts +106 -5
- package/dist-standalone/trust-registry.js +1 -702
- package/dist-standalone/vault-auth.d.ts +91 -0
- package/dist-standalone/vault-auth.js +1 -0
- package/dist-standalone/vault-store-loader.d.ts +110 -0
- package/dist-standalone/vault-store-loader.js +1 -0
- package/dist-standalone/verify.js +1 -16
- package/dist-standalone/version-info.d.ts +259 -0
- package/dist-standalone/version-info.js +1 -0
- package/dist-standalone/xfetch.js +1 -247
- package/llms.txt +1 -0
- package/package.json +65 -5
- package/share1.dat +0 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +0 -29
- package/dist-standalone/_deps/crypto/base64.js +0 -222
- package/dist-standalone/_deps/crypto/cjs/base64.js +0 -665
- package/dist-standalone/_deps/crypto/cjs/errors.js +0 -675
- package/dist-standalone/_deps/crypto/cjs/hmac.js +0 -473
- package/dist-standalone/_deps/crypto/cjs/index.js +0 -852
- package/dist-standalone/_deps/crypto/cjs/package.json +0 -1
- package/dist-standalone/_deps/crypto/cjs/padding.js +0 -511
- package/dist-standalone/_deps/crypto/cjs/share-header.js +0 -372
- package/dist-standalone/_deps/crypto/cjs/shares.js +0 -874
- package/dist-standalone/_deps/crypto/cjs/tlv.js +0 -1021
- package/dist-standalone/_deps/crypto/cjs/uuid.js +0 -443
- package/dist-standalone/_deps/crypto/cjs/verify.js +0 -414
- package/dist-standalone/_deps/crypto/cjs/xorida.js +0 -923
- package/dist-standalone/_deps/crypto/errors.d.ts +0 -51
- package/dist-standalone/_deps/crypto/errors.js +0 -199
- package/dist-standalone/_deps/crypto/hmac.d.ts +0 -39
- package/dist-standalone/_deps/crypto/hmac.js +0 -134
- package/dist-standalone/_deps/crypto/index.d.ts +0 -20
- package/dist-standalone/_deps/crypto/index.js +0 -145
- package/dist-standalone/_deps/crypto/padding.d.ts +0 -19
- package/dist-standalone/_deps/crypto/padding.js +0 -159
- package/dist-standalone/_deps/crypto/share-header.d.ts +0 -44
- package/dist-standalone/_deps/crypto/share-header.js +0 -92
- package/dist-standalone/_deps/crypto/shares.d.ts +0 -27
- package/dist-standalone/_deps/crypto/shares.js +0 -295
- package/dist-standalone/_deps/crypto/tlv.d.ts +0 -26
- package/dist-standalone/_deps/crypto/tlv.js +0 -364
- package/dist-standalone/_deps/crypto/uuid.d.ts +0 -22
- package/dist-standalone/_deps/crypto/uuid.js +0 -136
- package/dist-standalone/_deps/crypto/verify.d.ts +0 -15
- package/dist-standalone/_deps/crypto/verify.js +0 -71
- package/dist-standalone/_deps/crypto/xorida.d.ts +0 -44
- package/dist-standalone/_deps/crypto/xorida.js +0 -366
- package/dist-standalone/_deps/shared/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/errors.js.map +0 -1
- package/dist-standalone/_deps/shared/index.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/index.js.map +0 -1
- package/dist-standalone/_deps/shared/types.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.js.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.js.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.js.map +0 -1
- package/dist-standalone/_deps/xregistry/index.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/index.js.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.js.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.js.map +0 -1
- package/dist-standalone/_deps/xregistry/types.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/types.js.map +0 -1
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module timeouts
|
|
3
|
+
* Configurable request timeouts for xBind operations
|
|
4
|
+
*
|
|
5
|
+
* Provides centralized timeout management with:
|
|
6
|
+
* - Per-operation timeout configuration
|
|
7
|
+
* - Default timeouts for registry, gateway, transport
|
|
8
|
+
* - Timeout inheritance and overrides
|
|
9
|
+
* - Timeout error messages with context
|
|
10
|
+
* - Integration with AbortController/cancellation tokens
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { TimeoutConfig, withTimeout, createTimeoutController } from '@private.me/xbind';
|
|
15
|
+
*
|
|
16
|
+
* // Create timeout configuration
|
|
17
|
+
* const config = new TimeoutConfig({
|
|
18
|
+
* registry: 10000,
|
|
19
|
+
* gateway: 15000,
|
|
20
|
+
* transport: 30000,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Use timeout with fetch
|
|
24
|
+
* const controller = createTimeoutController(config.registry);
|
|
25
|
+
* const response = await fetch(url, { signal: controller.signal });
|
|
26
|
+
*
|
|
27
|
+
* // Wrap async operation with timeout
|
|
28
|
+
* const result = await withTimeout(
|
|
29
|
+
* async () => performOperation(),
|
|
30
|
+
* 5000,
|
|
31
|
+
* 'Operation description'
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import type { Result } from '@private.me/shared';
|
|
36
|
+
/**
|
|
37
|
+
* Timeout configuration for different operation types.
|
|
38
|
+
* All values in milliseconds.
|
|
39
|
+
*/
|
|
40
|
+
export interface TimeoutConfigOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Timeout for registry operations (lookup, register, update).
|
|
43
|
+
* Default: 10000ms (10 seconds)
|
|
44
|
+
*/
|
|
45
|
+
readonly registry?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Timeout for gateway operations (send, poll, status).
|
|
48
|
+
* Default: 15000ms (15 seconds)
|
|
49
|
+
*/
|
|
50
|
+
readonly gateway?: number;
|
|
51
|
+
/**
|
|
52
|
+
* Timeout for transport operations (HTTP/HTTPS requests).
|
|
53
|
+
* Default: 30000ms (30 seconds)
|
|
54
|
+
*/
|
|
55
|
+
readonly transport?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Default timeout for operations without specific category.
|
|
58
|
+
* Default: 30000ms (30 seconds)
|
|
59
|
+
*/
|
|
60
|
+
readonly default?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Override timeout for specific operation.
|
|
63
|
+
* Takes precedence over category timeouts.
|
|
64
|
+
*/
|
|
65
|
+
readonly override?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Timeout error with operation context.
|
|
69
|
+
*/
|
|
70
|
+
export declare class TimeoutError extends Error {
|
|
71
|
+
readonly code: "TIMEOUT";
|
|
72
|
+
readonly operation: string;
|
|
73
|
+
readonly timeoutMs: number;
|
|
74
|
+
readonly timestamp: number;
|
|
75
|
+
constructor(operation: string, timeoutMs: number);
|
|
76
|
+
/**
|
|
77
|
+
* Create timeout error with additional context.
|
|
78
|
+
*/
|
|
79
|
+
static withContext(operation: string, timeoutMs: number, context?: Record<string, unknown>): TimeoutError;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Default timeout values (milliseconds).
|
|
83
|
+
*/
|
|
84
|
+
export declare const DEFAULT_TIMEOUTS: {
|
|
85
|
+
readonly registry: 10000;
|
|
86
|
+
readonly gateway: 15000;
|
|
87
|
+
readonly transport: 30000;
|
|
88
|
+
readonly default: 30000;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Timeout configuration manager.
|
|
92
|
+
* Provides timeout values with inheritance and override support.
|
|
93
|
+
*/
|
|
94
|
+
export declare class TimeoutConfig {
|
|
95
|
+
private readonly registry;
|
|
96
|
+
private readonly gateway;
|
|
97
|
+
private readonly transport;
|
|
98
|
+
private readonly default;
|
|
99
|
+
constructor(options?: TimeoutConfigOptions);
|
|
100
|
+
/**
|
|
101
|
+
* Get timeout for registry operations.
|
|
102
|
+
* @param override - Optional override timeout
|
|
103
|
+
*/
|
|
104
|
+
getRegistry(override?: number): number;
|
|
105
|
+
/**
|
|
106
|
+
* Get timeout for gateway operations.
|
|
107
|
+
* @param override - Optional override timeout
|
|
108
|
+
*/
|
|
109
|
+
getGateway(override?: number): number;
|
|
110
|
+
/**
|
|
111
|
+
* Get timeout for transport operations.
|
|
112
|
+
* @param override - Optional override timeout
|
|
113
|
+
*/
|
|
114
|
+
getTransport(override?: number): number;
|
|
115
|
+
/**
|
|
116
|
+
* Get default timeout.
|
|
117
|
+
* @param override - Optional override timeout
|
|
118
|
+
*/
|
|
119
|
+
getDefault(override?: number): number;
|
|
120
|
+
/**
|
|
121
|
+
* Get timeout for specific operation category.
|
|
122
|
+
* @param category - Operation category
|
|
123
|
+
* @param override - Optional override timeout
|
|
124
|
+
*/
|
|
125
|
+
get(category: 'registry' | 'gateway' | 'transport' | 'default', override?: number): number;
|
|
126
|
+
/**
|
|
127
|
+
* Create new config with overrides.
|
|
128
|
+
* @param overrides - Timeout overrides
|
|
129
|
+
*/
|
|
130
|
+
with(overrides: TimeoutConfigOptions): TimeoutConfig;
|
|
131
|
+
/**
|
|
132
|
+
* Validate timeout value.
|
|
133
|
+
*/
|
|
134
|
+
private validateTimeout;
|
|
135
|
+
/**
|
|
136
|
+
* Export configuration as plain object.
|
|
137
|
+
*/
|
|
138
|
+
toJSON(): TimeoutConfigOptions;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Operation timeout controller with cancellation support.
|
|
142
|
+
* Wraps AbortController with timeout semantics.
|
|
143
|
+
*/
|
|
144
|
+
export declare class OperationTimeoutController {
|
|
145
|
+
readonly signal: AbortSignal;
|
|
146
|
+
readonly timeoutMs: number;
|
|
147
|
+
readonly operation: string;
|
|
148
|
+
private readonly controller;
|
|
149
|
+
private readonly timer;
|
|
150
|
+
private timedOut;
|
|
151
|
+
constructor(timeoutMs: number, operation?: string);
|
|
152
|
+
/**
|
|
153
|
+
* Cancel the operation (clear timeout).
|
|
154
|
+
*/
|
|
155
|
+
cancel(): void;
|
|
156
|
+
/**
|
|
157
|
+
* Clear timeout if operation completed successfully.
|
|
158
|
+
* Call this when operation finishes to prevent unnecessary abort.
|
|
159
|
+
*/
|
|
160
|
+
clear(): void;
|
|
161
|
+
/**
|
|
162
|
+
* Check if controller timed out (vs manual cancellation).
|
|
163
|
+
*/
|
|
164
|
+
didTimeout(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Get timeout error if operation timed out.
|
|
167
|
+
*/
|
|
168
|
+
getTimeoutError(): TimeoutError | null;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Create timeout controller for operation.
|
|
172
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
173
|
+
* @param operation - Operation description for error messages
|
|
174
|
+
*/
|
|
175
|
+
export declare function createTimeoutController(timeoutMs: number, operation?: string): OperationTimeoutController;
|
|
176
|
+
/**
|
|
177
|
+
* Wrap async operation with timeout.
|
|
178
|
+
* Rejects with TimeoutError if operation exceeds timeout.
|
|
179
|
+
*
|
|
180
|
+
* @param fn - Async operation to execute
|
|
181
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
182
|
+
* @param operation - Operation description for error messages
|
|
183
|
+
* @returns Result of operation or timeout error
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const result = await withTimeout(
|
|
188
|
+
* async () => fetch('https://api.example.com'),
|
|
189
|
+
* 5000,
|
|
190
|
+
* 'API request'
|
|
191
|
+
* );
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
export declare function withTimeout<T>(fn: () => Promise<T>, timeoutMs: number, operation?: string): Promise<T>;
|
|
195
|
+
/**
|
|
196
|
+
* Wrap async operation with timeout (Result-based).
|
|
197
|
+
* Returns ok(result) or err('TIMEOUT') without throwing.
|
|
198
|
+
*
|
|
199
|
+
* @param fn - Async operation to execute
|
|
200
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
201
|
+
* @param operation - Operation description for error messages
|
|
202
|
+
* @returns Result with operation result or timeout error
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const result = await withTimeoutResult(
|
|
207
|
+
* async () => fetch('https://api.example.com'),
|
|
208
|
+
* 5000,
|
|
209
|
+
* 'API request'
|
|
210
|
+
* );
|
|
211
|
+
* if (result.ok) {
|
|
212
|
+
* console.log('Success:', result.value);
|
|
213
|
+
* } else {
|
|
214
|
+
* console.error('Timeout:', result.error);
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export declare function withTimeoutResult<T>(fn: () => Promise<T>, timeoutMs: number, operation?: string): Promise<Result<T, 'TIMEOUT'>>;
|
|
219
|
+
/**
|
|
220
|
+
* Create timeout signal for use with fetch/AbortController APIs.
|
|
221
|
+
*
|
|
222
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
223
|
+
* @param operation - Operation description
|
|
224
|
+
* @returns AbortSignal that aborts after timeout
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* const signal = createOperationTimeoutSignal(5000, 'fetch request');
|
|
229
|
+
* const response = await fetch(url, { signal });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export declare function createOperationTimeoutSignal(timeoutMs: number, operation?: string): AbortSignal;
|
|
233
|
+
/**
|
|
234
|
+
* Timeout helper for operations with cancellation token.
|
|
235
|
+
* Returns controller that can be passed to fetch, agent.send, etc.
|
|
236
|
+
*
|
|
237
|
+
* @param config - Timeout configuration
|
|
238
|
+
* @param category - Operation category
|
|
239
|
+
* @param operation - Operation description
|
|
240
|
+
* @param override - Optional timeout override
|
|
241
|
+
* @returns Timeout controller
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const controller = createOperationTimeout(config, 'registry', 'lookup DID');
|
|
246
|
+
* const response = await fetch(url, { signal: controller.signal });
|
|
247
|
+
* controller.clear(); // Clear timeout on success
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
export declare function createOperationTimeout(config: TimeoutConfig, category: 'registry' | 'gateway' | 'transport' | 'default', operation: string, override?: number): OperationTimeoutController;
|
|
251
|
+
/**
|
|
252
|
+
* Check if error is a timeout error.
|
|
253
|
+
*/
|
|
254
|
+
export declare function isTimeoutError(error: unknown): error is TimeoutError;
|
|
255
|
+
/**
|
|
256
|
+
* Extract timeout from error if it's a timeout error.
|
|
257
|
+
* Returns timeout in milliseconds or undefined.
|
|
258
|
+
*/
|
|
259
|
+
export declare function getTimeoutFromError(error: unknown): number | undefined;
|
|
260
|
+
/**
|
|
261
|
+
* Create timeout configuration from environment variables.
|
|
262
|
+
* Looks for:
|
|
263
|
+
* - XBIND_TIMEOUT_REGISTRY
|
|
264
|
+
* - XBIND_TIMEOUT_GATEWAY
|
|
265
|
+
* - XBIND_TIMEOUT_TRANSPORT
|
|
266
|
+
* - XBIND_TIMEOUT_DEFAULT
|
|
267
|
+
*
|
|
268
|
+
* Falls back to default values if not set.
|
|
269
|
+
*/
|
|
270
|
+
export declare function createTimeoutConfigFromEnv(): TimeoutConfig;
|
|
271
|
+
/**
|
|
272
|
+
* Global default timeout configuration.
|
|
273
|
+
* Can be overridden by setting environment variables.
|
|
274
|
+
*/
|
|
275
|
+
export declare const globalTimeoutConfig: TimeoutConfig;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{err,ok}from"./_deps/shared/index.js";export class TimeoutError extends Error{code="TIMEOUT";operation;timeoutMs;timestamp;constructor(t,e){super(`Operation '${t}' timed out after ${e}ms. Actions: (1) Check network connectivity, (2) Increase timeout threshold, (3) Verify service availability, (4) Implement retry with exponential backoff.`),this.name="TimeoutError",this.operation=t,this.timeoutMs=e,this.timestamp=Date.now()}static withContext(t,e,r){const i=new TimeoutError(t,e);return r&&Object.assign(i,{context:r}),i}}export const DEFAULT_TIMEOUTS={registry:1e4,gateway:15e3,transport:3e4,default:3e4};export class TimeoutConfig{registry;gateway;transport;default;constructor(t={}){this.registry=t.registry??DEFAULT_TIMEOUTS.registry,this.gateway=t.gateway??DEFAULT_TIMEOUTS.gateway,this.transport=t.transport??DEFAULT_TIMEOUTS.transport,this.default=t.default??DEFAULT_TIMEOUTS.default,this.validateTimeout("registry",this.registry),this.validateTimeout("gateway",this.gateway),this.validateTimeout("transport",this.transport),this.validateTimeout("default",this.default)}getRegistry(t){return t??this.registry}getGateway(t){return t??this.gateway}getTransport(t){return t??this.transport}getDefault(t){return t??this.default}get(t,e){switch(t){case"registry":return this.getRegistry(e);case"gateway":return this.getGateway(e);case"transport":return this.getTransport(e);case"default":return this.getDefault(e)}}with(t){return new TimeoutConfig({registry:t.registry??this.registry,gateway:t.gateway??this.gateway,transport:t.transport??this.transport,default:t.default??this.default})}validateTimeout(t,e){if(!Number.isFinite(e)||e<=0)throw new Error(`Invalid timeout for '${t}': ${e}. Must be positive finite number.`);e>3e5&&console.warn(`[xbind:timeout] Large timeout for '${t}': ${e}ms. Consider reducing to avoid indefinite hangs.`)}toJSON(){return{registry:this.registry,gateway:this.gateway,transport:this.transport,default:this.default}}}export class OperationTimeoutController{signal;timeoutMs;operation;controller;timer;timedOut=!1;constructor(t,e="operation"){this.controller=new AbortController,this.signal=this.controller.signal,this.timeoutMs=t,this.operation=e,this.timer=setTimeout(()=>{this.timedOut=!0,this.controller.abort()},t)}cancel(){clearTimeout(this.timer),this.controller.signal.aborted||this.controller.abort()}clear(){clearTimeout(this.timer)}didTimeout(){return this.timedOut}getTimeoutError(){return this.timedOut?new TimeoutError(this.operation,this.timeoutMs):null}}export function createTimeoutController(t,e="operation"){return new OperationTimeoutController(t,e)}export async function withTimeout(t,e,r="operation"){const i=createTimeoutController(e,r);try{const o=await Promise.race([t(),new Promise((t,o)=>{i.signal.addEventListener("abort",()=>{o(new TimeoutError(r,e))})})]);return i.clear(),o}catch(t){throw i.clear(),t}}export async function withTimeoutResult(t,e,r="operation"){try{const i=await withTimeout(t,e,r);return ok(i)}catch(t){if(t instanceof TimeoutError)return err("TIMEOUT");throw t}}export function createOperationTimeoutSignal(t,e="operation"){return createTimeoutController(t,e).signal}export function createOperationTimeout(t,e,r,i){return createTimeoutController(t.get(e,i),r)}export function isTimeoutError(t){return t instanceof TimeoutError}export function getTimeoutFromError(t){return isTimeoutError(t)?t.timeoutMs:void 0}export function createTimeoutConfigFromEnv(){const t={},e=process.env.XBIND_TIMEOUT_REGISTRY;if(e){const r=parseInt(e,10);isNaN(r)||(t.registry=r)}const r=process.env.XBIND_TIMEOUT_GATEWAY;if(r){const e=parseInt(r,10);isNaN(e)||(t.gateway=e)}const i=process.env.XBIND_TIMEOUT_TRANSPORT;if(i){const e=parseInt(i,10);isNaN(e)||(t.transport=e)}const o=process.env.XBIND_TIMEOUT_DEFAULT;if(o){const e=parseInt(o,10);isNaN(e)||(t.default=e)}return new TimeoutConfig(t)}export const globalTimeoutConfig=createTimeoutConfigFromEnv();
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module trace-context
|
|
3
|
+
* W3C Trace Context implementation for distributed tracing
|
|
4
|
+
*
|
|
5
|
+
* Implements the W3C Trace Context specification for distributed tracing across
|
|
6
|
+
* xBind agent operations, enabling correlation of requests across service boundaries.
|
|
7
|
+
*
|
|
8
|
+
* Standard: https://www.w3.org/TR/trace-context/
|
|
9
|
+
*
|
|
10
|
+
* Headers:
|
|
11
|
+
* - `traceparent`: Required header with version, trace-id, parent-id, and trace-flags
|
|
12
|
+
* - `tracestate`: Optional vendor-specific state
|
|
13
|
+
*
|
|
14
|
+
* Format: `{version}-{trace-id}-{parent-id}-{trace-flags}`
|
|
15
|
+
* Example: `00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01`
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { TraceContext } from '@private.me/xbind';
|
|
20
|
+
*
|
|
21
|
+
* // Create a new trace context
|
|
22
|
+
* const ctx = TraceContext.create();
|
|
23
|
+
*
|
|
24
|
+
* // Attach to request headers
|
|
25
|
+
* const headers = new Headers();
|
|
26
|
+
* ctx.inject(headers);
|
|
27
|
+
*
|
|
28
|
+
* // Extract from incoming headers
|
|
29
|
+
* const incomingCtx = TraceContext.extract(headers);
|
|
30
|
+
*
|
|
31
|
+
* // Create child span
|
|
32
|
+
* const childCtx = incomingCtx.createChild();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* W3C Trace Context specification constants
|
|
37
|
+
*/
|
|
38
|
+
export declare const TRACEPARENT_HEADER = "traceparent";
|
|
39
|
+
export declare const TRACESTATE_HEADER = "tracestate";
|
|
40
|
+
/**
|
|
41
|
+
* Trace context version (currently only 00 is supported)
|
|
42
|
+
*/
|
|
43
|
+
export declare const TRACE_VERSION = "00";
|
|
44
|
+
/**
|
|
45
|
+
* Trace flags
|
|
46
|
+
*/
|
|
47
|
+
export declare enum TraceFlags {
|
|
48
|
+
/** Not sampled */
|
|
49
|
+
NONE = 0,
|
|
50
|
+
/** Sampled */
|
|
51
|
+
SAMPLED = 1
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Trace state key-value pair
|
|
55
|
+
*/
|
|
56
|
+
export interface TraceStateEntry {
|
|
57
|
+
key: string;
|
|
58
|
+
value: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Parsed traceparent header components
|
|
62
|
+
*/
|
|
63
|
+
export interface TraceparentSpec {
|
|
64
|
+
version: string;
|
|
65
|
+
traceId: string;
|
|
66
|
+
parentId: string;
|
|
67
|
+
traceFlags: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* W3C Trace Context implementation
|
|
71
|
+
*
|
|
72
|
+
* Represents a single point in a distributed trace with parent-child relationships.
|
|
73
|
+
*/
|
|
74
|
+
export declare class TraceContext {
|
|
75
|
+
/** W3C version (always '00' for now) */
|
|
76
|
+
readonly version: string;
|
|
77
|
+
/** Trace ID (32 hex characters, 16 bytes) */
|
|
78
|
+
readonly traceId: string;
|
|
79
|
+
/** Parent span ID (16 hex characters, 8 bytes) */
|
|
80
|
+
readonly parentId: string;
|
|
81
|
+
/** Trace flags (8-bit field) */
|
|
82
|
+
readonly traceFlags: number;
|
|
83
|
+
/** Vendor-specific trace state */
|
|
84
|
+
readonly traceState: TraceStateEntry[];
|
|
85
|
+
/**
|
|
86
|
+
* Create a trace context
|
|
87
|
+
*
|
|
88
|
+
* @param traceId - 32-character hex trace ID
|
|
89
|
+
* @param parentId - 16-character hex parent span ID
|
|
90
|
+
* @param traceFlags - 8-bit trace flags
|
|
91
|
+
* @param traceState - Optional vendor-specific state
|
|
92
|
+
*/
|
|
93
|
+
constructor(traceId: string, parentId: string, traceFlags?: number, traceState?: TraceStateEntry[]);
|
|
94
|
+
/**
|
|
95
|
+
* Create a new root trace context
|
|
96
|
+
*
|
|
97
|
+
* Generates a new trace ID and parent ID for a new trace.
|
|
98
|
+
*
|
|
99
|
+
* @param sampled - Whether this trace should be sampled
|
|
100
|
+
* @returns New trace context
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const ctx = TraceContext.create();
|
|
105
|
+
* console.log(ctx.traceId); // => "4bf92f3577b34da6a3ce929d0e0e4736"
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
static create(sampled?: boolean): TraceContext;
|
|
109
|
+
/**
|
|
110
|
+
* Extract trace context from headers
|
|
111
|
+
*
|
|
112
|
+
* Parses the traceparent and tracestate headers according to W3C spec.
|
|
113
|
+
*
|
|
114
|
+
* @param headers - Headers object or plain object
|
|
115
|
+
* @returns Trace context if found, null otherwise
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const headers = new Headers({
|
|
120
|
+
* traceparent: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
|
|
121
|
+
* });
|
|
122
|
+
* const ctx = TraceContext.extract(headers);
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
static extract(headers: Headers | Record<string, string>): TraceContext | null;
|
|
126
|
+
/**
|
|
127
|
+
* Inject trace context into headers
|
|
128
|
+
*
|
|
129
|
+
* Adds traceparent and tracestate headers to the provided headers object.
|
|
130
|
+
*
|
|
131
|
+
* @param headers - Headers to modify
|
|
132
|
+
* @returns Modified headers
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* const ctx = TraceContext.create();
|
|
137
|
+
* const headers = new Headers();
|
|
138
|
+
* ctx.inject(headers);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
inject<T extends Headers | Record<string, string>>(headers: T): T;
|
|
142
|
+
/**
|
|
143
|
+
* Create a child trace context
|
|
144
|
+
*
|
|
145
|
+
* Generates a new parent ID (span ID) while keeping the same trace ID.
|
|
146
|
+
* This represents a new span in the same trace.
|
|
147
|
+
*
|
|
148
|
+
* @param sampled - Override sampling decision (defaults to parent's decision)
|
|
149
|
+
* @returns New child trace context
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const parent = TraceContext.create();
|
|
154
|
+
* const child = parent.createChild();
|
|
155
|
+
* console.log(parent.traceId === child.traceId); // => true
|
|
156
|
+
* console.log(parent.parentId === child.parentId); // => false
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
createChild(sampled?: boolean): TraceContext;
|
|
160
|
+
/**
|
|
161
|
+
* Format as traceparent header value
|
|
162
|
+
*
|
|
163
|
+
* @returns traceparent string
|
|
164
|
+
*/
|
|
165
|
+
toTraceparent(): string;
|
|
166
|
+
/**
|
|
167
|
+
* Format as tracestate header value
|
|
168
|
+
*
|
|
169
|
+
* @returns tracestate string
|
|
170
|
+
*/
|
|
171
|
+
toTracestate(): string;
|
|
172
|
+
/**
|
|
173
|
+
* Check if this trace is sampled
|
|
174
|
+
*
|
|
175
|
+
* @returns True if sampled
|
|
176
|
+
*/
|
|
177
|
+
isSampled(): boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Add or update tracestate entry
|
|
180
|
+
*
|
|
181
|
+
* @param key - Vendor key (format: vendor[@system])
|
|
182
|
+
* @param value - State value
|
|
183
|
+
* @returns New trace context with updated state
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const ctx = TraceContext.create();
|
|
188
|
+
* const updated = ctx.withTracestate('private.me', 'did:key:z6Mk...');
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
withTracestate(key: string, value: string): TraceContext;
|
|
192
|
+
/**
|
|
193
|
+
* Get tracestate value by key
|
|
194
|
+
*
|
|
195
|
+
* @param key - Vendor key
|
|
196
|
+
* @returns State value or undefined
|
|
197
|
+
*/
|
|
198
|
+
getTracestate(key: string): string | undefined;
|
|
199
|
+
/**
|
|
200
|
+
* Convert to plain object
|
|
201
|
+
*
|
|
202
|
+
* @returns Plain object representation
|
|
203
|
+
*/
|
|
204
|
+
toObject(): {
|
|
205
|
+
version: string;
|
|
206
|
+
traceId: string;
|
|
207
|
+
parentId: string;
|
|
208
|
+
traceFlags: number;
|
|
209
|
+
traceState: TraceStateEntry[];
|
|
210
|
+
sampled: boolean;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Generate a random trace ID (32 hex characters)
|
|
215
|
+
*
|
|
216
|
+
* @returns 32-character hex string
|
|
217
|
+
*/
|
|
218
|
+
export declare function generateTraceId(): string;
|
|
219
|
+
/**
|
|
220
|
+
* Generate a random span ID (16 hex characters)
|
|
221
|
+
*
|
|
222
|
+
* @returns 16-character hex string
|
|
223
|
+
*/
|
|
224
|
+
export declare function generateSpanId(): string;
|
|
225
|
+
/**
|
|
226
|
+
* Validate trace ID format
|
|
227
|
+
*
|
|
228
|
+
* @param traceId - Trace ID to validate
|
|
229
|
+
* @returns True if valid
|
|
230
|
+
*/
|
|
231
|
+
export declare function validateTraceId(traceId: string): boolean;
|
|
232
|
+
/**
|
|
233
|
+
* Validate span ID format
|
|
234
|
+
*
|
|
235
|
+
* @param spanId - Span ID to validate
|
|
236
|
+
* @returns True if valid
|
|
237
|
+
*/
|
|
238
|
+
export declare function validateSpanId(spanId: string): boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Parse traceparent header value
|
|
241
|
+
*
|
|
242
|
+
* @param traceparent - Header value
|
|
243
|
+
* @returns Parsed components or null if invalid
|
|
244
|
+
*/
|
|
245
|
+
export declare function parseTraceparent(traceparent: string): TraceparentSpec | null;
|
|
246
|
+
/**
|
|
247
|
+
* Parse tracestate header value
|
|
248
|
+
*
|
|
249
|
+
* @param tracestate - Header value
|
|
250
|
+
* @returns Array of key-value pairs
|
|
251
|
+
*/
|
|
252
|
+
export declare function parseTracestate(tracestate: string): TraceStateEntry[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TRACEPARENT_HEADER="traceparent";export const TRACESTATE_HEADER="tracestate";export const TRACE_VERSION="00";export var TraceFlags;!function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"}(TraceFlags||(TraceFlags={}));export class TraceContext{version;traceId;parentId;traceFlags;traceState;constructor(t,e,r=TraceFlags.SAMPLED,a=[]){if(!validateTraceId(t))throw new Error(`Invalid trace ID: ${t}. Must be 32 hex characters.`);if(!validateSpanId(e))throw new Error(`Invalid parent ID: ${e}. Must be 16 hex characters.`);if(r<0||r>255)throw new Error(`Invalid trace flags: ${r}. Must be 0-255.`);this.version="00",this.traceId=t,this.parentId=e,this.traceFlags=r,this.traceState=a}static create(t=!0){const e=generateTraceId(),r=generateSpanId(),a=t?TraceFlags.SAMPLED:TraceFlags.NONE;return new TraceContext(e,r,a)}static extract(t){const e=t instanceof Headers?t.get("traceparent"):t.traceparent;if(!e)return null;const r=parseTraceparent(e);if(!r)return null;const a=t instanceof Headers?t.get("tracestate"):t.tracestate,n=a?parseTracestate(a):[];return new TraceContext(r.traceId,r.parentId,r.traceFlags,n)}inject(t){const e=this.toTraceparent();return t instanceof Headers?(t.set("traceparent",e),this.traceState.length>0&&t.set("tracestate",this.toTracestate())):(t.traceparent=e,this.traceState.length>0&&(t.tracestate=this.toTracestate())),t}createChild(t){const e=generateSpanId(),r=void 0!==t?t?TraceFlags.SAMPLED:TraceFlags.NONE:this.traceFlags;return new TraceContext(this.traceId,e,r,this.traceState)}toTraceparent(){const t=this.traceFlags.toString(16).padStart(2,"0");return`${this.version}-${this.traceId}-${this.parentId}-${t}`}toTracestate(){return this.traceState.map(({key:t,value:e})=>`${t}=${e}`).join(",")}isSampled(){return(this.traceFlags&TraceFlags.SAMPLED)===TraceFlags.SAMPLED}withTracestate(t,e){const r=this.traceState.filter(e=>e.key!==t);return r.unshift({key:t,value:e}),new TraceContext(this.traceId,this.parentId,this.traceFlags,r)}getTracestate(t){const e=this.traceState.find(e=>e.key===t);return e?.value}toObject(){return{version:this.version,traceId:this.traceId,parentId:this.parentId,traceFlags:this.traceFlags,traceState:[...this.traceState],sampled:this.isSampled()}}}export function generateTraceId(){return generateRandomHex(32)}export function generateSpanId(){return generateRandomHex(16)}export function validateTraceId(t){return"string"==typeof t&&(32===t.length&&(!!/^[a-f0-9]{32}$/.test(t)&&"00000000000000000000000000000000"!==t))}export function validateSpanId(t){return"string"==typeof t&&(16===t.length&&(!!/^[a-f0-9]{16}$/.test(t)&&"0000000000000000"!==t))}export function parseTraceparent(t){if("string"!=typeof t)return null;const e=t.split("-");if(4!==e.length)return null;const[r,a,n,s]=e;if("00"!==r)return null;if(!validateTraceId(a??""))return null;if(!validateSpanId(n??""))return null;if(!s||2!==s.length)return null;if(!/^[a-f0-9]{2}$/.test(s))return null;return{version:r??"00",traceId:a??"",parentId:n??"",traceFlags:parseInt(s,16)}}export function parseTracestate(t){if(!t)return[];const e=[],r=t.split(",");for(const t of r){const r=t.trim(),a=r.indexOf("=");if(-1===a)continue;const n=r.substring(0,a).trim(),s=r.substring(a+1).trim();n&&s&&e.push({key:n,value:s})}return e}function generateRandomHex(t){const e=Math.ceil(t/2);if("undefined"!=typeof crypto&&crypto.getRandomValues){const r=new Uint8Array(e);return crypto.getRandomValues(r),Array.from(r).map(t=>t.toString(16).padStart(2,"0")).join("").substring(0,t)}try{return require("node:crypto").randomBytes(e).toString("hex").substring(0,t)}catch{throw new Error("Cryptographic random generation unavailable. Install crypto polyfill or use environment with crypto support.")}}
|