@private.me/xbind 3.0.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -7
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
- package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
- package/dist-standalone/_deps/shared/cjs/index.js +463 -1
- package/dist-standalone/_deps/shared/cjs/types.js +315 -1
- package/dist-standalone/_deps/shared/errors.js +244 -1
- package/dist-standalone/_deps/shared/index.js +72 -1
- package/dist-standalone/_deps/shared/types.js +86 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +659 -1
- package/dist-standalone/agent-sdk.js +328 -1
- package/dist-standalone/agent.js +1800 -1
- package/dist-standalone/approval.js +193 -1
- package/dist-standalone/async-iterators.js +382 -1
- package/dist-standalone/auth.js +219 -1
- package/dist-standalone/auto-accept.js +229 -1
- package/dist-standalone/backup-config.js +201 -1
- package/dist-standalone/backup.js +326 -1
- package/dist-standalone/batch-operations.js +388 -1
- package/dist-standalone/cancellation.js +477 -1
- package/dist-standalone/checkpoint.js +186 -1
- package/dist-standalone/circuit-breaker.js +468 -1
- package/dist-standalone/cjs/agent-call.js +701 -1
- package/dist-standalone/cjs/agent-sdk.js +332 -1
- package/dist-standalone/cjs/agent.js +1837 -1
- package/dist-standalone/cjs/approval.js +199 -1
- package/dist-standalone/cjs/async-iterators.js +392 -1
- package/dist-standalone/cjs/auth.js +225 -1
- package/dist-standalone/cjs/auto-accept.js +233 -1
- package/dist-standalone/cjs/backup-config.js +207 -1
- package/dist-standalone/cjs/backup.js +330 -1
- package/dist-standalone/cjs/batch-operations.js +397 -1
- package/dist-standalone/cjs/cancellation.js +490 -1
- package/dist-standalone/cjs/checkpoint.js +193 -1
- package/dist-standalone/cjs/circuit-breaker.js +476 -1
- package/dist-standalone/cjs/cli/init.js +492 -1
- package/dist-standalone/cjs/config-validation.js +522 -1
- package/dist-standalone/cjs/connect.js +312 -1
- package/dist-standalone/cjs/connection-pool.js +506 -1
- package/dist-standalone/cjs/correlation-id.js +339 -1
- package/dist-standalone/cjs/crypto-utils.js +176 -1
- package/dist-standalone/cjs/debug-mode.js +534 -1
- package/dist-standalone/cjs/did-document.js +101 -1
- package/dist-standalone/cjs/did-privateme.js +130 -1
- package/dist-standalone/cjs/did-web.js +201 -1
- package/dist-standalone/cjs/discovery.js +462 -1
- package/dist-standalone/cjs/dual-mode.js +251 -1
- package/dist-standalone/cjs/email-templates.js +313 -1
- package/dist-standalone/cjs/email-transport.js +239 -1
- package/dist-standalone/cjs/envelope.js +538 -1
- package/dist-standalone/cjs/errors.js +913 -1
- package/dist-standalone/cjs/event-emitter.js +461 -1
- package/dist-standalone/cjs/gateway-state.js +55 -1
- package/dist-standalone/cjs/gateway-transport.js +120 -1
- package/dist-standalone/cjs/graceful-degradation.js +403 -1
- package/dist-standalone/cjs/guardrails.js +223 -1
- package/dist-standalone/cjs/health-check.js +336 -1
- package/dist-standalone/cjs/http-compat.js +272 -1
- package/dist-standalone/cjs/http-status-map.js +571 -1
- package/dist-standalone/cjs/identity.js +645 -1
- package/dist-standalone/cjs/index.js +406 -1
- package/dist-standalone/cjs/invitation.js +421 -1
- package/dist-standalone/cjs/invite.js +328 -1
- package/dist-standalone/cjs/key-agreement.js +335 -1
- package/dist-standalone/cjs/lazy-init.js +300 -1
- package/dist-standalone/cjs/logger.js +291 -1
- package/dist-standalone/cjs/mdns-discovery.js +202 -1
- package/dist-standalone/cjs/nonce-store.js +80 -1
- package/dist-standalone/cjs/pairing-manager.js +223 -1
- package/dist-standalone/cjs/plugin-system.js +264 -1
- package/dist-standalone/cjs/plugins/logging.js +168 -1
- package/dist-standalone/cjs/plugins/metrics.js +181 -1
- package/dist-standalone/cjs/plugins/validation.js +302 -1
- package/dist-standalone/cjs/policy.js +320 -1
- package/dist-standalone/cjs/progress-callbacks.js +583 -1
- package/dist-standalone/cjs/redis-nonce-store.js +76 -1
- package/dist-standalone/cjs/registry-middleware.js +50 -1
- package/dist-standalone/cjs/retry-strategies.js +544 -1
- package/dist-standalone/cjs/retry-transport.js +102 -1
- package/dist-standalone/cjs/runtime/browser.js +533 -1
- package/dist-standalone/cjs/runtime/edge.js +526 -1
- package/dist-standalone/cjs/runtime/react-native.js +394 -1
- package/dist-standalone/cjs/security-policy.js +245 -1
- package/dist-standalone/cjs/serialization.js +1040 -1
- package/dist-standalone/cjs/split-channel.js +225 -1
- package/dist-standalone/cjs/subscription-proof.js +230 -1
- package/dist-standalone/cjs/succession.js +148 -1
- package/dist-standalone/cjs/timeouts.js +412 -1
- package/dist-standalone/cjs/trace-context.js +424 -1
- package/dist-standalone/cjs/trace-spans.js +495 -1
- package/dist-standalone/cjs/transport.js +63 -1
- package/dist-standalone/cjs/trust-registry.js +991 -1
- package/dist-standalone/cjs/types/error-response.js +56 -1
- package/dist-standalone/cjs/vault-auth.js +178 -1
- package/dist-standalone/cjs/vault-store-loader.js +194 -1
- package/dist-standalone/cjs/verify.js +25 -1
- package/dist-standalone/cjs/version-info.js +543 -1
- package/dist-standalone/cjs/xfetch.js +340 -1
- package/dist-standalone/cli/init.js +455 -1
- package/dist-standalone/cli/setup.js +514 -1
- package/dist-standalone/cli/types.js +27 -1
- package/dist-standalone/cli/xbind.js +148 -1
- package/dist-standalone/config-validation.js +513 -1
- package/dist-standalone/connect.js +274 -1
- package/dist-standalone/connection-pool.js +500 -1
- package/dist-standalone/correlation-id.js +326 -1
- package/dist-standalone/crypto-utils.js +157 -1
- package/dist-standalone/debug-mode.js +510 -1
- package/dist-standalone/did-document.js +96 -1
- package/dist-standalone/did-privateme.js +121 -1
- package/dist-standalone/did-web.js +196 -1
- package/dist-standalone/discovery.js +458 -1
- package/dist-standalone/dual-mode.js +247 -1
- package/dist-standalone/email-templates.js +309 -1
- package/dist-standalone/email-transport.js +232 -1
- package/dist-standalone/envelope.js +525 -1
- package/dist-standalone/errors.js +896 -1
- package/dist-standalone/event-emitter.js +456 -1
- package/dist-standalone/gateway-state.js +51 -1
- package/dist-standalone/gateway-transport.js +116 -1
- package/dist-standalone/graceful-degradation.js +396 -1
- package/dist-standalone/guardrails.js +216 -1
- package/dist-standalone/health-check.js +332 -1
- package/dist-standalone/http-compat.js +267 -1
- package/dist-standalone/http-status-map.js +561 -1
- package/dist-standalone/identity.js +619 -1
- package/dist-standalone/index.js +78 -1
- package/dist-standalone/invitation.js +415 -1
- package/dist-standalone/invite.js +324 -1
- package/dist-standalone/key-agreement.js +325 -1
- package/dist-standalone/lazy-init.js +295 -1
- package/dist-standalone/logger.js +285 -1
- package/dist-standalone/mdns-discovery.js +195 -1
- package/dist-standalone/nonce-store.js +76 -1
- package/dist-standalone/pairing-manager.js +219 -1
- package/dist-standalone/plugin-system.js +257 -1
- package/dist-standalone/plugins/logging.d.ts +84 -0
- package/dist-standalone/plugins/logging.js +163 -0
- package/dist-standalone/plugins/metrics.d.ts +111 -0
- package/dist-standalone/plugins/metrics.js +176 -0
- package/dist-standalone/plugins/validation.d.ts +104 -0
- package/dist-standalone/plugins/validation.js +297 -0
- package/dist-standalone/policy.js +315 -1
- package/dist-standalone/progress-callbacks.js +576 -1
- package/dist-standalone/redis-nonce-store.js +72 -1
- package/dist-standalone/registry-middleware.js +47 -1
- package/dist-standalone/retry-strategies.js +534 -1
- package/dist-standalone/retry-transport.js +98 -1
- package/dist-standalone/runtime/browser.d.ts +311 -0
- package/dist-standalone/runtime/browser.js +516 -0
- package/dist-standalone/runtime/edge.d.ts +282 -0
- package/dist-standalone/runtime/edge.js +511 -0
- package/dist-standalone/runtime/react-native.d.ts +157 -0
- package/dist-standalone/runtime/react-native.js +383 -0
- package/dist-standalone/security-policy.js +239 -1
- package/dist-standalone/serialization.js +1031 -1
- package/dist-standalone/split-channel.js +219 -1
- package/dist-standalone/subscription-proof.js +224 -1
- package/dist-standalone/succession.js +142 -1
- package/dist-standalone/timeouts.js +398 -1
- package/dist-standalone/trace-context.js +414 -1
- package/dist-standalone/trace-spans.js +488 -1
- package/dist-standalone/transport.js +59 -1
- package/dist-standalone/trust-registry.js +950 -1
- package/dist-standalone/types/error-response.d.ts +209 -0
- package/dist-standalone/types/error-response.js +52 -0
- package/dist-standalone/vault-auth.js +174 -1
- package/dist-standalone/vault-store-loader.js +187 -1
- package/dist-standalone/verify.js +16 -1
- package/dist-standalone/version-info.js +530 -1
- package/dist-standalone/xfetch.js +335 -1
- package/package.json +4 -10
- package/share1.dat +0 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
- package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
- package/dist-standalone/_deps/shared/cjs/package.json +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
- package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
- package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
- package/dist-standalone/cjs/package.json +0 -3
- package/dist-standalone/package.json +0 -10
|
@@ -1 +1,340 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module xfetch
|
|
4
|
+
* Drop-in fetch() replacement with auto-upgrade to xBind
|
|
5
|
+
*
|
|
6
|
+
* Provides transparent upgrade from HTTP to xBind when both parties support it.
|
|
7
|
+
* Falls back to regular HTTP fetch() when xBind is not available.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.xfetch = xfetch;
|
|
11
|
+
exports.isXBindSupported = isXBindSupported;
|
|
12
|
+
exports.getXBindCapability = getXBindCapability;
|
|
13
|
+
const agent_js_1 = require("./agent.js");
|
|
14
|
+
const agent_call_js_1 = require("./agent-call.js");
|
|
15
|
+
/**
|
|
16
|
+
* Check if endpoint supports xBind
|
|
17
|
+
*
|
|
18
|
+
* Sends OPTIONS request with xBind capability header.
|
|
19
|
+
* Endpoint responds with xBind-Support header if available.
|
|
20
|
+
*
|
|
21
|
+
* @param url - Target URL
|
|
22
|
+
* @returns Capability check result
|
|
23
|
+
*/
|
|
24
|
+
async function checkXBindSupport(url) {
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(url, {
|
|
27
|
+
method: 'OPTIONS',
|
|
28
|
+
headers: {
|
|
29
|
+
'X-Capability-Check': 'xbind',
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const supported = response.headers.get('X-xBind-Support') === 'true';
|
|
33
|
+
if (!supported) {
|
|
34
|
+
return { supported: false };
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
supported: true,
|
|
38
|
+
endpoint: response.headers.get('X-xBind-Endpoint') || undefined,
|
|
39
|
+
peerDID: response.headers.get('X-xBind-DID') || undefined,
|
|
40
|
+
version: response.headers.get('X-xBind-Version') || undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
// Network error or CORS - assume no xBind support
|
|
45
|
+
return { supported: false };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Send request via xBind transport
|
|
50
|
+
*
|
|
51
|
+
* Uses synchronous request-response pattern with direct signing and encryption.
|
|
52
|
+
* This is more appropriate for HTTP semantics than async agent.send().
|
|
53
|
+
*
|
|
54
|
+
* Architecture:
|
|
55
|
+
* 1. Wrap HTTP request in payload
|
|
56
|
+
* 2. Add correlation ID for response matching
|
|
57
|
+
* 3. Send via agent.send() to server DID
|
|
58
|
+
* 4. Wait for response with matching correlation ID
|
|
59
|
+
* 5. Decrypt and verify response
|
|
60
|
+
* 6. Return as standard HTTP Response
|
|
61
|
+
*
|
|
62
|
+
* @param url - Target URL
|
|
63
|
+
* @param capability - xBind capability info
|
|
64
|
+
* @param options - Request options
|
|
65
|
+
* @param agent - Agent instance
|
|
66
|
+
* @returns xBind response
|
|
67
|
+
*/
|
|
68
|
+
async function sendViaXBind(url, capability, options, agent) {
|
|
69
|
+
const startTime = Date.now();
|
|
70
|
+
// Validate that server provided DID and endpoint
|
|
71
|
+
if (!capability.peerDID) {
|
|
72
|
+
throw new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.NETWORK_ERROR, 'Server supports xBind but did not provide X-xBind-DID header', { url, capability });
|
|
73
|
+
}
|
|
74
|
+
const peerDID = capability.peerDID; // Type narrowing for later use
|
|
75
|
+
const xbindEndpoint = capability.endpoint || url;
|
|
76
|
+
// Generate secure correlation ID using crypto.getRandomValues()
|
|
77
|
+
const randomBytes = new Uint8Array(16);
|
|
78
|
+
crypto.getRandomValues(randomBytes);
|
|
79
|
+
const randomHex = Array.from(randomBytes)
|
|
80
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
81
|
+
.join('');
|
|
82
|
+
const correlationId = `xfetch_${Date.now()}_${randomHex}`;
|
|
83
|
+
// Build HTTP request payload to wrap in envelope
|
|
84
|
+
const httpRequest = {
|
|
85
|
+
correlationId,
|
|
86
|
+
method: options.method || 'GET',
|
|
87
|
+
url,
|
|
88
|
+
headers: options.headers || {},
|
|
89
|
+
body: options.body ? (typeof options.body === 'string'
|
|
90
|
+
? options.body
|
|
91
|
+
: JSON.stringify(options.body)) : undefined,
|
|
92
|
+
};
|
|
93
|
+
// Set up response handler BEFORE sending request
|
|
94
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
95
|
+
const timeout = setTimeout(() => {
|
|
96
|
+
reject(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.TIMEOUT, 'xBind request timed out waiting for response', { url, correlationId, timeout: options.timeout || 30000 }));
|
|
97
|
+
}, options.timeout || 30000);
|
|
98
|
+
// Listen for response message
|
|
99
|
+
// TODO: This requires agent to support message handlers
|
|
100
|
+
// For now, we'll use a polling approach via the server's HTTP endpoint
|
|
101
|
+
// Alternative: Send request and poll for response
|
|
102
|
+
// This is less elegant but works without modifying Agent
|
|
103
|
+
(async () => {
|
|
104
|
+
try {
|
|
105
|
+
// Send request via agent.send()
|
|
106
|
+
const sendResult = await agent.send({
|
|
107
|
+
to: peerDID,
|
|
108
|
+
payload: httpRequest,
|
|
109
|
+
scope: 'xfetch',
|
|
110
|
+
action: 'http_request',
|
|
111
|
+
});
|
|
112
|
+
if (!sendResult.ok) {
|
|
113
|
+
clearTimeout(timeout);
|
|
114
|
+
reject(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.NETWORK_ERROR, `Failed to send xBind request: ${sendResult.error}`, { url, error: sendResult.error }));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// Poll server for response (synchronous HTTP endpoint)
|
|
118
|
+
// Server should provide /xfetch/response/:correlationId endpoint
|
|
119
|
+
const responseUrl = `${xbindEndpoint}/response/${correlationId}`;
|
|
120
|
+
const pollInterval = 100; // Poll every 100ms
|
|
121
|
+
const maxPolls = (options.timeout || 30000) / pollInterval;
|
|
122
|
+
for (let i = 0; i < maxPolls; i++) {
|
|
123
|
+
await new Promise(r => setTimeout(r, pollInterval));
|
|
124
|
+
try {
|
|
125
|
+
const response = await fetch(responseUrl);
|
|
126
|
+
if (response.status === 200) {
|
|
127
|
+
// Got response
|
|
128
|
+
clearTimeout(timeout);
|
|
129
|
+
const latency = Date.now() - startTime;
|
|
130
|
+
resolve(Object.assign(response, {
|
|
131
|
+
usedXBind: true,
|
|
132
|
+
transport: {
|
|
133
|
+
protocol: 'xbind',
|
|
134
|
+
latency,
|
|
135
|
+
peerDID: peerDID,
|
|
136
|
+
},
|
|
137
|
+
}));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// 404 or 202 means still processing, continue polling
|
|
141
|
+
}
|
|
142
|
+
catch (pollError) {
|
|
143
|
+
// Network error during polling, continue
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Timeout
|
|
148
|
+
clearTimeout(timeout);
|
|
149
|
+
reject(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.TIMEOUT, 'Response not available after polling', { url, correlationId, polls: maxPolls }));
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
clearTimeout(timeout);
|
|
153
|
+
reject(error instanceof agent_call_js_1.AgentError ? error : new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.NETWORK_ERROR, 'xBind request failed', { url, error: String(error) }));
|
|
154
|
+
}
|
|
155
|
+
})();
|
|
156
|
+
});
|
|
157
|
+
return responsePromise;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Send request via standard HTTP fetch
|
|
161
|
+
*
|
|
162
|
+
* @param url - Target URL
|
|
163
|
+
* @param options - Request options
|
|
164
|
+
* @returns HTTP response wrapped as XFetchResponse
|
|
165
|
+
*/
|
|
166
|
+
async function sendViaHTTP(url, options) {
|
|
167
|
+
const startTime = Date.now();
|
|
168
|
+
// Remove xFetch-specific options before passing to fetch
|
|
169
|
+
const { forceXBind, disableXBind, agent, retry, ...fetchOptions } = options;
|
|
170
|
+
const response = await fetch(url, fetchOptions);
|
|
171
|
+
const latency = Date.now() - startTime;
|
|
172
|
+
// Wrap Response with xFetch metadata
|
|
173
|
+
return Object.assign(response, {
|
|
174
|
+
usedXBind: false,
|
|
175
|
+
transport: {
|
|
176
|
+
protocol: url.startsWith('https') ? 'https' : 'http',
|
|
177
|
+
latency,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Retry logic with exponential backoff
|
|
183
|
+
*
|
|
184
|
+
* @param fn - Function to retry
|
|
185
|
+
* @param options - Retry configuration
|
|
186
|
+
* @returns Result of function
|
|
187
|
+
*/
|
|
188
|
+
async function withRetry(fn, options) {
|
|
189
|
+
const maxAttempts = options?.maxAttempts ?? 3;
|
|
190
|
+
const initialDelay = options?.initialDelay ?? 1000;
|
|
191
|
+
const multiplier = options?.multiplier ?? 2;
|
|
192
|
+
let lastError;
|
|
193
|
+
let delay = initialDelay;
|
|
194
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
195
|
+
try {
|
|
196
|
+
return await fn();
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
200
|
+
// Don't retry on client errors (4xx)
|
|
201
|
+
if (error instanceof Response && error.status >= 400 && error.status < 500) {
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
if (attempt < maxAttempts) {
|
|
205
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
206
|
+
delay *= multiplier;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
throw lastError;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* xFetch - Drop-in fetch() replacement with auto-upgrade to xBind
|
|
214
|
+
*
|
|
215
|
+
* @param url - Target URL
|
|
216
|
+
* @param options - Request options (extends fetch RequestInit)
|
|
217
|
+
* @returns Response with xBind metadata
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* // Basic usage (auto-upgrades to xBind if supported)
|
|
222
|
+
* const response = await xfetch('https://api.example.com/data');
|
|
223
|
+
* console.log('Used xBind?', response.usedXBind);
|
|
224
|
+
* const data = await response.json();
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* // Force xBind (fail if not supported)
|
|
230
|
+
* const response = await xfetch('https://api.example.com/secure', {
|
|
231
|
+
* forceXBind: true,
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* // Disable xBind (HTTP only)
|
|
238
|
+
* const response = await xfetch('https://api.example.com/public', {
|
|
239
|
+
* disableXBind: true,
|
|
240
|
+
* });
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* // With retry and timeout
|
|
246
|
+
* const response = await xfetch('https://api.example.com/data', {
|
|
247
|
+
* timeout: 10000,
|
|
248
|
+
* retry: {
|
|
249
|
+
* maxAttempts: 5,
|
|
250
|
+
* initialDelay: 500,
|
|
251
|
+
* multiplier: 2,
|
|
252
|
+
* },
|
|
253
|
+
* });
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
async function xfetch(url, options = {}) {
|
|
257
|
+
// Validate URL
|
|
258
|
+
let parsedURL;
|
|
259
|
+
try {
|
|
260
|
+
parsedURL = new URL(url);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
throw new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.INVALID_PARAMS, `Invalid URL: ${url}`, { url, error });
|
|
264
|
+
}
|
|
265
|
+
// Only support HTTP(S) protocols
|
|
266
|
+
if (!['http:', 'https:'].includes(parsedURL.protocol)) {
|
|
267
|
+
throw new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.INVALID_PARAMS, `Unsupported protocol: ${parsedURL.protocol}`, { url, protocol: parsedURL.protocol });
|
|
268
|
+
}
|
|
269
|
+
// Apply timeout if specified
|
|
270
|
+
const timeout = options.timeout ?? 30000;
|
|
271
|
+
const controller = new AbortController();
|
|
272
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
273
|
+
try {
|
|
274
|
+
// Merge abort signals if user provided one
|
|
275
|
+
const signal = options.signal
|
|
276
|
+
? AbortSignal.any?.([options.signal, controller.signal]) ?? controller.signal
|
|
277
|
+
: controller.signal;
|
|
278
|
+
const requestOptions = {
|
|
279
|
+
...options,
|
|
280
|
+
signal,
|
|
281
|
+
};
|
|
282
|
+
// If xBind is explicitly disabled, use HTTP directly
|
|
283
|
+
if (options.disableXBind) {
|
|
284
|
+
return await withRetry(() => sendViaHTTP(url, requestOptions), options.retry);
|
|
285
|
+
}
|
|
286
|
+
// Check if endpoint supports xBind
|
|
287
|
+
const capability = await checkXBindSupport(url);
|
|
288
|
+
// If xBind is forced but not supported, fail
|
|
289
|
+
if (options.forceXBind && !capability.supported) {
|
|
290
|
+
throw new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.NETWORK_ERROR, `xBind required but endpoint does not support it: ${url}`, { url, forceXBind: true, capability });
|
|
291
|
+
}
|
|
292
|
+
// If xBind is supported, use it
|
|
293
|
+
if (capability.supported) {
|
|
294
|
+
// Create or use provided agent
|
|
295
|
+
const agent = options.agent ?? await agent_js_1.Agent.from({ identity: 'ephemeral', identityTTL: 3600000 });
|
|
296
|
+
return await withRetry(() => sendViaXBind(url, capability, requestOptions, agent), options.retry);
|
|
297
|
+
}
|
|
298
|
+
// Fallback to HTTP
|
|
299
|
+
return await withRetry(() => sendViaHTTP(url, requestOptions), options.retry);
|
|
300
|
+
}
|
|
301
|
+
finally {
|
|
302
|
+
clearTimeout(timeoutId);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Check if a URL supports xBind
|
|
307
|
+
*
|
|
308
|
+
* Utility function for checking xBind support before making a request.
|
|
309
|
+
*
|
|
310
|
+
* @param url - URL to check
|
|
311
|
+
* @returns True if endpoint supports xBind
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```typescript
|
|
315
|
+
* const supported = await isXBindSupported('https://api.example.com');
|
|
316
|
+
* if (supported) {
|
|
317
|
+
* console.log('Endpoint supports xBind - request will be upgraded');
|
|
318
|
+
* }
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
async function isXBindSupported(url) {
|
|
322
|
+
const capability = await checkXBindSupport(url);
|
|
323
|
+
return capability.supported;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Get xBind capability information for a URL
|
|
327
|
+
*
|
|
328
|
+
* @param url - URL to check
|
|
329
|
+
* @returns Capability information
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const info = await getXBindCapability('https://api.example.com');
|
|
334
|
+
* console.log('Peer DID:', info.peerDID);
|
|
335
|
+
* console.log('xBind version:', info.version);
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
async function getXBindCapability(url) {
|
|
339
|
+
return checkXBindSupport(url);
|
|
340
|
+
}
|