@astrasyncai/adapter-lambda 2.0.0 → 2.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/dist/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
+ // src/handler.ts
2
+ import { evaluateEdgeRequest } from "@astrasyncai/verification-gateway/edge-core";
3
+
1
4
  // src/cf.ts
2
- import {
3
- sanitizeHeaders
4
- } from "@astrasyncai/verification-gateway/metadata-capture";
5
+ import { buildObservedMetadata as buildObservedMetadataCore } from "@astrasyncai/verification-gateway/edge-core";
5
6
  function getRequest(event) {
6
7
  return event.Records[0].cf.request;
7
8
  }
@@ -16,6 +17,14 @@ function flattenHeaders(headers) {
16
17
  }
17
18
  return out;
18
19
  }
20
+ function flattenHeadersSingle(headers) {
21
+ const out = {};
22
+ for (const [name, entries] of Object.entries(headers)) {
23
+ if (!entries?.length) continue;
24
+ out[name] = entries.map((e) => e.value).join(", ");
25
+ }
26
+ return out;
27
+ }
19
28
  function decodeBody(request) {
20
29
  const body = request.body;
21
30
  if (!body?.data) return { truncated: false };
@@ -33,10 +42,17 @@ function collectConnectionMetadata(request) {
33
42
  put("country", "cloudfront-viewer-country");
34
43
  put("countryName", "cloudfront-viewer-country-name");
35
44
  put("region", "cloudfront-viewer-country-region");
45
+ put("countryRegionName", "cloudfront-viewer-country-region-name");
36
46
  put("city", "cloudfront-viewer-city");
37
47
  put("asn", "cloudfront-viewer-asn");
38
48
  put("tlsVersion", "cloudfront-viewer-tls");
39
49
  put("httpVersion", "cloudfront-viewer-http-version");
50
+ put("timeZone", "cloudfront-viewer-time-zone");
51
+ put("metroCode", "cloudfront-viewer-metro-code");
52
+ put("tlsFingerprint", "cloudfront-viewer-ja3-fingerprint");
53
+ put("ja4", "cloudfront-viewer-ja4-fingerprint");
54
+ put("headerOrder", "cloudfront-viewer-header-order", (v) => v.slice(0, 256));
55
+ put("headerCount", "cloudfront-viewer-header-count");
40
56
  const devices = [];
41
57
  for (const [label, header] of [
42
58
  ["desktop", "cloudfront-is-desktop-viewer"],
@@ -49,18 +65,33 @@ function collectConnectionMetadata(request) {
49
65
  if (devices.length) out.device = devices.join(",");
50
66
  return out;
51
67
  }
52
- function buildObservedMetadata(request) {
53
- const { headers, apiKeyFormat, platformHeaders } = sanitizeHeaders(
54
- flattenHeaders(request.headers)
55
- );
68
+ function toNormalizedRequest(request) {
69
+ const body = decodeBody(request);
56
70
  const connection = collectConnectionMetadata(request);
71
+ const clientIp = getHeader(request, "cloudfront-viewer-address")?.split(":")[0];
57
72
  return {
58
- headers,
59
- ...apiKeyFormat && { apiKeyFormat },
60
- ...platformHeaders && { platformHeaders },
73
+ method: request.method,
74
+ uri: request.uri,
75
+ querystring: request.querystring ?? "",
76
+ headers: flattenHeadersSingle(request.headers),
77
+ ...body.text !== void 0 && { bodyText: body.text },
78
+ ...body.truncated && { bodyTruncated: true },
79
+ ...clientIp && { clientIp },
61
80
  ...Object.keys(connection).length > 0 && { connection }
62
81
  };
63
82
  }
83
+ function applyCoreOutcome(request, application) {
84
+ if (application.action === "respond") {
85
+ return jsonResponse(application.status, application.bodyJson, application.headers);
86
+ }
87
+ for (const prefix of application.stripHeaderPrefixes) {
88
+ stripHeadersByPrefix(request, prefix);
89
+ }
90
+ for (const [name, value] of Object.entries(application.injectHeaders)) {
91
+ setHeader(request, name, value);
92
+ }
93
+ return request;
94
+ }
64
95
  function normalizeOriginHost(request) {
65
96
  const originDomain = request.origin?.custom?.domainName ?? request.origin?.s3?.domainName ?? void 0;
66
97
  if (!originDomain) return;
@@ -144,483 +175,6 @@ function _resetConfigCaches() {
144
175
  secretCache.clear();
145
176
  }
146
177
 
147
- // src/classify.ts
148
- import { extractCredentials } from "@astrasyncai/verification-gateway/verify";
149
- import { detectPlatformFingerprint } from "@astrasyncai/verification-gateway/platform-signatures";
150
- import { matchEdgePathRule } from "@astrasyncai/verification-gateway/edge-config";
151
- import { sanitizeHeaders as sanitizeHeaders2 } from "@astrasyncai/verification-gateway/metadata-capture";
152
-
153
- // src/detect.ts
154
- var SD_JWT_PATTERN = /^eyJ[\w-]+\.[\w-]*\.[\w-]+~/;
155
- function sniffJwtHeader(compact) {
156
- try {
157
- const seg = compact.split(".")[0];
158
- return JSON.parse(Buffer.from(seg, "base64url").toString("utf8"));
159
- } catch {
160
- return void 0;
161
- }
162
- }
163
- function parseJsonBody(body) {
164
- if (!body.text) return void 0;
165
- try {
166
- const parsed = JSON.parse(body.text);
167
- return typeof parsed === "object" && parsed !== null ? parsed : void 0;
168
- } catch {
169
- return void 0;
170
- }
171
- }
172
- function detectCommerce(request, host, body) {
173
- const url = requestUrl(request, host);
174
- const method = request.method;
175
- const headers = flattenHeaders(request.headers);
176
- const jsonBody = parseJsonBody(body);
177
- const truncated = body.truncated ? { bodyTruncated: true } : {};
178
- const authHeader = getHeader(request, "authorization");
179
- const bearer = authHeader?.replace(/^Bearer\s+/i, "");
180
- const mandateHeader = getHeader(request, "x-payment-mandate") ?? getHeader(request, "payment-mandate");
181
- const sdJwtCandidate = [mandateHeader, bearer].find((v) => v && SD_JWT_PATTERN.test(v));
182
- if (sdJwtCandidate) {
183
- const typ = sniffJwtHeader(sdJwtCandidate)?.typ;
184
- return {
185
- protocol: "vi-sd-jwt",
186
- artifacts: { viSdJwt: sdJwtCandidate },
187
- evidence: { typ, ...truncated }
188
- };
189
- }
190
- const signature = getHeader(request, "signature");
191
- const signatureInput = getHeader(request, "signature-input");
192
- if (signature && signatureInput) {
193
- const signatureAgent = getHeader(request, "signature-agent");
194
- const registryHint = getHeader(request, "x-agent-registry");
195
- const lowerInput = signatureInput.toLowerCase();
196
- const protocol = signatureAgent ? "rfc9421-web-bot-auth" : registryHint?.toLowerCase().includes("visa") || lowerInput.includes("visa") ? "rfc9421-tap" : registryHint?.toLowerCase().includes("mastercard") || lowerInput.includes("mastercard") ? "rfc9421-agent-pay" : "rfc9421";
197
- return {
198
- protocol,
199
- artifacts: {
200
- rfc9421: { headers, method, url, ...body.text && { body: body.text } }
201
- },
202
- evidence: {
203
- ...signatureAgent && { signatureAgent },
204
- ...registryHint && { registryHint },
205
- keyid: signatureInput.match(/keyid="([^"]+)"/)?.[1],
206
- ...truncated
207
- }
208
- };
209
- }
210
- const acpTimestamp = getHeader(request, "timestamp");
211
- if (signature && !signatureInput && (acpTimestamp || getHeader(request, "idempotency-key"))) {
212
- return {
213
- protocol: "acp",
214
- artifacts: {
215
- acpRequest: {
216
- method,
217
- url,
218
- headers,
219
- ...body.text && { rawBody: body.text },
220
- ...jsonBody && { body: jsonBody }
221
- }
222
- },
223
- evidence: { hasTimestamp: Boolean(acpTimestamp), ...truncated }
224
- };
225
- }
226
- if (/\/checkout[-_]sessions(\/|$)/.test(request.uri)) {
227
- return {
228
- protocol: "ucp",
229
- artifacts: { ucpRequest: { method, url, ...jsonBody && { body: jsonBody } } },
230
- evidence: { path: request.uri, ...truncated }
231
- };
232
- }
233
- if (jsonBody) {
234
- const intent = jsonBody.intent_mandate ?? jsonBody.intentMandate;
235
- const cart = jsonBody.cart_mandate ?? jsonBody.cartMandate;
236
- const payment = jsonBody.payment_mandate ?? jsonBody.paymentMandate;
237
- if (typeof intent === "string" || typeof cart === "string" || typeof payment === "string") {
238
- return {
239
- protocol: "ap2",
240
- artifacts: {
241
- ap2Mandates: {
242
- ...typeof intent === "string" && { intent },
243
- ...typeof cart === "string" && { cart },
244
- ...typeof payment === "string" && { payment }
245
- }
246
- },
247
- evidence: {
248
- mandatesPresent: [
249
- typeof intent === "string" && "intent",
250
- typeof cart === "string" && "cart",
251
- typeof payment === "string" && "payment"
252
- ].filter(Boolean),
253
- ...truncated
254
- }
255
- };
256
- }
257
- }
258
- if (authHeader && /^Payment\s+/i.test(authHeader)) {
259
- return {
260
- protocol: "mpp",
261
- artifacts: {
262
- mppRequest: {
263
- method,
264
- url,
265
- headers,
266
- ...body.text && { rawBody: body.text },
267
- ...jsonBody && { body: jsonBody }
268
- }
269
- },
270
- evidence: truncated
271
- };
272
- }
273
- if (getHeader(request, "x-payment")) {
274
- return {
275
- protocol: "x402",
276
- artifacts: {
277
- x402Request: { method, url, headers, ...jsonBody && { body: jsonBody } }
278
- },
279
- evidence: truncated
280
- };
281
- }
282
- return void 0;
283
- }
284
-
285
- // src/classify.ts
286
- var STATIC_EXTENSIONS = /\.(css|js|mjs|map|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|mp4|webm|pdf|txt|xml)$/i;
287
- var BOT_UA_PATTERN = /curl|wget|python-requests|python-urllib|node-fetch|axios|got|okhttp|go-http-client|libwww|httpie|java\/|ruby|php|scrapy|headless|phantomjs|selenium|playwright|puppeteer|bot|crawler|spider|slurp/i;
288
- var BROWSER_UA_PATTERN = /mozilla\/\d/i;
289
- function resolvePolicy(config, path) {
290
- if (STATIC_EXTENSIONS.test(path)) {
291
- return { mode: config.mode, depth: config.depth, skip: true };
292
- }
293
- const rule = matchEdgePathRule(config.pathRules, path);
294
- if (!rule) return { mode: config.mode, depth: config.depth, skip: false };
295
- return {
296
- mode: rule.mode ?? config.mode,
297
- depth: rule.depth ?? config.depth,
298
- skip: rule.skip === true
299
- };
300
- }
301
- function classify(request, host, depth) {
302
- const userAgent = getHeader(request, "user-agent") ?? "";
303
- const flat = flattenHeaders(request.headers);
304
- const credentials = extractCredentials(flat, {});
305
- if (credentials.astraId || credentials.apiKey || credentials.jwt) {
306
- return { tier: "verified", protocol: "x-astra" };
307
- }
308
- const { apiKeyFormat, platformHeaders } = sanitizeHeaders2(flat);
309
- const detectionInput = { userAgent, apiKeyFormat, platformHeaders };
310
- if (depth !== "classify") {
311
- const commerce = detectCommerce(request, host, decodeBody(request));
312
- if (commerce) {
313
- return {
314
- tier: "identified",
315
- protocol: commerce.protocol,
316
- artifacts: commerce.artifacts,
317
- evidence: commerce.evidence,
318
- fingerprint: detectPlatformFingerprint(detectionInput)
319
- };
320
- }
321
- }
322
- const fingerprint = detectPlatformFingerprint(detectionInput);
323
- if (fingerprint) {
324
- return { tier: "identified", protocol: "platform-ua", fingerprint };
325
- }
326
- if (!userAgent || BOT_UA_PATTERN.test(userAgent) || !BROWSER_UA_PATTERN.test(userAgent)) {
327
- return { tier: "anonymous-bot", evidence: { userAgent: userAgent || "(none)" } };
328
- }
329
- if (!getHeader(request, "sec-fetch-mode") && !getHeader(request, "accept-language")) {
330
- return { tier: "anonymous-bot", evidence: { userAgent, spoofedBrowser: true } };
331
- }
332
- return { tier: "human" };
333
- }
334
-
335
- // src/pipeline.ts
336
- import { verify, extractCredentials as extractCredentials2 } from "@astrasyncai/verification-gateway/verify";
337
- import { resolveHttpPdlss } from "@astrasyncai/verification-gateway/http-pdlss";
338
-
339
- // src/events.ts
340
- import { reportUnregisteredAttempt } from "@astrasyncai/verification-gateway/verify";
341
- async function withBudget(promise, ms) {
342
- let timer;
343
- try {
344
- return await Promise.race([
345
- promise,
346
- new Promise((resolve) => {
347
- timer = setTimeout(() => resolve(void 0), ms);
348
- })
349
- ]);
350
- } catch {
351
- return void 0;
352
- } finally {
353
- if (timer) clearTimeout(timer);
354
- }
355
- }
356
- async function emitBeacon(options, request, classification, budgetMs) {
357
- const clientIp = getHeader(request, "cloudfront-viewer-address")?.split(":")[0];
358
- await withBudget(
359
- reportUnregisteredAttempt(
360
- { apiBaseUrl: options.apiBaseUrl, counterpartyId: options.counterpartyId },
361
- {
362
- counterpartyUrl: options.counterpartyUrl,
363
- counterpartyType: "website",
364
- sourceIp: clientIp,
365
- userAgent: getHeader(request, "user-agent"),
366
- requestPath: request.uri,
367
- requestMethod: request.method,
368
- // 3.9.0 evidence fields (backend persists them into eventData).
369
- protocol: classification.protocol,
370
- evidence: classification.evidence,
371
- // Workstream D — full sanitised header + connection capture for the
372
- // unregistered caller (fire-and-forget, so free).
373
- observedMetadata: buildObservedMetadata(request)
374
- }
375
- ),
376
- budgetMs
377
- );
378
- }
379
- function sampled(rate, random = Math.random) {
380
- if (rate <= 0) return false;
381
- if (rate >= 1) return true;
382
- return random() < rate;
383
- }
384
-
385
- // src/pipeline.ts
386
- function baseOutcome(classification) {
387
- return {
388
- tier: classification.tier,
389
- decision: "allow",
390
- reasons: [],
391
- protocol: classification.protocol
392
- };
393
- }
394
- function buildVerificationRequest(options, request, classification, includeArtifacts) {
395
- const headers = flattenHeaders(request.headers);
396
- const clientIp = getHeader(request, "cloudfront-viewer-address")?.split(":")[0];
397
- const pdlss = resolveHttpPdlss({
398
- method: request.method,
399
- astraPurpose: getHeader(request, "x-astra-purpose"),
400
- astraAction: getHeader(request, "x-astra-action")
401
- });
402
- const attemptId = getHeader(request, "x-astra-attempt-id");
403
- return {
404
- ...attemptId && /^att_[0-9a-f]{32}$/.test(attemptId) && { attemptId },
405
- credentials: extractCredentials2(headers, {}),
406
- purpose: pdlss.purpose,
407
- action: pdlss.action,
408
- resource: request.uri,
409
- createSession: true,
410
- counterpartyType: "website",
411
- counterpartyUrl: options.counterpartyUrl,
412
- callerMetadata: {
413
- sourceIp: clientIp,
414
- userAgent: getHeader(request, "user-agent"),
415
- referer: getHeader(request, "referer"),
416
- host: getHeader(request, "host"),
417
- // Workstream D — the full sanitised header + connection capture. The
418
- // backend stores it fire-and-forget (zero decision latency) and uses the
419
- // key-format / platform headers for attribution.
420
- observedMetadata: buildObservedMetadata(request)
421
- },
422
- ...includeArtifacts && classification.artifacts && { commerceArtifacts: classification.artifacts }
423
- };
424
- }
425
- function outcomeFromVerifyResult(classification, result) {
426
- const outcome = baseOutcome(classification);
427
- outcome.sessionId = result.sessionId;
428
- outcome.correlationId = result.correlationId;
429
- outcome.guidance = result.guidance;
430
- if (result.misconfigured) {
431
- outcome.reasons = ["verification key misconfigured \u2014 fail-open (merchant_misconfig)"];
432
- outcome.misconfig = true;
433
- return outcome;
434
- }
435
- if (result.requiresStepUp || result.stepUpApproval) {
436
- outcome.decision = "manual_review";
437
- outcome.reasons = ["step-up approval required"];
438
- outcome.stepUp = {
439
- pollUrl: result.stepUpApproval?.pollUrl,
440
- expiresAt: result.stepUpApproval?.expiresAt
441
- };
442
- return outcome;
443
- }
444
- if (!result.identityVerified || !result.policyAllowed) {
445
- outcome.decision = "deny";
446
- outcome.reasons = result.denialReasons?.length ? result.denialReasons : [!result.identityVerified ? "identity not verified" : "denied by access policy"];
447
- return outcome;
448
- }
449
- outcome.asat = {
450
- trustScore: result.agent?.trustScore,
451
- agentId: result.agent?.astraId,
452
- developerId: result.developer?.astradId,
453
- accessLevel: result.accessLevel
454
- };
455
- return outcome;
456
- }
457
- async function runTier(options, request, classification, policy, apiKey, sampling) {
458
- const budgets = options.budgets ?? {};
459
- const beaconMs = budgets.beaconMs ?? 300;
460
- const verifyMs = policy.mode === "enforce" ? budgets.verifyEnforceMs ?? 3e3 : budgets.verifyObserveMs ?? 1500;
461
- const gatewayConfig = {
462
- apiBaseUrl: options.apiBaseUrl,
463
- ...apiKey && { apiKey },
464
- counterpartyId: options.counterpartyId,
465
- // The shell handles its own error semantics; init self-probes don't
466
- // belong in a per-request edge function.
467
- disableInitChecks: true
468
- };
469
- try {
470
- switch (classification.tier) {
471
- case "verified": {
472
- if (policy.depth === "classify") {
473
- return baseOutcome(classification);
474
- }
475
- const result = await withBudget(
476
- verify(
477
- gatewayConfig,
478
- buildVerificationRequest(options, request, classification, policy.depth === "authorize")
479
- ),
480
- verifyMs
481
- );
482
- if (!result) {
483
- const outcome = baseOutcome(classification);
484
- outcome.reasons = ["verify-access unreachable within budget \u2014 fail-open"];
485
- return outcome;
486
- }
487
- return outcomeFromVerifyResult(classification, result);
488
- }
489
- case "identified": {
490
- if (policy.depth === "authorize" && classification.artifacts) {
491
- const result = await withBudget(
492
- verify(gatewayConfig, buildVerificationRequest(options, request, classification, true)),
493
- verifyMs
494
- );
495
- if (!result) {
496
- const outcome2 = baseOutcome(classification);
497
- outcome2.reasons = ["verify-access unreachable within budget \u2014 fail-open"];
498
- return outcome2;
499
- }
500
- return outcomeFromVerifyResult(classification, result);
501
- }
502
- await emitBeacon(options, request, classification, beaconMs);
503
- const outcome = baseOutcome(classification);
504
- if (policy.mode === "enforce" && policy.depth !== "classify") {
505
- outcome.decision = "deny";
506
- outcome.reasons = ["agent is not registered with AstraSync"];
507
- }
508
- return outcome;
509
- }
510
- case "anonymous-bot": {
511
- if (sampled(sampling.anonymousBeaconRate, options.random)) {
512
- await emitBeacon(
513
- options,
514
- request,
515
- { ...classification, protocol: classification.protocol ?? "ua-heuristic" },
516
- beaconMs
517
- );
518
- }
519
- return baseOutcome(classification);
520
- }
521
- default:
522
- return baseOutcome(classification);
523
- }
524
- } catch (err) {
525
- const outcome = baseOutcome(classification);
526
- outcome.reasons = [
527
- `edge pipeline error \u2014 fail-open: ${err instanceof Error ? err.message : String(err)}`
528
- ];
529
- return outcome;
530
- }
531
- }
532
-
533
- // src/messaging.ts
534
- var PROTOCOL_MESSAGES = {
535
- "vi-sd-jwt": "This request presented Visa Intelligent Commerce credentials, but the agent is not verified with AstraSync. Register the agent to pair your VI credentials with a verified identity and mandate.",
536
- "rfc9421-agent-pay": "This request carried a Mastercard Agent Pay signature, but the agent is not verified with AstraSync. Register the agent to pair Agent Pay credentials with a verified identity and mandate.",
537
- "rfc9421-tap": "This request carried a Visa TAP signature, but the agent is not verified with AstraSync. Register the agent to pair TAP credentials with a verified identity and mandate.",
538
- "rfc9421-web-bot-auth": "This request carried a Web Bot Auth signature, but the agent is not verified with AstraSync. Register the agent to link your signed-agent identity to a verified principal.",
539
- rfc9421: "This request carried an RFC 9421 HTTP message signature, but the agent is not verified with AstraSync. Register the agent to link the signing identity to a verified principal.",
540
- acp: "This request used the Agentic Commerce Protocol, but the agent is not verified with AstraSync. Register the agent to transact on this property.",
541
- ucp: "This request used a UCP checkout session, but the agent is not verified with AstraSync. Register the agent to transact on this property.",
542
- ap2: "This request presented AP2 mandates, but the agent is not verified with AstraSync. Register the agent to pair your mandates with a verified identity.",
543
- mpp: "This request presented MPP payment credentials, but the agent is not verified with AstraSync. Register the agent to transact on this property.",
544
- x402: "This request used x402 payment headers, but the agent is not verified with AstraSync. Register the agent to transact on this property.",
545
- "platform-ua": "This traffic was identified as a platform AI agent without AstraSync credentials. Register the agent to access this property with a verified identity.",
546
- "x-astra": "The presented AstraSync credentials failed verification or were denied by this property\u2019s access policy."
547
- };
548
- function buildDenyBody(outcome, apiBaseUrl) {
549
- const origin = apiBaseUrl.replace(/\/api\/?$/, "");
550
- const protocolMessage = (outcome.protocol && PROTOCOL_MESSAGES[outcome.protocol]) ?? "This request could not be verified by AstraSync.";
551
- return {
552
- success: false,
553
- error: {
554
- // UNAUTHORIZED = identity failed (register/re-authenticate helps);
555
- // INSUFFICIENT_ACCESS = identity fine, policy denied (fix the mandate).
556
- code: outcome.tier === "verified" ? "INSUFFICIENT_ACCESS" : "UNAUTHORIZED",
557
- message: protocolMessage,
558
- ...outcome.protocol && { protocol: outcome.protocol },
559
- failures: outcome.reasons,
560
- ...outcome.correlationId && { correlationId: outcome.correlationId },
561
- guidance: {
562
- // `||` not `??`: verify() fills these with EMPTY STRINGS when the
563
- // well-known cache is cold — empty means "use the fallback".
564
- message: outcome.guidance?.message || "Register this agent with AstraSync to receive verifiable credentials.",
565
- registrationUrl: outcome.guidance?.registrationUrl || `${origin}/register`,
566
- documentationUrl: outcome.guidance?.documentationUrl || `${origin}/docs/edge-adapter`
567
- }
568
- }
569
- };
570
- }
571
- function buildManualReviewBody(outcome) {
572
- return {
573
- success: false,
574
- status: "manual_review",
575
- message: "This action requires human approval. Poll the step-up approval endpoint and retry once approved.",
576
- ...outcome.stepUp && { stepUpApproval: outcome.stepUp },
577
- ...outcome.correlationId && { correlationId: outcome.correlationId }
578
- };
579
- }
580
-
581
- // src/enforce.ts
582
- var ASAT_HEADERS = {
583
- trustScore: "X-AstraSync-Trust-Score",
584
- agentId: "X-AstraSync-Agent-Id",
585
- developerId: "X-AstraSync-Developer-Id",
586
- permissions: "X-AstraSync-Permissions",
587
- observedDecision: "X-AstraSync-Observed-Decision"
588
- };
589
- function applyOutcome(options, request, outcome, policy) {
590
- stripHeadersByPrefix(request, "x-astrasync-");
591
- if (outcome.misconfig) {
592
- setHeader(request, ASAT_HEADERS.observedDecision, "misconfig");
593
- }
594
- if (policy.mode !== "enforce") {
595
- if (outcome.decision !== "allow") {
596
- setHeader(request, ASAT_HEADERS.observedDecision, outcome.decision);
597
- }
598
- injectAsat(request, outcome);
599
- return request;
600
- }
601
- switch (outcome.decision) {
602
- case "deny":
603
- return jsonResponse(403, buildDenyBody(outcome, options.apiBaseUrl));
604
- case "manual_review":
605
- return jsonResponse(202, buildManualReviewBody(outcome), {
606
- "Retry-After": "30"
607
- });
608
- default:
609
- injectAsat(request, outcome);
610
- return request;
611
- }
612
- }
613
- function injectAsat(request, outcome) {
614
- if (!outcome.asat) return;
615
- const { trustScore, agentId, developerId, accessLevel } = outcome.asat;
616
- if (trustScore !== void 0) {
617
- setHeader(request, ASAT_HEADERS.trustScore, String(trustScore));
618
- }
619
- if (agentId) setHeader(request, ASAT_HEADERS.agentId, agentId);
620
- if (developerId) setHeader(request, ASAT_HEADERS.developerId, developerId);
621
- if (accessLevel) setHeader(request, ASAT_HEADERS.permissions, accessLevel);
622
- }
623
-
624
178
  // src/handler.ts
625
179
  function createEdgeHandler(options) {
626
180
  const log = options.log ?? ((level, message, data) => {
@@ -632,27 +186,13 @@ function createEdgeHandler(options) {
632
186
  normalizeOriginHost(request);
633
187
  const apiKey = await resolveApiKey({ ...options, log });
634
188
  const config = await loadEdgeConfig(options, apiKey);
635
- const policy = resolvePolicy(config, request.uri);
636
- if (policy.skip) return request;
637
- const host = getHeader(request, "x-forwarded-host") ?? getHeader(request, "host") ?? new URL(options.counterpartyUrl).host;
638
- const classification = classify(request, host, policy.depth);
639
- if (classification.tier === "human") return request;
640
- const outcome = await runTier(
189
+ const { application } = await evaluateEdgeRequest(
641
190
  { ...options, log },
642
- request,
643
- classification,
644
- policy,
645
- apiKey,
646
- config.sampling
191
+ toNormalizedRequest(request),
192
+ config,
193
+ apiKey
647
194
  );
648
- if (outcome.decision !== "allow") {
649
- log(
650
- "info",
651
- policy.mode === "enforce" ? `${outcome.decision.toUpperCase()}: ${request.method} ${request.uri}` : `[OBSERVE] would have been ${outcome.decision.toUpperCase()}: ${request.method} ${request.uri}`,
652
- { tier: outcome.tier, protocol: outcome.protocol, reasons: outcome.reasons }
653
- );
654
- }
655
- return applyOutcome(options, request, outcome, policy);
195
+ return applyCoreOutcome(request, application);
656
196
  } catch (err) {
657
197
  log("error", "edge handler error \u2014 passing request through", {
658
198
  error: err instanceof Error ? err.message : String(err)
@@ -661,6 +201,70 @@ function createEdgeHandler(options) {
661
201
  }
662
202
  };
663
203
  }
204
+
205
+ // src/classify.ts
206
+ import {
207
+ classify as classifyCore,
208
+ resolvePolicy,
209
+ _patterns
210
+ } from "@astrasyncai/verification-gateway/edge-core";
211
+ function classify(request, host, depth) {
212
+ return classifyCore(toNormalizedRequest(request), host, depth);
213
+ }
214
+
215
+ // src/detect.ts
216
+ import {
217
+ detectCommerce as detectCommerceCore
218
+ } from "@astrasyncai/verification-gateway/edge-core";
219
+ function detectCommerce(request, host, body) {
220
+ const normalized = toNormalizedRequest(request);
221
+ return detectCommerceCore(
222
+ {
223
+ ...normalized,
224
+ bodyText: body.text,
225
+ bodyTruncated: body.truncated
226
+ },
227
+ host
228
+ );
229
+ }
230
+
231
+ // src/pipeline.ts
232
+ import { runTier as runTierCore } from "@astrasyncai/verification-gateway/edge-core";
233
+ async function runTier(options, request, classification, policy, apiKey, sampling) {
234
+ return runTierCore(
235
+ options,
236
+ toNormalizedRequest(request),
237
+ classification,
238
+ policy,
239
+ apiKey,
240
+ sampling
241
+ );
242
+ }
243
+
244
+ // src/enforce.ts
245
+ import {
246
+ applyOutcome as resolveOutcomeApplication,
247
+ ASAT_HEADERS
248
+ } from "@astrasyncai/verification-gateway/edge-core";
249
+ function applyOutcome(options, request, outcome, policy) {
250
+ return applyCoreOutcome(request, resolveOutcomeApplication(options, outcome, policy));
251
+ }
252
+
253
+ // src/messaging.ts
254
+ import {
255
+ buildDenyBody,
256
+ buildManualReviewBody
257
+ } from "@astrasyncai/verification-gateway/edge-core";
258
+
259
+ // src/events.ts
260
+ import {
261
+ emitBeacon as emitBeaconCore,
262
+ withBudget,
263
+ sampled
264
+ } from "@astrasyncai/verification-gateway/edge-core";
265
+ async function emitBeacon(options, request, classification, budgetMs) {
266
+ await emitBeaconCore(options, toNormalizedRequest(request), classification, budgetMs);
267
+ }
664
268
  export {
665
269
  ASAT_HEADERS,
666
270
  _resetConfigCaches,