@getvitops/astro 8.3.0-alpha.1 → 8.3.0-alpha.2

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.
@@ -230,7 +230,11 @@ function createConversionRoute(options) {
230
230
  if (isFormPost) {
231
231
  const formData = await new Response(bytes, { headers: { "content-type": contentType } }).formData();
232
232
  body = {};
233
- for (const [key, value] of formData.entries()) if (typeof value === "string") body[key] = value;
233
+ for (const [key, value] of formData.entries()) {
234
+ if (typeof value !== "string") continue;
235
+ const prior = body[key];
236
+ body[key] = typeof prior === "string" && prior ? `${prior}, ${value}` : value;
237
+ }
234
238
  } else {
235
239
  const parsed = JSON.parse(new TextDecoder().decode(bytes));
236
240
  if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return respondError("Malformed request.", 400);
@@ -1 +1 @@
1
- {"version":3,"file":"conversion.mjs","names":[],"sources":["../../src/routes/conversion.ts"],"sourcesContent":["/**\n * The conversion endpoint, as a factory.\n *\n * A site's contact form and its `tel:`/`mailto:` beacons all mean the same\n * thing — someone got in touch — and all want the same treatment: read the\n * attribution cookie off the request, build the event, notify whoever the\n * config says. That is the same three lines in every project, so it ships\n * here.\n *\n * What deliberately does **not** ship here is field-level validation and business\n * rules — a consumer's form has its own required fields and its own response\n * shape, and baking a guess at those in would make the factory something to\n * fight rather than mount. The handler takes the parsed body and an optional\n * `validate`; that part is the caller's. What **does** ship here is generic abuse\n * resistance (honeypot, origin, rate limit, body size): this is a public,\n * unauthenticated endpoint that sends email, so leaving it wide open makes it a\n * relay and a cost amplifier regardless of whose form is behind it.\n *\n * The path matters: the capture script beacons `tel:`/`mailto:` conversions at\n * `TRACKING_ENDPOINT` (`/api/track`, exported from `@getvitops/astro/tracking`),\n * and the integration warns at build if no route answers it.\n *\n * ```ts\n * // src/pages/api/track.ts\n * export const prerender = false;\n * import { env } from 'cloudflare:workers';\n * import { createConversionRoute } from '@getvitops/astro/routes';\n *\n * export const POST = createConversionRoute({\n * context: {\n * // A bare address is shorthand for `{ provider: 'cloudflare', to }`.\n * notifications: { email: 'sales@acme.ca' },\n * canonical: 'https://acme.ca',\n * organizationName: 'Acme',\n * },\n * binding: () => env.EMAIL,\n * // A no-JS <form method=\"post\"> submission never sees a JSON response body —\n * // it's a full navigation, so send it somewhere real.\n * redirectTo: '/thanks',\n * });\n * ```\n *\n * Cloudflare Turnstile is a separate, opt-in layer on top of the checks above —\n * pass `turnstile: { secretKey }` and a submission carrying a\n * `cf-turnstile-response` token gets it verified server-side via\n * `verifyTurnstileToken` (`@getvitops/utils/security`). The widget itself is the\n * consumer's own markup (`<script src=\"https://challenges.cloudflare.com/\n * turnstile/v0/api.js\" async defer></script>` + `<div class=\"cf-turnstile\"\n * data-sitekey=\"…\">` on their form) — there is no `FormRenderer` for this to hook\n * into generically, matching how the rest of the form is already hand-written.\n * `site.security.turnstile.siteKey` is what the generated privacy policy reads to\n * name Turnstile as a processor; wiring this option up is what makes that true\n * rather than an assertion nothing checks.\n *\n * Two things this example used to get wrong, both of which cost a consumer real\n * time. It read the binding off `locals.runtime.env`, which Astro removed in v6\n * — and because the resulting throw precedes the response, Astro re-reads the\n * request and reports a misleading \"Body has already been used\" instead of the\n * actual error. This package peers on `astro >= 7`, so that form could not work\n * for any supported consumer; `import { env } from 'cloudflare:workers'` is the\n * current one. It also called a `toNotifyContext(config)` helper that used to\n * not exist — `context` can now be built with `toNotifyContext(config)` (also\n * exported from this module), rather than every consumer hand-writing a\n * `NotifyContext` literal:\n *\n * ```ts\n * import config from '../../site.json' with { type: 'json' };\n * import { createConversionRoute, toNotifyContext, toCrmConfig } from '@getvitops/astro/routes';\n *\n * export const POST = createConversionRoute({\n * context: toNotifyContext(config),\n * crm: toCrmConfig(config) && { config: toCrmConfig(config)!, credentials: () => ({ ... }) },\n * binding: () => env.EMAIL,\n * redirectTo: '/thanks',\n * });\n * ```\n */\nimport { AD_PLATFORMS, normalizeConversionActionIds } from '@getvitops/utils/ads';\nimport type { AdPropertySetup } from '@getvitops/utils/ads';\nimport { deliverToCrm, describeCrmResult } from '@getvitops/utils/crm';\nimport type { CrmConfig, CrmCredentials, CrmResult } from '@getvitops/utils/crm';\nimport {\n describeGoogleAdsOfflineResult,\n type EmailBinding,\n notify,\n type NotifyContext,\n uploadClickConversion,\n type GoogleAdsCredentials,\n type GoogleAdsUploadResult,\n} from '@getvitops/utils/notify';\nimport { verifyTurnstileToken } from '@getvitops/utils/security';\nimport { parseTrackingCookie, readCookie } from '@getvitops/utils/tracking';\nimport type { ConversionEvent, TrackingData } from '@getvitops/utils/tracking';\nexport {\n resolveOrganizationName,\n toCrmConfig,\n toGoogleAdsConfig,\n toNotifyContext,\n} from './config-adapters.ts';\n\n/** The subset of Astro's `APIContext` this needs — typed structurally so the\n * factory does not pin an Astro version. */\nexport interface ConversionRequestContext {\n request: Request;\n locals?: unknown;\n}\n\n/**\n * Records a hit for `id` at time `at` (ms) and returns the ms since the previous\n * hit for that `id`, or `null` if this is the first. Pure interface — a store\n * decides nothing, it only remembers.\n */\nexport interface RateLimitStore {\n hit(id: string, at: number): Promise<number | null> | number | null;\n}\n\n/**\n * Per-isolate, in-memory default. Good enough for a single Worker instance\n * fielding occasional contact-form spam; not durable across a fleet, and not\n * shared with other routes. Inject a KV- or Durable-Object-backed `store` for\n * real cross-instance limiting — the interface is one method wide on purpose.\n */\nclass MemoryRateLimitStore implements RateLimitStore {\n private lastHit = new Map<string, number>();\n hit(id: string, at: number): number | null {\n const prev = this.lastHit.get(id);\n this.lastHit.set(id, at);\n return prev === undefined ? null : at - prev;\n }\n}\n\nexport interface ConversionRouteOptions {\n /** Where notifications go. Build it from your site config. */\n context: NotifyContext;\n /**\n * The Workers `send_email` binding for this request, e.g. `() => env.EMAIL`\n * with `env` imported from `cloudflare:workers`. Omit it in dev and the\n * notification is printed instead of sent.\n *\n * Not `(locals) => locals.runtime.env.EMAIL` — Astro removed `locals.runtime`\n * in v6, and this package peers on `>= 7`.\n */\n binding?: (locals: unknown) => EmailBinding | undefined;\n /**\n * Push the submission into a CRM (HubSpot, Zoho Bigin or Jobber) before notifying.\n * Credentials arrive as a function of `locals`, exactly like `binding` above\n * — the consumer reads them from `import { env } from 'cloudflare:workers'`.\n * Credentials are never config; that rule is absolute in this toolchain.\n *\n * A failed CRM write never fails the request and never blocks the honeypot/\n * Turnstile gates above it — those run first, so a bot never reaches the\n * CRM. The outcome is folded into the notification email's \"Delivery\"\n * section (via `describeCrmResult`) so a failed write is never silent.\n */\n crm?: {\n config: CrmConfig;\n credentials?: (locals: unknown) => CrmCredentials | undefined;\n fetch?: typeof fetch;\n timeoutMs?: number;\n };\n /**\n * Upload the conversion to Google Ads server-side instead of relying on the\n * visitor's browser to fire `gtag.js` — see `site.ads.google.reporting` and\n * `conversionActionId`. Credentials arrive as a function of `locals`, same\n * shape as `crm.credentials` and `binding` above.\n *\n * Only fires for an action whose `conversionActionId` is actually set (via\n * `toGoogleAdsConfig(config)`, or hand-built the same shape) **and** whose\n * event carries a Google click id (`gclid`/`gbraid`/`wbraid` on `_ac`) —\n * an organic conversion has nothing for the Ads API to attribute, so it's\n * silently skipped rather than reported as a failure. A failed upload never\n * fails the request, matching `crm` above; the outcome folds into the same\n * \"Delivery\" section via `describeGoogleAdsOfflineResult`.\n */\n googleAdsOffline?: {\n config: Pick<AdPropertySetup, 'accountId' | 'conversionActionId' | 'reporting'>;\n credentials?: (locals: unknown) => GoogleAdsCredentials | undefined;\n fetch?: typeof fetch;\n };\n /**\n * Reject a submission before anything is sent. Return a message to refuse.\n * This is where a consumer puts required-field checks.\n */\n validate?: (body: Record<string, unknown>) => string | null;\n /** Called with every channel outcome, delivered or not, plus the CRM\n * outcome when `crm` is configured. Defaults to console. */\n onResult?: (result: ConversionResult) => void;\n /** Injected in tests. */\n now?: () => number;\n /**\n * Where to send a no-JS `<form method=\"post\">` submission on success. Without\n * this, that visitor's browser navigates to the raw `{\"success\":true}` JSON\n * body as its next page — functional, but not a page anyone should see. Has\n * no effect on the beacon/fetch (JSON) path, which always gets a JSON response.\n */\n redirectTo?: string;\n /**\n * Where to send a refused no-JS submission (validation, spam checks, malformed\n * body). Falls back to `redirectTo` if omitted, and to the JSON error response\n * if neither is set.\n */\n errorRedirectTo?: string;\n /**\n * Field names treated as honeypots: a bot fills every input it finds, a person\n * never sees these (hidden by CSS in the form markup). A non-empty value is\n * silently treated as spam — the response looks identical to a real success,\n * denying the bot the signal it needs to know the trick worked, but nothing is\n * notified and the field never reaches `notify()`. Defaults to the same names\n * `render.ts`'s `OMIT_FIELDS` already knows about (`website`, `_honeypot`,\n * `honeypot`), so the two lists don't need to be kept in sync by hand.\n */\n honeypotFields?: string[];\n /**\n * Reject a submission whose `Content-Length` exceeds this many bytes. Default\n * 25_000 — generous for a contact form with no file upload, tiny for anything\n * meant to relay large payloads. The header is only a fast path: a chunked\n * request carries none, so the body is also read through a byte-capped\n * reader and refused the moment it passes the cap.\n */\n maxBodyBytes?: number;\n /**\n * Fall back to the first `X-Forwarded-For` hop as the client IP when\n * `CF-Connecting-IP` is absent. Default `false`: off Cloudflare the header is\n * whatever the client sent, so honouring it lets a script mint a fresh\n * rate-limit bucket per request. Turn on only behind a proxy that overwrites\n * it. With neither header trusted there is no IP, and rate limiting is skipped.\n */\n trustForwardedFor?: boolean;\n /**\n * Reject a submission whose `Origin` header doesn't match this origin.\n * Defaults to `context.canonical`'s origin. Pass `false` to disable.\n *\n * Best-effort, not a CSRF guard: browsers omit `Origin` on plenty of\n * legitimate same-origin requests, so its *absence* is never treated as a\n * mismatch — only a present-and-different value is rejected. That still stops\n * the common case (a script hitting the endpoint directly from elsewhere)\n * without breaking real submissions from older or privacy-hardened browsers.\n */\n allowedOrigin?: string | false;\n /**\n * Minimum interval between accepted submissions from the same IP. Pass\n * `false` to disable. `windowMs` defaults to 10 seconds — enough to blunt a\n * script firing in a loop without troubling someone who fat-fingers a form\n * and resubmits.\n */\n rateLimit?: { windowMs?: number; store?: RateLimitStore } | false;\n /**\n * Verify a Cloudflare Turnstile token server-side. The **secret** key —\n * unlike `site.security.turnstile.siteKey`, which is public config — must come\n * from an env var, never a config file (the same rule as every other\n * credential in this toolchain).\n */\n turnstile?: {\n secretKey: string;\n /** The submitted field carrying the token. Default `cf-turnstile-response`\n * — the name Cloudflare's widget submits under. */\n fieldName?: string;\n /**\n * Reject a submission with no token at all, rather than only a present-and-\n * invalid one. Default `false`: an absent token is the ordinary shape of a\n * visitor with JS disabled, and this factory is built around that path\n * staying usable. Set `true` only for a form where verified-human matters\n * enough to accept that trade-off — doing so means that form no longer\n * works without JS, on purpose.\n */\n required?: boolean;\n };\n}\n\n/** `notify()`'s own result, plus the CRM and Google Ads offline-upload\n * outcomes when those are configured. Additive over\n * `Awaited<ReturnType<typeof notify>>` — a consumer's existing `onResult`\n * reading `.results`/`.delivered` keeps compiling unchanged. */\nexport interface ConversionResult extends Awaited<ReturnType<typeof notify>> {\n crm?: CrmResult | undefined;\n googleAds?: GoogleAdsUploadResult | undefined;\n}\n\n/** The click id to report to Google Ads, checked in the same order as\n * `AD_PLATFORMS.google.clickIdParams` rather than `getPrimaryClickId()` —\n * that helper returns whichever platform's click id happens to appear first\n * in the *cross-platform* precedence table, which may not be Google's even\n * when a `gclid` is present. */\nfunction googleClickId(tracking: TrackingData | null): { param: string; value: string } | null {\n if (!tracking) return null;\n for (const param of AD_PLATFORMS.google.clickIdParams) {\n const value = tracking[param];\n if (value) return { param, value: String(value) };\n }\n return null;\n}\n\n/** Fields that are never part of a submission's meaning. `cf-turnstile-response`\n * is excluded unconditionally, matching `render.ts`'s `OMIT_FIELDS` — the field\n * carries a verification token, not something the visitor wrote. */\nconst CONTROL_FIELDS = new Set(['event', 'phone', 'cf-turnstile-response']);\n/** The capture script sends `link.href` as-is: `tel:` + digits and the punctuation people type\n * (a space arrives as `%20`). Not free text. */\nconst PHONE_RE = /^(?:tel:)?\\+?[\\d\\s().%-]{3,40}$/i;\n/** `mailto:` + an address, with an optional `?subject=…` query. A shape check, not deliverability. */\nconst EMAIL_RE = /^(?:mailto:)?[^\\s@<>\"?]{1,64}@[^\\s@<>\"?]{1,255}(?:\\?[^\\s<>\"]{0,256})?$/i;\n\nconst DEFAULT_HONEYPOT_FIELDS = ['website', '_honeypot', 'honeypot'];\nconst DEFAULT_MAX_BODY_BYTES = 25_000;\n\n/**\n * Read a request body, giving up as soon as it exceeds `max` bytes. Returns `null` when over\n * the cap. `Content-Length` is optional on chunked requests, so it cannot be the only guard.\n */\nasync function readCapped(request: Request, max: number): Promise<Uint8Array<ArrayBuffer> | null> {\n if (!request.body) return new Uint8Array();\n const reader = request.body.getReader();\n const chunks: Uint8Array[] = [];\n let total = 0;\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n total += value.byteLength;\n if (total > max) {\n await reader.cancel();\n return null;\n }\n chunks.push(value);\n }\n const bytes = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n bytes.set(chunk, offset);\n offset += chunk.byteLength;\n }\n return bytes;\n}\nconst DEFAULT_RATE_LIMIT_WINDOW_MS = 10_000;\n\nfunction toFormData(\n body: Record<string, unknown>,\n honeypotFields: string[],\n): Record<string, string> {\n const out: Record<string, string> = {};\n for (const [key, value] of Object.entries(body)) {\n if (CONTROL_FIELDS.has(key) || honeypotFields.includes(key)) continue;\n if (value === undefined || value === null) continue;\n out[key] = String(value);\n }\n return out;\n}\n\nfunction hasHoneypot(body: Record<string, unknown>, honeypotFields: string[]): boolean {\n return honeypotFields.some((field) => {\n const value = body[field];\n return typeof value === 'string' && value.trim() !== '';\n });\n}\n\nfunction originOf(url: string | undefined): string | null {\n if (!url) return null;\n try {\n return new URL(url).origin;\n } catch {\n return null;\n }\n}\n\n/** A `Location` header response — used for the no-JS form path instead of\n * `Response.redirect()`, which requires a base URL this factory doesn't have. */\nfunction redirectTo(location: string, status = 303): Response {\n return new Response(null, { status, headers: { Location: location } });\n}\n\n/**\n * Build a POST handler for conversions.\n *\n * Returns 204 for a beacon and 200 with `{ success }` for a JSON (fetch-based)\n * form submission, matching what `navigator.sendBeacon` and a fetch-submitting\n * form each expect. A no-JS `<form method=\"post\">` — `application/\n * x-www-form-urlencoded` or `multipart/form-data` — is parsed the same way and,\n * on success, redirected to `redirectTo` if one is configured.\n *\n * **A failed notification does not fail the request.** The visitor has already\n * submitted; refusing them because our mail did not go out turns a lost\n * notification into a lost conversion. Failures are reported through `onResult`.\n */\nexport function createConversionRoute(options: ConversionRouteOptions) {\n const {\n context,\n binding,\n validate,\n onResult,\n now = Date.now,\n redirectTo: successRedirect,\n errorRedirectTo = successRedirect,\n honeypotFields = DEFAULT_HONEYPOT_FIELDS,\n maxBodyBytes = DEFAULT_MAX_BODY_BYTES,\n trustForwardedFor = false,\n } = options;\n\n const allowedOrigin =\n options.allowedOrigin === false ? null : (options.allowedOrigin ?? originOf(context.canonical));\n\n const rateLimit =\n options.rateLimit === false\n ? null\n : {\n windowMs: options.rateLimit?.windowMs ?? DEFAULT_RATE_LIMIT_WINDOW_MS,\n store: options.rateLimit?.store ?? new MemoryRateLimitStore(),\n };\n\n return async ({ request, locals }: ConversionRequestContext): Promise<Response> => {\n const contentType = request.headers.get('content-type') ?? '';\n const isFormPost =\n contentType.includes('multipart/form-data') ||\n contentType.includes('application/x-www-form-urlencoded');\n\n const respondError = (error: string, status: number): Response =>\n isFormPost && errorRedirectTo\n ? redirectTo(errorRedirectTo)\n : Response.json({ success: false, error }, { status });\n\n const contentLength = Number(request.headers.get('content-length') ?? 0);\n if (contentLength > maxBodyBytes) {\n return respondError('Request too large.', 413);\n }\n\n if (allowedOrigin) {\n const requestOrigin = request.headers.get('origin');\n if (requestOrigin && requestOrigin !== allowedOrigin) {\n return respondError('Origin not allowed.', 403);\n }\n }\n\n // A chunked request has no Content-Length to check above, so cap the stream itself.\n const bytes = await readCapped(request, maxBodyBytes);\n if (!bytes) return respondError('Request too large.', 413);\n\n let body: Record<string, unknown>;\n try {\n if (isFormPost) {\n const formData = await new Response(bytes, {\n headers: { 'content-type': contentType },\n }).formData();\n body = {};\n for (const [key, value] of formData.entries()) {\n if (typeof value === 'string') body[key] = value;\n }\n } else {\n // Not just `application/json`: `navigator.sendBeacon` sends a bare\n // string as `text/plain`, and this must keep accepting that body.\n const parsed: unknown = JSON.parse(new TextDecoder().decode(bytes));\n if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {\n return respondError('Malformed request.', 400);\n }\n body = parsed as Record<string, unknown>;\n }\n } catch {\n return respondError('Malformed request.', 400);\n }\n\n const ip =\n request.headers.get('cf-connecting-ip') ??\n (trustForwardedFor ? request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() : null) ??\n undefined;\n\n // Classified before rate limiting, not read raw off the client — see the key\n // below. `phone`/`email` presence is still checked after limiting, since that\n // check just rejects a malformed beacon rather than deciding the bucket.\n const isCall = body['event'] === 'call';\n const isEmailClick = body['event'] === 'email';\n\n // Keyed on IP *and* the server's own classification (`call` | `email` |\n // `form`), never the raw `event` string the client sent. A call beacon and an\n // email-click beacon from the same visitor are two distinct, legitimate\n // conversions, not a repeat of one, so they need separate buckets — but\n // trusting the raw string for that also let a client dodge the form rate\n // limit entirely by varying `event` on every request (any value other than\n // exactly `'call'`/`'email'` used to get its own bucket). Collapsing\n // everything else to `'form'` closes that without losing the call/email split.\n if (rateLimit && ip) {\n const rlKey = `${ip}:${isCall ? 'call' : isEmailClick ? 'email' : 'form'}`;\n const sinceLastHit = await rateLimit.store.hit(rlKey, now());\n if (sinceLastHit !== null && sinceLastHit < rateLimit.windowMs) {\n return respondError('Too many requests.', 429);\n }\n }\n\n // A beacon skips every form check, so its one payload field is format-checked instead:\n // the value is interpolated into a notification someone reads, and an unauthenticated\n // caller should not be able to put arbitrary text there.\n if (isCall && !(typeof body['phone'] === 'string' && PHONE_RE.test(body['phone']))) {\n return respondError('Missing phone.', 400);\n }\n\n if (isEmailClick && !(typeof body['email'] === 'string' && EMAIL_RE.test(body['email']))) {\n return respondError('Missing email.', 400);\n }\n\n // A call and an email-click beacon are the same shape of thing: a fire-and-\n // forget ping with no form fields to validate, honeypot, verify or map into\n // a CRM — both skip every check below that exists for a form submission.\n const isBeacon = isCall || isEmailClick;\n\n const spam = !isBeacon && hasHoneypot(body, honeypotFields);\n\n if (!isBeacon && !spam && options.turnstile) {\n const {\n secretKey,\n fieldName = 'cf-turnstile-response',\n required = false,\n } = options.turnstile;\n const token = body[fieldName];\n if (typeof token === 'string' && token) {\n const verification = await verifyTurnstileToken(token, secretKey, ip);\n if (!verification.success) return respondError('Verification failed.', 400);\n } else if (required) {\n return respondError('Verification required.', 400);\n }\n }\n\n const refusal = !spam && validate?.(body);\n if (refusal) return respondError(refusal, 400);\n\n const respondSuccess = (): Response =>\n isBeacon\n ? new Response(null, { status: 204 })\n : isFormPost && successRedirect\n ? redirectTo(successRedirect)\n : Response.json({ success: true });\n\n // A honeypot trip never reaches `notify()` — the response is identical to a\n // real success, so a bot gets no signal that it was caught.\n if (spam) {\n onResult?.({\n results: [{ channel: 'email', sent: false, reason: 'Skipped: honeypot triggered.' }],\n delivered: false,\n });\n return respondSuccess();\n }\n\n const turnstileFieldName = options.turnstile?.fieldName;\n const excludedFields = turnstileFieldName\n ? [...honeypotFields, turnstileFieldName]\n : honeypotFields;\n\n const event: ConversionEvent = {\n type: isCall ? 'call' : isEmailClick ? 'email' : 'form',\n ...(isCall\n ? { phone: String(body['phone']).replace(/^tel:/, '') }\n : isEmailClick\n ? { email: String(body['email']).replace(/^mailto:/, '') }\n : { formData: toFormData(body, excludedFields) }),\n tracking: parseTrackingCookie(request.headers.get('cookie')),\n userAgent: request.headers.get('user-agent') ?? undefined,\n ip,\n at: now(),\n };\n\n // CRM before notify: the notification email states the CRM outcome, which\n // is only possible once it's known. Never attempted for a `tel:`/`mailto:`\n // beacon — a CRM contact comes from submitted form fields, and neither\n // event has any to map.\n let crmResult: CrmResult | null = null;\n if (!isBeacon && options.crm) {\n crmResult = await deliverToCrm(event, options.crm.config, {\n credentials: options.crm.credentials?.(locals),\n fetch: options.crm.fetch,\n timeoutMs: options.crm.timeoutMs,\n // Provider-neutral: HubSpot reads `hutk` to stitch the submission to\n // its own tracking; Jobber reads `pageUri` for its note body. Neither\n // is HubSpot-specific to build, so this is passed unconditionally\n // rather than gated on `options.crm.config.provider`.\n pageContext: {\n hutk: readCookie(request.headers.get('cookie'), 'hubspotutk') ?? undefined,\n pageUri: request.headers.get('referer') ?? undefined,\n },\n });\n }\n const crmNote = describeCrmResult(crmResult);\n\n // Unlike CRM, this runs for a `tel:`/`mailto:` beacon too — those are\n // exactly the actions the client-side gtag handler would otherwise fire\n // for. Skipped, not failed, when there's no click id to attribute, no\n // `conversionActionId` configured for this action, or no credentials for\n // this environment (e.g. local dev) — none of those is a real failure.\n let googleAdsResult: GoogleAdsUploadResult | null = null;\n const adsCfg = options.googleAdsOffline?.config;\n if (adsCfg?.reporting === 'server' && adsCfg.accountId) {\n const actionId = normalizeConversionActionIds(adsCfg.conversionActionId)[event.type];\n const clickId = googleClickId(event.tracking);\n const credentials = options.googleAdsOffline?.credentials?.(locals);\n if (actionId && clickId && credentials) {\n googleAdsResult = await uploadClickConversion(\n { customerId: adsCfg.accountId, conversionActionId: actionId, clickId, at: event.at },\n credentials,\n options.googleAdsOffline?.fetch,\n );\n }\n }\n const googleAdsNote = describeGoogleAdsOfflineResult(googleAdsResult);\n\n const result: ConversionResult = {\n ...(await notify(\n event,\n context,\n { email: binding?.(locals) },\n {\n notes: [crmNote, googleAdsNote].filter((n): n is string => n != null),\n },\n )),\n ...(crmResult ? { crm: crmResult } : {}),\n ...(googleAdsResult ? { googleAds: googleAdsResult } : {}),\n };\n\n if (onResult) onResult(result);\n else {\n for (const r of result.results) {\n if (!r.sent) console.warn(`[vitops] notification not sent (${r.channel}): ${r.reason}`);\n }\n if (crmResult && !crmResult.ok) {\n console.warn(\n `[vitops] CRM write not delivered (${crmResult.provider}): ${crmResult.reason}`,\n );\n }\n if (googleAdsResult && !googleAdsResult.ok) {\n console.warn(`[vitops] Google Ads offline upload not delivered: ${googleAdsResult.reason}`);\n }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AA+B,eAAe;EAEpE,MAAM,SAA2B;GAC/B,GAAI,MAAM,OACR,OACA,SACA,EAAE,OAAO,UAAU,MAAM,EAAE,GAC3B,EACE,OAAO,CAAC,SAAS,aAAa,CAAC,CAAC,QAAQ,MAAmB,KAAK,IAAI,EACtE,CACF;GACA,GAAI,YAAY,EAAE,KAAK,UAAU,IAAI,CAAC;GACtC,GAAI,kBAAkB,EAAE,WAAW,gBAAgB,IAAI,CAAC;EAC1D;EAEA,IAAI,UAAU,SAAS,MAAM;OACxB;GACH,KAAK,MAAM,KAAK,OAAO,SACrB,IAAI,CAAC,EAAE,MAAM,QAAQ,KAAK,mCAAmC,EAAE,QAAQ,KAAK,EAAE,QAAQ;GAExF,IAAI,aAAa,CAAC,UAAU,IAC1B,QAAQ,KACN,qCAAqC,UAAU,SAAS,KAAK,UAAU,QACzE;GAEF,IAAI,mBAAmB,CAAC,gBAAgB,IACtC,QAAQ,KAAK,qDAAqD,gBAAgB,QAAQ;EAE9F;EAEA,OAAO,eAAe;CACxB;AACF"}
1
+ {"version":3,"file":"conversion.mjs","names":[],"sources":["../../src/routes/conversion.ts"],"sourcesContent":["/**\n * The conversion endpoint, as a factory.\n *\n * A site's contact form and its `tel:`/`mailto:` beacons all mean the same\n * thing — someone got in touch — and all want the same treatment: read the\n * attribution cookie off the request, build the event, notify whoever the\n * config says. That is the same three lines in every project, so it ships\n * here.\n *\n * What deliberately does **not** ship here is field-level validation and business\n * rules — a consumer's form has its own required fields and its own response\n * shape, and baking a guess at those in would make the factory something to\n * fight rather than mount. The handler takes the parsed body and an optional\n * `validate`; that part is the caller's. What **does** ship here is generic abuse\n * resistance (honeypot, origin, rate limit, body size): this is a public,\n * unauthenticated endpoint that sends email, so leaving it wide open makes it a\n * relay and a cost amplifier regardless of whose form is behind it.\n *\n * The path matters: the capture script beacons `tel:`/`mailto:` conversions at\n * `TRACKING_ENDPOINT` (`/api/track`, exported from `@getvitops/astro/tracking`),\n * and the integration warns at build if no route answers it.\n *\n * ```ts\n * // src/pages/api/track.ts\n * export const prerender = false;\n * import { env } from 'cloudflare:workers';\n * import { createConversionRoute } from '@getvitops/astro/routes';\n *\n * export const POST = createConversionRoute({\n * context: {\n * // A bare address is shorthand for `{ provider: 'cloudflare', to }`.\n * notifications: { email: 'sales@acme.ca' },\n * canonical: 'https://acme.ca',\n * organizationName: 'Acme',\n * },\n * binding: () => env.EMAIL,\n * // A no-JS <form method=\"post\"> submission never sees a JSON response body —\n * // it's a full navigation, so send it somewhere real.\n * redirectTo: '/thanks',\n * });\n * ```\n *\n * Cloudflare Turnstile is a separate, opt-in layer on top of the checks above —\n * pass `turnstile: { secretKey }` and a submission carrying a\n * `cf-turnstile-response` token gets it verified server-side via\n * `verifyTurnstileToken` (`@getvitops/utils/security`). The widget itself is the\n * consumer's own markup (`<script src=\"https://challenges.cloudflare.com/\n * turnstile/v0/api.js\" async defer></script>` + `<div class=\"cf-turnstile\"\n * data-sitekey=\"…\">` on their form) — there is no `FormRenderer` for this to hook\n * into generically, matching how the rest of the form is already hand-written.\n * `site.security.turnstile.siteKey` is what the generated privacy policy reads to\n * name Turnstile as a processor; wiring this option up is what makes that true\n * rather than an assertion nothing checks.\n *\n * Two things this example used to get wrong, both of which cost a consumer real\n * time. It read the binding off `locals.runtime.env`, which Astro removed in v6\n * — and because the resulting throw precedes the response, Astro re-reads the\n * request and reports a misleading \"Body has already been used\" instead of the\n * actual error. This package peers on `astro >= 7`, so that form could not work\n * for any supported consumer; `import { env } from 'cloudflare:workers'` is the\n * current one. It also called a `toNotifyContext(config)` helper that used to\n * not exist — `context` can now be built with `toNotifyContext(config)` (also\n * exported from this module), rather than every consumer hand-writing a\n * `NotifyContext` literal:\n *\n * ```ts\n * import config from '../../site.json' with { type: 'json' };\n * import { createConversionRoute, toNotifyContext, toCrmConfig } from '@getvitops/astro/routes';\n *\n * export const POST = createConversionRoute({\n * context: toNotifyContext(config),\n * crm: toCrmConfig(config) && { config: toCrmConfig(config)!, credentials: () => ({ ... }) },\n * binding: () => env.EMAIL,\n * redirectTo: '/thanks',\n * });\n * ```\n */\nimport { AD_PLATFORMS, normalizeConversionActionIds } from '@getvitops/utils/ads';\nimport type { AdPropertySetup } from '@getvitops/utils/ads';\nimport { deliverToCrm, describeCrmResult } from '@getvitops/utils/crm';\nimport type { CrmConfig, CrmCredentials, CrmResult } from '@getvitops/utils/crm';\nimport {\n describeGoogleAdsOfflineResult,\n type EmailBinding,\n notify,\n type NotifyContext,\n uploadClickConversion,\n type GoogleAdsCredentials,\n type GoogleAdsUploadResult,\n} from '@getvitops/utils/notify';\nimport { verifyTurnstileToken } from '@getvitops/utils/security';\nimport { parseTrackingCookie, readCookie } from '@getvitops/utils/tracking';\nimport type { ConversionEvent, TrackingData } from '@getvitops/utils/tracking';\nexport {\n resolveOrganizationName,\n toCrmConfig,\n toGoogleAdsConfig,\n toNotifyContext,\n} from './config-adapters.ts';\n\n/** The subset of Astro's `APIContext` this needs — typed structurally so the\n * factory does not pin an Astro version. */\nexport interface ConversionRequestContext {\n request: Request;\n locals?: unknown;\n}\n\n/**\n * Records a hit for `id` at time `at` (ms) and returns the ms since the previous\n * hit for that `id`, or `null` if this is the first. Pure interface — a store\n * decides nothing, it only remembers.\n */\nexport interface RateLimitStore {\n hit(id: string, at: number): Promise<number | null> | number | null;\n}\n\n/**\n * Per-isolate, in-memory default. Good enough for a single Worker instance\n * fielding occasional contact-form spam; not durable across a fleet, and not\n * shared with other routes. Inject a KV- or Durable-Object-backed `store` for\n * real cross-instance limiting — the interface is one method wide on purpose.\n */\nclass MemoryRateLimitStore implements RateLimitStore {\n private lastHit = new Map<string, number>();\n hit(id: string, at: number): number | null {\n const prev = this.lastHit.get(id);\n this.lastHit.set(id, at);\n return prev === undefined ? null : at - prev;\n }\n}\n\nexport interface ConversionRouteOptions {\n /** Where notifications go. Build it from your site config. */\n context: NotifyContext;\n /**\n * The Workers `send_email` binding for this request, e.g. `() => env.EMAIL`\n * with `env` imported from `cloudflare:workers`. Omit it in dev and the\n * notification is printed instead of sent.\n *\n * Not `(locals) => locals.runtime.env.EMAIL` — Astro removed `locals.runtime`\n * in v6, and this package peers on `>= 7`.\n */\n binding?: (locals: unknown) => EmailBinding | undefined;\n /**\n * Push the submission into a CRM (HubSpot, Zoho Bigin or Jobber) before notifying.\n * Credentials arrive as a function of `locals`, exactly like `binding` above\n * — the consumer reads them from `import { env } from 'cloudflare:workers'`.\n * Credentials are never config; that rule is absolute in this toolchain.\n *\n * A failed CRM write never fails the request and never blocks the honeypot/\n * Turnstile gates above it — those run first, so a bot never reaches the\n * CRM. The outcome is folded into the notification email's \"Delivery\"\n * section (via `describeCrmResult`) so a failed write is never silent.\n */\n crm?: {\n config: CrmConfig;\n credentials?: (locals: unknown) => CrmCredentials | undefined;\n fetch?: typeof fetch;\n timeoutMs?: number;\n };\n /**\n * Upload the conversion to Google Ads server-side instead of relying on the\n * visitor's browser to fire `gtag.js` — see `site.ads.google.reporting` and\n * `conversionActionId`. Credentials arrive as a function of `locals`, same\n * shape as `crm.credentials` and `binding` above.\n *\n * Only fires for an action whose `conversionActionId` is actually set (via\n * `toGoogleAdsConfig(config)`, or hand-built the same shape) **and** whose\n * event carries a Google click id (`gclid`/`gbraid`/`wbraid` on `_ac`) —\n * an organic conversion has nothing for the Ads API to attribute, so it's\n * silently skipped rather than reported as a failure. A failed upload never\n * fails the request, matching `crm` above; the outcome folds into the same\n * \"Delivery\" section via `describeGoogleAdsOfflineResult`.\n */\n googleAdsOffline?: {\n config: Pick<AdPropertySetup, 'accountId' | 'conversionActionId' | 'reporting'>;\n credentials?: (locals: unknown) => GoogleAdsCredentials | undefined;\n fetch?: typeof fetch;\n };\n /**\n * Reject a submission before anything is sent. Return a message to refuse.\n * This is where a consumer puts required-field checks.\n */\n validate?: (body: Record<string, unknown>) => string | null;\n /** Called with every channel outcome, delivered or not, plus the CRM\n * outcome when `crm` is configured. Defaults to console. */\n onResult?: (result: ConversionResult) => void;\n /** Injected in tests. */\n now?: () => number;\n /**\n * Where to send a no-JS `<form method=\"post\">` submission on success. Without\n * this, that visitor's browser navigates to the raw `{\"success\":true}` JSON\n * body as its next page — functional, but not a page anyone should see. Has\n * no effect on the beacon/fetch (JSON) path, which always gets a JSON response.\n */\n redirectTo?: string;\n /**\n * Where to send a refused no-JS submission (validation, spam checks, malformed\n * body). Falls back to `redirectTo` if omitted, and to the JSON error response\n * if neither is set.\n */\n errorRedirectTo?: string;\n /**\n * Field names treated as honeypots: a bot fills every input it finds, a person\n * never sees these (hidden by CSS in the form markup). A non-empty value is\n * silently treated as spam — the response looks identical to a real success,\n * denying the bot the signal it needs to know the trick worked, but nothing is\n * notified and the field never reaches `notify()`. Defaults to the same names\n * `render.ts`'s `OMIT_FIELDS` already knows about (`website`, `_honeypot`,\n * `honeypot`), so the two lists don't need to be kept in sync by hand.\n */\n honeypotFields?: string[];\n /**\n * Reject a submission whose `Content-Length` exceeds this many bytes. Default\n * 25_000 — generous for a contact form with no file upload, tiny for anything\n * meant to relay large payloads. The header is only a fast path: a chunked\n * request carries none, so the body is also read through a byte-capped\n * reader and refused the moment it passes the cap.\n */\n maxBodyBytes?: number;\n /**\n * Fall back to the first `X-Forwarded-For` hop as the client IP when\n * `CF-Connecting-IP` is absent. Default `false`: off Cloudflare the header is\n * whatever the client sent, so honouring it lets a script mint a fresh\n * rate-limit bucket per request. Turn on only behind a proxy that overwrites\n * it. With neither header trusted there is no IP, and rate limiting is skipped.\n */\n trustForwardedFor?: boolean;\n /**\n * Reject a submission whose `Origin` header doesn't match this origin.\n * Defaults to `context.canonical`'s origin. Pass `false` to disable.\n *\n * Best-effort, not a CSRF guard: browsers omit `Origin` on plenty of\n * legitimate same-origin requests, so its *absence* is never treated as a\n * mismatch — only a present-and-different value is rejected. That still stops\n * the common case (a script hitting the endpoint directly from elsewhere)\n * without breaking real submissions from older or privacy-hardened browsers.\n */\n allowedOrigin?: string | false;\n /**\n * Minimum interval between accepted submissions from the same IP. Pass\n * `false` to disable. `windowMs` defaults to 10 seconds — enough to blunt a\n * script firing in a loop without troubling someone who fat-fingers a form\n * and resubmits.\n */\n rateLimit?: { windowMs?: number; store?: RateLimitStore } | false;\n /**\n * Verify a Cloudflare Turnstile token server-side. The **secret** key —\n * unlike `site.security.turnstile.siteKey`, which is public config — must come\n * from an env var, never a config file (the same rule as every other\n * credential in this toolchain).\n */\n turnstile?: {\n secretKey: string;\n /** The submitted field carrying the token. Default `cf-turnstile-response`\n * — the name Cloudflare's widget submits under. */\n fieldName?: string;\n /**\n * Reject a submission with no token at all, rather than only a present-and-\n * invalid one. Default `false`: an absent token is the ordinary shape of a\n * visitor with JS disabled, and this factory is built around that path\n * staying usable. Set `true` only for a form where verified-human matters\n * enough to accept that trade-off — doing so means that form no longer\n * works without JS, on purpose.\n */\n required?: boolean;\n };\n}\n\n/** `notify()`'s own result, plus the CRM and Google Ads offline-upload\n * outcomes when those are configured. Additive over\n * `Awaited<ReturnType<typeof notify>>` — a consumer's existing `onResult`\n * reading `.results`/`.delivered` keeps compiling unchanged. */\nexport interface ConversionResult extends Awaited<ReturnType<typeof notify>> {\n crm?: CrmResult | undefined;\n googleAds?: GoogleAdsUploadResult | undefined;\n}\n\n/** The click id to report to Google Ads, checked in the same order as\n * `AD_PLATFORMS.google.clickIdParams` rather than `getPrimaryClickId()` —\n * that helper returns whichever platform's click id happens to appear first\n * in the *cross-platform* precedence table, which may not be Google's even\n * when a `gclid` is present. */\nfunction googleClickId(tracking: TrackingData | null): { param: string; value: string } | null {\n if (!tracking) return null;\n for (const param of AD_PLATFORMS.google.clickIdParams) {\n const value = tracking[param];\n if (value) return { param, value: String(value) };\n }\n return null;\n}\n\n/** Fields that are never part of a submission's meaning. `cf-turnstile-response`\n * is excluded unconditionally, matching `render.ts`'s `OMIT_FIELDS` — the field\n * carries a verification token, not something the visitor wrote. */\nconst CONTROL_FIELDS = new Set(['event', 'phone', 'cf-turnstile-response']);\n/** The capture script sends `link.href` as-is: `tel:` + digits and the punctuation people type\n * (a space arrives as `%20`). Not free text. */\nconst PHONE_RE = /^(?:tel:)?\\+?[\\d\\s().%-]{3,40}$/i;\n/** `mailto:` + an address, with an optional `?subject=…` query. A shape check, not deliverability. */\nconst EMAIL_RE = /^(?:mailto:)?[^\\s@<>\"?]{1,64}@[^\\s@<>\"?]{1,255}(?:\\?[^\\s<>\"]{0,256})?$/i;\n\nconst DEFAULT_HONEYPOT_FIELDS = ['website', '_honeypot', 'honeypot'];\nconst DEFAULT_MAX_BODY_BYTES = 25_000;\n\n/**\n * Read a request body, giving up as soon as it exceeds `max` bytes. Returns `null` when over\n * the cap. `Content-Length` is optional on chunked requests, so it cannot be the only guard.\n */\nasync function readCapped(request: Request, max: number): Promise<Uint8Array<ArrayBuffer> | null> {\n if (!request.body) return new Uint8Array();\n const reader = request.body.getReader();\n const chunks: Uint8Array[] = [];\n let total = 0;\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n total += value.byteLength;\n if (total > max) {\n await reader.cancel();\n return null;\n }\n chunks.push(value);\n }\n const bytes = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n bytes.set(chunk, offset);\n offset += chunk.byteLength;\n }\n return bytes;\n}\nconst DEFAULT_RATE_LIMIT_WINDOW_MS = 10_000;\n\nfunction toFormData(\n body: Record<string, unknown>,\n honeypotFields: string[],\n): Record<string, string> {\n const out: Record<string, string> = {};\n for (const [key, value] of Object.entries(body)) {\n if (CONTROL_FIELDS.has(key) || honeypotFields.includes(key)) continue;\n if (value === undefined || value === null) continue;\n out[key] = String(value);\n }\n return out;\n}\n\nfunction hasHoneypot(body: Record<string, unknown>, honeypotFields: string[]): boolean {\n return honeypotFields.some((field) => {\n const value = body[field];\n return typeof value === 'string' && value.trim() !== '';\n });\n}\n\nfunction originOf(url: string | undefined): string | null {\n if (!url) return null;\n try {\n return new URL(url).origin;\n } catch {\n return null;\n }\n}\n\n/** A `Location` header response — used for the no-JS form path instead of\n * `Response.redirect()`, which requires a base URL this factory doesn't have. */\nfunction redirectTo(location: string, status = 303): Response {\n return new Response(null, { status, headers: { Location: location } });\n}\n\n/**\n * Build a POST handler for conversions.\n *\n * Returns 204 for a beacon and 200 with `{ success }` for a JSON (fetch-based)\n * form submission, matching what `navigator.sendBeacon` and a fetch-submitting\n * form each expect. A no-JS `<form method=\"post\">` — `application/\n * x-www-form-urlencoded` or `multipart/form-data` — is parsed the same way and,\n * on success, redirected to `redirectTo` if one is configured.\n *\n * **A failed notification does not fail the request.** The visitor has already\n * submitted; refusing them because our mail did not go out turns a lost\n * notification into a lost conversion. Failures are reported through `onResult`.\n */\nexport function createConversionRoute(options: ConversionRouteOptions) {\n const {\n context,\n binding,\n validate,\n onResult,\n now = Date.now,\n redirectTo: successRedirect,\n errorRedirectTo = successRedirect,\n honeypotFields = DEFAULT_HONEYPOT_FIELDS,\n maxBodyBytes = DEFAULT_MAX_BODY_BYTES,\n trustForwardedFor = false,\n } = options;\n\n const allowedOrigin =\n options.allowedOrigin === false ? null : (options.allowedOrigin ?? originOf(context.canonical));\n\n const rateLimit =\n options.rateLimit === false\n ? null\n : {\n windowMs: options.rateLimit?.windowMs ?? DEFAULT_RATE_LIMIT_WINDOW_MS,\n store: options.rateLimit?.store ?? new MemoryRateLimitStore(),\n };\n\n return async ({ request, locals }: ConversionRequestContext): Promise<Response> => {\n const contentType = request.headers.get('content-type') ?? '';\n const isFormPost =\n contentType.includes('multipart/form-data') ||\n contentType.includes('application/x-www-form-urlencoded');\n\n const respondError = (error: string, status: number): Response =>\n isFormPost && errorRedirectTo\n ? redirectTo(errorRedirectTo)\n : Response.json({ success: false, error }, { status });\n\n const contentLength = Number(request.headers.get('content-length') ?? 0);\n if (contentLength > maxBodyBytes) {\n return respondError('Request too large.', 413);\n }\n\n if (allowedOrigin) {\n const requestOrigin = request.headers.get('origin');\n if (requestOrigin && requestOrigin !== allowedOrigin) {\n return respondError('Origin not allowed.', 403);\n }\n }\n\n // A chunked request has no Content-Length to check above, so cap the stream itself.\n const bytes = await readCapped(request, maxBodyBytes);\n if (!bytes) return respondError('Request too large.', 413);\n\n let body: Record<string, unknown>;\n try {\n if (isFormPost) {\n const formData = await new Response(bytes, {\n headers: { 'content-type': contentType },\n }).formData();\n body = {};\n for (const [key, value] of formData.entries()) {\n if (typeof value !== 'string') continue;\n // Several values under one name (a group of tick boxes) join, so none is lost to \"last wins\".\n const prior = body[key];\n body[key] = typeof prior === 'string' && prior ? `${prior}, ${value}` : value;\n }\n } else {\n // Not just `application/json`: `navigator.sendBeacon` sends a bare\n // string as `text/plain`, and this must keep accepting that body.\n const parsed: unknown = JSON.parse(new TextDecoder().decode(bytes));\n if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {\n return respondError('Malformed request.', 400);\n }\n body = parsed as Record<string, unknown>;\n }\n } catch {\n return respondError('Malformed request.', 400);\n }\n\n const ip =\n request.headers.get('cf-connecting-ip') ??\n (trustForwardedFor ? request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() : null) ??\n undefined;\n\n // Classified before rate limiting, not read raw off the client — see the key\n // below. `phone`/`email` presence is still checked after limiting, since that\n // check just rejects a malformed beacon rather than deciding the bucket.\n const isCall = body['event'] === 'call';\n const isEmailClick = body['event'] === 'email';\n\n // Keyed on IP *and* the server's own classification (`call` | `email` |\n // `form`), never the raw `event` string the client sent. A call beacon and an\n // email-click beacon from the same visitor are two distinct, legitimate\n // conversions, not a repeat of one, so they need separate buckets — but\n // trusting the raw string for that also let a client dodge the form rate\n // limit entirely by varying `event` on every request (any value other than\n // exactly `'call'`/`'email'` used to get its own bucket). Collapsing\n // everything else to `'form'` closes that without losing the call/email split.\n if (rateLimit && ip) {\n const rlKey = `${ip}:${isCall ? 'call' : isEmailClick ? 'email' : 'form'}`;\n const sinceLastHit = await rateLimit.store.hit(rlKey, now());\n if (sinceLastHit !== null && sinceLastHit < rateLimit.windowMs) {\n return respondError('Too many requests.', 429);\n }\n }\n\n // A beacon skips every form check, so its one payload field is format-checked instead:\n // the value is interpolated into a notification someone reads, and an unauthenticated\n // caller should not be able to put arbitrary text there.\n if (isCall && !(typeof body['phone'] === 'string' && PHONE_RE.test(body['phone']))) {\n return respondError('Missing phone.', 400);\n }\n\n if (isEmailClick && !(typeof body['email'] === 'string' && EMAIL_RE.test(body['email']))) {\n return respondError('Missing email.', 400);\n }\n\n // A call and an email-click beacon are the same shape of thing: a fire-and-\n // forget ping with no form fields to validate, honeypot, verify or map into\n // a CRM — both skip every check below that exists for a form submission.\n const isBeacon = isCall || isEmailClick;\n\n const spam = !isBeacon && hasHoneypot(body, honeypotFields);\n\n if (!isBeacon && !spam && options.turnstile) {\n const {\n secretKey,\n fieldName = 'cf-turnstile-response',\n required = false,\n } = options.turnstile;\n const token = body[fieldName];\n if (typeof token === 'string' && token) {\n const verification = await verifyTurnstileToken(token, secretKey, ip);\n if (!verification.success) return respondError('Verification failed.', 400);\n } else if (required) {\n return respondError('Verification required.', 400);\n }\n }\n\n const refusal = !spam && validate?.(body);\n if (refusal) return respondError(refusal, 400);\n\n const respondSuccess = (): Response =>\n isBeacon\n ? new Response(null, { status: 204 })\n : isFormPost && successRedirect\n ? redirectTo(successRedirect)\n : Response.json({ success: true });\n\n // A honeypot trip never reaches `notify()` — the response is identical to a\n // real success, so a bot gets no signal that it was caught.\n if (spam) {\n onResult?.({\n results: [{ channel: 'email', sent: false, reason: 'Skipped: honeypot triggered.' }],\n delivered: false,\n });\n return respondSuccess();\n }\n\n const turnstileFieldName = options.turnstile?.fieldName;\n const excludedFields = turnstileFieldName\n ? [...honeypotFields, turnstileFieldName]\n : honeypotFields;\n\n const event: ConversionEvent = {\n type: isCall ? 'call' : isEmailClick ? 'email' : 'form',\n ...(isCall\n ? { phone: String(body['phone']).replace(/^tel:/, '') }\n : isEmailClick\n ? { email: String(body['email']).replace(/^mailto:/, '') }\n : { formData: toFormData(body, excludedFields) }),\n tracking: parseTrackingCookie(request.headers.get('cookie')),\n userAgent: request.headers.get('user-agent') ?? undefined,\n ip,\n at: now(),\n };\n\n // CRM before notify: the notification email states the CRM outcome, which\n // is only possible once it's known. Never attempted for a `tel:`/`mailto:`\n // beacon — a CRM contact comes from submitted form fields, and neither\n // event has any to map.\n let crmResult: CrmResult | null = null;\n if (!isBeacon && options.crm) {\n crmResult = await deliverToCrm(event, options.crm.config, {\n credentials: options.crm.credentials?.(locals),\n fetch: options.crm.fetch,\n timeoutMs: options.crm.timeoutMs,\n // Provider-neutral: HubSpot reads `hutk` to stitch the submission to\n // its own tracking; Jobber reads `pageUri` for its note body. Neither\n // is HubSpot-specific to build, so this is passed unconditionally\n // rather than gated on `options.crm.config.provider`.\n pageContext: {\n hutk: readCookie(request.headers.get('cookie'), 'hubspotutk') ?? undefined,\n pageUri: request.headers.get('referer') ?? undefined,\n },\n });\n }\n const crmNote = describeCrmResult(crmResult);\n\n // Unlike CRM, this runs for a `tel:`/`mailto:` beacon too — those are\n // exactly the actions the client-side gtag handler would otherwise fire\n // for. Skipped, not failed, when there's no click id to attribute, no\n // `conversionActionId` configured for this action, or no credentials for\n // this environment (e.g. local dev) — none of those is a real failure.\n let googleAdsResult: GoogleAdsUploadResult | null = null;\n const adsCfg = options.googleAdsOffline?.config;\n if (adsCfg?.reporting === 'server' && adsCfg.accountId) {\n const actionId = normalizeConversionActionIds(adsCfg.conversionActionId)[event.type];\n const clickId = googleClickId(event.tracking);\n const credentials = options.googleAdsOffline?.credentials?.(locals);\n if (actionId && clickId && credentials) {\n googleAdsResult = await uploadClickConversion(\n { customerId: adsCfg.accountId, conversionActionId: actionId, clickId, at: event.at },\n credentials,\n options.googleAdsOffline?.fetch,\n );\n }\n }\n const googleAdsNote = describeGoogleAdsOfflineResult(googleAdsResult);\n\n const result: ConversionResult = {\n ...(await notify(\n event,\n context,\n { email: binding?.(locals) },\n {\n notes: [crmNote, googleAdsNote].filter((n): n is string => n != null),\n },\n )),\n ...(crmResult ? { crm: crmResult } : {}),\n ...(googleAdsResult ? { googleAds: googleAdsResult } : {}),\n };\n\n if (onResult) onResult(result);\n else {\n for (const r of result.results) {\n if (!r.sent) console.warn(`[vitops] notification not sent (${r.channel}): ${r.reason}`);\n }\n if (crmResult && !crmResult.ok) {\n console.warn(\n `[vitops] CRM write not delivered (${crmResult.provider}): ${crmResult.reason}`,\n );\n }\n if (googleAdsResult && !googleAdsResult.ok) {\n console.warn(`[vitops] Google Ads offline upload not delivered: ${googleAdsResult.reason}`);\n }\n }\n\n return respondSuccess();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0HA,IAAM,uBAAN,MAAqD;CACnD,AAAQ,0BAAU,IAAI,IAAoB;CAC1C,IAAI,IAAY,IAA2B;EACzC,MAAM,OAAO,KAAK,QAAQ,IAAI,EAAE;EAChC,KAAK,QAAQ,IAAI,IAAI,EAAE;EACvB,OAAO,SAAS,SAAY,OAAO,KAAK;CAC1C;AACF;;;;;;AA0JA,SAAS,cAAc,UAAwE;CAC7F,IAAI,CAAC,UAAU,OAAO;CACtB,KAAK,MAAM,SAAS,aAAa,OAAO,eAAe;EACrD,MAAM,QAAQ,SAAS;EACvB,IAAI,OAAO,OAAO;GAAE;GAAO,OAAO,OAAO,KAAK;EAAE;CAClD;CACA,OAAO;AACT;;;;AAKA,MAAM,iCAAiB,IAAI,IAAI;CAAC;CAAS;CAAS;AAAuB,CAAC;;;AAG1E,MAAM,WAAW;;AAEjB,MAAM,WAAW;AAEjB,MAAM,0BAA0B;CAAC;CAAW;CAAa;AAAU;AACnE,MAAM,yBAAyB;;;;;AAM/B,eAAe,WAAW,SAAkB,KAAsD;CAChG,IAAI,CAAC,QAAQ,MAAM,uBAAO,IAAI,WAAW;CACzC,MAAM,SAAS,QAAQ,KAAK,UAAU;CACtC,MAAM,SAAuB,CAAC;CAC9B,IAAI,QAAQ;CACZ,SAAS;EACP,MAAM,EAAE,MAAM,UAAU,MAAM,OAAO,KAAK;EAC1C,IAAI,MAAM;EACV,SAAS,MAAM;EACf,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BAA6B,OAAO,kBAAkB,CAAC,CAAC,MAAM;GAC/E,MAAM,UAAU,cAAc,MAAM,QAAQ;GAC5C,MAAM,cAAc,QAAQ,kBAAkB,cAAc,MAAM;GAClE,IAAI,YAAY,WAAW,aACzB,kBAAkB,MAAM,sBACtB;IAAE,YAAY,OAAO;IAAW,oBAAoB;IAAU;IAAS,IAAI,MAAM;GAAG,GACpF,aACA,QAAQ,kBAAkB,KAC5B;EAEJ;EACA,MAAM,gBAAgB,+BAA+B,eAAe;EAEpE,MAAM,SAA2B;GAC/B,GAAI,MAAM,OACR,OACA,SACA,EAAE,OAAO,UAAU,MAAM,EAAE,GAC3B,EACE,OAAO,CAAC,SAAS,aAAa,CAAC,CAAC,QAAQ,MAAmB,KAAK,IAAI,EACtE,CACF;GACA,GAAI,YAAY,EAAE,KAAK,UAAU,IAAI,CAAC;GACtC,GAAI,kBAAkB,EAAE,WAAW,gBAAgB,IAAI,CAAC;EAC1D;EAEA,IAAI,UAAU,SAAS,MAAM;OACxB;GACH,KAAK,MAAM,KAAK,OAAO,SACrB,IAAI,CAAC,EAAE,MAAM,QAAQ,KAAK,mCAAmC,EAAE,QAAQ,KAAK,EAAE,QAAQ;GAExF,IAAI,aAAa,CAAC,UAAU,IAC1B,QAAQ,KACN,qCAAqC,UAAU,SAAS,KAAK,UAAU,QACzE;GAEF,IAAI,mBAAmB,CAAC,gBAAgB,IACtC,QAAQ,KAAK,qDAAqD,gBAAgB,QAAQ;EAE9F;EAEA,OAAO,eAAe;CACxB;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getvitops/astro",
3
- "version": "8.3.0-alpha.1",
3
+ "version": "8.3.0-alpha.2",
4
4
  "description": "Astro integration for the Vitops design system — favicons/PWA, web-component scripts, and CSS, via a getvitops() integration + <Head /> component.",
5
5
  "homepage": "https://docs.vitops.ca",
6
6
  "bugs": "https://github.com/getvitops/design-system/issues",
@@ -109,11 +109,11 @@
109
109
  "access": "public"
110
110
  },
111
111
  "dependencies": {
112
- "@getvitops/config": "8.3.0-alpha.1",
113
- "@getvitops/core": "8.3.0-alpha.1",
114
- "@getvitops/generator": "8.3.0-alpha.1",
115
- "@getvitops/utils": "8.3.0-alpha.1",
116
- "@getvitops/vite": "8.3.0-alpha.1"
112
+ "@getvitops/config": "8.3.0-alpha.2",
113
+ "@getvitops/core": "8.3.0-alpha.2",
114
+ "@getvitops/generator": "8.3.0-alpha.2",
115
+ "@getvitops/utils": "8.3.0-alpha.2",
116
+ "@getvitops/vite": "8.3.0-alpha.2"
117
117
  },
118
118
  "devDependencies": {
119
119
  "@astrojs/sitemap": "^3.7.3",
@@ -8,11 +8,15 @@
8
8
  * <SiteFooter {sitemap} {legal} name={siteTitle} />
9
9
  *
10
10
  * Both menus are optional and each renders only if it has items, so a one-page site
11
- * gets just the copyright line. Menus are FLAT here (depth 1): a footer that nests is
12
- * a site map, and wants a page, not a column. `name` is `organization.name`.
11
+ * gets just the copyright line. `name` is `organization.name`.
13
12
  *
14
- * The two `<nav>`s are named ("Site map", "Legal") so the landmark list is not a pile
15
- * of unlabeled regions, and the copyright is plain text — never a link.
13
+ * **One level of grouping in the site map.** A top-level item with children becomes a column:
14
+ * its label is the column heading and its children are the links. A childless item stays in the
15
+ * flat list ahead of the columns. Deeper levels are ignored; a footer that nests further is a
16
+ * site map and wants a page. The legal menu is always flat.
17
+ *
18
+ * Every `<nav>` is named (a column by its heading, the others "Site map" / "Legal") so the
19
+ * landmark list is not a pile of unlabeled regions, and the copyright is plain text, never a link.
16
20
  */
17
21
  import { isCurrent, linkRel, menuItems, type NavMenuInput } from './sitenav.ts';
18
22
 
@@ -43,16 +47,43 @@ const {
43
47
  class: className,
44
48
  } = Astro.props;
45
49
 
50
+ const sitemapItems = menuItems(sitemap);
51
+ const columns = sitemapItems
52
+ .filter((item) => (item.children?.length ?? 0) > 0)
53
+ .map((item, i) => ({ id: `sitefooter-col-${i}`, heading: item, items: item.children! }));
54
+ const flat = sitemapItems.filter((item) => !(item.children?.length ?? 0));
46
55
  const groups = [
47
- { label: sitemapLabel, items: menuItems(sitemap), kind: 'sitemap' },
56
+ { label: sitemapLabel, items: flat, kind: 'sitemap' },
48
57
  { label: legalLabel, items: menuItems(legal), kind: 'legal' },
49
58
  ].filter((g) => g.items.length > 0);
50
59
  ---
51
60
 
52
61
  <footer class:list={['sitefooter', className]}>
53
62
  {
54
- groups.length > 0 && (
63
+ (groups.length > 0 || columns.length > 0) && (
55
64
  <div class="sitefooter__nav">
65
+ {columns.map((col) => (
66
+ <nav class="sitefooter__group sitefooter__group--column" aria-labelledby={col.id}>
67
+ <p class="sitefooter__heading font-eyebrow" id={col.id}>
68
+ {col.heading.label}
69
+ </p>
70
+ <ul class="sitefooter__list sitefooter__list--column" role="list">
71
+ {col.items.map((item) => (
72
+ <li>
73
+ <a
74
+ class="sitefooter__link"
75
+ href={item.url || '#'}
76
+ target={item.target || undefined}
77
+ rel={linkRel(item)}
78
+ aria-current={isCurrent(item.url, current) ? 'page' : undefined}
79
+ >
80
+ {item.label}
81
+ </a>
82
+ </li>
83
+ ))}
84
+ </ul>
85
+ </nav>
86
+ ))}
56
87
  {groups.map((g) => (
57
88
  <nav class={`sitefooter__group sitefooter__group--${g.kind}`} aria-label={g.label}>
58
89
  <ul class="sitefooter__list" role="list">
@@ -32,7 +32,9 @@
32
32
  * EmDash menu items have no description field. Convention until they do (an upstream
33
33
  * request): an item's **`titleAttr` is its description**, rendered as muted second-line
34
34
  * text under the link. It is NOT emitted as a `title` tooltip. `cssClasses` pass through
35
- * on the link.
35
+ * on the link. An item with `draft` in its `cssClasses` is a planned link: it stays in the
36
+ * menu but is not rendered. An item with `cta` there renders as a button (`.cta`), and the
37
+ * `actions` slot adds header buttons that are not menu items (rendered beside each tier's nav).
36
38
  *
37
39
  * Accessibility: `<nav aria-label>` landmark, `aria-current="page"` on the current
38
40
  * item, the link is never inside `<summary>` (no nested interactive controls), caret
@@ -157,6 +159,11 @@ const tiers = selectTiers({
157
159
  <SiteNavList items={tier.items} current={current} />
158
160
  </div>
159
161
  </nav>
162
+ {Astro.slots.has('actions') && (
163
+ <div class="sitenav__actions">
164
+ <slot name="actions" />
165
+ </div>
166
+ )}
160
167
  </div>
161
168
  );
162
169
  })
@@ -28,6 +28,8 @@ export interface NavItem {
28
28
  titleAttr?: string;
29
29
  /** Extra classes from the editor; passed through on the link. */
30
30
  cssClasses?: string;
31
+ /** `true` is the same as `draft` in `cssClasses`: a planned link, kept but not rendered. */
32
+ hidden?: boolean;
31
33
  children?: NavItem[];
32
34
  }
33
35
 
@@ -41,10 +43,32 @@ export const MENU_NAMES = {
41
43
  mega: 'primary-mega',
42
44
  } as const;
43
45
 
46
+ /**
47
+ * A planned link: an item whose `cssClasses` contains `draft` is kept in the menu (so it is there
48
+ * to publish later, with its place in the order) but is not rendered, nor are its children.
49
+ * EmDash menu items have no draft state of their own, so the editor's "CSS classes" field carries it.
50
+ */
51
+ export function isDraft(item: NavItem): boolean {
52
+ return item.hidden === true || /(^|\s)draft(\s|$)/.test(item.cssClasses ?? '');
53
+ }
54
+
55
+ const hasDraft = (items: NavItem[]): boolean =>
56
+ items.some((item) => isDraft(item) || hasDraft(item.children ?? []));
57
+
58
+ /** Drop draft items at every level, returning copies; the input is never mutated. */
59
+ function withoutDrafts(items: NavItem[]): NavItem[] {
60
+ if (!hasDraft(items)) return items; // no drafts: hand back the cached menu untouched
61
+ return items
62
+ .filter((item) => !isDraft(item))
63
+ .map((item) =>
64
+ item.children?.length ? { ...item, children: withoutDrafts(item.children) } : item,
65
+ );
66
+ }
67
+
44
68
  export function menuItems(menu: NavMenuInput): NavItem[] {
45
69
  if (!menu) return [];
46
70
  const items = Array.isArray(menu) ? menu : (menu.items ?? []);
47
- return items;
71
+ return withoutDrafts(items);
48
72
  }
49
73
 
50
74
  /**
package/virtual.d.ts CHANGED
@@ -71,7 +71,7 @@ declare module 'virtual:getvitops/head' {
71
71
 
72
72
  /**
73
73
  * The committed `.vitops/reviews.json` cache as a build-time import — for components that
74
- * render in a Worker and so cannot read the file (the EmDash `vitops.reviews` block). Always
74
+ * render in a Worker and so cannot read the file (the EmDash `vitops_reviews` block). Always
75
75
  * defined: with no cache file it is `{ version: 1, locations: {} }`.
76
76
  */
77
77
  declare module 'virtual:getvitops/reviews' {