@secondlayer/subgraphs 3.7.4 → 3.9.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.
@@ -0,0 +1,19 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/runtime/emitter.ts", "../src/runtime/formats/index.ts", "../src/runtime/formats/cloudevents.ts", "../src/runtime/formats/cloudflare.ts", "../src/runtime/formats/inngest.ts", "../src/runtime/formats/raw.ts", "../src/runtime/formats/standard-webhooks.ts", "../src/runtime/formats/trigger.ts", "../src/runtime/subscription-state.ts", "../src/runtime/emitter-matcher.ts"],
4
+ "sourcesContent": [
5
+ "import { randomUUID } from \"node:crypto\";\nimport {\n\ttype Database,\n\ttype Subscription,\n\ttype SubscriptionOutbox,\n\tgetTargetDb,\n} from \"@secondlayer/shared/db\";\nimport { getSubscriptionSigningSecret } from \"@secondlayer/shared/db/queries/subscriptions\";\nimport { logger } from \"@secondlayer/shared/logger\";\nimport type { SubscriptionTestResult } from \"@secondlayer/shared/schemas/subscriptions\";\nimport { listen, targetListenerUrl } from \"@secondlayer/shared/queue/listener\";\nimport { type Kysely, sql } from \"kysely\";\nimport { buildForFormat } from \"./formats/index.ts\";\nimport { refreshMatcher } from \"./subscription-state.ts\";\n\n/**\n * Subscription emitter — drains `subscription_outbox` and POSTs deliveries.\n *\n * Hot path: LISTEN on `subscriptions:new_outbox` and `subscriptions:changed`.\n * On notify, claim a batch with `FOR UPDATE SKIP LOCKED LIMIT 50`, dispatch\n * each row via HTTP, write a `subscription_deliveries` attempt row, then\n * either mark `status='delivered'` or schedule the next attempt.\n *\n * Backoff schedule (attempt → wait):\n * 0 → 30s, 1 → 2m, 2 → 10m, 3 → 1h, 4 → 6h, 5 → 24h, 6 → 72h.\n * After `max_retries` (default 7) attempts → `status='dead'`.\n *\n * Per-sub circuit breaker: 20 consecutive failures → sub flipped to\n * `paused` with `circuit_opened_at=NOW()`. Manual /resume drains backlog.\n *\n * Per-sub concurrency cap: in-memory semaphore, default 4 in-flight HTTP\n * requests per subscription. Sprint-4 adds SSRF allowlist.\n */\n\nconst BATCH_SIZE = 50;\nconst LIVE_SHARE = 0.9; // 90% of batch to non-replay, 10% to replay\nconst BACKOFF_SECONDS = [30, 120, 600, 3600, 21600, 86400, 259200];\nconst CIRCUIT_THRESHOLD = 20;\n/**\n * When a batch is claimed the outbox row's `next_attempt_at` is pushed\n * `LOCK_WINDOW_MS` into the future. Any crash between claim + settle\n * leaves the row re-claimable after this window expires — the SSOT for\n * double-dispatch prevention.\n */\nconst LOCK_WINDOW_MS = 60_000;\n\ninterface RunningState {\n\trunning: boolean;\n\tinFlightBySub: Map<string, number>;\n\tclaimInFlight: boolean;\n}\n\nfunction nextDelaySeconds(attempt: number): number {\n\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\treturn BACKOFF_SECONDS[Math.min(attempt, BACKOFF_SECONDS.length - 1)]!;\n}\n\n// ── SSRF guard ────────────────────────────────────────────────────────\n// Block deliveries to private/loopback/link-local ranges unless\n// SECONDLAYER_ALLOW_PRIVATE_EGRESS=true (self-host + local-dev opt-in).\n\nconst PRIVATE_V4_PATTERNS = [\n\t/^127\\./, // loopback\n\t/^10\\./, // private class A\n\t/^172\\.(1[6-9]|2\\d|3[01])\\./, // private class B\n\t/^192\\.168\\./, // private class C\n\t/^169\\.254\\./, // link-local\n\t/^0\\./, // \"this\" network\n\t/^100\\.(6[4-9]|[7-9]\\d|1[01]\\d|12[0-7])\\./, // CGNAT 100.64/10\n];\n\n/**\n * Reject hostnames that resolve to, or are spelled as, private IPs.\n * Covers v4 literal, v6 literal, IPv4-mapped IPv6 (`::ffff:127.0.0.1`),\n * and the `localhost` alias. DNS-level rebinding still bypasses this\n * check (hostname that resolves to a private IP at egress time) — mitigate\n * with an egress allowlist at the network level if that matters.\n */\nfunction isPrivateEgress(url: string): boolean {\n\tlet parsed: URL;\n\ttry {\n\t\tparsed = new URL(url);\n\t} catch {\n\t\treturn true; // malformed URL: reject\n\t}\n\tif (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\") {\n\t\treturn true;\n\t}\n\n\t// Strip brackets from IPv6 literals.\n\tconst raw = parsed.hostname.toLowerCase();\n\tconst host =\n\t\traw.startsWith(\"[\") && raw.endsWith(\"]\") ? raw.slice(1, -1) : raw;\n\n\tif (host === \"localhost\" || host === \"0.0.0.0\") return true;\n\tif (host === \"::\" || host === \"::1\") return true;\n\t// Unique-local (fc00::/7) + link-local (fe80::/10)\n\tif (/^f[cd][0-9a-f]{2}:/.test(host)) return true;\n\tif (/^fe[89ab][0-9a-f]:/.test(host)) return true;\n\n\t// IPv4-mapped IPv6 — `::ffff:127.0.0.1` or `::ffff:7f00:0001`\n\tconst mapped = host.match(/^::ffff:(.+)$/);\n\tif (mapped) {\n\t\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\t\tconst inner = mapped[1]!;\n\t\t// Dotted form: rerun v4 checks.\n\t\tif (/^\\d+\\.\\d+\\.\\d+\\.\\d+$/.test(inner)) {\n\t\t\tfor (const p of PRIVATE_V4_PATTERNS) if (p.test(inner)) return true;\n\t\t}\n\t\t// Hex form: 7f00:0001 → 127.0.0.1\n\t\tconst hex = inner.match(/^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);\n\t\tif (hex) {\n\t\t\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\t\t\tconst a = Number.parseInt(hex[1]!, 16);\n\t\t\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\t\t\tconst b = Number.parseInt(hex[2]!, 16);\n\t\t\tconst dotted = `${(a >> 8) & 0xff}.${a & 0xff}.${(b >> 8) & 0xff}.${b & 0xff}`;\n\t\t\tfor (const p of PRIVATE_V4_PATTERNS) if (p.test(dotted)) return true;\n\t\t}\n\t}\n\n\tfor (const p of PRIVATE_V4_PATTERNS) {\n\t\tif (p.test(host)) return true;\n\t}\n\treturn false;\n}\n\nfunction allowPrivateEgress(): boolean {\n\treturn process.env.SECONDLAYER_ALLOW_PRIVATE_EGRESS === \"true\";\n}\n\n/** The wire result of one POST attempt (no DB side effect) — shared by the\n * emitter hot path and the `deliverTestEvent` test path. */\ninterface PostResult {\n\tok: boolean;\n\tstatusCode: number | null;\n\terror: string | null;\n\tdurationMs: number;\n\tresponseBody: string | null;\n\tresponseHeaders: Record<string, string> | null;\n}\n\n/** POST a pre-built body to a subscription URL with the SSRF guard + timeout.\n * Pure transport: returns the attempt result; the caller logs the delivery row. */\nasync function postToSubscription(\n\turl: string,\n\tbody: string,\n\theaders: Record<string, string>,\n\ttimeoutMs: number,\n): Promise<PostResult> {\n\tif (isPrivateEgress(url) && !allowPrivateEgress()) {\n\t\tlogger.warn(\"[emitter] refused private egress\", { url });\n\t\treturn {\n\t\t\tok: false,\n\t\t\tstatusCode: null,\n\t\t\terror:\n\t\t\t\t\"refused private egress (set SECONDLAYER_ALLOW_PRIVATE_EGRESS=true to allow)\",\n\t\t\tdurationMs: 0,\n\t\t\tresponseBody: null,\n\t\t\tresponseHeaders: null,\n\t\t};\n\t}\n\n\tconst start = performance.now();\n\tlet statusCode: number | null = null;\n\tlet error: string | null = null;\n\tlet ok = false;\n\tlet responseBody = \"\";\n\tlet responseHeaders: Record<string, string> = {};\n\ttry {\n\t\tconst res = await fetch(url, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders,\n\t\t\tbody,\n\t\t\tsignal: AbortSignal.timeout(timeoutMs),\n\t\t});\n\t\tstatusCode = res.status;\n\t\tok = res.ok;\n\t\t// Collect small response preview for the delivery log (≤8KB).\n\t\tconst buf = await res.arrayBuffer();\n\t\tconst truncated = buf.byteLength > 8192 ? buf.slice(0, 8192) : buf;\n\t\tresponseBody = Buffer.from(truncated).toString(\"utf8\");\n\t\tresponseHeaders = Object.fromEntries(res.headers.entries());\n\t} catch (err) {\n\t\terror = err instanceof Error ? err.message : String(err);\n\t}\n\treturn {\n\t\tok,\n\t\tstatusCode,\n\t\terror,\n\t\tdurationMs: Math.round(performance.now() - start),\n\t\tresponseBody: responseBody || null,\n\t\tresponseHeaders,\n\t};\n}\n\nasync function dispatchOne(\n\tdb: Kysely<Database>,\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n): Promise<{\n\tok: boolean;\n\tstatusCode: number | null;\n\terror: string | null;\n\tdurationMs: number;\n}> {\n\tconst { body, headers } = buildForFormat(\n\t\toutboxRow,\n\t\tsub,\n\t\tgetSubscriptionSigningSecret(sub),\n\t);\n\tconst r = await postToSubscription(sub.url, body, headers, sub.timeout_ms);\n\n\tconst attempt = outboxRow.attempt + 1;\n\tawait db\n\t\t.insertInto(\"subscription_deliveries\")\n\t\t.values({\n\t\t\toutbox_id: outboxRow.id,\n\t\t\tsubscription_id: outboxRow.subscription_id,\n\t\t\tattempt,\n\t\t\tstatus_code: r.statusCode,\n\t\t\tresponse_headers: r.responseHeaders,\n\t\t\tresponse_body: r.responseBody,\n\t\t\terror_message: r.error,\n\t\t\tduration_ms: r.durationMs,\n\t\t})\n\t\t.execute();\n\n\treturn {\n\t\tok: r.ok,\n\t\tstatusCode: r.statusCode,\n\t\terror: r.error,\n\t\tdurationMs: r.durationMs,\n\t};\n}\n\n/** A representative (non-persisted) outbox row for a test delivery, shaped to the\n * subscription's kind so `buildForFormat` produces a realistic body. */\nfunction buildTestOutboxRow(sub: Subscription): SubscriptionOutbox {\n\tconst now = new Date();\n\treturn {\n\t\tid: randomUUID(),\n\t\tsubscription_id: sub.id,\n\t\tkind: sub.kind,\n\t\tsubgraph_name: sub.subgraph_name ?? null,\n\t\ttable_name: sub.table_name ?? null,\n\t\tblock_height: 0,\n\t\ttx_id: null,\n\t\trow_pk: null,\n\t\tevent_type:\n\t\t\tsub.kind === \"chain\"\n\t\t\t\t? \"chain.test.apply\"\n\t\t\t\t: `${sub.subgraph_name ?? \"subgraph\"}.${sub.table_name ?? \"test\"}.created`,\n\t\tpayload: {\n\t\t\ttest: true,\n\t\t\tmessage: \"Secondlayer test delivery\",\n\t\t\tsubscription_id: sub.id,\n\t\t\tsent_at: now.toISOString(),\n\t\t},\n\t\tdedup_key: `test:${sub.id}:${now.getTime()}`,\n\t\tattempt: 0,\n\t\tnext_attempt_at: now,\n\t\tstatus: \"pending\",\n\t\tis_replay: false,\n\t\tdelivered_at: null,\n\t\tfailed_at: null,\n\t\tlocked_by: null,\n\t\tlocked_until: null,\n\t\tcreated_at: now,\n\t};\n}\n\n/**\n * Build a representative webhook for `sub`'s configured format, POST it (same\n * SSRF guard + timeout + signing as a real delivery), and log a delivery row\n * with a null `outbox_id` so it appears under the subscription's deliveries\n * without being tied to a queued event. Powers `POST /:id/test`.\n */\nexport async function deliverTestEvent(\n\tdb: Kysely<Database>,\n\tsub: Subscription,\n): Promise<SubscriptionTestResult> {\n\tconst testRow = buildTestOutboxRow(sub);\n\tconst { body, headers } = buildForFormat(\n\t\ttestRow,\n\t\tsub,\n\t\tgetSubscriptionSigningSecret(sub),\n\t);\n\tconst r = await postToSubscription(sub.url, body, headers, sub.timeout_ms);\n\tconst inserted = await db\n\t\t.insertInto(\"subscription_deliveries\")\n\t\t.values({\n\t\t\toutbox_id: null,\n\t\t\tsubscription_id: sub.id,\n\t\t\tattempt: 1,\n\t\t\tstatus_code: r.statusCode,\n\t\t\tresponse_headers: r.responseHeaders,\n\t\t\tresponse_body: r.responseBody,\n\t\t\terror_message: r.error,\n\t\t\tduration_ms: r.durationMs,\n\t\t})\n\t\t.returning(\"id\")\n\t\t.executeTakeFirstOrThrow();\n\treturn {\n\t\tok: r.ok,\n\t\tstatusCode: r.statusCode,\n\t\terror: r.error,\n\t\tdurationMs: r.durationMs,\n\t\tdeliveryId: inserted.id,\n\t};\n}\n\nasync function settleDelivered(\n\tdb: Kysely<Database>,\n\toutboxRow: SubscriptionOutbox,\n): Promise<void> {\n\tawait db.transaction().execute(async (tx) => {\n\t\tawait tx\n\t\t\t.updateTable(\"subscription_outbox\")\n\t\t\t.set({\n\t\t\t\tstatus: \"delivered\",\n\t\t\t\tdelivered_at: new Date(),\n\t\t\t\tattempt: outboxRow.attempt + 1,\n\t\t\t\tlocked_by: null,\n\t\t\t\tlocked_until: null,\n\t\t\t})\n\t\t\t.where(\"id\", \"=\", outboxRow.id)\n\t\t\t.execute();\n\t\tawait tx\n\t\t\t.updateTable(\"subscriptions\")\n\t\t\t.set({\n\t\t\t\tlast_delivery_at: new Date(),\n\t\t\t\tlast_success_at: new Date(),\n\t\t\t\tcircuit_failures: 0,\n\t\t\t\tlast_error: null,\n\t\t\t\tupdated_at: new Date(),\n\t\t\t})\n\t\t\t.where(\"id\", \"=\", outboxRow.subscription_id)\n\t\t\t.execute();\n\t});\n}\n\nasync function settleFailed(\n\tdb: Kysely<Database>,\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n\terrText: string,\n): Promise<void> {\n\tconst attempt = outboxRow.attempt + 1;\n\tconst isDead = attempt >= sub.max_retries;\n\tconst nextAt = isDead\n\t\t? null\n\t\t: new Date(Date.now() + nextDelaySeconds(outboxRow.attempt) * 1000);\n\n\tawait db.transaction().execute(async (tx) => {\n\t\tawait tx\n\t\t\t.updateTable(\"subscription_outbox\")\n\t\t\t.set({\n\t\t\t\tattempt,\n\t\t\t\tnext_attempt_at: nextAt ?? new Date(),\n\t\t\t\tstatus: isDead ? \"dead\" : \"pending\",\n\t\t\t\tfailed_at: isDead ? new Date() : null,\n\t\t\t\tlocked_by: null,\n\t\t\t\tlocked_until: null,\n\t\t\t})\n\t\t\t.where(\"id\", \"=\", outboxRow.id)\n\t\t\t.execute();\n\n\t\t// Atomic increment — concurrent failures must not clobber each other.\n\t\t// `RETURNING circuit_failures` gives us the post-increment value to\n\t\t// decide whether this failure tripped the circuit.\n\t\tconst incResult = await sql<{ circuit_failures: number }>`\n\t\t\tUPDATE subscriptions\n\t\t\tSET circuit_failures = circuit_failures + 1,\n\t\t\t\tlast_delivery_at = NOW(),\n\t\t\t\tlast_error = ${errText.slice(0, 500)},\n\t\t\t\tupdated_at = NOW()\n\t\t\tWHERE id = ${sub.id}\n\t\t\tRETURNING circuit_failures\n\t\t`.execute(tx);\n\t\tconst newFailures =\n\t\t\tincResult.rows[0]?.circuit_failures ?? sub.circuit_failures + 1;\n\t\tconst shouldTripCircuit = newFailures >= CIRCUIT_THRESHOLD;\n\n\t\tif (shouldTripCircuit) {\n\t\t\t// Transition to paused only on the first failure that crossed\n\t\t\t// the threshold — additional failures in-flight harmlessly\n\t\t\t// re-set the same fields.\n\t\t\tawait tx\n\t\t\t\t.updateTable(\"subscriptions\")\n\t\t\t\t.set({\n\t\t\t\t\tstatus: \"paused\",\n\t\t\t\t\tcircuit_opened_at: new Date(),\n\t\t\t\t\tupdated_at: new Date(),\n\t\t\t\t})\n\t\t\t\t.where(\"id\", \"=\", sub.id)\n\t\t\t\t.execute();\n\t\t\tlogger.warn(\n\t\t\t\t\"Subscription circuit tripped — paused after consecutive failures\",\n\t\t\t\t{\n\t\t\t\t\tsubscription: sub.name,\n\t\t\t\t\tfailures: newFailures,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t});\n}\n\nasync function claimAndDrain(\n\tdb: Kysely<Database>,\n\tstate: RunningState,\n\temitterId: string,\n): Promise<number> {\n\tif (state.claimInFlight) return 0;\n\tstate.claimInFlight = true;\n\ttry {\n\t\t// FOR UPDATE SKIP LOCKED — multiple emitters split the batch.\n\t\t// 90/10 live vs replay so a big replay doesn't starve live emits.\n\t\tconst liveLimit = Math.max(1, Math.round(BATCH_SIZE * LIVE_SHARE));\n\t\tconst replayLimit = BATCH_SIZE - liveLimit;\n\t\tconst claimed = await db.transaction().execute(async (tx) => {\n\t\t\tconst live = await sql<SubscriptionOutbox>`\n\t\t\t\t\tSELECT * FROM subscription_outbox\n\t\t\t\t\tWHERE status = 'pending'\n\t\t\t\t\t\tAND next_attempt_at <= NOW()\n\t\t\t\t\t\tAND is_replay = FALSE\n\t\t\t\t\tORDER BY next_attempt_at ASC\n\t\t\t\t\tFOR UPDATE SKIP LOCKED\n\t\t\t\t\tLIMIT ${sql.lit(liveLimit)}\n\t\t\t\t`.execute(tx);\n\t\t\tconst replay = await sql<SubscriptionOutbox>`\n\t\t\t\t\tSELECT * FROM subscription_outbox\n\t\t\t\t\tWHERE status = 'pending'\n\t\t\t\t\t\tAND next_attempt_at <= NOW()\n\t\t\t\t\t\tAND is_replay = TRUE\n\t\t\t\t\tORDER BY next_attempt_at ASC\n\t\t\t\t\tFOR UPDATE SKIP LOCKED\n\t\t\t\t\tLIMIT ${sql.lit(replayLimit)}\n\t\t\t\t`.execute(tx);\n\n\t\t\tconst combined = [...live.rows, ...replay.rows];\n\t\t\tif (combined.length === 0) return [];\n\n\t\t\t// Push `next_attempt_at` forward by the lock window. This is\n\t\t\t// the only defense against double-dispatch if the emitter\n\t\t\t// process crashes mid-HTTP-call: the row won't be re-claimable\n\t\t\t// until `LOCK_WINDOW_MS` elapses, giving us a stale-lock\n\t\t\t// recovery window. `settleDelivered`/`settleFailed` overrides\n\t\t\t// this on the success/failure path.\n\t\t\tconst now = new Date();\n\t\t\tconst lockUntil = new Date(now.getTime() + LOCK_WINDOW_MS);\n\t\t\tawait tx\n\t\t\t\t.updateTable(\"subscription_outbox\")\n\t\t\t\t.set({\n\t\t\t\t\tlocked_by: emitterId,\n\t\t\t\t\tlocked_until: lockUntil,\n\t\t\t\t\tnext_attempt_at: lockUntil,\n\t\t\t\t})\n\t\t\t\t.where(\n\t\t\t\t\t\"id\",\n\t\t\t\t\t\"in\",\n\t\t\t\t\tcombined.map((r) => r.id),\n\t\t\t\t)\n\t\t\t\t.execute();\n\t\t\treturn combined;\n\t\t});\n\n\t\tif (claimed.length === 0) return 0;\n\n\t\t// Hydrate each claimed row's sub once, then dispatch with per-sub\n\t\t// concurrency cap enforced via in-memory semaphore.\n\t\tconst bySubId = new Map<string, SubscriptionOutbox[]>();\n\t\tfor (const row of claimed) {\n\t\t\tconst arr = bySubId.get(row.subscription_id);\n\t\t\tif (arr) arr.push(row);\n\t\t\telse bySubId.set(row.subscription_id, [row]);\n\t\t}\n\n\t\tconst subIds = Array.from(bySubId.keys());\n\t\tconst subs = await db\n\t\t\t.selectFrom(\"subscriptions\")\n\t\t\t.selectAll()\n\t\t\t.where(\"id\", \"in\", subIds)\n\t\t\t.execute();\n\t\tconst subById = new Map(subs.map((s) => [s.id, s]));\n\n\t\tawait Promise.all(\n\t\t\tsubIds.map((subId) =>\n\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\t\t\t\tdrainForSub(db, state, subById.get(subId)!, bySubId.get(subId)!),\n\t\t\t),\n\t\t);\n\n\t\treturn claimed.length;\n\t} finally {\n\t\tstate.claimInFlight = false;\n\t}\n}\n\nasync function drainForSub(\n\tdb: Kysely<Database>,\n\tstate: RunningState,\n\tsub: Subscription,\n\trows: SubscriptionOutbox[],\n): Promise<void> {\n\tconst cap = sub.concurrency || 4;\n\tconst counter = () => state.inFlightBySub.get(sub.id) ?? 0;\n\tconst inc = () => state.inFlightBySub.set(sub.id, counter() + 1);\n\tconst dec = () => state.inFlightBySub.set(sub.id, Math.max(0, counter() - 1));\n\n\tconst queue = [...rows];\n\tconst workers: Promise<void>[] = [];\n\tconst slots = Math.min(cap, queue.length);\n\n\tfor (let i = 0; i < slots; i++) {\n\t\tworkers.push(\n\t\t\t(async () => {\n\t\t\t\twhile (state.running && queue.length > 0) {\n\t\t\t\t\tconst row = queue.shift();\n\t\t\t\t\tif (!row) break;\n\t\t\t\t\tinc();\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst result = await dispatchOne(db, row, sub);\n\t\t\t\t\t\tif (result.ok) {\n\t\t\t\t\t\t\tawait settleDelivered(db, row);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tconst err = result.error ?? `HTTP ${result.statusCode ?? \"?\"}`;\n\t\t\t\t\t\t\tawait settleFailed(db, row, sub, err);\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tlogger.error(\"Emitter dispatch crashed\", {\n\t\t\t\t\t\t\toutboxId: row.id,\n\t\t\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t});\n\t\t\t\t\t\tawait settleFailed(\n\t\t\t\t\t\t\tdb,\n\t\t\t\t\t\t\trow,\n\t\t\t\t\t\t\tsub,\n\t\t\t\t\t\t\terr instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t);\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tdec();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})(),\n\t\t);\n\t}\n\tawait Promise.all(workers);\n}\n\nexport interface StartEmitterOptions {\n\t/** Interval for the background poll (ms). Defaults to 2 minutes. */\n\tpollIntervalMs?: number;\n\t/** Retention sweep interval (ms). Defaults to 1 hour. */\n\tretentionIntervalMs?: number;\n}\n\nasync function runRetention(db: Kysely<Database>): Promise<void> {\n\t// delivered outbox >7d, deliveries >30d, dead outbox >90d\n\tawait sql`\n\t\tDELETE FROM subscription_outbox\n\t\tWHERE status = 'delivered' AND delivered_at < NOW() - interval '7 days'\n\t`.execute(db);\n\tawait sql`\n\t\tDELETE FROM subscription_deliveries\n\t\tWHERE dispatched_at < NOW() - interval '30 days'\n\t`.execute(db);\n\tawait sql`\n\t\tDELETE FROM subscription_outbox\n\t\tWHERE status = 'dead' AND failed_at < NOW() - interval '90 days'\n\t`.execute(db);\n}\n\nexport async function startEmitter(\n\topts?: StartEmitterOptions,\n): Promise<() => Promise<void>> {\n\tconst emitterId = `emitter-${Math.random().toString(36).slice(2, 10)}`;\n\tconst db = getTargetDb();\n\tconst state: RunningState = {\n\t\trunning: true,\n\t\tinFlightBySub: new Map(),\n\t\tclaimInFlight: false,\n\t};\n\tconst pollIntervalMs = opts?.pollIntervalMs ?? 120_000;\n\tconst retentionIntervalMs = opts?.retentionIntervalMs ?? 60 * 60_000;\n\n\tlogger.info(\"[emitter] started\", { id: emitterId });\n\n\t// Bootstrap matcher from active subs. Retry with backoff — if this\n\t// stays broken, fail loud rather than run with an empty matcher (which\n\t// would silently drop every block's outbox emissions until the next\n\t// subscription CRUD fired `subscriptions:changed`).\n\tconst MATCHER_BOOT_ATTEMPTS = 5;\n\tlet lastErr: unknown = null;\n\tfor (let i = 0; i < MATCHER_BOOT_ATTEMPTS; i++) {\n\t\ttry {\n\t\t\tawait refreshMatcher(db);\n\t\t\tlastErr = null;\n\t\t\tbreak;\n\t\t} catch (err) {\n\t\t\tlastErr = err;\n\t\t\tconst delayMs = 500 * 2 ** i; // 500ms, 1s, 2s, 4s, 8s\n\t\t\tlogger.warn(\"[emitter] matcher refresh failed, retrying\", {\n\t\t\t\tattempt: i + 1,\n\t\t\t\tdelayMs,\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t\tawait new Promise((r) => setTimeout(r, delayMs));\n\t\t}\n\t}\n\tif (lastErr) {\n\t\tthrow new Error(\n\t\t\t`[emitter] matcher refresh failed ${MATCHER_BOOT_ATTEMPTS}×; aborting boot: ${\n\t\t\t\tlastErr instanceof Error ? lastErr.message : String(lastErr)\n\t\t\t}`,\n\t\t);\n\t}\n\n\t// LISTEN on new outbox + sub changes. Both channels fire on the TARGET DB\n\t// (subscription_outbox + subscriptions are control-plane tables), so bind the\n\t// listener there — under the split it is NOT `DATABASE_URL`.\n\tconst listenUrl = targetListenerUrl();\n\tconst stopNew = await listen(\n\t\t\"subscriptions:new_outbox\",\n\t\t() => {\n\t\t\tif (!state.running) return;\n\t\t\tvoid claimAndDrain(db, state, emitterId).catch((err) =>\n\t\t\t\tlogger.error(\"[emitter] claim failed\", {\n\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t}),\n\t\t\t);\n\t\t},\n\t\t{ connectionString: listenUrl },\n\t);\n\tconst stopChanged = await listen(\n\t\t\"subscriptions:changed\",\n\t\t() => {\n\t\t\tif (!state.running) return;\n\t\t\tvoid refreshMatcher(db).catch((err) =>\n\t\t\t\tlogger.error(\"[emitter] matcher refresh failed\", {\n\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t}),\n\t\t\t);\n\t\t},\n\t\t{ connectionString: listenUrl },\n\t);\n\n\t// Poll every pollIntervalMs as a safety net for missed notifications +\n\t// backoff wakeups (rows whose next_attempt_at has passed).\n\tconst poll = setInterval(() => {\n\t\tif (!state.running) return;\n\t\tvoid claimAndDrain(db, state, emitterId).catch((err) =>\n\t\t\tlogger.error(\"[emitter] poll claim failed\", {\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t}),\n\t\t);\n\t}, pollIntervalMs);\n\n\t// Kick once on startup so any rows that arrived before we started drain.\n\tvoid claimAndDrain(db, state, emitterId);\n\n\t// Retention sweep — hourly by default.\n\tconst retention = setInterval(() => {\n\t\tif (!state.running) return;\n\t\tvoid runRetention(db).catch((err) =>\n\t\t\tlogger.error(\"[emitter] retention failed\", {\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t}),\n\t\t);\n\t}, retentionIntervalMs);\n\n\treturn async () => {\n\t\tstate.running = false;\n\t\tclearInterval(poll);\n\t\tclearInterval(retention);\n\t\tawait stopNew();\n\t\tawait stopChanged();\n\t\tlogger.info(\"[emitter] stopped\", { id: emitterId });\n\t};\n}\n",
6
+ "import { signSecondlayerWebhook } from \"@secondlayer/shared/crypto/secondlayer-webhook\";\nimport type { Subscription, SubscriptionOutbox } from \"@secondlayer/shared/db\";\nimport { logger } from \"@secondlayer/shared/logger\";\nimport { buildCloudEvents } from \"./cloudevents.ts\";\nimport { buildCloudflare } from \"./cloudflare.ts\";\nimport { buildInngest } from \"./inngest.ts\";\nimport { buildRaw } from \"./raw.ts\";\nimport { buildStandardWebhooks } from \"./standard-webhooks.ts\";\nimport { buildTrigger } from \"./trigger.ts\";\n\nexport interface FormatBuildResult {\n\tbody: string;\n\theaders: Record<string, string>;\n}\n\nfunction buildBody(\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n\tsigningSecret: string,\n): FormatBuildResult {\n\tswitch (sub.format) {\n\t\tcase \"inngest\":\n\t\t\treturn buildInngest(outboxRow);\n\t\tcase \"trigger\":\n\t\t\treturn buildTrigger(outboxRow, sub);\n\t\tcase \"cloudflare\":\n\t\t\treturn buildCloudflare(outboxRow, sub);\n\t\tcase \"cloudevents\":\n\t\t\treturn buildCloudEvents(outboxRow, sub);\n\t\tcase \"raw\":\n\t\t\treturn buildRaw(outboxRow, sub);\n\t\tcase \"standard-webhooks\":\n\t\t\treturn buildStandardWebhooks(outboxRow, signingSecret);\n\t\tdefault:\n\t\t\tlogger.warn(\n\t\t\t\t\"Unknown subscription format, falling back to standard-webhooks\",\n\t\t\t\t{\n\t\t\t\t\tformat: sub.format,\n\t\t\t\t\tsubscriptionId: sub.id,\n\t\t\t\t},\n\t\t\t);\n\t\t\treturn buildStandardWebhooks(outboxRow, signingSecret);\n\t}\n}\n\n/**\n * Dispatch an outbox row through the format matching the subscription's\n * `format` column. Unknown formats fall back to `standard-webhooks` with\n * a warning log — receivers always get something deliverable.\n *\n * Every delivery, whatever its body shape, also gets the universal Secondlayer\n * authenticity headers (`webhook-id` + `X-Secondlayer-Signature`) so a receiver\n * can prove the payload came from us with one published public key — not just\n * the `standard-webhooks` format. The signature covers the exact body bytes\n * built here, so it must be attached after the body is final.\n */\nexport function buildForFormat(\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n\tsigningSecret: string,\n): FormatBuildResult {\n\tconst result = buildBody(outboxRow, sub, signingSecret);\n\tconst sigHeaders = signSecondlayerWebhook(outboxRow.id, result.body);\n\tif (sigHeaders) {\n\t\tresult.headers = { ...result.headers, ...sigHeaders };\n\t}\n\treturn result;\n}\n\nexport {\n\tbuildStandardWebhooks,\n\tbuildInngest,\n\tbuildTrigger,\n\tbuildCloudflare,\n\tbuildCloudEvents,\n\tbuildRaw,\n};\n",
7
+ "import type { Subscription, SubscriptionOutbox } from \"@secondlayer/shared/db\";\n\n/**\n * CloudEvents 1.0 structured JSON — https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/json-format.md\n *\n * Body shape:\n * {\n * \"specversion\": \"1.0\",\n * \"type\": \"<subgraph>.<table>.<created|updated|deleted>\",\n * \"source\": \"secondlayer:<subgraph_name>\",\n * \"id\": <outbox.id>, // UUID, used for dedup\n * \"time\": <ISO 8601>,\n * \"datacontenttype\": \"application/json\",\n * \"data\": { ...row }\n * }\n *\n * Content-Type is `application/cloudevents+json; charset=utf-8`. Binary\n * mode (headers as ce-*) isn't needed — structured mode is what every CE\n * SDK accepts out of the box.\n */\n\nexport function buildCloudEvents(\n\toutboxRow: SubscriptionOutbox,\n\t_sub: Subscription,\n): { body: string; headers: Record<string, string> } {\n\tconst event = {\n\t\tspecversion: \"1.0\",\n\t\ttype: outboxRow.event_type,\n\t\tsource: `secondlayer:${outboxRow.subgraph_name}`,\n\t\tid: outboxRow.id,\n\t\ttime: new Date(outboxRow.created_at).toISOString(),\n\t\tdatacontenttype: \"application/json\",\n\t\tdata: outboxRow.payload,\n\t};\n\treturn {\n\t\tbody: JSON.stringify(event),\n\t\theaders: {\n\t\t\t\"content-type\": \"application/cloudevents+json; charset=utf-8\",\n\t\t},\n\t};\n}\n",
8
+ "import { decryptSecret } from \"@secondlayer/shared/crypto/secrets\";\nimport type { Subscription, SubscriptionOutbox } from \"@secondlayer/shared/db\";\n\n/**\n * Cloudflare Workflows — https://developers.cloudflare.com/workflows/build/events-and-parameters/\n *\n * POST https://api.cloudflare.com/client/v4/accounts/{account}/workflows/{name}/instances\n * Authorization: Bearer <CF_API_TOKEN>\n *\n * Body: `{ params }` — the workflow entrypoint receives this as the\n * `event.payload` object. We slip the outbox id into `params._subscriptionId`\n * so Workflows can dedupe on replays.\n */\n\nfunction resolveBearer(sub: Subscription): string | null {\n\tconst cfg = sub.auth_config as { token?: string; tokenEnc?: string };\n\tif (cfg.tokenEnc) {\n\t\t// Let decrypt errors propagate — see trigger.ts comment.\n\t\treturn decryptSecret(Buffer.from(cfg.tokenEnc, \"base64\"));\n\t}\n\treturn cfg.token ?? null;\n}\n\nexport function buildCloudflare(\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n): { body: string; headers: Record<string, string> } {\n\tconst body = JSON.stringify({\n\t\tparams: {\n\t\t\t...(outboxRow.payload as Record<string, unknown>),\n\t\t\t_type: outboxRow.event_type,\n\t\t\t_outboxId: outboxRow.id,\n\t\t},\n\t});\n\tconst headers: Record<string, string> = {\n\t\t\"content-type\": \"application/json\",\n\t};\n\tconst token = resolveBearer(sub);\n\tif (token) headers.authorization = `Bearer ${token}`;\n\treturn { body, headers };\n}\n",
9
+ "import type { SubscriptionOutbox } from \"@secondlayer/shared/db\";\n\n/**\n * Inngest event format — https://www.inngest.com/docs/events/sending-events\n *\n * POST body is a JSON array of one or more events. The Inngest event key\n * goes in the URL path (`https://inn.gs/e/{EVENT_KEY}`), so the user sets\n * that on `subscription.url` directly — no auth header needed.\n *\n * Body per event:\n * - `name` — event name `<subgraph>.<table>.<created|updated|deleted>`\n * so it matches Inngest fn `event` triggers out of the box\n * - `data` — row payload\n * - `id` — dedupes on Inngest's side (mirrors outbox id)\n * - `ts` — unix millis when the event occurred\n * - `v` — version tag, fixed at `\"2026-04-23.v1\"` for forward compat\n */\n\nexport const INNGEST_VERSION = \"2026-04-23.v1\";\n\nexport function buildInngest(outboxRow: SubscriptionOutbox): {\n\tbody: string;\n\theaders: Record<string, string>;\n} {\n\tconst event = {\n\t\tname: outboxRow.event_type,\n\t\tdata: outboxRow.payload,\n\t\tid: outboxRow.id,\n\t\tts: new Date(outboxRow.created_at).getTime(),\n\t\tv: INNGEST_VERSION,\n\t};\n\treturn {\n\t\tbody: JSON.stringify([event]),\n\t\theaders: {\n\t\t\t\"content-type\": \"application/json\",\n\t\t},\n\t};\n}\n",
10
+ "import type { Subscription, SubscriptionOutbox } from \"@secondlayer/shared/db\";\n\n/**\n * Raw JSON — the row payload, nothing else. User controls Content-Type +\n * arbitrary headers via `sub.auth_config.headers`. Useful for targets that\n * already have their own wire contract (e.g. Zapier hooks, internal\n * services, Slack webhooks with raw-shaped bodies).\n *\n * Special-case: if `auth_config.authType === \"basic\"` and `basicAuth` is\n * set (`user:pass` base64 or plain user/pass object), emit a Basic header.\n * If `authType === \"bearer\"` with `token`/`tokenEnc`, emit a Bearer header.\n * Otherwise no auth is set — user URLs often include a token in the query\n * string (Slack) or an HMAC at the receiver (Zapier).\n */\n\nexport function buildRaw(\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n): { body: string; headers: Record<string, string> } {\n\tconst cfg = sub.auth_config as {\n\t\tcontentType?: string;\n\t\theaders?: Record<string, string>;\n\t\tauthType?: \"bearer\" | \"basic\" | \"none\";\n\t\ttoken?: string;\n\t\tbasicAuth?: string;\n\t};\n\tconst headers: Record<string, string> = {\n\t\t\"content-type\": cfg.contentType ?? \"application/json\",\n\t\t...(cfg.headers ?? {}),\n\t};\n\tif (cfg.authType === \"bearer\" && cfg.token) {\n\t\theaders.authorization = `Bearer ${cfg.token}`;\n\t} else if (cfg.authType === \"basic\" && cfg.basicAuth) {\n\t\theaders.authorization = `Basic ${cfg.basicAuth}`;\n\t}\n\treturn {\n\t\tbody: JSON.stringify(outboxRow.payload),\n\t\theaders,\n\t};\n}\n",
11
+ "import { sign } from \"@secondlayer/shared/crypto/standard-webhooks\";\nimport type { SubscriptionOutbox } from \"@secondlayer/shared/db\";\n\n/**\n * Build a Standard Webhooks POST body + headers for an outbox row.\n * https://standardwebhooks.com\n *\n * Body shape: `{ type, timestamp, data }`\n * - `type` — `<subgraph>.<table>.<verb>` where verb is `created`, `updated`,\n * or `deleted`, mapped from the flush op so a receiver sees the full row\n * lifecycle, not just the initial insert.\n * - `timestamp` — ISO 8601 at DISPATCH time (not row creation). Receivers\n * verify `webhook-timestamp` against a tolerance window (default 300s per\n * Svix); stamping at creation would fail verification on every retry\n * beyond the first, since retries fire up to 72h after the row was\n * written. The `webhook-id` header stays stable across retries for\n * receiver dedup.\n * - `data` — row payload (bigints already stringified by flush manifest).\n *\n * Headers: the three standardwebhooks.com headers + Content-Type.\n */\n\nexport interface StandardWebhooksPayload {\n\ttype: string;\n\ttimestamp: string;\n\tdata: Record<string, unknown>;\n}\n\nexport function buildStandardWebhooks(\n\toutboxRow: SubscriptionOutbox,\n\tsigningSecret: string,\n): { body: string; headers: Record<string, string> } {\n\tconst nowSeconds = Math.floor(Date.now() / 1000);\n\tconst payload: StandardWebhooksPayload = {\n\t\ttype: outboxRow.event_type,\n\t\ttimestamp: new Date(nowSeconds * 1000).toISOString(),\n\t\tdata: outboxRow.payload as Record<string, unknown>,\n\t};\n\tconst body = JSON.stringify(payload);\n\tconst sigHeaders = sign(body, signingSecret, {\n\t\tid: outboxRow.id,\n\t\ttimestampSeconds: nowSeconds,\n\t});\n\treturn {\n\t\tbody,\n\t\theaders: {\n\t\t\t\"content-type\": \"application/json\",\n\t\t\t...sigHeaders,\n\t\t},\n\t};\n}\n",
12
+ "import { decryptSecret } from \"@secondlayer/shared/crypto/secrets\";\nimport type { Subscription, SubscriptionOutbox } from \"@secondlayer/shared/db\";\n\n/**\n * Trigger.dev v3 task trigger — https://trigger.dev/docs/tasks/overview\n *\n * POST https://api.trigger.dev/api/v1/tasks/{taskId}/trigger\n * Authorization: Bearer <TRIGGER_SECRET_KEY>\n *\n * Body:\n * - `payload` — the row payload\n * - `options` — currently `{ idempotencyKey }` so Trigger dedupes replays\n *\n * The Trigger secret lives in `sub.auth_config.tokenEnc` (encrypted by\n * `crypto/secrets`) or `sub.auth_config.token` (plaintext, dev only).\n */\n\nfunction resolveBearer(sub: Subscription): string | null {\n\tconst cfg = sub.auth_config as {\n\t\tauthType?: string;\n\t\ttoken?: string;\n\t\ttokenEnc?: string;\n\t};\n\tif (cfg.tokenEnc) {\n\t\t// Let decrypt errors propagate — silently sending unauthenticated\n\t\t// requests produces downstream 401s with no hint the secret failed\n\t\t// to decrypt. Surface as a dispatch-time error so the delivery log\n\t\t// records the real cause.\n\t\treturn decryptSecret(Buffer.from(cfg.tokenEnc, \"base64\"));\n\t}\n\treturn cfg.token ?? null;\n}\n\nexport function buildTrigger(\n\toutboxRow: SubscriptionOutbox,\n\tsub: Subscription,\n): { body: string; headers: Record<string, string> } {\n\tconst body = JSON.stringify({\n\t\tpayload: outboxRow.payload,\n\t\toptions: {\n\t\t\tidempotencyKey: outboxRow.id,\n\t\t},\n\t});\n\tconst headers: Record<string, string> = {\n\t\t\"content-type\": \"application/json\",\n\t};\n\tconst token = resolveBearer(sub);\n\tif (token) headers.authorization = `Bearer ${token}`;\n\treturn { body, headers };\n}\n",
13
+ "import type { Database, Subscription } from \"@secondlayer/shared/db\";\nimport { listSubscriptions } from \"@secondlayer/shared/db/queries/subscriptions\";\nimport type { Kysely } from \"kysely\";\nimport { sql } from \"kysely\";\nimport { SubscriptionMatcher } from \"./emitter-matcher.ts\";\n\n/**\n * Singleton matcher populated at processor startup and hot-reloaded via\n * `pg_notify('subscriptions:changed')`. The block-processor reads from it\n * to decide which outbox rows to emit for each flushed write.\n *\n * Per-account listing: in oss/dedicated mode the tenant DB holds all subs\n * for the single account; the matcher loads every row. In platform mode\n * the emitter doesn't run at all (control plane only), so this module is\n * dedicated/oss-only.\n */\n\nexport const matcher = new SubscriptionMatcher();\n\nexport async function refreshMatcher(db: Kysely<Database>): Promise<number> {\n\t// listSubscriptions is account-scoped; the emitter wants every active\n\t// sub so we do a raw query.\n\tconst rows = await sql<Subscription>`\n\t\tSELECT * FROM subscriptions WHERE status = 'active'\n\t`.execute(db);\n\tmatcher.setAll(rows.rows);\n\treturn matcher.size();\n}\n\n// Per-account helper used by tests so the DATABASE_URL-based code path is\n// exercised through listSubscriptions (keeps the query helper in the\n// integration surface).\nexport async function refreshMatcherForAccount(\n\tdb: Kysely<Database>,\n\taccountId: string,\n): Promise<number> {\n\tconst rows = await listSubscriptions(db, accountId);\n\tmatcher.setAll(rows.filter((r: Subscription) => r.status === \"active\"));\n\treturn matcher.size();\n}\n",
14
+ "import type { Subscription } from \"@secondlayer/shared/db\";\n\n/**\n * Subscription matcher — holds the active-subscriptions cache keyed by\n * `(subgraph_name, table_name)` and evaluates each sub's scalar-only\n * JSONB filter against a row.\n *\n * Filter DSL (v1): scalar operators over string/number/boolean columns.\n * { column: value } → column === value\n * { column: { eq: value } } → column === value\n * { column: { neq: value } } → column !== value\n * { column: { gt|gte|lt|lte: N } } → numeric compare\n * { column: { in: [a, b, c] } } → column ∈ set\n * { } → match all\n *\n * Nested objects, arrays, and non-scalar values are rejected with `false`\n * (never match) — subscribers get logged warnings at sub create time.\n *\n * Multiple conditions AND together. OR is deliberately absent in v1.\n */\n\nexport type FilterPrimitive = string | number | boolean;\nexport type FilterOperator =\n\t| { eq: FilterPrimitive }\n\t| { neq: FilterPrimitive }\n\t| { gt: number | string }\n\t| { gte: number | string }\n\t| { lt: number | string }\n\t| { lte: number | string }\n\t| { in: FilterPrimitive[] };\n\nexport type FilterClause = FilterPrimitive | FilterOperator;\nexport type SubscriptionFilter = Record<string, FilterClause>;\n\nfunction isPrimitive(v: unknown): v is FilterPrimitive {\n\tconst t = typeof v;\n\treturn t === \"string\" || t === \"number\" || t === \"boolean\";\n}\n\n/**\n * Coerce a value to BigInt for arbitrary-precision compare — avoids the\n * silent precision loss of `Number()` on integers past 2^53. Returns\n * `null` for non-integer numerics, which is fine because the filter DSL\n * is documented as scalar-only + integer-amount-aware (Stacks uses\n * uint128 bigint columns everywhere).\n */\nfunction coerceBigInt(v: unknown): bigint | null {\n\tif (typeof v === \"bigint\") return v;\n\tif (typeof v === \"number\") {\n\t\tif (!Number.isFinite(v)) return null;\n\t\tif (!Number.isInteger(v)) return null;\n\t\treturn BigInt(v);\n\t}\n\tif (typeof v === \"string\" && /^-?\\d+$/.test(v)) {\n\t\ttry {\n\t\t\treturn BigInt(v);\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * Float compare fallback — used when a value isn't a clean integer\n * (rare in subgraph columns but defensible for user-supplied filters).\n */\nfunction coerceFloat(v: unknown): number | null {\n\tif (typeof v === \"number\" && Number.isFinite(v)) return v;\n\tif (typeof v === \"bigint\") return Number(v);\n\tif (typeof v === \"string\" && v !== \"\" && !Number.isNaN(Number(v))) {\n\t\treturn Number(v);\n\t}\n\treturn null;\n}\n\nfunction compareNumeric(a: unknown, b: unknown): 0 | 1 | -1 | null {\n\tconst ba = coerceBigInt(a);\n\tconst bb = coerceBigInt(b);\n\tif (ba !== null && bb !== null) {\n\t\tif (ba === bb) return 0;\n\t\treturn ba > bb ? 1 : -1;\n\t}\n\tconst fa = coerceFloat(a);\n\tconst fb = coerceFloat(b);\n\tif (fa === null || fb === null) return null;\n\tif (fa === fb) return 0;\n\treturn fa > fb ? 1 : -1;\n}\n\nfunction matchClause(rowValue: unknown, clause: FilterClause): boolean {\n\t// Reject non-primitive row values — `{ value: {...} }` should not\n\t// match `filter: { value: \"[object Object]\" }` via loose stringify.\n\t// Bigints are not primitives in the JSON sense but are supported\n\t// scalar inputs from subgraph rows.\n\tconst rowIsPrimitive = isPrimitive(rowValue) || typeof rowValue === \"bigint\";\n\n\tif (isPrimitive(clause)) {\n\t\tif (!rowIsPrimitive) return false;\n\t\tif (\n\t\t\ttypeof clause === \"number\" ||\n\t\t\ttypeof rowValue === \"number\" ||\n\t\t\ttypeof rowValue === \"bigint\"\n\t\t) {\n\t\t\tconst cmp = compareNumeric(rowValue, clause);\n\t\t\tif (cmp !== null) return cmp === 0;\n\t\t}\n\t\treturn rowValue === clause || String(rowValue) === String(clause);\n\t}\n\n\tif (clause === null || typeof clause !== \"object\" || Array.isArray(clause)) {\n\t\treturn false;\n\t}\n\n\tconst keys = Object.keys(clause);\n\tif (keys.length !== 1) return false;\n\tconst op = keys[0];\n\n\tconst c = clause as Record<string, unknown>;\n\tswitch (op) {\n\t\tcase \"eq\":\n\t\t\treturn matchClause(rowValue, c.eq as FilterPrimitive);\n\t\tcase \"neq\":\n\t\t\treturn !matchClause(rowValue, c.neq as FilterPrimitive);\n\t\tcase \"gt\":\n\t\tcase \"gte\":\n\t\tcase \"lt\":\n\t\tcase \"lte\": {\n\t\t\tif (!rowIsPrimitive) return false;\n\t\t\tconst cmp = compareNumeric(rowValue, c[op]);\n\t\t\tif (cmp === null) return false;\n\t\t\tif (op === \"gt\") return cmp > 0;\n\t\t\tif (op === \"gte\") return cmp >= 0;\n\t\t\tif (op === \"lt\") return cmp < 0;\n\t\t\treturn cmp <= 0;\n\t\t}\n\t\tcase \"in\": {\n\t\t\tconst list = c.in;\n\t\t\tif (!Array.isArray(list)) return false;\n\t\t\tif (!rowIsPrimitive) return false;\n\t\t\treturn list.some(\n\t\t\t\t(item) => isPrimitive(item) && matchClause(rowValue, item),\n\t\t\t);\n\t\t}\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nexport function matchesFilter(\n\tfilter: SubscriptionFilter | null | undefined,\n\trow: Record<string, unknown>,\n): boolean {\n\tif (!filter || Object.keys(filter).length === 0) return true;\n\tfor (const [col, clause] of Object.entries(filter)) {\n\t\tif (!matchClause(row[col], clause)) return false;\n\t}\n\treturn true;\n}\n\n// ── Cache keyed by (subgraph_name, table_name) ──────────────────────────\n\ntype MatcherKey = string;\n\nfunction key(subgraphName: string, tableName: string): MatcherKey {\n\treturn `${subgraphName}\u0000${tableName}`;\n}\n\nexport class SubscriptionMatcher {\n\tprivate readonly byKey = new Map<MatcherKey, Subscription[]>();\n\tprivate readonly byId = new Map<string, Subscription>();\n\n\t/** Replace the entire cache with a fresh snapshot. */\n\tsetAll(subs: Subscription[]): void {\n\t\tthis.byKey.clear();\n\t\tthis.byId.clear();\n\t\tfor (const sub of subs) {\n\t\t\tif (sub.status !== \"active\") continue;\n\t\t\t// This matcher keys on (subgraph, table) row writes; chain\n\t\t\t// subscriptions react to raw chain events and never flow through it.\n\t\t\tif (sub.kind !== \"subgraph\" || !sub.subgraph_name || !sub.table_name)\n\t\t\t\tcontinue;\n\t\t\tthis.byId.set(sub.id, sub);\n\t\t\tconst k = key(sub.subgraph_name, sub.table_name);\n\t\t\tconst arr = this.byKey.get(k);\n\t\t\tif (arr) arr.push(sub);\n\t\t\telse this.byKey.set(k, [sub]);\n\t\t}\n\t}\n\n\t/** Returns active subs for a (subgraph, table) whose filter matches the row. */\n\tmatch(\n\t\tsubgraphName: string,\n\t\ttableName: string,\n\t\trow: Record<string, unknown>,\n\t): Subscription[] {\n\t\tconst bucket = this.byKey.get(key(subgraphName, tableName));\n\t\tif (!bucket) return [];\n\t\tconst hits: Subscription[] = [];\n\t\tfor (const sub of bucket) {\n\t\t\tif (matchesFilter(sub.filter as SubscriptionFilter, row)) hits.push(sub);\n\t\t}\n\t\treturn hits;\n\t}\n\n\thas(subgraphName: string, tableName: string): boolean {\n\t\treturn this.byKey.has(key(subgraphName, tableName));\n\t}\n\n\tsize(): number {\n\t\treturn this.byId.size;\n\t}\n\n\tget(id: string): Subscription | undefined {\n\t\treturn this.byId.get(id);\n\t}\n}\n"
15
+ ],
16
+ "mappings": ";;;;AAAA;AACA;AAAA;AAAA;AAMA;AACA,mBAAS;AAET;AACA,gBAAsB;;;ACXtB;AAEA;;;ACmBO,SAAS,gBAAgB,CAC/B,WACA,MACoD;AAAA,EACpD,MAAM,QAAQ;AAAA,IACb,aAAa;AAAA,IACb,MAAM,UAAU;AAAA,IAChB,QAAQ,eAAe,UAAU;AAAA,IACjC,IAAI,UAAU;AAAA,IACd,MAAM,IAAI,KAAK,UAAU,UAAU,EAAE,YAAY;AAAA,IACjD,iBAAiB;AAAA,IACjB,MAAM,UAAU;AAAA,EACjB;AAAA,EACA,OAAO;AAAA,IACN,MAAM,KAAK,UAAU,KAAK;AAAA,IAC1B,SAAS;AAAA,MACR,gBAAgB;AAAA,IACjB;AAAA,EACD;AAAA;;;ACvCD;AAcA,SAAS,aAAa,CAAC,KAAkC;AAAA,EACxD,MAAM,MAAM,IAAI;AAAA,EAChB,IAAI,IAAI,UAAU;AAAA,IAEjB,OAAO,cAAc,OAAO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MAAM,YAAY;AAAA,IAClB,QAAO,KAAK,qBAAqB,EAAE,IAAI,UAAU,CAAC;AAAA;AAAA;",
17
+ "debugId": "1CD71540D1B0DBC964756E2164756E21",
18
+ "names": []
19
+ }
@@ -1217,6 +1217,37 @@ class PublicApiBlockSource {
1217
1217
  return map;
1218
1218
  }
1219
1219
  }
1220
+
1221
+ class FallbackBlockSource {
1222
+ primary;
1223
+ fallback;
1224
+ constructor(primary, fallback) {
1225
+ this.primary = primary;
1226
+ this.fallback = fallback;
1227
+ }
1228
+ async getTip() {
1229
+ try {
1230
+ return await this.primary.getTip();
1231
+ } catch (err) {
1232
+ logger3.warn("block source primary getTip failed — using DB tap", {
1233
+ error: err instanceof Error ? err.message : String(err)
1234
+ });
1235
+ return this.fallback.getTip();
1236
+ }
1237
+ }
1238
+ async loadBlockRange(fromHeight, toHeight) {
1239
+ try {
1240
+ return await this.primary.loadBlockRange(fromHeight, toHeight);
1241
+ } catch (err) {
1242
+ logger3.warn("block source primary loadBlockRange failed — using DB tap", {
1243
+ from: fromHeight,
1244
+ to: toHeight,
1245
+ error: err instanceof Error ? err.message : String(err)
1246
+ });
1247
+ return this.fallback.loadBlockRange(fromHeight, toHeight);
1248
+ }
1249
+ }
1250
+ }
1220
1251
  var postgresBlockSource = new PostgresBlockSource;
1221
1252
  function buildHttpClient() {
1222
1253
  const baseUrl = process.env.SUBGRAPH_INDEX_API_URL ?? process.env.STREAMS_API_URL ?? "http://api:3800";
@@ -1228,7 +1259,7 @@ function buildHttpClient() {
1228
1259
  }
1229
1260
  function resolveBlockSource(subgraph) {
1230
1261
  if (process.env.SUBGRAPH_SOURCE === "streams-index" && subgraph && isStreamsIndexEligible(subgraph)) {
1231
- return new PublicApiBlockSource(buildHttpClient(), referencedIndexEventTypes(subgraph));
1262
+ return new FallbackBlockSource(new PublicApiBlockSource(buildHttpClient(), referencedIndexEventTypes(subgraph)), postgresBlockSource);
1232
1263
  }
1233
1264
  if (process.env.SUBGRAPH_SOURCE === "streams-index" && subgraph) {
1234
1265
  logger3.debug("Subgraph not streams-index eligible, using DB tap", {
@@ -1463,6 +1494,13 @@ class SubscriptionMatcher {
1463
1494
 
1464
1495
  // src/runtime/subscription-state.ts
1465
1496
  var matcher = new SubscriptionMatcher;
1497
+ async function refreshMatcher(db) {
1498
+ const rows = await sql2`
1499
+ SELECT * FROM subscriptions WHERE status = 'active'
1500
+ `.execute(db);
1501
+ matcher.setAll(rows.rows);
1502
+ return matcher.size();
1503
+ }
1466
1504
 
1467
1505
  // src/runtime/block-processor.ts
1468
1506
  var routeCache = new Map;
@@ -1730,6 +1768,65 @@ function escapeLiteralDefault(value) {
1730
1768
  return value ? "TRUE" : "FALSE";
1731
1769
  return `'${String(value).replace(/'/g, "''")}'`;
1732
1770
  }
1771
+ function tableNeedsTrgm(tableDef) {
1772
+ return Object.values(tableDef.columns).some((col) => col.search);
1773
+ }
1774
+ function emitTableDDL(schemaName, tableName, tableDef) {
1775
+ const qualifiedName = `${schemaName}.${tableName}`;
1776
+ const statements = [];
1777
+ const columnDefs = [
1778
+ "_id BIGSERIAL PRIMARY KEY",
1779
+ "_block_height BIGINT NOT NULL",
1780
+ "_tx_id TEXT NOT NULL",
1781
+ "_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()"
1782
+ ];
1783
+ for (const [colName, col] of Object.entries(tableDef.columns)) {
1784
+ const sqlType = TYPE_MAP[col.type];
1785
+ const nullable = col.nullable ? "" : " NOT NULL";
1786
+ let colDef = `${colName} ${sqlType}${nullable}`;
1787
+ if (col.default !== undefined) {
1788
+ colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
1789
+ }
1790
+ columnDefs.push(colDef);
1791
+ }
1792
+ statements.push(`CREATE TABLE IF NOT EXISTS ${qualifiedName} (
1793
+ ${columnDefs.join(`,
1794
+ `)}
1795
+ )`);
1796
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_block_height ON ${qualifiedName} (_block_height)`);
1797
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_tx_id ON ${qualifiedName} (_tx_id)`);
1798
+ for (const [colName, col] of Object.entries(tableDef.columns)) {
1799
+ if (col.indexed) {
1800
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`);
1801
+ }
1802
+ }
1803
+ for (const [colName, col] of Object.entries(tableDef.columns)) {
1804
+ if (col.search) {
1805
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`);
1806
+ }
1807
+ }
1808
+ if (tableDef.indexes) {
1809
+ for (let i = 0;i < tableDef.indexes.length; i++) {
1810
+ const cols = tableDef.indexes[i];
1811
+ const idxName = `idx_${schemaName}_${tableName}_composite_${i}`;
1812
+ statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.join(", ")})`);
1813
+ }
1814
+ }
1815
+ if (tableDef.uniqueKeys) {
1816
+ for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
1817
+ const cols = tableDef.uniqueKeys[i];
1818
+ const constraintName = `uq_${schemaName}_${tableName}_${cols.join("_")}`;
1819
+ statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.join(", ")})`);
1820
+ }
1821
+ }
1822
+ return statements;
1823
+ }
1824
+ function emitForeignKeyDDL(schemaName, tableName, tableDef) {
1825
+ return (tableDef.relations ?? []).map((rel) => {
1826
+ const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
1827
+ return `ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`;
1828
+ });
1829
+ }
1733
1830
  function generateSubgraphSQL(def, schemaNameOverride) {
1734
1831
  const schemaName = schemaNameOverride ?? pgSchemaName(def.name);
1735
1832
  const statements = [];
@@ -1739,58 +1836,10 @@ function generateSubgraphSQL(def, schemaNameOverride) {
1739
1836
  }
1740
1837
  statements.push(`CREATE SCHEMA IF NOT EXISTS ${schemaName}`);
1741
1838
  for (const [tableName, tableDef] of Object.entries(def.schema)) {
1742
- const qualifiedName = `${schemaName}.${tableName}`;
1743
- const columnDefs = [
1744
- "_id BIGSERIAL PRIMARY KEY",
1745
- "_block_height BIGINT NOT NULL",
1746
- "_tx_id TEXT NOT NULL",
1747
- "_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()"
1748
- ];
1749
- for (const [colName, col] of Object.entries(tableDef.columns)) {
1750
- const sqlType = TYPE_MAP[col.type];
1751
- const nullable = col.nullable ? "" : " NOT NULL";
1752
- let colDef = `${colName} ${sqlType}${nullable}`;
1753
- if (col.default !== undefined) {
1754
- colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
1755
- }
1756
- columnDefs.push(colDef);
1757
- }
1758
- statements.push(`CREATE TABLE IF NOT EXISTS ${qualifiedName} (
1759
- ${columnDefs.join(`,
1760
- `)}
1761
- )`);
1762
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_block_height ON ${qualifiedName} (_block_height)`);
1763
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_tx_id ON ${qualifiedName} (_tx_id)`);
1764
- for (const [colName, col] of Object.entries(tableDef.columns)) {
1765
- if (col.indexed) {
1766
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`);
1767
- }
1768
- }
1769
- for (const [colName, col] of Object.entries(tableDef.columns)) {
1770
- if (col.search) {
1771
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`);
1772
- }
1773
- }
1774
- if (tableDef.indexes) {
1775
- for (let i = 0;i < tableDef.indexes.length; i++) {
1776
- const cols = tableDef.indexes[i];
1777
- const idxName = `idx_${schemaName}_${tableName}_composite_${i}`;
1778
- statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.join(", ")})`);
1779
- }
1780
- }
1781
- if (tableDef.uniqueKeys) {
1782
- for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
1783
- const cols = tableDef.uniqueKeys[i];
1784
- const constraintName = `uq_${schemaName}_${tableName}_${cols.join("_")}`;
1785
- statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.join(", ")})`);
1786
- }
1787
- }
1839
+ statements.push(...emitTableDDL(schemaName, tableName, tableDef));
1788
1840
  }
1789
1841
  for (const [tableName, tableDef] of Object.entries(def.schema)) {
1790
- for (const rel of tableDef.relations ?? []) {
1791
- const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
1792
- statements.push(`ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`);
1793
- }
1842
+ statements.push(...emitForeignKeyDDL(schemaName, tableName, tableDef));
1794
1843
  }
1795
1844
  const hashInput = JSON.stringify({
1796
1845
  name: def.name,
@@ -2921,5 +2970,5 @@ export {
2921
2970
  startSubgraphOperationRunner
2922
2971
  };
2923
2972
 
2924
- //# debugId=7833FCB6CB2ECC5D64756E2164756E21
2973
+ //# debugId=663C20C581D3E38564756E2164756E21
2925
2974
  //# sourceMappingURL=processor.js.map