@middy/http-x402 7.9.2 → 8.0.0-alpha.1
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/LICENSE +21 -0
- package/index.js +28 -20
- package/package.json +5 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-2026 [will Farrell](https://github.com/willfarrell), [Luciano Mammino](https://github.com/lmammino) and [Middy contributors](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
CHANGED
|
@@ -9,9 +9,9 @@ const pkg = `@middy/${name}`;
|
|
|
9
9
|
const defaults = {
|
|
10
10
|
FacilitatorClient: HTTPFacilitatorClient,
|
|
11
11
|
facilitatorUrl: "https://x402.org/facilitator",
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
versions: [
|
|
12
|
+
// v1 acceptance is opt-in (smaller attack surface, no legacy facilitator
|
|
13
|
+
// path): pass `versions: [1, 2]` to also accept the X-PAYMENT v1 flow.
|
|
14
|
+
versions: [2],
|
|
15
15
|
price: undefined,
|
|
16
16
|
amount: undefined,
|
|
17
17
|
decimals: 6,
|
|
@@ -31,7 +31,14 @@ const optionSchema = {
|
|
|
31
31
|
facilitatorUrl: { type: "string" },
|
|
32
32
|
versions: { type: "array", items: { enum: [1, 2] } },
|
|
33
33
|
price: {
|
|
34
|
-
oneOf: [
|
|
34
|
+
oneOf: [
|
|
35
|
+
{
|
|
36
|
+
type: "number",
|
|
37
|
+
exclusiveMinimum: 0,
|
|
38
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
39
|
+
},
|
|
40
|
+
{ type: "string" },
|
|
41
|
+
],
|
|
35
42
|
},
|
|
36
43
|
amount: { type: "string" },
|
|
37
44
|
decimals: { type: "integer" },
|
|
@@ -181,11 +188,9 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
181
188
|
};
|
|
182
189
|
|
|
183
190
|
const httpX402V2Before = async (request, paymentHeader) => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
} catch {
|
|
188
|
-
// An undecodable header is treated the same as no payment at all.
|
|
191
|
+
// An undecodable header is treated the same as no payment at all.
|
|
192
|
+
const payload = decodeHeader(paymentHeader);
|
|
193
|
+
if (payload === undefined) {
|
|
189
194
|
return respondPaymentRequired(request, "Payment required");
|
|
190
195
|
}
|
|
191
196
|
|
|
@@ -213,10 +218,8 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
213
218
|
};
|
|
214
219
|
|
|
215
220
|
const httpX402V1Before = async (request, paymentHeader) => {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
payload = decodeHeader(paymentHeader);
|
|
219
|
-
} catch {
|
|
221
|
+
const payload = decodeHeader(paymentHeader);
|
|
222
|
+
if (payload === undefined) {
|
|
220
223
|
return respondPaymentRequiredV1(request, "Payment required");
|
|
221
224
|
}
|
|
222
225
|
|
|
@@ -250,7 +253,7 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
250
253
|
request.internal.x402 = { payload, requirements: requirementsV1 };
|
|
251
254
|
};
|
|
252
255
|
|
|
253
|
-
const httpX402MiddlewareBefore =
|
|
256
|
+
const httpX402MiddlewareBefore = (request) => {
|
|
254
257
|
if (human?.(request)) return;
|
|
255
258
|
|
|
256
259
|
// A disabled version's payment header is not a payment header for this
|
|
@@ -448,18 +451,23 @@ const buildResource = (event) => {
|
|
|
448
451
|
const encodeHeader = (obj) =>
|
|
449
452
|
Buffer.from(JSON.stringify(obj)).toString("base64");
|
|
450
453
|
|
|
454
|
+
// Undefined for anything that is not a JSON object: bad base64, bad JSON, or a
|
|
455
|
+
// well-formed payload of the wrong shape. The callers cannot tell those apart,
|
|
456
|
+
// so the reason is not worth building.
|
|
451
457
|
const decodeHeader = (header) => {
|
|
452
|
-
|
|
458
|
+
let payload;
|
|
459
|
+
try {
|
|
460
|
+
payload = JSON.parse(Buffer.from(header, "base64").toString());
|
|
461
|
+
} catch {
|
|
462
|
+
// Bad base64 or bad JSON leaves `payload` undefined, which the shape
|
|
463
|
+
// check below rejects like any other non-object.
|
|
464
|
+
}
|
|
453
465
|
if (
|
|
454
466
|
payload === null ||
|
|
455
467
|
typeof payload !== "object" ||
|
|
456
468
|
Array.isArray(payload)
|
|
457
469
|
) {
|
|
458
|
-
|
|
459
|
-
throw new Error(`${pkg} payment payload must be an object`, {
|
|
460
|
-
// Stryker disable next-line ObjectLiteral: see above; cause is unobservable because the thrown error is swallowed.
|
|
461
|
-
cause: { package: pkg },
|
|
462
|
-
});
|
|
470
|
+
return undefined;
|
|
463
471
|
}
|
|
464
472
|
return payload;
|
|
465
473
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-x402",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.1",
|
|
4
4
|
"description": "x402 payment middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=24"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
@@ -67,11 +67,11 @@
|
|
|
67
67
|
"@x402/core": "^2.0.0"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@middy/util": "
|
|
70
|
+
"@middy/util": "8.0.0-alpha.1"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@middy/core": "
|
|
74
|
-
"@types/node": "^
|
|
73
|
+
"@middy/core": "8.0.0-alpha.1",
|
|
74
|
+
"@types/node": "^24.0.0",
|
|
75
75
|
"@x402/core": "^2.0.0"
|
|
76
76
|
}
|
|
77
77
|
}
|