@toon-protocol/client 3.1.1 → 3.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/dist/{chunk-KQE6S23B.js → chunk-ZHWYFPOP.js} +73 -20
- package/dist/chunk-ZHWYFPOP.js.map +1 -0
- package/dist/cli/main.js +9 -2
- package/dist/cli/main.js.map +1 -1
- package/dist/{client-AV355FHQ.js → client-SCEP67S5.js} +4 -2
- package/dist/index.d.ts +147 -15
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-KQE6S23B.js.map +0 -1
- /package/dist/{client-AV355FHQ.js.map → client-SCEP67S5.js.map} +0 -0
|
@@ -66,6 +66,24 @@ var RouteNotPricedError = class extends ToonClientError {
|
|
|
66
66
|
this.name = "RouteNotPricedError";
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
|
+
var BeforePayRefusedError = class extends ToonClientError {
|
|
70
|
+
/** The reason the hook returned, verbatim. */
|
|
71
|
+
reason;
|
|
72
|
+
/** The route the refused packet was addressed to. */
|
|
73
|
+
destination;
|
|
74
|
+
/** The resolved price the hook was shown, in base units. */
|
|
75
|
+
amount;
|
|
76
|
+
constructor(reason, destination, amount) {
|
|
77
|
+
super(
|
|
78
|
+
`beforePay refused this send to "${destination}" for ${amount.toString()}: ${reason}`,
|
|
79
|
+
"BEFORE_PAY_REFUSED"
|
|
80
|
+
);
|
|
81
|
+
this.name = "BeforePayRefusedError";
|
|
82
|
+
this.reason = reason;
|
|
83
|
+
this.destination = destination;
|
|
84
|
+
this.amount = amount;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
69
87
|
var PaymentRequiredError = class extends ToonClientError {
|
|
70
88
|
/** The route's terms, parsed from the x402 challenge body. */
|
|
71
89
|
terms;
|
|
@@ -549,6 +567,9 @@ function readString(obj, key) {
|
|
|
549
567
|
const v = obj[key];
|
|
550
568
|
return typeof v === "string" && v.trim().length > 0 ? v.trim() : void 0;
|
|
551
569
|
}
|
|
570
|
+
function readRequiredTransport(value) {
|
|
571
|
+
return value === "http" || value === "btp" ? value : void 0;
|
|
572
|
+
}
|
|
552
573
|
function readBaseUnits(value) {
|
|
553
574
|
if (typeof value === "bigint") return value;
|
|
554
575
|
if (typeof value === "number" && Number.isFinite(value)) return BigInt(Math.trunc(value));
|
|
@@ -609,14 +630,15 @@ function parseSelfDescription(body, readFrom) {
|
|
|
609
630
|
const price = readBaseUnits(r["price"]);
|
|
610
631
|
if (prefix === void 0 || price === void 0) return void 0;
|
|
611
632
|
const pricePerKib = readBaseUnits(r["pricePerKib"] ?? r["price_per_kib"]);
|
|
633
|
+
const routeTransport = readRequiredTransport(r["requiredTransport"]);
|
|
612
634
|
return {
|
|
613
635
|
prefix,
|
|
614
636
|
price,
|
|
615
|
-
...pricePerKib !== void 0 ? { pricePerKib } : {}
|
|
637
|
+
...pricePerKib !== void 0 ? { pricePerKib } : {},
|
|
638
|
+
...routeTransport !== void 0 ? { requiredTransport: routeTransport } : {}
|
|
616
639
|
};
|
|
617
640
|
}).filter((r) => r !== void 0) : [];
|
|
618
|
-
const
|
|
619
|
-
const requiredTransport = required === "http" || required === "btp" ? required : void 0;
|
|
641
|
+
const requiredTransport = readRequiredTransport(b["requiredTransport"]);
|
|
620
642
|
const supportedVersions = Array.isArray(b["supportedVersions"]) ? b["supportedVersions"].filter((v) => typeof v === "number") : [1];
|
|
621
643
|
const defaultVersion = typeof b["defaultVersion"] === "number" ? b["defaultVersion"] : 1;
|
|
622
644
|
const httpEndpoint = readString(b, "httpEndpoint");
|
|
@@ -651,6 +673,10 @@ function routeFor(desc, destination) {
|
|
|
651
673
|
}
|
|
652
674
|
return best;
|
|
653
675
|
}
|
|
676
|
+
function requiredTransportFor(desc, destination) {
|
|
677
|
+
const route = destination !== void 0 ? routeFor(desc, destination) : void 0;
|
|
678
|
+
return route?.requiredTransport ?? desc.requiredTransport;
|
|
679
|
+
}
|
|
654
680
|
|
|
655
681
|
// src/utils/binary.ts
|
|
656
682
|
function toBase64(bytes) {
|
|
@@ -2658,10 +2684,10 @@ function parseConnectorRouteTerms(body) {
|
|
|
2658
2684
|
}
|
|
2659
2685
|
|
|
2660
2686
|
// src/btp/transport-select.ts
|
|
2661
|
-
function selectTransport(description, preference = "auto", baseUrl = description.readFrom) {
|
|
2687
|
+
function selectTransport(description, preference = "auto", baseUrl = description.readFrom, destination) {
|
|
2662
2688
|
const http2 = resolveEndpoint2(description.httpEndpoint, baseUrl);
|
|
2663
2689
|
const btp = resolveEndpoint2(description.btpEndpoint, baseUrl);
|
|
2664
|
-
const required = description
|
|
2690
|
+
const required = requiredTransportFor(description, destination);
|
|
2665
2691
|
if (preference === "http" || preference === "btp") {
|
|
2666
2692
|
const url = preference === "http" ? http2 : btp;
|
|
2667
2693
|
if (url === void 0) {
|
|
@@ -7630,7 +7656,8 @@ async function send(context, destination, request = {}, options = {}) {
|
|
|
7630
7656
|
options.amount,
|
|
7631
7657
|
exchange.data.length
|
|
7632
7658
|
);
|
|
7633
|
-
|
|
7659
|
+
refuseIfCallerSaysSo(options.beforePay, destination, amount, request);
|
|
7660
|
+
const carriage = await context.transport(description, destination);
|
|
7634
7661
|
const first = await attempt(context, {
|
|
7635
7662
|
destination,
|
|
7636
7663
|
amount,
|
|
@@ -7649,6 +7676,12 @@ async function send(context, destination, request = {}, options = {}) {
|
|
|
7649
7676
|
});
|
|
7650
7677
|
return retry ?? first.result;
|
|
7651
7678
|
}
|
|
7679
|
+
function refuseIfCallerSaysSo(beforePay, destination, amount, request) {
|
|
7680
|
+
if (beforePay === void 0) return;
|
|
7681
|
+
const reason = beforePay({ destination, amount, request });
|
|
7682
|
+
if (typeof reason !== "string") return;
|
|
7683
|
+
throw new BeforePayRefusedError(reason, destination, amount);
|
|
7684
|
+
}
|
|
7652
7685
|
async function attempt(context, params) {
|
|
7653
7686
|
if (params.amount === 0n) return attemptUnpaid(context, params);
|
|
7654
7687
|
const channelId = await context.ensureChannel(params.description);
|
|
@@ -7866,7 +7899,16 @@ var ToonClient = class _ToonClient {
|
|
|
7866
7899
|
channelFacade;
|
|
7867
7900
|
description;
|
|
7868
7901
|
onChain;
|
|
7869
|
-
|
|
7902
|
+
/**
|
|
7903
|
+
* One live carriage per kind, not one per client.
|
|
7904
|
+
*
|
|
7905
|
+
* A node pins carriages **per route** (connector ADR 0072), so one client can
|
|
7906
|
+
* legitimately owe one destination a BTP session and another an HTTP one-shot.
|
|
7907
|
+
* Keying by kind keeps each carriage chosen once and reused — which is the
|
|
7908
|
+
* point of caching at all — without letting the first destination decide for
|
|
7909
|
+
* every later one.
|
|
7910
|
+
*/
|
|
7911
|
+
carriages = {};
|
|
7870
7912
|
btpSession;
|
|
7871
7913
|
hiddenService;
|
|
7872
7914
|
closed = false;
|
|
@@ -8094,7 +8136,7 @@ var ToonClient = class _ToonClient {
|
|
|
8094
8136
|
this.closed = true;
|
|
8095
8137
|
const session = this.btpSession;
|
|
8096
8138
|
this.btpSession = void 0;
|
|
8097
|
-
this.
|
|
8139
|
+
this.carriages = {};
|
|
8098
8140
|
if (session) await session.disconnect();
|
|
8099
8141
|
if (this.hiddenService) await this.hiddenService.close();
|
|
8100
8142
|
}
|
|
@@ -8128,7 +8170,7 @@ var ToonClient = class _ToonClient {
|
|
|
8128
8170
|
evictChannel: (channelId) => this.evictChannel(channelId),
|
|
8129
8171
|
channels: this.channels,
|
|
8130
8172
|
reconcileWatermark: (channelId) => this.reconcileWatermark(channelId),
|
|
8131
|
-
transport: (description) => this.transportFor(description),
|
|
8173
|
+
transport: (description, destination) => this.transportFor(description, destination),
|
|
8132
8174
|
senderId: this.identity.senderId,
|
|
8133
8175
|
chain: this.chain,
|
|
8134
8176
|
timeoutMs: this.config.timeoutMs
|
|
@@ -8253,19 +8295,26 @@ var ToonClient = class _ToonClient {
|
|
|
8253
8295
|
return key;
|
|
8254
8296
|
}
|
|
8255
8297
|
/**
|
|
8256
|
-
* The carriage
|
|
8298
|
+
* The carriage for `destination`, chosen once per kind and then reused.
|
|
8257
8299
|
*
|
|
8258
8300
|
* Reuse matters for BTP specifically: the whole reason to prefer it is that one
|
|
8259
8301
|
* ordered socket cannot race its own claim nonces into `F01 NonceNotAdvancing`
|
|
8260
8302
|
* (`client-edge-spec.md` §1.9), and a session rebuilt per request would give
|
|
8261
8303
|
* that up while paying for the handshake.
|
|
8304
|
+
*
|
|
8305
|
+
* `destination` is what lets the node's **per-route** pin decide, rather than
|
|
8306
|
+
* only its node-wide summary — which is silent on any node that pins one of
|
|
8307
|
+
* its own addresses and not another (TOON_Network#111). The choice is made
|
|
8308
|
+
* before the first packet, so a pinned route is dialled correctly on the first
|
|
8309
|
+
* attempt and never learns its carriage from a refusal.
|
|
8262
8310
|
*/
|
|
8263
|
-
async transportFor(description) {
|
|
8311
|
+
async transportFor(description, destination) {
|
|
8264
8312
|
if (this.closed) {
|
|
8265
8313
|
throw new ConfigError("This client has been closed; construct a new one to send again.");
|
|
8266
8314
|
}
|
|
8267
|
-
|
|
8268
|
-
const
|
|
8315
|
+
const choice = selectTransport(description, this.config.transport, void 0, destination);
|
|
8316
|
+
const cached = this.carriages[choice.kind];
|
|
8317
|
+
if (cached) return cached;
|
|
8269
8318
|
const httpEndpoint = choice.kind === "http" ? choice.url : httpEndpointOf(description, this.connector);
|
|
8270
8319
|
this.assertEndpointReachable(choice.url);
|
|
8271
8320
|
this.assertEndpointReachable(httpEndpoint);
|
|
@@ -8277,7 +8326,7 @@ var ToonClient = class _ToonClient {
|
|
|
8277
8326
|
});
|
|
8278
8327
|
if (choice.kind === "http") {
|
|
8279
8328
|
const carriage2 = { kind: "http", transport: http2 };
|
|
8280
|
-
this.
|
|
8329
|
+
this.carriages.http = carriage2;
|
|
8281
8330
|
return carriage2;
|
|
8282
8331
|
}
|
|
8283
8332
|
const session = new BtpRuntimeClient({
|
|
@@ -8297,15 +8346,18 @@ var ToonClient = class _ToonClient {
|
|
|
8297
8346
|
kind: "btp",
|
|
8298
8347
|
transport: new BtpPaidWriteTransport({
|
|
8299
8348
|
session,
|
|
8300
|
-
// HTTP fallback only where the node did not REQUIRE btp
|
|
8301
|
-
// onto a carriage the route refuses would
|
|
8302
|
-
// outage into a `402` per request.
|
|
8303
|
-
|
|
8349
|
+
// HTTP fallback only where the node did not REQUIRE btp *for this
|
|
8350
|
+
// destination*: falling back onto a carriage the route refuses would
|
|
8351
|
+
// turn a recoverable socket outage into a `402` per request. Read
|
|
8352
|
+
// through the same per-route resolution the choice above was made with,
|
|
8353
|
+
// so the fallback cannot be enabled against a route the node-wide field
|
|
8354
|
+
// happens not to describe (TOON_Network#111).
|
|
8355
|
+
...requiredTransportFor(description, destination) === "btp" ? {} : { fallback: http2 },
|
|
8304
8356
|
...this.config.btp.maxReconnectAttempts !== void 0 ? { maxReconnectAttempts: this.config.btp.maxReconnectAttempts } : {},
|
|
8305
8357
|
...this.config.btp.reconnectDelay !== void 0 ? { reconnectDelay: this.config.btp.reconnectDelay } : {}
|
|
8306
8358
|
})
|
|
8307
8359
|
};
|
|
8308
|
-
this.
|
|
8360
|
+
this.carriages.btp = carriage;
|
|
8309
8361
|
return carriage;
|
|
8310
8362
|
}
|
|
8311
8363
|
/**
|
|
@@ -8412,6 +8464,7 @@ export {
|
|
|
8412
8464
|
ChainUnavailableError,
|
|
8413
8465
|
ChannelNotOpenError,
|
|
8414
8466
|
RouteNotPricedError,
|
|
8467
|
+
BeforePayRefusedError,
|
|
8415
8468
|
PaymentRequiredError,
|
|
8416
8469
|
TransportRequiredError,
|
|
8417
8470
|
ChannelFundingError,
|
|
@@ -8559,4 +8612,4 @@ export {
|
|
|
8559
8612
|
toEnvelopeRequest,
|
|
8560
8613
|
ToonClient
|
|
8561
8614
|
};
|
|
8562
|
-
//# sourceMappingURL=chunk-
|
|
8615
|
+
//# sourceMappingURL=chunk-ZHWYFPOP.js.map
|