@hogsend/engine 0.61.0 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -11
- package/src/env.ts +28 -0
- package/src/lib/auth.ts +26 -0
- package/src/routes/events/index.ts +29 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hogsend/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.62.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -53,18 +53,18 @@
|
|
|
53
53
|
"svix": "^1.95.1",
|
|
54
54
|
"winston": "^3.19.0",
|
|
55
55
|
"zod": "^4.4.3",
|
|
56
|
-
"@hogsend/attribution": "^0.
|
|
57
|
-
"@hogsend/core": "^0.
|
|
58
|
-
"@hogsend/db": "^0.
|
|
59
|
-
"@hogsend/email": "^0.
|
|
60
|
-
"@hogsend/plugin-posthog": "^0.
|
|
61
|
-
"@hogsend/plugin-resend": "^0.
|
|
62
|
-
"@hogsend/sms": "^0.
|
|
56
|
+
"@hogsend/attribution": "^0.62.0",
|
|
57
|
+
"@hogsend/core": "^0.62.0",
|
|
58
|
+
"@hogsend/db": "^0.62.0",
|
|
59
|
+
"@hogsend/email": "^0.62.0",
|
|
60
|
+
"@hogsend/plugin-posthog": "^0.62.0",
|
|
61
|
+
"@hogsend/plugin-resend": "^0.62.0",
|
|
62
|
+
"@hogsend/sms": "^0.62.0"
|
|
63
63
|
},
|
|
64
64
|
"optionalDependencies": {
|
|
65
|
-
"@hogsend/plugin-apollo": "^0.
|
|
66
|
-
"@hogsend/plugin-postmark": "^0.
|
|
67
|
-
"@hogsend/plugin-twilio": "^0.
|
|
65
|
+
"@hogsend/plugin-apollo": "^0.62.0",
|
|
66
|
+
"@hogsend/plugin-postmark": "^0.62.0",
|
|
67
|
+
"@hogsend/plugin-twilio": "^0.62.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@types/node": "^22.15.3",
|
package/src/env.ts
CHANGED
|
@@ -28,6 +28,15 @@ export const env = createEnv({
|
|
|
28
28
|
.enum(["error", "warn", "info", "http", "debug"])
|
|
29
29
|
.default("info"),
|
|
30
30
|
DATABASE_URL: z.string().min(1),
|
|
31
|
+
// Connection-pool size for `createDatabase` (`@hogsend/db`). Read RAW off
|
|
32
|
+
// process.env there (packages/db has no dependency on this module);
|
|
33
|
+
// declared here so it is part of the validated env contract — the
|
|
34
|
+
// OUTBOUND_WEBHOOK_* stance. Deliberately a loose string: `packages/db`
|
|
35
|
+
// owns the parse (integer 1-50, anything else warns once and falls back to
|
|
36
|
+
// 10) and must NEVER crash a boot over a bad value. Hogsend Cloud sets this
|
|
37
|
+
// low (e.g. 3) for tenant stacks sharing one Postgres cell — the DSN's own
|
|
38
|
+
// `?max=` param is silently ignored by postgres.js.
|
|
39
|
+
DATABASE_POOL_MAX: z.string().optional(),
|
|
31
40
|
REDIS_URL: z.string().min(1).default("redis://localhost:6379"),
|
|
32
41
|
BETTER_AUTH_SECRET: z.string().min(1),
|
|
33
42
|
BETTER_AUTH_URL: z.string().url().default("http://localhost:3002"),
|
|
@@ -264,6 +273,25 @@ export const env = createEnv({
|
|
|
264
273
|
RESEND_WEBHOOK_SECRET: z.string().min(1).optional(),
|
|
265
274
|
ADMIN_API_KEY: z.string().min(1).optional(),
|
|
266
275
|
API_PUBLIC_URL: z.string().url().default("http://localhost:3002"),
|
|
276
|
+
/**
|
|
277
|
+
* The Hogsend Cloud metering kill-switch. When "true", the PUBLIC event
|
|
278
|
+
* ingest surface (`POST /v1/events`) refuses new events with a 429
|
|
279
|
+
* `{ error: "ingest_suspended" }` BEFORE any DB write or Hatchet push —
|
|
280
|
+
* the soft-block the control plane flips (via the substrate's setEnv +
|
|
281
|
+
* redeploy) when a tenant breaches its plan limit or lands in a billing
|
|
282
|
+
* hold, and un-sets on the new period / upgrade.
|
|
283
|
+
*
|
|
284
|
+
* Deliberately scoped to that one surface. Provider webhooks (email/SMS
|
|
285
|
+
* status), the tracking endpoints (`/v1/t/*`, `/l/:slug`, `/s/:code`),
|
|
286
|
+
* unsubscribe/preferences and health are UNAFFECTED: suspending ingest
|
|
287
|
+
* must not lose delivery telemetry or break compliance surfaces. Events
|
|
288
|
+
* already accepted keep flowing through journeys.
|
|
289
|
+
*
|
|
290
|
+
* An enum, NOT z.coerce.boolean — an explicit "false" must actually
|
|
291
|
+
* disable (coerce treats the string "false" as true; SMS_LINK_TRACKING /
|
|
292
|
+
* HOGSEND_TEST_MODE precedent). Unset ⇒ "false" ⇒ behavior unchanged.
|
|
293
|
+
*/
|
|
294
|
+
HOGSEND_INGEST_SUSPENDED: z.enum(["true", "false"]).default("false"),
|
|
267
295
|
ENABLED_JOURNEYS: z.string().default("*"),
|
|
268
296
|
// Buckets: same `"*"`-or-csv contract as ENABLED_JOURNEYS (Section 9.3).
|
|
269
297
|
// Evaluated at worker boot — a toggle requires a worker restart; only the
|
package/src/lib/auth.ts
CHANGED
|
@@ -90,6 +90,32 @@ export function createAuth(opts: {
|
|
|
90
90
|
// "hogsend" → prod cookie `__Secure-hogsend.session_token`.
|
|
91
91
|
advanced: {
|
|
92
92
|
cookiePrefix: cookiePrefix ?? "hogsend",
|
|
93
|
+
/**
|
|
94
|
+
* Client-IP resolution for better-auth's rate limiter. Without this,
|
|
95
|
+
* better-auth (>=1.6.25) reads only `x-forwarded-for` and — with no
|
|
96
|
+
* `trustedProxies` configured — REFUSES any multi-valued XFF
|
|
97
|
+
* (`getIPFromHeader`: `if (forwardedIps.length !== 1) return null`).
|
|
98
|
+
* Railway's edge always sends two values (`<client>, <edge-hop>`), so on
|
|
99
|
+
* Railway every request resolved to null and ALL callers collapsed onto
|
|
100
|
+
* one shared per-path bucket: ten failed logins by anyone locked the
|
|
101
|
+
* `/sign-in/email` path for everyone (observed live 2026-08-03 — the
|
|
102
|
+
* control plane's mint-credentials retries and a customer's Studio
|
|
103
|
+
* sign-ins 429'd each other).
|
|
104
|
+
*
|
|
105
|
+
* `x-real-ip` goes first because it is single-valued and OVERWRITTEN by
|
|
106
|
+
* Railway's edge (verified empirically 2026-08-03 against a Railway echo
|
|
107
|
+
* service: a spoofed `X-Real-Ip: 7.7.7.7` arrived as the caller's true
|
|
108
|
+
* IP, and a spoofed `X-Forwarded-For: 6.6.6.6` was stripped too) — so a
|
|
109
|
+
* direct client cannot forge it to dodge the limiter. Do NOT add
|
|
110
|
+
* `x-envoy-external-address`: Railway passes it through UNTOUCHED (the
|
|
111
|
+
* spoofed value survived), so trusting it would let anyone mint
|
|
112
|
+
* per-request buckets and bypass rate limiting entirely.
|
|
113
|
+
* `x-forwarded-for` stays as the fallback for non-Railway deploys where
|
|
114
|
+
* a single-hop proxy sends a single-valued XFF.
|
|
115
|
+
*/
|
|
116
|
+
ipAddress: {
|
|
117
|
+
ipAddressHeaders: ["x-real-ip", "x-forwarded-for"],
|
|
118
|
+
},
|
|
93
119
|
},
|
|
94
120
|
...(trustedOrigins && trustedOrigins.length > 0 ? { trustedOrigins } : {}),
|
|
95
121
|
// Passing `secondaryStorage` flips better-auth's rate-limit storage from the
|
|
@@ -50,6 +50,11 @@ const eventRequestSchema = z.object({
|
|
|
50
50
|
userToken: z.string().optional(),
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
+
const suspendedSchema = z.object({
|
|
54
|
+
error: z.literal("ingest_suspended"),
|
|
55
|
+
message: z.string(),
|
|
56
|
+
});
|
|
57
|
+
|
|
53
58
|
const eventResponseSchema = z.object({
|
|
54
59
|
stored: z.boolean(),
|
|
55
60
|
exits: z.array(
|
|
@@ -101,9 +106,24 @@ const eventRoute = createRoute({
|
|
|
101
106
|
description:
|
|
102
107
|
"Publishable key attempted to act on another identity without a verified userToken",
|
|
103
108
|
},
|
|
109
|
+
429: {
|
|
110
|
+
content: { "application/json": { schema: suspendedSchema } },
|
|
111
|
+
description:
|
|
112
|
+
"Event ingest is suspended for this instance (HOGSEND_INGEST_SUSPENDED)",
|
|
113
|
+
},
|
|
104
114
|
},
|
|
105
115
|
});
|
|
106
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The documented soft-block body. Verbatim contract — the Hogsend Cloud
|
|
119
|
+
* dashboard and the SDKs match on `error === "ingest_suspended"`.
|
|
120
|
+
*/
|
|
121
|
+
export const INGEST_SUSPENDED_BODY = {
|
|
122
|
+
error: "ingest_suspended",
|
|
123
|
+
message:
|
|
124
|
+
"Event ingest is suspended for this instance (plan limit reached or billing hold). Already-accepted events are unaffected.",
|
|
125
|
+
} as const;
|
|
126
|
+
|
|
107
127
|
export const eventsRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
108
128
|
eventRoute,
|
|
109
129
|
async (c) => {
|
|
@@ -111,6 +131,15 @@ export const eventsRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
|
111
131
|
c.get("container");
|
|
112
132
|
const body = c.req.valid("json");
|
|
113
133
|
|
|
134
|
+
// Cloud metering soft-block (PRD 06 §Tasks 1). Checked FIRST — before the
|
|
135
|
+
// identity gate, before `ingestEvent` writes `user_events`, before the
|
|
136
|
+
// Hatchet push — so a suspended instance costs the tenant DB nothing. Only
|
|
137
|
+
// this public ingest surface is gated: delivery webhooks, tracking and the
|
|
138
|
+
// compliance/preference routes stay live.
|
|
139
|
+
if (env.HOGSEND_INGEST_SUSPENDED === "true") {
|
|
140
|
+
return c.json(INGEST_SUSPENDED_BODY, 429);
|
|
141
|
+
}
|
|
142
|
+
|
|
114
143
|
const guard = gatePublishableIdentity(c, body, env.BETTER_AUTH_SECRET);
|
|
115
144
|
if (guard) return guard;
|
|
116
145
|
|