Files
zeekayandHanzo Dev 42425d6bcd webhooks: deliver through Hanzo KV, and stop pretending a dropped event was sent
QStash was already gone — `lib/cron/index.ts` shipped a stub whose publishJSON
logged a warning, returned `{ messageId: "noop" }` and DISCARDED the event. Every
customer webhook has been silently dropped since. Its sibling stub, `receiver`,
returned `true` for any signature.

Deliveries now go to a KV list (lib/kv-queue/webhooks.ts): rpush to enqueue, lpop
to take, failures returned with a raised attempt count until MAX_ATTEMPTS so a
customer endpoint that is briefly down keeps its event and one that is gone does
not spin. The signature is computed at enqueue and carried with the item, so a
retry re-sends identical bytes rather than re-signing a payload whose shape may
have moved. Enqueue never throws: a KV outage must not fail the caller's write.
/api/cron/webhooks drains it.

The cron guard was worse than the stub. It read

    if (process.env.VERCEL === "1") { ...verify Upstash-Signature... }

and this app runs on Kubernetes, so the branch was ALWAYS false — every
/api/cron/* route was reachable by anyone with the path, and the verifier behind
it asserted nothing anyway. Replaced with a shared secret that FAILS CLOSED: no
CRON_SECRET, no cron. Nothing schedules these today, so a missing secret costs
nothing where the old behaviour cost everything. It is a secret, not an identity —
user-facing auth stays with Hanzo IAM.

Also drops the QStash env block, the dead Upstash locker vars, and the vendor
vocabulary in lib/kv.ts, where the shims describe a SHAPE (options object,
{ score, member }) rather than whoever shipped it.

Verified with npx tsc --noEmit: 100 errors before and after, 0 introduced,
compared set-wise by file:line:code.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-09 10:16:19 -07:00

241 lines
5.9 KiB
TypeScript

import { Tinybird } from "@chronark/zod-bird";
import { z } from "zod";
import { VIDEO_EVENT_TYPES } from "../constants";
import { WEBHOOK_TRIGGERS } from "../webhook/constants";
const tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! });
export const getTotalAvgPageDuration = tb.buildPipe({
pipe: "get_total_average_page_duration__v5",
parameters: z.object({
documentId: z.string(),
excludedLinkIds: z.string().describe("Comma separated linkIds"),
excludedViewIds: z.string().describe("Comma separated viewIds"),
since: z.number(),
}),
data: z.object({
versionNumber: z.number().int(),
pageNumber: z.string(),
avg_duration: z.number(),
}),
});
export const getViewPageDuration = tb.buildPipe({
pipe: "get_page_duration_per_view__v5",
parameters: z.object({
documentId: z.string(),
viewId: z.string(),
since: z.number(),
until: z.number().optional(),
}),
data: z.object({
pageNumber: z.string(),
sum_duration: z.number(),
}),
});
export const getViewCompletionStats = tb.buildPipe({
pipe: "get_view_completion_stats__v1",
parameters: z.object({
documentId: z.string(),
excludedViewIds: z.string().describe("Comma separated viewIds"),
since: z.number(),
}),
data: z.object({
viewId: z.string(),
versionNumber: z.number().int(),
pages_viewed: z.number(),
}),
});
export const getTotalDocumentDuration = tb.buildPipe({
pipe: "get_total_document_duration__v1",
parameters: z.object({
documentId: z.string(),
excludedLinkIds: z.string().describe("Comma separated linkIds"),
excludedViewIds: z.string().describe("Comma separated viewIds"),
since: z.number(),
until: z.number().optional(),
}),
data: z.object({
sum_duration: z.number(),
}),
});
export const getTotalLinkDuration = tb.buildPipe({
pipe: "get_total_link_duration__v1",
parameters: z.object({
linkId: z.string(),
documentId: z.string(),
excludedViewIds: z.string().describe("Comma separated viewIds"),
since: z.number(),
until: z.number().optional(),
}),
data: z.object({
sum_duration: z.number(),
view_count: z.number(),
}),
});
export const getTotalViewerDuration = tb.buildPipe({
pipe: "get_total_viewer_duration__v1",
parameters: z.object({
viewIds: z.string().describe("Comma separated viewIds"),
since: z.number(),
until: z.number().optional(),
}),
data: z.object({
sum_duration: z.number(),
}),
});
export const getViewUserAgent_v2 = tb.buildPipe({
pipe: "get_useragent_per_view__v2",
parameters: z.object({
documentId: z.string(),
viewId: z.string(),
since: z.number(),
}),
data: z.object({
country: z.string(),
city: z.string(),
browser: z.string(),
os: z.string(),
device: z.string(),
}),
});
export const getViewUserAgent = tb.buildPipe({
pipe: "get_useragent_per_view__v3",
parameters: z.object({
viewId: z.string(),
}),
data: z.object({
country: z.string(),
city: z.string(),
browser: z.string(),
os: z.string(),
device: z.string(),
}),
});
export const getTotalDataroomDuration = tb.buildPipe({
pipe: "get_total_dataroom_duration__v1",
parameters: z.object({
dataroomId: z.string(),
excludedLinkIds: z.array(z.string()),
excludedViewIds: z.array(z.string()),
since: z.number(),
}),
data: z.object({
viewId: z.string(),
sum_duration: z.number(),
}),
});
export const getDocumentDurationPerViewer = tb.buildPipe({
pipe: "get_document_duration_per_viewer__v1",
parameters: z.object({
documentId: z.string(),
viewIds: z.string().describe("Comma separated viewIds"),
}),
data: z.object({
sum_duration: z.number(),
}),
});
export const getWebhookEvents = tb.buildPipe({
pipe: "get_webhook_events__v1",
parameters: z.object({
webhookId: z.string(),
}),
data: z.object({
event_id: z.string(),
webhook_id: z.string(),
message_id: z.string(), // delivery id
event: z.enum(WEBHOOK_TRIGGERS),
url: z.string(),
http_status: z.number(),
request_body: z.string(),
response_body: z.string(),
timestamp: z.string(),
}),
});
export const getVideoEventsByDocument = tb.buildPipe({
pipe: "get_video_events_by_document__v1",
parameters: z.object({
document_id: z.string(),
}),
data: z.object({
timestamp: z.string(),
view_id: z.string(),
event_type: z.enum(VIDEO_EVENT_TYPES),
start_time: z.number(),
end_time: z.number(),
playback_rate: z.number(),
volume: z.number(),
is_muted: z.number(),
is_focused: z.number(),
is_fullscreen: z.number(),
}),
});
export const getVideoEventsByView = tb.buildPipe({
pipe: "get_video_events_by_view__v1",
parameters: z.object({
document_id: z.string(),
view_id: z.string(),
}),
data: z.object({
timestamp: z.string(),
event_type: z.string(),
start_time: z.number(),
end_time: z.number(),
}),
});
export const getClickEventsByView = tb.buildPipe({
pipe: "get_click_events_by_view__v1",
parameters: z.object({
document_id: z.string(),
view_id: z.string(),
}),
data: z.object({
timestamp: z.string(),
document_id: z.string(),
dataroom_id: z.string().nullable(),
view_id: z.string(),
page_number: z.string(),
version_number: z.number(),
href: z.string(),
}),
});
export const getDataroomViewDocumentStats = tb.buildPipe({
pipe: "get_dataroom_view_document_stats__v1",
parameters: z.object({
viewIds: z.string().describe("Comma separated viewIds"),
}),
data: z.object({
viewId: z.string(),
documentId: z.string(),
sum_duration: z.number(),
pages_viewed: z.number(),
}),
});
export const getTotalTeamDuration = tb.buildPipe({
pipe: "get_total_team_duration__v1",
parameters: z.object({
documentIds: z.string().describe("Comma separated documentIds"),
since: z.number(),
until: z.number(),
}),
data: z.object({
total_duration: z.number(),
unique_countries: z.array(z.string()),
}),
});