Skip to content

feat: auto-detect realm from request Host header#249

Merged
brendanjryan merged 7 commits intomainfrom
feat/realm-auto-detection
Mar 27, 2026
Merged

feat: auto-detect realm from request Host header#249
brendanjryan merged 7 commits intomainfrom
feat/realm-auto-detection

Conversation

@brendanjryan
Copy link
Copy Markdown
Collaborator

@brendanjryan brendanjryan commented Mar 27, 2026

Summary

Updates realm resolution to be more robust, flowing from

  • explicit
  • env var
  • request hostname
  • hard-coded fallback (with warning)

This fixes a pretty common footgun where devs forget to set their realm and we get poor analytics as a result.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 27, 2026

Open in StackBlitz

npm i https://pkg.pr.new/mppx@249

commit: 84ac545

@brendanjryan brendanjryan force-pushed the feat/realm-auto-detection branch 10 times, most recently from 88f6e2d to e498d70 Compare March 27, 2026 02:24
@brendanjryan brendanjryan requested review from jxom and tmm March 27, 2026 02:25
Comment on lines +161 to +165
if (!realm)
warnOnce(
Warnings.noRealm,
'No realm configured. Will auto-detect from request Host header. Set `realm` in Mppx.create() or MPP_REALM env var for explicit control.',
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only show warning message if auto-detection fails?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel like this message will be noisy for most people otherwise

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair - can remove

@@ -0,0 +1,5 @@
---
'mppx': minor
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patch unless breaking change

Suggested change
'mppx': minor
'mppx': patch

'mppx': minor
---

Auto-detect `realm` from the request `Host` header when not explicitly configured. Resolution order: explicit value → env vars (`MPP_REALM`, `FLY_APP_NAME`, `VERCEL_URL`, etc.) → request URL hostname → `"MPP Payment"` fallback with a one-time warning. Removed the hard-coded `"MPP Payment"` default and deprioritized `HOST`/`HOSTNAME` env vars in favor of platform-specific alternatives.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Auto-detect `realm` from the request `Host` header when not explicitly configured. Resolution order: explicit value → env vars (`MPP_REALM`, `FLY_APP_NAME`, `VERCEL_URL`, etc.) → request URL hostname → `"MPP Payment"` fallback with a one-time warning. Removed the hard-coded `"MPP Payment"` default and deprioritized `HOST`/`HOSTNAME` env vars in favor of platform-specific alternatives.
Added `realm` auto-detectection from the request `Host` header when not explicitly configured. Resolution order: explicit value → env vars (`MPP_REALM`, `FLY_APP_NAME`, `VERCEL_URL`, etc.) → request URL hostname → `"MPP Payment"` fallback with a one-time warning. Removed the hard-coded `"MPP Payment"` default and deprioritized `HOST`/`HOSTNAME` env vars in favor of platform-specific alternatives.

3-tier realm resolution: explicit > env var > request hostname.
Removes hardcoded 'MPP Payment' fallback. When no realm is configured,
derives it from the incoming request's URL hostname at charge-time.
Throws if realm cannot be determined (no URL or non-HTTP URL).

This enables Cloudflare Workers (and any runtime) to automatically
use the correct domain as the realm without explicit configuration.

Also fixes env var precedence: MPP_REALM is now checked first.
Removes HOST/HOSTNAME from realm detection (unsafe in containers).
Warns once at Mppx.create() time when realm is not explicitly set.
@brendanjryan brendanjryan force-pushed the feat/realm-auto-detection branch from 3397b2d to b2ee629 Compare March 27, 2026 22:09
@brendanjryan brendanjryan merged commit 6188184 into main Mar 27, 2026
9 checks passed
@brendanjryan brendanjryan deleted the feat/realm-auto-detection branch March 27, 2026 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants