Request relay
The SPA never calls the target API directly. It posts the request to the relay endpoint, and the package sends it. This gives Nimbus access to HttpOnly cookies, the app key and the Laravel request life cycle.
The flow
POST /nimbus/api/relayPosts the built request. The prefix is configurable.NimbusRelayControllerBuilds a RequestRelayData.PendingRequestFactoryUses the Laravel Http client.AuthorizationHandlerFactoryA created token is revoked after the response.SendPendingRequestActionThe target application handles the request.DumpAndDieResponseShown in the dump viewer.RelayedRequestResponseDataStatus, headers, body, cookies, redirects and download.RelayResponseResourceProtecting the API routes
The relay can run a request as any user, so a page on another origin must not be able to call it through the browser of the developer. Every route in routes/api.php runs the RejectCrossSiteRequests middleware. It answers 403 when the Sec-Fetch-Site header is cross-site, when the Origin header does not match the request host, or when the X-Nimbus-Client header is missing. A browser does not let a cross-origin form add that header, so the SPA sends X-Nimbus-Client: web on every call. A script that calls these routes with curl must send the header too. The name lives in NimbusHeaderEnum::Client.
Why two servers locally
The relay calls the target with HTTP. The PHP built-in server handles one request at a time, so a relay to itself waits forever. The controller detects this and tells the user to set a second base_url. The workbench runs on ports 8000 and 8001 for the same reason. See local setup.
Magic features
Magic features work by headers. The relay adds a header, and a listener in the host app reads it. The listeners are registered in NimbusServiceProvider on the RouteMatched event, so they run before route middleware.
| Feature | Header or piece | What it does |
|---|---|---|
| Actor | ActorHeaderListener | Sets the user on a guard for this request only. No session or cookie is written. |
| Transaction mode | TransactionModeListener | Opens a transaction on every database connection and rolls it back when the app terminates. |
| Sandbox | SandboxModeListener | Fakes mail, notifications, queue or events for one request and reports what was captured. |
| Time travel | TimeTravelListener | Moves the app clock for one request. |
| Throttle reset | ResetRateLimiterListener and ResettingThrottleRequests | Clears the limiter keys before limits are checked. |
| Insights | InsightCollectorListener and the collectors in Services/Insights | Collects queries, logs, cache, events, jobs, mail, notifications, gates, HTTP calls and exceptions for the request. |
The relay pulls the insight with PullInsightAction after the response. Insights come from the current host only, because other hosts do not run Nimbus.
Header names live in NimbusHeaderEnum and in the listener constants. Keep the relay and the listener in sync when you add a feature.
Adding a magic feature
- Add a listener in
src/Modules/Magic/Listenersthat reads a new header. - Register it in
NimbusServiceProvider. - Set the header in
PendingRequestFactoryfrom a new field onRequestRelayData. - Add the field to
NimbusRelayRequestand the SPA request builder. - Test the listener on its own and the relay with an integration test.