@middy/http-x402 7.9.2 → 8.0.0-alpha.2
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/README.md +2 -2
- package/index.d.ts +30 -5
- package/index.js +196 -54
- package/package.json +19 -8
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/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<p>
|
|
6
6
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-unit.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-unit.yml/badge.svg" alt="GitHub Actions unit test status"></a>
|
|
7
7
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-dast.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-dast.yml/badge.svg" alt="GitHub Actions dast test status"></a>
|
|
8
|
-
<a href="https://github.com/middyjs/middy/actions/workflows/test-
|
|
8
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/test-bench.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-bench.yml/badge.svg" alt="GitHub Actions bench test status"></a>
|
|
9
9
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-sast.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-sast.yml/badge.svg" alt="GitHub Actions SAST test status"></a>
|
|
10
10
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-lint.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-lint.yml/badge.svg" alt="GitHub Actions lint test status"></a>
|
|
11
11
|
<br/>
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
<a href="https://github.com/middyjs/middy/blob/main/docs/CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg"></a>
|
|
22
22
|
<a href="https://biomejs.dev"><img alt="Checked with Biome" src="https://img.shields.io/badge/Checked_with-Biome-60a5fa?style=flat&logo=biome"></a>
|
|
23
23
|
<a href="https://conventionalcommits.org"><img alt="Conventional Commits" src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white"></a>
|
|
24
|
-
<a href="https://github.com/middyjs/middy/blob/main/package.json
|
|
24
|
+
<a href="https://github.com/middyjs/middy/blob/main/package.json">
|
|
25
25
|
<img alt="code coverage" src="https://img.shields.io/badge/code%20coverage-100%25-brightgreen"></a>
|
|
26
26
|
<br/>
|
|
27
27
|
</p>
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import type middy from "@middy/core";
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
APIGatewayEvent,
|
|
6
|
+
APIGatewayProxyEventV2,
|
|
7
|
+
Context as LambdaContext,
|
|
8
|
+
} from "aws-lambda";
|
|
5
9
|
|
|
6
10
|
export type RequestEvent = APIGatewayEvent | APIGatewayProxyEventV2;
|
|
7
11
|
|
|
@@ -23,18 +27,39 @@ interface BaseOptions {
|
|
|
23
27
|
description?: string;
|
|
24
28
|
mimeType?: string;
|
|
25
29
|
extra?: Record<string, unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Return `true` to skip payment for this request. Only a literal `true`
|
|
32
|
+
* bypasses; returning a Promise throws a TypeError.
|
|
33
|
+
*/
|
|
26
34
|
human?: (request: middy.Request<RequestEvent>) => boolean;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
export type Options = BaseOptions &
|
|
30
38
|
({ price: number | string } | { amount: string });
|
|
31
39
|
|
|
40
|
+
/**
|
|
41
|
+
* The verified payment the middleware writes to `request.internal.x402`. The
|
|
42
|
+
* settlement fields are added in `after`, once the facilitator settles.
|
|
43
|
+
*/
|
|
44
|
+
export interface X402Internal {
|
|
45
|
+
payload: unknown;
|
|
46
|
+
requirements: unknown;
|
|
47
|
+
payer?: string;
|
|
48
|
+
transaction?: string;
|
|
49
|
+
network?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `x402` is absent when `human` bypassed payment for the request.
|
|
54
|
+
*/
|
|
55
|
+
export type Internal = { x402?: X402Internal };
|
|
56
|
+
|
|
32
57
|
declare function httpX402<EventType extends RequestEvent = RequestEvent>(
|
|
33
58
|
options: Options,
|
|
34
|
-
): middy.MiddlewareObj<EventType, unknown, Error>;
|
|
59
|
+
): middy.MiddlewareObj<EventType, unknown, Error, LambdaContext, Internal>;
|
|
35
60
|
|
|
36
|
-
export declare function httpX402ValidateOptions(
|
|
37
|
-
options?:
|
|
38
|
-
):
|
|
61
|
+
export declare function httpX402ValidateOptions<TOptions extends Options>(
|
|
62
|
+
options?: TOptions,
|
|
63
|
+
): TOptions;
|
|
39
64
|
|
|
40
65
|
export default httpX402;
|
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" },
|
|
@@ -143,7 +150,7 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
143
150
|
request.response.headers["PAYMENT-REQUIRED"] =
|
|
144
151
|
encodeHeader(paymentRequired);
|
|
145
152
|
request.response.body = JSON.stringify(paymentRequired);
|
|
146
|
-
return request
|
|
153
|
+
return albMultiValueResponse(request);
|
|
147
154
|
};
|
|
148
155
|
|
|
149
156
|
// x402 v1 PaymentRequirements: every field is required by the v1 schema,
|
|
@@ -177,15 +184,13 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
177
184
|
request.response.body = JSON.stringify(
|
|
178
185
|
buildPaymentRequiredV1(request.event, error),
|
|
179
186
|
);
|
|
180
|
-
return request
|
|
187
|
+
return albMultiValueResponse(request);
|
|
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,23 +253,33 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
250
253
|
request.internal.x402 = { payload, requirements: requirementsV1 };
|
|
251
254
|
};
|
|
252
255
|
|
|
253
|
-
const httpX402MiddlewareBefore =
|
|
254
|
-
|
|
256
|
+
const httpX402MiddlewareBefore = (request) => {
|
|
257
|
+
// Only a literal `true` bypasses payment. An async callback returns a
|
|
258
|
+
// Promise, which is always truthy, so it would skip payment for every
|
|
259
|
+
// request; reject it loudly instead of failing open.
|
|
260
|
+
const isHuman = human?.(request);
|
|
261
|
+
if (typeof isHuman?.then === "function") {
|
|
262
|
+
throw new TypeError(`${pkg} human must return a boolean, not a Promise`, {
|
|
263
|
+
cause: { package: pkg },
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (isHuman === true) return;
|
|
255
267
|
|
|
256
268
|
// A disabled version's payment header is not a payment header for this
|
|
257
269
|
// server; the client falls through and is re-challenged with the
|
|
258
270
|
// enabled formats only.
|
|
259
|
-
|
|
271
|
+
// ALB with multi-value headers enabled sends only `multiValueHeaders`.
|
|
272
|
+
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
|
|
273
|
+
const headers =
|
|
274
|
+
request.event.headers ?? request.event.multiValueHeaders ?? {};
|
|
260
275
|
const v2PaymentHeader = v2Enabled
|
|
261
|
-
? (headers
|
|
262
|
-
headers["Payment-Signature"] ??
|
|
263
|
-
headers["PAYMENT-SIGNATURE"])
|
|
276
|
+
? readHeader(headers, "payment-signature")
|
|
264
277
|
: undefined;
|
|
265
278
|
if (v2PaymentHeader) {
|
|
266
279
|
return httpX402V2Before(request, v2PaymentHeader);
|
|
267
280
|
}
|
|
268
281
|
const v1PaymentHeader = v1Enabled
|
|
269
|
-
? (headers
|
|
282
|
+
? readHeader(headers, "x-payment")
|
|
270
283
|
: undefined;
|
|
271
284
|
if (v1PaymentHeader) {
|
|
272
285
|
return httpX402V1Before(request, v1PaymentHeader);
|
|
@@ -293,7 +306,7 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
293
306
|
)
|
|
294
307
|
: paymentRequired,
|
|
295
308
|
);
|
|
296
|
-
return request
|
|
309
|
+
return albMultiValueResponse(request);
|
|
297
310
|
}
|
|
298
311
|
return respondPaymentRequiredV1(request, "X-PAYMENT header is required");
|
|
299
312
|
};
|
|
@@ -306,6 +319,9 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
306
319
|
if (request.response.statusCode >= 400) return;
|
|
307
320
|
|
|
308
321
|
const { payload, requirements: storedRequirements } = stored;
|
|
322
|
+
// Decided by the event, not the handler's response: API Gateway REST
|
|
323
|
+
// accepts `headers` and `multiValueHeaders` side by side, ALB does not.
|
|
324
|
+
const multiValue = isAlbMultiValue(request.event);
|
|
309
325
|
// v1 settlements are reported in X-PAYMENT-RESPONSE, v2 in
|
|
310
326
|
// PAYMENT-RESPONSE.
|
|
311
327
|
const responseHeader =
|
|
@@ -323,29 +339,24 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
323
339
|
typeof error?.transaction === "string" ? error.transaction : "",
|
|
324
340
|
network: storedRequirements.network,
|
|
325
341
|
};
|
|
326
|
-
request.response
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
// The handler's response is replaced in place; a stale
|
|
334
|
-
// isBase64Encoded from a binary body would make API Gateway
|
|
335
|
-
// base64-decode this JSON.
|
|
336
|
-
request.response.isBase64Encoded = false;
|
|
342
|
+
request.response = settlementRefused(
|
|
343
|
+
payload,
|
|
344
|
+
responseHeader,
|
|
345
|
+
settleResponse,
|
|
346
|
+
request.response,
|
|
347
|
+
multiValue,
|
|
348
|
+
);
|
|
337
349
|
return;
|
|
338
350
|
}
|
|
339
351
|
|
|
340
352
|
if (!settleResult.success) {
|
|
341
|
-
request.response
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
request.response.isBase64Encoded = false;
|
|
353
|
+
request.response = settlementRefused(
|
|
354
|
+
payload,
|
|
355
|
+
responseHeader,
|
|
356
|
+
settleResult,
|
|
357
|
+
request.response,
|
|
358
|
+
multiValue,
|
|
359
|
+
);
|
|
349
360
|
return;
|
|
350
361
|
}
|
|
351
362
|
|
|
@@ -355,7 +366,14 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
355
366
|
transaction: settleResult.transaction,
|
|
356
367
|
network: settleResult.network,
|
|
357
368
|
};
|
|
358
|
-
|
|
369
|
+
if (multiValue) {
|
|
370
|
+
request.response.multiValueHeaders ??= {};
|
|
371
|
+
request.response.multiValueHeaders[responseHeader] = [
|
|
372
|
+
encodeHeader(settleResult),
|
|
373
|
+
];
|
|
374
|
+
} else {
|
|
375
|
+
request.response.headers[responseHeader] = encodeHeader(settleResult);
|
|
376
|
+
}
|
|
359
377
|
};
|
|
360
378
|
|
|
361
379
|
return {
|
|
@@ -364,6 +382,111 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
364
382
|
};
|
|
365
383
|
};
|
|
366
384
|
|
|
385
|
+
// Field names are case-insensitive (RFC 9110 §5.1) and API Gateway REST passes
|
|
386
|
+
// the client's casing through. ALB multi-value and VPC Lattice V2 deliver
|
|
387
|
+
// arrays; the first value is the one used.
|
|
388
|
+
const readHeader = (headers, lowerName) => {
|
|
389
|
+
for (const key of Object.keys(headers)) {
|
|
390
|
+
if (key.toLowerCase() === lowerName) {
|
|
391
|
+
const value = headers[key];
|
|
392
|
+
return Array.isArray(value) ? value[0] : value;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return undefined;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// Response-policy headers, not content: CORS (so a browser can read the 402 at
|
|
399
|
+
// all), `Vary`, and what http-security-headers sets. When those middlewares are
|
|
400
|
+
// .use()d after this one, their after hooks have already run on the handler's
|
|
401
|
+
// response, so they are carried over to the 402.
|
|
402
|
+
const policyHeaderNames = new Set([
|
|
403
|
+
"vary",
|
|
404
|
+
"strict-transport-security",
|
|
405
|
+
"x-content-type-options",
|
|
406
|
+
"x-frame-options",
|
|
407
|
+
"x-dns-prefetch-control",
|
|
408
|
+
"x-download-options",
|
|
409
|
+
"x-permitted-cross-domain-policies",
|
|
410
|
+
"x-xss-protection",
|
|
411
|
+
"referrer-policy",
|
|
412
|
+
"permissions-policy",
|
|
413
|
+
"origin-agent-cluster",
|
|
414
|
+
"reporting-endpoints",
|
|
415
|
+
"report-to",
|
|
416
|
+
"content-security-policy",
|
|
417
|
+
"content-security-policy-report-only",
|
|
418
|
+
]);
|
|
419
|
+
const isPolicyHeader = (name) => {
|
|
420
|
+
const lower = name.toLowerCase();
|
|
421
|
+
return (
|
|
422
|
+
policyHeaderNames.has(lower) ||
|
|
423
|
+
lower.startsWith("access-control-") ||
|
|
424
|
+
lower.startsWith("cross-origin-")
|
|
425
|
+
);
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// Payment was not taken, so nothing the handler produced for the paid resource
|
|
429
|
+
// may reach the client: not its body, and not its content headers, multi-value
|
|
430
|
+
// headers or v2 cookies (Location, Set-Cookie). The handler's response is
|
|
431
|
+
// replaced whole rather than patched; only policy headers are kept.
|
|
432
|
+
const settlementRefused = (
|
|
433
|
+
payload,
|
|
434
|
+
responseHeader,
|
|
435
|
+
settlement,
|
|
436
|
+
replaced,
|
|
437
|
+
multiValue,
|
|
438
|
+
) => {
|
|
439
|
+
const headers = {};
|
|
440
|
+
for (const map of [replaced.headers, replaced.multiValueHeaders]) {
|
|
441
|
+
if (!map) continue;
|
|
442
|
+
for (const name of Object.keys(map)) {
|
|
443
|
+
if (!isPolicyHeader(name)) continue;
|
|
444
|
+
// Single-value `headers` cannot carry a `multiValueHeaders` array.
|
|
445
|
+
const value = map[name];
|
|
446
|
+
headers[name] =
|
|
447
|
+
!multiValue && Array.isArray(value) ? value.join(", ") : value;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
headers["Content-Type"] = "application/json";
|
|
451
|
+
headers[responseHeader] = encodeHeader(settlement);
|
|
452
|
+
const response = {
|
|
453
|
+
statusCode: 402,
|
|
454
|
+
headers,
|
|
455
|
+
body: JSON.stringify({
|
|
456
|
+
x402Version: payload.x402Version,
|
|
457
|
+
error: settlement.errorReason,
|
|
458
|
+
}),
|
|
459
|
+
isBase64Encoded: false,
|
|
460
|
+
};
|
|
461
|
+
return multiValue ? toMultiValueHeaders(response) : response;
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
// An ALB target group with multi-value headers enabled sends
|
|
465
|
+
// `multiValueHeaders` and only reads them back: "You must use
|
|
466
|
+
// multiValueHeaders if you have enabled multi-value headers and headers
|
|
467
|
+
// otherwise." Its events are the only ones that carry `requestContext.elb`
|
|
468
|
+
// together with `multiValueHeaders`.
|
|
469
|
+
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
|
|
470
|
+
const isAlbMultiValue = (event) =>
|
|
471
|
+
event.requestContext?.elb !== undefined &&
|
|
472
|
+
event.multiValueHeaders !== undefined;
|
|
473
|
+
|
|
474
|
+
const toMultiValueHeaders = ({ headers, ...response }) => {
|
|
475
|
+
const multiValueHeaders = {};
|
|
476
|
+
for (const name of Object.keys(headers)) {
|
|
477
|
+
const value = headers[name];
|
|
478
|
+
multiValueHeaders[name] = Array.isArray(value) ? value : [value];
|
|
479
|
+
}
|
|
480
|
+
return { ...response, multiValueHeaders };
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const albMultiValueResponse = (request) => {
|
|
484
|
+
if (isAlbMultiValue(request.event)) {
|
|
485
|
+
request.response = toMultiValueHeaders(request.response);
|
|
486
|
+
}
|
|
487
|
+
return request.response;
|
|
488
|
+
};
|
|
489
|
+
|
|
367
490
|
// Facilitator clients throw errors carrying `invalidReason` / `errorReason`
|
|
368
491
|
// when the facilitator rejected the request; forward that reason without
|
|
369
492
|
// leaking the error message.
|
|
@@ -436,30 +559,49 @@ const toAtomicAmount = (price, decimals, amountOverride) => {
|
|
|
436
559
|
return String(BigInt(`${whole}${padded}`));
|
|
437
560
|
};
|
|
438
561
|
|
|
562
|
+
// The host comes only from API Gateway's `requestContext.domainName`; the Host
|
|
563
|
+
// header is client-controlled and never used. ALB and VPC Lattice carry no
|
|
564
|
+
// trusted domain, so they get `localhost`, with the request path from the
|
|
565
|
+
// event: `path` (ALB, Lattice V2) or `raw_path` (Lattice V1). Lattice includes
|
|
566
|
+
// the query string in both, which is not part of the resource. Lattice V2 also
|
|
567
|
+
// sends `version: "2.0"`, so the payload is told apart by
|
|
568
|
+
// `requestContext.http`, which only API Gateway HTTP API v2 and function URL
|
|
569
|
+
// events carry.
|
|
570
|
+
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
|
|
571
|
+
// https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html
|
|
439
572
|
const buildResource = (event) => {
|
|
440
|
-
|
|
441
|
-
|
|
573
|
+
const requestContext = event.requestContext;
|
|
574
|
+
if (requestContext?.http) {
|
|
575
|
+
return `https://${requestContext.domainName}${requestContext.http.path}`;
|
|
442
576
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
577
|
+
if (requestContext?.domainName) {
|
|
578
|
+
return `https://${requestContext.domainName}${requestContext.path ?? "/"}`;
|
|
579
|
+
}
|
|
580
|
+
const rawPath = requestContext?.path ?? event.path ?? event.raw_path;
|
|
581
|
+
const path = typeof rawPath === "string" ? rawPath.split("?", 1)[0] : "";
|
|
582
|
+
return `https://localhost${path || "/"}`;
|
|
446
583
|
};
|
|
447
584
|
|
|
448
585
|
const encodeHeader = (obj) =>
|
|
449
586
|
Buffer.from(JSON.stringify(obj)).toString("base64");
|
|
450
587
|
|
|
588
|
+
// Undefined for anything that is not a JSON object: bad base64, bad JSON, or a
|
|
589
|
+
// well-formed payload of the wrong shape. The callers cannot tell those apart,
|
|
590
|
+
// so the reason is not worth building.
|
|
451
591
|
const decodeHeader = (header) => {
|
|
452
|
-
|
|
592
|
+
let payload;
|
|
593
|
+
try {
|
|
594
|
+
payload = JSON.parse(Buffer.from(header, "base64").toString());
|
|
595
|
+
} catch {
|
|
596
|
+
// Bad base64 or bad JSON leaves `payload` undefined, which the shape
|
|
597
|
+
// check below rejects like any other non-object.
|
|
598
|
+
}
|
|
453
599
|
if (
|
|
454
600
|
payload === null ||
|
|
455
601
|
typeof payload !== "object" ||
|
|
456
602
|
Array.isArray(payload)
|
|
457
603
|
) {
|
|
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
|
-
});
|
|
604
|
+
return undefined;
|
|
463
605
|
}
|
|
464
606
|
return payload;
|
|
465
607
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-x402",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.2",
|
|
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": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"test": "npm run test:unit && npm run test:fuzz",
|
|
30
30
|
"test:unit": "node --test",
|
|
31
31
|
"test:fuzz": "node --test index.fuzz.js",
|
|
32
|
-
"test:
|
|
32
|
+
"test:bench": "node --experimental-bench --bench 'index.bench.js'",
|
|
33
33
|
"test:mutation": "cd ../.. && MUTATE_PACKAGE=\"$(basename \"$OLDPWD\")\" stryker run"
|
|
34
34
|
},
|
|
35
35
|
"license": "MIT",
|
|
@@ -64,14 +64,25 @@
|
|
|
64
64
|
"url": "https://github.com/sponsors/willfarrell"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@x402/core": "^2.
|
|
67
|
+
"@x402/core": "^2.28.0",
|
|
68
|
+
"@middy/core": "8.0.0-alpha.2",
|
|
69
|
+
"@types/aws-lambda": "^8.0.0"
|
|
70
|
+
},
|
|
71
|
+
"peerDependenciesMeta": {
|
|
72
|
+
"@middy/core": {
|
|
73
|
+
"optional": true
|
|
74
|
+
},
|
|
75
|
+
"@types/aws-lambda": {
|
|
76
|
+
"optional": true
|
|
77
|
+
}
|
|
68
78
|
},
|
|
69
79
|
"dependencies": {
|
|
70
|
-
"@middy/util": "
|
|
80
|
+
"@middy/util": "8.0.0-alpha.2"
|
|
71
81
|
},
|
|
72
82
|
"devDependencies": {
|
|
73
|
-
"@middy/core": "
|
|
74
|
-
"@types/
|
|
75
|
-
"@
|
|
83
|
+
"@middy/core": "8.0.0-alpha.2",
|
|
84
|
+
"@types/aws-lambda": "^8.0.0",
|
|
85
|
+
"@types/node": "^24.0.0",
|
|
86
|
+
"@x402/core": "^2.28.0"
|
|
76
87
|
}
|
|
77
88
|
}
|