Skip to content

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 ​

SPAPOST /nimbus/api/relayPosts the built request. The prefix is configurable.
ValidateNimbusRelayControllerBuilds a RequestRelayData.
Build the requestPendingRequestFactoryUses the Laravel Http client.
AuthorizeAuthorizationHandlerFactoryA created token is revoked after the response.
SendSendPendingRequestActionThe target application handles the request.
Response
Dump and dieDumpAndDieResponseShown in the dump viewer.
Any other responseUsed as it is.
Collect the resultRelayedRequestResponseDataStatus, headers, body, cookies, redirects and download.
Back to the SPARelayResponseResource

Protecting 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.

FeatureHeader or pieceWhat it does
ActorActorHeaderListenerSets the user on a guard for this request only. No session or cookie is written.
Transaction modeTransactionModeListenerOpens a transaction on every database connection and rolls it back when the app terminates.
SandboxSandboxModeListenerFakes mail, notifications, queue or events for one request and reports what was captured.
Time travelTimeTravelListenerMoves the app clock for one request.
Throttle resetResetRateLimiterListener and ResettingThrottleRequestsClears the limiter keys before limits are checked.
InsightsInsightCollectorListener and the collectors in Services/InsightsCollects 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 ​

  1. Add a listener in src/Modules/Magic/Listeners that reads a new header.
  2. Register it in NimbusServiceProvider.
  3. Set the header in PendingRequestFactory from a new field on RequestRelayData.
  4. Add the field to NimbusRelayRequest and the SPA request builder.
  5. Test the listener on its own and the relay with an integration test.