@parallel-protocol/x402 0.9.0 → 0.10.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/README.md +57 -10
- package/dist/{chunk-TH77IB3T.cjs → chunk-MYCUOJ3H.cjs} +196 -21
- package/dist/{chunk-G7HSURPM.js → chunk-VSNDEQGS.js} +197 -22
- package/dist/express.cjs +5 -5
- package/dist/express.d.cts +1 -1
- package/dist/express.d.ts +1 -1
- package/dist/express.js +1 -1
- package/dist/fastify.cjs +11 -11
- package/dist/fastify.d.cts +1 -1
- package/dist/fastify.d.ts +1 -1
- package/dist/fastify.js +1 -1
- package/dist/hono.cjs +6 -6
- package/dist/hono.d.cts +1 -1
- package/dist/hono.d.ts +1 -1
- package/dist/hono.js +1 -1
- package/dist/index.cjs +15 -15
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/llm.d.cts +1 -1
- package/dist/llm.d.ts +1 -1
- package/dist/next.cjs +13 -13
- package/dist/next.d.cts +1 -1
- package/dist/next.d.ts +1 -1
- package/dist/next.js +1 -1
- package/dist/{types-BkGQ2lPA.d.cts → types-BWL9h1eY.d.cts} +11 -0
- package/dist/{types-BkGQ2lPA.d.ts → types-BWL9h1eY.d.ts} +11 -0
- package/package.json +3 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { X402_ERROR_CODES, X402ConfigError, X402RuntimeError, isPriceRejection } from './chunk-QAECRSWF.js';
|
|
2
|
-
import { isKnownChain, isActiveParallelizerChain, ACTIVE_PARALLELIZER_CHAINS, getTokenDecimals as getTokenDecimals$1 } from '@parallel-protocol/chains';
|
|
2
|
+
import { isKnownChain, isActiveParallelizerChain, ACTIVE_PARALLELIZER_CHAINS, getUptoEscrowAddress, getTokenDecimals as getTokenDecimals$1 } from '@parallel-protocol/chains';
|
|
3
|
+
import { permit2NonceToBytes32 } from '@parallel-protocol/payment-core';
|
|
3
4
|
import { z } from 'zod';
|
|
4
5
|
|
|
5
6
|
var PARALLEL_TOKENS = {
|
|
@@ -33,6 +34,9 @@ var PARALLEL_TOKENS = {
|
|
|
33
34
|
sUSDe: "0x211Cc4DD073734dA055fbF44a2b4667d5E5fE5d2"
|
|
34
35
|
}
|
|
35
36
|
};
|
|
37
|
+
var USDC_EIP712_DOMAIN = {
|
|
38
|
+
base: { name: "USD Coin", version: "2" }
|
|
39
|
+
};
|
|
36
40
|
function getDefaultAcceptedTokens(network) {
|
|
37
41
|
const entry = PARALLEL_TOKENS[network];
|
|
38
42
|
if (!entry) return void 0;
|
|
@@ -46,6 +50,12 @@ function getMeteredAcceptedTokens(network) {
|
|
|
46
50
|
function getTokenDecimals(network, address) {
|
|
47
51
|
return getTokenDecimals$1(network, address);
|
|
48
52
|
}
|
|
53
|
+
function hasMixedDecimals(network, tokens) {
|
|
54
|
+
const decimals = new Set(
|
|
55
|
+
tokens?.map((token) => getTokenDecimals$1(network, token))
|
|
56
|
+
);
|
|
57
|
+
return decimals.size > 1;
|
|
58
|
+
}
|
|
49
59
|
|
|
50
60
|
// src/utils.ts
|
|
51
61
|
var EIP155_NETWORKS = {
|
|
@@ -116,6 +126,114 @@ function matchRoute(path, routes) {
|
|
|
116
126
|
}
|
|
117
127
|
return bestMatch;
|
|
118
128
|
}
|
|
129
|
+
function permit2Extra(network, asset) {
|
|
130
|
+
const domain = USDC_EIP712_DOMAIN[network];
|
|
131
|
+
const escrow = getUptoEscrowAddress(network);
|
|
132
|
+
if (!domain || !escrow) return void 0;
|
|
133
|
+
const usdc = PARALLEL_TOKENS[network].usdc;
|
|
134
|
+
if (usdc.toLowerCase() !== asset.toLowerCase()) return void 0;
|
|
135
|
+
return { ...domain, facilitatorAddress: escrow };
|
|
136
|
+
}
|
|
137
|
+
var EIP2612_GAS_SPONSORING_EXTENSION = {
|
|
138
|
+
eip2612GasSponsoring: {
|
|
139
|
+
info: {
|
|
140
|
+
description: "The facilitator accepts EIP-2612 gasless Permit to `Permit2` canonical contract.",
|
|
141
|
+
version: "1"
|
|
142
|
+
},
|
|
143
|
+
schema: {
|
|
144
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
145
|
+
type: "object",
|
|
146
|
+
properties: {
|
|
147
|
+
from: {
|
|
148
|
+
type: "string",
|
|
149
|
+
pattern: "^0x[a-fA-F0-9]{40}$",
|
|
150
|
+
description: "The address of the sender."
|
|
151
|
+
},
|
|
152
|
+
asset: {
|
|
153
|
+
type: "string",
|
|
154
|
+
pattern: "^0x[a-fA-F0-9]{40}$",
|
|
155
|
+
description: "The address of the ERC-20 token contract."
|
|
156
|
+
},
|
|
157
|
+
spender: {
|
|
158
|
+
type: "string",
|
|
159
|
+
pattern: "^0x[a-fA-F0-9]{40}$",
|
|
160
|
+
description: "The address of the spender (Canonical Permit2)."
|
|
161
|
+
},
|
|
162
|
+
amount: {
|
|
163
|
+
type: "string",
|
|
164
|
+
pattern: "^[0-9]+$",
|
|
165
|
+
description: "The amount to approve (uint256). Typically MaxUint."
|
|
166
|
+
},
|
|
167
|
+
nonce: {
|
|
168
|
+
type: "string",
|
|
169
|
+
pattern: "^[0-9]+$",
|
|
170
|
+
description: "The current nonce of the sender."
|
|
171
|
+
},
|
|
172
|
+
deadline: {
|
|
173
|
+
type: "string",
|
|
174
|
+
pattern: "^[0-9]+$",
|
|
175
|
+
description: "The timestamp at which the signature expires."
|
|
176
|
+
},
|
|
177
|
+
signature: {
|
|
178
|
+
type: "string",
|
|
179
|
+
pattern: "^0x[a-fA-F0-9]+$",
|
|
180
|
+
description: "The 65-byte concatenated signature (r, s, v) as a hex string."
|
|
181
|
+
},
|
|
182
|
+
version: {
|
|
183
|
+
type: "string",
|
|
184
|
+
pattern: "^[0-9]+(\\.[0-9]+)*$",
|
|
185
|
+
description: "Schema version identifier."
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
required: [
|
|
189
|
+
"from",
|
|
190
|
+
"asset",
|
|
191
|
+
"spender",
|
|
192
|
+
"amount",
|
|
193
|
+
"nonce",
|
|
194
|
+
"deadline",
|
|
195
|
+
"signature",
|
|
196
|
+
"version"
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
function signedPermit(info = {}) {
|
|
202
|
+
const { amount, deadline, signature } = info;
|
|
203
|
+
return typeof amount === "string" && typeof deadline === "string" && typeof signature === "string" ? { value: amount, deadline, signature } : void 0;
|
|
204
|
+
}
|
|
205
|
+
function isStandardPermit2Payment(decoded) {
|
|
206
|
+
const payment = decoded;
|
|
207
|
+
return typeof payment?.payload?.permit2Authorization === "object" && payment.payload.permit2Authorization !== null && payment.accepted?.scheme === "upto";
|
|
208
|
+
}
|
|
209
|
+
function toFacilitatorPayload(decoded, network) {
|
|
210
|
+
if (!isStandardPermit2Payment(decoded)) return decoded;
|
|
211
|
+
const { signature, permit2Authorization: auth } = decoded.payload;
|
|
212
|
+
const permit = signedPermit(decoded.extensions?.eip2612GasSponsoring?.info);
|
|
213
|
+
return {
|
|
214
|
+
scheme: "upto",
|
|
215
|
+
network,
|
|
216
|
+
method: "uptoPermit2",
|
|
217
|
+
tokenIn: auth.permitted.token,
|
|
218
|
+
payload: {
|
|
219
|
+
signature,
|
|
220
|
+
authorization: {
|
|
221
|
+
from: auth.from,
|
|
222
|
+
to: auth.witness.to,
|
|
223
|
+
value: auth.permitted.amount,
|
|
224
|
+
validAfter: auth.witness.validAfter,
|
|
225
|
+
validBefore: auth.deadline,
|
|
226
|
+
nonce: permit2NonceToBytes32(auth.nonce)
|
|
227
|
+
},
|
|
228
|
+
...permit && { permit2612: permit }
|
|
229
|
+
},
|
|
230
|
+
uptoParams: { payTo: auth.witness.to },
|
|
231
|
+
permit2Params: {
|
|
232
|
+
spender: auth.spender,
|
|
233
|
+
facilitator: auth.witness.facilitator
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
119
237
|
var ethereumAddressSchema = z.string().regex(/^0x[0-9a-fA-F]{40}$/, "Invalid Ethereum address");
|
|
120
238
|
var txHashSchema = z.string().regex(/^0x[0-9a-fA-F]{64}$/);
|
|
121
239
|
var priceSchema = z.union([
|
|
@@ -150,6 +268,7 @@ var routeConfigSchema = z.object({
|
|
|
150
268
|
description: z.string().optional(),
|
|
151
269
|
scheme: z.enum(["exact", "upto"]).optional(),
|
|
152
270
|
maxTimeoutSeconds: z.number().int().positive().optional(),
|
|
271
|
+
decimals: z.number().int().nonnegative().optional(),
|
|
153
272
|
meter: z.custom(
|
|
154
273
|
(value) => typeof value === "function",
|
|
155
274
|
"meter must be a function of the response"
|
|
@@ -171,9 +290,12 @@ var routeConfigSchema = z.object({
|
|
|
171
290
|
const usdp = PARALLEL_TOKENS[route.network]?.usdp;
|
|
172
291
|
if (!usdp) return true;
|
|
173
292
|
return route.acceptedTokens.every(
|
|
174
|
-
(token) => token.toLowerCase() === usdp.toLowerCase()
|
|
293
|
+
(token) => token.toLowerCase() === usdp.toLowerCase() || permit2Extra(route.network, token) !== void 0
|
|
175
294
|
);
|
|
176
|
-
}, "acceptedTokens on an upto route may only list the network's USDp")
|
|
295
|
+
}, "acceptedTokens on an upto route may only list the network's USDp, and its USDC where the escrow takes Permit2 payments").refine(
|
|
296
|
+
(route) => route.scheme !== "upto" || !hasMixedDecimals(route.network, route.acceptedTokens) || typeof route.price !== "bigint" && route.decimals === void 0,
|
|
297
|
+
'An upto route that accepts tokens with different decimals needs a price like "0.01", not a bigint or an explicit decimals'
|
|
298
|
+
);
|
|
177
299
|
var paymentMiddlewareConfigSchema = z.object({
|
|
178
300
|
facilitator: facilitatorConfigSchema,
|
|
179
301
|
routes: z.record(z.string(), routeConfigSchema).refine((r) => Object.keys(r).length > 0, "At least one route is required")
|
|
@@ -210,6 +332,9 @@ var settleSuccessSchema = z.object({
|
|
|
210
332
|
gasSponsored: z.boolean(),
|
|
211
333
|
// Metered routes: the amount actually charged (smallest unit).
|
|
212
334
|
amountSettled: z.string().optional(),
|
|
335
|
+
// A standard x402 Permit2 payment: the USDp the merchant received for
|
|
336
|
+
// amountSettled (smallest unit), when the facilitator read it.
|
|
337
|
+
amountDelivered: z.string().optional(),
|
|
213
338
|
// The facilitator answered from a recorded outcome, nothing was broadcast.
|
|
214
339
|
alreadySettled: z.boolean().optional()
|
|
215
340
|
});
|
|
@@ -248,13 +373,17 @@ function throwIfRedirect(response) {
|
|
|
248
373
|
status: response.status
|
|
249
374
|
});
|
|
250
375
|
}
|
|
251
|
-
function decodePaymentHeader(paymentHeader) {
|
|
376
|
+
function decodePaymentHeader(paymentHeader, network) {
|
|
252
377
|
try {
|
|
253
|
-
return decodeBase64(paymentHeader);
|
|
378
|
+
return toFacilitatorPayload(decodeBase64(paymentHeader), network);
|
|
254
379
|
} catch {
|
|
255
380
|
throw new X402RuntimeError(X402_ERROR_CODES.INVALID_PAYMENT);
|
|
256
381
|
}
|
|
257
382
|
}
|
|
383
|
+
function payerOf(paymentPayload) {
|
|
384
|
+
const from = paymentPayload?.payload?.authorization?.from;
|
|
385
|
+
return typeof from === "string" ? from : void 0;
|
|
386
|
+
}
|
|
258
387
|
var FacilitatorClient = class {
|
|
259
388
|
// Checked and kept when the client is built: a config changed afterwards
|
|
260
389
|
// does not move the calls elsewhere.
|
|
@@ -272,7 +401,10 @@ var FacilitatorClient = class {
|
|
|
272
401
|
this.apiKey = config.apiKey;
|
|
273
402
|
}
|
|
274
403
|
async verify(paymentHeader, paymentRequirements) {
|
|
275
|
-
const paymentPayload = decodePaymentHeader(
|
|
404
|
+
const paymentPayload = decodePaymentHeader(
|
|
405
|
+
paymentHeader,
|
|
406
|
+
paymentRequirements.network
|
|
407
|
+
);
|
|
276
408
|
let json;
|
|
277
409
|
try {
|
|
278
410
|
const response = await fetch(`${this.url}/x402/verify`, {
|
|
@@ -307,7 +439,10 @@ var FacilitatorClient = class {
|
|
|
307
439
|
}
|
|
308
440
|
}
|
|
309
441
|
async settle(paymentHeader, paymentRequirements, options) {
|
|
310
|
-
const paymentPayload = decodePaymentHeader(
|
|
442
|
+
const paymentPayload = decodePaymentHeader(
|
|
443
|
+
paymentHeader,
|
|
444
|
+
paymentRequirements.network
|
|
445
|
+
);
|
|
311
446
|
let json;
|
|
312
447
|
try {
|
|
313
448
|
const response = await fetch(`${this.url}/x402/settle`, {
|
|
@@ -358,17 +493,27 @@ var FacilitatorClient = class {
|
|
|
358
493
|
try {
|
|
359
494
|
const parsed = settleResponseSchema.parse(json);
|
|
360
495
|
if ("txHash" in parsed) {
|
|
496
|
+
const txHash = parsed.txHash;
|
|
497
|
+
const networkId = toEip155Network(paymentRequirements.network);
|
|
498
|
+
const payer = payerOf(paymentPayload);
|
|
361
499
|
return {
|
|
362
500
|
success: true,
|
|
363
|
-
txHash
|
|
501
|
+
txHash,
|
|
364
502
|
route: parsed.route,
|
|
365
503
|
chain: paymentRequirements.network,
|
|
366
504
|
gasSponsored: parsed.gasSponsored,
|
|
367
|
-
networkId
|
|
505
|
+
networkId,
|
|
368
506
|
...parsed.amountSettled !== void 0 && {
|
|
369
507
|
amountSettled: parsed.amountSettled
|
|
370
508
|
},
|
|
371
|
-
...parsed.
|
|
509
|
+
...parsed.amountDelivered !== void 0 && {
|
|
510
|
+
amountDelivered: parsed.amountDelivered
|
|
511
|
+
},
|
|
512
|
+
...parsed.alreadySettled && { alreadySettled: true },
|
|
513
|
+
transaction: txHash,
|
|
514
|
+
network: networkId,
|
|
515
|
+
amount: parsed.amountSettled ?? paymentRequirements.maxAmountRequired,
|
|
516
|
+
...payer && { payer }
|
|
372
517
|
};
|
|
373
518
|
}
|
|
374
519
|
return {
|
|
@@ -447,7 +592,7 @@ async function requestContext(adapter) {
|
|
|
447
592
|
body: adapter.getBody ? await adapter.getBody() : void 0
|
|
448
593
|
};
|
|
449
594
|
}
|
|
450
|
-
async function resolvePrice(config, ctx) {
|
|
595
|
+
async function resolvePrice(config, ctx, tokens) {
|
|
451
596
|
if (typeof config.price !== "function") return config.price;
|
|
452
597
|
if (!ctx) throw new X402ConfigError("A price function needs the request");
|
|
453
598
|
let price;
|
|
@@ -468,6 +613,11 @@ async function resolvePrice(config, ctx) {
|
|
|
468
613
|
)} \u2014 expected a positive amount like "0.01" or a bigint`
|
|
469
614
|
);
|
|
470
615
|
}
|
|
616
|
+
if (typeof price === "bigint" && config.scheme === "upto" && hasMixedDecimals(config.network, tokens)) {
|
|
617
|
+
throw new X402ConfigError(
|
|
618
|
+
`The price function of route "${config.network}${ctx.path}" returned a bigint, but the route accepts tokens with different decimals: return a price like "0.01"`
|
|
619
|
+
);
|
|
620
|
+
}
|
|
471
621
|
return price;
|
|
472
622
|
}
|
|
473
623
|
var SETTLE_AMOUNT_HEADER = "payment-settle-amount";
|
|
@@ -519,6 +669,17 @@ function invalidSettleAmountResponse(reason) {
|
|
|
519
669
|
reason === "meter" ? INVALID_METER_RESPONSE : INVALID_SETTLE_AMOUNT_RESPONSE
|
|
520
670
|
);
|
|
521
671
|
}
|
|
672
|
+
function refusingBigints(meter) {
|
|
673
|
+
return async (ctx) => {
|
|
674
|
+
const amount = await meter(ctx);
|
|
675
|
+
if (typeof amount === "bigint") {
|
|
676
|
+
throw new X402ConfigError(
|
|
677
|
+
'The meter returned a bigint, but the route accepts tokens with different decimals: return an amount like "0.75"'
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
return amount;
|
|
681
|
+
};
|
|
682
|
+
}
|
|
522
683
|
var AMOUNT_FORMAT = /^\d+(?:\.\d+)?$/;
|
|
523
684
|
async function resolveCharge(verified, settleHeader, response) {
|
|
524
685
|
if (verified.meter) {
|
|
@@ -598,18 +759,27 @@ function priceInUnits(price, decimals, asset) {
|
|
|
598
759
|
}
|
|
599
760
|
function buildPaymentRequired(url, config, resolvedTokens, price) {
|
|
600
761
|
const network = toEip155Network(config.network);
|
|
601
|
-
const
|
|
762
|
+
const scheme = config.scheme ?? "exact";
|
|
763
|
+
const entries = resolvedTokens.map((asset) => {
|
|
602
764
|
const decimals = resolveDecimals(config, asset);
|
|
603
765
|
return {
|
|
604
|
-
scheme
|
|
766
|
+
scheme,
|
|
605
767
|
network,
|
|
606
768
|
asset,
|
|
607
769
|
amount: priceInUnits(price, decimals, asset),
|
|
608
770
|
payTo: config.payTo,
|
|
609
771
|
maxTimeoutSeconds: config.maxTimeoutSeconds ?? DEFAULT_MAX_TIMEOUT_SECONDS,
|
|
610
|
-
extra: {
|
|
772
|
+
extra: {
|
|
773
|
+
decimals,
|
|
774
|
+
...scheme === "upto" && permit2Extra(config.network, asset)
|
|
775
|
+
}
|
|
611
776
|
};
|
|
612
777
|
});
|
|
778
|
+
const viaPermit2 = (entry) => "facilitatorAddress" in entry.extra;
|
|
779
|
+
const accepts = [
|
|
780
|
+
...entries.filter(viaPermit2),
|
|
781
|
+
...entries.filter((entry) => !viaPermit2(entry))
|
|
782
|
+
];
|
|
613
783
|
return {
|
|
614
784
|
x402Version: 2,
|
|
615
785
|
resource: {
|
|
@@ -617,13 +787,17 @@ function buildPaymentRequired(url, config, resolvedTokens, price) {
|
|
|
617
787
|
description: config.description,
|
|
618
788
|
mimeType: "application/json"
|
|
619
789
|
},
|
|
620
|
-
accepts
|
|
790
|
+
accepts,
|
|
791
|
+
...accepts.some(viaPermit2) && {
|
|
792
|
+
extensions: EIP2612_GAS_SPONSORING_EXTENSION
|
|
793
|
+
}
|
|
621
794
|
};
|
|
622
795
|
}
|
|
623
|
-
function decodePaymentPayload(header) {
|
|
796
|
+
function decodePaymentPayload(header, network) {
|
|
624
797
|
try {
|
|
625
|
-
const parsed =
|
|
626
|
-
Buffer.from(header, "base64").toString("utf8")
|
|
798
|
+
const parsed = toFacilitatorPayload(
|
|
799
|
+
JSON.parse(Buffer.from(header, "base64").toString("utf8")),
|
|
800
|
+
network
|
|
627
801
|
);
|
|
628
802
|
return typeof parsed === "object" && parsed !== null ? parsed : void 0;
|
|
629
803
|
} catch {
|
|
@@ -642,7 +816,7 @@ function deliveredToken(payload, chainTokens) {
|
|
|
642
816
|
return payload.tokenIn ?? chainTokens?.usdp;
|
|
643
817
|
}
|
|
644
818
|
function resolveDeliveredAsset(header, config, resolvedTokens) {
|
|
645
|
-
const payload = decodePaymentPayload(header);
|
|
819
|
+
const payload = decodePaymentPayload(header, config.network);
|
|
646
820
|
const target = payload ? deliveredToken(
|
|
647
821
|
payload,
|
|
648
822
|
PARALLEL_TOKENS[config.network]
|
|
@@ -717,7 +891,7 @@ function createPaymentGate(config) {
|
|
|
717
891
|
let request = typeof routeConfig.price === "function" ? await requestContext(adapter) : void 0;
|
|
718
892
|
let price;
|
|
719
893
|
try {
|
|
720
|
-
price = await resolvePrice(routeConfig, request);
|
|
894
|
+
price = await resolvePrice(routeConfig, request, resolvedTokens);
|
|
721
895
|
} catch (err) {
|
|
722
896
|
if (!isPriceRejection(err)) throw err;
|
|
723
897
|
const status = Number.isInteger(err.status) && err.status >= 400 && err.status <= 599 ? err.status : 400;
|
|
@@ -768,7 +942,8 @@ function createPaymentGate(config) {
|
|
|
768
942
|
)
|
|
769
943
|
};
|
|
770
944
|
}
|
|
771
|
-
|
|
945
|
+
const meter = routeConfig.meter && hasMixedDecimals(routeConfig.network, resolvedTokens) ? refusingBigints(routeConfig.meter) : routeConfig.meter;
|
|
946
|
+
if (meter && !request) request = await requestContext(adapter);
|
|
772
947
|
return {
|
|
773
948
|
type: "verified",
|
|
774
949
|
scheme: routeConfig.scheme ?? "exact",
|
|
@@ -776,7 +951,7 @@ function createPaymentGate(config) {
|
|
|
776
951
|
decimals: resolveDecimals(routeConfig, asset),
|
|
777
952
|
handlerTimeoutMs: handlerTimeoutFor(routeConfig),
|
|
778
953
|
settle: (options) => client.settle(paymentHeader, requirements, { ...options, settleToken }),
|
|
779
|
-
...
|
|
954
|
+
...meter && { meter, request }
|
|
780
955
|
};
|
|
781
956
|
};
|
|
782
957
|
}
|
package/dist/express.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkMYCUOJ3H_cjs = require('./chunk-MYCUOJ3H.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
|
|
6
6
|
// src/express/adapter.ts
|
|
@@ -108,8 +108,8 @@ var ExpressAdapter = class {
|
|
|
108
108
|
return originalEnd(...args);
|
|
109
109
|
});
|
|
110
110
|
next();
|
|
111
|
-
const outcome = await
|
|
112
|
-
if (outcome ===
|
|
111
|
+
const outcome = await chunkMYCUOJ3H_cjs.raceTimeout(endPromise, options?.timeoutMs);
|
|
112
|
+
if (outcome === chunkMYCUOJ3H_cjs.HANDLER_TIMEOUT) {
|
|
113
113
|
discarded = true;
|
|
114
114
|
return {
|
|
115
115
|
status: 504,
|
|
@@ -146,7 +146,7 @@ var ExpressAdapter = class {
|
|
|
146
146
|
const chunk = chunkBytes(args);
|
|
147
147
|
if (chunk) bytes.push(chunk);
|
|
148
148
|
}
|
|
149
|
-
return
|
|
149
|
+
return chunkMYCUOJ3H_cjs.parseJsonBody(
|
|
150
150
|
contentType,
|
|
151
151
|
Buffer.concat(bytes).toString("utf8")
|
|
152
152
|
);
|
|
@@ -170,7 +170,7 @@ var ExpressAdapter = class {
|
|
|
170
170
|
|
|
171
171
|
// src/express/index.ts
|
|
172
172
|
function paymentMiddleware(config) {
|
|
173
|
-
const middleware =
|
|
173
|
+
const middleware = chunkMYCUOJ3H_cjs.createPaymentMiddleware(config);
|
|
174
174
|
return async (req, res, next) => {
|
|
175
175
|
const result = await middleware(new ExpressAdapter(req, res, next));
|
|
176
176
|
if ("pass" in result) {
|
package/dist/express.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-BWL9h1eY.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class ExpressAdapter implements HTTPAdapter {
|
package/dist/express.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-BWL9h1eY.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class ExpressAdapter implements HTTPAdapter {
|
package/dist/express.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { raceTimeout, HANDLER_TIMEOUT, parseJsonBody, createPaymentMiddleware } from './chunk-
|
|
1
|
+
import { raceTimeout, HANDLER_TIMEOUT, parseJsonBody, createPaymentMiddleware } from './chunk-VSNDEQGS.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
|
|
4
4
|
// src/express/adapter.ts
|
package/dist/fastify.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkMYCUOJ3H_cjs = require('./chunk-MYCUOJ3H.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
var fp = require('fastify-plugin');
|
|
6
6
|
|
|
@@ -37,7 +37,7 @@ var FastifyAdapter = class {
|
|
|
37
37
|
// src/fastify/index.ts
|
|
38
38
|
function payloadJson(contentType, payload) {
|
|
39
39
|
const text = typeof payload === "string" ? payload : payload instanceof Uint8Array ? Buffer.from(payload).toString("utf8") : void 0;
|
|
40
|
-
return
|
|
40
|
+
return chunkMYCUOJ3H_cjs.parseJsonBody(contentType, text);
|
|
41
41
|
}
|
|
42
42
|
function refuse(reply, result) {
|
|
43
43
|
const json = JSON.stringify(result.body);
|
|
@@ -54,11 +54,11 @@ function headerValue(value) {
|
|
|
54
54
|
}
|
|
55
55
|
function paymentMiddleware(config) {
|
|
56
56
|
return fp__default.default(async (fastify) => {
|
|
57
|
-
const gate =
|
|
57
|
+
const gate = chunkMYCUOJ3H_cjs.createPaymentGate(config);
|
|
58
58
|
const verifiedMap = /* @__PURE__ */ new WeakMap();
|
|
59
59
|
const rawWriters = /* @__PURE__ */ new WeakMap();
|
|
60
60
|
const needsBody = (req) => {
|
|
61
|
-
const route =
|
|
61
|
+
const route = chunkMYCUOJ3H_cjs.matchRoute(
|
|
62
62
|
new FastifyAdapter(req).getPath(),
|
|
63
63
|
config.routes
|
|
64
64
|
);
|
|
@@ -111,29 +111,29 @@ function paymentMiddleware(config) {
|
|
|
111
111
|
if (!verified) return payload;
|
|
112
112
|
let raw;
|
|
113
113
|
if (verified.scheme === "upto") {
|
|
114
|
-
raw = headerValue(reply.getHeader(
|
|
115
|
-
reply.removeHeader(
|
|
114
|
+
raw = headerValue(reply.getHeader(chunkMYCUOJ3H_cjs.SETTLE_AMOUNT_HEADER));
|
|
115
|
+
reply.removeHeader(chunkMYCUOJ3H_cjs.SETTLE_AMOUNT_HEADER);
|
|
116
116
|
}
|
|
117
117
|
if (reply.statusCode < 200 || reply.statusCode >= 300) {
|
|
118
118
|
return payload;
|
|
119
119
|
}
|
|
120
120
|
let settleOptions;
|
|
121
121
|
if (verified.scheme === "upto") {
|
|
122
|
-
const resolved = await
|
|
122
|
+
const resolved = await chunkMYCUOJ3H_cjs.resolveCharge(verified, raw, {
|
|
123
123
|
status: reply.statusCode,
|
|
124
124
|
header: (name) => headerValue(reply.getHeader(name)),
|
|
125
125
|
body: async () => payloadJson(headerValue(reply.getHeader("content-type")), payload)
|
|
126
126
|
});
|
|
127
127
|
if (resolved.kind === "invalid") {
|
|
128
|
-
return refuse(reply,
|
|
128
|
+
return refuse(reply, chunkMYCUOJ3H_cjs.invalidSettleAmountResponse(resolved.reason));
|
|
129
129
|
}
|
|
130
130
|
if (resolved.kind === "skip") return payload;
|
|
131
131
|
settleOptions = { amount: resolved.amount };
|
|
132
132
|
}
|
|
133
|
-
const outcome = await
|
|
133
|
+
const outcome = await chunkMYCUOJ3H_cjs.settleVerified(verified, settleOptions);
|
|
134
134
|
if (!outcome.settled)
|
|
135
|
-
return refuse(reply,
|
|
136
|
-
reply.header("payment-response",
|
|
135
|
+
return refuse(reply, chunkMYCUOJ3H_cjs.settlementFailureResponse(outcome));
|
|
136
|
+
reply.header("payment-response", chunkMYCUOJ3H_cjs.encodeBase64(outcome.confirmation));
|
|
137
137
|
reply.header("access-control-expose-headers", "payment-response");
|
|
138
138
|
return payload;
|
|
139
139
|
});
|
package/dist/fastify.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyPluginAsync } from 'fastify';
|
|
2
|
-
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-BWL9h1eY.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class FastifyAdapter implements HTTPAdapter {
|
package/dist/fastify.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyPluginAsync } from 'fastify';
|
|
2
|
-
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-BWL9h1eY.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class FastifyAdapter implements HTTPAdapter {
|
package/dist/fastify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createPaymentGate, SETTLE_AMOUNT_HEADER, resolveCharge, invalidSettleAmountResponse, settleVerified, settlementFailureResponse, encodeBase64, matchRoute, parseJsonBody } from './chunk-
|
|
1
|
+
import { createPaymentGate, SETTLE_AMOUNT_HEADER, resolveCharge, invalidSettleAmountResponse, settleVerified, settlementFailureResponse, encodeBase64, matchRoute, parseJsonBody } from './chunk-VSNDEQGS.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
import fp from 'fastify-plugin';
|
|
4
4
|
|
package/dist/hono.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkMYCUOJ3H_cjs = require('./chunk-MYCUOJ3H.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
|
|
6
6
|
// src/hono/adapter.ts
|
|
@@ -30,7 +30,7 @@ var HonoAdapter = class {
|
|
|
30
30
|
// Hono caches the parsed body on the request, so the handler can still read
|
|
31
31
|
// it after the price function did.
|
|
32
32
|
async getBody() {
|
|
33
|
-
if (!
|
|
33
|
+
if (!chunkMYCUOJ3H_cjs.isJsonContentType(this.c.req.header("content-type"))) return void 0;
|
|
34
34
|
try {
|
|
35
35
|
return await this.c.req.json();
|
|
36
36
|
} catch {
|
|
@@ -38,8 +38,8 @@ var HonoAdapter = class {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
async runHandler(options) {
|
|
41
|
-
const outcome = await
|
|
42
|
-
if (outcome ===
|
|
41
|
+
const outcome = await chunkMYCUOJ3H_cjs.raceTimeout(this.next(), options?.timeoutMs);
|
|
42
|
+
if (outcome === chunkMYCUOJ3H_cjs.HANDLER_TIMEOUT) {
|
|
43
43
|
return {
|
|
44
44
|
status: 504,
|
|
45
45
|
setResponseHeader: () => {
|
|
@@ -64,7 +64,7 @@ var HonoAdapter = class {
|
|
|
64
64
|
getResponseHeader: (name) => res.headers.get(name) ?? void 0,
|
|
65
65
|
removeResponseHeader: (name) => res.headers.delete(name),
|
|
66
66
|
// Read from a clone: the response body streams once, to the client.
|
|
67
|
-
getResponseBody: () =>
|
|
67
|
+
getResponseBody: () => chunkMYCUOJ3H_cjs.jsonFromResponse(res),
|
|
68
68
|
sendResponse: () => {
|
|
69
69
|
},
|
|
70
70
|
// The response is replaced: release its body (and whatever upstream
|
|
@@ -79,7 +79,7 @@ var HonoAdapter = class {
|
|
|
79
79
|
|
|
80
80
|
// src/hono/index.ts
|
|
81
81
|
function paymentMiddleware(config) {
|
|
82
|
-
const middleware =
|
|
82
|
+
const middleware = chunkMYCUOJ3H_cjs.createPaymentMiddleware(config);
|
|
83
83
|
return async (c, next) => {
|
|
84
84
|
const result = await middleware(new HonoAdapter(c, next));
|
|
85
85
|
if ("pass" in result) {
|
package/dist/hono.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Next, MiddlewareHandler } from 'hono';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-BWL9h1eY.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class HonoAdapter implements HTTPAdapter {
|
package/dist/hono.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Next, MiddlewareHandler } from 'hono';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-BWL9h1eY.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class HonoAdapter implements HTTPAdapter {
|
package/dist/hono.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isJsonContentType, raceTimeout, HANDLER_TIMEOUT, jsonFromResponse, createPaymentMiddleware } from './chunk-
|
|
1
|
+
import { isJsonContentType, raceTimeout, HANDLER_TIMEOUT, jsonFromResponse, createPaymentMiddleware } from './chunk-VSNDEQGS.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
|
|
4
4
|
// src/hono/adapter.ts
|