@private.me/xbind 1.2.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/AGENTS.md +778 -0
- package/LICENSE.md +27 -0
- package/README.md +400 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +29 -0
- package/dist-standalone/_deps/crypto/base64.js +97 -0
- package/dist-standalone/_deps/crypto/cjs/base64.js +103 -0
- package/dist-standalone/_deps/crypto/cjs/errors.js +119 -0
- package/dist-standalone/_deps/crypto/cjs/hmac.js +71 -0
- package/dist-standalone/_deps/crypto/cjs/index.js +86 -0
- package/dist-standalone/_deps/crypto/cjs/padding.js +57 -0
- package/dist-standalone/_deps/crypto/cjs/share-header.js +68 -0
- package/dist-standalone/_deps/crypto/cjs/shares.js +152 -0
- package/dist-standalone/_deps/crypto/cjs/tlv.js +199 -0
- package/dist-standalone/_deps/crypto/cjs/uuid.js +61 -0
- package/dist-standalone/_deps/crypto/cjs/verify.js +24 -0
- package/dist-standalone/_deps/crypto/cjs/xorida.js +221 -0
- package/dist-standalone/_deps/crypto/errors.d.ts +51 -0
- package/dist-standalone/_deps/crypto/errors.js +109 -0
- package/dist-standalone/_deps/crypto/hmac.d.ts +39 -0
- package/dist-standalone/_deps/crypto/hmac.js +66 -0
- package/dist-standalone/_deps/crypto/index.d.ts +20 -0
- package/dist-standalone/_deps/crypto/index.js +45 -0
- package/dist-standalone/_deps/crypto/padding.d.ts +19 -0
- package/dist-standalone/_deps/crypto/padding.js +53 -0
- package/dist-standalone/_deps/crypto/share-header.d.ts +44 -0
- package/dist-standalone/_deps/crypto/share-header.js +63 -0
- package/dist-standalone/_deps/crypto/shares.d.ts +27 -0
- package/dist-standalone/_deps/crypto/shares.js +148 -0
- package/dist-standalone/_deps/crypto/tlv.d.ts +26 -0
- package/dist-standalone/_deps/crypto/tlv.js +195 -0
- package/dist-standalone/_deps/crypto/uuid.d.ts +22 -0
- package/dist-standalone/_deps/crypto/uuid.js +56 -0
- package/dist-standalone/_deps/crypto/verify.d.ts +15 -0
- package/dist-standalone/_deps/crypto/verify.js +15 -0
- package/dist-standalone/_deps/crypto/xorida.d.ts +44 -0
- package/dist-standalone/_deps/crypto/xorida.js +215 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +24 -0
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -0
- package/dist-standalone/_deps/mldsa-wasm/package.json +46 -0
- package/dist-standalone/_deps/mldsa-wasm/types/mldsa.d.ts +30 -0
- package/dist-standalone/_deps/shared/cjs/errors.js +582 -0
- package/dist-standalone/_deps/shared/cjs/index.js +492 -0
- package/dist-standalone/_deps/shared/cjs/package.json +1 -0
- package/dist-standalone/_deps/shared/cjs/types.js +403 -0
- package/dist-standalone/_deps/shared/errors.d.ts +48 -0
- package/dist-standalone/_deps/shared/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/shared/errors.js +192 -0
- package/dist-standalone/_deps/shared/errors.js.map +1 -0
- package/dist-standalone/_deps/shared/index.d.ts +4 -0
- package/dist-standalone/_deps/shared/index.d.ts.map +1 -0
- package/dist-standalone/_deps/shared/index.js +78 -0
- package/dist-standalone/_deps/shared/index.js.map +1 -0
- package/dist-standalone/_deps/shared/types.d.ts +1097 -0
- package/dist-standalone/_deps/shared/types.d.ts.map +1 -0
- package/dist-standalone/_deps/shared/types.js +89 -0
- package/dist-standalone/_deps/shared/types.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts +115 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts +13 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts +39 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +83 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts +99 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +143 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts +32 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +119 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts +109 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +8 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/errors.d.ts +115 -0
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/errors.js +253 -0
- package/dist-standalone/_deps/ux-helpers/errors.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/index.d.ts +13 -0
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/index.js +16 -0
- package/dist-standalone/_deps/ux-helpers/index.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts +39 -0
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/pagination.js +79 -0
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/progress.d.ts +99 -0
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/progress.js +138 -0
- package/dist-standalone/_deps/ux-helpers/progress.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/search.d.ts +32 -0
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/search.js +116 -0
- package/dist-standalone/_deps/ux-helpers/search.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/types.d.ts +109 -0
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/types.js +7 -0
- package/dist-standalone/_deps/ux-helpers/types.js.map +1 -0
- package/dist-standalone/_deps/xchange/auto-accept.d.ts +127 -0
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/package.json +1 -0
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -0
- package/dist-standalone/_deps/xchange/errors.d.ts +69 -0
- package/dist-standalone/_deps/xchange/errors.js +1 -0
- package/dist-standalone/_deps/xchange/index.d.ts +15 -0
- package/dist-standalone/_deps/xchange/index.js +1 -0
- package/dist-standalone/_deps/xchange/invite-client.d.ts +178 -0
- package/dist-standalone/_deps/xchange/invite-client.js +1 -0
- package/dist-standalone/_deps/xchange/lazy-init.d.ts +176 -0
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -0
- package/dist-standalone/_deps/xchange/trust-integration.d.ts +102 -0
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -0
- package/dist-standalone/_deps/xchange/xchange.d.ts +60 -0
- package/dist-standalone/_deps/xchange/xchange.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/package.json +1 -0
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -0
- package/dist-standalone/_deps/xregistry/discovery.d.ts +126 -0
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/discovery.js +1 -0
- package/dist-standalone/_deps/xregistry/discovery.js.map +1 -0
- package/dist-standalone/_deps/xregistry/errors.d.ts +41 -0
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/errors.js +1 -0
- package/dist-standalone/_deps/xregistry/errors.js.map +1 -0
- package/dist-standalone/_deps/xregistry/index.d.ts +8 -0
- package/dist-standalone/_deps/xregistry/index.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/index.js +1 -0
- package/dist-standalone/_deps/xregistry/index.js.map +1 -0
- package/dist-standalone/_deps/xregistry/registry.d.ts +85 -0
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/registry.js +1 -0
- package/dist-standalone/_deps/xregistry/registry.js.map +1 -0
- package/dist-standalone/_deps/xregistry/schema.d.ts +81 -0
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/schema.js +1 -0
- package/dist-standalone/_deps/xregistry/schema.js.map +1 -0
- package/dist-standalone/_deps/xregistry/types.d.ts +95 -0
- package/dist-standalone/_deps/xregistry/types.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/types.js +1 -0
- package/dist-standalone/_deps/xregistry/types.js.map +1 -0
- package/dist-standalone/agent-call.d.ts +286 -0
- package/dist-standalone/agent-call.js +642 -0
- package/dist-standalone/agent-sdk.d.ts +207 -0
- package/dist-standalone/agent-sdk.js +328 -0
- package/dist-standalone/agent.d.ts +670 -0
- package/dist-standalone/agent.js +1529 -0
- package/dist-standalone/approval.d.ts +145 -0
- package/dist-standalone/approval.js +193 -0
- package/dist-standalone/auth.d.ts +75 -0
- package/dist-standalone/auth.js +219 -0
- package/dist-standalone/auto-accept.d.ts +102 -0
- package/dist-standalone/auto-accept.js +229 -0
- package/dist-standalone/backup-config.d.ts +150 -0
- package/dist-standalone/backup-config.js +201 -0
- package/dist-standalone/checkpoint.d.ts +125 -0
- package/dist-standalone/checkpoint.js +186 -0
- package/dist-standalone/cjs/agent-call.js +651 -0
- package/dist-standalone/cjs/agent-sdk.js +332 -0
- package/dist-standalone/cjs/agent.js +1566 -0
- package/dist-standalone/cjs/approval.js +199 -0
- package/dist-standalone/cjs/auth.js +225 -0
- package/dist-standalone/cjs/auto-accept.js +233 -0
- package/dist-standalone/cjs/backup-config.js +207 -0
- package/dist-standalone/cjs/checkpoint.js +193 -0
- package/dist-standalone/cjs/cli/init.js +487 -0
- package/dist-standalone/cjs/connect.js +312 -0
- package/dist-standalone/cjs/did-document.js +101 -0
- package/dist-standalone/cjs/did-privateme.js +130 -0
- package/dist-standalone/cjs/did-web.js +201 -0
- package/dist-standalone/cjs/discovery.js +462 -0
- package/dist-standalone/cjs/dual-mode.js +251 -0
- package/dist-standalone/cjs/email-templates.js +313 -0
- package/dist-standalone/cjs/email-transport.js +239 -0
- package/dist-standalone/cjs/envelope.js +510 -0
- package/dist-standalone/cjs/errors.js +562 -0
- package/dist-standalone/cjs/gateway-state.js +55 -0
- package/dist-standalone/cjs/gateway-transport.js +120 -0
- package/dist-standalone/cjs/guardrails.js +223 -0
- package/dist-standalone/cjs/http-compat.js +272 -0
- package/dist-standalone/cjs/identity.js +541 -0
- package/dist-standalone/cjs/index.js +224 -0
- package/dist-standalone/cjs/invitation.js +421 -0
- package/dist-standalone/cjs/invite.js +328 -0
- package/dist-standalone/cjs/key-agreement.js +246 -0
- package/dist-standalone/cjs/lazy-init.js +300 -0
- package/dist-standalone/cjs/mdns-discovery.js +202 -0
- package/dist-standalone/cjs/nonce-store.js +66 -0
- package/dist-standalone/cjs/package.json +3 -0
- package/dist-standalone/cjs/pairing-manager.js +223 -0
- package/dist-standalone/cjs/policy.js +320 -0
- package/dist-standalone/cjs/redis-nonce-store.js +76 -0
- package/dist-standalone/cjs/registry-middleware.js +50 -0
- package/dist-standalone/cjs/retry-transport.js +102 -0
- package/dist-standalone/cjs/security-policy.js +204 -0
- package/dist-standalone/cjs/split-channel.js +177 -0
- package/dist-standalone/cjs/subscription-proof.js +230 -0
- package/dist-standalone/cjs/succession.js +148 -0
- package/dist-standalone/cjs/transport.js +63 -0
- package/dist-standalone/cjs/trust-registry.js +742 -0
- package/dist-standalone/cjs/verify.js +25 -0
- package/dist-standalone/cjs/xfetch.js +252 -0
- package/dist-standalone/cli/init.d.ts +63 -0
- package/dist-standalone/cli/init.js +450 -0
- package/dist-standalone/connect.d.ts +143 -0
- package/dist-standalone/connect.js +274 -0
- package/dist-standalone/did-document.d.ts +65 -0
- package/dist-standalone/did-document.js +96 -0
- package/dist-standalone/did-privateme.d.ts +70 -0
- package/dist-standalone/did-privateme.js +121 -0
- package/dist-standalone/did-web.d.ts +73 -0
- package/dist-standalone/did-web.js +196 -0
- package/dist-standalone/discovery.d.ts +176 -0
- package/dist-standalone/discovery.js +458 -0
- package/dist-standalone/dual-mode.d.ts +145 -0
- package/dist-standalone/dual-mode.js +247 -0
- package/dist-standalone/email-templates.d.ts +41 -0
- package/dist-standalone/email-templates.js +309 -0
- package/dist-standalone/email-transport.d.ts +139 -0
- package/dist-standalone/email-transport.js +232 -0
- package/dist-standalone/envelope.d.ts +288 -0
- package/dist-standalone/envelope.js +497 -0
- package/dist-standalone/errors.d.ts +74 -0
- package/dist-standalone/errors.js +548 -0
- package/dist-standalone/gateway-state.d.ts +32 -0
- package/dist-standalone/gateway-state.js +51 -0
- package/dist-standalone/gateway-transport.d.ts +59 -0
- package/dist-standalone/gateway-transport.js +116 -0
- package/dist-standalone/guardrails.d.ts +136 -0
- package/dist-standalone/guardrails.js +216 -0
- package/dist-standalone/http-compat.d.ts +150 -0
- package/dist-standalone/http-compat.js +267 -0
- package/dist-standalone/identity.d.ts +176 -0
- package/dist-standalone/identity.js +516 -0
- package/dist-standalone/index.d.ts +83 -0
- package/dist-standalone/index.js +51 -0
- package/dist-standalone/invitation.d.ts +211 -0
- package/dist-standalone/invitation.js +415 -0
- package/dist-standalone/invite.d.ts +192 -0
- package/dist-standalone/invite.js +324 -0
- package/dist-standalone/key-agreement.d.ts +122 -0
- package/dist-standalone/key-agreement.js +236 -0
- package/dist-standalone/lazy-init.d.ts +167 -0
- package/dist-standalone/lazy-init.js +295 -0
- package/dist-standalone/mdns-discovery.d.ts +117 -0
- package/dist-standalone/mdns-discovery.js +195 -0
- package/dist-standalone/nonce-store.d.ts +39 -0
- package/dist-standalone/nonce-store.js +62 -0
- package/dist-standalone/package.json +11 -0
- package/dist-standalone/pairing-manager.d.ts +147 -0
- package/dist-standalone/pairing-manager.js +219 -0
- package/dist-standalone/policy.d.ts +150 -0
- package/dist-standalone/policy.js +315 -0
- package/dist-standalone/redis-nonce-store.d.ts +93 -0
- package/dist-standalone/redis-nonce-store.js +72 -0
- package/dist-standalone/registry-middleware.d.ts +38 -0
- package/dist-standalone/registry-middleware.js +47 -0
- package/dist-standalone/retry-transport.d.ts +76 -0
- package/dist-standalone/retry-transport.js +98 -0
- package/dist-standalone/security-policy.d.ts +146 -0
- package/dist-standalone/security-policy.js +198 -0
- package/dist-standalone/split-channel.d.ts +69 -0
- package/dist-standalone/split-channel.js +171 -0
- package/dist-standalone/subscription-proof.d.ts +103 -0
- package/dist-standalone/subscription-proof.js +224 -0
- package/dist-standalone/succession.d.ts +57 -0
- package/dist-standalone/succession.js +142 -0
- package/dist-standalone/transport.d.ts +50 -0
- package/dist-standalone/transport.js +59 -0
- package/dist-standalone/trust-registry.d.ts +286 -0
- package/dist-standalone/trust-registry.js +702 -0
- package/dist-standalone/verify.d.ts +16 -0
- package/dist-standalone/verify.js +16 -0
- package/dist-standalone/xfetch.d.ts +129 -0
- package/dist-standalone/xfetch.js +247 -0
- package/llms.txt +800 -0
- package/package.json +79 -0
- package/share1.dat +0 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gateway transport adapter for delivering envelopes to Xail inbox users.
|
|
4
|
+
*
|
|
5
|
+
* Sends envelopes via POST /gateway/deliver on the hosted gateway API.
|
|
6
|
+
* For agent-to-agent (M2M), use HttpsTransportAdapter with direct URLs.
|
|
7
|
+
* Optionally polls for incoming messages.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.GatewayTransport = void 0;
|
|
11
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
12
|
+
/**
|
|
13
|
+
* Transport adapter for the Xail Gateway.
|
|
14
|
+
*
|
|
15
|
+
* Sends envelopes via POST /gateway/deliver with API key auth.
|
|
16
|
+
* Optionally polls GET /gateway/messages/:did for incoming messages.
|
|
17
|
+
*/
|
|
18
|
+
class GatewayTransport {
|
|
19
|
+
apiKey;
|
|
20
|
+
gateway;
|
|
21
|
+
timeoutMs;
|
|
22
|
+
fetchFn;
|
|
23
|
+
pollIntervalMs;
|
|
24
|
+
handlers = [];
|
|
25
|
+
pollTimer = null;
|
|
26
|
+
senderDid = null;
|
|
27
|
+
constructor(opts) {
|
|
28
|
+
this.apiKey = opts.apiKey;
|
|
29
|
+
this.gateway = opts.gateway.replace(/\/$/, '');
|
|
30
|
+
this.timeoutMs = opts.timeoutMs ?? 15_000;
|
|
31
|
+
this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
32
|
+
this.pollIntervalMs = opts.pollIntervalMs ?? 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Send a signed envelope to a recipient via the gateway.
|
|
36
|
+
* @param envelope - The signed transport envelope.
|
|
37
|
+
* @param _recipientDid - Recipient DID (already in envelope.recipient).
|
|
38
|
+
* @returns Success or transport error.
|
|
39
|
+
*/
|
|
40
|
+
async send(envelope, _recipientDid) {
|
|
41
|
+
const url = `${this.gateway}/gateway/deliver`;
|
|
42
|
+
try {
|
|
43
|
+
const controller = new AbortController();
|
|
44
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
45
|
+
const response = await this.fetchFn(url, {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(envelope),
|
|
52
|
+
signal: controller.signal,
|
|
53
|
+
});
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
return (0, shared_1.err)(response.status === 404
|
|
57
|
+
? 'RECIPIENT_UNREACHABLE'
|
|
58
|
+
: 'SEND_FAILED');
|
|
59
|
+
}
|
|
60
|
+
return (0, shared_1.ok)(undefined);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
if (e instanceof DOMException && e.name === 'AbortError') {
|
|
64
|
+
return (0, shared_1.err)('TIMEOUT');
|
|
65
|
+
}
|
|
66
|
+
return (0, shared_1.err)('NETWORK_ERROR');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Register a handler for incoming envelopes. */
|
|
70
|
+
onReceive(handler) {
|
|
71
|
+
this.handlers.push(handler);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Start polling for incoming messages.
|
|
75
|
+
* Must be called after Agent.create() sets the sender DID.
|
|
76
|
+
* @param did - The agent's own DID for polling.
|
|
77
|
+
*/
|
|
78
|
+
startPolling(did) {
|
|
79
|
+
this.senderDid = did;
|
|
80
|
+
if (this.pollIntervalMs > 0 && !this.pollTimer) {
|
|
81
|
+
this.pollTimer = setInterval(() => void this.poll(), this.pollIntervalMs);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Shut down the transport (stop polling, clear handlers). */
|
|
85
|
+
dispose() {
|
|
86
|
+
if (this.pollTimer) {
|
|
87
|
+
clearInterval(this.pollTimer);
|
|
88
|
+
this.pollTimer = null;
|
|
89
|
+
}
|
|
90
|
+
this.handlers = [];
|
|
91
|
+
}
|
|
92
|
+
/** Poll for incoming messages and dispatch to handlers. */
|
|
93
|
+
async poll() {
|
|
94
|
+
if (!this.senderDid)
|
|
95
|
+
return;
|
|
96
|
+
try {
|
|
97
|
+
const url = `${this.gateway}/gateway/messages/${encodeURIComponent(this.senderDid)}`;
|
|
98
|
+
const res = await this.fetchFn(url, {
|
|
99
|
+
headers: { Authorization: `Bearer ${this.apiKey}` },
|
|
100
|
+
});
|
|
101
|
+
if (!res.ok)
|
|
102
|
+
return;
|
|
103
|
+
const data = (await res.json());
|
|
104
|
+
for (const msg of data.messages) {
|
|
105
|
+
for (const handler of this.handlers) {
|
|
106
|
+
handler(msg.envelope);
|
|
107
|
+
}
|
|
108
|
+
// Ack delivery
|
|
109
|
+
await this.fetchFn(`${this.gateway}/gateway/ack/${msg.id}`, {
|
|
110
|
+
method: 'POST',
|
|
111
|
+
headers: { Authorization: `Bearer ${this.apiKey}` },
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// Silent failure — polling errors are non-fatal
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.GatewayTransport = GatewayTransport;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module guardrails
|
|
4
|
+
* Enhanced error messages with actionable suggestions for policy violations
|
|
5
|
+
*
|
|
6
|
+
* When policies deny requests, guardrails provide specific, actionable
|
|
7
|
+
* guidance on how to fix the issue or what to request from admins.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.Guardrails = exports.PolicyDenied = void 0;
|
|
11
|
+
exports.extractSuggestion = extractSuggestion;
|
|
12
|
+
exports.toPolicyDenied = toPolicyDenied;
|
|
13
|
+
/**
|
|
14
|
+
* Policy violation error with actionable suggestions
|
|
15
|
+
*/
|
|
16
|
+
class PolicyDenied extends Error {
|
|
17
|
+
rule;
|
|
18
|
+
allowed;
|
|
19
|
+
requested;
|
|
20
|
+
suggestion;
|
|
21
|
+
details;
|
|
22
|
+
constructor(
|
|
23
|
+
/** Which policy rule was violated */
|
|
24
|
+
rule,
|
|
25
|
+
/** What the policy allows */
|
|
26
|
+
allowed,
|
|
27
|
+
/** What was requested */
|
|
28
|
+
requested,
|
|
29
|
+
/** Actionable suggestion for how to fix */
|
|
30
|
+
suggestion,
|
|
31
|
+
/** Full policy violation details */
|
|
32
|
+
details) {
|
|
33
|
+
super(`Policy violation: ${rule}`);
|
|
34
|
+
this.rule = rule;
|
|
35
|
+
this.allowed = allowed;
|
|
36
|
+
this.requested = requested;
|
|
37
|
+
this.suggestion = suggestion;
|
|
38
|
+
this.details = details;
|
|
39
|
+
this.name = 'PolicyDenied';
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Format error for AI agent consumption
|
|
43
|
+
*
|
|
44
|
+
* Structured format optimized for LLM parsing
|
|
45
|
+
*/
|
|
46
|
+
toAgentFormat() {
|
|
47
|
+
return [
|
|
48
|
+
`POLICY_VIOLATION: ${this.rule}`,
|
|
49
|
+
`REQUESTED: ${JSON.stringify(this.requested)}`,
|
|
50
|
+
`ALLOWED: ${JSON.stringify(this.allowed)}`,
|
|
51
|
+
`SUGGESTION: ${this.suggestion}`,
|
|
52
|
+
].join('\n');
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Format error for human consumption
|
|
56
|
+
*
|
|
57
|
+
* User-friendly format with clear next steps
|
|
58
|
+
*/
|
|
59
|
+
toUserFormat() {
|
|
60
|
+
return [
|
|
61
|
+
`❌ Policy Violation: ${this.rule}`,
|
|
62
|
+
'',
|
|
63
|
+
`You requested: ${this.formatValue(this.requested)}`,
|
|
64
|
+
`Policy allows: ${this.formatValue(this.allowed)}`,
|
|
65
|
+
'',
|
|
66
|
+
`💡 Suggestion: ${this.suggestion}`,
|
|
67
|
+
].join('\n');
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Format error for logs (structured JSON)
|
|
71
|
+
*/
|
|
72
|
+
toLogFormat() {
|
|
73
|
+
return {
|
|
74
|
+
error: 'PolicyDenied',
|
|
75
|
+
rule: this.rule,
|
|
76
|
+
allowed: this.allowed,
|
|
77
|
+
requested: this.requested,
|
|
78
|
+
suggestion: this.suggestion,
|
|
79
|
+
details: this.details,
|
|
80
|
+
timestamp: new Date().toISOString(),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
formatValue(value) {
|
|
84
|
+
if (typeof value === 'number') {
|
|
85
|
+
// Format currency if it looks like money
|
|
86
|
+
if (this.rule.includes('amount') || this.rule.includes('Amount')) {
|
|
87
|
+
return `$${value.toLocaleString()}`;
|
|
88
|
+
}
|
|
89
|
+
return value.toLocaleString();
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(value)) {
|
|
92
|
+
return value.map((v) => `"${String(v)}"`).join(', ');
|
|
93
|
+
}
|
|
94
|
+
return JSON.stringify(value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.PolicyDenied = PolicyDenied;
|
|
98
|
+
/**
|
|
99
|
+
* Guardrail builder - fluent API for creating policy errors with suggestions
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const error = Guardrails.amountExceeded({
|
|
104
|
+
* requested: 5000,
|
|
105
|
+
* limit: 1000,
|
|
106
|
+
* type: 'per-transaction'
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* throw error;
|
|
110
|
+
* // ❌ Policy Violation: maxAmount
|
|
111
|
+
* // You requested: $5,000
|
|
112
|
+
* // Policy allows: $1,000
|
|
113
|
+
* // 💡 Suggestion: Reduce amount to $1,000 or request policy update from admin
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
class Guardrails {
|
|
117
|
+
/**
|
|
118
|
+
* Amount exceeded (per-transaction limit)
|
|
119
|
+
*/
|
|
120
|
+
static amountExceeded(options) {
|
|
121
|
+
let rule;
|
|
122
|
+
let suggestion;
|
|
123
|
+
switch (options.type) {
|
|
124
|
+
case 'per-transaction':
|
|
125
|
+
rule = 'maxAmount';
|
|
126
|
+
suggestion = `Reduce amount to $${options.limit.toLocaleString()} or request policy update from admin`;
|
|
127
|
+
break;
|
|
128
|
+
case 'daily':
|
|
129
|
+
rule = 'dailyAmount';
|
|
130
|
+
suggestion = `Reduce amount to stay within daily limit of $${options.limit.toLocaleString()}, or wait until tomorrow`;
|
|
131
|
+
break;
|
|
132
|
+
case 'monthly':
|
|
133
|
+
rule = 'monthlyAmount';
|
|
134
|
+
suggestion = `Reduce amount to stay within monthly limit of $${options.limit.toLocaleString()}, or wait until next month`;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
return new PolicyDenied(rule, options.limit, options.requested, suggestion);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Rate limit exceeded
|
|
141
|
+
*/
|
|
142
|
+
static rateLimitExceeded(options) {
|
|
143
|
+
const windowSec = options.windowMs / 1000;
|
|
144
|
+
const suggestion = windowSec >= 60
|
|
145
|
+
? `Wait ${Math.ceil(windowSec / 60)} minutes before making additional calls`
|
|
146
|
+
: `Wait ${windowSec} seconds before making additional calls`;
|
|
147
|
+
return new PolicyDenied('callsPerMinute', options.limit, options.current + 1, suggestion);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Tool not allowed
|
|
151
|
+
*/
|
|
152
|
+
static toolDenied(options) {
|
|
153
|
+
const [service] = options.tool.split(':');
|
|
154
|
+
const suggestion = options.allowed.length === 0
|
|
155
|
+
? `No tools are currently allowed. Request approval from admin to enable "${options.tool}"`
|
|
156
|
+
: `Request approval from admin to add "${options.tool}" or "${service}:*" to allowed tools`;
|
|
157
|
+
return new PolicyDenied('allowedTools', options.allowed, options.tool, suggestion);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Scope not allowed
|
|
161
|
+
*/
|
|
162
|
+
static scopeDenied(options) {
|
|
163
|
+
const suggestion = options.allowed.length === 0
|
|
164
|
+
? `No scopes are currently allowed. Request approval from admin to enable "${options.scope}"`
|
|
165
|
+
: `Request approval from admin to add "${options.scope}" to allowed scopes`;
|
|
166
|
+
return new PolicyDenied('allowedScopes', options.allowed, options.scope, suggestion);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Field filter denied (data access restriction)
|
|
170
|
+
*/
|
|
171
|
+
static fieldDenied(options) {
|
|
172
|
+
const suggestion = `Field "${options.field}" is restricted. Contact admin to update policy.fieldFilters for "${options.tool}"`;
|
|
173
|
+
return new PolicyDenied('fieldFilters', options.allowed, options.field, suggestion);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Time window restriction
|
|
177
|
+
*/
|
|
178
|
+
static timeWindowDenied(options) {
|
|
179
|
+
const suggestion = `Actions are only allowed between ${options.allowedStart} and ${options.allowedEnd}. Current time is ${options.current.toTimeString()}`;
|
|
180
|
+
return new PolicyDenied('timeWindow', `${options.allowedStart} - ${options.allowedEnd}`, options.current.toTimeString(), suggestion);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Generic policy violation
|
|
184
|
+
*/
|
|
185
|
+
static custom(options) {
|
|
186
|
+
return new PolicyDenied(options.rule, options.allowed, options.requested, options.suggestion);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
exports.Guardrails = Guardrails;
|
|
190
|
+
/**
|
|
191
|
+
* Helper: Extract actionable suggestion from AgentError details
|
|
192
|
+
*
|
|
193
|
+
* Used by agent.call() to convert PolicyViolationDetails into PolicyDenied
|
|
194
|
+
*/
|
|
195
|
+
function extractSuggestion(details) {
|
|
196
|
+
return details.fix ?? 'Contact administrator to update policy';
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Helper: Convert PolicyViolationDetails to PolicyDenied
|
|
200
|
+
*/
|
|
201
|
+
function toPolicyDenied(details) {
|
|
202
|
+
let rule;
|
|
203
|
+
switch (details.constraint) {
|
|
204
|
+
case 'amountPerTxn':
|
|
205
|
+
rule = 'maxAmount';
|
|
206
|
+
break;
|
|
207
|
+
case 'dailyAmount':
|
|
208
|
+
rule = 'dailyAmount';
|
|
209
|
+
break;
|
|
210
|
+
case 'callsPerMinute':
|
|
211
|
+
rule = 'callsPerMinute';
|
|
212
|
+
break;
|
|
213
|
+
case 'scope':
|
|
214
|
+
rule = 'allowedScopes';
|
|
215
|
+
break;
|
|
216
|
+
case 'tool':
|
|
217
|
+
rule = 'allowedTools';
|
|
218
|
+
break;
|
|
219
|
+
default:
|
|
220
|
+
rule = String(details.constraint);
|
|
221
|
+
}
|
|
222
|
+
return new PolicyDenied(rule, details.allowed, details.requested, details.fix, details);
|
|
223
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module http-compat
|
|
4
|
+
* HTTP client compatibility adapters for xBind auto-upgrade
|
|
5
|
+
*
|
|
6
|
+
* Provides drop-in replacements for popular HTTP clients (fetch, axios, got)
|
|
7
|
+
* that transparently upgrade to xBind when both parties support it.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.createAxiosCompat = createAxiosCompat;
|
|
11
|
+
exports.createGotCompat = createGotCompat;
|
|
12
|
+
exports.wrapFetch = wrapFetch;
|
|
13
|
+
const xfetch_js_1 = require("./xfetch.js");
|
|
14
|
+
/**
|
|
15
|
+
* Create axios-compatible HTTP client with xBind auto-upgrade
|
|
16
|
+
*
|
|
17
|
+
* Drop-in replacement for axios that transparently upgrades to xBind.
|
|
18
|
+
*
|
|
19
|
+
* @returns Axios-compatible client
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { createAxiosCompat } from '@private.me/xbind/http-compat';
|
|
24
|
+
*
|
|
25
|
+
* const axios = createAxiosCompat();
|
|
26
|
+
*
|
|
27
|
+
* // Auto-upgrades to xBind if supported
|
|
28
|
+
* const response = await axios.get('https://api.example.com/data');
|
|
29
|
+
* console.log('Used xBind?', response.xbind?.used);
|
|
30
|
+
* console.log('Data:', response.data);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function createAxiosCompat() {
|
|
34
|
+
async function request(config) {
|
|
35
|
+
// Normalize config
|
|
36
|
+
const cfg = typeof config === 'string' ? { url: config } : config;
|
|
37
|
+
const method = cfg.method?.toUpperCase() ?? 'GET';
|
|
38
|
+
const url = cfg.baseURL
|
|
39
|
+
? new URL(cfg.url ?? '', cfg.baseURL).toString()
|
|
40
|
+
: cfg.url ?? '';
|
|
41
|
+
// Build headers
|
|
42
|
+
const headers = { ...cfg.headers };
|
|
43
|
+
// Build URL with query params
|
|
44
|
+
let finalUrl = url;
|
|
45
|
+
if (cfg.params) {
|
|
46
|
+
const params = new URLSearchParams();
|
|
47
|
+
for (const [key, value] of Object.entries(cfg.params)) {
|
|
48
|
+
params.append(key, String(value));
|
|
49
|
+
}
|
|
50
|
+
finalUrl = `${url}?${params.toString()}`;
|
|
51
|
+
}
|
|
52
|
+
// Build request body
|
|
53
|
+
let body;
|
|
54
|
+
if (cfg.data) {
|
|
55
|
+
if (typeof cfg.data === 'string') {
|
|
56
|
+
body = cfg.data;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
body = JSON.stringify(cfg.data);
|
|
60
|
+
headers['Content-Type'] = 'application/json';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Convert to xfetch options
|
|
64
|
+
const xfetchOpts = {
|
|
65
|
+
method,
|
|
66
|
+
headers,
|
|
67
|
+
body,
|
|
68
|
+
timeout: cfg.timeout,
|
|
69
|
+
forceXBind: cfg.forceXBind,
|
|
70
|
+
disableXBind: cfg.disableXBind,
|
|
71
|
+
};
|
|
72
|
+
// Make request via xfetch
|
|
73
|
+
const response = await (0, xfetch_js_1.xfetch)(finalUrl, xfetchOpts);
|
|
74
|
+
// Parse response body based on responseType
|
|
75
|
+
let data;
|
|
76
|
+
const responseType = cfg.responseType ?? 'json';
|
|
77
|
+
switch (responseType) {
|
|
78
|
+
case 'json':
|
|
79
|
+
data = (await response.json());
|
|
80
|
+
break;
|
|
81
|
+
case 'text':
|
|
82
|
+
data = (await response.text());
|
|
83
|
+
break;
|
|
84
|
+
case 'blob':
|
|
85
|
+
data = (await response.blob());
|
|
86
|
+
break;
|
|
87
|
+
case 'arraybuffer':
|
|
88
|
+
data = (await response.arrayBuffer());
|
|
89
|
+
break;
|
|
90
|
+
default:
|
|
91
|
+
data = (await response.json());
|
|
92
|
+
}
|
|
93
|
+
// Validate status if custom validator provided
|
|
94
|
+
if (cfg.validateStatus && !cfg.validateStatus(response.status)) {
|
|
95
|
+
throw new Error(`Request failed with status ${response.status}`);
|
|
96
|
+
}
|
|
97
|
+
// Convert headers to object
|
|
98
|
+
const responseHeaders = {};
|
|
99
|
+
response.headers.forEach((value, key) => {
|
|
100
|
+
responseHeaders[key] = value;
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
data,
|
|
104
|
+
status: response.status,
|
|
105
|
+
statusText: response.statusText,
|
|
106
|
+
headers: responseHeaders,
|
|
107
|
+
config: cfg,
|
|
108
|
+
xbind: {
|
|
109
|
+
used: response.usedXBind,
|
|
110
|
+
protocol: response.transport.protocol,
|
|
111
|
+
latency: response.transport.latency,
|
|
112
|
+
peerDID: response.transport.peerDID,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// Convenience methods
|
|
117
|
+
async function get(url, config) {
|
|
118
|
+
return request({ ...config, url, method: 'GET' });
|
|
119
|
+
}
|
|
120
|
+
async function post(url, data, config) {
|
|
121
|
+
return request({ ...config, url, method: 'POST', data });
|
|
122
|
+
}
|
|
123
|
+
async function put(url, data, config) {
|
|
124
|
+
return request({ ...config, url, method: 'PUT', data });
|
|
125
|
+
}
|
|
126
|
+
async function patch(url, data, config) {
|
|
127
|
+
return request({ ...config, url, method: 'PATCH', data });
|
|
128
|
+
}
|
|
129
|
+
async function del(url, config) {
|
|
130
|
+
return request({ ...config, url, method: 'DELETE' });
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
request,
|
|
134
|
+
get,
|
|
135
|
+
post,
|
|
136
|
+
put,
|
|
137
|
+
patch,
|
|
138
|
+
delete: del,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Create got-compatible HTTP client with xBind auto-upgrade
|
|
143
|
+
*
|
|
144
|
+
* Drop-in replacement for got that transparently upgrades to xBind.
|
|
145
|
+
*
|
|
146
|
+
* @returns Got-compatible client
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* import { createGotCompat } from '@private.me/xbind/http-compat';
|
|
151
|
+
*
|
|
152
|
+
* const got = createGotCompat();
|
|
153
|
+
*
|
|
154
|
+
* // Auto-upgrades to xBind if supported
|
|
155
|
+
* const response = await got('https://api.example.com/data', {
|
|
156
|
+
* responseType: 'json',
|
|
157
|
+
* });
|
|
158
|
+
* console.log('Used xBind?', response.xbind?.used);
|
|
159
|
+
* console.log('Body:', response.body);
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
function createGotCompat() {
|
|
163
|
+
async function got(url, options) {
|
|
164
|
+
// Normalize options
|
|
165
|
+
const opts = typeof url === 'string' ? { ...options, url } : url;
|
|
166
|
+
const method = opts.method?.toUpperCase() ?? 'GET';
|
|
167
|
+
const finalUrl = opts.url ?? '';
|
|
168
|
+
// Build headers
|
|
169
|
+
const headers = { ...opts.headers };
|
|
170
|
+
// Build URL with search params
|
|
171
|
+
let urlWithParams = finalUrl;
|
|
172
|
+
if (opts.searchParams) {
|
|
173
|
+
const params = new URLSearchParams();
|
|
174
|
+
for (const [key, value] of Object.entries(opts.searchParams)) {
|
|
175
|
+
params.append(key, String(value));
|
|
176
|
+
}
|
|
177
|
+
urlWithParams = `${finalUrl}?${params.toString()}`;
|
|
178
|
+
}
|
|
179
|
+
// Build request body
|
|
180
|
+
let body;
|
|
181
|
+
if (opts.json) {
|
|
182
|
+
body = JSON.stringify(opts.json);
|
|
183
|
+
headers['Content-Type'] = 'application/json';
|
|
184
|
+
}
|
|
185
|
+
else if (opts.body) {
|
|
186
|
+
// Convert Buffer to string for compatibility with xfetch
|
|
187
|
+
// SAFETY: After instanceof check, both branches produce string type
|
|
188
|
+
body = (opts.body instanceof Buffer ? opts.body.toString() : opts.body);
|
|
189
|
+
}
|
|
190
|
+
// Extract timeout
|
|
191
|
+
const timeout = typeof opts.timeout === 'number'
|
|
192
|
+
? opts.timeout
|
|
193
|
+
: opts.timeout?.request;
|
|
194
|
+
// Convert to xfetch options
|
|
195
|
+
const xfetchOpts = {
|
|
196
|
+
method,
|
|
197
|
+
headers,
|
|
198
|
+
body,
|
|
199
|
+
timeout,
|
|
200
|
+
forceXBind: opts.forceXBind,
|
|
201
|
+
disableXBind: opts.disableXBind,
|
|
202
|
+
};
|
|
203
|
+
// Make request via xfetch
|
|
204
|
+
const response = await (0, xfetch_js_1.xfetch)(urlWithParams, xfetchOpts);
|
|
205
|
+
// Parse response body based on responseType
|
|
206
|
+
let responseBody;
|
|
207
|
+
const responseType = opts.responseType ?? 'text';
|
|
208
|
+
switch (responseType) {
|
|
209
|
+
case 'json':
|
|
210
|
+
responseBody = (await response.json());
|
|
211
|
+
break;
|
|
212
|
+
case 'text':
|
|
213
|
+
responseBody = (await response.text());
|
|
214
|
+
break;
|
|
215
|
+
case 'buffer':
|
|
216
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
217
|
+
responseBody = Buffer.from(arrayBuffer);
|
|
218
|
+
break;
|
|
219
|
+
default:
|
|
220
|
+
responseBody = (await response.text());
|
|
221
|
+
}
|
|
222
|
+
// Convert headers to object
|
|
223
|
+
const responseHeaders = {};
|
|
224
|
+
response.headers.forEach((value, key) => {
|
|
225
|
+
responseHeaders[key] = value;
|
|
226
|
+
});
|
|
227
|
+
return {
|
|
228
|
+
body: responseBody,
|
|
229
|
+
statusCode: response.status,
|
|
230
|
+
headers: responseHeaders,
|
|
231
|
+
url: response.url,
|
|
232
|
+
requestUrl: urlWithParams,
|
|
233
|
+
xbind: {
|
|
234
|
+
used: response.usedXBind,
|
|
235
|
+
protocol: response.transport.protocol,
|
|
236
|
+
latency: response.transport.latency,
|
|
237
|
+
peerDID: response.transport.peerDID,
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
// Convenience methods
|
|
242
|
+
got.get = (url, options) => got(url, { ...options, method: 'GET' });
|
|
243
|
+
got.post = (url, options) => got(url, { ...options, method: 'POST' });
|
|
244
|
+
got.put = (url, options) => got(url, { ...options, method: 'PUT' });
|
|
245
|
+
got.patch = (url, options) => got(url, { ...options, method: 'PATCH' });
|
|
246
|
+
got.delete = (url, options) => got(url, { ...options, method: 'DELETE' });
|
|
247
|
+
return got;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Wrap existing fetch implementation with xBind auto-upgrade
|
|
251
|
+
*
|
|
252
|
+
* Use this to upgrade an existing fetch instance (like node-fetch, cross-fetch, etc.)
|
|
253
|
+
*
|
|
254
|
+
* @param fetchImpl - Fetch implementation to wrap
|
|
255
|
+
* @returns Wrapped fetch with xBind support
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* import { wrapFetch } from '@private.me/xbind/http-compat';
|
|
260
|
+
* import nodeFetch from 'node-fetch';
|
|
261
|
+
*
|
|
262
|
+
* const fetch = wrapFetch(nodeFetch);
|
|
263
|
+
*
|
|
264
|
+
* // Uses xBind if supported, falls back to node-fetch
|
|
265
|
+
* const response = await fetch('https://api.example.com/data');
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
function wrapFetch(fetchImpl) {
|
|
269
|
+
// Return xfetch (which already handles auto-upgrade and fallback)
|
|
270
|
+
// The original fetchImpl is not used because xfetch uses global fetch
|
|
271
|
+
return xfetch_js_1.xfetch;
|
|
272
|
+
}
|