MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / logWebhookEvent

Function logWebhookEvent

apps/web/src/lib/integrations/db/webhook-events.ts:10–51  ·  view source on GitHub ↗
(data: {
  owner: Owner;
  platform: string;
  event_type: string;
  event_action: string;
  payload: unknown;
  headers: unknown;
  event_signature: string;
})

Source from the content-addressed store, hash-verified

8/**
9 * Identifies a row of the partitioned `webhook_events_v2`. `createdAt` lets
10 * updates touch one partition instead of probing all of them, so it must be
11 * passed back exactly as it was written or read.
12 */
13export type WebhookEventRef = { id: string; createdAt: string };
14
15/**
16 * Unicode NUL (\u0000) is not representable in PostgreSQL text, so a payload or
17 * header value that contains one makes the jsonb insert fail with 22P05. Webhook
18 * bodies are external JSON, so strip NULs recursively before insert rather than
19 * rejecting the whole event. NULs are replaced with the empty string so the rest
20 * of the field is preserved.
21 */
22function stripNulBytes(value: unknown): unknown {
23 if (typeof value === 'string') return value.replaceAll('\u0000', '');
24 if (Array.isArray(value)) return value.map(stripNulBytes);
25 if (value !== null && typeof value === 'object') {
26 // Object.fromEntries defines own properties, so a '__proto__' key survives
27 // instead of being routed through the inherited setter and dropped.
28 return Object.fromEntries(
29 Object.entries(value).map(([key, entry]) => [
30 key.replaceAll('\u0000', ''),
31 stripNulBytes(entry),
32 ])
33 );
34 }
35 return value;
36}
37
38/**
39 * Whether the pre-partitioning `webhook_events` table already holds this
40 * delivery. Until its rows age out, a redelivery of an event accepted before
41 * the switch is only recorded there. Remove once that table is dropped.
42 */
43export async function legacyWebhookEventIdForSignature(
44 tx: DrizzleTransaction,
45 eventSignature: string
46): Promise<string | null> {
47 const [row] = await tx
48 .select({ id: webhook_events.id })
49 .from(webhook_events)
50 .where(eq(webhook_events.event_signature, eventSignature))
51 .limit(1);
52 return row?.id ?? null;
53}
54

Callers 2

logWebhookFunction · 0.90
logWebhookFunction · 0.90

Calls 2

valuesMethod · 0.65
logMethod · 0.65

Tested by

no test coverage detected