@toon-protocol/client 3.1.0 → 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-M54FLOSJ.js → chunk-ZHWYFPOP.js} +74 -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-WC6UGT6C.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-M54FLOSJ.js.map +0 -1
- /package/dist/{client-WC6UGT6C.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) {
|
|
@@ -6629,6 +6655,7 @@ var ClientChannelFacade = class {
|
|
|
6629
6655
|
const existing = channels.resolveChannel(config.connector, terms);
|
|
6630
6656
|
if (existing !== void 0) {
|
|
6631
6657
|
this.current = { channelId: existing, terms };
|
|
6658
|
+
this.adoptOnChain(existing, terms);
|
|
6632
6659
|
return existing;
|
|
6633
6660
|
}
|
|
6634
6661
|
if (!config.autoOpenChannel) {
|
|
@@ -7629,7 +7656,8 @@ async function send(context, destination, request = {}, options = {}) {
|
|
|
7629
7656
|
options.amount,
|
|
7630
7657
|
exchange.data.length
|
|
7631
7658
|
);
|
|
7632
|
-
|
|
7659
|
+
refuseIfCallerSaysSo(options.beforePay, destination, amount, request);
|
|
7660
|
+
const carriage = await context.transport(description, destination);
|
|
7633
7661
|
const first = await attempt(context, {
|
|
7634
7662
|
destination,
|
|
7635
7663
|
amount,
|
|
@@ -7648,6 +7676,12 @@ async function send(context, destination, request = {}, options = {}) {
|
|
|
7648
7676
|
});
|
|
7649
7677
|
return retry ?? first.result;
|
|
7650
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
|
+
}
|
|
7651
7685
|
async function attempt(context, params) {
|
|
7652
7686
|
if (params.amount === 0n) return attemptUnpaid(context, params);
|
|
7653
7687
|
const channelId = await context.ensureChannel(params.description);
|
|
@@ -7865,7 +7899,16 @@ var ToonClient = class _ToonClient {
|
|
|
7865
7899
|
channelFacade;
|
|
7866
7900
|
description;
|
|
7867
7901
|
onChain;
|
|
7868
|
-
|
|
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 = {};
|
|
7869
7912
|
btpSession;
|
|
7870
7913
|
hiddenService;
|
|
7871
7914
|
closed = false;
|
|
@@ -8093,7 +8136,7 @@ var ToonClient = class _ToonClient {
|
|
|
8093
8136
|
this.closed = true;
|
|
8094
8137
|
const session = this.btpSession;
|
|
8095
8138
|
this.btpSession = void 0;
|
|
8096
|
-
this.
|
|
8139
|
+
this.carriages = {};
|
|
8097
8140
|
if (session) await session.disconnect();
|
|
8098
8141
|
if (this.hiddenService) await this.hiddenService.close();
|
|
8099
8142
|
}
|
|
@@ -8127,7 +8170,7 @@ var ToonClient = class _ToonClient {
|
|
|
8127
8170
|
evictChannel: (channelId) => this.evictChannel(channelId),
|
|
8128
8171
|
channels: this.channels,
|
|
8129
8172
|
reconcileWatermark: (channelId) => this.reconcileWatermark(channelId),
|
|
8130
|
-
transport: (description) => this.transportFor(description),
|
|
8173
|
+
transport: (description, destination) => this.transportFor(description, destination),
|
|
8131
8174
|
senderId: this.identity.senderId,
|
|
8132
8175
|
chain: this.chain,
|
|
8133
8176
|
timeoutMs: this.config.timeoutMs
|
|
@@ -8252,19 +8295,26 @@ var ToonClient = class _ToonClient {
|
|
|
8252
8295
|
return key;
|
|
8253
8296
|
}
|
|
8254
8297
|
/**
|
|
8255
|
-
* The carriage
|
|
8298
|
+
* The carriage for `destination`, chosen once per kind and then reused.
|
|
8256
8299
|
*
|
|
8257
8300
|
* Reuse matters for BTP specifically: the whole reason to prefer it is that one
|
|
8258
8301
|
* ordered socket cannot race its own claim nonces into `F01 NonceNotAdvancing`
|
|
8259
8302
|
* (`client-edge-spec.md` §1.9), and a session rebuilt per request would give
|
|
8260
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.
|
|
8261
8310
|
*/
|
|
8262
|
-
async transportFor(description) {
|
|
8311
|
+
async transportFor(description, destination) {
|
|
8263
8312
|
if (this.closed) {
|
|
8264
8313
|
throw new ConfigError("This client has been closed; construct a new one to send again.");
|
|
8265
8314
|
}
|
|
8266
|
-
|
|
8267
|
-
const
|
|
8315
|
+
const choice = selectTransport(description, this.config.transport, void 0, destination);
|
|
8316
|
+
const cached = this.carriages[choice.kind];
|
|
8317
|
+
if (cached) return cached;
|
|
8268
8318
|
const httpEndpoint = choice.kind === "http" ? choice.url : httpEndpointOf(description, this.connector);
|
|
8269
8319
|
this.assertEndpointReachable(choice.url);
|
|
8270
8320
|
this.assertEndpointReachable(httpEndpoint);
|
|
@@ -8276,7 +8326,7 @@ var ToonClient = class _ToonClient {
|
|
|
8276
8326
|
});
|
|
8277
8327
|
if (choice.kind === "http") {
|
|
8278
8328
|
const carriage2 = { kind: "http", transport: http2 };
|
|
8279
|
-
this.
|
|
8329
|
+
this.carriages.http = carriage2;
|
|
8280
8330
|
return carriage2;
|
|
8281
8331
|
}
|
|
8282
8332
|
const session = new BtpRuntimeClient({
|
|
@@ -8296,15 +8346,18 @@ var ToonClient = class _ToonClient {
|
|
|
8296
8346
|
kind: "btp",
|
|
8297
8347
|
transport: new BtpPaidWriteTransport({
|
|
8298
8348
|
session,
|
|
8299
|
-
// HTTP fallback only where the node did not REQUIRE btp
|
|
8300
|
-
// onto a carriage the route refuses would
|
|
8301
|
-
// outage into a `402` per request.
|
|
8302
|
-
|
|
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 },
|
|
8303
8356
|
...this.config.btp.maxReconnectAttempts !== void 0 ? { maxReconnectAttempts: this.config.btp.maxReconnectAttempts } : {},
|
|
8304
8357
|
...this.config.btp.reconnectDelay !== void 0 ? { reconnectDelay: this.config.btp.reconnectDelay } : {}
|
|
8305
8358
|
})
|
|
8306
8359
|
};
|
|
8307
|
-
this.
|
|
8360
|
+
this.carriages.btp = carriage;
|
|
8308
8361
|
return carriage;
|
|
8309
8362
|
}
|
|
8310
8363
|
/**
|
|
@@ -8411,6 +8464,7 @@ export {
|
|
|
8411
8464
|
ChainUnavailableError,
|
|
8412
8465
|
ChannelNotOpenError,
|
|
8413
8466
|
RouteNotPricedError,
|
|
8467
|
+
BeforePayRefusedError,
|
|
8414
8468
|
PaymentRequiredError,
|
|
8415
8469
|
TransportRequiredError,
|
|
8416
8470
|
ChannelFundingError,
|
|
@@ -8558,4 +8612,4 @@ export {
|
|
|
8558
8612
|
toEnvelopeRequest,
|
|
8559
8613
|
ToonClient
|
|
8560
8614
|
};
|
|
8561
|
-
//# sourceMappingURL=chunk-
|
|
8615
|
+
//# sourceMappingURL=chunk-ZHWYFPOP.js.map
|