herald-door 0.1.0 → 0.1.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/README.md CHANGED
@@ -2,12 +2,14 @@
2
2
 
3
3
  Put Herald in front of an API route. AI agents with a Verified by Concordium badge pay per call in the fast lane; everyone else keeps your free tier. Concordium testnet only.
4
4
 
5
- This is the herald-door package (in the repo; not on npm yet). Install it from the packed file:
5
+ On npm: https://www.npmjs.com/package/herald-door
6
6
 
7
7
  ```sh
8
- npm i ./herald-door-0.1.0.tgz
8
+ npm i herald-door
9
9
  ```
10
10
 
11
+ Or build it from the repo: `pnpm pack:door`, then `npm i ./herald-door-0.1.0.tgz`.
12
+
11
13
  ## Watch first, block nobody
12
14
 
13
15
  ```ts
package/dist/index.d.ts CHANGED
@@ -94,6 +94,11 @@ export interface DoorOptions {
94
94
  /** Label on receipts, e.g. "api" or "demo:badged". */
95
95
  source?: string;
96
96
  }
97
+ /**
98
+ * The client's IP for free-tier counting. Vercel sets x-vercel-forwarded-for and x-real-ip itself; the first
99
+ * x-forwarded-for hop is only a fallback for other hosts, since a client can put anything there.
100
+ */
101
+ export declare function clientIp(get: (name: string) => string | null | undefined): string;
97
102
  export declare function hasAgentCredentials(h: Record<string, string | undefined>): boolean;
98
103
  declare function laneOf(status: number): Lane;
99
104
  export declare class MemoryFreeTierCounter implements FreeTierCounter {
package/dist/index.js CHANGED
@@ -8,6 +8,14 @@
8
8
  */
9
9
  /** Check ids of the paid part of the door; a refusal there still means the badge itself was fine. */
10
10
  const PAYMENT_STEPS = new Set(["payment", "payer", "settle"]);
11
+ /**
12
+ * The client's IP for free-tier counting. Vercel sets x-vercel-forwarded-for and x-real-ip itself; the first
13
+ * x-forwarded-for hop is only a fallback for other hosts, since a client can put anything there.
14
+ */
15
+ export function clientIp(get) {
16
+ const v = get("x-vercel-forwarded-for")?.split(",")[0]?.trim();
17
+ return v || get("x-real-ip")?.trim() || get("x-forwarded-for")?.split(",")[0]?.trim() || "unknown";
18
+ }
11
19
  export function hasAgentCredentials(h) {
12
20
  return !!(h["x-agent-badge"] || h["x-agent-card"] || h["payment-signature"] || h["x-payment"] || h["x-herald-pass"]);
13
21
  }
@@ -120,7 +128,7 @@ export function heraldDoor(opts) {
120
128
  return Response.json({ error: "not_found", message: "No such resource." }, { status: 404 });
121
129
  const headers = {};
122
130
  req.headers.forEach((v, k) => (headers[k.toLowerCase()] = v));
123
- const ip = req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || req.headers.get("x-real-ip") || "unknown";
131
+ const ip = clientIp((n) => req.headers.get(n));
124
132
  const r = await handle({ url: req.url, headers, ip }, resource);
125
133
  return Response.json(r.body, {
126
134
  status: r.status,
package/package.json CHANGED
@@ -1,9 +1,18 @@
1
1
  {
2
2
  "name": "herald-door",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "author": "levithefirst",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/levithefirst/herald.git",
10
+ "directory": "packages/door"
11
+ },
12
+ "homepage": "https://useherald.vercel.app/publish",
13
+ "bugs": {
14
+ "url": "https://github.com/levithefirst/herald/issues"
15
+ },
7
16
  "type": "module",
8
17
  "description": "Put Herald in front of an API route: badged AI agents pay per call in the fast lane, everyone else keeps your free tier.",
9
18
  "keywords": [