@parallel-protocol/x402 0.5.3 → 0.6.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 +51 -4
- package/dist/{chunk-KQPIZ6SZ.cjs → chunk-K6QETMQA.cjs} +124 -15
- package/dist/{chunk-75GLDK7L.js → chunk-TLCEPWQI.js} +120 -16
- package/dist/express.cjs +14 -6
- package/dist/express.d.cts +2 -2
- package/dist/express.d.ts +2 -2
- package/dist/express.js +12 -4
- package/dist/fastify.cjs +62 -13
- package/dist/fastify.d.cts +1 -1
- package/dist/fastify.d.ts +1 -1
- package/dist/fastify.js +60 -11
- package/dist/hono.cjs +10 -6
- package/dist/hono.d.cts +2 -2
- package/dist/hono.d.ts +2 -2
- package/dist/hono.js +8 -4
- package/dist/index.cjs +21 -13
- package/dist/index.d.cts +29 -4
- package/dist/index.d.ts +29 -4
- package/dist/index.js +1 -1
- package/dist/next.cjs +36 -7
- package/dist/next.d.cts +1 -1
- package/dist/next.d.ts +1 -1
- package/dist/next.js +33 -4
- package/dist/types-CBzm5CtM.d.cts +131 -0
- package/dist/types-CBzm5CtM.d.ts +131 -0
- package/package.json +2 -2
- package/dist/chunk-2UEAVMCH.js +0 -17
- package/dist/chunk-XUNNMCO5.cjs +0 -20
- package/dist/types-CUi55YSx.d.cts +0 -95
- package/dist/types-CUi55YSx.d.ts +0 -95
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 RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-CBzm5CtM.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class ExpressAdapter implements HTTPAdapter {
|
|
@@ -11,7 +11,7 @@ declare class ExpressAdapter implements HTTPAdapter {
|
|
|
11
11
|
getMethod(): string;
|
|
12
12
|
getPath(): string;
|
|
13
13
|
getUrl(): string;
|
|
14
|
-
runHandler(): Promise<RunHandlerResult>;
|
|
14
|
+
runHandler(options?: RunHandlerOptions): Promise<RunHandlerResult>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
declare function paymentMiddleware(config: PaymentMiddlewareConfig): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
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 RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-CBzm5CtM.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class ExpressAdapter implements HTTPAdapter {
|
|
@@ -11,7 +11,7 @@ declare class ExpressAdapter implements HTTPAdapter {
|
|
|
11
11
|
getMethod(): string;
|
|
12
12
|
getPath(): string;
|
|
13
13
|
getUrl(): string;
|
|
14
|
-
runHandler(): Promise<RunHandlerResult>;
|
|
14
|
+
runHandler(options?: RunHandlerOptions): Promise<RunHandlerResult>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
declare function paymentMiddleware(config: PaymentMiddlewareConfig): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
package/dist/express.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { raceTimeout, HANDLER_TIMEOUT } from './chunk-
|
|
2
|
-
import { createPaymentMiddleware } from './chunk-75GLDK7L.js';
|
|
1
|
+
import { raceTimeout, HANDLER_TIMEOUT, createPaymentMiddleware } from './chunk-TLCEPWQI.js';
|
|
3
2
|
|
|
4
3
|
// src/express/adapter.ts
|
|
4
|
+
function headerValue(value) {
|
|
5
|
+
if (value === void 0) return void 0;
|
|
6
|
+
return Array.isArray(value) ? value[0] : String(value);
|
|
7
|
+
}
|
|
5
8
|
var TIMEOUT_BODY = JSON.stringify({
|
|
6
9
|
error: "HANDLER_TIMEOUT",
|
|
7
10
|
message: "The resource handler did not respond in time."
|
|
@@ -28,7 +31,7 @@ var ExpressAdapter = class {
|
|
|
28
31
|
getUrl() {
|
|
29
32
|
return `${this.req.protocol}://${this.req.headers.host}${this.req.originalUrl}`;
|
|
30
33
|
}
|
|
31
|
-
async runHandler() {
|
|
34
|
+
async runHandler(options) {
|
|
32
35
|
const res = this.res;
|
|
33
36
|
const next = this.next;
|
|
34
37
|
const originalWriteHead = res.writeHead.bind(res);
|
|
@@ -72,7 +75,7 @@ var ExpressAdapter = class {
|
|
|
72
75
|
return originalEnd(...args);
|
|
73
76
|
});
|
|
74
77
|
next();
|
|
75
|
-
const outcome = await raceTimeout(endPromise);
|
|
78
|
+
const outcome = await raceTimeout(endPromise, options?.timeoutMs);
|
|
76
79
|
if (outcome === HANDLER_TIMEOUT) {
|
|
77
80
|
discarded = true;
|
|
78
81
|
return {
|
|
@@ -80,6 +83,9 @@ var ExpressAdapter = class {
|
|
|
80
83
|
setResponseHeader: (name, value) => {
|
|
81
84
|
if (!res.headersSent) res.setHeader(name, value);
|
|
82
85
|
},
|
|
86
|
+
getResponseHeader: () => void 0,
|
|
87
|
+
removeResponseHeader: () => {
|
|
88
|
+
},
|
|
83
89
|
sendResponse: () => {
|
|
84
90
|
if (res.headersSent) return;
|
|
85
91
|
originalWriteHead(504, { "Content-Type": "application/json" });
|
|
@@ -92,6 +98,8 @@ var ExpressAdapter = class {
|
|
|
92
98
|
return {
|
|
93
99
|
status: res.statusCode,
|
|
94
100
|
setResponseHeader: (name, value) => res.setHeader(name, value),
|
|
101
|
+
getResponseHeader: (name) => headerValue(res.getHeader(name)),
|
|
102
|
+
removeResponseHeader: (name) => res.removeHeader(name),
|
|
95
103
|
sendResponse: () => {
|
|
96
104
|
settled = true;
|
|
97
105
|
restore();
|
package/dist/fastify.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkK6QETMQA_cjs = require('./chunk-K6QETMQA.cjs');
|
|
4
4
|
var fp = require('fastify-plugin');
|
|
5
5
|
|
|
6
6
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -29,10 +29,22 @@ var FastifyAdapter = class {
|
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
// src/fastify/index.ts
|
|
32
|
+
function refuse(reply, body) {
|
|
33
|
+
const json = JSON.stringify(body);
|
|
34
|
+
reply.status(402);
|
|
35
|
+
reply.header("Content-Type", "application/json");
|
|
36
|
+
reply.header("Content-Length", Buffer.byteLength(json));
|
|
37
|
+
return json;
|
|
38
|
+
}
|
|
39
|
+
function headerValue(value) {
|
|
40
|
+
if (value === void 0) return void 0;
|
|
41
|
+
return Array.isArray(value) ? value[0] : String(value);
|
|
42
|
+
}
|
|
32
43
|
function paymentMiddleware(config) {
|
|
33
44
|
return fp__default.default(async (fastify) => {
|
|
34
|
-
const gate =
|
|
35
|
-
const
|
|
45
|
+
const gate = chunkK6QETMQA_cjs.createPaymentGate(config);
|
|
46
|
+
const verifiedMap = /* @__PURE__ */ new WeakMap();
|
|
47
|
+
const rawWriters = /* @__PURE__ */ new WeakMap();
|
|
36
48
|
fastify.addHook("onRequest", async (req, reply) => {
|
|
37
49
|
const gateResult = await gate(new FastifyAdapter(req));
|
|
38
50
|
if (gateResult.type === "pass") return;
|
|
@@ -43,32 +55,69 @@ function paymentMiddleware(config) {
|
|
|
43
55
|
reply.status(gateResult.result.status).send(gateResult.result.body);
|
|
44
56
|
return;
|
|
45
57
|
}
|
|
46
|
-
|
|
58
|
+
verifiedMap.set(req, gateResult);
|
|
59
|
+
const raw = reply.raw;
|
|
60
|
+
rawWriters.set(req, {
|
|
61
|
+
write: raw.write,
|
|
62
|
+
end: raw.end,
|
|
63
|
+
writeHead: raw.writeHead
|
|
64
|
+
});
|
|
65
|
+
const refuse2 = () => {
|
|
66
|
+
throw new Error(
|
|
67
|
+
"x402: raw responses are not supported on paid routes \u2014 return the body from the handler so the payment can settle"
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
raw.write = refuse2;
|
|
71
|
+
raw.end = refuse2;
|
|
72
|
+
raw.writeHead = refuse2;
|
|
47
73
|
});
|
|
48
74
|
fastify.addHook("onSend", async (req, reply, payload) => {
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
75
|
+
const writers = rawWriters.get(req);
|
|
76
|
+
if (writers) {
|
|
77
|
+
const raw2 = reply.raw;
|
|
78
|
+
raw2.write = writers.write;
|
|
79
|
+
raw2.end = writers.end;
|
|
80
|
+
raw2.writeHead = writers.writeHead;
|
|
81
|
+
rawWriters.delete(req);
|
|
82
|
+
}
|
|
83
|
+
const verified = verifiedMap.get(req);
|
|
84
|
+
if (!verified) return payload;
|
|
85
|
+
let raw;
|
|
86
|
+
if (verified.scheme === "upto") {
|
|
87
|
+
raw = headerValue(reply.getHeader(chunkK6QETMQA_cjs.SETTLE_AMOUNT_HEADER));
|
|
88
|
+
reply.removeHeader(chunkK6QETMQA_cjs.SETTLE_AMOUNT_HEADER);
|
|
89
|
+
}
|
|
90
|
+
if (reply.statusCode < 200 || reply.statusCode >= 300) {
|
|
51
91
|
return payload;
|
|
52
92
|
}
|
|
93
|
+
let settleOptions;
|
|
94
|
+
if (verified.scheme === "upto") {
|
|
95
|
+
const resolved = chunkK6QETMQA_cjs.resolveSettleAmount(
|
|
96
|
+
raw,
|
|
97
|
+
verified.maxAmount,
|
|
98
|
+
verified.decimals
|
|
99
|
+
);
|
|
100
|
+
if (resolved.kind === "invalid") {
|
|
101
|
+
return refuse(reply, chunkK6QETMQA_cjs.INVALID_SETTLE_AMOUNT_RESPONSE);
|
|
102
|
+
}
|
|
103
|
+
if (resolved.kind === "skip") return payload;
|
|
104
|
+
settleOptions = { amount: resolved.amount };
|
|
105
|
+
}
|
|
53
106
|
let settled = false;
|
|
54
107
|
try {
|
|
55
|
-
const settleResponse = await settle();
|
|
108
|
+
const settleResponse = settleOptions ? await verified.settle(settleOptions) : await verified.settle();
|
|
56
109
|
if (settleResponse.success) {
|
|
57
|
-
reply.header("payment-response",
|
|
110
|
+
reply.header("payment-response", chunkK6QETMQA_cjs.encodeBase64(settleResponse));
|
|
58
111
|
reply.header("access-control-expose-headers", "payment-response");
|
|
59
112
|
settled = true;
|
|
60
113
|
}
|
|
61
114
|
} catch {
|
|
62
115
|
}
|
|
63
116
|
if (!settled) {
|
|
64
|
-
|
|
117
|
+
return refuse(reply, {
|
|
65
118
|
error: "PAYMENT_SETTLEMENT_FAILED",
|
|
66
119
|
message: "Payment could not be confirmed after multiple attempts. Please retry."
|
|
67
120
|
});
|
|
68
|
-
reply.status(402);
|
|
69
|
-
reply.header("Content-Type", "application/json");
|
|
70
|
-
reply.header("Content-Length", Buffer.byteLength(body));
|
|
71
|
-
return body;
|
|
72
121
|
}
|
|
73
122
|
return payload;
|
|
74
123
|
});
|
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-CBzm5CtM.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-CBzm5CtM.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, encodeBase64 } from './chunk-
|
|
1
|
+
import { createPaymentGate, SETTLE_AMOUNT_HEADER, resolveSettleAmount, encodeBase64, INVALID_SETTLE_AMOUNT_RESPONSE } from './chunk-TLCEPWQI.js';
|
|
2
2
|
import fp from 'fastify-plugin';
|
|
3
3
|
|
|
4
4
|
// src/fastify/adapter.ts
|
|
@@ -23,10 +23,22 @@ var FastifyAdapter = class {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
// src/fastify/index.ts
|
|
26
|
+
function refuse(reply, body) {
|
|
27
|
+
const json = JSON.stringify(body);
|
|
28
|
+
reply.status(402);
|
|
29
|
+
reply.header("Content-Type", "application/json");
|
|
30
|
+
reply.header("Content-Length", Buffer.byteLength(json));
|
|
31
|
+
return json;
|
|
32
|
+
}
|
|
33
|
+
function headerValue(value) {
|
|
34
|
+
if (value === void 0) return void 0;
|
|
35
|
+
return Array.isArray(value) ? value[0] : String(value);
|
|
36
|
+
}
|
|
26
37
|
function paymentMiddleware(config) {
|
|
27
38
|
return fp(async (fastify) => {
|
|
28
39
|
const gate = createPaymentGate(config);
|
|
29
|
-
const
|
|
40
|
+
const verifiedMap = /* @__PURE__ */ new WeakMap();
|
|
41
|
+
const rawWriters = /* @__PURE__ */ new WeakMap();
|
|
30
42
|
fastify.addHook("onRequest", async (req, reply) => {
|
|
31
43
|
const gateResult = await gate(new FastifyAdapter(req));
|
|
32
44
|
if (gateResult.type === "pass") return;
|
|
@@ -37,16 +49,57 @@ function paymentMiddleware(config) {
|
|
|
37
49
|
reply.status(gateResult.result.status).send(gateResult.result.body);
|
|
38
50
|
return;
|
|
39
51
|
}
|
|
40
|
-
|
|
52
|
+
verifiedMap.set(req, gateResult);
|
|
53
|
+
const raw = reply.raw;
|
|
54
|
+
rawWriters.set(req, {
|
|
55
|
+
write: raw.write,
|
|
56
|
+
end: raw.end,
|
|
57
|
+
writeHead: raw.writeHead
|
|
58
|
+
});
|
|
59
|
+
const refuse2 = () => {
|
|
60
|
+
throw new Error(
|
|
61
|
+
"x402: raw responses are not supported on paid routes \u2014 return the body from the handler so the payment can settle"
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
raw.write = refuse2;
|
|
65
|
+
raw.end = refuse2;
|
|
66
|
+
raw.writeHead = refuse2;
|
|
41
67
|
});
|
|
42
68
|
fastify.addHook("onSend", async (req, reply, payload) => {
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
69
|
+
const writers = rawWriters.get(req);
|
|
70
|
+
if (writers) {
|
|
71
|
+
const raw2 = reply.raw;
|
|
72
|
+
raw2.write = writers.write;
|
|
73
|
+
raw2.end = writers.end;
|
|
74
|
+
raw2.writeHead = writers.writeHead;
|
|
75
|
+
rawWriters.delete(req);
|
|
76
|
+
}
|
|
77
|
+
const verified = verifiedMap.get(req);
|
|
78
|
+
if (!verified) return payload;
|
|
79
|
+
let raw;
|
|
80
|
+
if (verified.scheme === "upto") {
|
|
81
|
+
raw = headerValue(reply.getHeader(SETTLE_AMOUNT_HEADER));
|
|
82
|
+
reply.removeHeader(SETTLE_AMOUNT_HEADER);
|
|
83
|
+
}
|
|
84
|
+
if (reply.statusCode < 200 || reply.statusCode >= 300) {
|
|
45
85
|
return payload;
|
|
46
86
|
}
|
|
87
|
+
let settleOptions;
|
|
88
|
+
if (verified.scheme === "upto") {
|
|
89
|
+
const resolved = resolveSettleAmount(
|
|
90
|
+
raw,
|
|
91
|
+
verified.maxAmount,
|
|
92
|
+
verified.decimals
|
|
93
|
+
);
|
|
94
|
+
if (resolved.kind === "invalid") {
|
|
95
|
+
return refuse(reply, INVALID_SETTLE_AMOUNT_RESPONSE);
|
|
96
|
+
}
|
|
97
|
+
if (resolved.kind === "skip") return payload;
|
|
98
|
+
settleOptions = { amount: resolved.amount };
|
|
99
|
+
}
|
|
47
100
|
let settled = false;
|
|
48
101
|
try {
|
|
49
|
-
const settleResponse = await settle();
|
|
102
|
+
const settleResponse = settleOptions ? await verified.settle(settleOptions) : await verified.settle();
|
|
50
103
|
if (settleResponse.success) {
|
|
51
104
|
reply.header("payment-response", encodeBase64(settleResponse));
|
|
52
105
|
reply.header("access-control-expose-headers", "payment-response");
|
|
@@ -55,14 +108,10 @@ function paymentMiddleware(config) {
|
|
|
55
108
|
} catch {
|
|
56
109
|
}
|
|
57
110
|
if (!settled) {
|
|
58
|
-
|
|
111
|
+
return refuse(reply, {
|
|
59
112
|
error: "PAYMENT_SETTLEMENT_FAILED",
|
|
60
113
|
message: "Payment could not be confirmed after multiple attempts. Please retry."
|
|
61
114
|
});
|
|
62
|
-
reply.status(402);
|
|
63
|
-
reply.header("Content-Type", "application/json");
|
|
64
|
-
reply.header("Content-Length", Buffer.byteLength(body));
|
|
65
|
-
return body;
|
|
66
115
|
}
|
|
67
116
|
return payload;
|
|
68
117
|
});
|
package/dist/hono.cjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var chunkKQPIZ6SZ_cjs = require('./chunk-KQPIZ6SZ.cjs');
|
|
3
|
+
var chunkK6QETMQA_cjs = require('./chunk-K6QETMQA.cjs');
|
|
5
4
|
|
|
6
5
|
// src/hono/adapter.ts
|
|
7
6
|
var TIMEOUT_BODY = JSON.stringify({
|
|
@@ -27,13 +26,16 @@ var HonoAdapter = class {
|
|
|
27
26
|
getUrl() {
|
|
28
27
|
return this.c.req.url;
|
|
29
28
|
}
|
|
30
|
-
async runHandler() {
|
|
31
|
-
const outcome = await
|
|
32
|
-
if (outcome ===
|
|
29
|
+
async runHandler(options) {
|
|
30
|
+
const outcome = await chunkK6QETMQA_cjs.raceTimeout(this.next(), options?.timeoutMs);
|
|
31
|
+
if (outcome === chunkK6QETMQA_cjs.HANDLER_TIMEOUT) {
|
|
33
32
|
return {
|
|
34
33
|
status: 504,
|
|
35
34
|
setResponseHeader: () => {
|
|
36
35
|
},
|
|
36
|
+
getResponseHeader: () => void 0,
|
|
37
|
+
removeResponseHeader: () => {
|
|
38
|
+
},
|
|
37
39
|
sendResponse: () => {
|
|
38
40
|
this.c.res = new Response(TIMEOUT_BODY, {
|
|
39
41
|
status: 504,
|
|
@@ -48,6 +50,8 @@ var HonoAdapter = class {
|
|
|
48
50
|
return {
|
|
49
51
|
status: res.status,
|
|
50
52
|
setResponseHeader: (name, value) => res.headers.set(name, value),
|
|
53
|
+
getResponseHeader: (name) => res.headers.get(name) ?? void 0,
|
|
54
|
+
removeResponseHeader: (name) => res.headers.delete(name),
|
|
51
55
|
sendResponse: () => {
|
|
52
56
|
},
|
|
53
57
|
discardResponse: () => {
|
|
@@ -58,7 +62,7 @@ var HonoAdapter = class {
|
|
|
58
62
|
|
|
59
63
|
// src/hono/index.ts
|
|
60
64
|
function paymentMiddleware(config) {
|
|
61
|
-
const middleware =
|
|
65
|
+
const middleware = chunkK6QETMQA_cjs.createPaymentMiddleware(config);
|
|
62
66
|
return async (c, next) => {
|
|
63
67
|
const result = await middleware(new HonoAdapter(c, next));
|
|
64
68
|
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 RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-CBzm5CtM.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class HonoAdapter implements HTTPAdapter {
|
|
@@ -10,7 +10,7 @@ declare class HonoAdapter implements HTTPAdapter {
|
|
|
10
10
|
getMethod(): string;
|
|
11
11
|
getPath(): string;
|
|
12
12
|
getUrl(): string;
|
|
13
|
-
runHandler(): Promise<RunHandlerResult>;
|
|
13
|
+
runHandler(options?: RunHandlerOptions): Promise<RunHandlerResult>;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
declare function paymentMiddleware(config: PaymentMiddlewareConfig): MiddlewareHandler;
|
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 RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-CBzm5CtM.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class HonoAdapter implements HTTPAdapter {
|
|
@@ -10,7 +10,7 @@ declare class HonoAdapter implements HTTPAdapter {
|
|
|
10
10
|
getMethod(): string;
|
|
11
11
|
getPath(): string;
|
|
12
12
|
getUrl(): string;
|
|
13
|
-
runHandler(): Promise<RunHandlerResult>;
|
|
13
|
+
runHandler(options?: RunHandlerOptions): Promise<RunHandlerResult>;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
declare function paymentMiddleware(config: PaymentMiddlewareConfig): MiddlewareHandler;
|
package/dist/hono.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { raceTimeout, HANDLER_TIMEOUT } from './chunk-
|
|
2
|
-
import { createPaymentMiddleware } from './chunk-75GLDK7L.js';
|
|
1
|
+
import { raceTimeout, HANDLER_TIMEOUT, createPaymentMiddleware } from './chunk-TLCEPWQI.js';
|
|
3
2
|
|
|
4
3
|
// src/hono/adapter.ts
|
|
5
4
|
var TIMEOUT_BODY = JSON.stringify({
|
|
@@ -25,13 +24,16 @@ var HonoAdapter = class {
|
|
|
25
24
|
getUrl() {
|
|
26
25
|
return this.c.req.url;
|
|
27
26
|
}
|
|
28
|
-
async runHandler() {
|
|
29
|
-
const outcome = await raceTimeout(this.next());
|
|
27
|
+
async runHandler(options) {
|
|
28
|
+
const outcome = await raceTimeout(this.next(), options?.timeoutMs);
|
|
30
29
|
if (outcome === HANDLER_TIMEOUT) {
|
|
31
30
|
return {
|
|
32
31
|
status: 504,
|
|
33
32
|
setResponseHeader: () => {
|
|
34
33
|
},
|
|
34
|
+
getResponseHeader: () => void 0,
|
|
35
|
+
removeResponseHeader: () => {
|
|
36
|
+
},
|
|
35
37
|
sendResponse: () => {
|
|
36
38
|
this.c.res = new Response(TIMEOUT_BODY, {
|
|
37
39
|
status: 504,
|
|
@@ -46,6 +48,8 @@ var HonoAdapter = class {
|
|
|
46
48
|
return {
|
|
47
49
|
status: res.status,
|
|
48
50
|
setResponseHeader: (name, value) => res.headers.set(name, value),
|
|
51
|
+
getResponseHeader: (name) => res.headers.get(name) ?? void 0,
|
|
52
|
+
removeResponseHeader: (name) => res.headers.delete(name),
|
|
49
53
|
sendResponse: () => {
|
|
50
54
|
},
|
|
51
55
|
discardResponse: () => {
|
package/dist/index.cjs
CHANGED
|
@@ -1,54 +1,62 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkK6QETMQA_cjs = require('./chunk-K6QETMQA.cjs');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
Object.defineProperty(exports, "FacilitatorClient", {
|
|
8
8
|
enumerable: true,
|
|
9
|
-
get: function () { return
|
|
9
|
+
get: function () { return chunkK6QETMQA_cjs.FacilitatorClient; }
|
|
10
10
|
});
|
|
11
11
|
Object.defineProperty(exports, "PARALLEL_TOKENS", {
|
|
12
12
|
enumerable: true,
|
|
13
|
-
get: function () { return
|
|
13
|
+
get: function () { return chunkK6QETMQA_cjs.PARALLEL_TOKENS; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "SETTLE_AMOUNT_HEADER", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return chunkK6QETMQA_cjs.SETTLE_AMOUNT_HEADER; }
|
|
14
18
|
});
|
|
15
19
|
Object.defineProperty(exports, "X402ConfigError", {
|
|
16
20
|
enumerable: true,
|
|
17
|
-
get: function () { return
|
|
21
|
+
get: function () { return chunkK6QETMQA_cjs.X402ConfigError; }
|
|
18
22
|
});
|
|
19
23
|
Object.defineProperty(exports, "X402RuntimeError", {
|
|
20
24
|
enumerable: true,
|
|
21
|
-
get: function () { return
|
|
25
|
+
get: function () { return chunkK6QETMQA_cjs.X402RuntimeError; }
|
|
22
26
|
});
|
|
23
27
|
Object.defineProperty(exports, "X402_ERROR_CODES", {
|
|
24
28
|
enumerable: true,
|
|
25
|
-
get: function () { return
|
|
29
|
+
get: function () { return chunkK6QETMQA_cjs.X402_ERROR_CODES; }
|
|
26
30
|
});
|
|
27
31
|
Object.defineProperty(exports, "createPaymentGate", {
|
|
28
32
|
enumerable: true,
|
|
29
|
-
get: function () { return
|
|
33
|
+
get: function () { return chunkK6QETMQA_cjs.createPaymentGate; }
|
|
30
34
|
});
|
|
31
35
|
Object.defineProperty(exports, "createPaymentMiddleware", {
|
|
32
36
|
enumerable: true,
|
|
33
|
-
get: function () { return
|
|
37
|
+
get: function () { return chunkK6QETMQA_cjs.createPaymentMiddleware; }
|
|
34
38
|
});
|
|
35
39
|
Object.defineProperty(exports, "encodeBase64", {
|
|
36
40
|
enumerable: true,
|
|
37
|
-
get: function () { return
|
|
41
|
+
get: function () { return chunkK6QETMQA_cjs.encodeBase64; }
|
|
38
42
|
});
|
|
39
43
|
Object.defineProperty(exports, "getDefaultAcceptedTokens", {
|
|
40
44
|
enumerable: true,
|
|
41
|
-
get: function () { return
|
|
45
|
+
get: function () { return chunkK6QETMQA_cjs.getDefaultAcceptedTokens; }
|
|
42
46
|
});
|
|
43
47
|
Object.defineProperty(exports, "parsePrice", {
|
|
44
48
|
enumerable: true,
|
|
45
|
-
get: function () { return
|
|
49
|
+
get: function () { return chunkK6QETMQA_cjs.parsePrice; }
|
|
50
|
+
});
|
|
51
|
+
Object.defineProperty(exports, "resolveSettleAmount", {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
get: function () { return chunkK6QETMQA_cjs.resolveSettleAmount; }
|
|
46
54
|
});
|
|
47
55
|
Object.defineProperty(exports, "toEip155Network", {
|
|
48
56
|
enumerable: true,
|
|
49
|
-
get: function () { return
|
|
57
|
+
get: function () { return chunkK6QETMQA_cjs.toEip155Network; }
|
|
50
58
|
});
|
|
51
59
|
Object.defineProperty(exports, "validateAddress", {
|
|
52
60
|
enumerable: true,
|
|
53
|
-
get: function () { return
|
|
61
|
+
get: function () { return chunkK6QETMQA_cjs.validateAddress; }
|
|
54
62
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { F as FacilitatorConfig,
|
|
2
|
-
export {
|
|
1
|
+
import { F as FacilitatorConfig, b as FacilitatorPaymentRequirements, S as SettleOptions, c as FacilitatorResponse, P as PaymentMiddlewareConfig, H as HTTPAdapter, d as PaymentGateResult, M as MiddlewareResult, e as PassResult } from './types-CBzm5CtM.cjs';
|
|
2
|
+
export { f as PaymentConfirmation, g as PaymentFailure, h as PaymentRequired, i as PaymentRequirements, j as ResourceInfo, k as RouteConfig, R as RunHandlerOptions, a as RunHandlerResult, V as VerifiedPayment } from './types-CBzm5CtM.cjs';
|
|
3
3
|
import { Address } from 'viem';
|
|
4
4
|
|
|
5
5
|
declare class FacilitatorClient {
|
|
6
6
|
private readonly config;
|
|
7
7
|
constructor(config: FacilitatorConfig);
|
|
8
8
|
verify(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<void>;
|
|
9
|
-
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<FacilitatorResponse>;
|
|
9
|
+
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements, options?: SettleOptions): Promise<FacilitatorResponse>;
|
|
10
10
|
pay(paymentHeader: string): Promise<FacilitatorResponse>;
|
|
11
11
|
private buildHeaders;
|
|
12
12
|
}
|
|
@@ -25,6 +25,31 @@ declare class X402RuntimeError extends Error {
|
|
|
25
25
|
constructor(code: string);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Response header a handler on a metered (`upto`) route sets to the amount it
|
|
30
|
+
* actually charges, in whole token units like `price` ("0.75"). Read and
|
|
31
|
+
* stripped by the middleware before the response leaves. Absent → the cap is
|
|
32
|
+
* charged; "0" → the request is served for free (no settlement).
|
|
33
|
+
*/
|
|
34
|
+
declare const SETTLE_AMOUNT_HEADER = "payment-settle-amount";
|
|
35
|
+
type SettleAmountResolution = {
|
|
36
|
+
kind: "settle";
|
|
37
|
+
amount: string;
|
|
38
|
+
} | {
|
|
39
|
+
kind: "skip";
|
|
40
|
+
} | {
|
|
41
|
+
kind: "invalid";
|
|
42
|
+
reason: "malformed" | "precision" | "above-cap";
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Turns the handler's `payment-settle-amount` header into a settle decision.
|
|
46
|
+
* A malformed value, one carrying more decimals than the token has, or one
|
|
47
|
+
* above the signed cap is a merchant bug: the payment fails closed rather than
|
|
48
|
+
* charging something the handler did not mean. Only an explicit zero skips
|
|
49
|
+
* the settlement — a positive amount the token cannot represent is never
|
|
50
|
+
* silently served for free.
|
|
51
|
+
*/
|
|
52
|
+
declare function resolveSettleAmount(raw: string | undefined, maxAmount: bigint, decimals: number): SettleAmountResolution;
|
|
28
53
|
declare function createPaymentGate(config: PaymentMiddlewareConfig): (adapter: Pick<HTTPAdapter, "getHeader" | "getPath" | "getUrl" | "getMethod">) => Promise<PaymentGateResult>;
|
|
29
54
|
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (adapter: HTTPAdapter) => Promise<MiddlewareResult | PassResult>;
|
|
30
55
|
|
|
@@ -49,4 +74,4 @@ declare function validateAddress(address: string): address is Address;
|
|
|
49
74
|
declare function toEip155Network(network: string): string;
|
|
50
75
|
declare function encodeBase64(obj: unknown): string;
|
|
51
76
|
|
|
52
|
-
export { FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentGateResult, PaymentMiddlewareConfig, X402ConfigError, type X402ErrorCode, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, toEip155Network, validateAddress };
|
|
77
|
+
export { FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentGateResult, PaymentMiddlewareConfig, SETTLE_AMOUNT_HEADER, type SettleAmountResolution, SettleOptions, X402ConfigError, type X402ErrorCode, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveSettleAmount, toEip155Network, validateAddress };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { F as FacilitatorConfig,
|
|
2
|
-
export {
|
|
1
|
+
import { F as FacilitatorConfig, b as FacilitatorPaymentRequirements, S as SettleOptions, c as FacilitatorResponse, P as PaymentMiddlewareConfig, H as HTTPAdapter, d as PaymentGateResult, M as MiddlewareResult, e as PassResult } from './types-CBzm5CtM.js';
|
|
2
|
+
export { f as PaymentConfirmation, g as PaymentFailure, h as PaymentRequired, i as PaymentRequirements, j as ResourceInfo, k as RouteConfig, R as RunHandlerOptions, a as RunHandlerResult, V as VerifiedPayment } from './types-CBzm5CtM.js';
|
|
3
3
|
import { Address } from 'viem';
|
|
4
4
|
|
|
5
5
|
declare class FacilitatorClient {
|
|
6
6
|
private readonly config;
|
|
7
7
|
constructor(config: FacilitatorConfig);
|
|
8
8
|
verify(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<void>;
|
|
9
|
-
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<FacilitatorResponse>;
|
|
9
|
+
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements, options?: SettleOptions): Promise<FacilitatorResponse>;
|
|
10
10
|
pay(paymentHeader: string): Promise<FacilitatorResponse>;
|
|
11
11
|
private buildHeaders;
|
|
12
12
|
}
|
|
@@ -25,6 +25,31 @@ declare class X402RuntimeError extends Error {
|
|
|
25
25
|
constructor(code: string);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Response header a handler on a metered (`upto`) route sets to the amount it
|
|
30
|
+
* actually charges, in whole token units like `price` ("0.75"). Read and
|
|
31
|
+
* stripped by the middleware before the response leaves. Absent → the cap is
|
|
32
|
+
* charged; "0" → the request is served for free (no settlement).
|
|
33
|
+
*/
|
|
34
|
+
declare const SETTLE_AMOUNT_HEADER = "payment-settle-amount";
|
|
35
|
+
type SettleAmountResolution = {
|
|
36
|
+
kind: "settle";
|
|
37
|
+
amount: string;
|
|
38
|
+
} | {
|
|
39
|
+
kind: "skip";
|
|
40
|
+
} | {
|
|
41
|
+
kind: "invalid";
|
|
42
|
+
reason: "malformed" | "precision" | "above-cap";
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Turns the handler's `payment-settle-amount` header into a settle decision.
|
|
46
|
+
* A malformed value, one carrying more decimals than the token has, or one
|
|
47
|
+
* above the signed cap is a merchant bug: the payment fails closed rather than
|
|
48
|
+
* charging something the handler did not mean. Only an explicit zero skips
|
|
49
|
+
* the settlement — a positive amount the token cannot represent is never
|
|
50
|
+
* silently served for free.
|
|
51
|
+
*/
|
|
52
|
+
declare function resolveSettleAmount(raw: string | undefined, maxAmount: bigint, decimals: number): SettleAmountResolution;
|
|
28
53
|
declare function createPaymentGate(config: PaymentMiddlewareConfig): (adapter: Pick<HTTPAdapter, "getHeader" | "getPath" | "getUrl" | "getMethod">) => Promise<PaymentGateResult>;
|
|
29
54
|
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (adapter: HTTPAdapter) => Promise<MiddlewareResult | PassResult>;
|
|
30
55
|
|
|
@@ -49,4 +74,4 @@ declare function validateAddress(address: string): address is Address;
|
|
|
49
74
|
declare function toEip155Network(network: string): string;
|
|
50
75
|
declare function encodeBase64(obj: unknown): string;
|
|
51
76
|
|
|
52
|
-
export { FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentGateResult, PaymentMiddlewareConfig, X402ConfigError, type X402ErrorCode, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, toEip155Network, validateAddress };
|
|
77
|
+
export { FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentGateResult, PaymentMiddlewareConfig, SETTLE_AMOUNT_HEADER, type SettleAmountResolution, SettleOptions, X402ConfigError, type X402ErrorCode, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveSettleAmount, toEip155Network, validateAddress };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { FacilitatorClient, PARALLEL_TOKENS, X402ConfigError, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, toEip155Network, validateAddress } from './chunk-
|
|
1
|
+
export { FacilitatorClient, PARALLEL_TOKENS, SETTLE_AMOUNT_HEADER, X402ConfigError, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveSettleAmount, toEip155Network, validateAddress } from './chunk-TLCEPWQI.js';
|