agentrail-edge 0.1.0 → 0.2.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/README.md +65 -2
- package/dist/edge.d.ts +46 -1
- package/dist/edge.js +160 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
The Agentrail edge, in front of a site on the Fetch API: Cloudflare Workers,
|
|
4
4
|
Vercel Routing Middleware, or anything else that speaks it. Each request is
|
|
5
5
|
answered through the site's origin and recorded as one event, sent to the
|
|
6
|
-
Agentrail collector after the answer through the platform's `waitUntil
|
|
7
|
-
|
|
6
|
+
Agentrail collector after the answer through the platform's `waitUntil`. A local
|
|
7
|
+
HMAC operation signs the origin trace handoff before forwarding the request.
|
|
8
8
|
|
|
9
9
|
In an answer meant for agents (Markdown, `llms.txt`), every link to the
|
|
10
10
|
site's own hosts is marked, so the request an agent makes by following one
|
|
@@ -70,6 +70,23 @@ In Routing Middleware, `vercel({ waitUntil, origin })` takes `waitUntil`
|
|
|
70
70
|
from `@vercel/functions` and fetches the site itself, since middleware cannot
|
|
71
71
|
rewrite an answer it passes on. Create the edge once, outside the handler.
|
|
72
72
|
|
|
73
|
+
## Backend trace handoff
|
|
74
|
+
|
|
75
|
+
Each forwarded request carries a new `traceparent` and an
|
|
76
|
+
`x-agentrail-trace-handoff` token authenticated with the workspace hash key.
|
|
77
|
+
The token binds the trace parent, key ID, issue time, HTTP method and final
|
|
78
|
+
origin path/query. The backend accepts it for 60 seconds. The caller's handoff,
|
|
79
|
+
tracestate and baggage are removed; the handoff response header is removed
|
|
80
|
+
before answering the visitor. A signing failure reaches `onError` as
|
|
81
|
+
`TraceHandoffSigningError` and the application request continues unsigned.
|
|
82
|
+
|
|
83
|
+
Apply path/query rewrites in `EdgeAdapter.prepareOriginRequest(request)`, or
|
|
84
|
+
`vercel({ waitUntil, prepareOriginRequest, origin })`. Signing happens after
|
|
85
|
+
that hook. The `origin` callback sends the prepared request; host-only changes
|
|
86
|
+
preserve the signature. A path/query change inside `origin` prevents backend
|
|
87
|
+
verification. `agentrail-node` starts a new trace for unsigned or refused
|
|
88
|
+
handoffs and keeps the caller's context as a directional span link.
|
|
89
|
+
|
|
73
90
|
## Options
|
|
74
91
|
|
|
75
92
|
- `hosts`: the site's own hosts, and how a link to each is marked: `'query'`
|
|
@@ -81,6 +98,8 @@ rewrite an answer it passes on. Create the edge once, outside the handler.
|
|
|
81
98
|
own check of its method, URL and headers. It runs after the answer. A
|
|
82
99
|
check that throws is told to `onError` as an `IdentifyError`, and the
|
|
83
100
|
request is recorded without a principal.
|
|
101
|
+
- `ignoreRequest(request)`: requests passed to the origin untouched and
|
|
102
|
+
recorded nowhere. See [Your own servers](#your-own-servers).
|
|
84
103
|
- `sessionIdleTimeout`: seconds without a request after which a session
|
|
85
104
|
ends; 1800 (30 minutes) by default.
|
|
86
105
|
- `sessionCookieDomain`: the domain a session is shared across, as
|
|
@@ -88,10 +107,54 @@ rewrite an answer it passes on. Create the edge once, outside the handler.
|
|
|
88
107
|
the one host. Give the browser script the same two values.
|
|
89
108
|
- `sessionCookie`, `service`, `fetch`: the session cookie's name, the
|
|
90
109
|
service the events name, and the fetch the collector's calls go through.
|
|
110
|
+
- `signers`: registrable hosts, beside the known signers, whose Web Bot
|
|
111
|
+
Auth key directories the edge may read; a signature by any other host is
|
|
112
|
+
recorded as a claim it did not check, and that host is never contacted.
|
|
91
113
|
- `onError(error)`: told of events that could not be sent, and of an
|
|
92
114
|
`identify` that threw; they are logged otherwise. Neither ever reaches the
|
|
93
115
|
visitor.
|
|
94
116
|
|
|
117
|
+
## Your own servers
|
|
118
|
+
|
|
119
|
+
Calls your own servers make through this edge are not visitors or agents. A
|
|
120
|
+
page rendered on the server that reads your API would otherwise record a step
|
|
121
|
+
for every read, all from a few addresses at a steady rate, and those steps
|
|
122
|
+
crowd out the journeys you want to see.
|
|
123
|
+
|
|
124
|
+
Leave them out with something only your servers can send:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
const edge = createEdge({
|
|
128
|
+
// ...
|
|
129
|
+
ignoreRequest: (request) =>
|
|
130
|
+
request.headers.get('x-my-internal') === env.INTERNAL_SECRET,
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
A user agent or an address will not do: any caller can claim one, and a caller
|
|
135
|
+
who matched it would never be recorded. Compare a secret in constant time
|
|
136
|
+
where your platform offers it.
|
|
137
|
+
|
|
138
|
+
## What it records of the caller
|
|
139
|
+
|
|
140
|
+
Beside the user agent, each event carries what the caller and the network
|
|
141
|
+
said of it, for the collector to read as features (never as proofs) when it
|
|
142
|
+
asks who the caller is: the `Accept` sent, and the `Sec-Fetch-Site`, `-Mode`
|
|
143
|
+
and `-Dest` a browser sends and a programmatic fetcher does not; what the
|
|
144
|
+
platform says of the client, where it says anything (`cloudflare(ctx)` reads
|
|
145
|
+
`request.cf`: the autonomous system and country on every plan, the JA4
|
|
146
|
+
fingerprint and the verified-bot category where the zone has Bot Management;
|
|
147
|
+
`vercel()` reads `x-vercel-ip-country` and `x-vercel-ja4-digest`, and BotID's
|
|
148
|
+
verified bots stay in the app, where `checkBotId()` runs; an adapter of your
|
|
149
|
+
own gives `signals(request)` or nothing); and a Web Bot Auth signature,
|
|
150
|
+
checked against the keys the agent publishes. The check reads the agent's
|
|
151
|
+
key directory from the edge, once per its `max-age`, from the known signers'
|
|
152
|
+
hosts and those `signers` names and from no other, so a site's edge contacts
|
|
153
|
+
only hosts it has been told of; a directory that cannot be read leaves the
|
|
154
|
+
signature `directory_unreachable`, never `invalid`.
|
|
155
|
+
Nothing here is hashed, since nothing identifies a person: a fingerprint is
|
|
156
|
+
shared by every copy of one client.
|
|
157
|
+
|
|
95
158
|
## Licence
|
|
96
159
|
|
|
97
160
|
[Apache-2.0](LICENSE).
|
package/dist/edge.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ import { type MarkedHosts } from './links.js';
|
|
|
4
4
|
export declare class IdentifyError extends Error {
|
|
5
5
|
readonly name = "IdentifyError";
|
|
6
6
|
}
|
|
7
|
+
/** The site's `ignoreRequest` threw: the request is recorded as any other. */
|
|
8
|
+
export declare class IgnoreRequestError extends Error {
|
|
9
|
+
readonly name = "IgnoreRequestError";
|
|
10
|
+
}
|
|
7
11
|
/**
|
|
8
12
|
* What the site's own check reads of a request: never its body, which has
|
|
9
13
|
* gone to the origin by the time the check runs.
|
|
@@ -15,6 +19,9 @@ export interface Principal {
|
|
|
15
19
|
readonly id: string;
|
|
16
20
|
readonly actingUserId?: string | null;
|
|
17
21
|
}
|
|
22
|
+
export declare class TraceHandoffSigningError extends Error {
|
|
23
|
+
readonly name = "TraceHandoffSigningError";
|
|
24
|
+
}
|
|
18
25
|
export interface EdgeOptions {
|
|
19
26
|
/** Where events go, and the edge source's secret. */
|
|
20
27
|
readonly collector: {
|
|
@@ -44,6 +51,16 @@ export interface EdgeOptions {
|
|
|
44
51
|
* method, URL and headers. It runs after the answer.
|
|
45
52
|
*/
|
|
46
53
|
readonly identify?: (request: RequestHead) => Principal | null | Promise<Principal | null>;
|
|
54
|
+
/**
|
|
55
|
+
* Requests this edge passes to the origin untouched and records nothing
|
|
56
|
+
* of, by the site's own check of their method, URL and headers: the calls
|
|
57
|
+
* the site's own servers make, which are neither a visitor nor an agent.
|
|
58
|
+
* Check something only your servers can send, such as a shared secret: a
|
|
59
|
+
* user agent or an address is what any caller can claim. A check that
|
|
60
|
+
* throws is told to `onError` as an `IgnoreRequestError`, and the request
|
|
61
|
+
* is recorded as any other.
|
|
62
|
+
*/
|
|
63
|
+
readonly ignoreRequest?: (request: RequestHead) => boolean;
|
|
47
64
|
/** The service the events name; the request's host by default. */
|
|
48
65
|
readonly service?: string;
|
|
49
66
|
/**
|
|
@@ -51,17 +68,44 @@ export interface EdgeOptions {
|
|
|
51
68
|
* Logs by default.
|
|
52
69
|
*/
|
|
53
70
|
readonly onError?: (error: Error) => void;
|
|
71
|
+
/**
|
|
72
|
+
* Registrable hosts, beside the known signers, whose Web Bot Auth key
|
|
73
|
+
* directories this edge may read; a signature by any other host is
|
|
74
|
+
* recorded as an unsupported claim, and that host is never contacted.
|
|
75
|
+
*/
|
|
76
|
+
readonly signers?: readonly string[];
|
|
54
77
|
/** The fetch the collector's calls go through; the platform's own by default. */
|
|
55
78
|
readonly fetch?: typeof fetch;
|
|
56
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* What the platform tells the edge of the client beyond its address, where
|
|
82
|
+
* it tells anything: features for the collector's classifier, never keys.
|
|
83
|
+
*/
|
|
84
|
+
export interface PlatformSignals {
|
|
85
|
+
/** The client's autonomous system number. */
|
|
86
|
+
readonly asn: number | null;
|
|
87
|
+
/** The client's country, two letters. */
|
|
88
|
+
readonly country: string | null;
|
|
89
|
+
/** The client's TLS fingerprint, JA4. */
|
|
90
|
+
readonly ja4: string | null;
|
|
91
|
+
/** A bot the platform verified against its own directory. */
|
|
92
|
+
readonly verifiedBot: {
|
|
93
|
+
readonly category: string;
|
|
94
|
+
readonly name: string | null;
|
|
95
|
+
} | null;
|
|
96
|
+
}
|
|
57
97
|
/** What the platform the edge runs on provides. */
|
|
58
98
|
export interface EdgeAdapter {
|
|
59
99
|
/** The client's address, as the platform saw it; null when it says none. */
|
|
60
100
|
clientAddress(request: Request): string | null;
|
|
101
|
+
/** What else the platform says of the client; absent on a platform that says nothing. */
|
|
102
|
+
signals?(request: Request): PlatformSignals;
|
|
61
103
|
/** Keeps the platform running work past the answer. */
|
|
62
104
|
waitUntil(work: Promise<unknown>): void;
|
|
63
105
|
/** Sends the request on to the site. */
|
|
64
106
|
origin(request: Request): Promise<Response>;
|
|
107
|
+
/** Apply path/query rewrites here, before the final request is signed. */
|
|
108
|
+
prepareOriginRequest?(request: Request): Request | Promise<Request>;
|
|
65
109
|
/** This edge instance, when the platform names one. */
|
|
66
110
|
readonly instance: string | null;
|
|
67
111
|
}
|
|
@@ -70,7 +114,7 @@ export interface Edge {
|
|
|
70
114
|
handle(request: Request, adapter: EdgeAdapter): Promise<Response>;
|
|
71
115
|
}
|
|
72
116
|
export declare function createEdge(options: EdgeOptions): Edge;
|
|
73
|
-
/** Cloudflare Workers: the platform's client address, and the Worker's `waitUntil`. */
|
|
117
|
+
/** Cloudflare Workers: the platform's client address, its word on the client, and the Worker's `waitUntil`. */
|
|
74
118
|
export declare function cloudflare(context: {
|
|
75
119
|
waitUntil(work: Promise<unknown>): void;
|
|
76
120
|
}, origin?: (request: Request) => Promise<Response>): EdgeAdapter;
|
|
@@ -83,4 +127,5 @@ export declare function cloudflare(context: {
|
|
|
83
127
|
export declare function vercel(options: {
|
|
84
128
|
readonly waitUntil: (work: Promise<unknown>) => void;
|
|
85
129
|
readonly origin: (request: Request) => Promise<Response>;
|
|
130
|
+
readonly prepareOriginRequest?: EdgeAdapter['prepareOriginRequest'];
|
|
86
131
|
}): EdgeAdapter;
|
package/dist/edge.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createCollector, formatTraceparent, hashKeyIdOf, keyedHash, newSessionId, newSpanId, newTraceId, parseMarker, parseTraceparent, sessionCookie, VERSION, } from 'agentrail-core';
|
|
1
|
+
import { createCollector, TRACE_HANDOFF_HEADER, signTraceHandoff, traceHandoffTarget, createSignatureVerifier, formatTraceparent, hashKeyIdOf, keyedHash, newSessionId, newSpanId, newTraceId, parseMarker, parseTraceparent, sessionCookie, VERSION, } from 'agentrail-core';
|
|
2
2
|
import { MARKER_PARAMETER, markLinks } from './links.js';
|
|
3
3
|
/**
|
|
4
4
|
* The edge adapter: in front of a customer's site, on the Fetch API, it
|
|
@@ -13,7 +13,11 @@ import { MARKER_PARAMETER, markLinks } from './links.js';
|
|
|
13
13
|
* `traceparent`, with any caller's trace kept as a span link only: a public
|
|
14
14
|
* endpoint never takes its parent from outside;
|
|
15
15
|
* - the client address and any verified principal, hashed with the
|
|
16
|
-
* workspace's key before anything leaves
|
|
16
|
+
* workspace's key before anything leaves;
|
|
17
|
+
* - what the caller and the network said of it, for the collector's
|
|
18
|
+
* classifier of who the caller is: the `Accept` and fetch metadata, the
|
|
19
|
+
* platform's word on the network and its verified bots, and a Web Bot
|
|
20
|
+
* Auth signature checked against the keys the agent publishes.
|
|
17
21
|
*
|
|
18
22
|
* The events go to the collector after the answer, through the platform's
|
|
19
23
|
* `waitUntil`, so recording never slows a request. Requests one edge answers
|
|
@@ -32,10 +36,25 @@ const MARKABLE_BYTES = 2 * 1024 * 1024;
|
|
|
32
36
|
* `identify`.
|
|
33
37
|
*/
|
|
34
38
|
const SEND_WITHIN_MS = 25_000;
|
|
39
|
+
/** The longest `Accept` the collector takes; a longer one is cut. */
|
|
40
|
+
const ACCEPT_LENGTH = 256;
|
|
41
|
+
/** The longest `Sec-Fetch-*` value the collector takes; the browsers' are one word. */
|
|
42
|
+
const FETCH_METADATA_LENGTH = 32;
|
|
43
|
+
/** The longest JA4 the collector takes. */
|
|
44
|
+
const JA4_LENGTH = 64;
|
|
45
|
+
/** The longest verified-bot category the collector takes. */
|
|
46
|
+
const BOT_CATEGORY_LENGTH = 64;
|
|
35
47
|
/** The site's `identify` threw: the request is recorded without a principal. */
|
|
36
48
|
export class IdentifyError extends Error {
|
|
37
49
|
name = 'IdentifyError';
|
|
38
50
|
}
|
|
51
|
+
/** The site's `ignoreRequest` threw: the request is recorded as any other. */
|
|
52
|
+
export class IgnoreRequestError extends Error {
|
|
53
|
+
name = 'IgnoreRequestError';
|
|
54
|
+
}
|
|
55
|
+
export class TraceHandoffSigningError extends Error {
|
|
56
|
+
name = 'TraceHandoffSigningError';
|
|
57
|
+
}
|
|
39
58
|
/**
|
|
40
59
|
* The markers a query carries in `h`, and the query without them, every
|
|
41
60
|
* other pair exactly as it came: a value that does not read as a marker is
|
|
@@ -136,7 +155,8 @@ export function createEdge(options) {
|
|
|
136
155
|
// Every call names the key its events were hashed with. Its id is a
|
|
137
156
|
// digest, which WebCrypto only gives asynchronously, so the collector is
|
|
138
157
|
// ready from the first send on, never on a request's path.
|
|
139
|
-
const
|
|
158
|
+
const keyId = hashKeyIdOf(hashKey);
|
|
159
|
+
const collector = keyId.then((hashKeyId) => createCollector({
|
|
140
160
|
endpoint: options.collector.endpoint,
|
|
141
161
|
secret: options.collector.secret,
|
|
142
162
|
component: {
|
|
@@ -148,6 +168,39 @@ export function createEdge(options) {
|
|
|
148
168
|
onError,
|
|
149
169
|
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
150
170
|
}));
|
|
171
|
+
// Checks each signed request against the keys its agent publishes; the
|
|
172
|
+
// directories are read once per `max-age` for the life of the edge.
|
|
173
|
+
const verify = createSignatureVerifier({
|
|
174
|
+
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
175
|
+
...(options.signers ? { signers: options.signers } : {}),
|
|
176
|
+
});
|
|
177
|
+
async function requestForOrigin(request, adapter, span) {
|
|
178
|
+
const prepared = await (adapter.prepareOriginRequest?.(request) ?? request);
|
|
179
|
+
const headers = new Headers(prepared.headers);
|
|
180
|
+
const traceparent = formatTraceparent(span);
|
|
181
|
+
headers.set('traceparent', traceparent);
|
|
182
|
+
headers.delete('tracestate');
|
|
183
|
+
headers.delete('baggage');
|
|
184
|
+
headers.delete(TRACE_HANDOFF_HEADER);
|
|
185
|
+
try {
|
|
186
|
+
headers.set(TRACE_HANDOFF_HEADER, await signTraceHandoff({
|
|
187
|
+
traceparent,
|
|
188
|
+
hashKeyId: await keyId,
|
|
189
|
+
issuedAt: Date.now(),
|
|
190
|
+
method: prepared.method,
|
|
191
|
+
target: traceHandoffTarget(prepared.url),
|
|
192
|
+
}, hashKey));
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
const failure = new TraceHandoffSigningError('Agentrail could not sign the origin trace handoff.', { cause: error });
|
|
196
|
+
// Reporting may throw in the site's callback; it belongs to waitUntil,
|
|
197
|
+
// so a capture failure still forwards the application request unsigned.
|
|
198
|
+
adapter.waitUntil(Promise.resolve().then(() => {
|
|
199
|
+
onError(failure);
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
return new Request(prepared, { headers });
|
|
203
|
+
}
|
|
151
204
|
/** A path as it is recorded: the allowlisted query only. */
|
|
152
205
|
function recordedPath(url) {
|
|
153
206
|
const kept = new URLSearchParams([...url.searchParams].filter(([name]) => allowlist.has(name)));
|
|
@@ -225,6 +278,8 @@ export function createEdge(options) {
|
|
|
225
278
|
const location = recordedLocation(observed.location, url);
|
|
226
279
|
const principal = await principalOf(request);
|
|
227
280
|
const actingUserId = principal?.actingUserId ?? null;
|
|
281
|
+
const signature = await verify(request);
|
|
282
|
+
const { signals } = input;
|
|
228
283
|
return {
|
|
229
284
|
schemaVersion: 1,
|
|
230
285
|
id: crypto.randomUUID(),
|
|
@@ -263,6 +318,24 @@ export function createEdge(options) {
|
|
|
263
318
|
: await keyedHash(hashKey, input.address),
|
|
264
319
|
userAgent: request.headers.get('user-agent'),
|
|
265
320
|
client: null,
|
|
321
|
+
clientVersion: null,
|
|
322
|
+
protocol: null,
|
|
323
|
+
accept: cut(request.headers.get('accept'), ACCEPT_LENGTH),
|
|
324
|
+
fetchMetadata: fetchMetadataOf(request.headers),
|
|
325
|
+
network: signals === null
|
|
326
|
+
? null
|
|
327
|
+
: {
|
|
328
|
+
asn: signals.asn,
|
|
329
|
+
country: signals.country,
|
|
330
|
+
ja4: cut(signals.ja4, JA4_LENGTH),
|
|
331
|
+
},
|
|
332
|
+
signature,
|
|
333
|
+
verifiedBot: signals?.verifiedBot === null || signals === null
|
|
334
|
+
? null
|
|
335
|
+
: {
|
|
336
|
+
category: signals.verifiedBot.category.slice(0, BOT_CATEGORY_LENGTH),
|
|
337
|
+
name: signals.verifiedBot.name,
|
|
338
|
+
},
|
|
266
339
|
},
|
|
267
340
|
principal: principal === null
|
|
268
341
|
? null
|
|
@@ -296,8 +369,33 @@ export function createEdge(options) {
|
|
|
296
369
|
],
|
|
297
370
|
};
|
|
298
371
|
}
|
|
372
|
+
/** Whether the site's own check leaves this request out; a broken check leaves nothing out. */
|
|
373
|
+
function ignored(request, adapter) {
|
|
374
|
+
if (options.ignoreRequest === undefined)
|
|
375
|
+
return false;
|
|
376
|
+
try {
|
|
377
|
+
return options.ignoreRequest(request);
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
const failure = new IgnoreRequestError("The site's ignoreRequest threw; the request is recorded.", { cause: error });
|
|
381
|
+
adapter.waitUntil(Promise.resolve().then(() => {
|
|
382
|
+
onError(failure);
|
|
383
|
+
}));
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
299
387
|
return {
|
|
300
388
|
async handle(request, adapter) {
|
|
389
|
+
if (ignored(request, adapter)) {
|
|
390
|
+
// The site's own rewrite still applies: it decides where a request
|
|
391
|
+
// goes, which is nothing to do with whether it is recorded.
|
|
392
|
+
const prepared = await (adapter.prepareOriginRequest?.(request) ??
|
|
393
|
+
request);
|
|
394
|
+
const headers = new Headers(prepared.headers);
|
|
395
|
+
// Only this edge signs a handoff, whatever the caller sent.
|
|
396
|
+
headers.delete(TRACE_HANDOFF_HEADER);
|
|
397
|
+
return adapter.origin(new Request(prepared, { headers }));
|
|
398
|
+
}
|
|
301
399
|
const start = Date.now();
|
|
302
400
|
const url = new URL(request.url);
|
|
303
401
|
const query = markersOffQuery(url.search);
|
|
@@ -318,6 +416,8 @@ export function createEdge(options) {
|
|
|
318
416
|
headers.set('traceparent', formatTraceparent(span));
|
|
319
417
|
// The caller's vendor state belongs to the caller's trace.
|
|
320
418
|
headers.delete('tracestate');
|
|
419
|
+
headers.delete('baggage');
|
|
420
|
+
headers.delete(TRACE_HANDOFF_HEADER);
|
|
321
421
|
const forwarded = new Request(originUrl, {
|
|
322
422
|
method: request.method,
|
|
323
423
|
headers,
|
|
@@ -349,12 +449,13 @@ export function createEdge(options) {
|
|
|
349
449
|
try {
|
|
350
450
|
// Links resolve against the address the origin answered for, so a
|
|
351
451
|
// relative one never carries the marker this request came with.
|
|
352
|
-
const answer = await marked(request.method, originUrl, await adapter.origin(forwarded), observed);
|
|
452
|
+
const answer = await marked(request.method, originUrl, await adapter.origin(await requestForOrigin(forwarded, adapter, span)), observed);
|
|
353
453
|
observed.status = answer.status;
|
|
354
454
|
observed.location = answer.headers.get('location');
|
|
355
455
|
observed.contentType = answer.headers.get('content-type');
|
|
356
456
|
// On every answer, so the session lasts another idle timeout.
|
|
357
457
|
const withCookie = new Response(answer.body, answer);
|
|
458
|
+
withCookie.headers.delete(TRACE_HANDOFF_HEADER);
|
|
358
459
|
withCookie.headers.append('set-cookie', cookie.write(session, url.protocol === 'https:'));
|
|
359
460
|
return withCookie;
|
|
360
461
|
}
|
|
@@ -368,6 +469,7 @@ export function createEdge(options) {
|
|
|
368
469
|
request: head,
|
|
369
470
|
url,
|
|
370
471
|
address: adapter.clientAddress(request),
|
|
472
|
+
signals: adapter.signals?.(request) ?? null,
|
|
371
473
|
instance: adapter.instance,
|
|
372
474
|
start,
|
|
373
475
|
end,
|
|
@@ -382,10 +484,52 @@ export function createEdge(options) {
|
|
|
382
484
|
},
|
|
383
485
|
};
|
|
384
486
|
}
|
|
385
|
-
/**
|
|
487
|
+
/** A header or platform value cut to what the collector takes; null for none or an empty one. */
|
|
488
|
+
function cut(value, length) {
|
|
489
|
+
return typeof value === 'string' && value.length > 0
|
|
490
|
+
? value.slice(0, length)
|
|
491
|
+
: null;
|
|
492
|
+
}
|
|
493
|
+
/** `Sec-Fetch-Site`, `-Mode` and `-Dest` together, as a browser sends all three. */
|
|
494
|
+
function fetchMetadataOf(headers) {
|
|
495
|
+
const site = cut(headers.get('sec-fetch-site'), FETCH_METADATA_LENGTH);
|
|
496
|
+
const mode = cut(headers.get('sec-fetch-mode'), FETCH_METADATA_LENGTH);
|
|
497
|
+
const dest = cut(headers.get('sec-fetch-dest'), FETCH_METADATA_LENGTH);
|
|
498
|
+
return site === null || mode === null || dest === null
|
|
499
|
+
? null
|
|
500
|
+
: { site, mode, dest };
|
|
501
|
+
}
|
|
502
|
+
/** A two-letter country, as the platforms give one; anything else is none. */
|
|
503
|
+
function countryOf(value) {
|
|
504
|
+
return typeof value === 'string' && /^[A-Z]{2}$/u.test(value) ? value : null;
|
|
505
|
+
}
|
|
506
|
+
function recordOf(value) {
|
|
507
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
508
|
+
? value
|
|
509
|
+
: null;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* What Cloudflare tells a Worker of the client in `request.cf`: its
|
|
513
|
+
* autonomous system and country on every plan; its JA4 under
|
|
514
|
+
* `botManagement`, and the category of a verified bot, where the zone's
|
|
515
|
+
* plan has Bot Management.
|
|
516
|
+
*/
|
|
517
|
+
function cloudflareSignals(request) {
|
|
518
|
+
const cf = recordOf(request.cf);
|
|
519
|
+
const bot = recordOf(cf?.botManagement);
|
|
520
|
+
const category = cut(typeof cf?.verifiedBotCategory === 'string' ? cf.verifiedBotCategory : null, BOT_CATEGORY_LENGTH);
|
|
521
|
+
return {
|
|
522
|
+
asn: typeof cf?.asn === 'number' ? cf.asn : null,
|
|
523
|
+
country: countryOf(cf?.country),
|
|
524
|
+
ja4: typeof bot?.ja4 === 'string' ? bot.ja4 : null,
|
|
525
|
+
verifiedBot: category === null ? null : { category, name: null },
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
/** Cloudflare Workers: the platform's client address, its word on the client, and the Worker's `waitUntil`. */
|
|
386
529
|
export function cloudflare(context, origin = (request) => fetch(request)) {
|
|
387
530
|
return {
|
|
388
531
|
clientAddress: (request) => request.headers.get('cf-connecting-ip'),
|
|
532
|
+
signals: cloudflareSignals,
|
|
389
533
|
waitUntil: (work) => {
|
|
390
534
|
context.waitUntil(work);
|
|
391
535
|
},
|
|
@@ -404,6 +548,17 @@ export function vercel(options) {
|
|
|
404
548
|
clientAddress: (request) => request.headers.get('x-real-ip') ??
|
|
405
549
|
request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ??
|
|
406
550
|
null,
|
|
551
|
+
// Vercel's edge names the country and the JA4 in headers; its verified
|
|
552
|
+
// bots are BotID's, read by `checkBotId()` in the app, not here.
|
|
553
|
+
signals: (request) => ({
|
|
554
|
+
asn: null,
|
|
555
|
+
country: countryOf(request.headers.get('x-vercel-ip-country')),
|
|
556
|
+
ja4: request.headers.get('x-vercel-ja4-digest'),
|
|
557
|
+
verifiedBot: null,
|
|
558
|
+
}),
|
|
559
|
+
...(options.prepareOriginRequest === undefined
|
|
560
|
+
? {}
|
|
561
|
+
: { prepareOriginRequest: options.prepareOriginRequest }),
|
|
407
562
|
waitUntil: options.waitUntil,
|
|
408
563
|
origin: options.origin,
|
|
409
564
|
instance: null,
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* Vercel or anything else on the Fetch API: one event per request, sent to
|
|
4
4
|
* the collector, with markers, a session and a trace of its own.
|
|
5
5
|
*/
|
|
6
|
-
export { IdentifyError, cloudflare, createEdge, vercel, type Edge, type EdgeAdapter, type EdgeOptions, type Principal, type RequestHead, } from './edge.js';
|
|
6
|
+
export { IdentifyError, IgnoreRequestError, TraceHandoffSigningError, cloudflare, createEdge, vercel, type Edge, type EdgeAdapter, type EdgeOptions, type PlatformSignals, type Principal, type RequestHead, } from './edge.js';
|
|
7
7
|
export { MARKER_PARAMETER, markLinks, type MarkedHosts } from './links.js';
|
package/dist/index.js
CHANGED
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* Vercel or anything else on the Fetch API: one event per request, sent to
|
|
4
4
|
* the collector, with markers, a session and a trace of its own.
|
|
5
5
|
*/
|
|
6
|
-
export { IdentifyError, cloudflare, createEdge, vercel, } from './edge.js';
|
|
6
|
+
export { IdentifyError, IgnoreRequestError, TraceHandoffSigningError, cloudflare, createEdge, vercel, } from './edge.js';
|
|
7
7
|
export { MARKER_PARAMETER, markLinks } from './links.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentrail-edge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The Agentrail edge: in front of a site on Cloudflare Workers, Vercel or any Fetch API runtime, it records each request and marks the links agents follow.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"node": ">=22"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"agentrail-core": "0.
|
|
28
|
+
"agentrail-core": "0.2.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "24.13.3",
|