herald-door 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +35 -2
- package/dist/index.d.ts +138 -0
- package/dist/index.js +184 -0
- package/package.json +28 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 levithefirst
|
|
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
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# herald-door
|
|
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
|
+
|
|
5
|
+
This is the herald-door package (in the repo; not on npm yet). Install it from the packed file:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i ./herald-door-0.1.0.tgz
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Watch first, block nobody
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { heraldDoor, remoteCheckEngine } from "herald-door";
|
|
15
|
+
|
|
16
|
+
const door = heraldDoor({
|
|
17
|
+
engine: remoteCheckEngine({ base: "https://useherald.vercel.app" }), // free checks only, no keys
|
|
18
|
+
priceMicroCcd: 7_000_000,
|
|
19
|
+
payTo: "<your testnet account>",
|
|
20
|
+
mode: "log-only",
|
|
21
|
+
onDecision: (d) => console.log(d.resource, d.badge, d.badgeValid),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
app.get("/posts/:slug", door.express((req) => ({ name: `posts:${req.params.slug}`, description: "a post", produce: async () => loadPost(req.params.slug) })));
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
In log-only mode the door never blocks. It runs Herald's free checks on any badge it sees and tells you what it would have done (`x-herald-would: fast | free | refuse`).
|
|
28
|
+
|
|
29
|
+
## What's in it
|
|
30
|
+
|
|
31
|
+
- `heraldDoor(options)` with three ways in: `handle` (framework-free), `next` (App Router route) and `express`.
|
|
32
|
+
- Lanes: `fast` (paid), `free` (teaser on the free tier), `throttled` (429 with how to take the fast lane), `refused` (403 with the failing step), `payment_required` (402), `log`.
|
|
33
|
+
- `remoteCheckEngine()`: the free checks through Herald's public `/api/check`. The paid fast lane needs Herald's engine (registry, Concordium ID, x402 on Concordium); a site can get it without running Herald by sitting behind a Herald publisher door (`/api/p/<id>/...`).
|
|
34
|
+
- `MemoryFreeTierCounter` for a single process; pass your own `counter` for anything bigger.
|
|
35
|
+
|
|
36
|
+
Examples: [`examples/`](examples). Licence: MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @herald/door: put Herald in front of an API route.
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic core (`handle`) plus two adapters: a Next.js route wrapper (`next`) and an
|
|
5
|
+
* Express-style middleware (`express`). The door engine does the actual checks and payment
|
|
6
|
+
* (Herald's engine: Agent Registry + Concordium ID + x402 on Concordium). This package holds
|
|
7
|
+
* the policy: who gets the fast lane, who stays on the free tier, enforce or log-only.
|
|
8
|
+
*/
|
|
9
|
+
export type Lane = "fast" | "free" | "refused" | "payment_required" | "throttled" | "unavailable" | "log" | "not_found";
|
|
10
|
+
export type Mode = "enforce" | "log-only";
|
|
11
|
+
export interface Resource {
|
|
12
|
+
/** Stable name, written on receipts, e.g. "crawl:payer-binding". */
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
/** Full content. Only produced for admitted (or log-only) requests. */
|
|
16
|
+
produce: () => Promise<unknown>;
|
|
17
|
+
/** What an unbadged visitor on the free tier gets, e.g. title and first paragraph. */
|
|
18
|
+
teaser?: () => Promise<unknown>;
|
|
19
|
+
}
|
|
20
|
+
export interface DoorRequest {
|
|
21
|
+
url: string;
|
|
22
|
+
headers: Record<string, string | undefined>;
|
|
23
|
+
/** Client IP, used only for free-tier counting. Never stored in clear. */
|
|
24
|
+
ip: string;
|
|
25
|
+
}
|
|
26
|
+
export interface DoorResponse {
|
|
27
|
+
status: number;
|
|
28
|
+
headers: Record<string, string>;
|
|
29
|
+
body: unknown;
|
|
30
|
+
lane: Lane;
|
|
31
|
+
}
|
|
32
|
+
export interface EngineKnock {
|
|
33
|
+
resource: string;
|
|
34
|
+
description: string;
|
|
35
|
+
url: string;
|
|
36
|
+
headers: Record<string, string | undefined>;
|
|
37
|
+
payTo: string;
|
|
38
|
+
priceMicroCcd: string;
|
|
39
|
+
/** Asset of the price: "CCD" (default) or a protocol-level token id such as "EURR". */
|
|
40
|
+
priceAsset?: string;
|
|
41
|
+
produce: () => Promise<unknown>;
|
|
42
|
+
source: string;
|
|
43
|
+
trustedRegistries?: string[];
|
|
44
|
+
}
|
|
45
|
+
export interface EngineCheck {
|
|
46
|
+
badge: string | null;
|
|
47
|
+
passed: boolean;
|
|
48
|
+
reasons: string[];
|
|
49
|
+
failedStep: number | null;
|
|
50
|
+
}
|
|
51
|
+
export interface DoorEngine {
|
|
52
|
+
/** The full door: badge checks, x402 payment, payer binding, settle. Returns 200, 402 or 403. */
|
|
53
|
+
knock(k: EngineKnock): Promise<{
|
|
54
|
+
status: number;
|
|
55
|
+
headers: Record<string, string>;
|
|
56
|
+
body: Record<string, unknown>;
|
|
57
|
+
}>;
|
|
58
|
+
/** Free checks only (no payment), for log-only mode. */
|
|
59
|
+
check(headers: Record<string, string | undefined>, trustedRegistries?: string[]): Promise<EngineCheck>;
|
|
60
|
+
}
|
|
61
|
+
export interface Decision {
|
|
62
|
+
at: string;
|
|
63
|
+
resource: string;
|
|
64
|
+
lane: Lane;
|
|
65
|
+
status: number;
|
|
66
|
+
badge: string | null;
|
|
67
|
+
/** true: the badge passed the free checks; false: a badge was shown and failed; null: no badge. */
|
|
68
|
+
badgeValid: boolean | null;
|
|
69
|
+
reasons: string[];
|
|
70
|
+
mode: Mode;
|
|
71
|
+
}
|
|
72
|
+
export interface FreeTierCounter {
|
|
73
|
+
/** Adds one hit for `key` in the current hour and returns the new count. */
|
|
74
|
+
hit(key: string, windowMs: number): Promise<number>;
|
|
75
|
+
}
|
|
76
|
+
export interface FreeTierOptions {
|
|
77
|
+
/** Free requests per IP per hour for visitors without a badge. */
|
|
78
|
+
perHour: number;
|
|
79
|
+
counter?: FreeTierCounter;
|
|
80
|
+
windowMs?: number;
|
|
81
|
+
}
|
|
82
|
+
export interface DoorOptions {
|
|
83
|
+
engine: DoorEngine;
|
|
84
|
+
/** Price in microCCD (1 CCD = 1,000,000). */
|
|
85
|
+
priceMicroCcd: string | number | bigint;
|
|
86
|
+
/** "CCD" (default) or a protocol-level token such as "EURR"; the price is then in that token's atomic units. */
|
|
87
|
+
asset?: string;
|
|
88
|
+
payTo: string;
|
|
89
|
+
mode?: Mode;
|
|
90
|
+
/** Free tier for unbadged visitors. `false` (default) means unbadged requests go to the door and are refused. */
|
|
91
|
+
freeTier?: FreeTierOptions | false;
|
|
92
|
+
trustedRegistries?: string[];
|
|
93
|
+
onDecision?: (d: Decision) => void | Promise<void>;
|
|
94
|
+
/** Label on receipts, e.g. "api" or "demo:badged". */
|
|
95
|
+
source?: string;
|
|
96
|
+
}
|
|
97
|
+
export declare function hasAgentCredentials(h: Record<string, string | undefined>): boolean;
|
|
98
|
+
declare function laneOf(status: number): Lane;
|
|
99
|
+
export declare class MemoryFreeTierCounter implements FreeTierCounter {
|
|
100
|
+
private m;
|
|
101
|
+
hit(key: string, windowMs: number): Promise<number>;
|
|
102
|
+
}
|
|
103
|
+
/** The parts of an Express (or Express-like) request the door reads. */
|
|
104
|
+
export interface ExpressReq {
|
|
105
|
+
headers: Record<string, string | string[] | undefined>;
|
|
106
|
+
protocol?: string;
|
|
107
|
+
originalUrl?: string;
|
|
108
|
+
url?: string;
|
|
109
|
+
ip?: string;
|
|
110
|
+
get?(name: string): string | undefined;
|
|
111
|
+
}
|
|
112
|
+
/** The parts of an Express response the door writes. */
|
|
113
|
+
export interface ExpressRes {
|
|
114
|
+
status(code: number): ExpressRes;
|
|
115
|
+
set(headers: Record<string, string>): ExpressRes;
|
|
116
|
+
json(body: unknown): unknown;
|
|
117
|
+
}
|
|
118
|
+
export declare function heraldDoor(opts: DoorOptions): {
|
|
119
|
+
handle: (req: DoorRequest, resource: Resource, sourceOverride?: string) => Promise<DoorResponse>;
|
|
120
|
+
next: <Ctx>(resolve: (req: Request, ctx: Ctx) => Promise<Resource | null> | Resource | null) => (req: Request, ctx: Ctx) => Promise<Response>;
|
|
121
|
+
express: (resolve: (req: ExpressReq) => Promise<Resource | null> | Resource | null) => (req: ExpressReq, res: ExpressRes, nextFn?: (e?: unknown) => void) => Promise<unknown>;
|
|
122
|
+
mode: Mode;
|
|
123
|
+
};
|
|
124
|
+
export declare const internal: {
|
|
125
|
+
laneOf: typeof laneOf;
|
|
126
|
+
hasAgentCredentials: typeof hasAgentCredentials;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* An engine for a server that doesn't run Herald itself: the free checks go to Herald's public
|
|
130
|
+
* `GET /api/check?badge=` (no keys, no payment). Enough for log-only mode. For the paid fast lane,
|
|
131
|
+
* put the site behind a Herald publisher door (`/api/p/<id>/...`) instead; `knock` here answers 503.
|
|
132
|
+
*/
|
|
133
|
+
export declare function remoteCheckEngine(opts?: {
|
|
134
|
+
base?: string;
|
|
135
|
+
fetch?: typeof fetch;
|
|
136
|
+
timeoutMs?: number;
|
|
137
|
+
}): DoorEngine;
|
|
138
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @herald/door: put Herald in front of an API route.
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic core (`handle`) plus two adapters: a Next.js route wrapper (`next`) and an
|
|
5
|
+
* Express-style middleware (`express`). The door engine does the actual checks and payment
|
|
6
|
+
* (Herald's engine: Agent Registry + Concordium ID + x402 on Concordium). This package holds
|
|
7
|
+
* the policy: who gets the fast lane, who stays on the free tier, enforce or log-only.
|
|
8
|
+
*/
|
|
9
|
+
/** Check ids of the paid part of the door; a refusal there still means the badge itself was fine. */
|
|
10
|
+
const PAYMENT_STEPS = new Set(["payment", "payer", "settle"]);
|
|
11
|
+
export function hasAgentCredentials(h) {
|
|
12
|
+
return !!(h["x-agent-badge"] || h["x-agent-card"] || h["payment-signature"] || h["x-payment"] || h["x-herald-pass"]);
|
|
13
|
+
}
|
|
14
|
+
function laneOf(status) {
|
|
15
|
+
return status === 200 ? "fast" : status === 402 ? "payment_required" : status === 403 ? "refused" : status === 429 ? "throttled" : status === 404 ? "not_found" : "unavailable";
|
|
16
|
+
}
|
|
17
|
+
export class MemoryFreeTierCounter {
|
|
18
|
+
m = new Map();
|
|
19
|
+
async hit(key, windowMs) {
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
const start = now - (now % windowMs);
|
|
22
|
+
const e = this.m.get(key);
|
|
23
|
+
const n = e && e.start === start ? e.n + 1 : 1;
|
|
24
|
+
this.m.set(key, { start, n });
|
|
25
|
+
return n;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function heraldDoor(opts) {
|
|
29
|
+
const mode = opts.mode ?? "enforce";
|
|
30
|
+
const price = String(opts.priceMicroCcd);
|
|
31
|
+
const source = opts.source ?? "api";
|
|
32
|
+
const freeTier = opts.freeTier || null;
|
|
33
|
+
const counter = freeTier ? (freeTier.counter ?? new MemoryFreeTierCounter()) : null;
|
|
34
|
+
const windowMs = freeTier?.windowMs ?? 3600_000;
|
|
35
|
+
async function decide(resource, res, badge, badgeValid, reasons) {
|
|
36
|
+
res.headers["x-herald-lane"] = res.lane;
|
|
37
|
+
res.headers["x-herald-mode"] = mode;
|
|
38
|
+
if (opts.onDecision) {
|
|
39
|
+
try {
|
|
40
|
+
await opts.onDecision({ at: new Date().toISOString(), resource: resource.name, lane: res.lane, status: res.status, badge, badgeValid, reasons, mode });
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
/* a logging failure never changes the answer */
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
/** Fast lane explanation sent with the free tier's 429. */
|
|
49
|
+
function fastLaneHelp(retryAfter) {
|
|
50
|
+
return {
|
|
51
|
+
error: "free_tier_used",
|
|
52
|
+
message: `That's the free tier for this hour (${freeTier?.perHour} requests). Come back in ${Math.ceil(retryAfter / 60)} minutes, or take the fast lane.`,
|
|
53
|
+
fastLane: {
|
|
54
|
+
how: "Send X-Agent-Badge with a Verified by Concordium badge. Herald checks the badge, then asks for an x402 payment from the badge's agentWallet. Paid requests get the full content.",
|
|
55
|
+
price: { amountAtomic: price, asset: opts.asset ?? "CCD" },
|
|
56
|
+
register: "/agents",
|
|
57
|
+
},
|
|
58
|
+
retryAfter,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async function handle(req, resource, sourceOverride) {
|
|
62
|
+
const h = req.headers;
|
|
63
|
+
const badged = hasAgentCredentials(h);
|
|
64
|
+
// Log-only: never block. Run the free checks on any badge, record what would have happened, serve the content.
|
|
65
|
+
if (mode === "log-only") {
|
|
66
|
+
let badgeValid = null;
|
|
67
|
+
let badge = null;
|
|
68
|
+
let reasons = [];
|
|
69
|
+
if (h["x-agent-badge"] || h["x-agent-card"]) {
|
|
70
|
+
try {
|
|
71
|
+
const c = await opts.engine.check(h, opts.trustedRegistries);
|
|
72
|
+
badgeValid = c.passed;
|
|
73
|
+
badge = c.badge;
|
|
74
|
+
reasons = c.reasons;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
badgeValid = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const res = { status: 200, headers: { "x-herald-would": badgeValid === null ? "free" : badgeValid ? "fast" : "refuse" }, body: await resource.produce(), lane: "log" };
|
|
81
|
+
return decide(resource, res, badge, badgeValid, reasons);
|
|
82
|
+
}
|
|
83
|
+
// No badge and a free tier: teaser while it lasts, then a 429 that explains the fast lane.
|
|
84
|
+
if (!badged && freeTier && counter) {
|
|
85
|
+
const n = await counter.hit(`${resource.name.split(":")[0]}|${req.ip}`, windowMs);
|
|
86
|
+
if (n <= freeTier.perHour) {
|
|
87
|
+
const body = resource.teaser ? await resource.teaser() : { message: "Free tier." };
|
|
88
|
+
const res = { status: 200, headers: { "x-herald-free-remaining": String(freeTier.perHour - n) }, body, lane: "free" };
|
|
89
|
+
return decide(resource, res, null, null, []);
|
|
90
|
+
}
|
|
91
|
+
const retryAfter = Math.max(1, Math.ceil((windowMs - (Date.now() % windowMs)) / 1000));
|
|
92
|
+
const res = { status: 429, headers: { "retry-after": String(retryAfter) }, body: fastLaneHelp(retryAfter), lane: "throttled" };
|
|
93
|
+
return decide(resource, res, null, null, ["free tier used"]);
|
|
94
|
+
}
|
|
95
|
+
// Badge (or no free tier): the full door. A bad badge is refused with the failing step: worse than none.
|
|
96
|
+
const r = await opts.engine.knock({
|
|
97
|
+
resource: resource.name,
|
|
98
|
+
description: resource.description,
|
|
99
|
+
url: req.url,
|
|
100
|
+
headers: h,
|
|
101
|
+
payTo: opts.payTo,
|
|
102
|
+
priceMicroCcd: price,
|
|
103
|
+
priceAsset: opts.asset ?? "CCD",
|
|
104
|
+
produce: resource.produce,
|
|
105
|
+
source: sourceOverride ?? source,
|
|
106
|
+
trustedRegistries: opts.trustedRegistries,
|
|
107
|
+
});
|
|
108
|
+
const b = r.body;
|
|
109
|
+
const lane = laneOf(r.status);
|
|
110
|
+
const failedId = b.checks?.find((c) => c.status === "fail")?.id ?? null;
|
|
111
|
+
// A badge counts as valid if it got past the free checks: admitted, asked to pay, or refused only at payment.
|
|
112
|
+
const badgeValid = !badged ? null : lane === "fast" || lane === "payment_required" || (failedId !== null && PAYMENT_STEPS.has(failedId));
|
|
113
|
+
return decide(resource, { status: r.status, headers: { ...r.headers }, body: r.body, lane }, b.badge ?? null, badgeValid, b.reasons ?? []);
|
|
114
|
+
}
|
|
115
|
+
/** Next.js App Router: `export const GET = door.next((req, ctx) => resource)`. */
|
|
116
|
+
function next(resolve) {
|
|
117
|
+
return async (req, ctx) => {
|
|
118
|
+
const resource = await resolve(req, ctx);
|
|
119
|
+
if (!resource)
|
|
120
|
+
return Response.json({ error: "not_found", message: "No such resource." }, { status: 404 });
|
|
121
|
+
const headers = {};
|
|
122
|
+
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";
|
|
124
|
+
const r = await handle({ url: req.url, headers, ip }, resource);
|
|
125
|
+
return Response.json(r.body, {
|
|
126
|
+
status: r.status,
|
|
127
|
+
headers: { ...r.headers, "access-control-allow-origin": "*", "access-control-expose-headers": "PAYMENT-REQUIRED, PAYMENT-RESPONSE, X-Herald-Lane, X-Herald-Pass, Retry-After" },
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** Express-style middleware: `app.get("/articles/:slug", door.express(req => resource))`. */
|
|
132
|
+
function express(resolve) {
|
|
133
|
+
return async (req, res, nextFn) => {
|
|
134
|
+
try {
|
|
135
|
+
const resource = await resolve(req);
|
|
136
|
+
if (!resource)
|
|
137
|
+
return res.status(404).json({ error: "not_found", message: "No such resource." });
|
|
138
|
+
const headers = {};
|
|
139
|
+
for (const [k, v] of Object.entries(req.headers))
|
|
140
|
+
headers[k.toLowerCase()] = Array.isArray(v) ? v[0] : v;
|
|
141
|
+
const host = req.get?.("host") ?? headers.host ?? "localhost";
|
|
142
|
+
const url = `${req.protocol ?? "https"}://${host}${req.originalUrl ?? req.url ?? "/"}`;
|
|
143
|
+
const r = await handle({ url, headers, ip: req.ip ?? "unknown" }, resource);
|
|
144
|
+
return res.status(r.status).set(r.headers).json(r.body);
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
if (nextFn)
|
|
148
|
+
return nextFn(e);
|
|
149
|
+
throw e;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return { handle, next, express, mode };
|
|
154
|
+
}
|
|
155
|
+
export const internal = { laneOf, hasAgentCredentials };
|
|
156
|
+
/**
|
|
157
|
+
* An engine for a server that doesn't run Herald itself: the free checks go to Herald's public
|
|
158
|
+
* `GET /api/check?badge=` (no keys, no payment). Enough for log-only mode. For the paid fast lane,
|
|
159
|
+
* put the site behind a Herald publisher door (`/api/p/<id>/...`) instead; `knock` here answers 503.
|
|
160
|
+
*/
|
|
161
|
+
export function remoteCheckEngine(opts = {}) {
|
|
162
|
+
const base = (opts.base ?? "https://useherald.vercel.app").replace(/\/$/, "");
|
|
163
|
+
const f = opts.fetch ?? fetch;
|
|
164
|
+
return {
|
|
165
|
+
async knock() {
|
|
166
|
+
return {
|
|
167
|
+
status: 503,
|
|
168
|
+
headers: {},
|
|
169
|
+
body: { error: "unavailable", message: "This server only runs Herald's free checks (log-only). The paid fast lane runs behind a Herald publisher door." },
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
async check(headers) {
|
|
173
|
+
const badge = headers["x-agent-badge"];
|
|
174
|
+
if (!badge)
|
|
175
|
+
return { badge: null, passed: false, reasons: ["no badge"], failedStep: 1 };
|
|
176
|
+
const r = await f(`${base}/api/check?badge=${encodeURIComponent(badge)}`, { signal: AbortSignal.timeout(opts.timeoutMs ?? 8000) });
|
|
177
|
+
const j = (await r.json());
|
|
178
|
+
// "warn" means Herald couldn't confirm right now: unknown, not a refusal.
|
|
179
|
+
if (j.verdict !== "pass" && j.verdict !== "refused")
|
|
180
|
+
throw new Error("not confirmed right now");
|
|
181
|
+
return { badge: j.badge ?? badge, passed: j.verdict === "pass", reasons: j.reasons ?? [], failedStep: j.checks?.find((c) => c.status === "fail")?.step ?? null };
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "herald-door",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "levithefirst",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"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
|
+
"keywords": [
|
|
10
|
+
"concordium",
|
|
11
|
+
"x402",
|
|
12
|
+
"ai-agents",
|
|
13
|
+
"pay-per-crawl"
|
|
14
|
+
],
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc -p tsconfig.build.json"
|
|
30
|
+
}
|
|
6
31
|
}
|