@emdash-cms/x402 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/enforcer.ts +6 -3
- package/src/middleware.ts +2 -1
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ const RESOLVED_VIRTUAL_MODULE_ID = "\0" + VIRTUAL_MODULE_ID;
|
|
|
6
6
|
*/
|
|
7
7
|
function x402(config) {
|
|
8
8
|
return {
|
|
9
|
-
name: "@
|
|
9
|
+
name: "@emdash-cms/x402",
|
|
10
10
|
hooks: { "astro:config:setup": ({ addMiddleware, updateConfig }) => {
|
|
11
11
|
updateConfig({ vite: { plugins: [{
|
|
12
12
|
name: "x402-virtual-config",
|
|
@@ -18,7 +18,7 @@ function x402(config) {
|
|
|
18
18
|
}
|
|
19
19
|
}] } });
|
|
20
20
|
addMiddleware({
|
|
21
|
-
entrypoint: "@
|
|
21
|
+
entrypoint: "@emdash-cms/x402/middleware",
|
|
22
22
|
order: "pre"
|
|
23
23
|
});
|
|
24
24
|
} }
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @emdash-cms/x402 -- x402 Payment Integration for Astro\n *\n * An Astro integration that provides x402 payment enforcement via\n * Astro.locals.x402. Supports bot-only mode using Cloudflare Bot Management.\n *\n * @example\n * ```ts\n * // astro.config.mjs\n * import { x402 } from \"@emdash-cms/x402\";\n *\n * export default defineConfig({\n * integrations: [\n * x402({\n * payTo: \"0xYourWallet\",\n * network: \"eip155:8453\",\n * defaultPrice: \"$0.01\",\n * botOnly: true,\n * }),\n * ],\n * });\n * ```\n *\n * ```astro\n * ---\n * const { x402 } = Astro.locals;\n *\n * const result = await x402.enforce(Astro.request, { price: \"$0.05\" });\n * if (result instanceof Response) return result;\n *\n * x402.applyHeaders(result, Astro.response);\n * ---\n * <article>Premium content here</article>\n * ```\n */\n\nimport type { AstroIntegration } from \"astro\";\n\nimport type { X402Config } from \"./types.js\";\n\nconst VIRTUAL_MODULE_ID = \"virtual:x402/config\";\nconst RESOLVED_VIRTUAL_MODULE_ID = \"\\0\" + VIRTUAL_MODULE_ID;\n\n/**\n * Create the x402 Astro integration.\n */\nexport function x402(config: X402Config): AstroIntegration {\n\treturn {\n\t\tname: \"@emdash-cms/x402\",\n\t\thooks: {\n\t\t\t\"astro:config:setup\": ({ addMiddleware, updateConfig }) => {\n\t\t\t\t// Inject the virtual module that provides config to the middleware\n\t\t\t\tupdateConfig({\n\t\t\t\t\tvite: {\n\t\t\t\t\t\tplugins: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: \"x402-virtual-config\",\n\t\t\t\t\t\t\t\tresolveId(id: string) {\n\t\t\t\t\t\t\t\t\tif (id === VIRTUAL_MODULE_ID) return RESOLVED_VIRTUAL_MODULE_ID;\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tload(id: string) {\n\t\t\t\t\t\t\t\t\tif (id === RESOLVED_VIRTUAL_MODULE_ID) {\n\t\t\t\t\t\t\t\t\t\treturn `export default ${JSON.stringify(config)}`;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t});\n\n\t\t\t\t// Register the middleware that puts the enforcer on locals\n\t\t\t\taddMiddleware({\n\t\t\t\t\tentrypoint: \"@emdash-cms/x402/middleware\",\n\t\t\t\t\torder: \"pre\",\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t};\n}\n\n// Re-export types for convenience\nexport type {\n\tEnforceOptions,\n\tEnforceResult,\n\tNetwork,\n\tPrice,\n\tX402Config,\n\tX402Enforcer,\n} from \"./types.js\";\n"],"mappings":";AAwCA,MAAM,oBAAoB;AAC1B,MAAM,6BAA6B,OAAO;;;;AAK1C,SAAgB,KAAK,QAAsC;AAC1D,QAAO;EACN,MAAM;EACN,OAAO,EACN,uBAAuB,EAAE,eAAe,mBAAmB;AAE1D,gBAAa,EACZ,MAAM,EACL,SAAS,CACR;IACC,MAAM;IACN,UAAU,IAAY;AACrB,SAAI,OAAO,kBAAmB,QAAO;;IAEtC,KAAK,IAAY;AAChB,SAAI,OAAO,2BACV,QAAO,kBAAkB,KAAK,UAAU,OAAO;;IAGjD,CACD,EACD,EACD,CAAC;AAGF,iBAAc;IACb,YAAY;IACZ,OAAO;IACP,CAAC;KAEH;EACD"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emdash-cms/x402",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "x402 payment protocol integration for Astro sites",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/
|
|
8
|
+
"url": "git+https://github.com/emdash-cms/emdash.git",
|
|
9
9
|
"directory": "packages/x402"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
package/src/enforcer.ts
CHANGED
|
@@ -81,9 +81,12 @@ async function getResourceServer(config: X402Config): Promise<x402ResourceServer
|
|
|
81
81
|
*/
|
|
82
82
|
function isBot(request: Request, threshold: number): boolean {
|
|
83
83
|
// Cloudflare Workers expose cf properties on the request
|
|
84
|
-
const cf = (request
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
const cf: unknown = Reflect.get(request, "cf");
|
|
85
|
+
if (cf == null || typeof cf !== "object") return false;
|
|
86
|
+
const bm: unknown = Reflect.get(cf, "botManagement");
|
|
87
|
+
if (bm == null || typeof bm !== "object") return false;
|
|
88
|
+
const score: unknown = Reflect.get(bm, "score");
|
|
89
|
+
if (typeof score !== "number") return false;
|
|
87
90
|
return score < threshold;
|
|
88
91
|
}
|
|
89
92
|
|
package/src/middleware.ts
CHANGED
|
@@ -15,7 +15,8 @@ import x402Config from "virtual:x402/config";
|
|
|
15
15
|
import { createEnforcer } from "./enforcer.js";
|
|
16
16
|
import type { X402Config } from "./types.js";
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
// eslint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- virtual module import has no type info
|
|
19
|
+
const config: X402Config = x402Config as X402Config;
|
|
19
20
|
const enforcer = createEnforcer(config);
|
|
20
21
|
|
|
21
22
|
export const onRequest = defineMiddleware(async (context, next) => {
|